diff options
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
| -rw-r--r-- | src/input_common/sdl/sdl_impl.cpp | 751 |
1 files changed, 601 insertions, 150 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index d76c279d3..c395d96cf 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}; |
| @@ -68,7 +77,17 @@ public: | |||
| 68 | 77 | ||
| 69 | float GetAxis(int axis, float range) 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 * range); | 80 | return static_cast<float>(state.axes.at(axis)) / (32767.0f * range); |
| 81 | } | ||
| 82 | |||
| 83 | bool RumblePlay(u16 amp_low, u16 amp_high) { | ||
| 84 | if (sdl_controller) { | ||
| 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; | ||
| 72 | } | 91 | } |
| 73 | 92 | ||
| 74 | std::tuple<float, float> GetAnalog(int axis_x, int axis_y, float range) const { | 93 | std::tuple<float, float> GetAnalog(int axis_x, int axis_y, float range) const { |
| @@ -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) { |
| @@ -329,8 +351,8 @@ 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 | float range_) | 355 | float deadzone_, float range_) |
| 334 | : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), | 356 | : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), |
| 335 | range(range_) {} | 357 | range(range_) {} |
| 336 | 358 | ||
| @@ -341,12 +363,12 @@ public: | |||
| 341 | return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), | 363 | return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), |
| 342 | y / r * (r - deadzone) / (1 - deadzone)); | 364 | y / r * (r - deadzone) / (1 - deadzone)); |
| 343 | } | 365 | } |
| 344 | return std::make_tuple<float, float>(0.0f, 0.0f); | 366 | return {}; |
| 345 | } | 367 | } |
| 346 | 368 | ||
| 347 | bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { | 369 | bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { |
| 348 | const auto [x, y] = GetStatus(); | 370 | const auto [x, y] = GetStatus(); |
| 349 | const float directional_deadzone = 0.4f; | 371 | const float directional_deadzone = 0.5f; |
| 350 | switch (direction) { | 372 | switch (direction) { |
| 351 | case Input::AnalogDirection::RIGHT: | 373 | case Input::AnalogDirection::RIGHT: |
| 352 | return x > directional_deadzone; | 374 | return x > directional_deadzone; |
| @@ -368,6 +390,93 @@ private: | |||
| 368 | const float range; | 390 | const float range; |
| 369 | }; | 391 | }; |
| 370 | 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, f32 freq_low, f32 amp_high, f32 freq_high) const override { | ||
| 404 | const auto process_amplitude = [](f32 amplitude) { | ||
| 405 | return static_cast<u16>((amplitude + std::pow(amplitude, 0.3f)) * 0.5f * 0xFFFF); | ||
| 406 | }; | ||
| 407 | |||
| 408 | const auto processed_amp_low = process_amplitude(amp_low); | ||
| 409 | const auto processed_amp_high = process_amplitude(amp_high); | ||
| 410 | |||
| 411 | return joystick->RumblePlay(processed_amp_low, processed_amp_high); | ||
| 412 | } | ||
| 413 | |||
| 414 | private: | ||
| 415 | std::shared_ptr<SDLJoystick> joystick; | ||
| 416 | }; | ||
| 417 | |||
| 418 | class SDLDirectionMotion final : public Input::MotionDevice { | ||
| 419 | public: | ||
| 420 | explicit SDLDirectionMotion(std::shared_ptr<SDLJoystick> joystick_, int hat_, Uint8 direction_) | ||
| 421 | : joystick(std::move(joystick_)), hat(hat_), direction(direction_) {} | ||
| 422 | |||
| 423 | Input::MotionStatus GetStatus() const override { | ||
| 424 | if (joystick->GetHatDirection(hat, direction)) { | ||
| 425 | return joystick->GetMotion().GetRandomMotion(2, 6); | ||
| 426 | } | ||
| 427 | return joystick->GetMotion().GetRandomMotion(0, 0); | ||
| 428 | } | ||
| 429 | |||
| 430 | private: | ||
| 431 | std::shared_ptr<SDLJoystick> joystick; | ||
| 432 | int hat; | ||
| 433 | Uint8 direction; | ||
| 434 | }; | ||
| 435 | |||
| 436 | class SDLAxisMotion final : public Input::MotionDevice { | ||
| 437 | public: | ||
| 438 | explicit SDLAxisMotion(std::shared_ptr<SDLJoystick> joystick_, int axis_, float threshold_, | ||
| 439 | bool trigger_if_greater_) | ||
| 440 | : joystick(std::move(joystick_)), axis(axis_), threshold(threshold_), | ||
| 441 | trigger_if_greater(trigger_if_greater_) {} | ||
| 442 | |||
| 443 | Input::MotionStatus GetStatus() const override { | ||
| 444 | const float axis_value = joystick->GetAxis(axis, 1.0f); | ||
| 445 | bool trigger = axis_value < threshold; | ||
| 446 | if (trigger_if_greater) { | ||
| 447 | trigger = axis_value > threshold; | ||
| 448 | } | ||
| 449 | |||
| 450 | if (trigger) { | ||
| 451 | return joystick->GetMotion().GetRandomMotion(2, 6); | ||
| 452 | } | ||
| 453 | return joystick->GetMotion().GetRandomMotion(0, 0); | ||
| 454 | } | ||
| 455 | |||
| 456 | private: | ||
| 457 | std::shared_ptr<SDLJoystick> joystick; | ||
| 458 | int axis; | ||
| 459 | float threshold; | ||
| 460 | bool trigger_if_greater; | ||
| 461 | }; | ||
| 462 | |||
| 463 | class SDLButtonMotion final : public Input::MotionDevice { | ||
| 464 | public: | ||
| 465 | explicit SDLButtonMotion(std::shared_ptr<SDLJoystick> joystick_, int button_) | ||
| 466 | : joystick(std::move(joystick_)), button(button_) {} | ||
| 467 | |||
| 468 | Input::MotionStatus GetStatus() const override { | ||
| 469 | if (joystick->GetButton(button)) { | ||
| 470 | return joystick->GetMotion().GetRandomMotion(2, 6); | ||
| 471 | } | ||
| 472 | return joystick->GetMotion().GetRandomMotion(0, 0); | ||
| 473 | } | ||
| 474 | |||
| 475 | private: | ||
| 476 | std::shared_ptr<SDLJoystick> joystick; | ||
| 477 | int button; | ||
| 478 | }; | ||
| 479 | |||
| 371 | /// A button device factory that creates button devices from SDL joystick | 480 | /// A button device factory that creates button devices from SDL joystick |
| 372 | class SDLButtonFactory final : public Input::Factory<Input::ButtonDevice> { | 481 | class SDLButtonFactory final : public Input::Factory<Input::ButtonDevice> { |
| 373 | public: | 482 | public: |
| @@ -448,7 +557,7 @@ class SDLAnalogFactory final : public Input::Factory<Input::AnalogDevice> { | |||
| 448 | public: | 557 | public: |
| 449 | explicit SDLAnalogFactory(SDLState& state_) : state(state_) {} | 558 | explicit SDLAnalogFactory(SDLState& state_) : state(state_) {} |
| 450 | /** | 559 | /** |
| 451 | * Creates analog device from joystick axes | 560 | * Creates an analog device from joystick axes |
| 452 | * @param params contains parameters for creating the device: | 561 | * @param params contains parameters for creating the device: |
| 453 | * - "guid": the guid of the joystick to bind | 562 | * - "guid": the guid of the joystick to bind |
| 454 | * - "port": the nth joystick of the same type | 563 | * - "port": the nth joystick of the same type |
| @@ -460,7 +569,7 @@ public: | |||
| 460 | const int port = params.Get("port", 0); | 569 | const int port = params.Get("port", 0); |
| 461 | const int axis_x = params.Get("axis_x", 0); | 570 | const int axis_x = params.Get("axis_x", 0); |
| 462 | const int axis_y = params.Get("axis_y", 1); | 571 | const int axis_y = params.Get("axis_y", 1); |
| 463 | const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); | 572 | const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); |
| 464 | const float range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); | 573 | const float range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); |
| 465 | auto joystick = state.GetSDLJoystickByGUID(guid, port); | 574 | auto joystick = state.GetSDLJoystickByGUID(guid, port); |
| 466 | 575 | ||
| @@ -474,17 +583,108 @@ private: | |||
| 474 | SDLState& state; | 583 | SDLState& state; |
| 475 | }; | 584 | }; |
| 476 | 585 | ||
| 586 | /// An vibration device factory that creates vibration devices from SDL joystick | ||
| 587 | class SDLVibrationFactory final : public Input::Factory<Input::VibrationDevice> { | ||
| 588 | public: | ||
| 589 | explicit SDLVibrationFactory(SDLState& state_) : state(state_) {} | ||
| 590 | /** | ||
| 591 | * Creates a vibration device from a joystick | ||
| 592 | * @param params contains parameters for creating the device: | ||
| 593 | * - "guid": the guid of the joystick to bind | ||
| 594 | * - "port": the nth joystick of the same type | ||
| 595 | */ | ||
| 596 | std::unique_ptr<Input::VibrationDevice> Create(const Common::ParamPackage& params) override { | ||
| 597 | const std::string guid = params.Get("guid", "0"); | ||
| 598 | const int port = params.Get("port", 0); | ||
| 599 | return std::make_unique<SDLVibration>(state.GetSDLJoystickByGUID(guid, port)); | ||
| 600 | } | ||
| 601 | |||
| 602 | private: | ||
| 603 | SDLState& state; | ||
| 604 | }; | ||
| 605 | |||
| 606 | /// A motion device factory that creates motion devices from SDL joystick | ||
| 607 | class SDLMotionFactory final : public Input::Factory<Input::MotionDevice> { | ||
| 608 | public: | ||
| 609 | explicit SDLMotionFactory(SDLState& state_) : state(state_) {} | ||
| 610 | /** | ||
| 611 | * Creates motion device from joystick axes | ||
| 612 | * @param params contains parameters for creating the device: | ||
| 613 | * - "guid": the guid of the joystick to bind | ||
| 614 | * - "port": the nth joystick of the same type | ||
| 615 | */ | ||
| 616 | std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override { | ||
| 617 | const std::string guid = params.Get("guid", "0"); | ||
| 618 | const int port = params.Get("port", 0); | ||
| 619 | |||
| 620 | auto joystick = state.GetSDLJoystickByGUID(guid, port); | ||
| 621 | |||
| 622 | if (params.Has("hat")) { | ||
| 623 | const int hat = params.Get("hat", 0); | ||
| 624 | const std::string direction_name = params.Get("direction", ""); | ||
| 625 | Uint8 direction; | ||
| 626 | if (direction_name == "up") { | ||
| 627 | direction = SDL_HAT_UP; | ||
| 628 | } else if (direction_name == "down") { | ||
| 629 | direction = SDL_HAT_DOWN; | ||
| 630 | } else if (direction_name == "left") { | ||
| 631 | direction = SDL_HAT_LEFT; | ||
| 632 | } else if (direction_name == "right") { | ||
| 633 | direction = SDL_HAT_RIGHT; | ||
| 634 | } else { | ||
| 635 | direction = 0; | ||
| 636 | } | ||
| 637 | // This is necessary so accessing GetHat with hat won't crash | ||
| 638 | joystick->SetHat(hat, SDL_HAT_CENTERED); | ||
| 639 | return std::make_unique<SDLDirectionMotion>(joystick, hat, direction); | ||
| 640 | } | ||
| 641 | |||
| 642 | if (params.Has("axis")) { | ||
| 643 | const int axis = params.Get("axis", 0); | ||
| 644 | const float threshold = params.Get("threshold", 0.5f); | ||
| 645 | const std::string direction_name = params.Get("direction", ""); | ||
| 646 | bool trigger_if_greater; | ||
| 647 | if (direction_name == "+") { | ||
| 648 | trigger_if_greater = true; | ||
| 649 | } else if (direction_name == "-") { | ||
| 650 | trigger_if_greater = false; | ||
| 651 | } else { | ||
| 652 | trigger_if_greater = true; | ||
| 653 | LOG_ERROR(Input, "Unknown direction {}", direction_name); | ||
| 654 | } | ||
| 655 | // This is necessary so accessing GetAxis with axis won't crash | ||
| 656 | joystick->SetAxis(axis, 0); | ||
| 657 | return std::make_unique<SDLAxisMotion>(joystick, axis, threshold, trigger_if_greater); | ||
| 658 | } | ||
| 659 | |||
| 660 | const int button = params.Get("button", 0); | ||
| 661 | // This is necessary so accessing GetButton with button won't crash | ||
| 662 | joystick->SetButton(button, false); | ||
| 663 | return std::make_unique<SDLButtonMotion>(joystick, button); | ||
| 664 | } | ||
| 665 | |||
| 666 | private: | ||
| 667 | SDLState& state; | ||
| 668 | }; | ||
| 669 | |||
| 477 | SDLState::SDLState() { | 670 | SDLState::SDLState() { |
| 478 | using namespace Input; | 671 | using namespace Input; |
| 479 | RegisterFactory<ButtonDevice>("sdl", std::make_shared<SDLButtonFactory>(*this)); | 672 | button_factory = std::make_shared<SDLButtonFactory>(*this); |
| 480 | RegisterFactory<AnalogDevice>("sdl", std::make_shared<SDLAnalogFactory>(*this)); | 673 | analog_factory = std::make_shared<SDLAnalogFactory>(*this); |
| 481 | 674 | vibration_factory = std::make_shared<SDLVibrationFactory>(*this); | |
| 482 | // If the frontend is going to manage the event loop, then we dont start one here | 675 | motion_factory = std::make_shared<SDLMotionFactory>(*this); |
| 483 | start_thread = !SDL_WasInit(SDL_INIT_JOYSTICK); | 676 | RegisterFactory<ButtonDevice>("sdl", button_factory); |
| 677 | RegisterFactory<AnalogDevice>("sdl", analog_factory); | ||
| 678 | RegisterFactory<VibrationDevice>("sdl", vibration_factory); | ||
| 679 | RegisterFactory<MotionDevice>("sdl", motion_factory); | ||
| 680 | |||
| 681 | // If the frontend is going to manage the event loop, then we don't start one here | ||
| 682 | start_thread = SDL_WasInit(SDL_INIT_JOYSTICK) == 0; | ||
| 484 | if (start_thread && SDL_Init(SDL_INIT_JOYSTICK) < 0) { | 683 | if (start_thread && SDL_Init(SDL_INIT_JOYSTICK) < 0) { |
| 485 | LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError()); | 684 | LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError()); |
| 486 | return; | 685 | return; |
| 487 | } | 686 | } |
| 687 | has_gamecontroller = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0; | ||
| 488 | if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) { | 688 | if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) { |
| 489 | LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError()); | 689 | LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError()); |
| 490 | } | 690 | } |
| @@ -497,7 +697,7 @@ SDLState::SDLState() { | |||
| 497 | using namespace std::chrono_literals; | 697 | using namespace std::chrono_literals; |
| 498 | while (initialized) { | 698 | while (initialized) { |
| 499 | SDL_PumpEvents(); | 699 | SDL_PumpEvents(); |
| 500 | std::this_thread::sleep_for(10ms); | 700 | std::this_thread::sleep_for(1ms); |
| 501 | } | 701 | } |
| 502 | }); | 702 | }); |
| 503 | } | 703 | } |
| @@ -512,6 +712,8 @@ SDLState::~SDLState() { | |||
| 512 | using namespace Input; | 712 | using namespace Input; |
| 513 | UnregisterFactory<ButtonDevice>("sdl"); | 713 | UnregisterFactory<ButtonDevice>("sdl"); |
| 514 | UnregisterFactory<AnalogDevice>("sdl"); | 714 | UnregisterFactory<AnalogDevice>("sdl"); |
| 715 | UnregisterFactory<VibrationDevice>("sdl"); | ||
| 716 | UnregisterFactory<MotionDevice>("sdl"); | ||
| 515 | 717 | ||
| 516 | CloseJoysticks(); | 718 | CloseJoysticks(); |
| 517 | SDL_DelEventWatch(&SDLEventWatcher, this); | 719 | SDL_DelEventWatch(&SDLEventWatcher, this); |
| @@ -523,65 +725,266 @@ SDLState::~SDLState() { | |||
| 523 | } | 725 | } |
| 524 | } | 726 | } |
| 525 | 727 | ||
| 526 | static Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Event& event) { | 728 | std::vector<Common::ParamPackage> SDLState::GetInputDevices() { |
| 729 | std::scoped_lock lock(joystick_map_mutex); | ||
| 730 | std::vector<Common::ParamPackage> devices; | ||
| 731 | for (const auto& [key, value] : joystick_map) { | ||
| 732 | for (const auto& joystick : value) { | ||
| 733 | if (auto* const controller = joystick->GetSDLGameController()) { | ||
| 734 | std::string name = | ||
| 735 | fmt::format("{} {}", SDL_GameControllerName(controller), joystick->GetPort()); | ||
| 736 | devices.emplace_back(Common::ParamPackage{ | ||
| 737 | {"class", "sdl"}, | ||
| 738 | {"display", std::move(name)}, | ||
| 739 | {"guid", joystick->GetGUID()}, | ||
| 740 | {"port", std::to_string(joystick->GetPort())}, | ||
| 741 | }); | ||
| 742 | } else if (auto* const joy = joystick->GetSDLJoystick()) { | ||
| 743 | std::string name = fmt::format("{} {}", SDL_JoystickName(joy), joystick->GetPort()); | ||
| 744 | devices.emplace_back(Common::ParamPackage{ | ||
| 745 | {"class", "sdl"}, | ||
| 746 | {"display", std::move(name)}, | ||
| 747 | {"guid", joystick->GetGUID()}, | ||
| 748 | {"port", std::to_string(joystick->GetPort())}, | ||
| 749 | }); | ||
| 750 | } | ||
| 751 | } | ||
| 752 | } | ||
| 753 | return devices; | ||
| 754 | } | ||
| 755 | |||
| 756 | namespace { | ||
| 757 | Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, s32 axis, | ||
| 758 | float value = 0.1f) { | ||
| 759 | Common::ParamPackage params({{"engine", "sdl"}}); | ||
| 760 | params.Set("port", port); | ||
| 761 | params.Set("guid", std::move(guid)); | ||
| 762 | params.Set("axis", axis); | ||
| 763 | if (value > 0) { | ||
| 764 | params.Set("direction", "+"); | ||
| 765 | params.Set("threshold", "0.5"); | ||
| 766 | } else { | ||
| 767 | params.Set("direction", "-"); | ||
| 768 | params.Set("threshold", "-0.5"); | ||
| 769 | } | ||
| 770 | return params; | ||
| 771 | } | ||
| 772 | |||
| 773 | Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, s32 button) { | ||
| 774 | Common::ParamPackage params({{"engine", "sdl"}}); | ||
| 775 | params.Set("port", port); | ||
| 776 | params.Set("guid", std::move(guid)); | ||
| 777 | params.Set("button", button); | ||
| 778 | return params; | ||
| 779 | } | ||
| 780 | |||
| 781 | Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, s32 hat, s32 value) { | ||
| 527 | Common::ParamPackage params({{"engine", "sdl"}}); | 782 | Common::ParamPackage params({{"engine", "sdl"}}); |
| 528 | 783 | ||
| 784 | params.Set("port", port); | ||
| 785 | params.Set("guid", std::move(guid)); | ||
| 786 | params.Set("hat", hat); | ||
| 787 | switch (value) { | ||
| 788 | case SDL_HAT_UP: | ||
| 789 | params.Set("direction", "up"); | ||
| 790 | break; | ||
| 791 | case SDL_HAT_DOWN: | ||
| 792 | params.Set("direction", "down"); | ||
| 793 | break; | ||
| 794 | case SDL_HAT_LEFT: | ||
| 795 | params.Set("direction", "left"); | ||
| 796 | break; | ||
| 797 | case SDL_HAT_RIGHT: | ||
| 798 | params.Set("direction", "right"); | ||
| 799 | break; | ||
| 800 | default: | ||
| 801 | return {}; | ||
| 802 | } | ||
| 803 | return params; | ||
| 804 | } | ||
| 805 | |||
| 806 | Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Event& event) { | ||
| 529 | switch (event.type) { | 807 | switch (event.type) { |
| 530 | case SDL_JOYAXISMOTION: { | 808 | case SDL_JOYAXISMOTION: { |
| 531 | const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); | 809 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) { |
| 532 | params.Set("port", joystick->GetPort()); | 810 | return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), |
| 533 | params.Set("guid", joystick->GetGUID()); | 811 | static_cast<s32>(event.jaxis.axis), |
| 534 | params.Set("axis", event.jaxis.axis); | 812 | event.jaxis.value); |
| 535 | if (event.jaxis.value > 0) { | ||
| 536 | params.Set("direction", "+"); | ||
| 537 | params.Set("threshold", "0.5"); | ||
| 538 | } else { | ||
| 539 | params.Set("direction", "-"); | ||
| 540 | params.Set("threshold", "-0.5"); | ||
| 541 | } | 813 | } |
| 542 | break; | 814 | break; |
| 543 | } | 815 | } |
| 544 | case SDL_JOYBUTTONUP: { | 816 | case SDL_JOYBUTTONUP: { |
| 545 | const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which); | 817 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which)) { |
| 546 | params.Set("port", joystick->GetPort()); | 818 | return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), |
| 547 | params.Set("guid", joystick->GetGUID()); | 819 | static_cast<s32>(event.jbutton.button)); |
| 548 | params.Set("button", event.jbutton.button); | 820 | } |
| 549 | break; | 821 | break; |
| 550 | } | 822 | } |
| 551 | case SDL_JOYHATMOTION: { | 823 | case SDL_JOYHATMOTION: { |
| 552 | const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which); | 824 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which)) { |
| 553 | params.Set("port", joystick->GetPort()); | 825 | return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), |
| 554 | params.Set("guid", joystick->GetGUID()); | 826 | static_cast<s32>(event.jhat.hat), |
| 555 | params.Set("hat", event.jhat.hat); | 827 | static_cast<s32>(event.jhat.value)); |
| 556 | switch (event.jhat.value) { | ||
| 557 | case SDL_HAT_UP: | ||
| 558 | params.Set("direction", "up"); | ||
| 559 | break; | ||
| 560 | case SDL_HAT_DOWN: | ||
| 561 | params.Set("direction", "down"); | ||
| 562 | break; | ||
| 563 | case SDL_HAT_LEFT: | ||
| 564 | params.Set("direction", "left"); | ||
| 565 | break; | ||
| 566 | case SDL_HAT_RIGHT: | ||
| 567 | params.Set("direction", "right"); | ||
| 568 | break; | ||
| 569 | default: | ||
| 570 | return {}; | ||
| 571 | } | 828 | } |
| 572 | break; | 829 | break; |
| 573 | } | 830 | } |
| 574 | } | 831 | } |
| 832 | return {}; | ||
| 833 | } | ||
| 834 | |||
| 835 | Common::ParamPackage SDLEventToMotionParamPackage(SDLState& state, const SDL_Event& event) { | ||
| 836 | switch (event.type) { | ||
| 837 | case SDL_JOYAXISMOTION: { | ||
| 838 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) { | ||
| 839 | return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), | ||
| 840 | static_cast<s32>(event.jaxis.axis), | ||
| 841 | event.jaxis.value); | ||
| 842 | } | ||
| 843 | break; | ||
| 844 | } | ||
| 845 | case SDL_JOYBUTTONUP: { | ||
| 846 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which)) { | ||
| 847 | return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), | ||
| 848 | static_cast<s32>(event.jbutton.button)); | ||
| 849 | } | ||
| 850 | break; | ||
| 851 | } | ||
| 852 | case SDL_JOYHATMOTION: { | ||
| 853 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which)) { | ||
| 854 | return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), | ||
| 855 | static_cast<s32>(event.jhat.hat), | ||
| 856 | static_cast<s32>(event.jhat.value)); | ||
| 857 | } | ||
| 858 | break; | ||
| 859 | } | ||
| 860 | } | ||
| 861 | return {}; | ||
| 862 | } | ||
| 863 | |||
| 864 | Common::ParamPackage BuildParamPackageForBinding(int port, const std::string& guid, | ||
| 865 | const SDL_GameControllerButtonBind& binding) { | ||
| 866 | switch (binding.bindType) { | ||
| 867 | case SDL_CONTROLLER_BINDTYPE_AXIS: | ||
| 868 | return BuildAnalogParamPackageForButton(port, guid, binding.value.axis); | ||
| 869 | case SDL_CONTROLLER_BINDTYPE_BUTTON: | ||
| 870 | return BuildButtonParamPackageForButton(port, guid, binding.value.button); | ||
| 871 | case SDL_CONTROLLER_BINDTYPE_HAT: | ||
| 872 | return BuildHatParamPackageForButton(port, guid, binding.value.hat.hat, | ||
| 873 | binding.value.hat.hat_mask); | ||
| 874 | } | ||
| 875 | return {}; | ||
| 876 | } | ||
| 877 | |||
| 878 | Common::ParamPackage BuildParamPackageForAnalog(int port, const std::string& guid, int axis_x, | ||
| 879 | int axis_y) { | ||
| 880 | Common::ParamPackage params; | ||
| 881 | params.Set("engine", "sdl"); | ||
| 882 | params.Set("port", port); | ||
| 883 | params.Set("guid", guid); | ||
| 884 | params.Set("axis_x", axis_x); | ||
| 885 | params.Set("axis_y", axis_y); | ||
| 575 | return params; | 886 | return params; |
| 576 | } | 887 | } |
| 888 | } // Anonymous namespace | ||
| 577 | 889 | ||
| 578 | namespace Polling { | 890 | ButtonMapping SDLState::GetButtonMappingForDevice(const Common::ParamPackage& params) { |
| 891 | if (!params.Has("guid") || !params.Has("port")) { | ||
| 892 | return {}; | ||
| 893 | } | ||
| 894 | const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0)); | ||
| 895 | auto* controller = joystick->GetSDLGameController(); | ||
| 896 | if (controller == nullptr) { | ||
| 897 | return {}; | ||
| 898 | } | ||
| 899 | |||
| 900 | // This list is missing ZL/ZR since those are not considered buttons in SDL GameController. | ||
| 901 | // We will add those afterwards | ||
| 902 | // This list also excludes Screenshot since theres not really a mapping for that | ||
| 903 | using ButtonBindings = | ||
| 904 | std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerButton>, 17>; | ||
| 905 | static constexpr ButtonBindings switch_to_sdl_button{{ | ||
| 906 | {Settings::NativeButton::A, SDL_CONTROLLER_BUTTON_B}, | ||
| 907 | {Settings::NativeButton::B, SDL_CONTROLLER_BUTTON_A}, | ||
| 908 | {Settings::NativeButton::X, SDL_CONTROLLER_BUTTON_Y}, | ||
| 909 | {Settings::NativeButton::Y, SDL_CONTROLLER_BUTTON_X}, | ||
| 910 | {Settings::NativeButton::LStick, SDL_CONTROLLER_BUTTON_LEFTSTICK}, | ||
| 911 | {Settings::NativeButton::RStick, SDL_CONTROLLER_BUTTON_RIGHTSTICK}, | ||
| 912 | {Settings::NativeButton::L, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, | ||
| 913 | {Settings::NativeButton::R, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, | ||
| 914 | {Settings::NativeButton::Plus, SDL_CONTROLLER_BUTTON_START}, | ||
| 915 | {Settings::NativeButton::Minus, SDL_CONTROLLER_BUTTON_BACK}, | ||
| 916 | {Settings::NativeButton::DLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT}, | ||
| 917 | {Settings::NativeButton::DUp, SDL_CONTROLLER_BUTTON_DPAD_UP}, | ||
| 918 | {Settings::NativeButton::DRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT}, | ||
| 919 | {Settings::NativeButton::DDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN}, | ||
| 920 | {Settings::NativeButton::SL, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, | ||
| 921 | {Settings::NativeButton::SR, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, | ||
| 922 | {Settings::NativeButton::Home, SDL_CONTROLLER_BUTTON_GUIDE}, | ||
| 923 | }}; | ||
| 924 | |||
| 925 | // Add the missing bindings for ZL/ZR | ||
| 926 | using ZBindings = | ||
| 927 | std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerAxis>, 2>; | ||
| 928 | static constexpr ZBindings switch_to_sdl_axis{{ | ||
| 929 | {Settings::NativeButton::ZL, SDL_CONTROLLER_AXIS_TRIGGERLEFT}, | ||
| 930 | {Settings::NativeButton::ZR, SDL_CONTROLLER_AXIS_TRIGGERRIGHT}, | ||
| 931 | }}; | ||
| 932 | |||
| 933 | ButtonMapping mapping; | ||
| 934 | mapping.reserve(switch_to_sdl_button.size() + switch_to_sdl_axis.size()); | ||
| 935 | |||
| 936 | for (const auto& [switch_button, sdl_button] : switch_to_sdl_button) { | ||
| 937 | const auto& binding = SDL_GameControllerGetBindForButton(controller, sdl_button); | ||
| 938 | mapping.insert_or_assign( | ||
| 939 | switch_button, | ||
| 940 | BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); | ||
| 941 | } | ||
| 942 | for (const auto& [switch_button, sdl_axis] : switch_to_sdl_axis) { | ||
| 943 | const auto& binding = SDL_GameControllerGetBindForAxis(controller, sdl_axis); | ||
| 944 | mapping.insert_or_assign( | ||
| 945 | switch_button, | ||
| 946 | BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); | ||
| 947 | } | ||
| 948 | |||
| 949 | return mapping; | ||
| 950 | } | ||
| 951 | |||
| 952 | AnalogMapping SDLState::GetAnalogMappingForDevice(const Common::ParamPackage& params) { | ||
| 953 | if (!params.Has("guid") || !params.Has("port")) { | ||
| 954 | return {}; | ||
| 955 | } | ||
| 956 | const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0)); | ||
| 957 | auto* controller = joystick->GetSDLGameController(); | ||
| 958 | if (controller == nullptr) { | ||
| 959 | return {}; | ||
| 960 | } | ||
| 961 | |||
| 962 | AnalogMapping mapping = {}; | ||
| 963 | const auto& binding_left_x = | ||
| 964 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX); | ||
| 965 | const auto& binding_left_y = | ||
| 966 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTY); | ||
| 967 | mapping.insert_or_assign(Settings::NativeAnalog::LStick, | ||
| 968 | BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), | ||
| 969 | binding_left_x.value.axis, | ||
| 970 | binding_left_y.value.axis)); | ||
| 971 | const auto& binding_right_x = | ||
| 972 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX); | ||
| 973 | const auto& binding_right_y = | ||
| 974 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY); | ||
| 975 | mapping.insert_or_assign(Settings::NativeAnalog::RStick, | ||
| 976 | BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), | ||
| 977 | binding_right_x.value.axis, | ||
| 978 | binding_right_y.value.axis)); | ||
| 979 | return mapping; | ||
| 980 | } | ||
| 579 | 981 | ||
| 982 | namespace Polling { | ||
| 580 | class SDLPoller : public InputCommon::Polling::DevicePoller { | 983 | class SDLPoller : public InputCommon::Polling::DevicePoller { |
| 581 | public: | 984 | public: |
| 582 | explicit SDLPoller(SDLState& state_) : state(state_) {} | 985 | explicit SDLPoller(SDLState& state_) : state(state_) {} |
| 583 | 986 | ||
| 584 | void Start() override { | 987 | void Start(const std::string& device_id) override { |
| 585 | state.event_queue.Clear(); | 988 | state.event_queue.Clear(); |
| 586 | state.polling = true; | 989 | state.polling = true; |
| 587 | } | 990 | } |
| @@ -601,71 +1004,116 @@ public: | |||
| 601 | Common::ParamPackage GetNextInput() override { | 1004 | Common::ParamPackage GetNextInput() override { |
| 602 | SDL_Event event; | 1005 | SDL_Event event; |
| 603 | while (state.event_queue.Pop(event)) { | 1006 | while (state.event_queue.Pop(event)) { |
| 604 | switch (event.type) { | 1007 | const auto package = FromEvent(event); |
| 605 | case SDL_JOYAXISMOTION: | 1008 | if (package) { |
| 606 | if (std::abs(event.jaxis.value / 32767.0) < 0.5) { | 1009 | return *package; |
| 607 | break; | ||
| 608 | } | ||
| 609 | [[fallthrough]]; | ||
| 610 | case SDL_JOYBUTTONUP: | ||
| 611 | case SDL_JOYHATMOTION: | ||
| 612 | return SDLEventToButtonParamPackage(state, event); | ||
| 613 | } | 1010 | } |
| 614 | } | 1011 | } |
| 615 | return {}; | 1012 | return {}; |
| 616 | } | 1013 | } |
| 1014 | [[nodiscard]] std::optional<Common::ParamPackage> FromEvent(const SDL_Event& event) const { | ||
| 1015 | switch (event.type) { | ||
| 1016 | case SDL_JOYAXISMOTION: | ||
| 1017 | if (std::abs(event.jaxis.value / 32767.0) < 0.5) { | ||
| 1018 | break; | ||
| 1019 | } | ||
| 1020 | [[fallthrough]]; | ||
| 1021 | case SDL_JOYBUTTONUP: | ||
| 1022 | case SDL_JOYHATMOTION: | ||
| 1023 | return {SDLEventToButtonParamPackage(state, event)}; | ||
| 1024 | } | ||
| 1025 | return std::nullopt; | ||
| 1026 | } | ||
| 617 | }; | 1027 | }; |
| 618 | 1028 | ||
| 619 | class SDLAnalogPoller final : public SDLPoller { | 1029 | class SDLMotionPoller final : public SDLPoller { |
| 620 | public: | 1030 | public: |
| 621 | explicit SDLAnalogPoller(SDLState& state_) : SDLPoller(state_) {} | 1031 | explicit SDLMotionPoller(SDLState& state_) : SDLPoller(state_) {} |
| 1032 | |||
| 1033 | Common::ParamPackage GetNextInput() override { | ||
| 1034 | SDL_Event event; | ||
| 1035 | while (state.event_queue.Pop(event)) { | ||
| 1036 | const auto package = FromEvent(event); | ||
| 1037 | if (package) { | ||
| 1038 | return *package; | ||
| 1039 | } | ||
| 1040 | } | ||
| 1041 | return {}; | ||
| 1042 | } | ||
| 1043 | [[nodiscard]] std::optional<Common::ParamPackage> FromEvent(const SDL_Event& event) const { | ||
| 1044 | switch (event.type) { | ||
| 1045 | case SDL_JOYAXISMOTION: | ||
| 1046 | if (std::abs(event.jaxis.value / 32767.0) < 0.5) { | ||
| 1047 | break; | ||
| 1048 | } | ||
| 1049 | [[fallthrough]]; | ||
| 1050 | case SDL_JOYBUTTONUP: | ||
| 1051 | case SDL_JOYHATMOTION: | ||
| 1052 | return {SDLEventToMotionParamPackage(state, event)}; | ||
| 1053 | } | ||
| 1054 | return std::nullopt; | ||
| 1055 | } | ||
| 1056 | }; | ||
| 622 | 1057 | ||
| 623 | void Start() override { | 1058 | /** |
| 624 | SDLPoller::Start(); | 1059 | * Attempts to match the press to a controller joy axis (left/right stick) and if a match |
| 1060 | * isn't found, checks if the event matches anything from SDLButtonPoller and uses that | ||
| 1061 | * instead | ||
| 1062 | */ | ||
| 1063 | class SDLAnalogPreferredPoller final : public SDLPoller { | ||
| 1064 | public: | ||
| 1065 | explicit SDLAnalogPreferredPoller(SDLState& state_) | ||
| 1066 | : SDLPoller(state_), button_poller(state_) {} | ||
| 625 | 1067 | ||
| 1068 | void Start(const std::string& device_id) override { | ||
| 1069 | SDLPoller::Start(device_id); | ||
| 626 | // Reset stored axes | 1070 | // Reset stored axes |
| 627 | analog_x_axis = -1; | 1071 | analog_x_axis = -1; |
| 628 | analog_y_axis = -1; | 1072 | analog_y_axis = -1; |
| 629 | analog_axes_joystick = -1; | ||
| 630 | } | 1073 | } |
| 631 | 1074 | ||
| 632 | Common::ParamPackage GetNextInput() override { | 1075 | Common::ParamPackage GetNextInput() override { |
| 633 | SDL_Event event; | 1076 | SDL_Event event; |
| 634 | while (state.event_queue.Pop(event)) { | 1077 | while (state.event_queue.Pop(event)) { |
| 635 | if (event.type != SDL_JOYAXISMOTION || std::abs(event.jaxis.value / 32767.0) < 0.5) { | 1078 | // Filter out axis events that are below a threshold |
| 1079 | if (event.type == SDL_JOYAXISMOTION && std::abs(event.jaxis.value / 32767.0) < 0.5) { | ||
| 636 | continue; | 1080 | continue; |
| 637 | } | 1081 | } |
| 638 | // An analog device needs two axes, so we need to store the axis for later and wait for | 1082 | if (event.type == SDL_JOYAXISMOTION) { |
| 639 | // a second SDL event. The axes also must be from the same joystick. | 1083 | const auto axis = event.jaxis.axis; |
| 640 | const int axis = event.jaxis.axis; | 1084 | // In order to return a complete analog param, we need inputs for both axes. |
| 641 | if (analog_x_axis == -1) { | 1085 | // First we take the x-axis (horizontal) input, then the y-axis (vertical) input. |
| 642 | analog_x_axis = axis; | 1086 | if (analog_x_axis == -1) { |
| 643 | analog_axes_joystick = event.jaxis.which; | 1087 | analog_x_axis = axis; |
| 644 | } else if (analog_y_axis == -1 && analog_x_axis != axis && | 1088 | } else if (analog_y_axis == -1 && analog_x_axis != axis) { |
| 645 | analog_axes_joystick == event.jaxis.which) { | 1089 | analog_y_axis = axis; |
| 646 | analog_y_axis = axis; | 1090 | } |
| 1091 | } else { | ||
| 1092 | // If the press wasn't accepted as a joy axis, check for a button press | ||
| 1093 | auto button_press = button_poller.FromEvent(event); | ||
| 1094 | if (button_press) { | ||
| 1095 | return *button_press; | ||
| 1096 | } | ||
| 647 | } | 1097 | } |
| 648 | } | 1098 | } |
| 649 | Common::ParamPackage params; | 1099 | |
| 650 | if (analog_x_axis != -1 && analog_y_axis != -1) { | 1100 | if (analog_x_axis != -1 && analog_y_axis != -1) { |
| 651 | const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); | 1101 | if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) { |
| 652 | params.Set("engine", "sdl"); | 1102 | auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), |
| 653 | params.Set("port", joystick->GetPort()); | 1103 | analog_x_axis, analog_y_axis); |
| 654 | params.Set("guid", joystick->GetGUID()); | 1104 | analog_x_axis = -1; |
| 655 | params.Set("axis_x", analog_x_axis); | 1105 | analog_y_axis = -1; |
| 656 | params.Set("axis_y", analog_y_axis); | 1106 | return params; |
| 657 | analog_x_axis = -1; | 1107 | } |
| 658 | analog_y_axis = -1; | ||
| 659 | analog_axes_joystick = -1; | ||
| 660 | return params; | ||
| 661 | } | 1108 | } |
| 662 | return params; | 1109 | |
| 1110 | return {}; | ||
| 663 | } | 1111 | } |
| 664 | 1112 | ||
| 665 | private: | 1113 | private: |
| 666 | int analog_x_axis = -1; | 1114 | int analog_x_axis = -1; |
| 667 | int analog_y_axis = -1; | 1115 | int analog_y_axis = -1; |
| 668 | SDL_JoystickID analog_axes_joystick = -1; | 1116 | SDLButtonPoller button_poller; |
| 669 | }; | 1117 | }; |
| 670 | } // namespace Polling | 1118 | } // namespace Polling |
| 671 | 1119 | ||
| @@ -673,12 +1121,15 @@ SDLState::Pollers SDLState::GetPollers(InputCommon::Polling::DeviceType type) { | |||
| 673 | Pollers pollers; | 1121 | Pollers pollers; |
| 674 | 1122 | ||
| 675 | switch (type) { | 1123 | switch (type) { |
| 676 | case InputCommon::Polling::DeviceType::Analog: | 1124 | case InputCommon::Polling::DeviceType::AnalogPreferred: |
| 677 | pollers.emplace_back(std::make_unique<Polling::SDLAnalogPoller>(*this)); | 1125 | pollers.emplace_back(std::make_unique<Polling::SDLAnalogPreferredPoller>(*this)); |
| 678 | break; | 1126 | break; |
| 679 | case InputCommon::Polling::DeviceType::Button: | 1127 | case InputCommon::Polling::DeviceType::Button: |
| 680 | pollers.emplace_back(std::make_unique<Polling::SDLButtonPoller>(*this)); | 1128 | pollers.emplace_back(std::make_unique<Polling::SDLButtonPoller>(*this)); |
| 681 | break; | 1129 | break; |
| 1130 | case InputCommon::Polling::DeviceType::Motion: | ||
| 1131 | pollers.emplace_back(std::make_unique<Polling::SDLMotionPoller>(*this)); | ||
| 1132 | break; | ||
| 682 | } | 1133 | } |
| 683 | 1134 | ||
| 684 | return pollers; | 1135 | return pollers; |