diff options
Diffstat (limited to 'src/input_common/drivers/sdl_driver.cpp')
| -rw-r--r-- | src/input_common/drivers/sdl_driver.cpp | 79 |
1 files changed, 51 insertions, 28 deletions
diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cpp index a5c63e74a..446c027d3 100644 --- a/src/input_common/drivers/sdl_driver.cpp +++ b/src/input_common/drivers/sdl_driver.cpp | |||
| @@ -13,11 +13,11 @@ | |||
| 13 | namespace InputCommon { | 13 | namespace InputCommon { |
| 14 | 14 | ||
| 15 | namespace { | 15 | namespace { |
| 16 | std::string GetGUID(SDL_Joystick* joystick) { | 16 | Common::UUID GetGUID(SDL_Joystick* joystick) { |
| 17 | const SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); | 17 | const SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); |
| 18 | char guid_str[33]; | 18 | std::array<u8, 16> data{}; |
| 19 | SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str)); | 19 | std::memcpy(data.data(), guid.data, sizeof(data)); |
| 20 | return guid_str; | 20 | return Common::UUID{data}; |
| 21 | } | 21 | } |
| 22 | } // Anonymous namespace | 22 | } // Anonymous namespace |
| 23 | 23 | ||
| @@ -31,9 +31,9 @@ static int SDLEventWatcher(void* user_data, SDL_Event* event) { | |||
| 31 | 31 | ||
| 32 | class SDLJoystick { | 32 | class SDLJoystick { |
| 33 | public: | 33 | public: |
| 34 | SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick, | 34 | SDLJoystick(Common::UUID guid_, int port_, SDL_Joystick* joystick, |
| 35 | SDL_GameController* game_controller) | 35 | SDL_GameController* game_controller) |
| 36 | : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose}, | 36 | : guid{guid_}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose}, |
| 37 | sdl_controller{game_controller, &SDL_GameControllerClose} { | 37 | sdl_controller{game_controller, &SDL_GameControllerClose} { |
| 38 | EnableMotion(); | 38 | EnableMotion(); |
| 39 | } | 39 | } |
| @@ -120,7 +120,7 @@ public: | |||
| 120 | */ | 120 | */ |
| 121 | const PadIdentifier GetPadIdentifier() const { | 121 | const PadIdentifier GetPadIdentifier() const { |
| 122 | return { | 122 | return { |
| 123 | .guid = Common::UUID{guid}, | 123 | .guid = guid, |
| 124 | .port = static_cast<std::size_t>(port), | 124 | .port = static_cast<std::size_t>(port), |
| 125 | .pad = 0, | 125 | .pad = 0, |
| 126 | }; | 126 | }; |
| @@ -129,7 +129,7 @@ public: | |||
| 129 | /** | 129 | /** |
| 130 | * The guid of the joystick | 130 | * The guid of the joystick |
| 131 | */ | 131 | */ |
| 132 | const std::string& GetGUID() const { | 132 | const Common::UUID& GetGUID() const { |
| 133 | return guid; | 133 | return guid; |
| 134 | } | 134 | } |
| 135 | 135 | ||
| @@ -228,7 +228,7 @@ public: | |||
| 228 | } | 228 | } |
| 229 | 229 | ||
| 230 | private: | 230 | private: |
| 231 | std::string guid; | 231 | Common::UUID guid; |
| 232 | int port; | 232 | int port; |
| 233 | std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; | 233 | std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; |
| 234 | std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller; | 234 | std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller; |
| @@ -240,7 +240,7 @@ private: | |||
| 240 | BasicMotion motion; | 240 | BasicMotion motion; |
| 241 | }; | 241 | }; |
| 242 | 242 | ||
| 243 | std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickByGUID(const std::string& guid, int port) { | 243 | std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickByGUID(const Common::UUID& guid, int port) { |
| 244 | std::scoped_lock lock{joystick_map_mutex}; | 244 | std::scoped_lock lock{joystick_map_mutex}; |
| 245 | const auto it = joystick_map.find(guid); | 245 | const auto it = joystick_map.find(guid); |
| 246 | 246 | ||
| @@ -259,9 +259,13 @@ std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickByGUID(const std::string& | |||
| 259 | return joystick_map[guid].emplace_back(std::move(joystick)); | 259 | return joystick_map[guid].emplace_back(std::move(joystick)); |
| 260 | } | 260 | } |
| 261 | 261 | ||
| 262 | std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickByGUID(const std::string& guid, int port) { | ||
| 263 | return GetSDLJoystickByGUID(Common::UUID{guid}, port); | ||
| 264 | } | ||
| 265 | |||
| 262 | std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) { | 266 | std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) { |
| 263 | auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); | 267 | auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); |
| 264 | const std::string guid = GetGUID(sdl_joystick); | 268 | const auto guid = GetGUID(sdl_joystick); |
| 265 | 269 | ||
| 266 | std::scoped_lock lock{joystick_map_mutex}; | 270 | std::scoped_lock lock{joystick_map_mutex}; |
| 267 | const auto map_it = joystick_map.find(guid); | 271 | const auto map_it = joystick_map.find(guid); |
| @@ -295,7 +299,7 @@ void SDLDriver::InitJoystick(int joystick_index) { | |||
| 295 | return; | 299 | return; |
| 296 | } | 300 | } |
| 297 | 301 | ||
| 298 | const std::string guid = GetGUID(sdl_joystick); | 302 | const auto guid = GetGUID(sdl_joystick); |
| 299 | 303 | ||
| 300 | std::scoped_lock lock{joystick_map_mutex}; | 304 | std::scoped_lock lock{joystick_map_mutex}; |
| 301 | if (joystick_map.find(guid) == joystick_map.end()) { | 305 | if (joystick_map.find(guid) == joystick_map.end()) { |
| @@ -324,7 +328,7 @@ void SDLDriver::InitJoystick(int joystick_index) { | |||
| 324 | } | 328 | } |
| 325 | 329 | ||
| 326 | void SDLDriver::CloseJoystick(SDL_Joystick* sdl_joystick) { | 330 | void SDLDriver::CloseJoystick(SDL_Joystick* sdl_joystick) { |
| 327 | const std::string guid = GetGUID(sdl_joystick); | 331 | const auto guid = GetGUID(sdl_joystick); |
| 328 | 332 | ||
| 329 | std::scoped_lock lock{joystick_map_mutex}; | 333 | std::scoped_lock lock{joystick_map_mutex}; |
| 330 | // This call to guid is safe since the joystick is guaranteed to be in the map | 334 | // This call to guid is safe since the joystick is guaranteed to be in the map |
| @@ -434,6 +438,7 @@ SDLDriver::SDLDriver(std::string input_engine_) : InputEngine(std::move(input_en | |||
| 434 | using namespace std::chrono_literals; | 438 | using namespace std::chrono_literals; |
| 435 | while (initialized) { | 439 | while (initialized) { |
| 436 | SDL_PumpEvents(); | 440 | SDL_PumpEvents(); |
| 441 | SendVibrations(); | ||
| 437 | std::this_thread::sleep_for(1ms); | 442 | std::this_thread::sleep_for(1ms); |
| 438 | } | 443 | } |
| 439 | }); | 444 | }); |
| @@ -469,7 +474,7 @@ std::vector<Common::ParamPackage> SDLDriver::GetInputDevices() const { | |||
| 469 | devices.emplace_back(Common::ParamPackage{ | 474 | devices.emplace_back(Common::ParamPackage{ |
| 470 | {"engine", GetEngineName()}, | 475 | {"engine", GetEngineName()}, |
| 471 | {"display", std::move(name)}, | 476 | {"display", std::move(name)}, |
| 472 | {"guid", joystick->GetGUID()}, | 477 | {"guid", joystick->GetGUID().RawString()}, |
| 473 | {"port", std::to_string(joystick->GetPort())}, | 478 | {"port", std::to_string(joystick->GetPort())}, |
| 474 | }); | 479 | }); |
| 475 | if (joystick->IsJoyconLeft()) { | 480 | if (joystick->IsJoyconLeft()) { |
| @@ -492,8 +497,8 @@ std::vector<Common::ParamPackage> SDLDriver::GetInputDevices() const { | |||
| 492 | devices.emplace_back(Common::ParamPackage{ | 497 | devices.emplace_back(Common::ParamPackage{ |
| 493 | {"engine", GetEngineName()}, | 498 | {"engine", GetEngineName()}, |
| 494 | {"display", std::move(name)}, | 499 | {"display", std::move(name)}, |
| 495 | {"guid", joystick->GetGUID()}, | 500 | {"guid", joystick->GetGUID().RawString()}, |
| 496 | {"guid2", joystick2->GetGUID()}, | 501 | {"guid2", joystick2->GetGUID().RawString()}, |
| 497 | {"port", std::to_string(joystick->GetPort())}, | 502 | {"port", std::to_string(joystick->GetPort())}, |
| 498 | }); | 503 | }); |
| 499 | } | 504 | } |
| @@ -531,57 +536,75 @@ Common::Input::VibrationError SDLDriver::SetRumble( | |||
| 531 | .type = Common::Input::VibrationAmplificationType::Exponential, | 536 | .type = Common::Input::VibrationAmplificationType::Exponential, |
| 532 | }; | 537 | }; |
| 533 | 538 | ||
| 534 | if (!joystick->RumblePlay(new_vibration)) { | 539 | if (vibration.type == Common::Input::VibrationAmplificationType::Test) { |
| 535 | return Common::Input::VibrationError::Unknown; | 540 | if (!joystick->RumblePlay(new_vibration)) { |
| 541 | return Common::Input::VibrationError::Unknown; | ||
| 542 | } | ||
| 543 | return Common::Input::VibrationError::None; | ||
| 536 | } | 544 | } |
| 537 | 545 | ||
| 546 | vibration_queue.Push(VibrationRequest{ | ||
| 547 | .identifier = identifier, | ||
| 548 | .vibration = new_vibration, | ||
| 549 | }); | ||
| 550 | |||
| 538 | return Common::Input::VibrationError::None; | 551 | return Common::Input::VibrationError::None; |
| 539 | } | 552 | } |
| 540 | 553 | ||
| 541 | Common::ParamPackage SDLDriver::BuildAnalogParamPackageForButton(int port, std::string guid, | 554 | void SDLDriver::SendVibrations() { |
| 555 | while (!vibration_queue.Empty()) { | ||
| 556 | VibrationRequest request; | ||
| 557 | vibration_queue.Pop(request); | ||
| 558 | const auto joystick = GetSDLJoystickByGUID(request.identifier.guid.RawString(), | ||
| 559 | static_cast<int>(request.identifier.port)); | ||
| 560 | joystick->RumblePlay(request.vibration); | ||
| 561 | } | ||
| 562 | } | ||
| 563 | |||
| 564 | Common::ParamPackage SDLDriver::BuildAnalogParamPackageForButton(int port, const Common::UUID& guid, | ||
| 542 | s32 axis, float value) const { | 565 | s32 axis, float value) const { |
| 543 | Common::ParamPackage params{}; | 566 | Common::ParamPackage params{}; |
| 544 | params.Set("engine", GetEngineName()); | 567 | params.Set("engine", GetEngineName()); |
| 545 | params.Set("port", port); | 568 | params.Set("port", port); |
| 546 | params.Set("guid", std::move(guid)); | 569 | params.Set("guid", guid.RawString()); |
| 547 | params.Set("axis", axis); | 570 | params.Set("axis", axis); |
| 548 | params.Set("threshold", "0.5"); | 571 | params.Set("threshold", "0.5"); |
| 549 | params.Set("invert", value < 0 ? "-" : "+"); | 572 | params.Set("invert", value < 0 ? "-" : "+"); |
| 550 | return params; | 573 | return params; |
| 551 | } | 574 | } |
| 552 | 575 | ||
| 553 | Common::ParamPackage SDLDriver::BuildButtonParamPackageForButton(int port, std::string guid, | 576 | Common::ParamPackage SDLDriver::BuildButtonParamPackageForButton(int port, const Common::UUID& guid, |
| 554 | s32 button) const { | 577 | s32 button) const { |
| 555 | Common::ParamPackage params{}; | 578 | Common::ParamPackage params{}; |
| 556 | params.Set("engine", GetEngineName()); | 579 | params.Set("engine", GetEngineName()); |
| 557 | params.Set("port", port); | 580 | params.Set("port", port); |
| 558 | params.Set("guid", std::move(guid)); | 581 | params.Set("guid", guid.RawString()); |
| 559 | params.Set("button", button); | 582 | params.Set("button", button); |
| 560 | return params; | 583 | return params; |
| 561 | } | 584 | } |
| 562 | 585 | ||
| 563 | Common::ParamPackage SDLDriver::BuildHatParamPackageForButton(int port, std::string guid, s32 hat, | 586 | Common::ParamPackage SDLDriver::BuildHatParamPackageForButton(int port, const Common::UUID& guid, |
| 564 | u8 value) const { | 587 | s32 hat, u8 value) const { |
| 565 | Common::ParamPackage params{}; | 588 | Common::ParamPackage params{}; |
| 566 | params.Set("engine", GetEngineName()); | 589 | params.Set("engine", GetEngineName()); |
| 567 | params.Set("port", port); | 590 | params.Set("port", port); |
| 568 | params.Set("guid", std::move(guid)); | 591 | params.Set("guid", guid.RawString()); |
| 569 | params.Set("hat", hat); | 592 | params.Set("hat", hat); |
| 570 | params.Set("direction", GetHatButtonName(value)); | 593 | params.Set("direction", GetHatButtonName(value)); |
| 571 | return params; | 594 | return params; |
| 572 | } | 595 | } |
| 573 | 596 | ||
| 574 | Common::ParamPackage SDLDriver::BuildMotionParam(int port, std::string guid) const { | 597 | Common::ParamPackage SDLDriver::BuildMotionParam(int port, const Common::UUID& guid) const { |
| 575 | Common::ParamPackage params{}; | 598 | Common::ParamPackage params{}; |
| 576 | params.Set("engine", GetEngineName()); | 599 | params.Set("engine", GetEngineName()); |
| 577 | params.Set("motion", 0); | 600 | params.Set("motion", 0); |
| 578 | params.Set("port", port); | 601 | params.Set("port", port); |
| 579 | params.Set("guid", std::move(guid)); | 602 | params.Set("guid", guid.RawString()); |
| 580 | return params; | 603 | return params; |
| 581 | } | 604 | } |
| 582 | 605 | ||
| 583 | Common::ParamPackage SDLDriver::BuildParamPackageForBinding( | 606 | Common::ParamPackage SDLDriver::BuildParamPackageForBinding( |
| 584 | int port, const std::string& guid, const SDL_GameControllerButtonBind& binding) const { | 607 | int port, const Common::UUID& guid, const SDL_GameControllerButtonBind& binding) const { |
| 585 | switch (binding.bindType) { | 608 | switch (binding.bindType) { |
| 586 | case SDL_CONTROLLER_BINDTYPE_NONE: | 609 | case SDL_CONTROLLER_BINDTYPE_NONE: |
| 587 | break; | 610 | break; |