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