diff options
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
| -rw-r--r-- | src/input_common/sdl/sdl_impl.cpp | 772 |
1 files changed, 615 insertions, 157 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index a2e0c0bd2..7827e324c 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp | |||
| @@ -3,10 +3,14 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | 5 | #include <algorithm> |
| 6 | #include <array> | ||
| 6 | #include <atomic> | 7 | #include <atomic> |
| 8 | #include <chrono> | ||
| 7 | #include <cmath> | 9 | #include <cmath> |
| 8 | #include <functional> | 10 | #include <functional> |
| 9 | #include <mutex> | 11 | #include <mutex> |
| 12 | #include <optional> | ||
| 13 | #include <sstream> | ||
| 10 | #include <string> | 14 | #include <string> |
| 11 | #include <thread> | 15 | #include <thread> |
| 12 | #include <tuple> | 16 | #include <tuple> |
| @@ -15,15 +19,17 @@ | |||
| 15 | #include <vector> | 19 | #include <vector> |
| 16 | #include <SDL.h> | 20 | #include <SDL.h> |
| 17 | #include "common/logging/log.h" | 21 | #include "common/logging/log.h" |
| 18 | #include "common/math_util.h" | ||
| 19 | #include "common/param_package.h" | 22 | #include "common/param_package.h" |
| 20 | #include "common/threadsafe_queue.h" | 23 | #include "common/threadsafe_queue.h" |
| 21 | #include "core/frontend/input.h" | 24 | #include "core/frontend/input.h" |
| 25 | #include "input_common/motion_input.h" | ||
| 22 | #include "input_common/sdl/sdl_impl.h" | 26 | #include "input_common/sdl/sdl_impl.h" |
| 27 | #include "input_common/settings.h" | ||
| 23 | 28 | ||
| 24 | namespace InputCommon::SDL { | 29 | namespace InputCommon::SDL { |
| 25 | 30 | ||
| 26 | static std::string GetGUID(SDL_Joystick* joystick) { | 31 | namespace { |
| 32 | std::string GetGUID(SDL_Joystick* joystick) { | ||
| 27 | const SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); | 33 | const SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); |
| 28 | char guid_str[33]; | 34 | char guid_str[33]; |
| 29 | SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str)); | 35 | SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str)); |
| @@ -31,7 +37,8 @@ static std::string GetGUID(SDL_Joystick* joystick) { | |||
| 31 | } | 37 | } |
| 32 | 38 | ||
| 33 | /// Creates a ParamPackage from an SDL_Event that can directly be used to create a ButtonDevice | 39 | /// Creates a ParamPackage from an SDL_Event that can directly be used to create a ButtonDevice |
| 34 | static Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Event& event); | 40 | Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Event& event); |
| 41 | } // Anonymous namespace | ||
| 35 | 42 | ||
| 36 | static int SDLEventWatcher(void* user_data, SDL_Event* event) { | 43 | static int SDLEventWatcher(void* user_data, SDL_Event* event) { |
| 37 | auto* const sdl_state = static_cast<SDLState*>(user_data); | 44 | auto* const sdl_state = static_cast<SDLState*>(user_data); |
| @@ -48,8 +55,10 @@ static int SDLEventWatcher(void* user_data, SDL_Event* event) { | |||
| 48 | 55 | ||
| 49 | class SDLJoystick { | 56 | class SDLJoystick { |
| 50 | public: | 57 | public: |
| 51 | SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick) | 58 | SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick, |
| 52 | : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose} {} | 59 | SDL_GameController* game_controller) |
| 60 | : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose}, | ||
| 61 | sdl_controller{game_controller, &SDL_GameControllerClose} {} | ||
| 53 | 62 | ||
| 54 | void SetButton(int button, bool value) { | 63 | void SetButton(int button, bool value) { |
| 55 | std::lock_guard lock{mutex}; | 64 | std::lock_guard lock{mutex}; |
| @@ -66,14 +75,24 @@ public: | |||
| 66 | state.axes.insert_or_assign(axis, value); | 75 | state.axes.insert_or_assign(axis, value); |
| 67 | } | 76 | } |
| 68 | 77 | ||
| 69 | float GetAxis(int axis) const { | 78 | float GetAxis(int axis, float range) const { |
| 70 | std::lock_guard lock{mutex}; | 79 | std::lock_guard lock{mutex}; |
| 71 | return state.axes.at(axis) / 32767.0f; | 80 | return static_cast<float>(state.axes.at(axis)) / (32767.0f * range); |
| 72 | } | 81 | } |
| 73 | 82 | ||
| 74 | std::tuple<float, float> GetAnalog(int axis_x, int axis_y) const { | 83 | bool RumblePlay(u16 amp_low, u16 amp_high) { |
| 75 | float x = GetAxis(axis_x); | 84 | if (sdl_controller) { |
| 76 | float y = GetAxis(axis_y); | 85 | return SDL_GameControllerRumble(sdl_controller.get(), amp_low, amp_high, 0) == 0; |
| 86 | } else if (sdl_joystick) { | ||
| 87 | return SDL_JoystickRumble(sdl_joystick.get(), amp_low, amp_high, 0) == 0; | ||
| 88 | } | ||
| 89 | |||
| 90 | return false; | ||
| 91 | } | ||
| 92 | |||
| 93 | std::tuple<float, float> GetAnalog(int axis_x, int axis_y, float range) const { | ||
| 94 | float x = GetAxis(axis_x, range); | ||
| 95 | float y = GetAxis(axis_y, range); | ||
| 77 | y = -y; // 3DS uses an y-axis inverse from SDL | 96 | y = -y; // 3DS uses an y-axis inverse from SDL |
| 78 | 97 | ||
| 79 | // Make sure the coordinates are in the unit circle, | 98 | // Make sure the coordinates are in the unit circle, |
| @@ -88,6 +107,10 @@ public: | |||
| 88 | return std::make_tuple(x, y); | 107 | return std::make_tuple(x, y); |
| 89 | } | 108 | } |
| 90 | 109 | ||
| 110 | const MotionInput& GetMotion() const { | ||
| 111 | return motion; | ||
| 112 | } | ||
| 113 | |||
| 91 | void SetHat(int hat, Uint8 direction) { | 114 | void SetHat(int hat, Uint8 direction) { |
| 92 | std::lock_guard lock{mutex}; | 115 | std::lock_guard lock{mutex}; |
| 93 | state.hats.insert_or_assign(hat, direction); | 116 | state.hats.insert_or_assign(hat, direction); |
| @@ -115,8 +138,13 @@ public: | |||
| 115 | return sdl_joystick.get(); | 138 | return sdl_joystick.get(); |
| 116 | } | 139 | } |
| 117 | 140 | ||
| 118 | void SetSDLJoystick(SDL_Joystick* joystick) { | 141 | SDL_GameController* GetSDLGameController() const { |
| 142 | return sdl_controller.get(); | ||
| 143 | } | ||
| 144 | |||
| 145 | void SetSDLJoystick(SDL_Joystick* joystick, SDL_GameController* controller) { | ||
| 119 | sdl_joystick.reset(joystick); | 146 | sdl_joystick.reset(joystick); |
| 147 | sdl_controller.reset(controller); | ||
| 120 | } | 148 | } |
| 121 | 149 | ||
| 122 | private: | 150 | private: |
| @@ -128,21 +156,29 @@ private: | |||
| 128 | std::string guid; | 156 | std::string guid; |
| 129 | int port; | 157 | int port; |
| 130 | std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; | 158 | std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; |
| 159 | std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller; | ||
| 131 | mutable std::mutex mutex; | 160 | mutable std::mutex mutex; |
| 161 | |||
| 162 | // Motion is initialized without PID values as motion input is not aviable for SDL2 | ||
| 163 | MotionInput motion{0.0f, 0.0f, 0.0f}; | ||
| 132 | }; | 164 | }; |
| 133 | 165 | ||
| 134 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { | 166 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { |
| 135 | std::lock_guard lock{joystick_map_mutex}; | 167 | std::lock_guard lock{joystick_map_mutex}; |
| 136 | const auto it = joystick_map.find(guid); | 168 | const auto it = joystick_map.find(guid); |
| 169 | |||
| 137 | if (it != joystick_map.end()) { | 170 | if (it != joystick_map.end()) { |
| 138 | while (it->second.size() <= static_cast<std::size_t>(port)) { | 171 | while (it->second.size() <= static_cast<std::size_t>(port)) { |
| 139 | auto joystick = | 172 | auto joystick = std::make_shared<SDLJoystick>(guid, static_cast<int>(it->second.size()), |
| 140 | std::make_shared<SDLJoystick>(guid, static_cast<int>(it->second.size()), nullptr); | 173 | nullptr, nullptr); |
| 141 | it->second.emplace_back(std::move(joystick)); | 174 | it->second.emplace_back(std::move(joystick)); |
| 142 | } | 175 | } |
| 143 | return it->second[port]; | 176 | |
| 177 | return it->second[static_cast<std::size_t>(port)]; | ||
| 144 | } | 178 | } |
| 145 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr); | 179 | |
| 180 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr, nullptr); | ||
| 181 | |||
| 146 | return joystick_map[guid].emplace_back(std::move(joystick)); | 182 | return joystick_map[guid].emplace_back(std::move(joystick)); |
| 147 | } | 183 | } |
| 148 | 184 | ||
| @@ -152,86 +188,72 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_ | |||
| 152 | 188 | ||
| 153 | std::lock_guard lock{joystick_map_mutex}; | 189 | std::lock_guard lock{joystick_map_mutex}; |
| 154 | const auto map_it = joystick_map.find(guid); | 190 | const auto map_it = joystick_map.find(guid); |
| 155 | if (map_it != joystick_map.end()) { | ||
| 156 | const auto vec_it = | ||
| 157 | std::find_if(map_it->second.begin(), map_it->second.end(), | ||
| 158 | [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) { | ||
| 159 | return sdl_joystick == joystick->GetSDLJoystick(); | ||
| 160 | }); | ||
| 161 | if (vec_it != map_it->second.end()) { | ||
| 162 | // This is the common case: There is already an existing SDL_Joystick maped to a | ||
| 163 | // SDLJoystick. return the SDLJoystick | ||
| 164 | return *vec_it; | ||
| 165 | } | ||
| 166 | 191 | ||
| 167 | // Search for a SDLJoystick without a mapped SDL_Joystick... | 192 | if (map_it == joystick_map.end()) { |
| 168 | const auto nullptr_it = std::find_if(map_it->second.begin(), map_it->second.end(), | 193 | return nullptr; |
| 169 | [](const std::shared_ptr<SDLJoystick>& joystick) { | 194 | } |
| 170 | return !joystick->GetSDLJoystick(); | 195 | |
| 171 | }); | 196 | const auto vec_it = std::find_if(map_it->second.begin(), map_it->second.end(), |
| 172 | if (nullptr_it != map_it->second.end()) { | 197 | [&sdl_joystick](const auto& joystick) { |
| 173 | // ... and map it | 198 | return joystick->GetSDLJoystick() == sdl_joystick; |
| 174 | (*nullptr_it)->SetSDLJoystick(sdl_joystick); | 199 | }); |
| 175 | return *nullptr_it; | ||
| 176 | } | ||
| 177 | 200 | ||
| 178 | // There is no SDLJoystick without a mapped SDL_Joystick | 201 | if (vec_it == map_it->second.end()) { |
| 179 | // Create a new SDLJoystick | 202 | return nullptr; |
| 180 | const int port = static_cast<int>(map_it->second.size()); | ||
| 181 | auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick); | ||
| 182 | return map_it->second.emplace_back(std::move(joystick)); | ||
| 183 | } | 203 | } |
| 184 | 204 | ||
| 185 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick); | 205 | return *vec_it; |
| 186 | return joystick_map[guid].emplace_back(std::move(joystick)); | ||
| 187 | } | 206 | } |
| 188 | 207 | ||
| 189 | void SDLState::InitJoystick(int joystick_index) { | 208 | void SDLState::InitJoystick(int joystick_index) { |
| 190 | SDL_Joystick* sdl_joystick = SDL_JoystickOpen(joystick_index); | 209 | SDL_Joystick* sdl_joystick = SDL_JoystickOpen(joystick_index); |
| 210 | SDL_GameController* sdl_gamecontroller = nullptr; | ||
| 211 | |||
| 212 | if (SDL_IsGameController(joystick_index)) { | ||
| 213 | sdl_gamecontroller = SDL_GameControllerOpen(joystick_index); | ||
| 214 | } | ||
| 215 | |||
| 191 | if (!sdl_joystick) { | 216 | if (!sdl_joystick) { |
| 192 | LOG_ERROR(Input, "failed to open joystick {}", joystick_index); | 217 | LOG_ERROR(Input, "Failed to open joystick {}", joystick_index); |
| 193 | return; | 218 | return; |
| 194 | } | 219 | } |
| 220 | |||
| 195 | const std::string guid = GetGUID(sdl_joystick); | 221 | const std::string guid = GetGUID(sdl_joystick); |
| 196 | 222 | ||
| 197 | std::lock_guard lock{joystick_map_mutex}; | 223 | std::lock_guard lock{joystick_map_mutex}; |
| 198 | if (joystick_map.find(guid) == joystick_map.end()) { | 224 | if (joystick_map.find(guid) == joystick_map.end()) { |
| 199 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick); | 225 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_gamecontroller); |
| 200 | joystick_map[guid].emplace_back(std::move(joystick)); | 226 | joystick_map[guid].emplace_back(std::move(joystick)); |
| 201 | return; | 227 | return; |
| 202 | } | 228 | } |
| 229 | |||
| 203 | auto& joystick_guid_list = joystick_map[guid]; | 230 | auto& joystick_guid_list = joystick_map[guid]; |
| 204 | const auto it = std::find_if( | 231 | const auto joystick_it = |
| 205 | joystick_guid_list.begin(), joystick_guid_list.end(), | 232 | std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), |
| 206 | [](const std::shared_ptr<SDLJoystick>& joystick) { return !joystick->GetSDLJoystick(); }); | 233 | [](const auto& joystick) { return !joystick->GetSDLJoystick(); }); |
| 207 | if (it != joystick_guid_list.end()) { | 234 | |
| 208 | (*it)->SetSDLJoystick(sdl_joystick); | 235 | if (joystick_it != joystick_guid_list.end()) { |
| 236 | (*joystick_it)->SetSDLJoystick(sdl_joystick, sdl_gamecontroller); | ||
| 209 | return; | 237 | return; |
| 210 | } | 238 | } |
| 239 | |||
| 211 | const int port = static_cast<int>(joystick_guid_list.size()); | 240 | const int port = static_cast<int>(joystick_guid_list.size()); |
| 212 | auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick); | 241 | auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick, sdl_gamecontroller); |
| 213 | joystick_guid_list.emplace_back(std::move(joystick)); | 242 | joystick_guid_list.emplace_back(std::move(joystick)); |
| 214 | } | 243 | } |
| 215 | 244 | ||
| 216 | void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { | 245 | void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { |
| 217 | const std::string guid = GetGUID(sdl_joystick); | 246 | const std::string guid = GetGUID(sdl_joystick); |
| 218 | 247 | ||
| 219 | std::shared_ptr<SDLJoystick> joystick; | 248 | std::lock_guard lock{joystick_map_mutex}; |
| 220 | { | 249 | // This call to guid is safe since the joystick is guaranteed to be in the map |
| 221 | std::lock_guard lock{joystick_map_mutex}; | 250 | const auto& joystick_guid_list = joystick_map[guid]; |
| 222 | // This call to guid is safe since the joystick is guaranteed to be in the map | 251 | const auto joystick_it = std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), |
| 223 | const auto& joystick_guid_list = joystick_map[guid]; | 252 | [&sdl_joystick](const auto& joystick) { |
| 224 | const auto joystick_it = | 253 | return joystick->GetSDLJoystick() == sdl_joystick; |
| 225 | std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), | 254 | }); |
| 226 | [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) { | 255 | |
| 227 | return joystick->GetSDLJoystick() == sdl_joystick; | 256 | (*joystick_it)->SetSDLJoystick(nullptr, nullptr); |
| 228 | }); | ||
| 229 | joystick = *joystick_it; | ||
| 230 | } | ||
| 231 | |||
| 232 | // Destruct SDL_Joystick outside the lock guard because SDL can internally call the | ||
| 233 | // event callback which locks the mutex again. | ||
| 234 | joystick->SetSDLJoystick(nullptr); | ||
| 235 | } | 257 | } |
| 236 | 258 | ||
| 237 | void SDLState::HandleGameControllerEvent(const SDL_Event& event) { | 259 | void SDLState::HandleGameControllerEvent(const SDL_Event& event) { |
| @@ -313,7 +335,7 @@ public: | |||
| 313 | trigger_if_greater(trigger_if_greater_) {} | 335 | trigger_if_greater(trigger_if_greater_) {} |
| 314 | 336 | ||
| 315 | bool GetStatus() const override { | 337 | bool GetStatus() const override { |
| 316 | const float axis_value = joystick->GetAxis(axis); | 338 | const float axis_value = joystick->GetAxis(axis, 1.0f); |
| 317 | if (trigger_if_greater) { | 339 | if (trigger_if_greater) { |
| 318 | return axis_value > threshold; | 340 | return axis_value > threshold; |
| 319 | } | 341 | } |
| @@ -329,22 +351,24 @@ private: | |||
| 329 | 351 | ||
| 330 | class SDLAnalog final : public Input::AnalogDevice { | 352 | class SDLAnalog final : public Input::AnalogDevice { |
| 331 | public: | 353 | public: |
| 332 | SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, float deadzone_) | 354 | explicit SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, |
| 333 | : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) {} | 355 | float deadzone_, float range_) |
| 356 | : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), | ||
| 357 | range(range_) {} | ||
| 334 | 358 | ||
| 335 | std::tuple<float, float> GetStatus() const override { | 359 | std::tuple<float, float> GetStatus() const override { |
| 336 | const auto [x, y] = joystick->GetAnalog(axis_x, axis_y); | 360 | const auto [x, y] = joystick->GetAnalog(axis_x, axis_y, range); |
| 337 | const float r = std::sqrt((x * x) + (y * y)); | 361 | const float r = std::sqrt((x * x) + (y * y)); |
| 338 | if (r > deadzone) { | 362 | if (r > deadzone) { |
| 339 | return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), | 363 | return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), |
| 340 | y / r * (r - deadzone) / (1 - deadzone)); | 364 | y / r * (r - deadzone) / (1 - deadzone)); |
| 341 | } | 365 | } |
| 342 | return std::make_tuple<float, float>(0.0f, 0.0f); | 366 | return {}; |
| 343 | } | 367 | } |
| 344 | 368 | ||
| 345 | bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { | 369 | bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { |
| 346 | const auto [x, y] = GetStatus(); | 370 | const auto [x, y] = GetStatus(); |
| 347 | const float directional_deadzone = 0.4f; | 371 | const float directional_deadzone = 0.5f; |
| 348 | switch (direction) { | 372 | switch (direction) { |
| 349 | case Input::AnalogDirection::RIGHT: | 373 | case Input::AnalogDirection::RIGHT: |
| 350 | return x > directional_deadzone; | 374 | return x > directional_deadzone; |
| @@ -363,6 +387,95 @@ private: | |||
| 363 | const int axis_x; | 387 | const int axis_x; |
| 364 | const int axis_y; | 388 | const int axis_y; |
| 365 | const float deadzone; | 389 | const float deadzone; |
| 390 | const float range; | ||
| 391 | }; | ||
| 392 | |||
| 393 | class SDLVibration final : public Input::VibrationDevice { | ||
| 394 | public: | ||
| 395 | explicit SDLVibration(std::shared_ptr<SDLJoystick> joystick_) | ||
| 396 | : joystick(std::move(joystick_)) {} | ||
| 397 | |||
| 398 | u8 GetStatus() const override { | ||
| 399 | joystick->RumblePlay(1, 1); | ||
| 400 | return joystick->RumblePlay(0, 0); | ||
| 401 | } | ||
| 402 | |||
| 403 | bool SetRumblePlay(f32 amp_low, [[maybe_unused]] f32 freq_low, f32 amp_high, | ||
| 404 | [[maybe_unused]] f32 freq_high) const override { | ||
| 405 | const auto process_amplitude = [](f32 amplitude) { | ||
| 406 | return static_cast<u16>((amplitude + std::pow(amplitude, 0.3f)) * 0.5f * 0xFFFF); | ||
| 407 | }; | ||
| 408 | |||
| 409 | const auto processed_amp_low = process_amplitude(amp_low); | ||
| 410 | const auto processed_amp_high = process_amplitude(amp_high); | ||
| 411 | |||
| 412 | return joystick->RumblePlay(processed_amp_low, processed_amp_high); | ||
| 413 | } | ||
| 414 | |||
| 415 | private: | ||
| 416 | std::shared_ptr<SDLJoystick> joystick; | ||
| 417 | }; | ||
| 418 | |||
| 419 | class SDLDirectionMotion final : public Input::MotionDevice { | ||
| 420 | public: | ||
| 421 | explicit SDLDirectionMotion(std::shared_ptr<SDLJoystick> joystick_, int hat_, Uint8 direction_) | ||
| 422 | : joystick(std::move(joystick_)), hat(hat_), direction(direction_) {} | ||
| 423 | |||
| 424 | Input::MotionStatus GetStatus() const override { | ||
| 425 | if (joystick->GetHatDirection(hat, direction)) { | ||
| 426 | return joystick->GetMotion().GetRandomMotion(2, 6); | ||
| 427 | } | ||
| 428 | return joystick->GetMotion().GetRandomMotion(0, 0); | ||
| 429 | } | ||
| 430 | |||
| 431 | private: | ||
| 432 | std::shared_ptr<SDLJoystick> joystick; | ||
| 433 | int hat; | ||
| 434 | Uint8 direction; | ||
| 435 | }; | ||
| 436 | |||
| 437 | class SDLAxisMotion final : public Input::MotionDevice { | ||
| 438 | public: | ||
| 439 | explicit SDLAxisMotion(std::shared_ptr<SDLJoystick> joystick_, int axis_, float threshold_, | ||
| 440 | bool trigger_if_greater_) | ||
| 441 | : joystick(std::move(joystick_)), axis(axis_), threshold(threshold_), | ||
| 442 | trigger_if_greater(trigger_if_greater_) {} | ||
| 443 | |||
| 444 | Input::MotionStatus GetStatus() const override { | ||
| 445 | const float axis_value = joystick->GetAxis(axis, 1.0f); | ||
| 446 | bool trigger = axis_value < threshold; | ||
| 447 | if (trigger_if_greater) { | ||
| 448 | trigger = axis_value > threshold; | ||
| 449 | } | ||
| 450 | |||
| 451 | if (trigger) { | ||
| 452 | return joystick->GetMotion().GetRandomMotion(2, 6); | ||
| 453 | } | ||
| 454 | return joystick->GetMotion().GetRandomMotion(0, 0); | ||
| 455 | } | ||
| 456 | |||
| 457 | private: | ||
| 458 | std::shared_ptr<SDLJoystick> joystick; | ||
| 459 | int axis; | ||
| 460 | float threshold; | ||
| 461 | bool trigger_if_greater; | ||
| 462 | }; | ||
| 463 | |||
| 464 | class SDLButtonMotion final : public Input::MotionDevice { | ||
| 465 | public: | ||
| 466 | explicit SDLButtonMotion(std::shared_ptr<SDLJoystick> joystick_, int button_) | ||
| 467 | : joystick(std::move(joystick_)), button(button_) {} | ||
| 468 | |||
| 469 | Input::MotionStatus GetStatus() const override { | ||
| 470 | if (joystick->GetButton(button)) { | ||
| 471 | return joystick->GetMotion().GetRandomMotion(2, 6); | ||
| 472 | } | ||
| 473 | return joystick->GetMotion().GetRandomMotion(0, 0); | ||
| 474 | } | ||
| 475 | |||
| 476 | private: | ||
| 477 | std::shared_ptr<SDLJoystick> joystick; | ||
| 478 | int button; | ||
| 366 | }; | 479 | }; |
| 367 | 480 | ||
| 368 | /// A button device factory that creates button devices from SDL joystick | 481 | /// A button device factory that creates button devices from SDL joystick |
| @@ -445,7 +558,7 @@ class SDLAnalogFactory final : public Input::Factory<Input::AnalogDevice> { | |||
| 445 | public: | 558 | public: |
| 446 | explicit SDLAnalogFactory(SDLState& state_) : state(state_) {} | 559 | explicit SDLAnalogFactory(SDLState& state_) : state(state_) {} |
| 447 | /** | 560 | /** |
| 448 | * Creates analog device from joystick axes | 561 | * Creates an analog device from joystick axes |
| 449 | * @param params contains parameters for creating the device: | 562 | * @param params contains parameters for creating the device: |
| 450 | * - "guid": the guid of the joystick to bind | 563 | * - "guid": the guid of the joystick to bind |
| 451 | * - "port": the nth joystick of the same type | 564 | * - "port": the nth joystick of the same type |
| @@ -457,14 +570,98 @@ public: | |||
| 457 | const int port = params.Get("port", 0); | 570 | const int port = params.Get("port", 0); |
| 458 | const int axis_x = params.Get("axis_x", 0); | 571 | const int axis_x = params.Get("axis_x", 0); |
| 459 | const int axis_y = params.Get("axis_y", 1); | 572 | const int axis_y = params.Get("axis_y", 1); |
| 460 | const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); | 573 | const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); |
| 461 | 574 | const float range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); | |
| 462 | auto joystick = state.GetSDLJoystickByGUID(guid, port); | 575 | auto joystick = state.GetSDLJoystickByGUID(guid, port); |
| 463 | 576 | ||
| 464 | // This is necessary so accessing GetAxis with axis_x and axis_y won't crash | 577 | // This is necessary so accessing GetAxis with axis_x and axis_y won't crash |
| 465 | joystick->SetAxis(axis_x, 0); | 578 | joystick->SetAxis(axis_x, 0); |
| 466 | joystick->SetAxis(axis_y, 0); | 579 | joystick->SetAxis(axis_y, 0); |
| 467 | return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y, deadzone); | 580 | return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y, deadzone, range); |
| 581 | } | ||
| 582 | |||
| 583 | private: | ||
| 584 | SDLState& state; | ||
| 585 | }; | ||
| 586 | |||
| 587 | /// An vibration device factory that creates vibration devices from SDL joystick | ||
| 588 | class SDLVibrationFactory final : public Input::Factory<Input::VibrationDevice> { | ||
| 589 | public: | ||
| 590 | explicit SDLVibrationFactory(SDLState& state_) : state(state_) {} | ||
| 591 | /** | ||
| 592 | * Creates a vibration device from a joystick | ||
| 593 | * @param params contains parameters for creating the device: | ||
| 594 | * - "guid": the guid of the joystick to bind | ||
| 595 | * - "port": the nth joystick of the same type | ||
| 596 | */ | ||
| 597 | std::unique_ptr<Input::VibrationDevice> Create(const Common::ParamPackage& params) override { | ||
| 598 | const std::string guid = params.Get("guid", "0"); | ||
| 599 | const int port = params.Get("port", 0); | ||
| 600 | return std::make_unique<SDLVibration>(state.GetSDLJoystickByGUID(guid, port)); | ||
| 601 | } | ||
| 602 | |||
| 603 | private: | ||
| 604 | SDLState& state; | ||
| 605 | }; | ||
| 606 | |||
| 607 | /// A motion device factory that creates motion devices from SDL joystick | ||
| 608 | class SDLMotionFactory final : public Input::Factory<Input::MotionDevice> { | ||
| 609 | public: | ||
| 610 | explicit SDLMotionFactory(SDLState& state_) : state(state_) {} | ||
| 611 | /** | ||
| 612 | * Creates motion device from joystick axes | ||
| 613 | * @param params contains parameters for creating the device: | ||
| 614 | * - "guid": the guid of the joystick to bind | ||
| 615 | * - "port": the nth joystick of the same type | ||
| 616 | */ | ||
| 617 | std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override { | ||
| 618 | const std::string guid = params.Get("guid", "0"); | ||
| 619 | const int port = params.Get("port", 0); | ||
| 620 | |||
| 621 | auto joystick = state.GetSDLJoystickByGUID(guid, port); | ||
| 622 | |||
| 623 | if (params.Has("hat")) { | ||
| 624 | const int hat = params.Get("hat", 0); | ||
| 625 | const std::string direction_name = params.Get("direction", ""); | ||
| 626 | Uint8 direction; | ||
| 627 | if (direction_name == "up") { | ||
| 628 | direction = SDL_HAT_UP; | ||
| 629 | } else if (direction_name == "down") { | ||
| 630 | direction = SDL_HAT_DOWN; | ||
| 631 | } else if (direction_name == "left") { | ||
| 632 | direction = SDL_HAT_LEFT; | ||
| 633 | } else if (direction_name == "right") { | ||
| 634 | direction = SDL_HAT_RIGHT; | ||
| 635 | } else { | ||
| 636 | direction = 0; | ||
| 637 | } | ||
| 638 | // This is necessary so accessing GetHat with hat won't crash | ||
| 639 | joystick->SetHat(hat, SDL_HAT_CENTERED); | ||
| 640 | return std::make_unique<SDLDirectionMotion>(joystick, hat, direction); | ||
| 641 | } | ||
| 642 | |||
| 643 | if (params.Has("axis")) { | ||
| 644 | const int axis = params.Get("axis", 0); | ||
| 645 | const float threshold = params.Get("threshold", 0.5f); | ||
| 646 | const std::string direction_name = params.Get("direction", ""); | ||
| 647 | bool trigger_if_greater; | ||
| 648 | if (direction_name == "+") { | ||
| 649 | trigger_if_greater = true; | ||
| 650 | } else if (direction_name == "-") { | ||
| 651 | trigger_if_greater = false; | ||
| 652 | } else { | ||
| 653 | trigger_if_greater = true; | ||
| 654 | LOG_ERROR(Input, "Unknown direction {}", direction_name); | ||
| 655 | } | ||
| 656 | // This is necessary so accessing GetAxis with axis won't crash | ||
| 657 | joystick->SetAxis(axis, 0); | ||
| 658 | return std::make_unique<SDLAxisMotion>(joystick, axis, threshold, trigger_if_greater); | ||
| 659 | } | ||
| 660 | |||
| 661 | const int button = params.Get("button", 0); | ||
| 662 | // This is necessary so accessing GetButton with button won't crash | ||
| 663 | joystick->SetButton(button, false); | ||
| 664 | return std::make_unique<SDLButtonMotion>(joystick, button); | ||
| 468 | } | 665 | } |
| 469 | 666 | ||
| 470 | private: | 667 | private: |
| @@ -473,15 +670,22 @@ private: | |||
| 473 | 670 | ||
| 474 | SDLState::SDLState() { | 671 | SDLState::SDLState() { |
| 475 | using namespace Input; | 672 | using namespace Input; |
| 476 | RegisterFactory<ButtonDevice>("sdl", std::make_shared<SDLButtonFactory>(*this)); | 673 | button_factory = std::make_shared<SDLButtonFactory>(*this); |
| 477 | RegisterFactory<AnalogDevice>("sdl", std::make_shared<SDLAnalogFactory>(*this)); | 674 | analog_factory = std::make_shared<SDLAnalogFactory>(*this); |
| 478 | 675 | vibration_factory = std::make_shared<SDLVibrationFactory>(*this); | |
| 479 | // If the frontend is going to manage the event loop, then we dont start one here | 676 | motion_factory = std::make_shared<SDLMotionFactory>(*this); |
| 480 | start_thread = !SDL_WasInit(SDL_INIT_JOYSTICK); | 677 | RegisterFactory<ButtonDevice>("sdl", button_factory); |
| 678 | RegisterFactory<AnalogDevice>("sdl", analog_factory); | ||
| 679 | RegisterFactory<VibrationDevice>("sdl", vibration_factory); | ||
| 680 | RegisterFactory<MotionDevice>("sdl", motion_factory); | ||
| 681 | |||
| 682 | // If the frontend is going to manage the event loop, then we don't start one here | ||
| 683 | start_thread = SDL_WasInit(SDL_INIT_JOYSTICK) == 0; | ||
| 481 | if (start_thread && SDL_Init(SDL_INIT_JOYSTICK) < 0) { | 684 | if (start_thread && SDL_Init(SDL_INIT_JOYSTICK) < 0) { |
| 482 | LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError()); | 685 | LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError()); |
| 483 | return; | 686 | return; |
| 484 | } | 687 | } |
| 688 | has_gamecontroller = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0; | ||
| 485 | if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) { | 689 | if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) { |
| 486 | LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError()); | 690 | LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError()); |
| 487 | } | 691 | } |
| @@ -494,7 +698,7 @@ SDLState::SDLState() { | |||
| 494 | using namespace std::chrono_literals; | 698 | using namespace std::chrono_literals; |
| 495 | while (initialized) { | 699 | while (initialized) { |
| 496 | SDL_PumpEvents(); | 700 | SDL_PumpEvents(); |
| 497 | std::this_thread::sleep_for(10ms); | 701 | std::this_thread::sleep_for(1ms); |
| 498 | } | 702 | } |
| 499 | }); | 703 | }); |
| 500 | } | 704 | } |
| @@ -509,6 +713,8 @@ SDLState::~SDLState() { | |||
| 509 | using namespace Input; | 713 | using namespace Input; |
| 510 | UnregisterFactory<ButtonDevice>("sdl"); | 714 | UnregisterFactory<ButtonDevice>("sdl"); |
| 511 | UnregisterFactory<AnalogDevice>("sdl"); | 715 | UnregisterFactory<AnalogDevice>("sdl"); |
| 716 | UnregisterFactory<VibrationDevice>("sdl"); | ||
| 717 | UnregisterFactory<MotionDevice>("sdl"); | ||
| 512 | 718 | ||
| 513 | CloseJoysticks(); | 719 | CloseJoysticks(); |
| 514 | SDL_DelEventWatch(&SDLEventWatcher, this); | 720 | SDL_DelEventWatch(&SDLEventWatcher, this); |
| @@ -520,65 +726,268 @@ SDLState::~SDLState() { | |||
| 520 | } | 726 | } |
| 521 | } | 727 | } |
| 522 | 728 | ||
| 523 | static Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Event& event) { | 729 | std::vector<Common::ParamPackage> SDLState::GetInputDevices() { |
| 730 | std::scoped_lock lock(joystick_map_mutex); | ||
| 731 | std::vector<Common::ParamPackage> devices; | ||
| 732 | for (const auto& [key, value] : joystick_map) { | ||
| 733 | for (const auto& joystick : value) { | ||
| 734 | if (auto* const controller = joystick->GetSDLGameController()) { | ||
| 735 | std::string name = | ||
| 736 | fmt::format("{} {}", SDL_GameControllerName(controller), joystick->GetPort()); | ||
| 737 | devices.emplace_back(Common::ParamPackage{ | ||
| 738 | {"class", "sdl"}, | ||
| 739 | {"display", std::move(name)}, | ||
| 740 | {"guid", joystick->GetGUID()}, | ||
| 741 | {"port", std::to_string(joystick->GetPort())}, | ||
| 742 | }); | ||
| 743 | } else if (auto* const joy = joystick->GetSDLJoystick()) { | ||
| 744 | std::string name = fmt::format("{} {}", SDL_JoystickName(joy), joystick->GetPort()); | ||
| 745 | devices.emplace_back(Common::ParamPackage{ | ||
| 746 | {"class", "sdl"}, | ||
| 747 | {"display", std::move(name)}, | ||
| 748 | {"guid", joystick->GetGUID()}, | ||
| 749 | {"port", std::to_string(joystick->GetPort())}, | ||
| 750 | }); | ||
| 751 | } | ||
| 752 | } | ||
| 753 | } | ||
| 754 | return devices; | ||
| 755 | } | ||
| 756 | |||
| 757 | namespace { | ||
| 758 | Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, s32 axis, | ||
| 759 | float value = 0.1f) { | ||
| 760 | Common::ParamPackage params({{"engine", "sdl"}}); | ||
| 761 | params.Set("port", port); | ||
| 762 | params.Set("guid", std::move(guid)); | ||
| 763 | params.Set("axis", axis); | ||
| 764 | if (value > 0) { | ||
| 765 | params.Set("direction", "+"); | ||
| 766 | params.Set("threshold", "0.5"); | ||
| 767 | } else { | ||
| 768 | params.Set("direction", "-"); | ||
| 769 | params.Set("threshold", "-0.5"); | ||
| 770 | } | ||
| 771 | return params; | ||
| 772 | } | ||
| 773 | |||
| 774 | Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, s32 button) { | ||
| 775 | Common::ParamPackage params({{"engine", "sdl"}}); | ||
| 776 | params.Set("port", port); | ||
| 777 | params.Set("guid", std::move(guid)); | ||
| 778 | params.Set("button", button); | ||
| 779 | return params; | ||
| 780 | } | ||
| 781 | |||
| 782 | Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, s32 hat, s32 value) { | ||
| 524 | Common::ParamPackage params({{"engine", "sdl"}}); | 783 | Common::ParamPackage params({{"engine", "sdl"}}); |
| 525 | 784 | ||
| 785 | params.Set("port", port); | ||
| 786 | params.Set("guid", std::move(guid)); | ||
| 787 | params.Set("hat", hat); | ||
| 788 | switch (value) { | ||
| 789 | case SDL_HAT_UP: | ||
| 790 | params.Set("direction", "up"); | ||
| 791 | break; | ||
| 792 | case SDL_HAT_DOWN: | ||
| 793 | params.Set("direction", "down"); | ||
| 794 | break; | ||
| 795 | case SDL_HAT_LEFT: | ||
| 796 | params.Set("direction", "left"); | ||
| 797 | break; | ||
| 798 | case SDL_HAT_RIGHT: | ||
| 799 | params.Set("direction", "right"); | ||
| 800 | break; | ||
| 801 | default: | ||
| 802 | return {}; | ||
| 803 | } | ||
| 804 | return params; | ||
| 805 | } | ||
| 806 | |||
| 807 | Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Event& event) { | ||
| 808 | switch (event.type) { | ||
| 809 | case SDL_JOYAXISMOTION: { | ||
| 810 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) { | ||
| 811 | return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), | ||
| 812 | static_cast<s32>(event.jaxis.axis), | ||
| 813 | event.jaxis.value); | ||
| 814 | } | ||
| 815 | break; | ||
| 816 | } | ||
| 817 | case SDL_JOYBUTTONUP: { | ||
| 818 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which)) { | ||
| 819 | return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), | ||
| 820 | static_cast<s32>(event.jbutton.button)); | ||
| 821 | } | ||
| 822 | break; | ||
| 823 | } | ||
| 824 | case SDL_JOYHATMOTION: { | ||
| 825 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which)) { | ||
| 826 | return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), | ||
| 827 | static_cast<s32>(event.jhat.hat), | ||
| 828 | static_cast<s32>(event.jhat.value)); | ||
| 829 | } | ||
| 830 | break; | ||
| 831 | } | ||
| 832 | } | ||
| 833 | return {}; | ||
| 834 | } | ||
| 835 | |||
| 836 | Common::ParamPackage SDLEventToMotionParamPackage(SDLState& state, const SDL_Event& event) { | ||
| 526 | switch (event.type) { | 837 | switch (event.type) { |
| 527 | case SDL_JOYAXISMOTION: { | 838 | case SDL_JOYAXISMOTION: { |
| 528 | const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); | 839 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) { |
| 529 | params.Set("port", joystick->GetPort()); | 840 | return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), |
| 530 | params.Set("guid", joystick->GetGUID()); | 841 | static_cast<s32>(event.jaxis.axis), |
| 531 | params.Set("axis", event.jaxis.axis); | 842 | event.jaxis.value); |
| 532 | if (event.jaxis.value > 0) { | ||
| 533 | params.Set("direction", "+"); | ||
| 534 | params.Set("threshold", "0.5"); | ||
| 535 | } else { | ||
| 536 | params.Set("direction", "-"); | ||
| 537 | params.Set("threshold", "-0.5"); | ||
| 538 | } | 843 | } |
| 539 | break; | 844 | break; |
| 540 | } | 845 | } |
| 541 | case SDL_JOYBUTTONUP: { | 846 | case SDL_JOYBUTTONUP: { |
| 542 | const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which); | 847 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which)) { |
| 543 | params.Set("port", joystick->GetPort()); | 848 | return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), |
| 544 | params.Set("guid", joystick->GetGUID()); | 849 | static_cast<s32>(event.jbutton.button)); |
| 545 | params.Set("button", event.jbutton.button); | 850 | } |
| 546 | break; | 851 | break; |
| 547 | } | 852 | } |
| 548 | case SDL_JOYHATMOTION: { | 853 | case SDL_JOYHATMOTION: { |
| 549 | const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which); | 854 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which)) { |
| 550 | params.Set("port", joystick->GetPort()); | 855 | return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), |
| 551 | params.Set("guid", joystick->GetGUID()); | 856 | static_cast<s32>(event.jhat.hat), |
| 552 | params.Set("hat", event.jhat.hat); | 857 | static_cast<s32>(event.jhat.value)); |
| 553 | switch (event.jhat.value) { | ||
| 554 | case SDL_HAT_UP: | ||
| 555 | params.Set("direction", "up"); | ||
| 556 | break; | ||
| 557 | case SDL_HAT_DOWN: | ||
| 558 | params.Set("direction", "down"); | ||
| 559 | break; | ||
| 560 | case SDL_HAT_LEFT: | ||
| 561 | params.Set("direction", "left"); | ||
| 562 | break; | ||
| 563 | case SDL_HAT_RIGHT: | ||
| 564 | params.Set("direction", "right"); | ||
| 565 | break; | ||
| 566 | default: | ||
| 567 | return {}; | ||
| 568 | } | 858 | } |
| 569 | break; | 859 | break; |
| 570 | } | 860 | } |
| 571 | } | 861 | } |
| 862 | return {}; | ||
| 863 | } | ||
| 864 | |||
| 865 | Common::ParamPackage BuildParamPackageForBinding(int port, const std::string& guid, | ||
| 866 | const SDL_GameControllerButtonBind& binding) { | ||
| 867 | switch (binding.bindType) { | ||
| 868 | case SDL_CONTROLLER_BINDTYPE_NONE: | ||
| 869 | break; | ||
| 870 | case SDL_CONTROLLER_BINDTYPE_AXIS: | ||
| 871 | return BuildAnalogParamPackageForButton(port, guid, binding.value.axis); | ||
| 872 | case SDL_CONTROLLER_BINDTYPE_BUTTON: | ||
| 873 | return BuildButtonParamPackageForButton(port, guid, binding.value.button); | ||
| 874 | case SDL_CONTROLLER_BINDTYPE_HAT: | ||
| 875 | return BuildHatParamPackageForButton(port, guid, binding.value.hat.hat, | ||
| 876 | binding.value.hat.hat_mask); | ||
| 877 | } | ||
| 878 | return {}; | ||
| 879 | } | ||
| 880 | |||
| 881 | Common::ParamPackage BuildParamPackageForAnalog(int port, const std::string& guid, int axis_x, | ||
| 882 | int axis_y) { | ||
| 883 | Common::ParamPackage params; | ||
| 884 | params.Set("engine", "sdl"); | ||
| 885 | params.Set("port", port); | ||
| 886 | params.Set("guid", guid); | ||
| 887 | params.Set("axis_x", axis_x); | ||
| 888 | params.Set("axis_y", axis_y); | ||
| 572 | return params; | 889 | return params; |
| 573 | } | 890 | } |
| 891 | } // Anonymous namespace | ||
| 574 | 892 | ||
| 575 | namespace Polling { | 893 | ButtonMapping SDLState::GetButtonMappingForDevice(const Common::ParamPackage& params) { |
| 894 | if (!params.Has("guid") || !params.Has("port")) { | ||
| 895 | return {}; | ||
| 896 | } | ||
| 897 | const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0)); | ||
| 898 | auto* controller = joystick->GetSDLGameController(); | ||
| 899 | if (controller == nullptr) { | ||
| 900 | return {}; | ||
| 901 | } | ||
| 576 | 902 | ||
| 903 | // This list is missing ZL/ZR since those are not considered buttons in SDL GameController. | ||
| 904 | // We will add those afterwards | ||
| 905 | // This list also excludes Screenshot since theres not really a mapping for that | ||
| 906 | using ButtonBindings = | ||
| 907 | std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerButton>, 17>; | ||
| 908 | static constexpr ButtonBindings switch_to_sdl_button{{ | ||
| 909 | {Settings::NativeButton::A, SDL_CONTROLLER_BUTTON_B}, | ||
| 910 | {Settings::NativeButton::B, SDL_CONTROLLER_BUTTON_A}, | ||
| 911 | {Settings::NativeButton::X, SDL_CONTROLLER_BUTTON_Y}, | ||
| 912 | {Settings::NativeButton::Y, SDL_CONTROLLER_BUTTON_X}, | ||
| 913 | {Settings::NativeButton::LStick, SDL_CONTROLLER_BUTTON_LEFTSTICK}, | ||
| 914 | {Settings::NativeButton::RStick, SDL_CONTROLLER_BUTTON_RIGHTSTICK}, | ||
| 915 | {Settings::NativeButton::L, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, | ||
| 916 | {Settings::NativeButton::R, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, | ||
| 917 | {Settings::NativeButton::Plus, SDL_CONTROLLER_BUTTON_START}, | ||
| 918 | {Settings::NativeButton::Minus, SDL_CONTROLLER_BUTTON_BACK}, | ||
| 919 | {Settings::NativeButton::DLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT}, | ||
| 920 | {Settings::NativeButton::DUp, SDL_CONTROLLER_BUTTON_DPAD_UP}, | ||
| 921 | {Settings::NativeButton::DRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT}, | ||
| 922 | {Settings::NativeButton::DDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN}, | ||
| 923 | {Settings::NativeButton::SL, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, | ||
| 924 | {Settings::NativeButton::SR, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, | ||
| 925 | {Settings::NativeButton::Home, SDL_CONTROLLER_BUTTON_GUIDE}, | ||
| 926 | }}; | ||
| 927 | |||
| 928 | // Add the missing bindings for ZL/ZR | ||
| 929 | using ZBindings = | ||
| 930 | std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerAxis>, 2>; | ||
| 931 | static constexpr ZBindings switch_to_sdl_axis{{ | ||
| 932 | {Settings::NativeButton::ZL, SDL_CONTROLLER_AXIS_TRIGGERLEFT}, | ||
| 933 | {Settings::NativeButton::ZR, SDL_CONTROLLER_AXIS_TRIGGERRIGHT}, | ||
| 934 | }}; | ||
| 935 | |||
| 936 | ButtonMapping mapping; | ||
| 937 | mapping.reserve(switch_to_sdl_button.size() + switch_to_sdl_axis.size()); | ||
| 938 | |||
| 939 | for (const auto& [switch_button, sdl_button] : switch_to_sdl_button) { | ||
| 940 | const auto& binding = SDL_GameControllerGetBindForButton(controller, sdl_button); | ||
| 941 | mapping.insert_or_assign( | ||
| 942 | switch_button, | ||
| 943 | BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); | ||
| 944 | } | ||
| 945 | for (const auto& [switch_button, sdl_axis] : switch_to_sdl_axis) { | ||
| 946 | const auto& binding = SDL_GameControllerGetBindForAxis(controller, sdl_axis); | ||
| 947 | mapping.insert_or_assign( | ||
| 948 | switch_button, | ||
| 949 | BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); | ||
| 950 | } | ||
| 951 | |||
| 952 | return mapping; | ||
| 953 | } | ||
| 954 | |||
| 955 | AnalogMapping SDLState::GetAnalogMappingForDevice(const Common::ParamPackage& params) { | ||
| 956 | if (!params.Has("guid") || !params.Has("port")) { | ||
| 957 | return {}; | ||
| 958 | } | ||
| 959 | const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0)); | ||
| 960 | auto* controller = joystick->GetSDLGameController(); | ||
| 961 | if (controller == nullptr) { | ||
| 962 | return {}; | ||
| 963 | } | ||
| 964 | |||
| 965 | AnalogMapping mapping = {}; | ||
| 966 | const auto& binding_left_x = | ||
| 967 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX); | ||
| 968 | const auto& binding_left_y = | ||
| 969 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTY); | ||
| 970 | mapping.insert_or_assign(Settings::NativeAnalog::LStick, | ||
| 971 | BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), | ||
| 972 | binding_left_x.value.axis, | ||
| 973 | binding_left_y.value.axis)); | ||
| 974 | const auto& binding_right_x = | ||
| 975 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX); | ||
| 976 | const auto& binding_right_y = | ||
| 977 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY); | ||
| 978 | mapping.insert_or_assign(Settings::NativeAnalog::RStick, | ||
| 979 | BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), | ||
| 980 | binding_right_x.value.axis, | ||
| 981 | binding_right_y.value.axis)); | ||
| 982 | return mapping; | ||
| 983 | } | ||
| 984 | |||
| 985 | namespace Polling { | ||
| 577 | class SDLPoller : public InputCommon::Polling::DevicePoller { | 986 | class SDLPoller : public InputCommon::Polling::DevicePoller { |
| 578 | public: | 987 | public: |
| 579 | explicit SDLPoller(SDLState& state_) : state(state_) {} | 988 | explicit SDLPoller(SDLState& state_) : state(state_) {} |
| 580 | 989 | ||
| 581 | void Start() override { | 990 | void Start([[maybe_unused]] const std::string& device_id) override { |
| 582 | state.event_queue.Clear(); | 991 | state.event_queue.Clear(); |
| 583 | state.polling = true; | 992 | state.polling = true; |
| 584 | } | 993 | } |
| @@ -598,70 +1007,116 @@ public: | |||
| 598 | Common::ParamPackage GetNextInput() override { | 1007 | Common::ParamPackage GetNextInput() override { |
| 599 | SDL_Event event; | 1008 | SDL_Event event; |
| 600 | while (state.event_queue.Pop(event)) { | 1009 | while (state.event_queue.Pop(event)) { |
| 601 | switch (event.type) { | 1010 | const auto package = FromEvent(event); |
| 602 | case SDL_JOYAXISMOTION: | 1011 | if (package) { |
| 603 | if (std::abs(event.jaxis.value / 32767.0) < 0.5) { | 1012 | return *package; |
| 604 | break; | ||
| 605 | } | ||
| 606 | case SDL_JOYBUTTONUP: | ||
| 607 | case SDL_JOYHATMOTION: | ||
| 608 | return SDLEventToButtonParamPackage(state, event); | ||
| 609 | } | 1013 | } |
| 610 | } | 1014 | } |
| 611 | return {}; | 1015 | return {}; |
| 612 | } | 1016 | } |
| 1017 | [[nodiscard]] std::optional<Common::ParamPackage> FromEvent(const SDL_Event& event) const { | ||
| 1018 | switch (event.type) { | ||
| 1019 | case SDL_JOYAXISMOTION: | ||
| 1020 | if (std::abs(event.jaxis.value / 32767.0) < 0.5) { | ||
| 1021 | break; | ||
| 1022 | } | ||
| 1023 | [[fallthrough]]; | ||
| 1024 | case SDL_JOYBUTTONUP: | ||
| 1025 | case SDL_JOYHATMOTION: | ||
| 1026 | return {SDLEventToButtonParamPackage(state, event)}; | ||
| 1027 | } | ||
| 1028 | return std::nullopt; | ||
| 1029 | } | ||
| 613 | }; | 1030 | }; |
| 614 | 1031 | ||
| 615 | class SDLAnalogPoller final : public SDLPoller { | 1032 | class SDLMotionPoller final : public SDLPoller { |
| 616 | public: | 1033 | public: |
| 617 | explicit SDLAnalogPoller(SDLState& state_) : SDLPoller(state_) {} | 1034 | explicit SDLMotionPoller(SDLState& state_) : SDLPoller(state_) {} |
| 1035 | |||
| 1036 | Common::ParamPackage GetNextInput() override { | ||
| 1037 | SDL_Event event; | ||
| 1038 | while (state.event_queue.Pop(event)) { | ||
| 1039 | const auto package = FromEvent(event); | ||
| 1040 | if (package) { | ||
| 1041 | return *package; | ||
| 1042 | } | ||
| 1043 | } | ||
| 1044 | return {}; | ||
| 1045 | } | ||
| 1046 | [[nodiscard]] std::optional<Common::ParamPackage> FromEvent(const SDL_Event& event) const { | ||
| 1047 | switch (event.type) { | ||
| 1048 | case SDL_JOYAXISMOTION: | ||
| 1049 | if (std::abs(event.jaxis.value / 32767.0) < 0.5) { | ||
| 1050 | break; | ||
| 1051 | } | ||
| 1052 | [[fallthrough]]; | ||
| 1053 | case SDL_JOYBUTTONUP: | ||
| 1054 | case SDL_JOYHATMOTION: | ||
| 1055 | return {SDLEventToMotionParamPackage(state, event)}; | ||
| 1056 | } | ||
| 1057 | return std::nullopt; | ||
| 1058 | } | ||
| 1059 | }; | ||
| 618 | 1060 | ||
| 619 | void Start() override { | 1061 | /** |
| 620 | SDLPoller::Start(); | 1062 | * Attempts to match the press to a controller joy axis (left/right stick) and if a match |
| 1063 | * isn't found, checks if the event matches anything from SDLButtonPoller and uses that | ||
| 1064 | * instead | ||
| 1065 | */ | ||
| 1066 | class SDLAnalogPreferredPoller final : public SDLPoller { | ||
| 1067 | public: | ||
| 1068 | explicit SDLAnalogPreferredPoller(SDLState& state_) | ||
| 1069 | : SDLPoller(state_), button_poller(state_) {} | ||
| 621 | 1070 | ||
| 1071 | void Start(const std::string& device_id) override { | ||
| 1072 | SDLPoller::Start(device_id); | ||
| 622 | // Reset stored axes | 1073 | // Reset stored axes |
| 623 | analog_x_axis = -1; | 1074 | analog_x_axis = -1; |
| 624 | analog_y_axis = -1; | 1075 | analog_y_axis = -1; |
| 625 | analog_axes_joystick = -1; | ||
| 626 | } | 1076 | } |
| 627 | 1077 | ||
| 628 | Common::ParamPackage GetNextInput() override { | 1078 | Common::ParamPackage GetNextInput() override { |
| 629 | SDL_Event event; | 1079 | SDL_Event event; |
| 630 | while (state.event_queue.Pop(event)) { | 1080 | while (state.event_queue.Pop(event)) { |
| 631 | if (event.type != SDL_JOYAXISMOTION || std::abs(event.jaxis.value / 32767.0) < 0.5) { | 1081 | // Filter out axis events that are below a threshold |
| 1082 | if (event.type == SDL_JOYAXISMOTION && std::abs(event.jaxis.value / 32767.0) < 0.5) { | ||
| 632 | continue; | 1083 | continue; |
| 633 | } | 1084 | } |
| 634 | // An analog device needs two axes, so we need to store the axis for later and wait for | 1085 | if (event.type == SDL_JOYAXISMOTION) { |
| 635 | // a second SDL event. The axes also must be from the same joystick. | 1086 | const auto axis = event.jaxis.axis; |
| 636 | const int axis = event.jaxis.axis; | 1087 | // In order to return a complete analog param, we need inputs for both axes. |
| 637 | if (analog_x_axis == -1) { | 1088 | // First we take the x-axis (horizontal) input, then the y-axis (vertical) input. |
| 638 | analog_x_axis = axis; | 1089 | if (analog_x_axis == -1) { |
| 639 | analog_axes_joystick = event.jaxis.which; | 1090 | analog_x_axis = axis; |
| 640 | } else if (analog_y_axis == -1 && analog_x_axis != axis && | 1091 | } else if (analog_y_axis == -1 && analog_x_axis != axis) { |
| 641 | analog_axes_joystick == event.jaxis.which) { | 1092 | analog_y_axis = axis; |
| 642 | analog_y_axis = axis; | 1093 | } |
| 1094 | } else { | ||
| 1095 | // If the press wasn't accepted as a joy axis, check for a button press | ||
| 1096 | auto button_press = button_poller.FromEvent(event); | ||
| 1097 | if (button_press) { | ||
| 1098 | return *button_press; | ||
| 1099 | } | ||
| 643 | } | 1100 | } |
| 644 | } | 1101 | } |
| 645 | Common::ParamPackage params; | 1102 | |
| 646 | if (analog_x_axis != -1 && analog_y_axis != -1) { | 1103 | if (analog_x_axis != -1 && analog_y_axis != -1) { |
| 647 | const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); | 1104 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) { |
| 648 | params.Set("engine", "sdl"); | 1105 | auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), |
| 649 | params.Set("port", joystick->GetPort()); | 1106 | analog_x_axis, analog_y_axis); |
| 650 | params.Set("guid", joystick->GetGUID()); | 1107 | analog_x_axis = -1; |
| 651 | params.Set("axis_x", analog_x_axis); | 1108 | analog_y_axis = -1; |
| 652 | params.Set("axis_y", analog_y_axis); | 1109 | return params; |
| 653 | analog_x_axis = -1; | 1110 | } |
| 654 | analog_y_axis = -1; | ||
| 655 | analog_axes_joystick = -1; | ||
| 656 | return params; | ||
| 657 | } | 1111 | } |
| 658 | return params; | 1112 | |
| 1113 | return {}; | ||
| 659 | } | 1114 | } |
| 660 | 1115 | ||
| 661 | private: | 1116 | private: |
| 662 | int analog_x_axis = -1; | 1117 | int analog_x_axis = -1; |
| 663 | int analog_y_axis = -1; | 1118 | int analog_y_axis = -1; |
| 664 | SDL_JoystickID analog_axes_joystick = -1; | 1119 | SDLButtonPoller button_poller; |
| 665 | }; | 1120 | }; |
| 666 | } // namespace Polling | 1121 | } // namespace Polling |
| 667 | 1122 | ||
| @@ -669,12 +1124,15 @@ SDLState::Pollers SDLState::GetPollers(InputCommon::Polling::DeviceType type) { | |||
| 669 | Pollers pollers; | 1124 | Pollers pollers; |
| 670 | 1125 | ||
| 671 | switch (type) { | 1126 | switch (type) { |
| 672 | case InputCommon::Polling::DeviceType::Analog: | 1127 | case InputCommon::Polling::DeviceType::AnalogPreferred: |
| 673 | pollers.emplace_back(std::make_unique<Polling::SDLAnalogPoller>(*this)); | 1128 | pollers.emplace_back(std::make_unique<Polling::SDLAnalogPreferredPoller>(*this)); |
| 674 | break; | 1129 | break; |
| 675 | case InputCommon::Polling::DeviceType::Button: | 1130 | case InputCommon::Polling::DeviceType::Button: |
| 676 | pollers.emplace_back(std::make_unique<Polling::SDLButtonPoller>(*this)); | 1131 | pollers.emplace_back(std::make_unique<Polling::SDLButtonPoller>(*this)); |
| 677 | break; | 1132 | break; |
| 1133 | case InputCommon::Polling::DeviceType::Motion: | ||
| 1134 | pollers.emplace_back(std::make_unique<Polling::SDLMotionPoller>(*this)); | ||
| 1135 | break; | ||
| 678 | } | 1136 | } |
| 679 | 1137 | ||
| 680 | return pollers; | 1138 | return pollers; |