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