diff options
Diffstat (limited to 'src/input_common/gcadapter/gc_poller.cpp')
| -rw-r--r-- | src/input_common/gcadapter/gc_poller.cpp | 60 |
1 files changed, 37 insertions, 23 deletions
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index 385ce8430..96e22d3ad 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | #include <list> | 6 | #include <list> |
| 7 | #include <mutex> | 7 | #include <mutex> |
| 8 | #include <utility> | 8 | #include <utility> |
| 9 | #include "common/assert.h" | ||
| 9 | #include "common/threadsafe_queue.h" | 10 | #include "common/threadsafe_queue.h" |
| 10 | #include "input_common/gcadapter/gc_adapter.h" | 11 | #include "input_common/gcadapter/gc_adapter.h" |
| 11 | #include "input_common/gcadapter/gc_poller.h" | 12 | #include "input_common/gcadapter/gc_poller.h" |
| @@ -20,7 +21,10 @@ public: | |||
| 20 | ~GCButton() override; | 21 | ~GCButton() override; |
| 21 | 22 | ||
| 22 | bool GetStatus() const override { | 23 | bool GetStatus() const override { |
| 23 | return gcadapter->GetPadState()[port].buttons.at(button); | 24 | if (gcadapter->DeviceConnected(port)) { |
| 25 | return gcadapter->GetPadState()[port].buttons.at(button); | ||
| 26 | } | ||
| 27 | return false; | ||
| 24 | } | 28 | } |
| 25 | 29 | ||
| 26 | private: | 30 | private: |
| @@ -34,22 +38,20 @@ public: | |||
| 34 | 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_, |
| 35 | GCAdapter::Adapter* adapter) | 39 | GCAdapter::Adapter* adapter) |
| 36 | : 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_), |
| 37 | gcadapter(adapter) { | 41 | gcadapter(adapter), origin_value(adapter->GetOriginValue(port_, axis_)) {} |
| 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. | ||
| 40 | if (axis > 3) { | ||
| 41 | threshold *= -0.5; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | 42 | ||
| 45 | bool GetStatus() const override { | 43 | bool GetStatus() const override { |
| 46 | const float axis_value = (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f; | 44 | if (gcadapter->DeviceConnected(port)) { |
| 47 | if (trigger_if_greater) { | 45 | const float current_axis_value = gcadapter->GetPadState()[port].axes.at(axis); |
| 48 | // TODO: Might be worthwile to set a slider for the trigger threshold. It is currently | 46 | const float axis_value = (current_axis_value - origin_value) / 128.0f; |
| 49 | // always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick | 47 | if (trigger_if_greater) { |
| 50 | return axis_value > threshold; | 48 | // TODO: Might be worthwile to set a slider for the trigger threshold. It is |
| 49 | // currently always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick | ||
| 50 | return axis_value > threshold; | ||
| 51 | } | ||
| 52 | return axis_value < -threshold; | ||
| 51 | } | 53 | } |
| 52 | return axis_value < -threshold; | 54 | return false; |
| 53 | } | 55 | } |
| 54 | 56 | ||
| 55 | private: | 57 | private: |
| @@ -58,6 +60,7 @@ private: | |||
| 58 | float threshold; | 60 | float threshold; |
| 59 | bool trigger_if_greater; | 61 | bool trigger_if_greater; |
| 60 | GCAdapter::Adapter* gcadapter; | 62 | GCAdapter::Adapter* gcadapter; |
| 63 | const float origin_value; | ||
| 61 | }; | 64 | }; |
| 62 | 65 | ||
| 63 | GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_) | 66 | GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_) |
| @@ -94,9 +97,12 @@ std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::Param | |||
| 94 | return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater, | 97 | return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater, |
| 95 | adapter.get()); | 98 | adapter.get()); |
| 96 | } | 99 | } |
| 100 | |||
| 101 | UNREACHABLE(); | ||
| 102 | return nullptr; | ||
| 97 | } | 103 | } |
| 98 | 104 | ||
| 99 | Common::ParamPackage GCButtonFactory::GetNextInput() { | 105 | Common::ParamPackage GCButtonFactory::GetNextInput() const { |
| 100 | Common::ParamPackage params; | 106 | Common::ParamPackage params; |
| 101 | GCAdapter::GCPadStatus pad; | 107 | GCAdapter::GCPadStatus pad; |
| 102 | auto& queue = adapter->GetPadQueue(); | 108 | auto& queue = adapter->GetPadQueue(); |
| @@ -144,14 +150,20 @@ void GCButtonFactory::EndConfiguration() { | |||
| 144 | class GCAnalog final : public Input::AnalogDevice { | 150 | class GCAnalog final : public Input::AnalogDevice { |
| 145 | public: | 151 | public: |
| 146 | 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) |
| 147 | : 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_)) {} | ||
| 148 | 156 | ||
| 149 | float GetAxis(int axis) const { | 157 | float GetAxis(int axis) const { |
| 150 | std::lock_guard lock{mutex}; | 158 | if (gcadapter->DeviceConnected(port)) { |
| 151 | // division is not by a perfect 128 to account for some variance in center location | 159 | std::lock_guard lock{mutex}; |
| 152 | // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range | 160 | const auto origin_value = axis % 2 == 0 ? origin_value_x : origin_value_y; |
| 153 | // [20-230] | 161 | // division is not by a perfect 128 to account for some variance in center location |
| 154 | return (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 95.0f; | 162 | // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range |
| 163 | // [20-230] | ||
| 164 | return (gcadapter->GetPadState()[port].axes.at(axis) - origin_value) / 95.0f; | ||
| 165 | } | ||
| 166 | return 0.0f; | ||
| 155 | } | 167 | } |
| 156 | 168 | ||
| 157 | std::pair<float, float> GetAnalog(int axis_x, int axis_y) const { | 169 | std::pair<float, float> GetAnalog(int axis_x, int axis_y) const { |
| @@ -201,8 +213,10 @@ private: | |||
| 201 | const int axis_x; | 213 | const int axis_x; |
| 202 | const int axis_y; | 214 | const int axis_y; |
| 203 | const float deadzone; | 215 | const float deadzone; |
| 204 | mutable std::mutex mutex; | ||
| 205 | GCAdapter::Adapter* gcadapter; | 216 | GCAdapter::Adapter* gcadapter; |
| 217 | const float origin_value_x; | ||
| 218 | const float origin_value_y; | ||
| 219 | mutable std::mutex mutex; | ||
| 206 | }; | 220 | }; |
| 207 | 221 | ||
| 208 | /// An analog device factory that creates analog devices from GC Adapter | 222 | /// An analog device factory that creates analog devices from GC Adapter |
| @@ -249,7 +263,7 @@ Common::ParamPackage GCAnalogFactory::GetNextInput() { | |||
| 249 | const u8 axis = static_cast<u8>(pad.axis); | 263 | const u8 axis = static_cast<u8>(pad.axis); |
| 250 | if (analog_x_axis == -1) { | 264 | if (analog_x_axis == -1) { |
| 251 | analog_x_axis = axis; | 265 | analog_x_axis = axis; |
| 252 | controller_number = port; | 266 | controller_number = static_cast<int>(port); |
| 253 | } else if (analog_y_axis == -1 && analog_x_axis != axis && controller_number == port) { | 267 | } else if (analog_y_axis == -1 && analog_x_axis != axis && controller_number == port) { |
| 254 | analog_y_axis = axis; | 268 | analog_y_axis = axis; |
| 255 | } | 269 | } |