diff options
Diffstat (limited to 'src/input_common')
| -rw-r--r-- | src/input_common/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/input_common/drivers/sdl_driver.cpp | 79 | ||||
| -rw-r--r-- | src/input_common/drivers/sdl_driver.h | 27 | ||||
| -rw-r--r-- | src/input_common/drivers/touch_screen.cpp | 89 | ||||
| -rw-r--r-- | src/input_common/drivers/touch_screen.h | 52 |
5 files changed, 181 insertions, 67 deletions
diff --git a/src/input_common/CMakeLists.txt b/src/input_common/CMakeLists.txt index d4fa69a77..48e799cf5 100644 --- a/src/input_common/CMakeLists.txt +++ b/src/input_common/CMakeLists.txt | |||
| @@ -44,7 +44,6 @@ else() | |||
| 44 | -Werror | 44 | -Werror |
| 45 | -Werror=conversion | 45 | -Werror=conversion |
| 46 | -Werror=ignored-qualifiers | 46 | -Werror=ignored-qualifiers |
| 47 | -Werror=shadow | ||
| 48 | $<$<CXX_COMPILER_ID:GNU>:-Werror=unused-but-set-parameter> | 47 | $<$<CXX_COMPILER_ID:GNU>:-Werror=unused-but-set-parameter> |
| 49 | $<$<CXX_COMPILER_ID:GNU>:-Werror=unused-but-set-variable> | 48 | $<$<CXX_COMPILER_ID:GNU>:-Werror=unused-but-set-variable> |
| 50 | -Werror=unused-variable | 49 | -Werror=unused-variable |
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; |
diff --git a/src/input_common/drivers/sdl_driver.h b/src/input_common/drivers/sdl_driver.h index dcd0d1e64..0846fbb50 100644 --- a/src/input_common/drivers/sdl_driver.h +++ b/src/input_common/drivers/sdl_driver.h | |||
| @@ -12,6 +12,7 @@ | |||
| 12 | #include <SDL.h> | 12 | #include <SDL.h> |
| 13 | 13 | ||
| 14 | #include "common/common_types.h" | 14 | #include "common/common_types.h" |
| 15 | #include "common/threadsafe_queue.h" | ||
| 15 | #include "input_common/input_engine.h" | 16 | #include "input_common/input_engine.h" |
| 16 | 17 | ||
| 17 | union SDL_Event; | 18 | union SDL_Event; |
| @@ -46,6 +47,7 @@ public: | |||
| 46 | * 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 |
| 47 | * 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 |
| 48 | */ | 49 | */ |
| 50 | std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const Common::UUID& guid, int port); | ||
| 49 | std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const std::string& guid, int port); | 51 | std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const std::string& guid, int port); |
| 50 | 52 | ||
| 51 | std::vector<Common::ParamPackage> GetInputDevices() const override; | 53 | std::vector<Common::ParamPackage> GetInputDevices() const override; |
| @@ -64,24 +66,32 @@ public: | |||
| 64 | const PadIdentifier& identifier, const Common::Input::VibrationStatus& vibration) override; | 66 | const PadIdentifier& identifier, const Common::Input::VibrationStatus& vibration) override; |
| 65 | 67 | ||
| 66 | private: | 68 | private: |
| 69 | struct VibrationRequest { | ||
| 70 | PadIdentifier identifier; | ||
| 71 | Common::Input::VibrationStatus vibration; | ||
| 72 | }; | ||
| 73 | |||
| 67 | void InitJoystick(int joystick_index); | 74 | void InitJoystick(int joystick_index); |
| 68 | void CloseJoystick(SDL_Joystick* sdl_joystick); | 75 | void CloseJoystick(SDL_Joystick* sdl_joystick); |
| 69 | 76 | ||
| 70 | /// Needs to be called before SDL_QuitSubSystem. | 77 | /// Needs to be called before SDL_QuitSubSystem. |
| 71 | void CloseJoysticks(); | 78 | void CloseJoysticks(); |
| 72 | 79 | ||
| 73 | Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, s32 axis, | 80 | /// Takes all vibrations from the queue and sends the command to the controller |
| 74 | float value = 0.1f) const; | 81 | void SendVibrations(); |
| 75 | Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, | 82 | |
| 83 | Common::ParamPackage BuildAnalogParamPackageForButton(int port, const Common::UUID& guid, | ||
| 84 | s32 axis, float value = 0.1f) const; | ||
| 85 | Common::ParamPackage BuildButtonParamPackageForButton(int port, const Common::UUID& guid, | ||
| 76 | s32 button) const; | 86 | s32 button) const; |
| 77 | 87 | ||
| 78 | Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, s32 hat, | 88 | Common::ParamPackage BuildHatParamPackageForButton(int port, const Common::UUID& guid, s32 hat, |
| 79 | u8 value) const; | 89 | u8 value) const; |
| 80 | 90 | ||
| 81 | Common::ParamPackage BuildMotionParam(int port, std::string guid) const; | 91 | Common::ParamPackage BuildMotionParam(int port, const Common::UUID& guid) const; |
| 82 | 92 | ||
| 83 | Common::ParamPackage BuildParamPackageForBinding( | 93 | Common::ParamPackage BuildParamPackageForBinding( |
| 84 | int port, const std::string& guid, const SDL_GameControllerButtonBind& binding) const; | 94 | int port, const Common::UUID& guid, const SDL_GameControllerButtonBind& binding) const; |
| 85 | 95 | ||
| 86 | Common::ParamPackage BuildParamPackageForAnalog(PadIdentifier identifier, int axis_x, | 96 | Common::ParamPackage BuildParamPackageForAnalog(PadIdentifier identifier, int axis_x, |
| 87 | int axis_y, float offset_x, | 97 | int axis_y, float offset_x, |
| @@ -107,8 +117,11 @@ private: | |||
| 107 | /// Returns true if the button is on the left joycon | 117 | /// Returns true if the button is on the left joycon |
| 108 | bool IsButtonOnLeftSide(Settings::NativeButton::Values button) const; | 118 | bool IsButtonOnLeftSide(Settings::NativeButton::Values button) const; |
| 109 | 119 | ||
| 120 | /// Queue of vibration request to controllers | ||
| 121 | Common::SPSCQueue<VibrationRequest> vibration_queue; | ||
| 122 | |||
| 110 | /// Map of GUID of a list of corresponding virtual Joysticks | 123 | /// Map of GUID of a list of corresponding virtual Joysticks |
| 111 | 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; |
| 112 | std::mutex joystick_map_mutex; | 125 | std::mutex joystick_map_mutex; |
| 113 | 126 | ||
| 114 | bool start_thread = false; | 127 | bool start_thread = false; |
diff --git a/src/input_common/drivers/touch_screen.cpp b/src/input_common/drivers/touch_screen.cpp index 8acbe4584..1753e0893 100644 --- a/src/input_common/drivers/touch_screen.cpp +++ b/src/input_common/drivers/touch_screen.cpp | |||
| @@ -14,38 +14,93 @@ constexpr PadIdentifier identifier = { | |||
| 14 | 14 | ||
| 15 | TouchScreen::TouchScreen(std::string input_engine_) : InputEngine(std::move(input_engine_)) { | 15 | TouchScreen::TouchScreen(std::string input_engine_) : InputEngine(std::move(input_engine_)) { |
| 16 | PreSetController(identifier); | 16 | PreSetController(identifier); |
| 17 | ReleaseAllTouch(); | ||
| 17 | } | 18 | } |
| 18 | 19 | ||
| 19 | void TouchScreen::TouchMoved(float x, float y, std::size_t finger) { | 20 | void TouchScreen::TouchMoved(float x, float y, std::size_t finger_id) { |
| 20 | if (finger >= 16) { | 21 | const auto index = GetIndexFromFingerId(finger_id); |
| 22 | if (!index) { | ||
| 23 | // Touch doesn't exist handle it as a new one | ||
| 24 | TouchPressed(x, y, finger_id); | ||
| 21 | return; | 25 | return; |
| 22 | } | 26 | } |
| 23 | TouchPressed(x, y, finger); | 27 | const auto i = index.value(); |
| 28 | fingers[i].is_active = true; | ||
| 29 | SetButton(identifier, static_cast<int>(i), true); | ||
| 30 | SetAxis(identifier, static_cast<int>(i * 2), x); | ||
| 31 | SetAxis(identifier, static_cast<int>(i * 2 + 1), y); | ||
| 24 | } | 32 | } |
| 25 | 33 | ||
| 26 | void TouchScreen::TouchPressed(float x, float y, std::size_t finger) { | 34 | void TouchScreen::TouchPressed(float x, float y, std::size_t finger_id) { |
| 27 | if (finger >= 16) { | 35 | if (GetIndexFromFingerId(finger_id)) { |
| 36 | // Touch already exist. Just update the data | ||
| 37 | TouchMoved(x, y, finger_id); | ||
| 28 | return; | 38 | return; |
| 29 | } | 39 | } |
| 30 | SetButton(identifier, static_cast<int>(finger), true); | 40 | const auto index = GetNextFreeIndex(); |
| 31 | SetAxis(identifier, static_cast<int>(finger * 2), x); | 41 | if (!index) { |
| 32 | SetAxis(identifier, static_cast<int>(finger * 2 + 1), y); | 42 | // No free entries. Ignore input |
| 43 | return; | ||
| 44 | } | ||
| 45 | const auto i = index.value(); | ||
| 46 | fingers[i].is_enabled = true; | ||
| 47 | fingers[i].finger_id = finger_id; | ||
| 48 | TouchMoved(x, y, finger_id); | ||
| 33 | } | 49 | } |
| 34 | 50 | ||
| 35 | void TouchScreen::TouchReleased(std::size_t finger) { | 51 | void TouchScreen::TouchReleased(std::size_t finger_id) { |
| 36 | if (finger >= 16) { | 52 | const auto index = GetIndexFromFingerId(finger_id); |
| 53 | if (!index) { | ||
| 37 | return; | 54 | return; |
| 38 | } | 55 | } |
| 39 | SetButton(identifier, static_cast<int>(finger), false); | 56 | const auto i = index.value(); |
| 40 | SetAxis(identifier, static_cast<int>(finger * 2), 0.0f); | 57 | fingers[i].is_enabled = false; |
| 41 | SetAxis(identifier, static_cast<int>(finger * 2 + 1), 0.0f); | 58 | SetButton(identifier, static_cast<int>(i), false); |
| 59 | SetAxis(identifier, static_cast<int>(i * 2), 0.0f); | ||
| 60 | SetAxis(identifier, static_cast<int>(i * 2 + 1), 0.0f); | ||
| 61 | } | ||
| 62 | |||
| 63 | std::optional<std::size_t> TouchScreen::GetIndexFromFingerId(std::size_t finger_id) const { | ||
| 64 | for (std::size_t index = 0; index < MAX_FINGER_COUNT; ++index) { | ||
| 65 | const auto& finger = fingers[index]; | ||
| 66 | if (!finger.is_enabled) { | ||
| 67 | continue; | ||
| 68 | } | ||
| 69 | if (finger.finger_id == finger_id) { | ||
| 70 | return index; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | return std::nullopt; | ||
| 74 | } | ||
| 75 | |||
| 76 | std::optional<std::size_t> TouchScreen::GetNextFreeIndex() const { | ||
| 77 | for (std::size_t index = 0; index < MAX_FINGER_COUNT; ++index) { | ||
| 78 | if (!fingers[index].is_enabled) { | ||
| 79 | return index; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | return std::nullopt; | ||
| 83 | } | ||
| 84 | |||
| 85 | void TouchScreen::ClearActiveFlag() { | ||
| 86 | for (auto& finger : fingers) { | ||
| 87 | finger.is_active = false; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | void TouchScreen::ReleaseInactiveTouch() { | ||
| 92 | for (const auto& finger : fingers) { | ||
| 93 | if (!finger.is_active) { | ||
| 94 | TouchReleased(finger.finger_id); | ||
| 95 | } | ||
| 96 | } | ||
| 42 | } | 97 | } |
| 43 | 98 | ||
| 44 | void TouchScreen::ReleaseAllTouch() { | 99 | void TouchScreen::ReleaseAllTouch() { |
| 45 | for (int index = 0; index < 16; ++index) { | 100 | for (const auto& finger : fingers) { |
| 46 | SetButton(identifier, index, false); | 101 | if (finger.is_enabled) { |
| 47 | SetAxis(identifier, index * 2, 0.0f); | 102 | TouchReleased(finger.finger_id); |
| 48 | SetAxis(identifier, index * 2 + 1, 0.0f); | 103 | } |
| 49 | } | 104 | } |
| 50 | } | 105 | } |
| 51 | 106 | ||
diff --git a/src/input_common/drivers/touch_screen.h b/src/input_common/drivers/touch_screen.h index 193478ead..f46036ffd 100644 --- a/src/input_common/drivers/touch_screen.h +++ b/src/input_common/drivers/touch_screen.h | |||
| @@ -3,41 +3,65 @@ | |||
| 3 | 3 | ||
| 4 | #pragma once | 4 | #pragma once |
| 5 | 5 | ||
| 6 | #include <optional> | ||
| 7 | |||
| 6 | #include "input_common/input_engine.h" | 8 | #include "input_common/input_engine.h" |
| 7 | 9 | ||
| 8 | namespace InputCommon { | 10 | namespace InputCommon { |
| 9 | 11 | ||
| 10 | /** | 12 | /** |
| 11 | * A button device factory representing a keyboard. It receives keyboard events and forward them | 13 | * A touch device factory representing a touch screen. It receives touch events and forward them |
| 12 | * to all button devices it created. | 14 | * to all touch devices it created. |
| 13 | */ | 15 | */ |
| 14 | class TouchScreen final : public InputEngine { | 16 | class TouchScreen final : public InputEngine { |
| 15 | public: | 17 | public: |
| 16 | explicit TouchScreen(std::string input_engine_); | 18 | explicit TouchScreen(std::string input_engine_); |
| 17 | 19 | ||
| 18 | /** | 20 | /** |
| 19 | * Signals that mouse has moved. | 21 | * Signals that touch has moved and marks this touch point as active |
| 20 | * @param x the x-coordinate of the cursor | 22 | * @param x new horizontal position |
| 21 | * @param y the y-coordinate of the cursor | 23 | * @param y new vertical position |
| 22 | * @param center_x the x-coordinate of the middle of the screen | 24 | * @param finger_id of the touch point to be updated |
| 23 | * @param center_y the y-coordinate of the middle of the screen | ||
| 24 | */ | 25 | */ |
| 25 | void TouchMoved(float x, float y, std::size_t finger); | 26 | void TouchMoved(float x, float y, std::size_t finger_id); |
| 26 | 27 | ||
| 27 | /** | 28 | /** |
| 28 | * Sets the status of all buttons bound with the key to pressed | 29 | * Signals and creates a new touch point with this finger id |
| 29 | * @param key_code the code of the key to press | 30 | * @param x starting horizontal position |
| 31 | * @param y starting vertical position | ||
| 32 | * @param finger_id to be assigned to the new touch point | ||
| 30 | */ | 33 | */ |
| 31 | void TouchPressed(float x, float y, std::size_t finger); | 34 | void TouchPressed(float x, float y, std::size_t finger_id); |
| 32 | 35 | ||
| 33 | /** | 36 | /** |
| 34 | * Sets the status of all buttons bound with the key to released | 37 | * Signals and resets the touch point related to the this finger id |
| 35 | * @param key_code the code of the key to release | 38 | * @param finger_id to be released |
| 36 | */ | 39 | */ |
| 37 | void TouchReleased(std::size_t finger); | 40 | void TouchReleased(std::size_t finger_id); |
| 41 | |||
| 42 | /// Resets the active flag for each touch point | ||
| 43 | void ClearActiveFlag(); | ||
| 44 | |||
| 45 | /// Releases all touch that haven't been marked as active | ||
| 46 | void ReleaseInactiveTouch(); | ||
| 38 | 47 | ||
| 39 | /// Resets all inputs to their initial value | 48 | /// Resets all inputs to their initial value |
| 40 | void ReleaseAllTouch(); | 49 | void ReleaseAllTouch(); |
| 50 | |||
| 51 | private: | ||
| 52 | static constexpr std::size_t MAX_FINGER_COUNT = 16; | ||
| 53 | |||
| 54 | struct TouchStatus { | ||
| 55 | std::size_t finger_id{}; | ||
| 56 | bool is_enabled{}; | ||
| 57 | bool is_active{}; | ||
| 58 | }; | ||
| 59 | |||
| 60 | std::optional<std::size_t> GetIndexFromFingerId(std::size_t finger_id) const; | ||
| 61 | |||
| 62 | std::optional<std::size_t> GetNextFreeIndex() const; | ||
| 63 | |||
| 64 | std::array<TouchStatus, MAX_FINGER_COUNT> fingers{}; | ||
| 41 | }; | 65 | }; |
| 42 | 66 | ||
| 43 | } // namespace InputCommon | 67 | } // namespace InputCommon |