summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/input_common/gcadapter/gc_adapter.cpp69
-rw-r--r--src/input_common/gcadapter/gc_adapter.h10
-rw-r--r--src/input_common/gcadapter/gc_poller.cpp24
3 files changed, 67 insertions, 36 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp
index 38210ffcb..898a278a9 100644
--- a/src/input_common/gcadapter/gc_adapter.cpp
+++ b/src/input_common/gcadapter/gc_adapter.cpp
@@ -25,6 +25,7 @@ Adapter::Adapter() {
25 LOG_INFO(Input, "GC Adapter Initialization started"); 25 LOG_INFO(Input, "GC Adapter Initialization started");
26 26
27 current_status = NO_ADAPTER_DETECTED; 27 current_status = NO_ADAPTER_DETECTED;
28 get_origin.fill(true);
28 29
29 const int init_res = libusb_init(&libusb_ctx); 30 const int init_res = libusb_init(&libusb_ctx);
30 if (init_res == LIBUSB_SUCCESS) { 31 if (init_res == LIBUSB_SUCCESS) {
@@ -36,13 +37,8 @@ Adapter::Adapter() {
36 37
37GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload) { 38GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload) {
38 GCPadStatus pad = {}; 39 GCPadStatus pad = {};
39 bool get_origin = false;
40 40
41 ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4); 41 ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4);
42 if (type != ControllerTypes::None) {
43 get_origin = true;
44 }
45
46 adapter_controllers_status[port] = type; 42 adapter_controllers_status[port] = type;
47 43
48 static constexpr std::array<PadButton, 8> b1_buttons{ 44 static constexpr std::array<PadButton, 8> b1_buttons{
@@ -58,6 +54,11 @@ GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& ad
58 PadButton::PAD_TRIGGER_L, 54 PadButton::PAD_TRIGGER_L,
59 }; 55 };
60 56
57 if (adapter_controllers_status[port] == ControllerTypes::None && !get_origin[port]) {
58 // Controller may have been disconnected, recalibrate if reconnected.
59 get_origin[port] = true;
60 }
61
61 if (adapter_controllers_status[port] != ControllerTypes::None) { 62 if (adapter_controllers_status[port] != ControllerTypes::None) {
62 const u8 b1 = adapter_payload[1 + (9 * port) + 1]; 63 const u8 b1 = adapter_payload[1 + (9 * port) + 1];
63 const u8 b2 = adapter_payload[1 + (9 * port) + 2]; 64 const u8 b2 = adapter_payload[1 + (9 * port) + 2];
@@ -74,16 +75,22 @@ GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& ad
74 } 75 }
75 } 76 }
76 77
77 if (get_origin) {
78 pad.button |= PAD_GET_ORIGIN;
79 }
80
81 pad.stick_x = adapter_payload[1 + (9 * port) + 3]; 78 pad.stick_x = adapter_payload[1 + (9 * port) + 3];
82 pad.stick_y = adapter_payload[1 + (9 * port) + 4]; 79 pad.stick_y = adapter_payload[1 + (9 * port) + 4];
83 pad.substick_x = adapter_payload[1 + (9 * port) + 5]; 80 pad.substick_x = adapter_payload[1 + (9 * port) + 5];
84 pad.substick_y = adapter_payload[1 + (9 * port) + 6]; 81 pad.substick_y = adapter_payload[1 + (9 * port) + 6];
85 pad.trigger_left = adapter_payload[1 + (9 * port) + 7]; 82 pad.trigger_left = adapter_payload[1 + (9 * port) + 7];
86 pad.trigger_right = adapter_payload[1 + (9 * port) + 8]; 83 pad.trigger_right = adapter_payload[1 + (9 * port) + 8];
84
85 if (get_origin[port]) {
86 origin_status[port].stick_x = pad.stick_x;
87 origin_status[port].stick_y = pad.stick_y;
88 origin_status[port].substick_x = pad.substick_x;
89 origin_status[port].substick_y = pad.substick_y;
90 origin_status[port].trigger_left = pad.trigger_left;
91 origin_status[port].trigger_right = pad.trigger_right;
92 get_origin[port] = false;
93 }
87 } 94 }
88 return pad; 95 return pad;
89} 96}
@@ -132,31 +139,31 @@ void Adapter::Read() {
132 for (std::size_t port = 0; port < pads.size(); ++port) { 139 for (std::size_t port = 0; port < pads.size(); ++port) {
133 pads[port] = GetPadStatus(port, adapter_payload_copy); 140 pads[port] = GetPadStatus(port, adapter_payload_copy);
134 if (DeviceConnected(port) && configuring) { 141 if (DeviceConnected(port) && configuring) {
135 if (pads[port].button != PAD_GET_ORIGIN) { 142 if (pads[port].button != 0) {
136 pad_queue[port].Push(pads[port]); 143 pad_queue[port].Push(pads[port]);
137 } 144 }
138 145
139 // Accounting for a threshold here because of some controller variance 146 // Accounting for a threshold here because of some controller variance
140 if (pads[port].stick_x > pads[port].MAIN_STICK_CENTER_X + pads[port].THRESHOLD || 147 if (pads[port].stick_x > origin_status[port].stick_x + pads[port].THRESHOLD ||
141 pads[port].stick_x < pads[port].MAIN_STICK_CENTER_X - pads[port].THRESHOLD) { 148 pads[port].stick_x < origin_status[port].stick_x - pads[port].THRESHOLD) {
142 pads[port].axis = GCAdapter::PadAxes::StickX; 149 pads[port].axis = GCAdapter::PadAxes::StickX;
143 pads[port].axis_value = pads[port].stick_x; 150 pads[port].axis_value = pads[port].stick_x;
144 pad_queue[port].Push(pads[port]); 151 pad_queue[port].Push(pads[port]);
145 } 152 }
146 if (pads[port].stick_y > pads[port].MAIN_STICK_CENTER_Y + pads[port].THRESHOLD || 153 if (pads[port].stick_y > origin_status[port].stick_y + pads[port].THRESHOLD ||
147 pads[port].stick_y < pads[port].MAIN_STICK_CENTER_Y - pads[port].THRESHOLD) { 154 pads[port].stick_y < origin_status[port].stick_y - pads[port].THRESHOLD) {
148 pads[port].axis = GCAdapter::PadAxes::StickY; 155 pads[port].axis = GCAdapter::PadAxes::StickY;
149 pads[port].axis_value = pads[port].stick_y; 156 pads[port].axis_value = pads[port].stick_y;
150 pad_queue[port].Push(pads[port]); 157 pad_queue[port].Push(pads[port]);
151 } 158 }
152 if (pads[port].substick_x > pads[port].C_STICK_CENTER_X + pads[port].THRESHOLD || 159 if (pads[port].substick_x > origin_status[port].substick_x + pads[port].THRESHOLD ||
153 pads[port].substick_x < pads[port].C_STICK_CENTER_X - pads[port].THRESHOLD) { 160 pads[port].substick_x < origin_status[port].substick_x - pads[port].THRESHOLD) {
154 pads[port].axis = GCAdapter::PadAxes::SubstickX; 161 pads[port].axis = GCAdapter::PadAxes::SubstickX;
155 pads[port].axis_value = pads[port].substick_x; 162 pads[port].axis_value = pads[port].substick_x;
156 pad_queue[port].Push(pads[port]); 163 pad_queue[port].Push(pads[port]);
157 } 164 }
158 if (pads[port].substick_y > pads[port].C_STICK_CENTER_Y + pads[port].THRESHOLD || 165 if (pads[port].substick_y > origin_status[port].substick_y + pads[port].THRESHOLD ||
159 pads[port].substick_y < pads[port].C_STICK_CENTER_Y - pads[port].THRESHOLD) { 166 pads[port].substick_y < origin_status[port].substick_y - pads[port].THRESHOLD) {
160 pads[port].axis = GCAdapter::PadAxes::SubstickY; 167 pads[port].axis = GCAdapter::PadAxes::SubstickY;
161 pads[port].axis_value = pads[port].substick_y; 168 pads[port].axis_value = pads[port].substick_y;
162 pad_queue[port].Push(pads[port]); 169 pad_queue[port].Push(pads[port]);
@@ -237,6 +244,9 @@ void Adapter::Setup() {
237 } 244 }
238 libusb_free_device_list(devices, 1); 245 libusb_free_device_list(devices, 1);
239 } 246 }
247 // Break out of the ScanThreadFunc() loop that is constantly looking for the device
248 // Assumes user has GC adapter plugged in before launch to use the adapter
249 detect_thread_running = false;
240} 250}
241 251
242bool Adapter::CheckDeviceAccess(libusb_device* device) { 252bool Adapter::CheckDeviceAccess(libusb_device* device) {
@@ -345,6 +355,7 @@ void Adapter::Reset() {
345 adapter_input_thread.join(); 355 adapter_input_thread.join();
346 356
347 adapter_controllers_status.fill(ControllerTypes::None); 357 adapter_controllers_status.fill(ControllerTypes::None);
358 get_origin.fill(true);
348 current_status = NO_ADAPTER_DETECTED; 359 current_status = NO_ADAPTER_DETECTED;
349 360
350 if (usb_adapter_handle) { 361 if (usb_adapter_handle) {
@@ -367,6 +378,7 @@ void Adapter::ResetDeviceType(std::size_t port) {
367} 378}
368 379
369void Adapter::BeginConfiguration() { 380void Adapter::BeginConfiguration() {
381 get_origin.fill(true);
370 for (auto& pq : pad_queue) { 382 for (auto& pq : pad_queue) {
371 pq.Clear(); 383 pq.Clear();
372 } 384 }
@@ -396,4 +408,25 @@ const std::array<GCState, 4>& Adapter::GetPadState() const {
396 return state; 408 return state;
397} 409}
398 410
411int Adapter::GetOriginValue(int port, int axis) const {
412 const auto& status = origin_status[port];
413
414 switch (static_cast<PadAxes>(axis)) {
415 case PadAxes::StickX:
416 return status.stick_x;
417 case PadAxes::StickY:
418 return status.stick_y;
419 case PadAxes::SubstickX:
420 return status.substick_x;
421 case PadAxes::SubstickY:
422 return status.substick_y;
423 case PadAxes::TriggerLeft:
424 return status.trigger_left;
425 case PadAxes::TriggerRight:
426 return status.trigger_right;
427 default:
428 return 0;
429 }
430}
431
399} // namespace GCAdapter 432} // namespace GCAdapter
diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h
index e2cdd6255..3586c8bda 100644
--- a/src/input_common/gcadapter/gc_adapter.h
+++ b/src/input_common/gcadapter/gc_adapter.h
@@ -17,12 +17,6 @@ struct libusb_device_handle;
17 17
18namespace GCAdapter { 18namespace GCAdapter {
19 19
20enum {
21 PAD_USE_ORIGIN = 0x0080,
22 PAD_GET_ORIGIN = 0x2000,
23 PAD_ERR_STATUS = 0x8000,
24};
25
26enum class PadButton { 20enum class PadButton {
27 PAD_BUTTON_LEFT = 0x0001, 21 PAD_BUTTON_LEFT = 0x0001,
28 PAD_BUTTON_RIGHT = 0x0002, 22 PAD_BUTTON_RIGHT = 0x0002,
@@ -109,6 +103,8 @@ public:
109 std::array<GCState, 4>& GetPadState(); 103 std::array<GCState, 4>& GetPadState();
110 const std::array<GCState, 4>& GetPadState() const; 104 const std::array<GCState, 4>& GetPadState() const;
111 105
106 int GetOriginValue(int port, int axis) const;
107
112private: 108private:
113 GCPadStatus GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload); 109 GCPadStatus GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload);
114 110
@@ -159,6 +155,8 @@ private:
159 155
160 std::array<Common::SPSCQueue<GCPadStatus>, 4> pad_queue; 156 std::array<Common::SPSCQueue<GCPadStatus>, 4> pad_queue;
161 std::array<GCState, 4> state; 157 std::array<GCState, 4> state;
158 std::array<bool, 4> get_origin;
159 std::array<GCPadStatus, 4> origin_status;
162}; 160};
163 161
164} // namespace GCAdapter 162} // namespace GCAdapter
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp
index b20419ec3..96e22d3ad 100644
--- a/src/input_common/gcadapter/gc_poller.cpp
+++ b/src/input_common/gcadapter/gc_poller.cpp
@@ -38,18 +38,12 @@ public:
38 explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, 38 explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_,
39 GCAdapter::Adapter* adapter) 39 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), origin_value(adapter->GetOriginValue(port_, axis_)) {}
42 // L/R triggers range is only in positive direction beginning near 0
43 // 0.0 threshold equates to near half trigger press, but threshold accounts for variability.
44 if (axis > 3) {
45 threshold *= -0.5;
46 }
47 }
48 42
49 bool GetStatus() const override { 43 bool GetStatus() const override {
50 if (gcadapter->DeviceConnected(port)) { 44 if (gcadapter->DeviceConnected(port)) {
51 const float axis_value = 45 const float current_axis_value = gcadapter->GetPadState()[port].axes.at(axis);
52 (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f; 46 const float axis_value = (current_axis_value - origin_value) / 128.0f;
53 if (trigger_if_greater) { 47 if (trigger_if_greater) {
54 // TODO: Might be worthwile to set a slider for the trigger threshold. It is 48 // TODO: Might be worthwile to set a slider for the trigger threshold. It is
55 // currently always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick 49 // currently always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick
@@ -66,6 +60,7 @@ private:
66 float threshold; 60 float threshold;
67 bool trigger_if_greater; 61 bool trigger_if_greater;
68 GCAdapter::Adapter* gcadapter; 62 GCAdapter::Adapter* gcadapter;
63 const float origin_value;
69}; 64};
70 65
71GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_) 66GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
@@ -155,15 +150,18 @@ void GCButtonFactory::EndConfiguration() {
155class GCAnalog final : public Input::AnalogDevice { 150class GCAnalog final : public Input::AnalogDevice {
156public: 151public:
157 GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter) 152 GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter)
158 : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {} 153 : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter),
154 origin_value_x(adapter->GetOriginValue(port_, axis_x_)),
155 origin_value_y(adapter->GetOriginValue(port_, axis_y_)) {}
159 156
160 float GetAxis(int axis) const { 157 float GetAxis(int axis) const {
161 if (gcadapter->DeviceConnected(port)) { 158 if (gcadapter->DeviceConnected(port)) {
162 std::lock_guard lock{mutex}; 159 std::lock_guard lock{mutex};
160 const auto origin_value = axis % 2 == 0 ? origin_value_x : origin_value_y;
163 // division is not by a perfect 128 to account for some variance in center location 161 // division is not by a perfect 128 to account for some variance in center location
164 // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range 162 // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range
165 // [20-230] 163 // [20-230]
166 return (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 95.0f; 164 return (gcadapter->GetPadState()[port].axes.at(axis) - origin_value) / 95.0f;
167 } 165 }
168 return 0.0f; 166 return 0.0f;
169 } 167 }
@@ -215,8 +213,10 @@ private:
215 const int axis_x; 213 const int axis_x;
216 const int axis_y; 214 const int axis_y;
217 const float deadzone; 215 const float deadzone;
218 mutable std::mutex mutex;
219 GCAdapter::Adapter* gcadapter; 216 GCAdapter::Adapter* gcadapter;
217 const float origin_value_x;
218 const float origin_value_y;
219 mutable std::mutex mutex;
220}; 220};
221 221
222/// An analog device factory that creates analog devices from GC Adapter 222/// An analog device factory that creates analog devices from GC Adapter