aboutsummaryrefslogtreecommitdiff
path: root/src/input_common/input_engine.cpp
blob: 91aa96aa7300c84a08c13befe79fff3fcb929a35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/logging/log.h"
#include "input_common/input_engine.h"

namespace InputCommon {

void InputEngine::PreSetController(const PadIdentifier& identifier) {
    std::scoped_lock lock{mutex};
    controller_list.try_emplace(identifier);
}

void InputEngine::PreSetButton(const PadIdentifier& identifier, int button) {
    std::scoped_lock lock{mutex};
    ControllerData& controller = controller_list.at(identifier);
    controller.buttons.try_emplace(button, false);
}

void InputEngine::PreSetHatButton(const PadIdentifier& identifier, int button) {
    std::scoped_lock lock{mutex};
    ControllerData& controller = controller_list.at(identifier);
    controller.hat_buttons.try_emplace(button, u8{0});
}

void InputEngine::PreSetAxis(const PadIdentifier& identifier, int axis) {
    std::scoped_lock lock{mutex};
    ControllerData& controller = controller_list.at(identifier);
    controller.axes.try_emplace(axis, 0.0f);
}

void InputEngine::PreSetMotion(const PadIdentifier& identifier, int motion) {
    std::scoped_lock lock{mutex};
    ControllerData& controller = controller_list.at(identifier);
    controller.motions.try_emplace(motion);
}

void InputEngine::SetButton(const PadIdentifier& identifier, int button, bool value) {
    {
        std::scoped_lock lock{mutex};
        ControllerData& controller = controller_list.at(identifier);
        if (!configuring) {
            controller.buttons.insert_or_assign(button, value);
        }
    }
    TriggerOnButtonChange(identifier, button, value);
}

void InputEngine::SetHatButton(const PadIdentifier& identifier, int button, u8 value) {
    {
        std::scoped_lock lock{mutex};
        ControllerData& controller = controller_list.at(identifier);
        if (!configuring) {
            controller.hat_buttons.insert_or_assign(button, value);
        }
    }
    TriggerOnHatButtonChange(identifier, button, value);
}

void InputEngine::SetAxis(const PadIdentifier& identifier, int axis, f32 value) {
    {
        std::scoped_lock lock{mutex};
        ControllerData& controller = controller_list.at(identifier);
        if (!configuring) {
            controller.axes.insert_or_assign(axis, value);
        }
    }
    TriggerOnAxisChange(identifier, axis, value);
}

void InputEngine::SetBattery(const PadIdentifier& identifier, Common::Input::BatteryLevel value) {
    {
        std::scoped_lock lock{mutex};
        ControllerData& controller = controller_list.at(identifier);
        if (!configuring) {
            controller.battery = value;
        }
    }
    TriggerOnBatteryChange(identifier, value);
}

void InputEngine::SetColor(const PadIdentifier& identifier, Common::Input::BodyColorStatus value) {
    {
        std::scoped_lock lock{mutex};
        ControllerData& controller = controller_list.at(identifier);
        if (!configuring) {
            controller.color = value;
        }
    }
    TriggerOnColorChange(identifier, value);
}

void InputEngine::SetMotion(const PadIdentifier& identifier, int motion, const BasicMotion& value) {
    {
        std::scoped_lock lock{mutex};
        ControllerData& controller = controller_list.at(identifier);
        if (!configuring) {
            controller.motions.insert_or_assign(motion, value);
        }
    }
    TriggerOnMotionChange(identifier, motion, value);
}

void InputEngine::SetCamera(const PadIdentifier& identifier,
                            const Common::Input::CameraStatus& value) {
    {
        std::scoped_lock lock{mutex};
        ControllerData& controller = controller_list.at(identifier);
        if (!configuring) {
            controller.camera = value;
        }
    }
    TriggerOnCameraChange(identifier, value);
}

void InputEngine::SetNfc(const PadIdentifier& identifier, const Common::Input::NfcStatus& value) {
    {
        std::scoped_lock lock{mutex};
        ControllerData& controller = controller_list.at(identifier);
        if (!configuring) {
            controller.nfc = value;
        }
    }
    TriggerOnNfcChange(identifier, value);
}

bool InputEngine::GetButton(const PadIdentifier& identifier, int button) const {
    std::scoped_lock lock{mutex};
    const auto controller_iter = controller_list.find(identifier);
    if (controller_iter == controller_list.cend()) {
        LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
                  identifier.pad, identifier.port);
        return false;
    }
    const ControllerData& controller = controller_iter->second;
    const auto button_iter = controller.buttons.find(button);
    if (button_iter == controller.buttons.cend()) {
        LOG_ERROR(Input, "Invalid button {}", button);
        return false;
    }
    return button_iter->second;
}

bool InputEngine::GetHatButton(const PadIdentifier& identifier, int button, u8 direction) const {
    std::scoped_lock lock{mutex};
    const auto controller_iter = controller_list.find(identifier);
    if (controller_iter == controller_list.cend()) {
        LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
                  identifier.pad, identifier.port);
        return false;
    }
    const ControllerData& controller = controller_iter->second;
    const auto hat_iter = controller.hat_buttons.find(button);
    if (hat_iter == controller.hat_buttons.cend()) {
        LOG_ERROR(Input, "Invalid hat button {}", button);
        return false;
    }
    return (hat_iter->second & direction) != 0;
}

