From 3c618a330630ccde8233b0bb7d0bc21059829fa8 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 08:52:06 -0500 Subject: [PATCH 01/13] input_engine: Remove unnecessary return This is a void function, so it doesn't need this. --- src/input_common/input_engine.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h index 02272b3f87..fb89f9257c 100644 --- a/src/input_common/input_engine.h +++ b/src/input_common/input_engine.h @@ -116,9 +116,7 @@ public: // Sets a led pattern for a controller virtual void SetLeds([[maybe_unused]] const PadIdentifier& identifier, - [[maybe_unused]] const Common::Input::LedStatus led_status) { - return; - } + [[maybe_unused]] const Common::Input::LedStatus led_status) {} // Sets rumble to a controller virtual Common::Input::VibrationError SetRumble( From 479369db43092089b7a8ace65d4eadd78cb56b00 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 08:52:59 -0500 Subject: [PATCH 02/13] input_engine: Remove unnecessary semi-colons Silences -Wextra-semi warnings --- src/input_common/input_engine.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h index fb89f9257c..c85c72af86 100644 --- a/src/input_common/input_engine.h +++ b/src/input_common/input_engine.h @@ -138,36 +138,36 @@ public: /// Used for automapping features virtual std::vector GetInputDevices() const { return {}; - }; + } /// Retrieves the button mappings for the given device virtual InputCommon::ButtonMapping GetButtonMappingForDevice( [[maybe_unused]] const Common::ParamPackage& params) { return {}; - }; + } /// Retrieves the analog mappings for the given device virtual InputCommon::AnalogMapping GetAnalogMappingForDevice( [[maybe_unused]] const Common::ParamPackage& params) { return {}; - }; + } /// Retrieves the motion mappings for the given device virtual InputCommon::MotionMapping GetMotionMappingForDevice( [[maybe_unused]] const Common::ParamPackage& params) { return {}; - }; + } /// Retrieves the name of the given input. virtual Common::Input::ButtonNames GetUIName( [[maybe_unused]] const Common::ParamPackage& params) const { return Common::Input::ButtonNames::Engine; - }; + } /// Retrieves the index number of the given hat button direction virtual u8 GetHatButtonId([[maybe_unused]] const std::string& direction_name) const { return 0; - }; + } void PreSetController(const PadIdentifier& identifier); void PreSetButton(const PadIdentifier& identifier, int button); From 9a104e2b60a764efece235b45c805059a39df357 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 08:54:21 -0500 Subject: [PATCH 03/13] input_engine: Remove callback clearing in constructor The callback map is a member variable, so this will always be empty on initial construction. --- src/input_common/input_engine.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h index c85c72af86..44accd0bea 100644 --- a/src/input_common/input_engine.h +++ b/src/input_common/input_engine.h @@ -102,9 +102,7 @@ struct InputIdentifier { class InputEngine { public: - explicit InputEngine(const std::string& input_engine_) : input_engine(input_engine_) { - callback_list.clear(); - } + explicit InputEngine(const std::string& input_engine_) : input_engine(input_engine_) {} virtual ~InputEngine() = default; From 2b92d22bda767c7c723935c357bea474f8e2d2f8 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 09:05:23 -0500 Subject: [PATCH 04/13] input_engine: std::move engine name where applicable We can allow the name to be moved into, allowing allocations to be avoided. --- src/input_common/drivers/gc_adapter.cpp | 2 +- src/input_common/drivers/gc_adapter.h | 6 +++--- src/input_common/drivers/keyboard.cpp | 2 +- src/input_common/drivers/keyboard.h | 4 ++-- src/input_common/drivers/mouse.cpp | 2 +- src/input_common/drivers/mouse.h | 4 ++-- src/input_common/drivers/sdl_driver.cpp | 2 +- src/input_common/drivers/sdl_driver.h | 12 ++++++------ src/input_common/drivers/tas_input.cpp | 2 +- src/input_common/drivers/tas_input.h | 6 +++--- src/input_common/drivers/touch_screen.cpp | 2 +- src/input_common/drivers/touch_screen.h | 4 ++-- src/input_common/drivers/udp_client.cpp | 2 +- src/input_common/drivers/udp_client.h | 6 +++--- src/input_common/input_engine.h | 2 +- 15 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/input_common/drivers/gc_adapter.cpp b/src/input_common/drivers/gc_adapter.cpp index 8b65742236..4511477555 100644 --- a/src/input_common/drivers/gc_adapter.cpp +++ b/src/input_common/drivers/gc_adapter.cpp @@ -69,7 +69,7 @@ private: libusb_device_handle* handle{}; }; -GCAdapter::GCAdapter(const std::string& input_engine_) : InputEngine(input_engine_) { +GCAdapter::GCAdapter(std::string input_engine_) : InputEngine(std::move(input_engine_)) { if (usb_adapter_handle) { return; } diff --git a/src/input_common/drivers/gc_adapter.h b/src/input_common/drivers/gc_adapter.h index 8dc51d2e58..3c4f0396c2 100644 --- a/src/input_common/drivers/gc_adapter.h +++ b/src/input_common/drivers/gc_adapter.h @@ -22,10 +22,10 @@ namespace InputCommon { class LibUSBContext; class LibUSBDeviceHandle; -class GCAdapter : public InputCommon::InputEngine { +class GCAdapter : public InputEngine { public: - explicit GCAdapter(const std::string& input_engine_); - ~GCAdapter(); + explicit GCAdapter(std::string input_engine_); + ~GCAdapter() override; Common::Input::VibrationError SetRumble( const PadIdentifier& identifier, const Common::Input::VibrationStatus vibration) override; diff --git a/src/input_common/drivers/keyboard.cpp b/src/input_common/drivers/keyboard.cpp index 23b0c0ccf3..4c1e5bbec7 100644 --- a/src/input_common/drivers/keyboard.cpp +++ b/src/input_common/drivers/keyboard.cpp @@ -24,7 +24,7 @@ constexpr PadIdentifier keyboard_modifier_identifier = { .pad = 1, }; -Keyboard::Keyboard(const std::string& input_engine_) : InputEngine(input_engine_) { +Keyboard::Keyboard(std::string input_engine_) : InputEngine(std::move(input_engine_)) { // Keyboard is broken into 3 diferent sets: // key: Unfiltered intended for controllers. // keyboard_key: Allows only Settings::NativeKeyboard::Keys intended for keyboard emulation. diff --git a/src/input_common/drivers/keyboard.h b/src/input_common/drivers/keyboard.h index ad123b1368..3856c882ce 100644 --- a/src/input_common/drivers/keyboard.h +++ b/src/input_common/drivers/keyboard.h @@ -12,9 +12,9 @@ namespace InputCommon { * A button device factory representing a keyboard. It receives keyboard events and forward them * to all button devices it created. */ -class Keyboard final : public InputCommon::InputEngine { +class Keyboard final : public InputEngine { public: - explicit Keyboard(const std::string& input_engine_); + explicit Keyboard(std::string input_engine_); /** * Sets the status of all buttons bound with the key to pressed diff --git a/src/input_common/drivers/mouse.cpp b/src/input_common/drivers/mouse.cpp index 752118e97e..aa69216c8d 100644 --- a/src/input_common/drivers/mouse.cpp +++ b/src/input_common/drivers/mouse.cpp @@ -24,7 +24,7 @@ constexpr PadIdentifier identifier = { .pad = 0, }; -Mouse::Mouse(const std::string& input_engine_) : InputEngine(input_engine_) { +Mouse::Mouse(std::string input_engine_) : InputEngine(std::move(input_engine_)) { PreSetController(identifier); PreSetAxis(identifier, mouse_axis_x); PreSetAxis(identifier, mouse_axis_y); diff --git a/src/input_common/drivers/mouse.h b/src/input_common/drivers/mouse.h index 4a1fd2fd96..0404461781 100644 --- a/src/input_common/drivers/mouse.h +++ b/src/input_common/drivers/mouse.h @@ -27,9 +27,9 @@ enum class MouseButton { * A button device factory representing a keyboard. It receives keyboard events and forward them * to all button devices it created. */ -class Mouse final : public InputCommon::InputEngine { +class Mouse final : public InputEngine { public: - explicit Mouse(const std::string& input_engine_); + explicit Mouse(std::string input_engine_); /** * Signals that mouse has moved. diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cpp index 1052ed3946..1bae6cdc85 100644 --- a/src/input_common/drivers/sdl_driver.cpp +++ b/src/input_common/drivers/sdl_driver.cpp @@ -387,7 +387,7 @@ void SDLDriver::CloseJoysticks() { joystick_map.clear(); } -SDLDriver::SDLDriver(const std::string& input_engine_) : InputEngine(input_engine_) { +SDLDriver::SDLDriver(std::string input_engine_) : InputEngine(std::move(input_engine_)) { if (!Settings::values.enable_raw_input) { // Disable raw input. When enabled this setting causes SDL to die when a web applet opens SDL_SetHint(SDL_HINT_JOYSTICK_RAWINPUT, "0"); diff --git a/src/input_common/drivers/sdl_driver.h b/src/input_common/drivers/sdl_driver.h index d03ff4b84f..c6fffe3746 100644 --- a/src/input_common/drivers/sdl_driver.h +++ b/src/input_common/drivers/sdl_driver.h @@ -19,19 +19,19 @@ using SDL_GameController = struct _SDL_GameController; using SDL_Joystick = struct _SDL_Joystick; using SDL_JoystickID = s32; +namespace InputCommon { + +class SDLJoystick; + using ButtonBindings = std::array, 17>; using ZButtonBindings = std::array, 2>; -namespace InputCommon { - -class SDLJoystick; - -class SDLDriver : public InputCommon::InputEngine { +class SDLDriver : public InputEngine { public: /// Initializes and registers SDL device factories - SDLDriver(const std::string& input_engine_); + explicit SDLDriver(std::string input_engine_); /// Unregisters SDL device factories and shut them down. ~SDLDriver() override; diff --git a/src/input_common/drivers/tas_input.cpp b/src/input_common/drivers/tas_input.cpp index 0e01fb0d96..a2bedc9516 100644 --- a/src/input_common/drivers/tas_input.cpp +++ b/src/input_common/drivers/tas_input.cpp @@ -47,7 +47,7 @@ constexpr std::array, 20> text_to_tas_but {"KEY_ZR", TasButton::TRIGGER_ZR}, }; -Tas::Tas(const std::string& input_engine_) : InputCommon::InputEngine(input_engine_) { +Tas::Tas(std::string input_engine_) : InputEngine(std::move(input_engine_)) { for (size_t player_index = 0; player_index < PLAYER_NUMBER; player_index++) { PadIdentifier identifier{ .guid = Common::UUID{}, diff --git a/src/input_common/drivers/tas_input.h b/src/input_common/drivers/tas_input.h index c95a130fcb..9a7f987275 100644 --- a/src/input_common/drivers/tas_input.h +++ b/src/input_common/drivers/tas_input.h @@ -81,10 +81,10 @@ enum class TasState { Stopped, }; -class Tas final : public InputCommon::InputEngine { +class Tas final : public InputEngine { public: - explicit Tas(const std::string& input_engine_); - ~Tas(); + explicit Tas(std::string input_engine_); + ~Tas() override; /** * Changes the input status that will be stored in each frame diff --git a/src/input_common/drivers/touch_screen.cpp b/src/input_common/drivers/touch_screen.cpp index 45b3086f6a..8807818250 100644 --- a/src/input_common/drivers/touch_screen.cpp +++ b/src/input_common/drivers/touch_screen.cpp @@ -13,7 +13,7 @@ constexpr PadIdentifier identifier = { .pad = 0, }; -TouchScreen::TouchScreen(const std::string& input_engine_) : InputEngine(input_engine_) { +TouchScreen::TouchScreen(std::string input_engine_) : InputEngine(std::move(input_engine_)) { PreSetController(identifier); } diff --git a/src/input_common/drivers/touch_screen.h b/src/input_common/drivers/touch_screen.h index 25c11e8bfd..bf395c40b6 100644 --- a/src/input_common/drivers/touch_screen.h +++ b/src/input_common/drivers/touch_screen.h @@ -12,9 +12,9 @@ namespace InputCommon { * A button device factory representing a keyboard. It receives keyboard events and forward them * to all button devices it created. */ -class TouchScreen final : public InputCommon::InputEngine { +class TouchScreen final : public InputEngine { public: - explicit TouchScreen(const std::string& input_engine_); + explicit TouchScreen(std::string input_engine_); /** * Signals that mouse has moved. diff --git a/src/input_common/drivers/udp_client.cpp b/src/input_common/drivers/udp_client.cpp index fdee0f2d53..4ab991a7df 100644 --- a/src/input_common/drivers/udp_client.cpp +++ b/src/input_common/drivers/udp_client.cpp @@ -136,7 +136,7 @@ static void SocketLoop(Socket* socket) { socket->Loop(); } -UDPClient::UDPClient(const std::string& input_engine_) : InputEngine(input_engine_) { +UDPClient::UDPClient(std::string input_engine_) : InputEngine(std::move(input_engine_)) { LOG_INFO(Input, "Udp Initialization started"); ReloadSockets(); } diff --git a/src/input_common/drivers/udp_client.h b/src/input_common/drivers/udp_client.h index 5d483f26b3..1adc947c45 100644 --- a/src/input_common/drivers/udp_client.h +++ b/src/input_common/drivers/udp_client.h @@ -49,10 +49,10 @@ struct DeviceStatus { * A button device factory representing a keyboard. It receives keyboard events and forward them * to all button devices it created. */ -class UDPClient final : public InputCommon::InputEngine { +class UDPClient final : public InputEngine { public: - explicit UDPClient(const std::string& input_engine_); - ~UDPClient(); + explicit UDPClient(std::string input_engine_); + ~UDPClient() override; void ReloadSockets(); diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h index 44accd0bea..b21adfabfa 100644 --- a/src/input_common/input_engine.h +++ b/src/input_common/input_engine.h @@ -102,7 +102,7 @@ struct InputIdentifier { class InputEngine { public: - explicit InputEngine(const std::string& input_engine_) : input_engine(input_engine_) {} + explicit InputEngine(std::string input_engine_) : input_engine{std::move(input_engine_)} {} virtual ~InputEngine() = default; From 38f3442ea56a3ac9447924c015c2a9ade0f5bb83 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 09:09:03 -0500 Subject: [PATCH 05/13] input_engine: Pass VibrationStatus by const reference in SetRumble() Avoids creating copies of the struct where not necessary. --- src/common/input.h | 6 ++---- src/input_common/drivers/gc_adapter.cpp | 4 ++-- src/input_common/drivers/gc_adapter.h | 2 +- src/input_common/drivers/sdl_driver.cpp | 6 ++++-- src/input_common/drivers/sdl_driver.h | 2 +- src/input_common/input_engine.h | 2 +- src/input_common/input_poller.cpp | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/common/input.h b/src/common/input.h index eaee0bdeaf..12d5d976fa 100644 --- a/src/common/input.h +++ b/src/common/input.h @@ -266,11 +266,9 @@ class OutputDevice { public: virtual ~OutputDevice() = default; - virtual void SetLED([[maybe_unused]] LedStatus led_status) { - return; - } + virtual void SetLED([[maybe_unused]] LedStatus led_status) {} - virtual VibrationError SetVibration([[maybe_unused]] VibrationStatus vibration_status) { + virtual VibrationError SetVibration([[maybe_unused]] const VibrationStatus& vibration_status) { return VibrationError::NotSupported; } diff --git a/src/input_common/drivers/gc_adapter.cpp b/src/input_common/drivers/gc_adapter.cpp index 4511477555..7ab4540a87 100644 --- a/src/input_common/drivers/gc_adapter.cpp +++ b/src/input_common/drivers/gc_adapter.cpp @@ -325,8 +325,8 @@ bool GCAdapter::GetGCEndpoint(libusb_device* device) { return true; } -Common::Input::VibrationError GCAdapter::SetRumble(const PadIdentifier& identifier, - const Common::Input::VibrationStatus vibration) { +Common::Input::VibrationError GCAdapter::SetRumble( + const PadIdentifier& identifier, const Common::Input::VibrationStatus& vibration) { const auto mean_amplitude = (vibration.low_amplitude + vibration.high_amplitude) * 0.5f; const auto processed_amplitude = static_cast((mean_amplitude + std::pow(mean_amplitude, 0.3f)) * 0.5f * 0x8); diff --git a/src/input_common/drivers/gc_adapter.h b/src/input_common/drivers/gc_adapter.h index 3c4f0396c2..7ce1912a3d 100644 --- a/src/input_common/drivers/gc_adapter.h +++ b/src/input_common/drivers/gc_adapter.h @@ -28,7 +28,7 @@ public: ~GCAdapter() override; Common::Input::VibrationError SetRumble( - const PadIdentifier& identifier, const Common::Input::VibrationStatus vibration) override; + const PadIdentifier& identifier, const Common::Input::VibrationStatus& vibration) override; /// Used for automapping features std::vector GetInputDevices() const override; diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cpp index 1bae6cdc85..a9219dbf2a 100644 --- a/src/input_common/drivers/sdl_driver.cpp +++ b/src/input_common/drivers/sdl_driver.cpp @@ -491,8 +491,9 @@ std::vector SDLDriver::GetInputDevices() const { } return devices; } -Common::Input::VibrationError SDLDriver::SetRumble(const PadIdentifier& identifier, - const Common::Input::VibrationStatus vibration) { + +Common::Input::VibrationError SDLDriver::SetRumble( + const PadIdentifier& identifier, const Common::Input::VibrationStatus& vibration) { const auto joystick = GetSDLJoystickByGUID(identifier.guid.Format(), static_cast(identifier.port)); const auto process_amplitude_exp = [](f32 amplitude, f32 factor) { @@ -526,6 +527,7 @@ Common::Input::VibrationError SDLDriver::SetRumble(const PadIdentifier& identifi return Common::Input::VibrationError::None; } + Common::ParamPackage SDLDriver::BuildAnalogParamPackageForButton(int port, std::string guid, s32 axis, float value) const { Common::ParamPackage params{}; diff --git a/src/input_common/drivers/sdl_driver.h b/src/input_common/drivers/sdl_driver.h index c6fffe3746..e9a5d2e266 100644 --- a/src/input_common/drivers/sdl_driver.h +++ b/src/input_common/drivers/sdl_driver.h @@ -59,7 +59,7 @@ public: u8 GetHatButtonId(const std::string& direction_name) const override; Common::Input::VibrationError SetRumble( - const PadIdentifier& identifier, const Common::Input::VibrationStatus vibration) override; + const PadIdentifier& identifier, const Common::Input::VibrationStatus& vibration) override; private: void InitJoystick(int joystick_index); diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h index b21adfabfa..15cd5fa2e5 100644 --- a/src/input_common/input_engine.h +++ b/src/input_common/input_engine.h @@ -119,7 +119,7 @@ public: // Sets rumble to a controller virtual Common::Input::VibrationError SetRumble( [[maybe_unused]] const PadIdentifier& identifier, - [[maybe_unused]] const Common::Input::VibrationStatus vibration) { + [[maybe_unused]] const Common::Input::VibrationStatus& vibration) { return Common::Input::VibrationError::NotSupported; } diff --git a/src/input_common/input_poller.cpp b/src/input_common/input_poller.cpp index 7e4eafded0..de63f36b3d 100644 --- a/src/input_common/input_poller.cpp +++ b/src/input_common/input_poller.cpp @@ -673,7 +673,7 @@ public: } virtual Common::Input::VibrationError SetVibration( - Common::Input::VibrationStatus vibration_status) { + const Common::Input::VibrationStatus& vibration_status) { return input_engine->SetRumble(identifier, vibration_status); } From 985599e485cbee94eb99d0bfcfdbec5345968b15 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 09:20:55 -0500 Subject: [PATCH 06/13] input_engine: Pass LedStatus by const reference Avoids copies where reasonably applicable --- src/common/input.h | 2 +- src/input_common/input_engine.h | 2 +- src/input_common/input_poller.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/input.h b/src/common/input.h index 12d5d976fa..0b92449bc4 100644 --- a/src/common/input.h +++ b/src/common/input.h @@ -266,7 +266,7 @@ class OutputDevice { public: virtual ~OutputDevice() = default; - virtual void SetLED([[maybe_unused]] LedStatus led_status) {} + virtual void SetLED([[maybe_unused]] const LedStatus& led_status) {} virtual VibrationError SetVibration([[maybe_unused]] const VibrationStatus& vibration_status) { return VibrationError::NotSupported; diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h index 15cd5fa2e5..78e7046c7c 100644 --- a/src/input_common/input_engine.h +++ b/src/input_common/input_engine.h @@ -114,7 +114,7 @@ public: // Sets a led pattern for a controller virtual void SetLeds([[maybe_unused]] const PadIdentifier& identifier, - [[maybe_unused]] const Common::Input::LedStatus led_status) {} + [[maybe_unused]] const Common::Input::LedStatus& led_status) {} // Sets rumble to a controller virtual Common::Input::VibrationError SetRumble( diff --git a/src/input_common/input_poller.cpp b/src/input_common/input_poller.cpp index de63f36b3d..c56d5e0c24 100644 --- a/src/input_common/input_poller.cpp +++ b/src/input_common/input_poller.cpp @@ -668,7 +668,7 @@ public: explicit OutputFromIdentifier(PadIdentifier identifier_, InputEngine* input_engine_) : identifier(identifier_), input_engine(input_engine_) {} - virtual void SetLED(Common::Input::LedStatus led_status) { + virtual void SetLED(const Common::Input::LedStatus& led_status) { input_engine->SetLeds(identifier, led_status); } From a92dbec96242fe246de99878b12e217b3087463e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 09:23:19 -0500 Subject: [PATCH 07/13] input_engine: std::move InputIdentifier in SetCallback() Allows avoiding std::function allocations. --- src/input_common/input_engine.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input_common/input_engine.cpp b/src/input_common/input_engine.cpp index 2b2105376f..6b057e2f13 100644 --- a/src/input_common/input_engine.cpp +++ b/src/input_common/input_engine.cpp @@ -342,7 +342,7 @@ const std::string& InputEngine::GetEngineName() const { int InputEngine::SetCallback(InputIdentifier input_identifier) { std::lock_guard lock{mutex_callback}; - callback_list.insert_or_assign(last_callback_key, input_identifier); + callback_list.insert_or_assign(last_callback_key, std::move(input_identifier)); return last_callback_key++; } From 755822ceecf2a261a09f486706955bebc23d3917 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 09:26:44 -0500 Subject: [PATCH 08/13] input_engine: Take BasicMotion by const reference with SetMotion() and TriggerOnMotionChange() Copies the BasicMotion instance once instead of twice. --- src/input_common/drivers/sdl_driver.cpp | 4 ++-- src/input_common/input_engine.cpp | 4 ++-- src/input_common/input_engine.h | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cpp index a9219dbf2a..e33a5ff31f 100644 --- a/src/input_common/drivers/sdl_driver.cpp +++ b/src/input_common/drivers/sdl_driver.cpp @@ -88,7 +88,7 @@ public: return true; } - BasicMotion GetMotion() { + const BasicMotion& GetMotion() const { return motion; } @@ -367,7 +367,7 @@ void SDLDriver::HandleGameControllerEvent(const SDL_Event& event) { if (joystick->UpdateMotion(event.csensor)) { const PadIdentifier identifier = joystick->GetPadIdentifier(); SetMotion(identifier, 0, joystick->GetMotion()); - }; + } } break; } diff --git a/src/input_common/input_engine.cpp b/src/input_common/input_engine.cpp index 6b057e2f13..5481607bf1 100644 --- a/src/input_common/input_engine.cpp +++ b/src/input_common/input_engine.cpp @@ -91,7 +91,7 @@ void InputEngine::SetBattery(const PadIdentifier& identifier, BatteryLevel value TriggerOnBatteryChange(identifier, value); } -void InputEngine::SetMotion(const PadIdentifier& identifier, int motion, BasicMotion value) { +void InputEngine::SetMotion(const PadIdentifier& identifier, int motion, const BasicMotion& value) { { std::lock_guard lock{mutex}; ControllerData& controller = controller_list.at(identifier); @@ -286,7 +286,7 @@ void InputEngine::TriggerOnBatteryChange(const PadIdentifier& identifier, } void InputEngine::TriggerOnMotionChange(const PadIdentifier& identifier, int motion, - BasicMotion value) { + const BasicMotion& value) { std::lock_guard lock{mutex_callback}; for (const std::pair poller_pair : callback_list) { const InputIdentifier& poller = poller_pair.second; diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h index 78e7046c7c..f9fa5fec3c 100644 --- a/src/input_common/input_engine.h +++ b/src/input_common/input_engine.h @@ -190,7 +190,7 @@ protected: void SetHatButton(const PadIdentifier& identifier, int button, u8 value); void SetAxis(const PadIdentifier& identifier, int axis, f32 value); void SetBattery(const PadIdentifier& identifier, BatteryLevel value); - void SetMotion(const PadIdentifier& identifier, int motion, BasicMotion value); + void SetMotion(const PadIdentifier& identifier, int motion, const BasicMotion& value); virtual std::string GetHatButtonName([[maybe_unused]] u8 direction_value) const { return "Unknown"; @@ -209,7 +209,8 @@ private: void TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value); void TriggerOnAxisChange(const PadIdentifier& identifier, int button, f32 value); void TriggerOnBatteryChange(const PadIdentifier& identifier, BatteryLevel value); - void TriggerOnMotionChange(const PadIdentifier& identifier, int motion, BasicMotion value); + void TriggerOnMotionChange(const PadIdentifier& identifier, int motion, + const BasicMotion& value); bool IsInputIdentifierEqual(const InputIdentifier& input_identifier, const PadIdentifier& identifier, EngineInputType type, From e826e6715a39d10b9560fcbf3cd9081a24ddc870 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 09:32:55 -0500 Subject: [PATCH 09/13] input_engine: Iterate by reference rather than by value where applicable Avoids creating copies of several object instances (some of which being over 100 bytes in size). --- src/input_common/input_engine.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/input_common/input_engine.cpp b/src/input_common/input_engine.cpp index 5481607bf1..fce9143481 100644 --- a/src/input_common/input_engine.cpp +++ b/src/input_common/input_engine.cpp @@ -170,19 +170,19 @@ BasicMotion InputEngine::GetMotion(const PadIdentifier& identifier, int motion) } void InputEngine::ResetButtonState() { - for (std::pair controller : controller_list) { - for (std::pair button : controller.second.buttons) { + for (const auto& controller : controller_list) { + for (const auto& button : controller.second.buttons) { SetButton(controller.first, button.first, false); } - for (std::pair button : controller.second.hat_buttons) { + for (const auto& button : controller.second.hat_buttons) { SetHatButton(controller.first, button.first, false); } } } void InputEngine::ResetAnalogState() { - for (std::pair controller : controller_list) { - for (std::pair axis : controller.second.axes) { + for (const auto& controller : controller_list) { + for (const auto& axis : controller.second.axes) { SetAxis(controller.first, axis.first, 0.0); } } @@ -190,7 +190,7 @@ void InputEngine::ResetAnalogState() { void InputEngine::TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value) { std::lock_guard lock{mutex_callback}; - for (const std::pair poller_pair : callback_list) { + for (const auto& poller_pair : callback_list) { const InputIdentifier& poller = poller_pair.second; if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Button, button)) { continue; @@ -218,7 +218,7 @@ void InputEngine::TriggerOnButtonChange(const PadIdentifier& identifier, int but void InputEngine::TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value) { std::lock_guard lock{mutex_callback}; - for (const std::pair poller_pair : callback_list) { + for (const auto& poller_pair : callback_list) { const InputIdentifier& poller = poller_pair.second; if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::HatButton, button)) { continue; @@ -247,7 +247,7 @@ void InputEngine::TriggerOnHatButtonChange(const PadIdentifier& identifier, int void InputEngine::TriggerOnAxisChange(const PadIdentifier& identifier, int axis, f32 value) { std::lock_guard lock{mutex_callback}; - for (const std::pair poller_pair : callback_list) { + for (const auto& poller_pair : callback_list) { const InputIdentifier& poller = poller_pair.second; if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Analog, axis)) { continue; @@ -274,7 +274,7 @@ void InputEngine::TriggerOnAxisChange(const PadIdentifier& identifier, int axis, void InputEngine::TriggerOnBatteryChange(const PadIdentifier& identifier, [[maybe_unused]] BatteryLevel value) { std::lock_guard lock{mutex_callback}; - for (const std::pair poller_pair : callback_list) { + for (const auto& poller_pair : callback_list) { const InputIdentifier& poller = poller_pair.second; if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Battery, 0)) { continue; @@ -288,7 +288,7 @@ void InputEngine::TriggerOnBatteryChange(const PadIdentifier& identifier, void InputEngine::TriggerOnMotionChange(const PadIdentifier& identifier, int motion, const BasicMotion& value) { std::lock_guard lock{mutex_callback}; - for (const std::pair poller_pair : callback_list) { + for (const auto& poller_pair : callback_list) { const InputIdentifier& poller = poller_pair.second; if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Motion, motion)) { continue; From e51b852aee249b4f462ba3a769340910aa6a88c7 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 09:34:46 -0500 Subject: [PATCH 10/13] input_engine: Remove left-over namespace qualifiers These types are part of the InputCommon namespace. --- src/input_common/input_engine.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h index f9fa5fec3c..59707e1731 100644 --- a/src/input_common/input_engine.h +++ b/src/input_common/input_engine.h @@ -139,19 +139,19 @@ public: } /// Retrieves the button mappings for the given device - virtual InputCommon::ButtonMapping GetButtonMappingForDevice( + virtual ButtonMapping GetButtonMappingForDevice( [[maybe_unused]] const Common::ParamPackage& params) { return {}; } /// Retrieves the analog mappings for the given device - virtual InputCommon::AnalogMapping GetAnalogMappingForDevice( + virtual AnalogMapping GetAnalogMappingForDevice( [[maybe_unused]] const Common::ParamPackage& params) { return {}; } /// Retrieves the motion mappings for the given device - virtual InputCommon::MotionMapping GetMotionMappingForDevice( + virtual MotionMapping GetMotionMappingForDevice( [[maybe_unused]] const Common::ParamPackage& params) { return {}; } From 4d4a23447688030402dc1499db8a827065154136 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 09:50:48 -0500 Subject: [PATCH 11/13] input_engine: Avoid redundant map lookups We can use iterators to avoid looking up into maps twice in the getter functions. At the same time we can also avoid copying the ControllerData structs, since they're 264 bytes in size. --- src/input_common/input_engine.cpp | 40 ++++++++++++++++++------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/input_common/input_engine.cpp b/src/input_common/input_engine.cpp index fce9143481..a4a07d4ac3 100644 --- a/src/input_common/input_engine.cpp +++ b/src/input_common/input_engine.cpp @@ -104,68 +104,76 @@ void InputEngine::SetMotion(const PadIdentifier& identifier, int motion, const B bool InputEngine::GetButton(const PadIdentifier& identifier, int button) const { std::lock_guard lock{mutex}; - if (!controller_list.contains(identifier)) { + const auto controller_iter = controller_list.find(identifier); + if (controller_iter == controller_list.cend()) { LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.Format(), identifier.pad, identifier.port); return false; } - ControllerData controller = controller_list.at(identifier); - if (!controller.buttons.contains(button)) { + const ControllerData& controller = controller_iter->second; + const auto button_iter = controller.buttons.find(button); + if (button_iter == controller.buttons.cend()) { LOG_ERROR(Input, "Invalid button {}", button); return false; } - return controller.buttons.at(button); + return button_iter->second; } bool InputEngine::GetHatButton(const PadIdentifier& identifier, int button, u8 direction) const { std::lock_guard lock{mutex}; - if (!controller_list.contains(identifier)) { + const auto controller_iter = controller_list.find(identifier); + if (controller_iter == controller_list.cend()) { LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.Format(), identifier.pad, identifier.port); return false; } - ControllerData controller = controller_list.at(identifier); - if (!controller.hat_buttons.contains(button)) { + const ControllerData& controller = controller_iter->second; + const auto hat_iter = controller.hat_buttons.find(button); + if (hat_iter == controller.hat_buttons.cend()) { LOG_ERROR(Input, "Invalid hat button {}", button); return false; } - return (controller.hat_buttons.at(button) & direction) != 0; + return (hat_iter->second & direction) != 0; } f32 InputEngine::GetAxis(const PadIdentifier& identifier, int axis) const { std::lock_guard lock{mutex}; - if (!controller_list.contains(identifier)) { + const auto controller_iter = controller_list.find(identifier); + if (controller_iter == controller_list.cend()) { LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.Format(), identifier.pad, identifier.port); return 0.0f; } - ControllerData controller = controller_list.at(identifier); - if (!controller.axes.contains(axis)) { + const ControllerData& controller = controller_iter->second; + const auto axis_iter = controller.axes.find(axis); + if (axis_iter == controller.axes.cend()) { LOG_ERROR(Input, "Invalid axis {}", axis); return 0.0f; } - return controller.axes.at(axis); + return axis_iter->second; } BatteryLevel InputEngine::GetBattery(const PadIdentifier& identifier) const { std::lock_guard lock{mutex}; - if (!controller_list.contains(identifier)) { + const auto controller_iter = controller_list.find(identifier); + if (controller_iter == controller_list.cend()) { LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.Format(), identifier.pad, identifier.port); return BatteryLevel::Charging; } - ControllerData controller = controller_list.at(identifier); + const ControllerData& controller = controller_iter->second; return controller.battery; } BasicMotion InputEngine::GetMotion(const PadIdentifier& identifier, int motion) const { std::lock_guard lock{mutex}; - if (!controller_list.contains(identifier)) { + const auto controller_iter = controller_list.find(identifier); + if (controller_iter == controller_list.cend()) { LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.Format(), identifier.pad, identifier.port); return {}; } - ControllerData controller = controller_list.at(identifier); + const ControllerData& controller = controller_iter->second; return controller.motions.at(motion); } From a9d39b68952bf3ce9607a5947f056eb990e2b430 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 10:18:02 -0500 Subject: [PATCH 12/13] input_engine: Simplify PreSet* family of functions We can make use of try_emplace() to insert values only if they don't already exist. --- src/input_common/input_engine.cpp | 20 +++++--------------- src/input_common/input_engine.h | 18 +++++++++--------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/input_common/input_engine.cpp b/src/input_common/input_engine.cpp index a4a07d4ac3..9c17ca4f77 100644 --- a/src/input_common/input_engine.cpp +++ b/src/input_common/input_engine.cpp @@ -10,41 +10,31 @@ namespace InputCommon { void InputEngine::PreSetController(const PadIdentifier& identifier) { std::lock_guard lock{mutex}; - if (!controller_list.contains(identifier)) { - controller_list.insert_or_assign(identifier, ControllerData{}); - } + controller_list.try_emplace(identifier); } void InputEngine::PreSetButton(const PadIdentifier& identifier, int button) { std::lock_guard lock{mutex}; ControllerData& controller = controller_list.at(identifier); - if (!controller.buttons.contains(button)) { - controller.buttons.insert_or_assign(button, false); - } + controller.buttons.try_emplace(button, false); } void InputEngine::PreSetHatButton(const PadIdentifier& identifier, int button) { std::lock_guard lock{mutex}; ControllerData& controller = controller_list.at(identifier); - if (!controller.hat_buttons.contains(button)) { - controller.hat_buttons.insert_or_assign(button, u8{0}); - } + controller.hat_buttons.try_emplace(button, u8{0}); } void InputEngine::PreSetAxis(const PadIdentifier& identifier, int axis) { std::lock_guard lock{mutex}; ControllerData& controller = controller_list.at(identifier); - if (!controller.axes.contains(axis)) { - controller.axes.insert_or_assign(axis, 0.0f); - } + controller.axes.try_emplace(axis, 0.0f); } void InputEngine::PreSetMotion(const PadIdentifier& identifier, int motion) { std::lock_guard lock{mutex}; ControllerData& controller = controller_list.at(identifier); - if (!controller.motions.contains(motion)) { - controller.motions.insert_or_assign(motion, BasicMotion{}); - } + controller.motions.try_emplace(motion); } void InputEngine::SetButton(const PadIdentifier& identifier, int button, bool value) { diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h index 59707e1731..ec88904845 100644 --- a/src/input_common/input_engine.h +++ b/src/input_common/input_engine.h @@ -23,15 +23,15 @@ struct PadIdentifier { friend constexpr bool operator==(const PadIdentifier&, const PadIdentifier&) = default; }; -// Basic motion data containing data from the sensors and a timestamp in microsecons +// Basic motion data containing data from the sensors and a timestamp in microseconds struct BasicMotion { - float gyro_x; - float gyro_y; - float gyro_z; - float accel_x; - float accel_y; - float accel_z; - u64 delta_timestamp; + float gyro_x{}; + float gyro_y{}; + float gyro_z{}; + float accel_x{}; + float accel_y{}; + float accel_z{}; + u64 delta_timestamp{}; }; // Stages of a battery charge @@ -202,7 +202,7 @@ private: std::unordered_map hat_buttons; std::unordered_map axes; std::unordered_map motions; - BatteryLevel battery; + BatteryLevel battery{}; }; void TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value); From e4de1783e129c705585adf1b88439a5e07b61fb7 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 13 Dec 2021 10:21:37 -0500 Subject: [PATCH 13/13] input_engine: Fix typo in TriggerOnAxisChange() parameter name --- src/input_common/input_engine.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h index ec88904845..390581c948 100644 --- a/src/input_common/input_engine.h +++ b/src/input_common/input_engine.h @@ -207,7 +207,7 @@ private: void TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value); void TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value); - void TriggerOnAxisChange(const PadIdentifier& identifier, int button, f32 value); + void TriggerOnAxisChange(const PadIdentifier& identifier, int axis, f32 value); void TriggerOnBatteryChange(const PadIdentifier& identifier, BatteryLevel value); void TriggerOnMotionChange(const PadIdentifier& identifier, int motion, const BasicMotion& value);