diff options
Diffstat (limited to 'src/input_common/main.cpp')
| -rw-r--r-- | src/input_common/main.cpp | 212 |
1 files changed, 154 insertions, 58 deletions
diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp index b9d5d0ec3..ea1a1cee6 100644 --- a/src/input_common/main.cpp +++ b/src/input_common/main.cpp | |||
| @@ -11,6 +11,7 @@ | |||
| 11 | #include "input_common/keyboard.h" | 11 | #include "input_common/keyboard.h" |
| 12 | #include "input_common/main.h" | 12 | #include "input_common/main.h" |
| 13 | #include "input_common/motion_emu.h" | 13 | #include "input_common/motion_emu.h" |
| 14 | #include "input_common/touch_from_button.h" | ||
| 14 | #include "input_common/udp/udp.h" | 15 | #include "input_common/udp/udp.h" |
| 15 | #ifdef HAVE_SDL2 | 16 | #ifdef HAVE_SDL2 |
| 16 | #include "input_common/sdl/sdl.h" | 17 | #include "input_common/sdl/sdl.h" |
| @@ -18,67 +19,176 @@ | |||
| 18 | 19 | ||
| 19 | namespace InputCommon { | 20 | namespace InputCommon { |
| 20 | 21 | ||
| 21 | static std::shared_ptr<Keyboard> keyboard; | 22 | struct InputSubsystem::Impl { |
| 22 | static std::shared_ptr<MotionEmu> motion_emu; | 23 | void Initialize() { |
| 24 | auto gcadapter = std::make_shared<GCAdapter::Adapter>(); | ||
| 25 | gcbuttons = std::make_shared<GCButtonFactory>(gcadapter); | ||
| 26 | Input::RegisterFactory<Input::ButtonDevice>("gcpad", gcbuttons); | ||
| 27 | gcanalog = std::make_shared<GCAnalogFactory>(gcadapter); | ||
| 28 | Input::RegisterFactory<Input::AnalogDevice>("gcpad", gcanalog); | ||
| 29 | |||
| 30 | keyboard = std::make_shared<Keyboard>(); | ||
| 31 | Input::RegisterFactory<Input::ButtonDevice>("keyboard", keyboard); | ||
| 32 | Input::RegisterFactory<Input::AnalogDevice>("analog_from_button", | ||
| 33 | std::make_shared<AnalogFromButton>()); | ||
| 34 | motion_emu = std::make_shared<MotionEmu>(); | ||
| 35 | Input::RegisterFactory<Input::MotionDevice>("motion_emu", motion_emu); | ||
| 36 | Input::RegisterFactory<Input::TouchDevice>("touch_from_button", | ||
| 37 | std::make_shared<TouchFromButtonFactory>()); | ||
| 38 | |||
| 23 | #ifdef HAVE_SDL2 | 39 | #ifdef HAVE_SDL2 |
| 24 | static std::unique_ptr<SDL::State> sdl; | 40 | sdl = SDL::Init(); |
| 25 | #endif | 41 | #endif |
| 26 | static std::unique_ptr<CemuhookUDP::State> udp; | ||
| 27 | static std::shared_ptr<GCButtonFactory> gcbuttons; | ||
| 28 | static std::shared_ptr<GCAnalogFactory> gcanalog; | ||
| 29 | |||
| 30 | void Init() { | ||
| 31 | auto gcadapter = std::make_shared<GCAdapter::Adapter>(); | ||
| 32 | gcbuttons = std::make_shared<GCButtonFactory>(gcadapter); | ||
| 33 | Input::RegisterFactory<Input::ButtonDevice>("gcpad", gcbuttons); | ||
| 34 | gcanalog = std::make_shared<GCAnalogFactory>(gcadapter); | ||
| 35 | Input::RegisterFactory<Input::AnalogDevice>("gcpad", gcanalog); | ||
| 36 | |||
| 37 | keyboard = std::make_shared<Keyboard>(); | ||
| 38 | Input::RegisterFactory<Input::ButtonDevice>("keyboard", keyboard); | ||
| 39 | Input::RegisterFactory<Input::AnalogDevice>("analog_from_button", | ||
| 40 | std::make_shared<AnalogFromButton>()); | ||
| 41 | motion_emu = std::make_shared<MotionEmu>(); | ||
| 42 | Input::RegisterFactory<Input::MotionDevice>("motion_emu", motion_emu); | ||
| 43 | 42 | ||
| 43 | udp = CemuhookUDP::Init(); | ||
| 44 | } | ||
| 45 | |||
| 46 | void Shutdown() { | ||
| 47 | Input::UnregisterFactory<Input::ButtonDevice>("keyboard"); | ||
| 48 | keyboard.reset(); | ||
| 49 | Input::UnregisterFactory<Input::AnalogDevice>("analog_from_button"); | ||
| 50 | Input::UnregisterFactory<Input::MotionDevice>("motion_emu"); | ||
| 51 | motion_emu.reset(); | ||
| 52 | Input::UnregisterFactory<Input::TouchDevice>("touch_from_button"); | ||
| 44 | #ifdef HAVE_SDL2 | 53 | #ifdef HAVE_SDL2 |
| 45 | sdl = SDL::Init(); | 54 | sdl.reset(); |
| 46 | #endif | 55 | #endif |
| 56 | udp.reset(); | ||
| 57 | Input::UnregisterFactory<Input::ButtonDevice>("gcpad"); | ||
| 58 | Input::UnregisterFactory<Input::AnalogDevice>("gcpad"); | ||
| 59 | |||
| 60 | gcbuttons.reset(); | ||
| 61 | gcanalog.reset(); | ||
| 62 | } | ||
| 63 | |||
| 64 | [[nodiscard]] std::vector<Common::ParamPackage> GetInputDevices() const { | ||
| 65 | std::vector<Common::ParamPackage> devices = { | ||
| 66 | Common::ParamPackage{{"display", "Any"}, {"class", "any"}}, | ||
| 67 | Common::ParamPackage{{"display", "Keyboard/Mouse"}, {"class", "key"}}, | ||
| 68 | }; | ||
| 69 | #ifdef HAVE_SDL2 | ||
| 70 | auto sdl_devices = sdl->GetInputDevices(); | ||
| 71 | devices.insert(devices.end(), sdl_devices.begin(), sdl_devices.end()); | ||
| 72 | #endif | ||
| 73 | auto udp_devices = udp->GetInputDevices(); | ||
| 74 | devices.insert(devices.end(), udp_devices.begin(), udp_devices.end()); | ||
| 75 | return devices; | ||
| 76 | } | ||
| 77 | |||
| 78 | [[nodiscard]] AnalogMapping GetAnalogMappingForDevice( | ||
| 79 | const Common::ParamPackage& params) const { | ||
| 80 | if (!params.Has("class") || params.Get("class", "") == "any") { | ||
| 81 | return {}; | ||
| 82 | } | ||
| 83 | if (params.Get("class", "") == "key") { | ||
| 84 | // TODO consider returning the SDL key codes for the default keybindings | ||
| 85 | return {}; | ||
| 86 | } | ||
| 87 | #ifdef HAVE_SDL2 | ||
| 88 | if (params.Get("class", "") == "sdl") { | ||
| 89 | return sdl->GetAnalogMappingForDevice(params); | ||
| 90 | } | ||
| 91 | #endif | ||
| 92 | return {}; | ||
| 93 | } | ||
| 94 | |||
| 95 | [[nodiscard]] ButtonMapping GetButtonMappingForDevice( | ||
| 96 | const Common::ParamPackage& params) const { | ||
| 97 | if (!params.Has("class") || params.Get("class", "") == "any") { | ||
| 98 | return {}; | ||
| 99 | } | ||
| 100 | if (params.Get("class", "") == "key") { | ||
| 101 | // TODO consider returning the SDL key codes for the default keybindings | ||
| 102 | return {}; | ||
| 103 | } | ||
| 104 | #ifdef HAVE_SDL2 | ||
| 105 | if (params.Get("class", "") == "sdl") { | ||
| 106 | return sdl->GetButtonMappingForDevice(params); | ||
| 107 | } | ||
| 108 | #endif | ||
| 109 | return {}; | ||
| 110 | } | ||
| 47 | 111 | ||
| 48 | udp = CemuhookUDP::Init(); | 112 | std::shared_ptr<Keyboard> keyboard; |
| 49 | } | 113 | std::shared_ptr<MotionEmu> motion_emu; |
| 50 | |||
| 51 | void Shutdown() { | ||
| 52 | Input::UnregisterFactory<Input::ButtonDevice>("keyboard"); | ||
| 53 | keyboard.reset(); | ||
| 54 | Input::UnregisterFactory<Input::AnalogDevice>("analog_from_button"); | ||
| 55 | Input::UnregisterFactory<Input::MotionDevice>("motion_emu"); | ||
| 56 | motion_emu.reset(); | ||
| 57 | #ifdef HAVE_SDL2 | 114 | #ifdef HAVE_SDL2 |
| 58 | sdl.reset(); | 115 | std::unique_ptr<SDL::State> sdl; |
| 59 | #endif | 116 | #endif |
| 60 | udp.reset(); | 117 | std::unique_ptr<CemuhookUDP::State> udp; |
| 61 | Input::UnregisterFactory<Input::ButtonDevice>("gcpad"); | 118 | std::shared_ptr<GCButtonFactory> gcbuttons; |
| 62 | Input::UnregisterFactory<Input::AnalogDevice>("gcpad"); | 119 | std::shared_ptr<GCAnalogFactory> gcanalog; |
| 120 | }; | ||
| 121 | |||
| 122 | InputSubsystem::InputSubsystem() : impl{std::make_unique<Impl>()} {} | ||
| 123 | |||
| 124 | InputSubsystem::~InputSubsystem() = default; | ||
| 125 | |||
| 126 | void InputSubsystem::Initialize() { | ||
| 127 | impl->Initialize(); | ||
| 128 | } | ||
| 129 | |||
| 130 | void InputSubsystem::Shutdown() { | ||
| 131 | impl->Shutdown(); | ||
| 132 | } | ||
| 133 | |||
| 134 | Keyboard* InputSubsystem::GetKeyboard() { | ||
| 135 | return impl->keyboard.get(); | ||
| 136 | } | ||
| 137 | |||
| 138 | const Keyboard* InputSubsystem::GetKeyboard() const { | ||
| 139 | return impl->keyboard.get(); | ||
| 140 | } | ||
| 141 | |||
| 142 | MotionEmu* InputSubsystem::GetMotionEmu() { | ||
| 143 | return impl->motion_emu.get(); | ||
| 144 | } | ||
| 145 | |||
| 146 | const MotionEmu* InputSubsystem::GetMotionEmu() const { | ||
| 147 | return impl->motion_emu.get(); | ||
| 148 | } | ||
| 63 | 149 | ||
| 64 | gcbuttons.reset(); | 150 | std::vector<Common::ParamPackage> InputSubsystem::GetInputDevices() const { |
| 65 | gcanalog.reset(); | 151 | return impl->GetInputDevices(); |
| 66 | } | 152 | } |
| 67 | 153 | ||
| 68 | Keyboard* GetKeyboard() { | 154 | AnalogMapping InputSubsystem::GetAnalogMappingForDevice(const Common::ParamPackage& device) const { |
| 69 | return keyboard.get(); | 155 | return impl->GetAnalogMappingForDevice(device); |
| 70 | } | 156 | } |
| 71 | 157 | ||
| 72 | MotionEmu* GetMotionEmu() { | 158 | ButtonMapping InputSubsystem::GetButtonMappingForDevice(const Common::ParamPackage& device) const { |
| 73 | return motion_emu.get(); | 159 | return impl->GetButtonMappingForDevice(device); |
| 74 | } | 160 | } |
| 75 | 161 | ||
| 76 | GCButtonFactory* GetGCButtons() { | 162 | GCAnalogFactory* InputSubsystem::GetGCAnalogs() { |
| 77 | return gcbuttons.get(); | 163 | return impl->gcanalog.get(); |
| 78 | } | 164 | } |
| 79 | 165 | ||
| 80 | GCAnalogFactory* GetGCAnalogs() { | 166 | const GCAnalogFactory* InputSubsystem::GetGCAnalogs() const { |
| 81 | return gcanalog.get(); | 167 | return impl->gcanalog.get(); |
| 168 | } | ||
| 169 | |||
| 170 | GCButtonFactory* InputSubsystem::GetGCButtons() { | ||
| 171 | return impl->gcbuttons.get(); | ||
| 172 | } | ||
| 173 | |||
| 174 | const GCButtonFactory* InputSubsystem::GetGCButtons() const { | ||
| 175 | return impl->gcbuttons.get(); | ||
| 176 | } | ||
| 177 | |||
| 178 | void InputSubsystem::ReloadInputDevices() { | ||
| 179 | if (!impl->udp) { | ||
| 180 | return; | ||
| 181 | } | ||
| 182 | impl->udp->ReloadUDPClient(); | ||
| 183 | } | ||
| 184 | |||
| 185 | std::vector<std::unique_ptr<Polling::DevicePoller>> InputSubsystem::GetPollers( | ||
| 186 | Polling::DeviceType type) const { | ||
| 187 | #ifdef HAVE_SDL2 | ||
| 188 | return impl->sdl->GetPollers(type); | ||
| 189 | #else | ||
| 190 | return {}; | ||
| 191 | #endif | ||
| 82 | } | 192 | } |
| 83 | 193 | ||
| 84 | std::string GenerateKeyboardParam(int key_code) { | 194 | std::string GenerateKeyboardParam(int key_code) { |
| @@ -102,18 +212,4 @@ std::string GenerateAnalogParamFromKeys(int key_up, int key_down, int key_left, | |||
| 102 | }; | 212 | }; |
| 103 | return circle_pad_param.Serialize(); | 213 | return circle_pad_param.Serialize(); |
| 104 | } | 214 | } |
| 105 | |||
| 106 | namespace Polling { | ||
| 107 | |||
| 108 | std::vector<std::unique_ptr<DevicePoller>> GetPollers(DeviceType type) { | ||
| 109 | std::vector<std::unique_ptr<DevicePoller>> pollers; | ||
| 110 | |||
| 111 | #ifdef HAVE_SDL2 | ||
| 112 | pollers = sdl->GetPollers(type); | ||
| 113 | #endif | ||
| 114 | |||
| 115 | return pollers; | ||
| 116 | } | ||
| 117 | |||
| 118 | } // namespace Polling | ||
| 119 | } // namespace InputCommon | 215 | } // namespace InputCommon |