f32 InputEngine::GetAxis(const PadIdentifier& identifier, int axis) const {
    std::scoped_lock lock{mutex};
    const auto controller_iter = controller_list.find(identifier);
    if (controller_iter == controller_list.cend()) {
        LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
                  identifier.pad, identifier.port);
        return 0.0f;
    }
    const ControllerData& controller = controller_iter->second;
    const auto axis_iter = controller.axes.find(axis);
    if (axis_iter == controller.axes.cend()) {
        LOG_ERROR(Input, "Invalid axis {}", axis);
        return 0.0f;
    }
    return axis_iter->second;
}

Common::Input::BatteryLevel InputEngine::GetBattery(const PadIdentifier& identifier) const {
    std::scoped_lock lock{mutex};
    const auto controller_iter = controller_list.find(identifier);
    if (controller_iter == controller_list.cend()) {
        LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
                  identifier.pad, identifier.port);
        return Common::Input::BatteryLevel::Charging;
    }
    const ControllerData& controller = controller_iter->second;
    return controller.battery;
}

Common::Input::BodyColorStatus InputEngine::GetColor(const PadIdentifier& identifier) const {
    std::scoped_lock lock{mutex};
    const auto controller_iter = controller_list.find(identifier);
    if (controller_iter == controller_list.cend()) {
        LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
                  identifier.pad, identifier.port);
        return {};
    }
    const ControllerData& controller = controller_iter->second;
    return controller.color;
}

BasicMotion InputEngine::GetMotion(const PadIdentifier& identifier, int motion) const {
    std::scoped_lock lock{mutex};
    const auto controller_iter = controller_list.find(identifier);
    if (controller_iter == controller_list.cend()) {
        LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
                  identifier.pad, identifier.port);
        return {};
    }
    const ControllerData& controller = controller_iter->second;
    return controller.motions.at(motion);
}

Common::Input::CameraStatus InputEngine::GetCamera(const PadIdentifier& identifier) const {
    std::scoped_lock lock{mutex};
    const auto controller_iter = controller_list.find(identifier);
    if (controller_iter == controller_list.cend()) {
        LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
                  identifier.pad, identifier.port);
        return {};
    }
    const ControllerData& controller = controller_iter->second;
    return controller.camera;
}

Common::Input::NfcStatus InputEngine::GetNfc(const PadIdentifier& identifier) const {
    std::scoped_lock lock{mutex};
    const auto controller_iter = controller_list.find(identifier);
    if (controller_iter == controller_list.cend()) {
        LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
                  identifier.pad, identifier.port);
        return {};
    }
    const ControllerData& controller = controller_iter->second;
    return controller.nfc;
}

void InputEngine::ResetButtonState() {
    for (const auto& controller : controller_list) {
        for (const auto& button : controller.second.buttons) {
            SetButton(controller.first, button.first, false);
        }
        for (const auto& button : controller.second.hat_buttons) {
            SetHatButton(controller.first, button.first, 0);
        }
    }
}

void InputEngine::ResetAnalogState() {
    for (const auto& controller : controller_list) {
        for (const auto& axis : controller.second.axes) {
            SetAxis(controller.first, axis.first, 0.0);
        }
    }
}

void InputEngine::TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value) {
    std::scoped_lock lock{mutex_callback};
    for (const auto& poller_pair : callback_list) {
        const InputIdentifier& poller = poller_pair.second;
        if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Button, button)) {
            continue;
        }
        if (poller.callback.on_change) {
            poller.callback.on_change();
        }
    }
    if (!configuring || !mapping_callback.on_data) {
        return;
    }

    PreSetButton(identifier, button);
    if (value == GetButton(identifier, button)) {
        return;
    }
    mapping_callback.on_data(MappingData{
        .engine = GetEngineName(),
        .pad = identifier,
        .type = EngineInputType::Button,
        .index = button,
        .button_value = value,
    });
}

void InputEngine::TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value) {
    std::scoped_lock lock{mutex_callback};
    for (const auto& poller_pair : callback_list) {
        const InputIdentifier& poller = poller_pair.second;
        if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::HatButton, button)) {
            continue;
        }
        if (poller.callback.on_change) {
            poller.callback.on_change();
        }
    }
    if (!configuring || !mapping_callback.on_data) {
        return;
    }
    for (std::size_t index = 1; index < 0xff; index <<= 1) {
        bool button_value = (value & index) != 0;
        if (button_value == GetHatButton(identifier, button, static_cast<u8>(index))) {
            continue;
        }
        mapping_callback.on_data(MappingData{
            .engine = GetEngineName(),
            .pad = identifier,
            .type = EngineInputType::HatButton,
            .index = button,
            .hat_name = GetHatButtonName(static_cast<u8>(index)),
        });
    }
}

