diff options
Diffstat (limited to 'src/input_common/drivers/sdl_driver.cpp')
| -rw-r--r-- | src/input_common/drivers/sdl_driver.cpp | 924 |
1 files changed, 924 insertions, 0 deletions
diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cpp new file mode 100644 index 000000000..90128b6cf --- /dev/null +++ b/src/input_common/drivers/sdl_driver.cpp | |||
| @@ -0,0 +1,924 @@ | |||
| 1 | // Copyright 2018 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/logging/log.h" | ||
| 6 | #include "common/math_util.h" | ||
| 7 | #include "common/param_package.h" | ||
| 8 | #include "common/settings.h" | ||
| 9 | #include "common/thread.h" | ||
| 10 | #include "common/vector_math.h" | ||
| 11 | #include "input_common/drivers/sdl_driver.h" | ||
| 12 | |||
| 13 | namespace InputCommon { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | std::string GetGUID(SDL_Joystick* joystick) { | ||
| 17 | const SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); | ||
| 18 | char guid_str[33]; | ||
| 19 | SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str)); | ||
| 20 | return guid_str; | ||
| 21 | } | ||
| 22 | } // Anonymous namespace | ||
| 23 | |||
| 24 | static int SDLEventWatcher(void* user_data, SDL_Event* event) { | ||
| 25 | auto* const sdl_state = static_cast<SDLDriver*>(user_data); | ||
| 26 | |||
| 27 | sdl_state->HandleGameControllerEvent(*event); | ||
| 28 | |||
| 29 | return 0; | ||
| 30 | } | ||
| 31 | |||
| 32 | class SDLJoystick { | ||
| 33 | public: | ||
| 34 | SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick, | ||
| 35 | SDL_GameController* game_controller) | ||
| 36 | : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose}, | ||
| 37 | sdl_controller{game_controller, &SDL_GameControllerClose} { | ||
| 38 | EnableMotion(); | ||
| 39 | } | ||
| 40 | |||
| 41 | void EnableMotion() { | ||
| 42 | if (sdl_controller) { | ||
| 43 | SDL_GameController* controller = sdl_controller.get(); | ||
| 44 | if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL) && !has_accel) { | ||
| 45 | SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_TRUE); | ||
| 46 | has_accel = true; | ||
| 47 | } | ||
| 48 | if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO) && !has_gyro) { | ||
| 49 | SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, SDL_TRUE); | ||
| 50 | has_gyro = true; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | bool HasGyro() const { | ||
| 56 | return has_gyro; | ||
| 57 | } | ||
| 58 | |||
| 59 | bool HasAccel() const { | ||
| 60 | return has_accel; | ||
| 61 | } | ||
| 62 | |||
| 63 | bool UpdateMotion(SDL_ControllerSensorEvent event) { | ||
| 64 | constexpr float gravity_constant = 9.80665f; | ||
| 65 | std::lock_guard lock{mutex}; | ||
| 66 | const u64 time_difference = event.timestamp - last_motion_update; | ||
| 67 | last_motion_update = event.timestamp; | ||
| 68 | switch (event.sensor) { | ||
| 69 | case SDL_SENSOR_ACCEL: { | ||
| 70 | motion.accel_x = -event.data[0] / gravity_constant; | ||
| 71 | motion.accel_y = event.data[2] / gravity_constant; | ||
| 72 | motion.accel_z = -event.data[1] / gravity_constant; | ||
| 73 | break; | ||
| 74 | } | ||
| 75 | case SDL_SENSOR_GYRO: { | ||
| 76 | motion.gyro_x = event.data[0] / (Common::PI * 2); | ||
| 77 | motion.gyro_y = -event.data[2] / (Common::PI * 2); | ||
| 78 | motion.gyro_z = event.data[1] / (Common::PI * 2); | ||
| 79 | break; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | // Ignore duplicated timestamps | ||
| 84 | if (time_difference == 0) { | ||
| 85 | return false; | ||
| 86 | } | ||
| 87 | motion.delta_timestamp = time_difference * 1000; | ||
| 88 | return true; | ||
| 89 | } | ||
| 90 | |||
| 91 | BasicMotion GetMotion() { | ||
| 92 | return motion; | ||
| 93 | } | ||
| 94 | |||
| 95 | bool RumblePlay(const Common::Input::VibrationStatus vibration) { | ||
| 96 | constexpr u32 rumble_max_duration_ms = 1000; | ||
| 97 | if (sdl_controller) { | ||
| 98 | return SDL_GameControllerRumble( | ||
| 99 | sdl_controller.get(), static_cast<u16>(vibration.low_amplitude), | ||
| 100 | static_cast<u16>(vibration.high_amplitude), rumble_max_duration_ms) != -1; | ||
| 101 | } else if (sdl_joystick) { | ||
| 102 | return SDL_JoystickRumble(sdl_joystick.get(), static_cast<u16>(vibration.low_amplitude), | ||
| 103 | static_cast<u16>(vibration.high_amplitude), | ||
| 104 | rumble_max_duration_ms) != -1; | ||
| 105 | } | ||
| 106 | |||
| 107 | return false; | ||
| 108 | } | ||
| 109 | |||
| 110 | bool HasHDRumble() const { | ||
| 111 | if (sdl_controller) { | ||
| 112 | return (SDL_GameControllerGetType(sdl_controller.get()) == | ||
| 113 | SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO); | ||
| 114 | } | ||
| 115 | return false; | ||
| 116 | } | ||
| 117 | /** | ||
| 118 | * The Pad identifier of the joystick | ||
| 119 | */ | ||
| 120 | const PadIdentifier GetPadIdentifier() const { | ||
| 121 | return { | ||
| 122 | .guid = Common::UUID{guid}, | ||
| 123 | .port = static_cast<std::size_t>(port), | ||
| 124 | .pad = 0, | ||
| 125 | }; | ||
| 126 | } | ||
| 127 | |||
| 128 | /** | ||
| 129 | * The guid of the joystick | ||
| 130 | */ | ||
| 131 | const std::string& GetGUID() const { | ||
| 132 | return guid; | ||
| 133 | } | ||
| 134 | |||
| 135 | /** | ||
| 136 | * The number of joystick from the same type that were connected before this joystick | ||
| 137 | */ | ||
| 138 | int GetPort() const { | ||
| 139 | return port; | ||
| 140 | } | ||
| 141 | |||
| 142 | SDL_Joystick* GetSDLJoystick() const { | ||
| 143 | return sdl_joystick.get(); | ||
| 144 | } | ||
| 145 | |||
| 146 | SDL_GameController* GetSDLGameController() const { | ||
| 147 | return sdl_controller.get(); | ||
| 148 | } | ||
| 149 | |||
| 150 | void SetSDLJoystick(SDL_Joystick* joystick, SDL_GameController* controller) { | ||
| 151 | sdl_joystick.reset(joystick); | ||
| 152 | sdl_controller.reset(controller); | ||
| 153 | } | ||
| 154 | |||
| 155 | bool IsJoyconLeft() const { | ||
| 156 | const std::string controller_name = GetControllerName(); | ||
| 157 | if (std::strstr(controller_name.c_str(), "Joy-Con Left") != nullptr) { | ||
| 158 | return true; | ||
| 159 | } | ||
| 160 | if (std::strstr(controller_name.c_str(), "Joy-Con (L)") != nullptr) { | ||
| 161 | return true; | ||
| 162 | } | ||
| 163 | return false; | ||
| 164 | } | ||
| 165 | |||
| 166 | bool IsJoyconRight() const { | ||
| 167 | const std::string controller_name = GetControllerName(); | ||
| 168 | if (std::strstr(controller_name.c_str(), "Joy-Con Right") != nullptr) { | ||
| 169 | return true; | ||
| 170 | } | ||
| 171 | if (std::strstr(controller_name.c_str(), "Joy-Con (R)") != nullptr) { | ||
| 172 | return true; | ||
| 173 | } | ||
| 174 | return false; | ||
| 175 | } | ||
| 176 | |||
| 177 | BatteryLevel GetBatteryLevel() { | ||
| 178 | const auto level = SDL_JoystickCurrentPowerLevel(sdl_joystick.get()); | ||
| 179 | switch (level) { | ||
| 180 | case SDL_JOYSTICK_POWER_EMPTY: | ||
| 181 | return BatteryLevel::Empty; | ||
| 182 | case SDL_JOYSTICK_POWER_LOW: | ||
| 183 | return BatteryLevel::Critical; | ||
| 184 | case SDL_JOYSTICK_POWER_MEDIUM: | ||
| 185 | return BatteryLevel::Low; | ||
| 186 | case SDL_JOYSTICK_POWER_FULL: | ||
| 187 | return BatteryLevel::Medium; | ||
| 188 | case SDL_JOYSTICK_POWER_MAX: | ||
| 189 | return BatteryLevel::Full; | ||
| 190 | case SDL_JOYSTICK_POWER_UNKNOWN: | ||
| 191 | case SDL_JOYSTICK_POWER_WIRED: | ||
| 192 | default: | ||
| 193 | return BatteryLevel::Charging; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | std::string GetControllerName() const { | ||
| 198 | if (sdl_controller) { | ||
| 199 | switch (SDL_GameControllerGetType(sdl_controller.get())) { | ||
| 200 | case SDL_CONTROLLER_TYPE_XBOX360: | ||
| 201 | return "XBox 360 Controller"; | ||
| 202 | case SDL_CONTROLLER_TYPE_XBOXONE: | ||
| 203 | return "XBox One Controller"; | ||
| 204 | default: | ||
| 205 | break; | ||
| 206 | } | ||
| 207 | const auto name = SDL_GameControllerName(sdl_controller.get()); | ||
| 208 | if (name) { | ||
| 209 | return name; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | if (sdl_joystick) { | ||
| 214 | const auto name = SDL_JoystickName(sdl_joystick.get()); | ||
| 215 | if (name) { | ||
| 216 | return name; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | return "Unknown"; | ||
| 221 | } | ||
| 222 | |||
| 223 | private: | ||
| 224 | std::string guid; | ||
| 225 | int port; | ||
| 226 | std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; | ||
| 227 | std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller; | ||
| 228 | mutable std::mutex mutex; | ||
| 229 | |||
| 230 | u64 last_motion_update{}; | ||
| 231 | bool has_gyro{false}; | ||
| 232 | bool has_accel{false}; | ||
| 233 | BasicMotion motion; | ||
| 234 | }; | ||
| 235 | |||
| 236 | std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickByGUID(const std::string& guid, int port) { | ||
| 237 | std::lock_guard lock{joystick_map_mutex}; | ||
| 238 | const auto it = joystick_map.find(guid); | ||
| 239 | |||
| 240 | if (it != joystick_map.end()) { | ||
| 241 | while (it->second.size() <= static_cast<std::size_t>(port)) { | ||
| 242 | auto joystick = std::make_shared<SDLJoystick>(guid, static_cast<int>(it->second.size()), | ||
| 243 | nullptr, nullptr); | ||
| 244 | it->second.emplace_back(std::move(joystick)); | ||
| 245 | } | ||
| 246 | |||
| 247 | return it->second[static_cast<std::size_t>(port)]; | ||
| 248 | } | ||
| 249 | |||
| 250 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr, nullptr); | ||
| 251 | |||
| 252 | return joystick_map[guid].emplace_back(std::move(joystick)); | ||
| 253 | } | ||
| 254 | |||
| 255 | std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) { | ||
| 256 | auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); | ||
| 257 | const std::string guid = GetGUID(sdl_joystick); | ||
| 258 | |||
| 259 | std::lock_guard lock{joystick_map_mutex}; | ||
| 260 | const auto map_it = joystick_map.find(guid); | ||
| 261 | |||
| 262 | if (map_it == joystick_map.end()) { | ||
| 263 | return nullptr; | ||
| 264 | } | ||
| 265 | |||
| 266 | const auto vec_it = std::find_if(map_it->second.begin(), map_it->second.end(), | ||
| 267 | [&sdl_joystick](const auto& joystick) { | ||
| 268 | return joystick->GetSDLJoystick() == sdl_joystick; | ||
| 269 | }); | ||
| 270 | |||
| 271 | if (vec_it == map_it->second.end()) { | ||
| 272 | return nullptr; | ||
| 273 | } | ||
| 274 | |||
| 275 | return *vec_it; | ||
| 276 | } | ||
| 277 | |||
| 278 | void SDLDriver::InitJoystick(int joystick_index) { | ||
| 279 | SDL_Joystick* sdl_joystick = SDL_JoystickOpen(joystick_index); | ||
| 280 | SDL_GameController* sdl_gamecontroller = nullptr; | ||
| 281 | |||
| 282 | if (SDL_IsGameController(joystick_index)) { | ||
| 283 | sdl_gamecontroller = SDL_GameControllerOpen(joystick_index); | ||
| 284 | } | ||
| 285 | |||
| 286 | if (!sdl_joystick) { | ||
| 287 | LOG_ERROR(Input, "Failed to open joystick {}", joystick_index); | ||
| 288 | return; | ||
| 289 | } | ||
| 290 | |||
| 291 | const std::string guid = GetGUID(sdl_joystick); | ||
| 292 | |||
| 293 | std::lock_guard lock{joystick_map_mutex}; | ||
| 294 | if (joystick_map.find(guid) == joystick_map.end()) { | ||
| 295 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_gamecontroller); | ||
| 296 | PreSetController(joystick->GetPadIdentifier()); | ||
| 297 | SetBattery(joystick->GetPadIdentifier(), joystick->GetBatteryLevel()); | ||
| 298 | joystick_map[guid].emplace_back(std::move(joystick)); | ||
| 299 | return; | ||
| 300 | } | ||
| 301 | |||
| 302 | auto& joystick_guid_list = joystick_map[guid]; | ||
| 303 | const auto joystick_it = | ||
| 304 | std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), | ||
| 305 | [](const auto& joystick) { return !joystick->GetSDLJoystick(); }); | ||
| 306 | |||
| 307 | if (joystick_it != joystick_guid_list.end()) { | ||
| 308 | (*joystick_it)->SetSDLJoystick(sdl_joystick, sdl_gamecontroller); | ||
| 309 | return; | ||
| 310 | } | ||
| 311 | |||
| 312 | const int port = static_cast<int>(joystick_guid_list.size()); | ||
| 313 | auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick, sdl_gamecontroller); | ||
| 314 | PreSetController(joystick->GetPadIdentifier()); | ||
| 315 | SetBattery(joystick->GetPadIdentifier(), joystick->GetBatteryLevel()); | ||
| 316 | joystick_guid_list.emplace_back(std::move(joystick)); | ||
| 317 | } | ||
| 318 | |||
| 319 | void SDLDriver::CloseJoystick(SDL_Joystick* sdl_joystick) { | ||
| 320 | const std::string guid = GetGUID(sdl_joystick); | ||
| 321 | |||
| 322 | std::lock_guard lock{joystick_map_mutex}; | ||
| 323 | // This call to guid is safe since the joystick is guaranteed to be in the map | ||
| 324 | const auto& joystick_guid_list = joystick_map[guid]; | ||
| 325 | const auto joystick_it = std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), | ||
| 326 | [&sdl_joystick](const auto& joystick) { | ||
| 327 | return joystick->GetSDLJoystick() == sdl_joystick; | ||
| 328 | }); | ||
| 329 | |||
| 330 | if (joystick_it != joystick_guid_list.end()) { | ||
| 331 | (*joystick_it)->SetSDLJoystick(nullptr, nullptr); | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | void SDLDriver::HandleGameControllerEvent(const SDL_Event& event) { | ||
| 336 | switch (event.type) { | ||
| 337 | case SDL_JOYBUTTONUP: { | ||
| 338 | if (const auto joystick = GetSDLJoystickBySDLID(event.jbutton.which)) { | ||
| 339 | const PadIdentifier identifier = joystick->GetPadIdentifier(); | ||
| 340 | SetButton(identifier, event.jbutton.button, false); | ||
| 341 | } | ||
| 342 | break; | ||
| 343 | } | ||
| 344 | case SDL_JOYBUTTONDOWN: { | ||
| 345 | if (const auto joystick = GetSDLJoystickBySDLID(event.jbutton.which)) { | ||
| 346 | const PadIdentifier identifier = joystick->GetPadIdentifier(); | ||
| 347 | SetButton(identifier, event.jbutton.button, true); | ||
| 348 | } | ||
| 349 | break; | ||
| 350 | } | ||
| 351 | case SDL_JOYHATMOTION: { | ||
| 352 | if (const auto joystick = GetSDLJoystickBySDLID(event.jhat.which)) { | ||
| 353 | const PadIdentifier identifier = joystick->GetPadIdentifier(); | ||
| 354 | SetHatButton(identifier, event.jhat.hat, event.jhat.value); | ||
| 355 | } | ||
| 356 | break; | ||
| 357 | } | ||
| 358 | case SDL_JOYAXISMOTION: { | ||
| 359 | if (const auto joystick = GetSDLJoystickBySDLID(event.jaxis.which)) { | ||
| 360 | const PadIdentifier identifier = joystick->GetPadIdentifier(); | ||
| 361 | SetAxis(identifier, event.jaxis.axis, event.jaxis.value / 32767.0f); | ||
| 362 | } | ||
| 363 | break; | ||
| 364 | } | ||
| 365 | case SDL_CONTROLLERSENSORUPDATE: { | ||
| 366 | if (auto joystick = GetSDLJoystickBySDLID(event.csensor.which)) { | ||
| 367 | if (joystick->UpdateMotion(event.csensor)) { | ||
| 368 | const PadIdentifier identifier = joystick->GetPadIdentifier(); | ||
| 369 | SetMotion(identifier, 0, joystick->GetMotion()); | ||
| 370 | }; | ||
| 371 | } | ||
| 372 | break; | ||
| 373 | } | ||
| 374 | case SDL_JOYDEVICEREMOVED: | ||
| 375 | LOG_DEBUG(Input, "Controller removed with Instance_ID {}", event.jdevice.which); | ||
| 376 | CloseJoystick(SDL_JoystickFromInstanceID(event.jdevice.which)); | ||
| 377 | break; | ||
| 378 | case SDL_JOYDEVICEADDED: | ||
| 379 | LOG_DEBUG(Input, "Controller connected with device index {}", event.jdevice.which); | ||
| 380 | InitJoystick(event.jdevice.which); | ||
| 381 | break; | ||
| 382 | } | ||
| 383 | } | ||
| 384 | |||
| 385 | void SDLDriver::CloseJoysticks() { | ||
| 386 | std::lock_guard lock{joystick_map_mutex}; | ||
| 387 | joystick_map.clear(); | ||
| 388 | } | ||
| 389 | |||
| 390 | SDLDriver::SDLDriver(const std::string& input_engine_) : InputEngine(input_engine_) { | ||
| 391 | Common::SetCurrentThreadName("yuzu:input:SDL"); | ||
| 392 | |||
| 393 | if (!Settings::values.enable_raw_input) { | ||
| 394 | // Disable raw input. When enabled this setting causes SDL to die when a web applet opens | ||
| 395 | SDL_SetHint(SDL_HINT_JOYSTICK_RAWINPUT, "0"); | ||
| 396 | } | ||
| 397 | |||
| 398 | // Prevent SDL from adding undesired axis | ||
| 399 | SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); | ||
| 400 | |||
| 401 | // Enable HIDAPI rumble. This prevents SDL from disabling motion on PS4 and PS5 controllers | ||
| 402 | SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, "1"); | ||
| 403 | SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1"); | ||
| 404 | SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); | ||
| 405 | |||
| 406 | // Use hidapi driver for joycons. This will allow joycons to be detected as a GameController and | ||
| 407 | // not a generic one | ||
| 408 | SDL_SetHint("SDL_JOYSTICK_HIDAPI_JOY_CONS", "1"); | ||
| 409 | |||
| 410 | // Turn off Pro controller home led | ||
| 411 | SDL_SetHint("SDL_JOYSTICK_HIDAPI_SWITCH_HOME_LED", "0"); | ||
| 412 | |||
| 413 | // If the frontend is going to manage the event loop, then we don't start one here | ||
| 414 | start_thread = SDL_WasInit(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) == 0; | ||
| 415 | if (start_thread && SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0) { | ||
| 416 | LOG_CRITICAL(Input, "SDL_Init failed with: {}", SDL_GetError()); | ||
| 417 | return; | ||
| 418 | } | ||
| 419 | |||
| 420 | SDL_AddEventWatch(&SDLEventWatcher, this); | ||
| 421 | |||
| 422 | initialized = true; | ||
| 423 | if (start_thread) { | ||
| 424 | poll_thread = std::thread([this] { | ||
| 425 | using namespace std::chrono_literals; | ||
| 426 | while (initialized) { | ||
| 427 | SDL_PumpEvents(); | ||
| 428 | std::this_thread::sleep_for(1ms); | ||
| 429 | } | ||
| 430 | }); | ||
| 431 | } | ||
| 432 | // Because the events for joystick connection happens before we have our event watcher added, we | ||
| 433 | // can just open all the joysticks right here | ||
| 434 | for (int i = 0; i < SDL_NumJoysticks(); ++i) { | ||
| 435 | InitJoystick(i); | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | SDLDriver::~SDLDriver() { | ||
| 440 | CloseJoysticks(); | ||
| 441 | SDL_DelEventWatch(&SDLEventWatcher, this); | ||
| 442 | |||
| 443 | initialized = false; | ||
| 444 | if (start_thread) { | ||
| 445 | poll_thread.join(); | ||
| 446 | SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER); | ||
| 447 | } | ||
| 448 | } | ||
| 449 | |||
| 450 | std::vector<Common::ParamPackage> SDLDriver::GetInputDevices() const { | ||
| 451 | std::vector<Common::ParamPackage> devices; | ||
| 452 | std::unordered_map<int, std::shared_ptr<SDLJoystick>> joycon_pairs; | ||
| 453 | for (const auto& [key, value] : joystick_map) { | ||
| 454 | for (const auto& joystick : value) { | ||
| 455 | if (!joystick->GetSDLJoystick()) { | ||
| 456 | continue; | ||
| 457 | } | ||
| 458 | const std::string name = | ||
| 459 | fmt::format("{} {}", joystick->GetControllerName(), joystick->GetPort()); | ||
| 460 | devices.emplace_back(Common::ParamPackage{ | ||
| 461 | {"engine", GetEngineName()}, | ||
| 462 | {"display", std::move(name)}, | ||
| 463 | {"guid", joystick->GetGUID()}, | ||
| 464 | {"port", std::to_string(joystick->GetPort())}, | ||
| 465 | }); | ||
| 466 | if (joystick->IsJoyconLeft()) { | ||
| 467 | joycon_pairs.insert_or_assign(joystick->GetPort(), joystick); | ||
| 468 | } | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | // Add dual controllers | ||
| 473 | for (const auto& [key, value] : joystick_map) { | ||
| 474 | for (const auto& joystick : value) { | ||
| 475 | if (joystick->IsJoyconRight()) { | ||
| 476 | if (!joycon_pairs.contains(joystick->GetPort())) { | ||
| 477 | continue; | ||
| 478 | } | ||
| 479 | const auto joystick2 = joycon_pairs.at(joystick->GetPort()); | ||
| 480 | |||
| 481 | const std::string name = | ||
| 482 | fmt::format("{} {}", "Nintendo Dual Joy-Con", joystick->GetPort()); | ||
| 483 | devices.emplace_back(Common::ParamPackage{ | ||
| 484 | {"engine", GetEngineName()}, | ||
| 485 | {"display", std::move(name)}, | ||
| 486 | {"guid", joystick->GetGUID()}, | ||
| 487 | {"guid2", joystick2->GetGUID()}, | ||
| 488 | {"port", std::to_string(joystick->GetPort())}, | ||
| 489 | }); | ||
| 490 | } | ||
| 491 | } | ||
| 492 | } | ||
| 493 | return devices; | ||
| 494 | } | ||
| 495 | Common::Input::VibrationError SDLDriver::SetRumble(const PadIdentifier& identifier, | ||
| 496 | const Common::Input::VibrationStatus vibration) { | ||
| 497 | const auto joystick = | ||
| 498 | GetSDLJoystickByGUID(identifier.guid.Format(), static_cast<int>(identifier.port)); | ||
| 499 | const auto process_amplitude_exp = [](f32 amplitude, f32 factor) { | ||
| 500 | return (amplitude + std::pow(amplitude, factor)) * 0.5f * 0xFFFF; | ||
| 501 | }; | ||
| 502 | |||
| 503 | // Default exponential curve for rumble | ||
| 504 | f32 factor = 0.35f; | ||
| 505 | |||
| 506 | // If vibration is set as a linear output use a flatter value | ||
| 507 | if (vibration.type == Common::Input::VibrationAmplificationType::Linear) { | ||
| 508 | factor = 0.5f; | ||
| 509 | } | ||
| 510 | |||
| 511 | // Amplitude for HD rumble needs no modification | ||
| 512 | if (joystick->HasHDRumble()) { | ||
| 513 | factor = 1.0f; | ||
| 514 | } | ||
| 515 | |||
| 516 | const Common::Input::VibrationStatus new_vibration{ | ||
| 517 | .low_amplitude = process_amplitude_exp(vibration.low_amplitude, factor), | ||
| 518 | .low_frequency = vibration.low_frequency, | ||
| 519 | .high_amplitude = process_amplitude_exp(vibration.high_amplitude, factor), | ||
| 520 | .high_frequency = vibration.high_frequency, | ||
| 521 | .type = Common::Input::VibrationAmplificationType::Exponential, | ||
| 522 | }; | ||
| 523 | |||
| 524 | if (!joystick->RumblePlay(new_vibration)) { | ||
| 525 | return Common::Input::VibrationError::Unknown; | ||
| 526 | } | ||
| 527 | |||
| 528 | return Common::Input::VibrationError::None; | ||
| 529 | } | ||
| 530 | Common::ParamPackage SDLDriver::BuildAnalogParamPackageForButton(int port, std::string guid, | ||
| 531 | s32 axis, float value) const { | ||
| 532 | Common::ParamPackage params{}; | ||
| 533 | params.Set("engine", GetEngineName()); | ||
| 534 | params.Set("port", port); | ||
| 535 | params.Set("guid", std::move(guid)); | ||
| 536 | params.Set("axis", axis); | ||
| 537 | params.Set("threshold", "0.5"); | ||
| 538 | params.Set("invert", value < 0 ? "-" : "+"); | ||
| 539 | return params; | ||
| 540 | } | ||
| 541 | |||
| 542 | Common::ParamPackage SDLDriver::BuildButtonParamPackageForButton(int port, std::string guid, | ||
| 543 | s32 button) const { | ||
| 544 | Common::ParamPackage params{}; | ||
| 545 | params.Set("engine", GetEngineName()); | ||
| 546 | params.Set("port", port); | ||
| 547 | params.Set("guid", std::move(guid)); | ||
| 548 | params.Set("button", button); | ||
| 549 | return params; | ||
| 550 | } | ||
| 551 | |||
| 552 | Common::ParamPackage SDLDriver::BuildHatParamPackageForButton(int port, std::string guid, s32 hat, | ||
| 553 | u8 value) const { | ||
| 554 | Common::ParamPackage params{}; | ||
| 555 | params.Set("engine", GetEngineName()); | ||
| 556 | params.Set("port", port); | ||
| 557 | params.Set("guid", std::move(guid)); | ||
| 558 | params.Set("hat", hat); | ||
| 559 | params.Set("direction", GetHatButtonName(value)); | ||
| 560 | return params; | ||
| 561 | } | ||
| 562 | |||
| 563 | Common::ParamPackage SDLDriver::BuildMotionParam(int port, std::string guid) const { | ||
| 564 | Common::ParamPackage params{}; | ||
| 565 | params.Set("engine", GetEngineName()); | ||
| 566 | params.Set("motion", 0); | ||
| 567 | params.Set("port", port); | ||
| 568 | params.Set("guid", std::move(guid)); | ||
| 569 | return params; | ||
| 570 | } | ||
| 571 | |||
| 572 | Common::ParamPackage SDLDriver::BuildParamPackageForBinding( | ||
| 573 | int port, const std::string& guid, const SDL_GameControllerButtonBind& binding) const { | ||
| 574 | switch (binding.bindType) { | ||
| 575 | case SDL_CONTROLLER_BINDTYPE_NONE: | ||
| 576 | break; | ||
| 577 | case SDL_CONTROLLER_BINDTYPE_AXIS: | ||
| 578 | return BuildAnalogParamPackageForButton(port, guid, binding.value.axis); | ||
| 579 | case SDL_CONTROLLER_BINDTYPE_BUTTON: | ||
| 580 | return BuildButtonParamPackageForButton(port, guid, binding.value.button); | ||
| 581 | case SDL_CONTROLLER_BINDTYPE_HAT: | ||
| 582 | return BuildHatParamPackageForButton(port, guid, binding.value.hat.hat, | ||
| 583 | static_cast<u8>(binding.value.hat.hat_mask)); | ||
| 584 | } | ||
| 585 | return {}; | ||
| 586 | } | ||
| 587 | |||
| 588 | Common::ParamPackage SDLDriver::BuildParamPackageForAnalog(PadIdentifier identifier, int axis_x, | ||
| 589 | int axis_y, float offset_x, | ||
| 590 | float offset_y) const { | ||
| 591 | Common::ParamPackage params; | ||
| 592 | params.Set("engine", GetEngineName()); | ||
| 593 | params.Set("port", static_cast<int>(identifier.port)); | ||
| 594 | params.Set("guid", identifier.guid.Format()); | ||
| 595 | params.Set("axis_x", axis_x); | ||
| 596 | params.Set("axis_y", axis_y); | ||
| 597 | params.Set("offset_x", offset_x); | ||
| 598 | params.Set("offset_y", offset_y); | ||
| 599 | params.Set("invert_x", "+"); | ||
| 600 | params.Set("invert_y", "+"); | ||
| 601 | return params; | ||
| 602 | } | ||
| 603 | |||
| 604 | ButtonMapping SDLDriver::GetButtonMappingForDevice(const Common::ParamPackage& params) { | ||
| 605 | if (!params.Has("guid") || !params.Has("port")) { | ||
| 606 | return {}; | ||
| 607 | } | ||
| 608 | const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0)); | ||
| 609 | |||
| 610 | auto* controller = joystick->GetSDLGameController(); | ||
| 611 | if (controller == nullptr) { | ||
| 612 | return {}; | ||
| 613 | } | ||
| 614 | |||
| 615 | // This list is missing ZL/ZR since those are not considered buttons in SDL GameController. | ||
| 616 | // We will add those afterwards | ||
| 617 | // This list also excludes Screenshot since theres not really a mapping for that | ||
| 618 | ButtonBindings switch_to_sdl_button; | ||
| 619 | |||
| 620 | if (SDL_GameControllerGetType(controller) == SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO) { | ||
| 621 | switch_to_sdl_button = GetNintendoButtonBinding(joystick); | ||
| 622 | } else { | ||
| 623 | switch_to_sdl_button = GetDefaultButtonBinding(); | ||
| 624 | } | ||
| 625 | |||
| 626 | // Add the missing bindings for ZL/ZR | ||
| 627 | static constexpr ZButtonBindings switch_to_sdl_axis{{ | ||
| 628 | {Settings::NativeButton::ZL, SDL_CONTROLLER_AXIS_TRIGGERLEFT}, | ||
| 629 | {Settings::NativeButton::ZR, SDL_CONTROLLER_AXIS_TRIGGERRIGHT}, | ||
| 630 | }}; | ||
| 631 | |||
| 632 | // Parameters contain two joysticks return dual | ||
| 633 | if (params.Has("guid2")) { | ||
| 634 | const auto joystick2 = GetSDLJoystickByGUID(params.Get("guid2", ""), params.Get("port", 0)); | ||
| 635 | |||
| 636 | if (joystick2->GetSDLGameController() != nullptr) { | ||
| 637 | return GetDualControllerMapping(joystick, joystick2, switch_to_sdl_button, | ||
| 638 | switch_to_sdl_axis); | ||
| 639 | } | ||
| 640 | } | ||
| 641 | |||
| 642 | return GetSingleControllerMapping(joystick, switch_to_sdl_button, switch_to_sdl_axis); | ||
| 643 | } | ||
| 644 | |||
| 645 | ButtonBindings SDLDriver::GetDefaultButtonBinding() const { | ||
| 646 | return { | ||
| 647 | std::pair{Settings::NativeButton::A, SDL_CONTROLLER_BUTTON_B}, | ||
| 648 | {Settings::NativeButton::B, SDL_CONTROLLER_BUTTON_A}, | ||
| 649 | {Settings::NativeButton::X, SDL_CONTROLLER_BUTTON_Y}, | ||
| 650 | {Settings::NativeButton::Y, SDL_CONTROLLER_BUTTON_X}, | ||
| 651 | {Settings::NativeButton::LStick, SDL_CONTROLLER_BUTTON_LEFTSTICK}, | ||
| 652 | {Settings::NativeButton::RStick, SDL_CONTROLLER_BUTTON_RIGHTSTICK}, | ||
| 653 | {Settings::NativeButton::L, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, | ||
| 654 | {Settings::NativeButton::R, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, | ||
| 655 | {Settings::NativeButton::Plus, SDL_CONTROLLER_BUTTON_START}, | ||
| 656 | {Settings::NativeButton::Minus, SDL_CONTROLLER_BUTTON_BACK}, | ||
| 657 | {Settings::NativeButton::DLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT}, | ||
| 658 | {Settings::NativeButton::DUp, SDL_CONTROLLER_BUTTON_DPAD_UP}, | ||
| 659 | {Settings::NativeButton::DRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT}, | ||
| 660 | {Settings::NativeButton::DDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN}, | ||
| 661 | {Settings::NativeButton::SL, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, | ||
| 662 | {Settings::NativeButton::SR, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, | ||
| 663 | {Settings::NativeButton::Home, SDL_CONTROLLER_BUTTON_GUIDE}, | ||
| 664 | }; | ||
| 665 | } | ||
| 666 | |||
| 667 | ButtonBindings SDLDriver::GetNintendoButtonBinding( | ||
| 668 | const std::shared_ptr<SDLJoystick>& joystick) const { | ||
| 669 | // Default SL/SR mapping for pro controllers | ||
| 670 | auto sl_button = SDL_CONTROLLER_BUTTON_LEFTSHOULDER; | ||
| 671 | auto sr_button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER; | ||
| 672 | |||
| 673 | if (joystick->IsJoyconLeft()) { | ||
| 674 | sl_button = SDL_CONTROLLER_BUTTON_PADDLE2; | ||
| 675 | sr_button = SDL_CONTROLLER_BUTTON_PADDLE4; | ||
| 676 | } | ||
| 677 | if (joystick->IsJoyconRight()) { | ||
| 678 | sl_button = SDL_CONTROLLER_BUTTON_PADDLE3; | ||
| 679 | sr_button = SDL_CONTROLLER_BUTTON_PADDLE1; | ||
| 680 | } | ||
| 681 | |||
| 682 | return { | ||
| 683 | std::pair{Settings::NativeButton::A, SDL_CONTROLLER_BUTTON_A}, | ||
| 684 | {Settings::NativeButton::B, SDL_CONTROLLER_BUTTON_B}, | ||
| 685 | {Settings::NativeButton::X, SDL_CONTROLLER_BUTTON_X}, | ||
| 686 | {Settings::NativeButton::Y, SDL_CONTROLLER_BUTTON_Y}, | ||
| 687 | {Settings::NativeButton::LStick, SDL_CONTROLLER_BUTTON_LEFTSTICK}, | ||
| 688 | {Settings::NativeButton::RStick, SDL_CONTROLLER_BUTTON_RIGHTSTICK}, | ||
| 689 | {Settings::NativeButton::L, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, | ||
| 690 | {Settings::NativeButton::R, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, | ||
| 691 | {Settings::NativeButton::Plus, SDL_CONTROLLER_BUTTON_START}, | ||
| 692 | {Settings::NativeButton::Minus, SDL_CONTROLLER_BUTTON_BACK}, | ||
| 693 | {Settings::NativeButton::DLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT}, | ||
| 694 | {Settings::NativeButton::DUp, SDL_CONTROLLER_BUTTON_DPAD_UP}, | ||
| 695 | {Settings::NativeButton::DRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT}, | ||
| 696 | {Settings::NativeButton::DDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN}, | ||
| 697 | {Settings::NativeButton::SL, sl_button}, | ||
| 698 | {Settings::NativeButton::SR, sr_button}, | ||
| 699 | {Settings::NativeButton::Home, SDL_CONTROLLER_BUTTON_GUIDE}, | ||
| 700 | }; | ||
| 701 | } | ||
| 702 | |||
| 703 | ButtonMapping SDLDriver::GetSingleControllerMapping( | ||
| 704 | const std::shared_ptr<SDLJoystick>& joystick, const ButtonBindings& switch_to_sdl_button, | ||
| 705 | const ZButtonBindings& switch_to_sdl_axis) const { | ||
| 706 | ButtonMapping mapping; | ||
| 707 | mapping.reserve(switch_to_sdl_button.size() + switch_to_sdl_axis.size()); | ||
| 708 | auto* controller = joystick->GetSDLGameController(); | ||
| 709 | |||
| 710 | for (const auto& [switch_button, sdl_button] : switch_to_sdl_button) { | ||
| 711 | const auto& binding = SDL_GameControllerGetBindForButton(controller, sdl_button); | ||
| 712 | mapping.insert_or_assign( | ||
| 713 | switch_button, | ||
| 714 | BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); | ||
| 715 | } | ||
| 716 | for (const auto& [switch_button, sdl_axis] : switch_to_sdl_axis) { | ||
| 717 | const auto& binding = SDL_GameControllerGetBindForAxis(controller, sdl_axis); | ||
| 718 | mapping.insert_or_assign( | ||
| 719 | switch_button, | ||
| 720 | BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); | ||
| 721 | } | ||
| 722 | |||
| 723 | return mapping; | ||
| 724 | } | ||
| 725 | |||
| 726 | ButtonMapping SDLDriver::GetDualControllerMapping(const std::shared_ptr<SDLJoystick>& joystick, | ||
| 727 | const std::shared_ptr<SDLJoystick>& joystick2, | ||
| 728 | const ButtonBindings& switch_to_sdl_button, | ||
| 729 | const ZButtonBindings& switch_to_sdl_axis) const { | ||
| 730 | ButtonMapping mapping; | ||
| 731 | mapping.reserve(switch_to_sdl_button.size() + switch_to_sdl_axis.size()); | ||
| 732 | auto* controller = joystick->GetSDLGameController(); | ||
| 733 | auto* controller2 = joystick2->GetSDLGameController(); | ||
| 734 | |||
| 735 | for (const auto& [switch_button, sdl_button] : switch_to_sdl_button) { | ||
| 736 | if (IsButtonOnLeftSide(switch_button)) { | ||
| 737 | const auto& binding = SDL_GameControllerGetBindForButton(controller2, sdl_button); | ||
| 738 | mapping.insert_or_assign( | ||
| 739 | switch_button, | ||
| 740 | BuildParamPackageForBinding(joystick2->GetPort(), joystick2->GetGUID(), binding)); | ||
| 741 | continue; | ||
| 742 | } | ||
| 743 | const auto& binding = SDL_GameControllerGetBindForButton(controller, sdl_button); | ||
| 744 | mapping.insert_or_assign( | ||
| 745 | switch_button, | ||
| 746 | BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); | ||
| 747 | } | ||
| 748 | for (const auto& [switch_button, sdl_axis] : switch_to_sdl_axis) { | ||
| 749 | if (IsButtonOnLeftSide(switch_button)) { | ||
| 750 | const auto& binding = SDL_GameControllerGetBindForAxis(controller2, sdl_axis); | ||
| 751 | mapping.insert_or_assign( | ||
| 752 | switch_button, | ||
| 753 | BuildParamPackageForBinding(joystick2->GetPort(), joystick2->GetGUID(), binding)); | ||
| 754 | continue; | ||
| 755 | } | ||
| 756 | const auto& binding = SDL_GameControllerGetBindForAxis(controller, sdl_axis); | ||
| 757 | mapping.insert_or_assign( | ||
| 758 | switch_button, | ||
| 759 | BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); | ||
| 760 | } | ||
| 761 | |||
| 762 | return mapping; | ||
| 763 | } | ||
| 764 | |||
| 765 | bool SDLDriver::IsButtonOnLeftSide(Settings::NativeButton::Values button) const { | ||
| 766 | switch (button) { | ||
| 767 | case Settings::NativeButton::DDown: | ||
| 768 | case Settings::NativeButton::DLeft: | ||
| 769 | case Settings::NativeButton::DRight: | ||
| 770 | case Settings::NativeButton::DUp: | ||
| 771 | case Settings::NativeButton::L: | ||
| 772 | case Settings::NativeButton::LStick: | ||
| 773 | case Settings::NativeButton::Minus: | ||
| 774 | case Settings::NativeButton::Screenshot: | ||
| 775 | case Settings::NativeButton::ZL: | ||
| 776 | return true; | ||
| 777 | default: | ||
| 778 | return false; | ||
| 779 | } | ||
| 780 | } | ||
| 781 | |||
| 782 | AnalogMapping SDLDriver::GetAnalogMappingForDevice(const Common::ParamPackage& params) { | ||
| 783 | if (!params.Has("guid") || !params.Has("port")) { | ||
| 784 | return {}; | ||
| 785 | } | ||
| 786 | const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0)); | ||
| 787 | const auto joystick2 = GetSDLJoystickByGUID(params.Get("guid2", ""), params.Get("port", 0)); | ||
| 788 | auto* controller = joystick->GetSDLGameController(); | ||
| 789 | if (controller == nullptr) { | ||
| 790 | return {}; | ||
| 791 | } | ||
| 792 | |||
| 793 | AnalogMapping mapping = {}; | ||
| 794 | const auto& binding_left_x = | ||
| 795 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX); | ||
| 796 | const auto& binding_left_y = | ||
| 797 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTY); | ||
| 798 | if (params.Has("guid2")) { | ||
| 799 | const auto identifier = joystick2->GetPadIdentifier(); | ||
| 800 | PreSetController(identifier); | ||
| 801 | PreSetAxis(identifier, binding_left_x.value.axis); | ||
| 802 | PreSetAxis(identifier, binding_left_y.value.axis); | ||
| 803 | const auto left_offset_x = -GetAxis(identifier, binding_left_x.value.axis); | ||
| 804 | const auto left_offset_y = -GetAxis(identifier, binding_left_y.value.axis); | ||
| 805 | mapping.insert_or_assign(Settings::NativeAnalog::LStick, | ||
| 806 | BuildParamPackageForAnalog(identifier, binding_left_x.value.axis, | ||
| 807 | binding_left_y.value.axis, | ||
| 808 | left_offset_x, left_offset_y)); | ||
| 809 | } else { | ||
| 810 | const auto identifier = joystick->GetPadIdentifier(); | ||
| 811 | PreSetController(identifier); | ||
| 812 | PreSetAxis(identifier, binding_left_x.value.axis); | ||
| 813 | PreSetAxis(identifier, binding_left_y.value.axis); | ||
| 814 | const auto left_offset_x = -GetAxis(identifier, binding_left_x.value.axis); | ||
| 815 | const auto left_offset_y = -GetAxis(identifier, binding_left_y.value.axis); | ||
| 816 | mapping.insert_or_assign(Settings::NativeAnalog::LStick, | ||
| 817 | BuildParamPackageForAnalog(identifier, binding_left_x.value.axis, | ||
| 818 | binding_left_y.value.axis, | ||
| 819 | left_offset_x, left_offset_y)); | ||
| 820 | } | ||
| 821 | const auto& binding_right_x = | ||
| 822 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX); | ||
| 823 | const auto& binding_right_y = | ||
| 824 | SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY); | ||
| 825 | const auto identifier = joystick->GetPadIdentifier(); | ||
| 826 | PreSetController(identifier); | ||
| 827 | PreSetAxis(identifier, binding_right_x.value.axis); | ||
| 828 | PreSetAxis(identifier, binding_right_y.value.axis); | ||
| 829 | const auto right_offset_x = -GetAxis(identifier, binding_right_x.value.axis); | ||
| 830 | const auto right_offset_y = -GetAxis(identifier, binding_right_y.value.axis); | ||
| 831 | mapping.insert_or_assign(Settings::NativeAnalog::RStick, | ||
| 832 | BuildParamPackageForAnalog(identifier, binding_right_x.value.axis, | ||
| 833 | binding_right_y.value.axis, right_offset_x, | ||
| 834 | right_offset_y)); | ||
| 835 | return mapping; | ||
| 836 | } | ||
| 837 | |||
| 838 | MotionMapping SDLDriver::GetMotionMappingForDevice(const Common::ParamPackage& params) { | ||
| 839 | if (!params.Has("guid") || !params.Has("port")) { | ||
| 840 | return {}; | ||
| 841 | } | ||
| 842 | const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0)); | ||
| 843 | const auto joystick2 = GetSDLJoystickByGUID(params.Get("guid2", ""), params.Get("port", 0)); | ||
| 844 | auto* controller = joystick->GetSDLGameController(); | ||
| 845 | if (controller == nullptr) { | ||
| 846 | return {}; | ||
| 847 | } | ||
| 848 | |||
| 849 | MotionMapping mapping = {}; | ||
| 850 | joystick->EnableMotion(); | ||
| 851 | |||
| 852 | if (joystick->HasGyro() || joystick->HasAccel()) { | ||
| 853 | mapping.insert_or_assign(Settings::NativeMotion::MotionRight, | ||
| 854 | BuildMotionParam(joystick->GetPort(), joystick->GetGUID())); | ||
| 855 | } | ||
| 856 | if (params.Has("guid2")) { | ||
| 857 | joystick2->EnableMotion(); | ||
| 858 | if (joystick2->HasGyro() || joystick2->HasAccel()) { | ||
| 859 | mapping.insert_or_assign(Settings::NativeMotion::MotionLeft, | ||
| 860 | BuildMotionParam(joystick2->GetPort(), joystick2->GetGUID())); | ||
| 861 | } | ||
| 862 | } else { | ||
| 863 | if (joystick->HasGyro() || joystick->HasAccel()) { | ||
| 864 | mapping.insert_or_assign(Settings::NativeMotion::MotionLeft, | ||
| 865 | BuildMotionParam(joystick->GetPort(), joystick->GetGUID())); | ||
| 866 | } | ||
| 867 | } | ||
| 868 | |||
| 869 | return mapping; | ||
| 870 | } | ||
| 871 | |||
| 872 | Common::Input::ButtonNames SDLDriver::GetUIName(const Common::ParamPackage& params) const { | ||
| 873 | if (params.Has("button")) { | ||
| 874 | // TODO(German77): Find how to substitue the values for real button names | ||
| 875 | return Common::Input::ButtonNames::Value; | ||
| 876 | } | ||
| 877 | if (params.Has("hat")) { | ||
| 878 | return Common::Input::ButtonNames::Value; | ||
| 879 | } | ||
| 880 | if (params.Has("axis")) { | ||
| 881 | return Common::Input::ButtonNames::Value; | ||
| 882 | } | ||
| 883 | if (params.Has("axis_x") && params.Has("axis_y") && params.Has("axis_z")) { | ||
| 884 | return Common::Input::ButtonNames::Value; | ||
| 885 | } | ||
| 886 | if (params.Has("motion")) { | ||
| 887 | return Common::Input::ButtonNames::Engine; | ||
| 888 | } | ||
| 889 | |||
| 890 | return Common::Input::ButtonNames::Invalid; | ||
| 891 | } | ||
| 892 | |||
| 893 | std::string SDLDriver::GetHatButtonName(u8 direction_value) const { | ||
| 894 | switch (direction_value) { | ||
| 895 | case SDL_HAT_UP: | ||
| 896 | return "up"; | ||
| 897 | case SDL_HAT_DOWN: | ||
| 898 | return "down"; | ||
| 899 | case SDL_HAT_LEFT: | ||
| 900 | return "left"; | ||
| 901 | case SDL_HAT_RIGHT: | ||
| 902 | return "right"; | ||
| 903 | default: | ||
| 904 | return {}; | ||
| 905 | } | ||
| 906 | } | ||
| 907 | |||
| 908 | u8 SDLDriver::GetHatButtonId(const std::string& direction_name) const { | ||
| 909 | Uint8 direction; | ||
| 910 | if (direction_name == "up") { | ||
| 911 | direction = SDL_HAT_UP; | ||
| 912 | } else if (direction_name == "down") { | ||
| 913 | direction = SDL_HAT_DOWN; | ||
| 914 | } else if (direction_name == "left") { | ||
| 915 | direction = SDL_HAT_LEFT; | ||
| 916 | } else if (direction_name == "right") { | ||
| 917 | direction = SDL_HAT_RIGHT; | ||
| 918 | } else { | ||
| 919 | direction = 0; | ||
| 920 | } | ||
| 921 | return direction; | ||
| 922 | } | ||
| 923 | |||
| 924 | } // namespace InputCommon | ||