summaryrefslogtreecommitdiffstats
path: root/src/input_common/main.cpp
blob: c77fc04ee6bfd54aed51fb924eed16ce1e22c2c6 (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
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <memory>
#include "common/input.h"
#include "common/param_package.h"
#include "input_common/drivers/camera.h"
#include "input_common/drivers/keyboard.h"
#include "input_common/drivers/mouse.h"
#include "input_common/drivers/tas_input.h"
#include "input_common/drivers/touch_screen.h"
#include "input_common/drivers/udp_client.h"
#include "input_common/drivers/virtual_amiibo.h"
#include "input_common/drivers/virtual_gamepad.h"
#include "input_common/helpers/stick_from_buttons.h"
#include "input_common/helpers/touch_from_buttons.h"
#include "input_common/input_engine.h"
#include "input_common/input_mapping.h"
#include "input_common/input_poller.h"
#include "input_common/main.h"

#ifdef HAVE_LIBUSB
#include "input_common/drivers/gc_adapter.h"
#endif
#ifdef HAVE_SDL2
#include "input_common/drivers/joycon.h"
#include "input_common/drivers/sdl_driver.h"
#endif

namespace InputCommon {

/// Dummy engine to get periodic updates
class UpdateEngine final : public InputEngine {
public:
    explicit UpdateEngine(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
        PreSetController(identifier);
    }

    void PumpEvents() {
        SetButton(identifier, 0, last_state);
        last_state = !last_state;
    }

private:
    static constexpr PadIdentifier identifier = {
        .guid = Common::UUID{},
        .port = 0,
        .pad = 0,
    };

    bool last_state{};
};

struct InputSubsystem::Impl {
    template <typename Engine>
    void RegisterEngine(std::string name, std::shared_ptr<Engine>& engine) {
        MappingCallback mapping_callback{[this](const MappingData& data) { RegisterInput(data); }};

        engine = std::make_shared<Engine>(name);
        engine->SetMappingCallback(mapping_callback);

        std::shared_ptr<InputFactory> input_factory = std::make_shared<InputFactory>(engine);
        std::shared_ptr<OutputFactory> output_factory = std::make_shared<OutputFactory>(engine);
        Common::Input::RegisterInputFactory(engine->GetEngineName(), std::move(input_factory));
        Common::Input::RegisterOutputFactory(engine->GetEngineName(), std::move(output_factory));
    }

    void Initialize() {
        mapping_factory = std::make_shared<MappingFactory>();

        RegisterEngine("updater", update_engine);
        RegisterEngine("keyboard", keyboard);
        RegisterEngine("mouse", mouse);
        RegisterEngine("touch", touch_screen);
#ifdef HAVE_LIBUSB
        RegisterEngine("gcpad", gcadapter);
#endif
        RegisterEngine("cemuhookudp", udp_client);
        RegisterEngine("tas", tas_input);
        RegisterEngine("camera", camera);
        RegisterEngine("virtual_amiibo", virtual_amiibo);
        RegisterEngine("virtual_gamepad", virtual_gamepad);
#ifdef HAVE_SDL2
        RegisterEngine("sdl", sdl);
        RegisterEngine("joycon", joycon);
#endif

        Common::Input::RegisterInputFactory("touch_from_button",
                                            std::make_shared<TouchFromButton>());
        Common::Input::RegisterInputFactory("analog_from_button",
                                            std::make_shared<StickFromButton>());
    }

    template <typename Engine>
    void UnregisterEngine(std::shared_ptr<Engine>& engine) {
        Common::Input::UnregisterInputFactory(engine->GetEngineName());
        Common::Input::UnregisterOutputFactory(engine->GetEngineName());
        engine.reset();
    }

    void Shutdown() {
        UnregisterEngine(update_engine);
        UnregisterEngine(keyboard);
        UnregisterEngine(mouse);
        UnregisterEngine(touch_screen);
#ifdef HAVE_LIBUSB
        UnregisterEngine(gcadapter);
#endif
        UnregisterEngine(udp_client);
        UnregisterEngine(tas_input);
        UnregisterEngine(camera);
        UnregisterEngine(virtual_amiibo);
        UnregisterEngine(virtual_gamepad);
#ifdef HAVE_SDL2
        UnregisterEngine(sdl);
        UnregisterEngine(joycon);
#endif

        Common::Input::UnregisterInputFactory("touch_from_button");
        Common::Input::UnregisterInputFactory("analog_from_button");
    }

    [[nodiscard]] std::vector<Common::ParamPackage> GetInputDevices() const {
        std::vector<Common::ParamPackage> devices = {
            Common::ParamPackage{{"display", "Any"}, {"engine", "any"}},
        };

        auto keyboard_devices = keyboard->GetInputDevices();
        devices.insert(devices.end(), keyboard_devices.begin(), keyboard_devices.end());
        auto mouse_devices = mouse->GetInputDevices();
        devices.insert(devices.end(), mouse_devices.begin(), mouse_devices.end());
#ifdef HAVE_LIBUSB
        auto gcadapter_devices = gcadapter->GetInputDevices();
        devices.insert(devices.end(), gcadapter_devices.begin(), gcadapter_devices.end());
#endif
        auto udp_devices = udp_client->GetInputDevices();
        devices.insert(devices.end(), udp_devices.begin(), udp_devices.end());
#ifdef HAVE_SDL2
        auto joycon_devices = joycon->GetInputDevices();
        devices.insert(devices.end(), joycon_devices.begin(), joycon_devices.end());
        auto sdl_devices = sdl->GetInputDevices();
        devices.insert(devices.end(), sdl_devices.begin(), sdl_devices.end());
#endif

        return devices;
    }

    [[nodiscard]] std::shared_ptr<InputEngine> GetInputEngine(
        const Common::ParamPackage& params) const {
        if (!params.Has("engine") || params.Get("engine", "") == "any") {
            return nullptr;
        }
        const std::string engine = params.Get("engine", "");
        if (engine == keyboard->GetEngineName()) {
            return keyboard;
        }
        if (engine == mouse->GetEngineName()) {
            return mouse;
        }
#ifdef HAVE_LIBUSB
        if (engine == gcadapter->GetEngineName()) {
            return gcadapter;
        }
#endif
        if (engine == udp_client->GetEngineName()) {
            return udp_client;
        }
#ifdef HAVE_SDL2
        if (engine == sdl->GetEngineName()) {
            return sdl;
        }
        if (engine == joycon->GetEngineName()) {
            return joycon;
        }
#endif
        return nullptr;
    }

    [[nodiscard]] AnalogMapping GetAnalogMappingForDevice(
        const Common::ParamPackage& params) const {
        const auto input_engine = GetInputEngine(params);

        if (input_engine == nullptr) {
            return {};
        }

        return input_engine->GetAnalogMappingForDevice(params);
    }

    [[nodiscard]] ButtonMapping GetButtonMappingForDevice(
        const Common::ParamPackage& params) const {
        const auto input_engine = GetInputEngine(params);

        if (input_engine == nullptr) {
            return {};
        }

        return input_engine->GetButtonMappingForDevice(params);
    }

    [[nodiscard]] MotionMapping GetMotionMappingForDevice(
        const Common::ParamPackage& params) const {
        const auto input_engine = GetInputEngine(params);

        if (input_engine == nullptr) {
            return {};
        }

        return input_engine->GetMotionMappingForDevice(params);
    }

    Common::Input::ButtonNames GetButtonName(const Common::ParamPackage& params) const {
        if (!params.Has("engine") || params.Get("engine", "") == "any") {
            return Common::Input::ButtonNames::Undefined;
        }
        const auto input_engine = GetInputEngine(params);

        if (input_engine == nullptr) {
            return Common::Input::ButtonNames::Invalid;
        }

        return input_engine->GetUIName(params);
    }

    bool IsStickInverted(const Common::ParamPackage& params) {
        const auto input_engine = GetInputEngine(params);

        if (input_engine == nullptr) {
            return false;
        }

        return input_engine->IsStickInverted(params);
    }

    bool IsController(const Common::ParamPackage& params) {
        const std::string engine = params.Get("engine", "");
        if (engine == mouse->GetEngineName()) {
            return true;
        }
#ifdef HAVE_LIBUSB
        if (engine == gcadapter->GetEngineName()) {
            return true;
        }
#endif
        if (engine == udp_client->GetEngineName()) {
            return true;
        }
        if (engine == tas_input->GetEngineName()) {
            return true;
        }
        if (engine == virtual_gamepad->GetEngineName()) {
            return true;
        }
#ifdef HAVE_SDL2
        if (engine == sdl->GetEngineName()) {
            return true;
        }
        if (engine == joycon->GetEngineName()) {
            return true;
        }
#endif
        return false;
    }

    void BeginConfiguration() {
        keyboard->BeginConfiguration();
        mouse->BeginConfiguration();
#ifdef HAVE_LIBUSB
        gcadapter->BeginConfiguration();
#endif
        udp_client->BeginConfiguration();
#ifdef HAVE_SDL2
        sdl->BeginConfiguration();
        joycon->BeginConfiguration();
#endif
    }

    void EndConfiguration() {
        keyboard->EndConfiguration();
        mouse->EndConfiguration();
#ifdef HAVE_LIBUSB
        gcadapter->EndConfiguration();
#endif
        udp_client->EndConfiguration();
#ifdef HAVE_SDL2
        sdl->EndConfiguration();
        joycon->EndConfiguration();
#endif
    }

    void PumpEvents() const {
        update_engine->PumpEvents();
#ifdef HAVE_SDL2
        sdl->PumpEvents();
#endif
    }

    void RegisterInput(const MappingData& data) {
        mapping_factory->RegisterInput(data);
    }

    std::shared_ptr<MappingFactory> mapping_factory;

    std::shared_ptr<UpdateEngine> update_engine;
    std::shared_ptr<Keyboard> keyboard;
    std::shared_ptr<Mouse> mouse;
    std::shared_ptr<TouchScreen> touch_screen;
    std::shared_ptr<TasInput::Tas> tas_input;
    std::shared_ptr<CemuhookUDP::UDPClient> udp_client;
    std::shared_ptr<Camera> camera;
    std::shared_ptr<VirtualAmiibo> virtual_amiibo;
    std::shared_ptr<VirtualGamepad> virtual_gamepad;

#ifdef HAVE_LIBUSB
    std::shared_ptr<GCAdapter> gcadapter;
#endif

#ifdef HAVE_SDL2
    std::shared_ptr<SDLDriver> sdl;
    std::shared_ptr<Joycons> joycon;
#endif
};

InputSubsystem::InputSubsystem() : impl{std::make_unique<Impl>()} {}

InputSubsystem::~InputSubsystem() = default;

void InputSubsystem::Initialize() {
    impl->Initialize();
}

void InputSubsystem::Shutdown() {
    impl->Shutdown();
}

Keyboard* InputSubsystem::GetKeyboard() {
    return impl->keyboard.get();
}

const Keyboard* InputSubsystem::GetKeyboard() const {
    return impl->keyboard.get();
}

Mouse* InputSubsystem::GetMouse() {
    return impl->mouse.get();
}

const Mouse* InputSubsystem::GetMouse() const {
    return impl->mouse.get();
}

TouchScreen* InputSubsystem::GetTouchScreen() {
    return impl->touch_screen.get();
}

const TouchScreen* InputSubsystem::GetTouchScreen() const {
    return impl->touch_screen.get();
}

TasInput::Tas* InputSubsystem::GetTas() {
    return impl->tas_input.get();
}

const TasInput::Tas* InputSubsystem::GetTas() const {
    return impl->tas_input.get();
}

Camera* InputSubsystem::GetCamera() {
    return impl->camera.get();
}

const Camera* InputSubsystem::GetCamera() const {
    return impl->camera.get();
}

VirtualAmiibo* InputSubsystem::GetVirtualAmiibo() {
    return impl->virtual_amiibo.get();
}

const VirtualAmiibo* InputSubsystem::GetVirtualAmiibo() const {
    return impl->virtual_amiibo.get();
}

VirtualGamepad* InputSubsystem::GetVirtualGamepad() {
    return impl->virtual_gamepad.get();
}

const VirtualGamepad* InputSubsystem::GetVirtualGamepad() const {
    return impl->virtual_gamepad.get();
}

std::vector<Common::ParamPackage> InputSubsystem::GetInputDevices() const {
    return impl->GetInputDevices();
}

AnalogMapping InputSubsystem::GetAnalogMappingForDevice(const Common::ParamPackage& device) const {
    return impl->GetAnalogMappingForDevice(device);
}

ButtonMapping InputSubsystem::GetButtonMappingForDevice(const Common::ParamPackage& device) const {
    return impl->GetButtonMappingForDevice(device);
}

MotionMapping InputSubsystem::GetMotionMappingForDevice(const Common::ParamPackage& device) const {
    return impl->GetMotionMappingForDevice(device);
}

Common::Input::ButtonNames InputSubsystem::GetButtonName(const Common::ParamPackage& params) const {
    return impl->GetButtonName(params);
}

bool InputSubsystem::IsController(const Common::ParamPackage& params) const {
    return impl->IsController(params);
}

bool InputSubsystem::IsStickInverted(const Common::ParamPackage& params) const {
    if (params.Has("axis_x") && params.Has("axis_y")) {
        return impl->IsStickInverted(params);
    }
    return false;
}

void InputSubsystem::ReloadInputDevices() {
    impl->udp_client.get()->ReloadSockets();
}

void InputSubsystem::BeginMapping(Polling::InputType type) {
    impl->BeginConfiguration();
    impl->mapping_factory->BeginMapping(type);
}

Common::ParamPackage InputSubsystem::GetNextInput() const {
    return impl->mapping_factory->GetNextInput();
}

void InputSubsystem::StopMapping() const {
    impl->EndConfiguration();
    impl->mapping_factory->StopMapping();
}

void InputSubsystem::PumpEvents() const {
    impl->PumpEvents();
}

std::string GenerateKeyboardParam(int key_code) {
    Common::ParamPackage param;
    param.Set("engine", "keyboard");
    param.Set("code", key_code);
    param.Set("toggle", false);
    return param.Serialize();
}

std::string GenerateAnalogParamFromKeys(int key_up, int key_down, int key_left, int key_right,
                                        int key_modifier, float modifier_scale) {
    Common::ParamPackage circle_pad_param{
        {"engine", "analog_from_button"},
        {"up", GenerateKeyboardParam(key_up)},
        {"down", GenerateKeyboardParam(key_down)},
        {"left", GenerateKeyboardParam(key_left)},
        {"right", GenerateKeyboardParam(key_right)},
        {"modifier", GenerateKeyboardParam(key_modifier)},
        {"modifier_scale", std::to_string(modifier_scale)},
    };
    return circle_pad_param.Serialize();
}
} // namespace InputCommon