void InputEngine::TriggerOnAxisChange(const PadIdentifier& identifier, int axis, f32 value) {
    std::scoped_lock lock{mutex_callback};
    for (const auto& poller_pair : callback_list) {
        const InputIdentifier& poller = poller_pair.second;
        if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Analog, axis)) {
            continue;
        }
        if (poller.callback.on_change) {
            poller.callback.on_change();
        }
    }
    if (!configuring || !mapping_callback.on_data) {
        return;
    }
    if (std::abs(value - GetAxis(identifier, axis)) < 0.5f) {
        return;
    }
    mapping_callback.on_data(MappingData{
        .engine = GetEngineName(),
        .pad = identifier,
        .type = EngineInputType::Analog,
        .index = axis,
        .axis_value = value,
    });
}

void InputEngine::TriggerOnBatteryChange(const PadIdentifier& identifier,
                                         [[maybe_unused]] Common::Input::BatteryLevel value) {
    std::scoped_lock lock{mutex_callback};
    for (const auto& poller_pair : callback_list) {
        const InputIdentifier& poller = poller_pair.second;
        if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Battery, 0)) {
            continue;
        }
        if (poller.callback.on_change) {
            poller.callback.on_change();
        }
    }
}

void InputEngine::TriggerOnColorChange(const PadIdentifier& identifier,
                                       [[maybe_unused]] Common::Input::BodyColorStatus value) {
    std::scoped_lock lock{mutex_callback};
    for (const auto& poller_pair : callback_list) {
        const InputIdentifier& poller = poller_pair.second;
        if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Color, 0)) {
            continue;
        }
        if (poller.callback.on_change) {
            poller.callback.on_change();
        }
    }
}

void InputEngine::TriggerOnMotionChange(const PadIdentifier& identifier, int motion,
                                        const BasicMotion& value) {
    std::scoped_lock lock{mutex_callback};
    for (const auto& poller_pair : callback_list) {
        const InputIdentifier& poller = poller_pair.second;
        if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Motion, motion)) {
            continue;
        }
        if (poller.callback.on_change) {
            poller.callback.on_change();
        }
    }
    if (!configuring || !mapping_callback.on_data) {
        return;
    }
    bool is_active = false;
    if (std::abs(value.accel_x) > 1.5f || std::abs(value.accel_y) > 1.5f ||
        std::abs(value.accel_z) > 1.5f) {
        is_active = true;
    }
    if (std::abs(value.gyro_x) > 0.6f || std::abs(value.gyro_y) > 0.6f ||
        std::abs(value.gyro_z) > 0.6f) {
        is_active = true;
    }
    if (!is_active) {
        return;
    }
    mapping_callback.on_data(MappingData{
        .engine = GetEngineName(),
        .pad = identifier,
        .type = EngineInputType::Motion,
        .index = motion,
        .motion_value = value,
    });
}

void InputEngine::TriggerOnCameraChange(const PadIdentifier& identifier,
                                        [[maybe_unused]] const Common::Input::CameraStatus& value) {
    std::scoped_lock lock{mutex_callback};
    for (const auto& poller_pair : callback_list) {
        const InputIdentifier& poller = poller_pair.second;
        if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Camera, 0)) {
            continue;
        }
        if (poller.callback.on_change) {
            poller.callback.on_change();
        }
    }
}

void InputEngine::TriggerOnNfcChange(const PadIdentifier& identifier,
                                     [[maybe_unused]] const Common::Input::NfcStatus& value) {
    std::scoped_lock lock{mutex_callback};
    for (const auto& poller_pair : callback_list) {
        const InputIdentifier& poller = poller_pair.second;
        if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Nfc, 0)) {
            continue;
        }
        if (poller.callback.on_change) {
            poller.callback.on_change();
        }
    }
}

bool InputEngine::IsInputIdentifierEqual(const InputIdentifier& input_identifier,
                                         const PadIdentifier& identifier, EngineInputType type,
                                         int index) const {
    if (input_identifier.type != type) {
        return false;
    }
    if (input_identifier.index != index) {
        return false;
    }
    if (input_identifier.identifier != identifier) {
        return false;
    }
    return true;
}

void InputEngine::BeginConfiguration() {
    configuring = true;
}

void InputEngine::EndConfiguration() {
    configuring = false;
}

const std::string& InputEngine::GetEngineName() const {
    return input_engine;
}

int InputEngine::SetCallback(InputIdentifier input_identifier) {
    std::scoped_lock lock{mutex_callback};
    callback_list.insert_or_assign(last_callback_key, std::move(input_identifier));
    return last_callback_key++;
}

void InputEngine::SetMappingCallback(MappingCallback callback) {
    std::scoped_lock lock{mutex_callback};
    mapping_callback = std::move(callback);
}

void InputEngine::DeleteCallback(int key) {
    std::scoped_lock lock{mutex_callback};
    const auto& iterator = callback_list.find(key);
    if (iterator == callback_list.end()) {
        LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);
        return;
    }
    callback_list.erase(iterator);
}

} // namespace InputCommon