diff options
Diffstat (limited to 'src/input_common/gcadapter')
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.cpp | 35 | ||||
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.h | 6 | ||||
| -rw-r--r-- | src/input_common/gcadapter/gc_poller.cpp | 48 |
3 files changed, 45 insertions, 44 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 89c148aba..c95feb0d7 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp | |||
| @@ -21,7 +21,7 @@ | |||
| 21 | 21 | ||
| 22 | namespace GCAdapter { | 22 | namespace GCAdapter { |
| 23 | 23 | ||
| 24 | /// Used to loop through and assign button in poller | 24 | // Used to loop through and assign button in poller |
| 25 | constexpr std::array<PadButton, 12> PadButtonArray{ | 25 | constexpr std::array<PadButton, 12> PadButtonArray{ |
| 26 | PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT, PadButton::PAD_BUTTON_DOWN, | 26 | PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT, PadButton::PAD_BUTTON_DOWN, |
| 27 | PadButton::PAD_BUTTON_UP, PadButton::PAD_TRIGGER_Z, PadButton::PAD_TRIGGER_R, | 27 | PadButton::PAD_BUTTON_UP, PadButton::PAD_TRIGGER_Z, PadButton::PAD_TRIGGER_R, |
| @@ -29,6 +29,18 @@ constexpr std::array<PadButton, 12> PadButtonArray{ | |||
| 29 | PadButton::PAD_BUTTON_X, PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_START, | 29 | PadButton::PAD_BUTTON_X, PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_START, |
| 30 | }; | 30 | }; |
| 31 | 31 | ||
| 32 | static void PadToState(const GCPadStatus& pad, GCState& out_state) { | ||
| 33 | for (const auto& button : PadButtonArray) { | ||
| 34 | const auto button_key = static_cast<u16>(button); | ||
| 35 | const auto button_value = (pad.button & button_key) != 0; | ||
| 36 | out_state.buttons.insert_or_assign(static_cast<s32>(button_key), button_value); | ||
| 37 | } | ||
| 38 | |||
| 39 | for (std::size_t i = 0; i < pad.axis_values.size(); ++i) { | ||
| 40 | out_state.axes.insert_or_assign(static_cast<u32>(i), pad.axis_values[i]); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 32 | Adapter::Adapter() { | 44 | Adapter::Adapter() { |
| 33 | if (usb_adapter_handle != nullptr) { | 45 | if (usb_adapter_handle != nullptr) { |
| 34 | return; | 46 | return; |
| @@ -78,17 +90,17 @@ GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& ad | |||
| 78 | 90 | ||
| 79 | for (std::size_t i = 0; i < b1_buttons.size(); ++i) { | 91 | for (std::size_t i = 0; i < b1_buttons.size(); ++i) { |
| 80 | if ((b1 & (1U << i)) != 0) { | 92 | if ((b1 & (1U << i)) != 0) { |
| 81 | pad.button |= static_cast<u16>(b1_buttons[i]); | 93 | pad.button = static_cast<u16>(pad.button | static_cast<u16>(b1_buttons[i])); |
| 82 | } | 94 | } |
| 83 | } | 95 | } |
| 84 | 96 | ||
| 85 | for (std::size_t j = 0; j < b2_buttons.size(); ++j) { | 97 | for (std::size_t j = 0; j < b2_buttons.size(); ++j) { |
| 86 | if ((b2 & (1U << j)) != 0) { | 98 | if ((b2 & (1U << j)) != 0) { |
| 87 | pad.button |= static_cast<u16>(b2_buttons[j]); | 99 | pad.button = static_cast<u16>(pad.button | static_cast<u16>(b2_buttons[j])); |
| 88 | } | 100 | } |
| 89 | } | 101 | } |
| 90 | for (PadAxes axis : axes) { | 102 | for (PadAxes axis : axes) { |
| 91 | const std::size_t index = static_cast<std::size_t>(axis); | 103 | const auto index = static_cast<std::size_t>(axis); |
| 92 | pad.axis_values[index] = adapter_payload[offset + 3 + index]; | 104 | pad.axis_values[index] = adapter_payload[offset + 3 + index]; |
| 93 | } | 105 | } |
| 94 | 106 | ||
| @@ -100,17 +112,6 @@ GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& ad | |||
| 100 | return pad; | 112 | return pad; |
| 101 | } | 113 | } |
| 102 | 114 | ||
| 103 | void Adapter::PadToState(const GCPadStatus& pad, GCState& state) { | ||
| 104 | for (const auto& button : PadButtonArray) { | ||
| 105 | const u16 button_value = static_cast<u16>(button); | ||
| 106 | state.buttons.insert_or_assign(button_value, pad.button & button_value); | ||
| 107 | } | ||
| 108 | |||
| 109 | for (size_t i = 0; i < pad.axis_values.size(); ++i) { | ||
| 110 | state.axes.insert_or_assign(static_cast<u8>(i), pad.axis_values[i]); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | void Adapter::Read() { | 115 | void Adapter::Read() { |
| 115 | LOG_DEBUG(Input, "GC Adapter Read() thread started"); | 116 | LOG_DEBUG(Input, "GC Adapter Read() thread started"); |
| 116 | 117 | ||
| @@ -250,7 +251,7 @@ void Adapter::GetGCEndpoint(libusb_device* device) { | |||
| 250 | const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i]; | 251 | const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i]; |
| 251 | for (u8 e = 0; e < interface->bNumEndpoints; e++) { | 252 | for (u8 e = 0; e < interface->bNumEndpoints; e++) { |
| 252 | const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e]; | 253 | const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e]; |
| 253 | if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) { | 254 | if ((endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) != 0) { |
| 254 | input_endpoint = endpoint->bEndpointAddress; | 255 | input_endpoint = endpoint->bEndpointAddress; |
| 255 | } else { | 256 | } else { |
| 256 | output_endpoint = endpoint->bEndpointAddress; | 257 | output_endpoint = endpoint->bEndpointAddress; |
| @@ -419,7 +420,7 @@ const std::array<GCState, 4>& Adapter::GetPadState() const { | |||
| 419 | return state; | 420 | return state; |
| 420 | } | 421 | } |
| 421 | 422 | ||
| 422 | int Adapter::GetOriginValue(int port, int axis) const { | 423 | int Adapter::GetOriginValue(u32 port, u32 axis) const { |
| 423 | return origin_status[port].axis_values[axis]; | 424 | return origin_status[port].axis_values[axis]; |
| 424 | } | 425 | } |
| 425 | 426 | ||
diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index 75bf9fe74..4f5f3de8e 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h | |||
| @@ -60,7 +60,7 @@ struct GCPadStatus { | |||
| 60 | 60 | ||
| 61 | struct GCState { | 61 | struct GCState { |
| 62 | std::unordered_map<int, bool> buttons; | 62 | std::unordered_map<int, bool> buttons; |
| 63 | std::unordered_map<int, u16> axes; | 63 | std::unordered_map<u32, u16> axes; |
| 64 | }; | 64 | }; |
| 65 | 65 | ||
| 66 | enum class ControllerTypes { None, Wired, Wireless }; | 66 | enum class ControllerTypes { None, Wired, Wireless }; |
| @@ -89,13 +89,11 @@ public: | |||
| 89 | std::array<GCState, 4>& GetPadState(); | 89 | std::array<GCState, 4>& GetPadState(); |
| 90 | const std::array<GCState, 4>& GetPadState() const; | 90 | const std::array<GCState, 4>& GetPadState() const; |
| 91 | 91 | ||
| 92 | int GetOriginValue(int port, int axis) const; | 92 | int GetOriginValue(u32 port, u32 axis) const; |
| 93 | 93 | ||
| 94 | private: | 94 | private: |
| 95 | GCPadStatus GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload); | 95 | GCPadStatus GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload); |
| 96 | 96 | ||
| 97 | void PadToState(const GCPadStatus& pad, GCState& state); | ||
| 98 | |||
| 99 | void Read(); | 97 | void Read(); |
| 100 | 98 | ||
| 101 | /// Resets status of device connected to port | 99 | /// Resets status of device connected to port |
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index 92e9e8e89..893556916 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp | |||
| @@ -15,7 +15,7 @@ namespace InputCommon { | |||
| 15 | 15 | ||
| 16 | class GCButton final : public Input::ButtonDevice { | 16 | class GCButton final : public Input::ButtonDevice { |
| 17 | public: | 17 | public: |
| 18 | explicit GCButton(int port_, int button_, const GCAdapter::Adapter* adapter) | 18 | explicit GCButton(u32 port_, int button_, const GCAdapter::Adapter* adapter) |
| 19 | : port(port_), button(button_), gcadapter(adapter) {} | 19 | : port(port_), button(button_), gcadapter(adapter) {} |
| 20 | 20 | ||
| 21 | ~GCButton() override; | 21 | ~GCButton() override; |
| @@ -28,14 +28,14 @@ public: | |||
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | private: | 30 | private: |
| 31 | const int port; | 31 | const u32 port; |
| 32 | const int button; | 32 | const int button; |
| 33 | const GCAdapter::Adapter* gcadapter; | 33 | const GCAdapter::Adapter* gcadapter; |
| 34 | }; | 34 | }; |
| 35 | 35 | ||
| 36 | class GCAxisButton final : public Input::ButtonDevice { | 36 | class GCAxisButton final : public Input::ButtonDevice { |
| 37 | public: | 37 | public: |
| 38 | explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, | 38 | explicit GCAxisButton(u32 port_, u32 axis_, float threshold_, bool trigger_if_greater_, |
| 39 | const GCAdapter::Adapter* adapter) | 39 | const GCAdapter::Adapter* adapter) |
| 40 | : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), | 40 | : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), |
| 41 | gcadapter(adapter), | 41 | gcadapter(adapter), |
| @@ -56,8 +56,8 @@ public: | |||
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | private: | 58 | private: |
| 59 | const int port; | 59 | const u32 port; |
| 60 | const int axis; | 60 | const u32 axis; |
| 61 | float threshold; | 61 | float threshold; |
| 62 | bool trigger_if_greater; | 62 | bool trigger_if_greater; |
| 63 | const GCAdapter::Adapter* gcadapter; | 63 | const GCAdapter::Adapter* gcadapter; |
| @@ -70,8 +70,8 @@ GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_) | |||
| 70 | GCButton::~GCButton() = default; | 70 | GCButton::~GCButton() = default; |
| 71 | 71 | ||
| 72 | std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) { | 72 | std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) { |
| 73 | const int button_id = params.Get("button", 0); | 73 | const auto button_id = params.Get("button", 0); |
| 74 | const int port = params.Get("port", 0); | 74 | const auto port = static_cast<u32>(params.Get("port", 0)); |
| 75 | 75 | ||
| 76 | constexpr int PAD_STICK_ID = static_cast<u16>(GCAdapter::PadButton::PAD_STICK); | 76 | constexpr int PAD_STICK_ID = static_cast<u16>(GCAdapter::PadButton::PAD_STICK); |
| 77 | 77 | ||
| @@ -149,25 +149,27 @@ void GCButtonFactory::EndConfiguration() { | |||
| 149 | 149 | ||
| 150 | class GCAnalog final : public Input::AnalogDevice { | 150 | class GCAnalog final : public Input::AnalogDevice { |
| 151 | public: | 151 | public: |
| 152 | GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, | 152 | explicit GCAnalog(u32 port_, u32 axis_x_, u32 axis_y_, float deadzone_, |
| 153 | const GCAdapter::Adapter* adapter, float range_) | 153 | const GCAdapter::Adapter* adapter, float range_) |
| 154 | : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter), | 154 | : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter), |
| 155 | origin_value_x(static_cast<float>(adapter->GetOriginValue(port_, axis_x_))), | 155 | origin_value_x(static_cast<float>(adapter->GetOriginValue(port_, axis_x_))), |
| 156 | origin_value_y(static_cast<float>(adapter->GetOriginValue(port_, axis_y_))), | 156 | origin_value_y(static_cast<float>(adapter->GetOriginValue(port_, axis_y_))), |
| 157 | range(range_) {} | 157 | range(range_) {} |
| 158 | 158 | ||
| 159 | float GetAxis(int axis) const { | 159 | float GetAxis(u32 axis) const { |
| 160 | if (gcadapter->DeviceConnected(port)) { | 160 | if (gcadapter->DeviceConnected(port)) { |
| 161 | std::lock_guard lock{mutex}; | 161 | std::lock_guard lock{mutex}; |
| 162 | const auto origin_value = axis % 2 == 0 ? origin_value_x : origin_value_y; | 162 | const auto origin_value = axis % 2 == 0 ? origin_value_x : origin_value_y; |
| 163 | return (gcadapter->GetPadState()[port].axes.at(axis) - origin_value) / (100.0f * range); | 163 | const auto axis_value = |
| 164 | static_cast<float>(gcadapter->GetPadState()[port].axes.at(axis)); | ||
| 165 | return (axis_value - origin_value) / (100.0f * range); | ||
| 164 | } | 166 | } |
| 165 | return 0.0f; | 167 | return 0.0f; |
| 166 | } | 168 | } |
| 167 | 169 | ||
| 168 | std::pair<float, float> GetAnalog(int axis_x, int axis_y) const { | 170 | std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const { |
| 169 | float x = GetAxis(axis_x); | 171 | float x = GetAxis(analog_axis_x); |
| 170 | float y = GetAxis(axis_y); | 172 | float y = GetAxis(analog_axis_y); |
| 171 | 173 | ||
| 172 | // Make sure the coordinates are in the unit circle, | 174 | // Make sure the coordinates are in the unit circle, |
| 173 | // otherwise normalize it. | 175 | // otherwise normalize it. |
| @@ -208,9 +210,9 @@ public: | |||
| 208 | } | 210 | } |
| 209 | 211 | ||
| 210 | private: | 212 | private: |
| 211 | const int port; | 213 | const u32 port; |
| 212 | const int axis_x; | 214 | const u32 axis_x; |
| 213 | const int axis_y; | 215 | const u32 axis_y; |
| 214 | const float deadzone; | 216 | const float deadzone; |
| 215 | const GCAdapter::Adapter* gcadapter; | 217 | const GCAdapter::Adapter* gcadapter; |
| 216 | const float origin_value_x; | 218 | const float origin_value_x; |
| @@ -231,11 +233,11 @@ GCAnalogFactory::GCAnalogFactory(std::shared_ptr<GCAdapter::Adapter> adapter_) | |||
| 231 | * - "axis_y": the index of the axis to be bind as y-axis | 233 | * - "axis_y": the index of the axis to be bind as y-axis |
| 232 | */ | 234 | */ |
| 233 | std::unique_ptr<Input::AnalogDevice> GCAnalogFactory::Create(const Common::ParamPackage& params) { | 235 | std::unique_ptr<Input::AnalogDevice> GCAnalogFactory::Create(const Common::ParamPackage& params) { |
| 234 | const int port = params.Get("port", 0); | 236 | const auto port = static_cast<u32>(params.Get("port", 0)); |
| 235 | const int axis_x = params.Get("axis_x", 0); | 237 | const auto axis_x = static_cast<u32>(params.Get("axis_x", 0)); |
| 236 | const int axis_y = params.Get("axis_y", 1); | 238 | const auto axis_y = static_cast<u32>(params.Get("axis_y", 1)); |
| 237 | const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); | 239 | const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); |
| 238 | const float range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); | 240 | const auto range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); |
| 239 | 241 | ||
| 240 | return std::make_unique<GCAnalog>(port, axis_x, axis_y, deadzone, adapter.get(), range); | 242 | return std::make_unique<GCAnalog>(port, axis_x, axis_y, deadzone, adapter.get(), range); |
| 241 | } | 243 | } |
| @@ -256,7 +258,7 @@ Common::ParamPackage GCAnalogFactory::GetNextInput() { | |||
| 256 | for (std::size_t port = 0; port < queue.size(); ++port) { | 258 | for (std::size_t port = 0; port < queue.size(); ++port) { |
| 257 | while (queue[port].Pop(pad)) { | 259 | while (queue[port].Pop(pad)) { |
| 258 | if (pad.axis == GCAdapter::PadAxes::Undefined || | 260 | if (pad.axis == GCAdapter::PadAxes::Undefined || |
| 259 | std::abs((pad.axis_value - 128.0f) / 128.0f) < 0.1) { | 261 | std::abs((static_cast<float>(pad.axis_value) - 128.0f) / 128.0f) < 0.1f) { |
| 260 | continue; | 262 | continue; |
| 261 | } | 263 | } |
| 262 | // An analog device needs two axes, so we need to store the axis for later and wait for | 264 | // An analog device needs two axes, so we need to store the axis for later and wait for |