From 46b4461fbb0514dd50c096ef896b1752d81079d0 Mon Sep 17 00:00:00 2001 From: Ameer Date: Sun, 21 Jun 2020 21:50:58 -0400 Subject: shared_ptr for the GC adapter class, constexpr constants --- src/input_common/gcadapter/gc_adapter.cpp | 40 +++++++++++++------------------ src/input_common/gcadapter/gc_adapter.h | 40 +++++++++++++------------------ src/input_common/gcadapter/gc_poller.cpp | 24 +++++++++---------- src/input_common/gcadapter/gc_poller.h | 8 +++---- src/input_common/main.cpp | 6 +++-- 5 files changed, 52 insertions(+), 66 deletions(-) diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 498bd0d6e..db72d7633 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -6,7 +6,6 @@ #include "input_common/gcadapter/gc_adapter.h" namespace GCAdapter { -Adapter* Adapter::adapter_instance{nullptr}; Adapter::Adapter() { if (usb_adapter_handle != nullptr) { @@ -20,13 +19,6 @@ Adapter::Adapter() { StartScanThread(); } -Adapter* Adapter::GetInstance() { - if (!adapter_instance) { - adapter_instance = new Adapter; - } - return adapter_instance; -} - GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) { GCPadStatus pad = {}; bool get_origin = false; @@ -151,34 +143,26 @@ void Adapter::Read() { } // Accounting for a threshold here because of some controller variance - if (pad[port].stick_x > - pad_constants.MAIN_STICK_CENTER_X + pad_constants.THRESHOLD || - pad[port].stick_x < - pad_constants.MAIN_STICK_CENTER_X - pad_constants.THRESHOLD) { + if (pad[port].stick_x > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD || + pad[port].stick_x < pad[port].MAIN_STICK_CENTER_X - pad[port].THRESHOLD) { pad[port].axis = GCAdapter::PadAxes::StickX; pad[port].axis_value = pad[port].stick_x; pad_queue[port].Push(pad[port]); } - if (pad[port].stick_y > - pad_constants.MAIN_STICK_CENTER_Y + pad_constants.THRESHOLD || - pad[port].stick_y < - pad_constants.MAIN_STICK_CENTER_Y - pad_constants.THRESHOLD) { + if (pad[port].stick_y > pad[port].MAIN_STICK_CENTER_Y + pad[port].THRESHOLD || + pad[port].stick_y < pad[port].MAIN_STICK_CENTER_Y - pad[port].THRESHOLD) { pad[port].axis = GCAdapter::PadAxes::StickY; pad[port].axis_value = pad[port].stick_y; pad_queue[port].Push(pad[port]); } - if (pad[port].substick_x > - pad_constants.C_STICK_CENTER_X + pad_constants.THRESHOLD || - pad[port].substick_x < - pad_constants.C_STICK_CENTER_X - pad_constants.THRESHOLD) { + if (pad[port].substick_x > pad[port].C_STICK_CENTER_X + pad[port].THRESHOLD || + pad[port].substick_x < pad[port].C_STICK_CENTER_X - pad[port].THRESHOLD) { pad[port].axis = GCAdapter::PadAxes::SubstickX; pad[port].axis_value = pad[port].substick_x; pad_queue[port].Push(pad[port]); } - if (pad[port].substick_y > - pad_constants.C_STICK_CENTER_Y + pad_constants.THRESHOLD || - pad[port].substick_y < - pad_constants.C_STICK_CENTER_Y - pad_constants.THRESHOLD) { + if (pad[port].substick_y > pad[port].C_STICK_CENTER_Y + pad[port].THRESHOLD || + pad[port].substick_y < pad[port].C_STICK_CENTER_Y - pad[port].THRESHOLD) { pad[port].axis = GCAdapter::PadAxes::SubstickY; pad[port].axis_value = pad[port].substick_y; pad_queue[port].Push(pad[port]); @@ -367,8 +351,16 @@ std::array, 4>& Adapter::GetPadQueue() { return pad_queue; } +const std::array, 4>& Adapter::GetPadQueue() const { + return pad_queue; +} + std::array& Adapter::GetPadState() { return state; } +const std::array& Adapter::GetPadState() const { + return state; +} + } // end of namespace GCAdapter diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index cb0dd0ab1..d7a57e7b7 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -46,17 +46,6 @@ enum class PadAxes : u8 { TriggerRight, Undefined, }; -const struct GCPadConstants { - const u8 MAIN_STICK_CENTER_X = 0x80; - const u8 MAIN_STICK_CENTER_Y = 0x80; - const u8 MAIN_STICK_RADIUS = 0x7f; - const u8 C_STICK_CENTER_X = 0x80; - const u8 C_STICK_CENTER_Y = 0x80; - const u8 C_STICK_RADIUS = 0x7f; - - const u8 TRIGGER_CENTER = 20; - const u8 THRESHOLD = 10; -} pad_constants; struct GCPadStatus { u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits @@ -67,6 +56,15 @@ struct GCPadStatus { u8 trigger_left; // 0 <= trigger_left <= 255 u8 trigger_right; // 0 <= trigger_right <= 255 + static constexpr u8 MAIN_STICK_CENTER_X = 0x80; + static constexpr u8 MAIN_STICK_CENTER_Y = 0x80; + static constexpr u8 MAIN_STICK_RADIUS = 0x7f; + static constexpr u8 C_STICK_CENTER_X = 0x80; + static constexpr u8 C_STICK_CENTER_Y = 0x80; + static constexpr u8 C_STICK_RADIUS = 0x7f; + static constexpr u8 TRIGGER_CENTER = 20; + static constexpr u8 THRESHOLD = 10; + u8 port; PadAxes axis = PadAxes::Undefined; u8 axis_value = 255; @@ -87,28 +85,22 @@ enum { /// Singleton Adapter class class Adapter { public: - /// For retreiving the singleton instance - static Adapter* GetInstance(); + /// Initialize the GC Adapter capture and read sequence + Adapter(); + /// Close the adapter read thread and release the adapter + ~Adapter(); /// Used for polling void BeginConfiguration(); void EndConfiguration(); std::array, 4>& GetPadQueue(); + const std::array, 4>& GetPadQueue() const; + std::array& GetPadState(); - std::array, 4>& GetPadQueue() const; - std::array& GetPadState() const; + const std::array& GetPadState() const; private: - /// Singleton instance. - static Adapter* adapter_instance; - - /// Initialize the GC Adapter capture and read sequence - Adapter(); - - /// Close the adapter read thread and release the adapter - ~Adapter(); - GCPadStatus CheckStatus(int port, u8 adapter_payload[37]); void PadToState(GCPadStatus pad, GCState& state); diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index ac4126cb6..ad8b4b431 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -14,7 +14,8 @@ namespace InputCommon { class GCButton final : public Input::ButtonDevice { public: - explicit GCButton(int port_, int button_, int axis_, GCAdapter::Adapter* adapter) + explicit GCButton(int port_, int button_, int axis_, + std::shared_ptr adapter) : port(port_), button(button_), gcadapter(adapter) {} ~GCButton() override; @@ -26,13 +27,13 @@ public: private: const int port; const int button; - GCAdapter::Adapter* gcadapter; + std::shared_ptr gcadapter; }; class GCAxisButton final : public Input::ButtonDevice { public: explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, - GCAdapter::Adapter* adapter) + std::shared_ptr adapter) : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), gcadapter(adapter) {} @@ -49,12 +50,11 @@ private: const int axis; float threshold; bool trigger_if_greater; - GCAdapter::Adapter* gcadapter; + std::shared_ptr gcadapter; }; -GCButtonFactory::GCButtonFactory() { - adapter = GCAdapter::Adapter::GetInstance(); -} +GCButtonFactory::GCButtonFactory(std::shared_ptr adapter_) + : adapter(adapter_) {} GCButton::~GCButton() = default; @@ -171,7 +171,8 @@ void GCButtonFactory::EndConfiguration() { class GCAnalog final : public Input::AnalogDevice { public: - GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter) + GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, + std::shared_ptr adapter) : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {} float GetAxis(int axis) const { @@ -230,13 +231,12 @@ private: const int axis_y; const float deadzone; mutable std::mutex mutex; - GCAdapter::Adapter* gcadapter; + std::shared_ptr gcadapter; }; /// An analog device factory that creates analog devices from GC Adapter -GCAnalogFactory::GCAnalogFactory() { - adapter = GCAdapter::Adapter::GetInstance(); -}; +GCAnalogFactory::GCAnalogFactory(std::shared_ptr adapter_) + : adapter(adapter_) {} /** * Creates analog device from joystick axes diff --git a/src/input_common/gcadapter/gc_poller.h b/src/input_common/gcadapter/gc_poller.h index 31ff1c123..d3a56da5b 100644 --- a/src/input_common/gcadapter/gc_poller.h +++ b/src/input_common/gcadapter/gc_poller.h @@ -15,7 +15,7 @@ namespace InputCommon { */ class GCButtonFactory final : public Input::Factory { public: - GCButtonFactory(); + GCButtonFactory(std::shared_ptr adapter_); /** * Creates a button device from a button press @@ -35,14 +35,14 @@ public: } private: - GCAdapter::Adapter* adapter; + std::shared_ptr adapter; bool polling = false; }; /// An analog device factory that creates analog devices from GC Adapter class GCAnalogFactory final : public Input::Factory { public: - GCAnalogFactory(); + GCAnalogFactory(std::shared_ptr adapter_); std::unique_ptr Create(const Common::ParamPackage& params) override; Common::ParamPackage GetNextInput(); @@ -55,7 +55,7 @@ public: } private: - GCAdapter::Adapter* adapter; + std::shared_ptr adapter; int analog_x_axis = -1; int analog_y_axis = -1; int controller_number = -1; diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp index 89dddf7cf..fc399db7e 100644 --- a/src/input_common/main.cpp +++ b/src/input_common/main.cpp @@ -25,13 +25,15 @@ static std::shared_ptr motion_emu; static std::unique_ptr sdl; #endif static std::unique_ptr udp; +static std::shared_ptr gcadapter; static std::shared_ptr gcbuttons; static std::shared_ptr gcanalog; void Init() { - gcbuttons = std::make_shared(); + gcadapter = std::make_shared(); + gcbuttons = std::make_shared(gcadapter); Input::RegisterFactory("gcpad", gcbuttons); - gcanalog = std::make_shared(); + gcanalog = std::make_shared(gcadapter); Input::RegisterFactory("gcpad", gcanalog); keyboard = std::make_shared(); -- cgit v1.2.3