diff options
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
| -rw-r--r-- | src/input_common/sdl/sdl_impl.cpp | 675 |
1 files changed, 563 insertions, 112 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index d76c279d3..8c2cef35d 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,34 @@ 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(f32 amp_low, f32 amp_high, u32 time) { | ||
| 84 | const u16 raw_amp_low = static_cast<u16>(amp_low * 0xFFFF); | ||
| 85 | const u16 raw_amp_high = static_cast<u16>(amp_high * 0xFFFF); | ||
| 86 | // Lower drastically the number of state changes | ||
| 87 | if (raw_amp_low >> 11 == last_state_rumble_low >> 11 && | ||
| 88 | raw_amp_high >> 11 == last_state_rumble_high >> 11) { | ||
| 89 | if (raw_amp_low + raw_amp_high != 0 || | ||
| 90 | last_state_rumble_low + last_state_rumble_high == 0) { | ||
| 91 | return false; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | // Don't change state if last vibration was < 20ms | ||
| 95 | const auto now = std::chrono::system_clock::now(); | ||
| 96 | if (std::chrono::duration_cast<std::chrono::milliseconds>(now - last_vibration) < | ||
| 97 | std::chrono::milliseconds(20)) { | ||
| 98 | return raw_amp_low + raw_amp_high == 0; | ||
| 99 | } | ||
| 100 | |||
| 101 | last_vibration = now; | ||
| 102 | last_state_rumble_low = raw_amp_low; | ||
| 103 | last_state_rumble_high = raw_amp_high; | ||
| 104 | if (sdl_joystick) { | ||
| 105 | SDL_JoystickRumble(sdl_joystick.get(), raw_amp_low, raw_amp_high, time); | ||
| 106 | } | ||
| 107 | return false; | ||
| 72 | } | 108 | } |
| 73 | 109 | ||
| 74 | std::tuple<float, float> GetAnalog(int axis_x, int axis_y, float range) const { | 110 | std::tuple<float, float> GetAnalog(int axis_x, int axis_y, float range) const { |
| @@ -88,6 +124,10 @@ public: | |||
| 88 | return std::make_tuple(x, y); | 124 | return std::make_tuple(x, y); |
| 89 | } | 125 | } |
| 90 | 126 | ||
| 127 | const MotionInput& GetMotion() const { | ||
| 128 | return motion; | ||
| 129 | } | ||
| 130 | |||
| 91 | void SetHat(int hat, Uint8 direction) { | 131 | void SetHat(int hat, Uint8 direction) { |
| 92 | std::lock_guard lock{mutex}; | 132 | std::lock_guard lock{mutex}; |
| 93 | state.hats.insert_or_assign(hat, direction); | 133 | state.hats.insert_or_assign(hat, direction); |
| @@ -115,10 +155,15 @@ public: | |||
| 115 | return sdl_joystick.get(); | 155 | return sdl_joystick.get(); |
| 116 | } | 156 | } |
| 117 | 157 | ||
| 118 | void SetSDLJoystick(SDL_Joystick* joystick) { | 158 | void SetSDLJoystick(SDL_Joystick* joystick, SDL_GameController* controller) { |
| 159 | sdl_controller.reset(controller); | ||
| 119 | sdl_joystick.reset(joystick); | 160 | sdl_joystick.reset(joystick); |
| 120 | } | 161 | } |
| 121 | 162 | ||
| 163 | SDL_GameController* GetSDLGameController() const { | ||
| 164 | return sdl_controller.get(); | ||
| 165 | } | ||
| 166 | |||
| 122 | private: | 167 | private: |
| 123 | struct State { | 168 | struct State { |
| 124 | std::unordered_map<int, bool> buttons; | 169 | std::unordered_map<int, bool> buttons; |
| @@ -127,8 +172,15 @@ private: | |||
| 127 | } state; | 172 | } state; |
| 128 | std::string guid; | 173 | std::string guid; |
| 129 | int port; | 174 | int port; |
| 175 | u16 last_state_rumble_high = 0; | ||
| 176 | u16 last_state_rumble_low = 0; | ||
| 177 | std::chrono::time_point<std::chrono::system_clock> last_vibration; | ||
| 130 | std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; | 178 | std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; |
| 179 | std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller; | ||
| 131 | mutable std::mutex mutex; | 180 | mutable std::mutex mutex; |
| 181 | |||
| 182 | // Motion is initialized without PID values as motion input is not aviable for SDL2 | ||
| 183 | MotionInput motion{0.0f, 0.0f, 0.0f}; | ||
| 132 | }; | 184 | }; |
| 133 | 185 | ||
| 134 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { | 186 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { |
| @@ -136,18 +188,19 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& g | |||
| 136 | const auto it = joystick_map.find(guid); | 188 | const auto it = joystick_map.find(guid); |
| 137 | if (it != joystick_map.end()) { | 189 | if (it != joystick_map.end()) { |
| 138 | while (it->second.size() <= static_cast<std::size_t>(port)) { | 190 | while (it->second.size() <= static_cast<std::size_t>(port)) { |
| 139 | auto joystick = | 191 | 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); | 192 | nullptr, nullptr); |
| 141 | it->second.emplace_back(std::move(joystick)); | 193 | it->second.emplace_back(std::move(joystick)); |
| 142 | } | 194 | } |
| 143 | return it->second[port]; | 195 | return it->second[static_cast<std::size_t>(port)]; |
| 144 | } | 196 | } |
| 145 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr); | 197 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr, nullptr); |
| 146 | return joystick_map[guid].emplace_back(std::move(joystick)); | 198 | return joystick_map[guid].emplace_back(std::move(joystick)); |
| 147 | } | 199 | } |
| 148 | 200 | ||
| 149 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) { | 201 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) { |
| 150 | auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); | 202 | auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); |
| 203 | auto sdl_controller = SDL_GameControllerFromInstanceID(sdl_id); | ||
| 151 | const std::string guid = GetGUID(sdl_joystick); | 204 | const std::string guid = GetGUID(sdl_joystick); |
| 152 | 205 | ||
| 153 | std::lock_guard lock{joystick_map_mutex}; | 206 | std::lock_guard lock{joystick_map_mutex}; |
| @@ -159,7 +212,7 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_ | |||
| 159 | return sdl_joystick == joystick->GetSDLJoystick(); | 212 | return sdl_joystick == joystick->GetSDLJoystick(); |
| 160 | }); | 213 | }); |
| 161 | if (vec_it != map_it->second.end()) { | 214 | if (vec_it != map_it->second.end()) { |
| 162 | // This is the common case: There is already an existing SDL_Joystick maped to a | 215 | // This is the common case: There is already an existing SDL_Joystick mapped to a |
| 163 | // SDLJoystick. return the SDLJoystick | 216 | // SDLJoystick. return the SDLJoystick |
| 164 | return *vec_it; | 217 | return *vec_it; |
| 165 | } | 218 | } |
| @@ -167,36 +220,40 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_ | |||
| 167 | // Search for a SDLJoystick without a mapped SDL_Joystick... | 220 | // Search for a SDLJoystick without a mapped SDL_Joystick... |
| 168 | const auto nullptr_it = std::find_if(map_it->second.begin(), map_it->second.end(), | 221 | const auto nullptr_it = std::find_if(map_it->second.begin(), map_it->second.end(), |
| 169 | [](const std::shared_ptr<SDLJoystick>& joystick) { | 222 | [](const std::shared_ptr<SDLJoystick>& joystick) { |
| 170 | return !joystick->GetSDLJoystick(); | 223 | return joystick->GetSDLJoystick() == nullptr; |
| 171 | }); | 224 | }); |
| 172 | if (nullptr_it != map_it->second.end()) { | 225 | if (nullptr_it != map_it->second.end()) { |
| 173 | // ... and map it | 226 | // ... and map it |
| 174 | (*nullptr_it)->SetSDLJoystick(sdl_joystick); | 227 | (*nullptr_it)->SetSDLJoystick(sdl_joystick, sdl_controller); |
| 175 | return *nullptr_it; | 228 | return *nullptr_it; |
| 176 | } | 229 | } |
| 177 | 230 | ||
| 178 | // There is no SDLJoystick without a mapped SDL_Joystick | 231 | // There is no SDLJoystick without a mapped SDL_Joystick |
| 179 | // Create a new SDLJoystick | 232 | // Create a new SDLJoystick |
| 180 | const int port = static_cast<int>(map_it->second.size()); | 233 | const int port = static_cast<int>(map_it->second.size()); |
| 181 | auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick); | 234 | auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick, sdl_controller); |
| 182 | return map_it->second.emplace_back(std::move(joystick)); | 235 | return map_it->second.emplace_back(std::move(joystick)); |
| 183 | } | 236 | } |
| 184 | 237 | ||
| 185 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick); | 238 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_controller); |
| 186 | return joystick_map[guid].emplace_back(std::move(joystick)); | 239 | return joystick_map[guid].emplace_back(std::move(joystick)); |
| 187 | } | 240 | } |
| 188 | 241 | ||
| 189 | void SDLState::InitJoystick(int joystick_index) { | 242 | void SDLState::InitJoystick(int joystick_index) { |
| 190 | SDL_Joystick* sdl_joystick = SDL_JoystickOpen(joystick_index); | 243 | SDL_Joystick* sdl_joystick = SDL_JoystickOpen(joystick_index); |
| 244 | SDL_GameController* sdl_gamecontroller = nullptr; | ||
| 245 | if (SDL_IsGameController(joystick_index)) { | ||
| 246 | sdl_gamecontroller = SDL_GameControllerOpen(joystick_index); | ||
| 247 | } | ||
| 191 | if (!sdl_joystick) { | 248 | if (!sdl_joystick) { |
| 192 | LOG_ERROR(Input, "failed to open joystick {}", joystick_index); | 249 | LOG_ERROR(Input, "Failed to open joystick {}", joystick_index); |
| 193 | return; | 250 | return; |
| 194 | } | 251 | } |
| 195 | const std::string guid = GetGUID(sdl_joystick); | 252 | const std::string guid = GetGUID(sdl_joystick); |
| 196 | 253 | ||
| 197 | std::lock_guard lock{joystick_map_mutex}; | 254 | std::lock_guard lock{joystick_map_mutex}; |
| 198 | if (joystick_map.find(guid) == joystick_map.end()) { | 255 | if (joystick_map.find(guid) == joystick_map.end()) { |
| 199 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick); | 256 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_gamecontroller); |
| 200 | joystick_map[guid].emplace_back(std::move(joystick)); | 257 | joystick_map[guid].emplace_back(std::move(joystick)); |
| 201 | return; | 258 | return; |
| 202 | } | 259 | } |
| @@ -205,33 +262,32 @@ void SDLState::InitJoystick(int joystick_index) { | |||
| 205 | joystick_guid_list.begin(), joystick_guid_list.end(), | 262 | joystick_guid_list.begin(), joystick_guid_list.end(), |
| 206 | [](const std::shared_ptr<SDLJoystick>& joystick) { return !joystick->GetSDLJoystick(); }); | 263 | [](const std::shared_ptr<SDLJoystick>& joystick) { return !joystick->GetSDLJoystick(); }); |
| 207 | if (it != joystick_guid_list.end()) { | 264 | if (it != joystick_guid_list.end()) { |
| 208 | (*it)->SetSDLJoystick(sdl_joystick); | 265 | (*it)->SetSDLJoystick(sdl_joystick, sdl_gamecontroller); |
| 209 | return; | 266 | return; |
| 210 | } | 267 | } |
| 211 | const int port = static_cast<int>(joystick_guid_list.size()); | 268 | const int port = static_cast<int>(joystick_guid_list.size()); |
| 212 | auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick); | 269 | auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick, sdl_gamecontroller); |
| 213 | joystick_guid_list.emplace_back(std::move(joystick)); | 270 | joystick_guid_list.emplace_back(std::move(joystick)); |
| 214 | } | 271 | } |
| 215 | 272 | ||
| 216 | void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { | 273 | void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { |
| 217 | const std::string guid = GetGUID(sdl_joystick); | 274 | const std::string guid = GetGUID(sdl_joystick); |
| 218 | 275 | ||
| 219 | std::shared_ptr<SDLJoystick> joystick; | 276 | std::shared_ptr<SDLJoystick> found_joystick; |
| 220 | { | 277 | { |
| 221 | std::lock_guard lock{joystick_map_mutex}; | 278 | std::lock_guard lock{joystick_map_mutex}; |
| 222 | // This call to guid is safe since the joystick is guaranteed to be in the map | 279 | // This call to guid is safe since the joystick is guaranteed to be in the map |
| 223 | const auto& joystick_guid_list = joystick_map[guid]; | 280 | const auto& joystick_guid_list = joystick_map[guid]; |
| 224 | const auto joystick_it = | 281 | const auto joystick_it = std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), |
| 225 | std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), | 282 | [&sdl_joystick](const auto& joystick) { |
| 226 | [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) { | 283 | return joystick->GetSDLJoystick() == sdl_joystick; |
| 227 | return joystick->GetSDLJoystick() == sdl_joystick; | 284 | }); |
| 228 | }); | 285 | found_joystick = *joystick_it; |
| 229 | joystick = *joystick_it; | ||
| 230 | } | 286 | } |
| 231 | 287 | ||
| 232 | // Destruct SDL_Joystick outside the lock guard because SDL can internally call the | 288 | // Destruct SDL_Joystick outside the lock guard because SDL can internally call the |
| 233 | // event callback which locks the mutex again. | 289 | // event callback which locks the mutex again. |
| 234 | joystick->SetSDLJoystick(nullptr); | 290 | found_joystick->SetSDLJoystick(nullptr, nullptr); |
| 235 | } | 291 | } |
| 236 | 292 | ||
| 237 | void SDLState::HandleGameControllerEvent(const SDL_Event& event) { | 293 | void SDLState::HandleGameControllerEvent(const SDL_Event& event) { |
| @@ -285,6 +341,12 @@ public: | |||
| 285 | return joystick->GetButton(button); | 341 | return joystick->GetButton(button); |
| 286 | } | 342 | } |
| 287 | 343 | ||
| 344 | bool SetRumblePlay(f32 amp_high, f32 amp_low, f32 freq_high, f32 freq_low) const override { | ||
| 345 | const f32 new_amp_low = pow(amp_low, 0.5f) * (3.0f - 2.0f * pow(amp_low, 0.15f)); | ||
| 346 | const f32 new_amp_high = pow(amp_high, 0.5f) * (3.0f - 2.0f * pow(amp_high, 0.15f)); | ||
| 347 | return joystick->RumblePlay(new_amp_low, new_amp_high, 250); | ||
| 348 | } | ||
| 349 | |||
| 288 | private: | 350 | private: |
| 289 | std::shared_ptr<SDLJoystick> joystick; | 351 | std::shared_ptr<SDLJoystick> joystick; |
| 290 | int button; | 352 | int button; |
| @@ -329,8 +391,8 @@ private: | |||
| 329 | 391 | ||
| 330 | class SDLAnalog final : public Input::AnalogDevice { | 392 | class SDLAnalog final : public Input::AnalogDevice { |
| 331 | public: | 393 | public: |
| 332 | SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, float deadzone_, | 394 | explicit SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, |
| 333 | float range_) | 395 | float deadzone_, float range_) |
| 334 | : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), | 396 | : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), |
| 335 | range(range_) {} | 397 | range(range_) {} |
| 336 | 398 | ||
| @@ -341,12 +403,12 @@ public: | |||
| 341 | return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), | 403 | return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), |
| 342 | y / r * (r - deadzone) / (1 - deadzone)); | 404 | y / r * (r - deadzone) / (1 - deadzone)); |
| 343 | } | 405 | } |
| 344 | return std::make_tuple<float, float>(0.0f, 0.0f); | 406 | return {}; |
| 345 | } | 407 | } |
| 346 | 408 | ||
| 347 | bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { | 409 | bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { |
| 348 | const auto [x, y] = GetStatus(); | 410 | const auto [x, y] = GetStatus(); |
| 349 | const float directional_deadzone = 0.4f; | 411 | const float directional_deadzone = 0.5f; |
| 350 | switch (direction) { | 412 | switch (direction) { |
| 351 | case Input::AnalogDirection::RIGHT: | 413 | case Input::AnalogDirection::RIGHT: |
| 352 | return x > directional_deadzone; | 414 | return x > directional_deadzone; |
| @@ -368,6 +430,68 @@ private: | |||
| 368 | const float range; | 430 | const float range; |
| 369 | }; | 431 | }; |
| 370 | 432 | ||
| 433 | class SDLDirectionMotion final : public Input::MotionDevice { | ||
| 434 | public: | ||
| 435 | explicit SDLDirectionMotion(std::shared_ptr<SDLJoystick> joystick_, int hat_, Uint8 direction_) | ||
| 436 | : joystick(std::move(joystick_)), hat(hat_), direction(direction_) {} | ||
| 437 | |||
| 438 | Input::MotionStatus GetStatus() const override { | ||
| 439 | if (joystick->GetHatDirection(hat, direction)) { | ||
| 440 | return joystick->GetMotion().GetRandomMotion(2, 6); | ||
| 441 | } | ||
| 442 | return joystick->GetMotion().GetRandomMotion(0, 0); | ||
| 443 | } | ||
| 444 | |||
| 445 | private: | ||
| 446 | std::shared_ptr<SDLJoystick> joystick; | ||
| 447 | int hat; | ||
| 448 | Uint8 direction; | ||
| 449 | }; | ||
| 450 | |||
| 451 | class SDLAxisMotion final : public Input::MotionDevice { | ||
| 452 | public: | ||
| 453 | explicit SDLAxisMotion(std::shared_ptr<SDLJoystick> joystick_, int axis_, float threshold_, | ||
| 454 | bool trigger_if_greater_) | ||
| 455 | : joystick(std::move(joystick_)), axis(axis_), threshold(threshold_), | ||
| 456 | trigger_if_greater(trigger_if_greater_) {} | ||
| 457 | |||
| 458 | Input::MotionStatus GetStatus() const override { | ||
| 459 | const float axis_value = joystick->GetAxis(axis, 1.0f); | ||
| 460 | bool trigger = axis_value < threshold; | ||
| 461 | if (trigger_if_greater) { | ||
| 462 | trigger = axis_value > threshold; | ||
| 463 | } | ||
| 464 | |||
| 465 | if (trigger) { | ||
| 466 | return joystick->GetMotion().GetRandomMotion(2, 6); | ||
| 467 | } | ||
| 468 | return joystick->GetMotion().GetRandomMotion(0, 0); | ||
| 469 | } | ||
| 470 | |||
| 471 | private: | ||
| 472 | std::shared_ptr<SDLJoystick> joystick; | ||
| 473 | int axis; | ||
| 474 | float threshold; | ||
| 475 | bool trigger_if_greater; | ||
| 476 | }; | ||
| 477 | |||
| 478 | class SDLButtonMotion final : public Input::MotionDevice { | ||
| 479 | public: | ||
| 480 | explicit SDLButtonMotion(std::shared_ptr<SDLJoystick> joystick_, int button_) | ||
| 481 | : joystick(std::move(joystick_)), button(button_) {} | ||
| 482 | |||
| 483 | Input::MotionStatus GetStatus() const override { | ||
| 484 | if (joystick->GetButton(button)) { | ||
| 485 | return joystick->GetMotion().GetRandomMotion(2, 6); | ||
| 486 | } | ||
| 487 | return joystick->GetMotion().GetRandomMotion(0, 0); | ||
| 488 | } | ||
| 489 | |||
| 490 | private: | ||
| 491 | std::shared_ptr<SDLJoystick> joystick; | ||
| 492 | int button; | ||
| 493 | }; | ||
| 494 | |||
| 371 | /// A button device factory that creates button devices from SDL joystick | 495 | /// A button device factory that creates button devices from SDL joystick |
| 372 | class SDLButtonFactory final : public Input::Factory<Input::ButtonDevice> { | 496 | class SDLButtonFactory final : public Input::Factory<Input::ButtonDevice> { |
| 373 | public: | 497 | public: |
| @@ -460,7 +584,7 @@ public: | |||
| 460 | const int port = params.Get("port", 0); | 584 | const int port = params.Get("port", 0); |
| 461 | const int axis_x = params.Get("axis_x", 0); | 585 | const int axis_x = params.Get("axis_x", 0); |
| 462 | const int axis_y = params.Get("axis_y", 1); | 586 | const int axis_y = params.Get("axis_y", 1); |
| 463 | const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); | 587 | 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); | 588 | const float range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); |
| 465 | auto joystick = state.GetSDLJoystickByGUID(guid, port); | 589 | auto joystick = state.GetSDLJoystickByGUID(guid, port); |
| 466 | 590 | ||
| @@ -474,17 +598,86 @@ private: | |||
| 474 | SDLState& state; | 598 | SDLState& state; |
| 475 | }; | 599 | }; |
| 476 | 600 | ||
| 601 | /// A motion device factory that creates motion devices from SDL joystick | ||
| 602 | class SDLMotionFactory final : public Input::Factory<Input::MotionDevice> { | ||
| 603 | public: | ||
| 604 | explicit SDLMotionFactory(SDLState& state_) : state(state_) {} | ||
| 605 | /** | ||
| 606 | * Creates motion device from joystick axes | ||
| 607 | * @param params contains parameters for creating the device: | ||
| 608 | * - "guid": the guid of the joystick to bind | ||
| 609 | * - "port": the nth joystick of the same type | ||
| 610 | */ | ||
| 611 | std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override { | ||
| 612 | const std::string guid = params.Get("guid", "0"); | ||
| 613 | const int port = params.Get("port", 0); | ||
| 614 | |||
| 615 | auto joystick = state.GetSDLJoystickByGUID(guid, port); | ||
| 616 | |||
| 617 | if (params.Has("hat")) { | ||
| 618 | const int hat = params.Get("hat", 0); | ||
| 619 | const std::string direction_name = params.Get("direction", ""); | ||
| 620 | Uint8 direction; | ||
| 621 | if (direction_name == "up") { | ||
| 622 | direction = SDL_HAT_UP; | ||
| 623 | } else if (direction_name == "down") { | ||
| 624 | direction = SDL_HAT_DOWN; | ||
| 625 | } else if (direction_name == "left") { | ||
| 626 | direction = SDL_HAT_LEFT; | ||
| 627 | } else if (direction_name == "right") { | ||
| 628 | direction = SDL_HAT_RIGHT; | ||
| 629 | } else { | ||
| 630 | direction = 0; | ||
| 631 | } | ||
| 632 | // This is necessary so accessing GetHat with hat won't crash | ||
| 633 | joystick->SetHat(hat, SDL_HAT_CENTERED); | ||
| 634 | return std::make_unique<SDLDirectionMotion>(joystick, hat, direction); | ||
| 635 | } | ||
| 636 | |||
| 637 | if (params.Has("axis")) { | ||
| 638 | const int axis = params.Get("axis", 0); | ||
| 639 | const float threshold = params.Get("threshold", 0.5f); | ||
| 640 | const std::string direction_name = params.Get("direction", ""); | ||
| 641 | bool trigger_if_greater; | ||
| 642 | if (direction_name == "+") { | ||
| 643 | trigger_if_greater = true; | ||
| 644 | } else if (direction_name == "-") { | ||
| 645 | trigger_if_greater = false; | ||
| 646 | } else { | ||
| 647 | trigger_if_greater = true; | ||
| 648 | LOG_ERROR(Input, "Unknown direction {}", direction_name); | ||
| 649 | } | ||
| 650 | // This is necessary so accessing GetAxis with axis won't crash | ||
| 651 | joystick->SetAxis(axis, 0); | ||
| 652 | return std::make_unique<SDLAxisMotion>(joystick, axis, threshold, trigger_if_greater); | ||
| 653 | } | ||
| 654 | |||
| 655 | const int button = params.Get("button", 0); | ||
| 656 | // This is necessary so accessing GetButton with button won't crash | ||
| 657 | joystick->SetButton(button, false); | ||
| 658 | return std::make_unique<SDLButtonMotion>(joystick, button); | ||
| 659 | } | ||
| 660 | |||
| 661 | private: | ||
| 662 | SDLState& state; | ||
| 663 | }; | ||
| 664 | |||
| 477 | SDLState::SDLState() { | 665 | SDLState::SDLState() { |
| 478 | using namespace Input; | 666 | using namespace Input; |
| 479 | RegisterFactory<ButtonDevice>("sdl", std::make_shared<SDLButtonFactory>(*this)); | 667 | analog_factory = std::make_shared<SDLAnalogFactory>(*this); |
| 480 | RegisterFactory<AnalogDevice>("sdl", std::make_shared<SDLAnalogFactory>(*this)); | 668 | button_factory = std::make_shared<SDLButtonFactory>(*this); |
| 481 | 669 | motion_factory = std::make_shared<SDLMotionFactory>(*this); | |
| 482 | // If the frontend is going to manage the event loop, then we dont start one here | 670 | RegisterFactory<AnalogDevice>("sdl", analog_factory); |
| 483 | start_thread = !SDL_WasInit(SDL_INIT_JOYSTICK); | 671 | RegisterFactory<ButtonDevice>("sdl", button_factory); |
| 672 | RegisterFactory<MotionDevice>("sdl", motion_factory); | ||
| 673 | |||
| 674 | // If the frontend is going to manage the event loop, then we don't start one here | ||
| 675 | start_thread = SDL_WasInit(SDL_INIT_JOYSTICK) == 0; | ||
| 484 | if (start_thread && SDL_Init(SDL_INIT_JOYSTICK) < 0) { | 676 | if (start_thread && SDL_Init(SDL_INIT_JOYSTICK) < 0) { |
| 485 | LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError()); | 677 | LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError()); |
| 486 | return; | 678 | return; |
| 487 | } | 679 | } |
| 680 | has_gamecontroller = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0; | ||
| 488 | if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) { | 681 | 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()); | 682 | LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError()); |
| 490 | } | 683 | } |
| @@ -497,7 +690,7 @@ SDLState::SDLState() { | |||
| 497 | using namespace std::chrono_literals; | 690 | using namespace std::chrono_literals; |
| 498 | while (initialized) { | 691 | while (initialized) { |
| 499 | SDL_PumpEvents(); | 692 | SDL_PumpEvents(); |
| 500 | std::this_thread::sleep_for(10ms); | 693 | std::this_thread::sleep_for(5ms); |
| 501 | } | 694 | } |
| 502 | }); | 695 | }); |
| 503 | } | 696 | } |
| @@ -512,6 +705,7 @@ SDLState::~SDLState() { | |||
| 512 | using namespace Input; | 705 | using namespace Input; |
| 513 | UnregisterFactory<ButtonDevice>("sdl"); | 706 | UnregisterFactory<ButtonDevice>("sdl"); |
| 514 | UnregisterFactory<AnalogDevice>("sdl"); | 707 | UnregisterFactory<AnalogDevice>("sdl"); |
| 708 | UnregisterFactory<MotionDevice>("sdl"); | ||
| 515 | 709 | ||
| 516 | CloseJoysticks(); | 710 | CloseJoysticks(); |
| 517 | SDL_DelEventWatch(&SDLEventWatcher, this); | 711 | SDL_DelEventWatch(&SDLEventWatcher, this); |
| @@ -523,65 +717,255 @@ SDLState::~SDLState() { | |||
| 523 | } | 717 | } |
| 524 | } | 718 | } |
| 525 | 719 | ||
| 526 | static Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Event& event) { | 720 | std::vector<Common::ParamPackage> SDLState::GetInputDevices() { |
| 721 | std::scoped_lock lock(joystick_map_mutex); | ||
| 722 | std::vector<Common::ParamPackage> devices; | ||
| 723 | for (const auto& [key, value] : joystick_map) { | ||
| 724 | for (const auto& joystick : value) { | ||
| 725 | auto* joy = joystick->GetSDLJoystick(); | ||
| 726 | if (auto* controller = joystick->GetSDLGameController()) { | ||
| 727 | std::string name = | ||
| 728 | fmt::format("{} {}", SDL_GameControllerName(controller), joystick->GetPort()); | ||
| 729 | devices.emplace_back(Common::ParamPackage{ | ||
| 730 | {"class", "sdl"}, | ||
| 731 | {"display", std::move(name)}, | ||
| 732 | {"guid", joystick->GetGUID()}, | ||
| 733 | {"port", std::to_string(joystick->GetPort())}, | ||
| 734 | }); | ||
| 735 | } else if (joy) { | ||
| 736 | std::string name = fmt::format("{} {}", SDL_JoystickName(joy), 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 | } | ||
| 744 | } | ||
| 745 | } | ||
| 746 | return devices; | ||
| 747 | } | ||
| 748 | |||
| 749 | namespace { | ||
| 750 | Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, s32 axis, | ||
| 751 | float value = 0.1f) { | ||
| 752 | Common::ParamPackage params({{"engine", "sdl"}}); | ||
| 753 | params.Set("port", port); | ||
| 754 | params.Set("guid", std::move(guid)); | ||
| 755 | params.Set("axis", axis); | ||
| 756 | if (value > 0) { | ||
| 757 | params.Set("direction", "+"); | ||
| 758 | params.Set("threshold", "0.5"); | ||
| 759 | } else { | ||
| 760 | params.Set("direction", "-"); | ||
| 761 | params.Set("threshold", "-0.5"); | ||
| 762 | } | ||
| 763 | return params; | ||
| 764 | } | ||
| 765 | |||
| 766 | Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, s32 button) { | ||
| 527 | Common::ParamPackage params({{"engine", "sdl"}}); | 767 | Common::ParamPackage params({{"engine", "sdl"}}); |
| 768 | params.Set("port", port); | ||
| 769 | params.Set("guid", std::move(guid)); | ||
| 770 | params.Set("button", button); | ||
| 771 | return params; | ||
| 772 | } | ||
| 773 | |||
| 774 | Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, s32 hat, s32 value) { | ||
| 775 | Common::ParamPackage params({{"engine", "sdl"}}); | ||
| 776 | |||
| 777 | params.Set("port", port); | ||
| 778 | params.Set("guid", std::move(guid)); | ||
| 779 | params.Set("hat", hat); | ||
| 780 | switch (value) { | ||
| 781 | case SDL_HAT_UP: | ||
| 782 | params.Set("direction", "up"); | ||
| 783 | break; | ||
| 784 | case SDL_HAT_DOWN: | ||
| 785 | params.Set("direction", "down"); | ||
| 786 | break; | ||
| 787 | case SDL_HAT_LEFT: | ||
| 788 | params.Set("direction", "left"); | ||
| 789 | break; | ||
| 790 | case SDL_HAT_RIGHT: | ||
| 791 | params.Set("direction", "right"); | ||
| 792 | break; | ||
| 793 | default: | ||
| 794 | return {}; | ||
| 795 | } | ||
| 796 | return params; | ||
| 797 | } | ||
| 528 | 798 | ||
| 799 | Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Event& event) { | ||
| 529 | switch (event.type) { | 800 | switch (event.type) { |
| 530 | case SDL_JOYAXISMOTION: { | 801 | case SDL_JOYAXISMOTION: { |
| 531 | const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); | 802 | const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); |
| 532 | params.Set("port", joystick->GetPort()); | 803 | return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), |
| 533 | params.Set("guid", joystick->GetGUID()); | 804 | static_cast<s32>(event.jaxis.axis), |
| 534 | params.Set("axis", event.jaxis.axis); | 805 | 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 | } | ||
| 542 | break; | ||
| 543 | } | 806 | } |
| 544 | case SDL_JOYBUTTONUP: { | 807 | case SDL_JOYBUTTONUP: { |
| 545 | const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which); | 808 | const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which); |
| 546 | params.Set("port", joystick->GetPort()); | 809 | return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), |
| 547 | params.Set("guid", joystick->GetGUID()); | 810 | static_cast<s32>(event.jbutton.button)); |
| 548 | params.Set("button", event.jbutton.button); | ||
| 549 | break; | ||
| 550 | } | 811 | } |
| 551 | case SDL_JOYHATMOTION: { | 812 | case SDL_JOYHATMOTION: { |
| 552 | const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which); | 813 | const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which); |
| 553 | params.Set("port", joystick->GetPort()); | 814 | return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), |
| 554 | params.Set("guid", joystick->GetGUID()); | 815 | static_cast<s32>(event.jhat.hat), |
| 555 | params.Set("hat", event.jhat.hat); | 816 | 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 | } | ||
| 572 | break; | ||
| 573 | } | 817 | } |
| 574 | } | 818 | } |
| 819 | return {}; | ||
| 820 | } | ||
| 821 | |||
| 822 | Common::ParamPackage SDLEventToMotionParamPackage(SDLState& state, const SDL_Event& event) { | ||
| 823 | switch (event.type) { | ||
| 824 | case SDL_JOYAXISMOTION: { | ||
| 825 | const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); | ||
| 826 | return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), | ||
| 827 | static_cast<s32>(event.jaxis.axis), | ||
| 828 | event.jaxis.value); | ||
| 829 | } | ||
| 830 | case SDL_JOYBUTTONUP: { | ||
| 831 | const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which); | ||
| 832 | return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), | ||
| 833 | static_cast<s32>(event.jbutton.button)); | ||
| 834 | } | ||
| 835 | case SDL_JOYHATMOTION: { | ||
| 836 | const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which); | ||
| 837 | return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), | ||
| 838 | static_cast<s32>(event.jhat.hat), | ||
| 839 | static_cast<s32>(event.jhat.value)); | ||
| 840 | } | ||
| 841 | } | ||
| 842 | return {}; | ||
| 843 | } | ||
| 844 | |||
| 845 | Common::ParamPackage BuildParamPackageForBinding(int port, const std::string& guid, | ||
| 846 | const SDL_GameControllerButtonBind& binding) { | ||
| 847 | switch (binding.bindType) { | ||
| 848 | case SDL_CONTROLLER_BINDTYPE_AXIS: | ||
| 849 | return BuildAnalogParamPackageForButton(port, guid, binding.value.axis); | ||
| 850 | case SDL_CONTROLLER_BINDTYPE_BUTTON: | ||
| 851 | return BuildButtonParamPackageForButton(port, guid, binding.value.button); | ||
| 852 | case SDL_CONTROLLER_BINDTYPE_HAT: | ||
| 853 | return BuildHatParamPackageForButton(port, guid, binding.value.hat.hat, | ||
| 854 | binding.value.hat.hat_mask); | ||
| 855 | } | ||
| 856 | return {}; | ||
| 857 | } | ||
| 858 | |||
| 859 | Common::ParamPackage BuildParamPackageForAnalog(int port, const std::string& guid, int axis_x, | ||
| 860 | int axis_y) { | ||
| 861 | Common::ParamPackage params; | ||
| 862 | params.Set("engine", "sdl"); | ||
| 863 | params.Set("port", port); | ||
| 864 | params.Set("guid", guid); | ||
| 865 | params.Set("axis_x", axis_x); | ||
| 866 | params.Set("axis_y", axis_y); | ||
| 575 | return params; | 867 | return params; |
| 576 | } | 868 | } |
| 869 | } // Anonymous namespace | ||
| 577 | 870 | ||
| 578 | namespace Polling { | 871 | ButtonMapping SDLState::GetButtonMappingForDevice(const Common::ParamPackage& params) { |
| 872 | if (!params.Has("guid") || !params.Has("port")) { | ||
| 873 | return {}; | ||
| 874 | } | ||
| 875 | const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0)); | ||
| 876 | auto* controller = joystick->GetSDLGameController(); | ||
| 877 | if (controller == nullptr) { | ||
| 878 | return {}; | ||
| 879 | } | ||
| 880 | |||
| 881 | // This list is missing ZL/ZR since those are not considered buttons in SDL GameController. | ||
| 882 | // We will add those afterwards | ||
| 883 | // This list also excludes Screenshot since theres not really a mapping for that | ||
| 884 | using ButtonBindings = | ||
| 885 | std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerButton>, 17>; | ||
| 886 | static constexpr ButtonBindings switch_to_sdl_button{{ | ||
| 887 | {Settings::NativeButton::A, SDL_CONTROLLER_BUTTON_B}, | ||
| 888 | {Settings::NativeButton::B, SDL_CONTROLLER_BUTTON_A}, | ||
| 889 | {Settings::NativeButton::X, SDL_CONTROLLER_BUTTON_Y}, | ||
| 890 | {Settings::NativeButton::Y, SDL_CONTROLLER_BUTTON_X}, | ||
| 891 | {Settings::NativeButton::LStick, SDL_CONTROLLER_BUTTON_LEFTSTICK}, | ||
| 892 | {Settings::NativeButton::RStick, SDL_CONTROLLER_BUTTON_RIGHTSTICK}, | ||
| 893 | {Settings::NativeButton::L, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, | ||
| 894 | {Settings::NativeButton::R, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, | ||
| 895 | {Settings::NativeButton::Plus, SDL_CONTROLLER_BUTTON_START}, | ||
| 896 | {Settings::NativeButton::Minus, SDL_CONTROLLER_BUTTON_BACK}, | ||
| 897 | {Settings::NativeButton::DLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT}, | ||
| 898 | {Settings::NativeButton::DUp, SDL_CONTROLLER_BUTTON_DPAD_UP}, | ||
| 899 | {Settings::NativeButton::DRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT}, | ||
| 900 | {Settings::NativeButton::DDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN}, | ||
| 901 | {Settings::NativeButton::SL, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, | ||
| 902 | {Settings::NativeButton::SR, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, | ||
| 903 | {Settings::NativeButton::Home, SDL_CONTROLLER_BUTTON_GUIDE}, | ||
| 904 | }}; | ||
| 905 | |||
| 906 | // Add the missing bindings for ZL/ZR | ||
| 907 | using ZBindings = | ||
| 908 | std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerAxis>, 2>; | ||
| 909 | static constexpr ZBindings switch_to_sdl_axis{{ | ||
| 910 | {Settings::NativeButton::ZL, SDL_CONTROLLER_AXIS_TRIGGERLEFT}, | ||
| 911 | {Settings::NativeButton::ZR, SDL_CONTROLLER_AXIS_TRIGGERRIGHT}, | ||
| 912 | }}; | ||
| 913 | |||
| 914 | ButtonMapping mapping; | ||
| 915 | mapping.reserve(switch_to_sdl_button.size() + switch_to_sdl_axis.size()); | ||
| 916 | |||
| 917 | for (const auto& [switch_button, sdl_button] : switch_to_sdl_button) { | ||
| 918 | const auto& binding = SDL_GameControllerGetBindForButton(controller, sdl_button); | ||
| 919 | mapping.insert_or_assign( | ||
| 920 | switch_button, | ||
| 921 | BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); | ||
| 922 | } | ||
| 923 | for (const auto& [switch_button, sdl_axis] : switch_to_sdl_axis) { | ||
| 924 | const auto& binding = SDL_GameControllerGetBindForAxis(controller, sdl_axis); | ||
| 925 | mapping.insert_or_assign( | ||
| 926 | switch_button, | ||
| 927 | BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); | ||
| 928 | } | ||
| 929 | |||
| 930 | return mapping; | ||
| 931 | } | ||
| 579 | 932 | ||
| 933 | AnalogMapping SDLState::GetAnalogMappingForDevice(const Common::ParamPackage& params) { | ||
| 934 | if (!params.Has("guid") || !params.Has("port")) { | ||
| 935 | return {}; | ||
| 936 | } | ||
| 937 | const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0)); | ||
| 938 | auto* controller = joystick->GetSDLGameController(); | ||
| 939 | if (controller == nullptr) { | ||
| 940 | return {}; | ||
| 941 | } | ||
| 942 | |||
| 943 | AnalogMapping mapping = {}; | ||
| 944 | const auto& binding_left_x = | ||
| 945 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX); | ||
| 946 | const auto& binding_left_y = | ||
| 947 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTY); | ||
| 948 | mapping.insert_or_assign(Settings::NativeAnalog::LStick, | ||
| 949 | BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), | ||
| 950 | binding_left_x.value.axis, | ||
| 951 | binding_left_y.value.axis)); | ||
| 952 | const auto& binding_right_x = | ||
| 953 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX); | ||
| 954 | const auto& binding_right_y = | ||
| 955 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY); | ||
| 956 | mapping.insert_or_assign(Settings::NativeAnalog::RStick, | ||
| 957 | BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), | ||
| 958 | binding_right_x.value.axis, | ||
| 959 | binding_right_y.value.axis)); | ||
| 960 | return mapping; | ||
| 961 | } | ||
| 962 | |||
| 963 | namespace Polling { | ||
| 580 | class SDLPoller : public InputCommon::Polling::DevicePoller { | 964 | class SDLPoller : public InputCommon::Polling::DevicePoller { |
| 581 | public: | 965 | public: |
| 582 | explicit SDLPoller(SDLState& state_) : state(state_) {} | 966 | explicit SDLPoller(SDLState& state_) : state(state_) {} |
| 583 | 967 | ||
| 584 | void Start() override { | 968 | void Start(const std::string& device_id) override { |
| 585 | state.event_queue.Clear(); | 969 | state.event_queue.Clear(); |
| 586 | state.polling = true; | 970 | state.polling = true; |
| 587 | } | 971 | } |
| @@ -601,71 +985,135 @@ public: | |||
| 601 | Common::ParamPackage GetNextInput() override { | 985 | Common::ParamPackage GetNextInput() override { |
| 602 | SDL_Event event; | 986 | SDL_Event event; |
| 603 | while (state.event_queue.Pop(event)) { | 987 | while (state.event_queue.Pop(event)) { |
| 604 | switch (event.type) { | 988 | const auto package = FromEvent(event); |
| 605 | case SDL_JOYAXISMOTION: | 989 | if (package) { |
| 606 | if (std::abs(event.jaxis.value / 32767.0) < 0.5) { | 990 | return *package; |
| 607 | break; | ||
| 608 | } | ||
| 609 | [[fallthrough]]; | ||
| 610 | case SDL_JOYBUTTONUP: | ||
| 611 | case SDL_JOYHATMOTION: | ||
| 612 | return SDLEventToButtonParamPackage(state, event); | ||
| 613 | } | 991 | } |
| 614 | } | 992 | } |
| 615 | return {}; | 993 | return {}; |
| 616 | } | 994 | } |
| 995 | [[nodiscard]] std::optional<Common::ParamPackage> FromEvent(const SDL_Event& event) const { | ||
| 996 | switch (event.type) { | ||
| 997 | case SDL_JOYAXISMOTION: | ||
| 998 | if (std::abs(event.jaxis.value / 32767.0) < 0.5) { | ||
| 999 | break; | ||
| 1000 | } | ||
| 1001 | [[fallthrough]]; | ||
| 1002 | case SDL_JOYBUTTONUP: | ||
| 1003 | case SDL_JOYHATMOTION: | ||
| 1004 | return {SDLEventToButtonParamPackage(state, event)}; | ||
| 1005 | } | ||
| 1006 | return std::nullopt; | ||
| 1007 | } | ||
| 617 | }; | 1008 | }; |
| 618 | 1009 | ||
| 619 | class SDLAnalogPoller final : public SDLPoller { | 1010 | class SDLMotionPoller final : public SDLPoller { |
| 620 | public: | 1011 | public: |
| 621 | explicit SDLAnalogPoller(SDLState& state_) : SDLPoller(state_) {} | 1012 | explicit SDLMotionPoller(SDLState& state_) : SDLPoller(state_) {} |
| 622 | 1013 | ||
| 623 | void Start() override { | 1014 | Common::ParamPackage GetNextInput() override { |
| 624 | SDLPoller::Start(); | 1015 | SDL_Event event; |
| 1016 | while (state.event_queue.Pop(event)) { | ||
| 1017 | const auto package = FromEvent(event); | ||
| 1018 | if (package) { | ||
| 1019 | return *package; | ||
| 1020 | } | ||
| 1021 | } | ||
| 1022 | return {}; | ||
| 1023 | } | ||
| 1024 | [[nodiscard]] std::optional<Common::ParamPackage> FromEvent(const SDL_Event& event) const { | ||
| 1025 | switch (event.type) { | ||
| 1026 | case SDL_JOYAXISMOTION: | ||
| 1027 | if (std::abs(event.jaxis.value / 32767.0) < 0.5) { | ||
| 1028 | break; | ||
| 1029 | } | ||
| 1030 | [[fallthrough]]; | ||
| 1031 | case SDL_JOYBUTTONUP: | ||
| 1032 | case SDL_JOYHATMOTION: | ||
| 1033 | return {SDLEventToMotionParamPackage(state, event)}; | ||
| 1034 | } | ||
| 1035 | return std::nullopt; | ||
| 1036 | } | ||
| 1037 | }; | ||
| 1038 | |||
| 1039 | /** | ||
| 1040 | * Attempts to match the press to a controller joy axis (left/right stick) and if a match | ||
| 1041 | * isn't found, checks if the event matches anything from SDLButtonPoller and uses that | ||
| 1042 | * instead | ||
| 1043 | */ | ||
| 1044 | class SDLAnalogPreferredPoller final : public SDLPoller { | ||
| 1045 | public: | ||
| 1046 | explicit SDLAnalogPreferredPoller(SDLState& state_) | ||
| 1047 | : SDLPoller(state_), button_poller(state_) {} | ||
| 625 | 1048 | ||
| 1049 | void Start(const std::string& device_id) override { | ||
| 1050 | SDLPoller::Start(device_id); | ||
| 1051 | // Load the game controller | ||
| 626 | // Reset stored axes | 1052 | // Reset stored axes |
| 627 | analog_x_axis = -1; | 1053 | analog_x_axis = -1; |
| 628 | analog_y_axis = -1; | 1054 | analog_y_axis = -1; |
| 629 | analog_axes_joystick = -1; | ||
| 630 | } | 1055 | } |
| 631 | 1056 | ||
| 632 | Common::ParamPackage GetNextInput() override { | 1057 | Common::ParamPackage GetNextInput() override { |
| 633 | SDL_Event event; | 1058 | SDL_Event event; |
| 634 | while (state.event_queue.Pop(event)) { | 1059 | while (state.event_queue.Pop(event)) { |
| 635 | if (event.type != SDL_JOYAXISMOTION || std::abs(event.jaxis.value / 32767.0) < 0.5) { | 1060 | // Filter out axis events that are below a threshold |
| 1061 | if (event.type == SDL_JOYAXISMOTION && std::abs(event.jaxis.value / 32767.0) < 0.5) { | ||
| 636 | continue; | 1062 | continue; |
| 637 | } | 1063 | } |
| 638 | // An analog device needs two axes, so we need to store the axis for later and wait for | 1064 | // Simplify controller config by testing if game controller support is enabled. |
| 639 | // a second SDL event. The axes also must be from the same joystick. | 1065 | if (event.type == SDL_JOYAXISMOTION) { |
| 640 | const int axis = event.jaxis.axis; | 1066 | const auto axis = event.jaxis.axis; |
| 641 | if (analog_x_axis == -1) { | 1067 | const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); |
| 642 | analog_x_axis = axis; | 1068 | auto* const controller = joystick->GetSDLGameController(); |
| 643 | analog_axes_joystick = event.jaxis.which; | 1069 | if (controller) { |
| 644 | } else if (analog_y_axis == -1 && analog_x_axis != axis && | 1070 | const auto axis_left_x = |
| 645 | analog_axes_joystick == event.jaxis.which) { | 1071 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX) |
| 646 | analog_y_axis = axis; | 1072 | .value.axis; |
| 1073 | const auto axis_left_y = | ||
| 1074 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTY) | ||
| 1075 | .value.axis; | ||
| 1076 | const auto axis_right_x = | ||
| 1077 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX) | ||
| 1078 | .value.axis; | ||
| 1079 | const auto axis_right_y = | ||
| 1080 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY) | ||
| 1081 | .value.axis; | ||
| 1082 | |||
| 1083 | if (axis == axis_left_x || axis == axis_left_y) { | ||
| 1084 | analog_x_axis = axis_left_x; | ||
| 1085 | analog_y_axis = axis_left_y; | ||
| 1086 | break; | ||
| 1087 | } else if (axis == axis_right_x || axis == axis_right_y) { | ||
| 1088 | analog_x_axis = axis_right_x; | ||
| 1089 | analog_y_axis = axis_right_y; | ||
| 1090 | break; | ||
| 1091 | } | ||
| 1092 | } | ||
| 1093 | } | ||
| 1094 | |||
| 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; | ||
| 647 | } | 1099 | } |
| 648 | } | 1100 | } |
| 649 | Common::ParamPackage params; | 1101 | |
| 650 | if (analog_x_axis != -1 && analog_y_axis != -1) { | 1102 | if (analog_x_axis != -1 && analog_y_axis != -1) { |
| 651 | const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); | 1103 | const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); |
| 652 | params.Set("engine", "sdl"); | 1104 | auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), |
| 653 | params.Set("port", joystick->GetPort()); | 1105 | analog_x_axis, analog_y_axis); |
| 654 | params.Set("guid", joystick->GetGUID()); | ||
| 655 | params.Set("axis_x", analog_x_axis); | ||
| 656 | params.Set("axis_y", analog_y_axis); | ||
| 657 | analog_x_axis = -1; | 1106 | analog_x_axis = -1; |
| 658 | analog_y_axis = -1; | 1107 | analog_y_axis = -1; |
| 659 | analog_axes_joystick = -1; | ||
| 660 | return params; | 1108 | return params; |
| 661 | } | 1109 | } |
| 662 | return params; | 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; |