From 2680526e6ba1240363297c9ec34dbde807229d72 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 28 Aug 2020 20:58:29 -0400 Subject: [PATCH 1/6] sdl_impl: Mark FromEvent() as a const member function This doesn't modify internal member state, so it can be marked as const. --- src/input_common/sdl/sdl_impl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index c8d9eb2bca..43fac76500 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp @@ -784,7 +784,7 @@ public: } return {}; } - std::optional FromEvent(const SDL_Event& event) { + [[nodiscard]] std::optional FromEvent(const SDL_Event& event) const { switch (event.type) { case SDL_JOYAXISMOTION: if (std::abs(event.jaxis.value / 32767.0) < 0.5) { @@ -795,7 +795,7 @@ public: case SDL_JOYHATMOTION: return {SDLEventToButtonParamPackage(state, event)}; } - return {}; + return std::nullopt; } }; From 2e2dde2f956cecfee8413b8ae43f31f13ac8e681 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 28 Aug 2020 21:04:14 -0400 Subject: [PATCH 2/6] sdl_impl: Simplify make_tuple call The purpose of make_tuple is that you don't need to explicitly type out the types of the things that comprise said tuple. Given this just returns default values, we can simplify this a bit. --- src/input_common/sdl/sdl_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index 43fac76500..cb2cdf96f9 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp @@ -358,7 +358,7 @@ public: return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), y / r * (r - deadzone) / (1 - deadzone)); } - return std::make_tuple(0.0f, 0.0f); + return {}; } bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { From f3ac0883451ba4359a155c0d10992d675a63d9c9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 28 Aug 2020 21:08:06 -0400 Subject: [PATCH 3/6] sdl_impl: Prevent type truncation in BuildAnalogParamPackageForButton() default arguments We need to add the 'f' suffix to make the right hand side a float and not a double. --- src/input_common/sdl/sdl_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index cb2cdf96f9..9d3d1803c8 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp @@ -574,7 +574,7 @@ std::vector SDLState::GetInputDevices() { namespace { Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, u8 axis, - float value = 0.1) { + float value = 0.1f) { Common::ParamPackage params({{"engine", "sdl"}}); params.Set("port", port); params.Set("guid", guid); From e92164e6a06361382b8072228530f69a6b10189f Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 28 Aug 2020 21:13:23 -0400 Subject: [PATCH 4/6] sdl_impl: Make use of insert_or_assign() where applicable Avoids churning ParamPackage instances. --- src/input_common/sdl/sdl_impl.cpp | 32 +++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index 9d3d1803c8..85fcd457cb 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp @@ -697,16 +697,17 @@ ButtonMapping SDLState::GetButtonMappingForDevice(const Common::ParamPackage& pa return {}; } const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0)); - auto controller = joystick->GetSDLGameController(); - if (!controller) { + auto* controller = joystick->GetSDLGameController(); + if (controller == nullptr) { return {}; } ButtonMapping mapping{}; for (const auto& [switch_button, sdl_button] : switch_to_sdl_button) { const auto& binding = SDL_GameControllerGetBindForButton(controller, sdl_button); - mapping[switch_button] = - BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding); + mapping.insert_or_assign( + switch_button, + BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); } // Add the missing bindings for ZL/ZR @@ -717,8 +718,9 @@ ButtonMapping SDLState::GetButtonMappingForDevice(const Common::ParamPackage& pa }; for (const auto& [switch_button, sdl_axis] : switch_to_sdl_axis) { const auto& binding = SDL_GameControllerGetBindForAxis(controller, sdl_axis); - mapping[switch_button] = - BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding); + mapping.insert_or_assign( + switch_button, + BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); } return mapping; @@ -729,8 +731,8 @@ AnalogMapping SDLState::GetAnalogMappingForDevice(const Common::ParamPackage& pa return {}; } const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0)); - auto controller = joystick->GetSDLGameController(); - if (!controller) { + auto* controller = joystick->GetSDLGameController(); + if (controller == nullptr) { return {}; } @@ -739,16 +741,18 @@ AnalogMapping SDLState::GetAnalogMappingForDevice(const Common::ParamPackage& pa SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX); const auto& binding_left_y = SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTY); - mapping[Settings::NativeAnalog::LStick] = - BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), - binding_left_x.value.axis, binding_left_y.value.axis); + mapping.insert_or_assign(Settings::NativeAnalog::LStick, + BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), + binding_left_x.value.axis, + binding_left_y.value.axis)); const auto& binding_right_x = SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX); const auto& binding_right_y = SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY); - mapping[Settings::NativeAnalog::RStick] = - BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), - binding_right_x.value.axis, binding_right_y.value.axis); + mapping.insert_or_assign(Settings::NativeAnalog::RStick, + BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), + binding_right_x.value.axis, + binding_right_y.value.axis)); return mapping; } From f2a680ca893f29fc35135986ad56b961205d610e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 28 Aug 2020 21:14:51 -0400 Subject: [PATCH 5/6] sdl_impl: Make use of std::move on std::string where applicable Avoids redundant copies. --- src/input_common/sdl/sdl_impl.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index 85fcd457cb..8d83184d2d 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp @@ -577,7 +577,7 @@ Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid float value = 0.1f) { Common::ParamPackage params({{"engine", "sdl"}}); params.Set("port", port); - params.Set("guid", guid); + params.Set("guid", std::move(guid)); params.Set("axis", axis); if (value > 0) { params.Set("direction", "+"); @@ -592,7 +592,7 @@ Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, u8 button) { Common::ParamPackage params({{"engine", "sdl"}}); params.Set("port", port); - params.Set("guid", guid); + params.Set("guid", std::move(guid)); params.Set("button", button); return params; } @@ -601,7 +601,7 @@ Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, u Common::ParamPackage params({{"engine", "sdl"}}); params.Set("port", port); - params.Set("guid", guid); + params.Set("guid", std::move(guid)); params.Set("hat", hat); switch (value) { case SDL_HAT_UP: From 69fa6b4906ea3ca36a6ca7dc92de0683332dcf52 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 28 Aug 2020 21:21:14 -0400 Subject: [PATCH 6/6] sdl_impl: Reduce allocations in GetButtonMappingForDevice() These maps can be constexpr arrays of std::pair. --- src/input_common/sdl/sdl_impl.cpp | 68 +++++++++++++++++-------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index 8d83184d2d..a9e676f4b2 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include +#include #include #include #include @@ -670,29 +671,6 @@ Common::ParamPackage BuildParamPackageForAnalog(int port, const std::string& gui } // Anonymous namespace ButtonMapping SDLState::GetButtonMappingForDevice(const Common::ParamPackage& params) { - // This list is missing ZL/ZR since those are not considered buttons in SDL GameController. - // We will add those afterwards - // This list also excludes Screenshot since theres not really a mapping for that - std::unordered_map - switch_to_sdl_button = { - {Settings::NativeButton::A, SDL_CONTROLLER_BUTTON_B}, - {Settings::NativeButton::B, SDL_CONTROLLER_BUTTON_A}, - {Settings::NativeButton::X, SDL_CONTROLLER_BUTTON_Y}, - {Settings::NativeButton::Y, SDL_CONTROLLER_BUTTON_X}, - {Settings::NativeButton::LStick, SDL_CONTROLLER_BUTTON_LEFTSTICK}, - {Settings::NativeButton::RStick, SDL_CONTROLLER_BUTTON_RIGHTSTICK}, - {Settings::NativeButton::L, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, - {Settings::NativeButton::R, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, - {Settings::NativeButton::Plus, SDL_CONTROLLER_BUTTON_START}, - {Settings::NativeButton::Minus, SDL_CONTROLLER_BUTTON_BACK}, - {Settings::NativeButton::DLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT}, - {Settings::NativeButton::DUp, SDL_CONTROLLER_BUTTON_DPAD_UP}, - {Settings::NativeButton::DRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT}, - {Settings::NativeButton::DDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN}, - {Settings::NativeButton::SL, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, - {Settings::NativeButton::SR, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, - {Settings::NativeButton::Home, SDL_CONTROLLER_BUTTON_GUIDE}, - }; if (!params.Has("guid") || !params.Has("port")) { return {}; } @@ -702,20 +680,48 @@ ButtonMapping SDLState::GetButtonMappingForDevice(const Common::ParamPackage& pa return {}; } - ButtonMapping mapping{}; + // This list is missing ZL/ZR since those are not considered buttons in SDL GameController. + // We will add those afterwards + // This list also excludes Screenshot since theres not really a mapping for that + using ButtonBindings = + std::array, 17>; + static constexpr ButtonBindings switch_to_sdl_button{{ + {Settings::NativeButton::A, SDL_CONTROLLER_BUTTON_B}, + {Settings::NativeButton::B, SDL_CONTROLLER_BUTTON_A}, + {Settings::NativeButton::X, SDL_CONTROLLER_BUTTON_Y}, + {Settings::NativeButton::Y, SDL_CONTROLLER_BUTTON_X}, + {Settings::NativeButton::LStick, SDL_CONTROLLER_BUTTON_LEFTSTICK}, + {Settings::NativeButton::RStick, SDL_CONTROLLER_BUTTON_RIGHTSTICK}, + {Settings::NativeButton::L, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, + {Settings::NativeButton::R, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, + {Settings::NativeButton::Plus, SDL_CONTROLLER_BUTTON_START}, + {Settings::NativeButton::Minus, SDL_CONTROLLER_BUTTON_BACK}, + {Settings::NativeButton::DLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT}, + {Settings::NativeButton::DUp, SDL_CONTROLLER_BUTTON_DPAD_UP}, + {Settings::NativeButton::DRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT}, + {Settings::NativeButton::DDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN}, + {Settings::NativeButton::SL, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}, + {Settings::NativeButton::SR, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}, + {Settings::NativeButton::Home, SDL_CONTROLLER_BUTTON_GUIDE}, + }}; + + // Add the missing bindings for ZL/ZR + using ZBindings = + std::array, 2>; + static constexpr ZBindings switch_to_sdl_axis{{ + {Settings::NativeButton::ZL, SDL_CONTROLLER_AXIS_TRIGGERLEFT}, + {Settings::NativeButton::ZR, SDL_CONTROLLER_AXIS_TRIGGERRIGHT}, + }}; + + ButtonMapping mapping; + mapping.reserve(switch_to_sdl_button.size() + switch_to_sdl_axis.size()); + for (const auto& [switch_button, sdl_button] : switch_to_sdl_button) { const auto& binding = SDL_GameControllerGetBindForButton(controller, sdl_button); mapping.insert_or_assign( switch_button, BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding)); } - - // Add the missing bindings for ZL/ZR - std::unordered_map switch_to_sdl_axis = - { - {Settings::NativeButton::ZL, SDL_CONTROLLER_AXIS_TRIGGERLEFT}, - {Settings::NativeButton::ZR, SDL_CONTROLLER_AXIS_TRIGGERRIGHT}, - }; for (const auto& [switch_button, sdl_axis] : switch_to_sdl_axis) { const auto& binding = SDL_GameControllerGetBindForAxis(controller, sdl_axis); mapping.insert_or_assign(