diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/input_common/drivers/sdl_driver.cpp | 56 | ||||
| -rw-r--r-- | src/input_common/drivers/sdl_driver.h | 15 |
2 files changed, 38 insertions, 33 deletions
diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cpp index 1a14ef10b..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 |
| @@ -470,7 +474,7 @@ std::vector<Common::ParamPackage> SDLDriver::GetInputDevices() const { | |||
| 470 | devices.emplace_back(Common::ParamPackage{ | 474 | devices.emplace_back(Common::ParamPackage{ |
| 471 | {"engine", GetEngineName()}, | 475 | {"engine", GetEngineName()}, |
| 472 | {"display", std::move(name)}, | 476 | {"display", std::move(name)}, |
| 473 | {"guid", joystick->GetGUID()}, | 477 | {"guid", joystick->GetGUID().RawString()}, |
| 474 | {"port", std::to_string(joystick->GetPort())}, | 478 | {"port", std::to_string(joystick->GetPort())}, |
| 475 | }); | 479 | }); |
| 476 | if (joystick->IsJoyconLeft()) { | 480 | if (joystick->IsJoyconLeft()) { |
| @@ -493,8 +497,8 @@ std::vector<Common::ParamPackage> SDLDriver::GetInputDevices() const { | |||
| 493 | devices.emplace_back(Common::ParamPackage{ | 497 | devices.emplace_back(Common::ParamPackage{ |
| 494 | {"engine", GetEngineName()}, | 498 | {"engine", GetEngineName()}, |
| 495 | {"display", std::move(name)}, | 499 | {"display", std::move(name)}, |
| 496 | {"guid", joystick->GetGUID()}, | 500 | {"guid", joystick->GetGUID().RawString()}, |
| 497 | {"guid2", joystick2->GetGUID()}, | 501 | {"guid2", joystick2->GetGUID().RawString()}, |
| 498 | {"port", std::to_string(joystick->GetPort())}, | 502 | {"port", std::to_string(joystick->GetPort())}, |
| 499 | }); | 503 | }); |
| 500 | } | 504 | } |
| @@ -557,50 +561,50 @@ void SDLDriver::SendVibrations() { | |||
| 557 | } | 561 | } |
| 558 | } | 562 | } |
| 559 | 563 | ||
| 560 | Common::ParamPackage SDLDriver::BuildAnalogParamPackageForButton(int port, std::string guid, | 564 | Common::ParamPackage SDLDriver::BuildAnalogParamPackageForButton(int port, const Common::UUID& guid, |
| 561 | s32 axis, float value) const { | 565 | s32 axis, float value) const { |
| 562 | Common::ParamPackage params{}; | 566 | Common::ParamPackage params{}; |
| 563 | params.Set("engine", GetEngineName()); | 567 | params.Set("engine", GetEngineName()); |
| 564 | params.Set("port", port); | 568 | params.Set("port", port); |
| 565 | params.Set("guid", std::move(guid)); | 569 | params.Set("guid", guid.RawString()); |
| 566 | params.Set("axis", axis); | 570 | params.Set("axis", axis); |
| 567 | params.Set("threshold", "0.5"); | 571 | params.Set("threshold", "0.5"); |
| 568 | params.Set("invert", value < 0 ? "-" : "+"); | 572 | params.Set("invert", value < 0 ? "-" : "+"); |
| 569 | return params; | 573 | return params; |
| 570 | } | 574 | } |
| 571 | 575 | ||
| 572 | Common::ParamPackage SDLDriver::BuildButtonParamPackageForButton(int port, std::string guid, | 576 | Common::ParamPackage SDLDriver::BuildButtonParamPackageForButton(int port, const Common::UUID& guid, |
| 573 | s32 button) const { | 577 | s32 button) const { |
| 574 | Common::ParamPackage params{}; | 578 | Common::ParamPackage params{}; |
| 575 | params.Set("engine", GetEngineName()); | 579 | params.Set("engine", GetEngineName()); |
| 576 | params.Set("port", port); | 580 | params.Set("port", port); |
| 577 | params.Set("guid", std::move(guid)); | 581 | params.Set("guid", guid.RawString()); |
| 578 | params.Set("button", button); | 582 | params.Set("button", button); |
| 579 | return params; | 583 | return params; |
| 580 | } | 584 | } |
| 581 | 585 | ||
| 582 | Common::ParamPackage SDLDriver::BuildHatParamPackageForButton(int port, std::string guid, s32 hat, | 586 | Common::ParamPackage SDLDriver::BuildHatParamPackageForButton(int port, const Common::UUID& guid, |
| 583 | u8 value) const { | 587 | s32 hat, u8 value) const { |
| 584 | Common::ParamPackage params{}; | 588 | Common::ParamPackage params{}; |
| 585 | params.Set("engine", GetEngineName()); | 589 | params.Set("engine", GetEngineName()); |
| 586 | params.Set("port", port); | 590 | params.Set("port", port); |
| 587 | params.Set("guid", std::move(guid)); | 591 | params.Set("guid", guid.RawString()); |
| 588 | params.Set("hat", hat); | 592 | params.Set("hat", hat); |
| 589 | params.Set("direction", GetHatButtonName(value)); | 593 | params.Set("direction", GetHatButtonName(value)); |
| 590 | return params; | 594 | return params; |
| 591 | } | 595 | } |
| 592 | 596 | ||
| 593 | Common::ParamPackage SDLDriver::BuildMotionParam(int port, std::string guid) const { | 597 | Common::ParamPackage SDLDriver::BuildMotionParam(int port, const Common::UUID& guid) const { |
| 594 | Common::ParamPackage params{}; | 598 | Common::ParamPackage params{}; |
| 595 | params.Set("engine", GetEngineName()); | 599 | params.Set("engine", GetEngineName()); |
| 596 | params.Set("motion", 0); | 600 | params.Set("motion", 0); |
| 597 | params.Set("port", port); | 601 | params.Set("port", port); |
| 598 | params.Set("guid", std::move(guid)); | 602 | params.Set("guid", guid.RawString()); |
| 599 | return params; | 603 | return params; |
| 600 | } | 604 | } |
| 601 | 605 | ||
| 602 | Common::ParamPackage SDLDriver::BuildParamPackageForBinding( | 606 | Common::ParamPackage SDLDriver::BuildParamPackageForBinding( |
| 603 | int port, const std::string& guid, const SDL_GameControllerButtonBind& binding) const { | 607 | int port, const Common::UUID& guid, const SDL_GameControllerButtonBind& binding) const { |
| 604 | switch (binding.bindType) { | 608 | switch (binding.bindType) { |
| 605 | case SDL_CONTROLLER_BINDTYPE_NONE: | 609 | case SDL_CONTROLLER_BINDTYPE_NONE: |
| 606 | break; | 610 | break; |
diff --git a/src/input_common/drivers/sdl_driver.h b/src/input_common/drivers/sdl_driver.h index c82632506..0846fbb50 100644 --- a/src/input_common/drivers/sdl_driver.h +++ b/src/input_common/drivers/sdl_driver.h | |||
| @@ -47,6 +47,7 @@ public: | |||
| 47 | * Check how many identical joysticks (by guid) were connected before the one with sdl_id and so | 47 | * Check how many identical joysticks (by guid) were connected before the one with sdl_id and so |
| 48 | * tie it to a SDLJoystick with the same guid and that port | 48 | * tie it to a SDLJoystick with the same guid and that port |
| 49 | */ | 49 | */ |
| 50 | std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const Common::UUID& guid, int port); | ||
| 50 | std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const std::string& guid, int port); | 51 | std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const std::string& guid, int port); |
| 51 | 52 | ||
| 52 | std::vector<Common::ParamPackage> GetInputDevices() const override; | 53 | std::vector<Common::ParamPackage> GetInputDevices() const override; |
| @@ -79,18 +80,18 @@ private: | |||
| 79 | /// Takes all vibrations from the queue and sends the command to the controller | 80 | /// Takes all vibrations from the queue and sends the command to the controller |
| 80 | void SendVibrations(); | 81 | void SendVibrations(); |
| 81 | 82 | ||
| 82 | Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, s32 axis, | 83 | Common::ParamPackage BuildAnalogParamPackageForButton(int port, const Common::UUID& guid, |
| 83 | float value = 0.1f) const; | 84 | s32 axis, float value = 0.1f) const; |
| 84 | Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, | 85 | Common::ParamPackage BuildButtonParamPackageForButton(int port, const Common::UUID& guid, |
| 85 | s32 button) const; | 86 | s32 button) const; |
| 86 | 87 | ||
| 87 | Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, s32 hat, | 88 | Common::ParamPackage BuildHatParamPackageForButton(int port, const Common::UUID& guid, s32 hat, |
| 88 | u8 value) const; | 89 | u8 value) const; |
| 89 | 90 | ||
| 90 | Common::ParamPackage BuildMotionParam(int port, std::string guid) const; | 91 | Common::ParamPackage BuildMotionParam(int port, const Common::UUID& guid) const; |
| 91 | 92 | ||
| 92 | Common::ParamPackage BuildParamPackageForBinding( | 93 | Common::ParamPackage BuildParamPackageForBinding( |
| 93 | int port, const std::string& guid, const SDL_GameControllerButtonBind& binding) const; | 94 | int port, const Common::UUID& guid, const SDL_GameControllerButtonBind& binding) const; |
| 94 | 95 | ||
| 95 | Common::ParamPackage BuildParamPackageForAnalog(PadIdentifier identifier, int axis_x, | 96 | Common::ParamPackage BuildParamPackageForAnalog(PadIdentifier identifier, int axis_x, |
| 96 | int axis_y, float offset_x, | 97 | int axis_y, float offset_x, |
| @@ -120,7 +121,7 @@ private: | |||
| 120 | Common::SPSCQueue<VibrationRequest> vibration_queue; | 121 | Common::SPSCQueue<VibrationRequest> vibration_queue; |
| 121 | 122 | ||
| 122 | /// Map of GUID of a list of corresponding virtual Joysticks | 123 | /// Map of GUID of a list of corresponding virtual Joysticks |
| 123 | std::unordered_map<std::string, std::vector<std::shared_ptr<SDLJoystick>>> joystick_map; | 124 | std::unordered_map<Common::UUID, std::vector<std::shared_ptr<SDLJoystick>>> joystick_map; |
| 124 | std::mutex joystick_map_mutex; | 125 | std::mutex joystick_map_mutex; |
| 125 | 126 | ||
| 126 | bool start_thread = false; | 127 | bool start_thread = false; |