summaryrefslogtreecommitdiff
path: root/src/input_common
diff options
context:
space:
mode:
authorGravatar Ameer2020-07-06 21:58:31 -0400
committerGravatar Ameer2020-07-06 21:58:31 -0400
commit7ad423923dddb5e037d54e70cb066b03f8346dec (patch)
tree5907a594637850d69e46fd26ae44da39264231d5 /src/input_common
parentMerge pull request #4194 from ReinUsesLisp/fix-shader-cache (diff)
downloadyuzu-7ad423923dddb5e037d54e70cb066b03f8346dec.tar.gz
yuzu-7ad423923dddb5e037d54e70cb066b03f8346dec.tar.xz
yuzu-7ad423923dddb5e037d54e70cb066b03f8346dec.zip
Save origin state of GC controller analog features, compare against origin for input detection
Diffstat (limited to 'src/input_common')
-rw-r--r--src/input_common/gcadapter/gc_adapter.cpp72
-rw-r--r--src/input_common/gcadapter/gc_adapter.h11
-rw-r--r--src/input_common/gcadapter/gc_poller.cpp17
3 files changed, 72 insertions, 28 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp
index b39d2a3fb..8465309d0 100644
--- a/src/input_common/gcadapter/gc_adapter.cpp
+++ b/src/input_common/gcadapter/gc_adapter.cpp
@@ -25,19 +25,15 @@ Adapter::Adapter() {
25 25
26 current_status = NO_ADAPTER_DETECTED; 26 current_status = NO_ADAPTER_DETECTED;
27 libusb_init(&libusb_ctx); 27 libusb_init(&libusb_ctx);
28 get_origin.fill(true);
28 29
29 StartScanThread(); 30 StartScanThread();
30} 31}
31 32
32GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) { 33GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) {
33 GCPadStatus pad = {}; 34 GCPadStatus pad = {};
34 bool get_origin = false;
35 35
36 ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4); 36 ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4);
37 if (type != ControllerTypes::None) {
38 get_origin = true;
39 }
40
41 adapter_controllers_status[port] = type; 37 adapter_controllers_status[port] = type;
42 38
43 static constexpr std::array<PadButton, 8> b1_buttons{ 39 static constexpr std::array<PadButton, 8> b1_buttons{
@@ -69,16 +65,22 @@ GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_pa
69 } 65 }
70 } 66 }
71 67
72 if (get_origin) {
73 pad.button |= PAD_GET_ORIGIN;
74 }
75
76 pad.stick_x = adapter_payload[1 + (9 * port) + 3]; 68 pad.stick_x = adapter_payload[1 + (9 * port) + 3];
77 pad.stick_y = adapter_payload[1 + (9 * port) + 4]; 69 pad.stick_y = adapter_payload[1 + (9 * port) + 4];
78 pad.substick_x = adapter_payload[1 + (9 * port) + 5]; 70 pad.substick_x = adapter_payload[1 + (9 * port) + 5];
79 pad.substick_y = adapter_payload[1 + (9 * port) + 6]; 71 pad.substick_y = adapter_payload[1 + (9 * port) + 6];
80 pad.trigger_left = adapter_payload[1 + (9 * port) + 7]; 72 pad.trigger_left = adapter_payload[1 + (9 * port) + 7];
81 pad.trigger_right = adapter_payload[1 + (9 * port) + 8]; 73 pad.trigger_right = adapter_payload[1 + (9 * port) + 8];
74
75 if (get_origin[port]) {
76 origin_status[port].stick_x = pad.stick_x;
77 origin_status[port].stick_y = pad.stick_y;
78 origin_status[port].substick_x = pad.substick_x;
79 origin_status[port].substick_y = pad.substick_y;
80 origin_status[port].trigger_left = pad.trigger_left;
81 origin_status[port].trigger_right = pad.trigger_right;
82 get_origin[port] = false;
83 }
82 } 84 }
83 return pad; 85 return pad;
84} 86}
@@ -127,31 +129,31 @@ void Adapter::Read() {
127 for (std::size_t port = 0; port < pads.size(); ++port) { 129 for (std::size_t port = 0; port < pads.size(); ++port) {
128 pads[port] = GetPadStatus(port, adapter_payload_copy); 130 pads[port] = GetPadStatus(port, adapter_payload_copy);
129 if (DeviceConnected(port) && configuring) { 131 if (DeviceConnected(port) && configuring) {
130 if (pads[port].button != PAD_GET_ORIGIN) { 132 if (pads[port].button != 0) {
131 pad_queue[port].Push(pads[port]); 133 pad_queue[port].Push(pads[port]);
132 } 134 }
133 135
134 // Accounting for a threshold here because of some controller variance 136 // Accounting for a threshold here because of some controller variance
135 if (pads[port].stick_x > pads[port].MAIN_STICK_CENTER_X + pads[port].THRESHOLD || 137 if (pads[port].stick_x > origin_status[port].stick_x + pads[port].THRESHOLD ||
136 pads[port].stick_x < pads[port].MAIN_STICK_CENTER_X - pads[port].THRESHOLD) { 138 pads[port].stick_x < origin_status[port].stick_x - pads[port].THRESHOLD) {
137 pads[port].axis = GCAdapter::PadAxes::StickX; 139 pads[port].axis = GCAdapter::PadAxes::StickX;
138 pads[port].axis_value = pads[port].stick_x; 140 pads[port].axis_value = pads[port].stick_x;
139 pad_queue[port].Push(pads[port]); 141 pad_queue[port].Push(pads[port]);
140 } 142 }
141 if (pads[port].stick_y > pads[port].MAIN_STICK_CENTER_Y + pads[port].THRESHOLD || 143 if (pads[port].stick_y > origin_status[port].stick_y + pads[port].THRESHOLD ||
142 pads[port].stick_y < pads[port].MAIN_STICK_CENTER_Y - pads[port].THRESHOLD) { 144 pads[port].stick_y < origin_status[port].stick_y - pads[port].THRESHOLD) {
143 pads[port].axis = GCAdapter::PadAxes::StickY; 145 pads[port].axis = GCAdapter::PadAxes::StickY;
144 pads[port].axis_value = pads[port].stick_y; 146 pads[port].axis_value = pads[port].stick_y;
145 pad_queue[port].Push(pads[port]); 147 pad_queue[port].Push(pads[port]);
146 } 148 }
147 if (pads[port].substick_x > pads[port].C_STICK_CENTER_X + pads[port].THRESHOLD || 149 if (pads[port].substick_x > origin_status[port].substick_x + pads[port].THRESHOLD ||
148 pads[port].substick_x < pads[port].C_STICK_CENTER_X - pads[port].THRESHOLD) { 150 pads[port].substick_x < origin_status[port].substick_x - pads[port].THRESHOLD) {
149 pads[port].axis = GCAdapter::PadAxes::SubstickX; 151 pads[port].axis = GCAdapter::PadAxes::SubstickX;
150 pads[port].axis_value = pads[port].substick_x; 152 pads[port].axis_value = pads[port].substick_x;
151 pad_queue[port].Push(pads[port]); 153 pad_queue[port].Push(pads[port]);
152 } 154 }
153 if (pads[port].substick_y > pads[port].C_STICK_CENTER_Y + pads[port].THRESHOLD || 155 if (pads[port].substick_y > origin_status[port].substick_y + pads[port].THRESHOLD ||
154 pads[port].substick_y < pads[port].C_STICK_CENTER_Y - pads[port].THRESHOLD) { 156 pads[port].substick_y < origin_status[port].substick_y - pads[port].THRESHOLD) {
155 pads[port].axis = GCAdapter::PadAxes::SubstickY; 157 pads[port].axis = GCAdapter::PadAxes::SubstickY;
156 pads[port].axis_value = pads[port].substick_y; 158 pads[port].axis_value = pads[port].substick_y;
157 pad_queue[port].Push(pads[port]); 159 pad_queue[port].Push(pads[port]);
@@ -325,6 +327,7 @@ void Adapter::Reset() {
325 adapter_input_thread.join(); 327 adapter_input_thread.join();
326 328
327 adapter_controllers_status.fill(ControllerTypes::None); 329 adapter_controllers_status.fill(ControllerTypes::None);
330 get_origin.fill(true);
328 current_status = NO_ADAPTER_DETECTED; 331 current_status = NO_ADAPTER_DETECTED;
329 332
330 if (usb_adapter_handle) { 333 if (usb_adapter_handle) {
@@ -347,6 +350,7 @@ void Adapter::ResetDeviceType(int port) {
347} 350}
348 351
349void Adapter::BeginConfiguration() { 352void Adapter::BeginConfiguration() {
353 get_origin.fill(true);
350 for (auto& pq : pad_queue) { 354 for (auto& pq : pad_queue) {
351 pq.Clear(); 355 pq.Clear();
352 } 356 }
@@ -376,4 +380,36 @@ const std::array<GCState, 4>& Adapter::GetPadState() const {
376 return state; 380 return state;
377} 381}
378 382
383int Adapter::GetOriginValue(int port, int axis) {
384 const PadAxes padaxis = static_cast<PadAxes>(axis);
385 if (padaxis == PadAxes::StickX)
386 return origin_status[port].stick_x;
387 if (padaxis == PadAxes::StickY)
388 return origin_status[port].stick_y;
389 if (padaxis == PadAxes::SubstickX)
390 return origin_status[port].substick_x;
391 if (padaxis == PadAxes::SubstickY)
392 return origin_status[port].substick_x;
393 if (padaxis == PadAxes::TriggerLeft)
394 return origin_status[port].trigger_left;
395 if (padaxis == PadAxes::TriggerRight)
396 return origin_status[port].trigger_right;
397}
398
399const int Adapter::GetOriginValue(int port, int axis) const {
400 const PadAxes padaxis = static_cast<PadAxes>(axis);
401 if (padaxis == PadAxes::StickX)
402 return origin_status[port].stick_x;
403 if (padaxis == PadAxes::StickY)
404 return origin_status[port].stick_y;
405 if (padaxis == PadAxes::SubstickX)
406 return origin_status[port].substick_x;
407 if (padaxis == PadAxes::SubstickY)
408 return origin_status[port].substick_x;
409 if (padaxis == PadAxes::TriggerLeft)
410 return origin_status[port].trigger_left;
411 if (padaxis == PadAxes::TriggerRight)
412 return origin_status[port].trigger_right;
413}
414
379} // namespace GCAdapter 415} // namespace GCAdapter
diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h
index 0ea6263eb..8b08d667d 100644
--- a/src/input_common/gcadapter/gc_adapter.h
+++ b/src/input_common/gcadapter/gc_adapter.h
@@ -13,12 +13,6 @@
13 13
14namespace GCAdapter { 14namespace GCAdapter {
15 15
16enum {
17 PAD_USE_ORIGIN = 0x0080,
18 PAD_GET_ORIGIN = 0x2000,
19 PAD_ERR_STATUS = 0x8000,
20};
21
22enum class PadButton { 16enum class PadButton {
23 PAD_BUTTON_LEFT = 0x0001, 17 PAD_BUTTON_LEFT = 0x0001,
24 PAD_BUTTON_RIGHT = 0x0002, 18 PAD_BUTTON_RIGHT = 0x0002,
@@ -102,6 +96,9 @@ public:
102 std::array<GCState, 4>& GetPadState(); 96 std::array<GCState, 4>& GetPadState();
103 const std::array<GCState, 4>& GetPadState() const; 97 const std::array<GCState, 4>& GetPadState() const;
104 98
99 int GetOriginValue(int port, int axis);
100 const int GetOriginValue(int port, int axis) const;
101
105private: 102private:
106 GCPadStatus GetPadStatus(int port, const std::array<u8, 37>& adapter_payload); 103 GCPadStatus GetPadStatus(int port, const std::array<u8, 37>& adapter_payload);
107 104
@@ -155,6 +152,8 @@ private:
155 152
156 std::array<Common::SPSCQueue<GCPadStatus>, 4> pad_queue; 153 std::array<Common::SPSCQueue<GCPadStatus>, 4> pad_queue;
157 std::array<GCState, 4> state; 154 std::array<GCState, 4> state;
155 std::array<bool, 4> get_origin;
156 std::array<GCPadStatus, 4> origin_status;
158}; 157};
159 158
160} // namespace GCAdapter 159} // namespace GCAdapter
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp
index 385ce8430..c9bb7e571 100644
--- a/src/input_common/gcadapter/gc_poller.cpp
+++ b/src/input_common/gcadapter/gc_poller.cpp
@@ -34,7 +34,7 @@ public:
34 explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, 34 explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_,
35 GCAdapter::Adapter* adapter) 35 GCAdapter::Adapter* adapter)
36 : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), 36 : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_),
37 gcadapter(adapter) { 37 gcadapter(adapter), origin_value(adapter->GetOriginValue(port_, axis_)) {
38 // L/R triggers range is only in positive direction beginning near 0 38 // L/R triggers range is only in positive direction beginning near 0
39 // 0.0 threshold equates to near half trigger press, but threshold accounts for variability. 39 // 0.0 threshold equates to near half trigger press, but threshold accounts for variability.
40 if (axis > 3) { 40 if (axis > 3) {
@@ -43,7 +43,8 @@ public:
43 } 43 }
44 44
45 bool GetStatus() const override { 45 bool GetStatus() const override {
46 const float axis_value = (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f; 46 const float current_axis_value = gcadapter->GetPadState()[port].axes.at(axis);
47 const float axis_value = (current_axis_value - origin_value) / 128.0f;
47 if (trigger_if_greater) { 48 if (trigger_if_greater) {
48 // TODO: Might be worthwile to set a slider for the trigger threshold. It is currently 49 // TODO: Might be worthwile to set a slider for the trigger threshold. It is currently
49 // always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick 50 // always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick
@@ -58,6 +59,7 @@ private:
58 float threshold; 59 float threshold;
59 bool trigger_if_greater; 60 bool trigger_if_greater;
60 GCAdapter::Adapter* gcadapter; 61 GCAdapter::Adapter* gcadapter;
62 const float origin_value;
61}; 63};
62 64
63GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_) 65GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
@@ -144,14 +146,19 @@ void GCButtonFactory::EndConfiguration() {
144class GCAnalog final : public Input::AnalogDevice { 146class GCAnalog final : public Input::AnalogDevice {
145public: 147public:
146 GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter) 148 GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter)
147 : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter) {} 149 : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter),
150 origin_value_x(adapter->GetOriginValue(port_, axis_x_)),
151 origin_value_y(adapter->GetOriginValue(port_, axis_y_)) {}
148 152
149 float GetAxis(int axis) const { 153 float GetAxis(int axis) const {
150 std::lock_guard lock{mutex}; 154 std::lock_guard lock{mutex};
151 // division is not by a perfect 128 to account for some variance in center location 155 // division is not by a perfect 128 to account for some variance in center location
152 // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range 156 // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range
153 // [20-230] 157 // [20-230]
154 return (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 95.0f; 158 if (axis % 2 == 0)
159 return (gcadapter->GetPadState()[port].axes.at(axis) - origin_value_x) / 95.0f;
160 else
161 return (gcadapter->GetPadState()[port].axes.at(axis) - origin_value_y) / 95.0f;
155 } 162 }
156 163
157 std::pair<float, float> GetAnalog(int axis_x, int axis_y) const { 164 std::pair<float, float> GetAnalog(int axis_x, int axis_y) const {
@@ -201,6 +208,8 @@ private:
201 const int axis_x; 208 const int axis_x;
202 const int axis_y; 209 const int axis_y;
203 const float deadzone; 210 const float deadzone;
211 const float origin_value_x;
212 const float origin_value_y;
204 mutable std::mutex mutex; 213 mutable std::mutex mutex;
205 GCAdapter::Adapter* gcadapter; 214 GCAdapter::Adapter* gcadapter;
206}; 215};