diff options
Diffstat (limited to 'src/input_common/gcadapter/gc_poller.cpp')
| -rw-r--r-- | src/input_common/gcadapter/gc_poller.cpp | 310 |
1 files changed, 310 insertions, 0 deletions
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp new file mode 100644 index 000000000..772bd8890 --- /dev/null +++ b/src/input_common/gcadapter/gc_poller.cpp | |||
| @@ -0,0 +1,310 @@ | |||
| 1 | #include <atomic> | ||
| 2 | #include <list> | ||
| 3 | #include <mutex> | ||
| 4 | #include <utility> | ||
| 5 | #include "input_common/gcadapter/gc_poller.h" | ||
| 6 | #include "input_common/gcadapter/gc_adapter.h" | ||
| 7 | #include "common/threadsafe_queue.h" | ||
| 8 | |||
| 9 | // Using extern as to avoid multply defined symbols. | ||
| 10 | extern Common::SPSCQueue<GCPadStatus> pad_queue[4]; | ||
| 11 | extern struct GCState state[4]; | ||
| 12 | |||
| 13 | namespace InputCommon { | ||
| 14 | |||
| 15 | class GCButton final : public Input::ButtonDevice { | ||
| 16 | public: | ||
| 17 | explicit GCButton(int port_, int button_, int axis_) | ||
| 18 | : port(port_), button(button_) { | ||
| 19 | } | ||
| 20 | |||
| 21 | ~GCButton() override; | ||
| 22 | |||
| 23 | bool GetStatus() const override { | ||
| 24 | return state[port].buttons.at(button); | ||
| 25 | } | ||
| 26 | |||
| 27 | private: | ||
| 28 | const int port; | ||
| 29 | const int button; | ||
| 30 | }; | ||
| 31 | |||
| 32 | class GCAxisButton final : public Input::ButtonDevice { | ||
| 33 | public: | ||
| 34 | explicit GCAxisButton(int port_, int axis_, float threshold_, | ||
| 35 | bool trigger_if_greater_) | ||
| 36 | : port(port_), axis(axis_), threshold(threshold_), | ||
| 37 | trigger_if_greater(trigger_if_greater_) { | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | bool GetStatus() const override { | ||
| 42 | const float axis_value = (state[port].axes.at(axis) - 128.0f) / 128.0f; | ||
| 43 | if (trigger_if_greater) { | ||
| 44 | return axis_value > 0.10f; //TODO(ameerj) : Fix threshold. | ||
| 45 | } | ||
| 46 | return axis_value < -0.10f; | ||
| 47 | } | ||
| 48 | |||
| 49 | private: | ||
| 50 | const int port; | ||
| 51 | const int axis; | ||
| 52 | float threshold; | ||
| 53 | bool trigger_if_greater; | ||
| 54 | }; | ||
| 55 | |||
| 56 | GCButtonFactory::GCButtonFactory() { | ||
| 57 | GCAdapter::Init(); | ||
| 58 | } | ||
| 59 | |||
| 60 | GCButton::~GCButton() { | ||
| 61 | GCAdapter::Shutdown(); | ||
| 62 | } | ||
| 63 | |||
| 64 | std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) { | ||
| 65 | int button_id = params.Get("button", 0); | ||
| 66 | int port = params.Get("port", 0); | ||
| 67 | // For Axis buttons, used by the binary sticks. | ||
| 68 | if (params.Has("axis")) { | ||
| 69 | const int axis = params.Get("axis", 0); | ||
| 70 | const float threshold = params.Get("threshold", 0.5f); | ||
| 71 | const std::string direction_name = params.Get("direction", ""); | ||
| 72 | bool trigger_if_greater; | ||
| 73 | if (direction_name == "+") { | ||
| 74 | trigger_if_greater = true; | ||
| 75 | } else if (direction_name == "-") { | ||
| 76 | trigger_if_greater = false; | ||
| 77 | } else { | ||
| 78 | trigger_if_greater = true; | ||
| 79 | LOG_ERROR(Input, "Unknown direction {}", direction_name); | ||
| 80 | } | ||
| 81 | return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater); | ||
| 82 | } | ||
| 83 | |||
| 84 | std::unique_ptr<GCButton> button = | ||
| 85 | std::make_unique<GCButton>(port, button_id, params.Get("axis", 0)); | ||
| 86 | return std::move(button); | ||
| 87 | } | ||
| 88 | |||
| 89 | Common::ParamPackage GCButtonFactory::GetNextInput() { | ||
| 90 | Common::ParamPackage params; | ||
| 91 | GCPadStatus pad; | ||
| 92 | for (int i = 0; i < 4; i++) { | ||
| 93 | while (pad_queue[i].Pop(pad)) { | ||
| 94 | // This while loop will break on the earliest detected button | ||
| 95 | params.Set("engine", "gcpad"); | ||
| 96 | params.Set("port", i); | ||
| 97 | // I was debating whether to keep these verbose for ease of reading | ||
| 98 | // or to use a while loop shifting the bits to test and set the value. | ||
| 99 | if (pad.button & PAD_BUTTON_A) { | ||
| 100 | params.Set("button", PAD_BUTTON_A); | ||
| 101 | break; | ||
| 102 | } | ||
| 103 | if (pad.button & PAD_BUTTON_B) { | ||
| 104 | params.Set("button", PAD_BUTTON_B); | ||
| 105 | break; | ||
| 106 | } | ||
| 107 | if (pad.button & PAD_BUTTON_X) { | ||
| 108 | params.Set("button", PAD_BUTTON_X); | ||
| 109 | break; | ||
| 110 | } | ||
| 111 | if (pad.button & PAD_BUTTON_Y) { | ||
| 112 | params.Set("button", PAD_BUTTON_Y); | ||
| 113 | break; | ||
| 114 | } | ||
| 115 | if (pad.button & PAD_BUTTON_DOWN) { | ||
| 116 | params.Set("button", PAD_BUTTON_DOWN); | ||
| 117 | break; | ||
| 118 | } | ||
| 119 | if (pad.button & PAD_BUTTON_LEFT) { | ||
| 120 | params.Set("button", PAD_BUTTON_LEFT); | ||
| 121 | break; | ||
| 122 | } | ||
| 123 | if (pad.button & PAD_BUTTON_RIGHT) { | ||
| 124 | params.Set("button", PAD_BUTTON_RIGHT); | ||
| 125 | break; | ||
| 126 | } | ||
| 127 | if (pad.button & PAD_BUTTON_UP) { | ||
| 128 | params.Set("button", PAD_BUTTON_UP); | ||
| 129 | break; | ||
| 130 | } | ||
| 131 | if (pad.button & PAD_TRIGGER_L) { | ||
| 132 | params.Set("button", PAD_TRIGGER_L); | ||
| 133 | break; | ||
| 134 | } | ||
| 135 | if (pad.button & PAD_TRIGGER_R) { | ||
| 136 | params.Set("button", PAD_TRIGGER_R); | ||
| 137 | break; | ||
| 138 | } | ||
| 139 | if (pad.button & PAD_TRIGGER_Z) { | ||
| 140 | params.Set("button", PAD_TRIGGER_Z); | ||
| 141 | break; | ||
| 142 | } | ||
| 143 | if (pad.button & PAD_BUTTON_START) { | ||
| 144 | params.Set("button", PAD_BUTTON_START); | ||
| 145 | break; | ||
| 146 | } | ||
| 147 | // For Axis button implementation | ||
| 148 | if (pad.axis_which != 255) { | ||
| 149 | params.Set("axis", pad.axis_which); | ||
| 150 | params.Set("button", PAD_STICK); | ||
| 151 | if (pad.axis_value > 128) { | ||
| 152 | params.Set("direction", "+"); | ||
| 153 | params.Set("threshold", "0.5"); | ||
| 154 | } else { | ||
| 155 | params.Set("direction", "-"); | ||
| 156 | params.Set("threshold", "-0.5"); | ||
| 157 | } | ||
| 158 | break; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | return params; | ||
| 163 | } | ||
| 164 | |||
| 165 | void GCButtonFactory::BeginConfiguration() { | ||
| 166 | polling = true; | ||
| 167 | for (int i = 0; i < 4; i++) | ||
| 168 | pad_queue[i].Clear(); | ||
| 169 | GCAdapter::BeginConfiguration(); | ||
| 170 | } | ||
| 171 | |||
| 172 | void GCButtonFactory::EndConfiguration() { | ||
| 173 | polling = false; | ||
| 174 | |||
| 175 | for (int i = 0; i < 4; i++) | ||
| 176 | pad_queue[i].Clear(); | ||
| 177 | GCAdapter::EndConfiguration(); | ||
| 178 | } | ||
| 179 | |||
| 180 | class GCAnalog final : public Input::AnalogDevice { | ||
| 181 | public: | ||
| 182 | GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_) | ||
| 183 | : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) { | ||
| 184 | } | ||
| 185 | |||
| 186 | float GetAxis(int axis) const { | ||
| 187 | std::lock_guard lock{mutex}; | ||
| 188 | // division is not by a perfect 128 to account for some variance in center location | ||
| 189 | // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range [20-230] | ||
| 190 | return (state[port].axes.at(axis) - 128.0f) / 95.0f; | ||
| 191 | } | ||
| 192 | |||
| 193 | std::tuple<float, float> GetAnalog(int axis_x, int axis_y) const { | ||
| 194 | float x = GetAxis(axis_x); | ||
| 195 | float y = GetAxis(axis_y); | ||
| 196 | |||
| 197 | // Make sure the coordinates are in the unit circle, | ||
| 198 | // otherwise normalize it. | ||
| 199 | float r = x * x + y * y; | ||
| 200 | if (r > 1.0f) { | ||
| 201 | r = std::sqrt(r); | ||
| 202 | x /= r; | ||
| 203 | y /= r; | ||
| 204 | } | ||
| 205 | |||
| 206 | return std::make_tuple(x, y); | ||
| 207 | } | ||
| 208 | |||
| 209 | std::tuple<float, float> GetStatus() const override { | ||
| 210 | const auto [x, y] = GetAnalog(axis_x, axis_y); | ||
| 211 | const float r = std::sqrt((x * x) + (y * y)); | ||
| 212 | if (r > deadzone) { | ||
| 213 | return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), | ||
| 214 | y / r * (r - deadzone) / (1 - deadzone)); | ||
| 215 | } | ||
| 216 | return std::make_tuple<float, float>(0.0f, 0.0f); | ||
| 217 | } | ||
| 218 | |||
| 219 | bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { | ||
| 220 | const auto [x, y] = GetStatus(); | ||
| 221 | const float directional_deadzone = 0.4f; | ||
| 222 | switch (direction) { | ||
| 223 | case Input::AnalogDirection::RIGHT: | ||
| 224 | return x > directional_deadzone; | ||
| 225 | case Input::AnalogDirection::LEFT: | ||
| 226 | return x < -directional_deadzone; | ||
| 227 | case Input::AnalogDirection::UP: | ||
| 228 | return y > directional_deadzone; | ||
| 229 | case Input::AnalogDirection::DOWN: | ||
| 230 | return y < -directional_deadzone; | ||
| 231 | } | ||
| 232 | return false; | ||
| 233 | } | ||
| 234 | |||
| 235 | private: | ||
| 236 | const int port; | ||
| 237 | const int axis_x; | ||
| 238 | const int axis_y; | ||
| 239 | const float deadzone; | ||
| 240 | mutable std::mutex mutex; | ||
| 241 | }; | ||
| 242 | |||
| 243 | |||
| 244 | /// An analog device factory that creates analog devices from GC Adapter | ||
| 245 | GCAnalogFactory::GCAnalogFactory() {}; | ||
| 246 | |||
| 247 | |||
| 248 | /** | ||
| 249 | * Creates analog device from joystick axes | ||
| 250 | * @param params contains parameters for creating the device: | ||
| 251 | * - "port": the nth gcpad on the adapter | ||
| 252 | * - "axis_x": the index of the axis to be bind as x-axis | ||
| 253 | * - "axis_y": the index of the axis to be bind as y-axis | ||
| 254 | */ | ||
| 255 | std::unique_ptr<Input::AnalogDevice> GCAnalogFactory::Create(const Common::ParamPackage& params) { | ||
| 256 | const std::string guid = params.Get("guid", "0"); | ||
| 257 | const int port = params.Get("port", 0); | ||
| 258 | const int axis_x = params.Get("axis_x", 0); | ||
| 259 | const int axis_y = params.Get("axis_y", 1); | ||
| 260 | const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); | ||
| 261 | |||
| 262 | return std::make_unique<GCAnalog>(port, axis_x, axis_y, deadzone); | ||
| 263 | } | ||
| 264 | |||
| 265 | void GCAnalogFactory::BeginConfiguration() { | ||
| 266 | polling = true; | ||
| 267 | for (int i = 0; i < 4; i++) | ||
| 268 | pad_queue[i].Clear(); | ||
| 269 | GCAdapter::BeginConfiguration(); | ||
| 270 | } | ||
| 271 | |||
| 272 | void GCAnalogFactory::EndConfiguration() { | ||
| 273 | polling = false; | ||
| 274 | for (int i = 0; i < 4; i++) | ||
| 275 | pad_queue[i].Clear(); | ||
| 276 | GCAdapter::EndConfiguration(); | ||
| 277 | } | ||
| 278 | |||
| 279 | Common::ParamPackage GCAnalogFactory::GetNextInput() { | ||
| 280 | GCPadStatus pad; | ||
| 281 | for (int i = 0; i < 4; i++) { | ||
| 282 | while (pad_queue[i].Pop(pad)) { | ||
| 283 | if (pad.axis_which == 255 || std::abs((pad.axis_value - 128.0f) / 128.0f) < 0.1) { | ||
| 284 | continue; | ||
| 285 | } | ||
| 286 | // An analog device needs two axes, so we need to store the axis for later and wait for | ||
| 287 | // a second SDL event. The axes also must be from the same joystick. | ||
| 288 | const int axis = pad.axis_which; | ||
| 289 | if (analog_x_axis == -1) { | ||
| 290 | analog_x_axis = axis; | ||
| 291 | controller_number = i; | ||
| 292 | } else if (analog_y_axis == -1 && analog_x_axis != axis && controller_number == i) { | ||
| 293 | analog_y_axis = axis; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | } | ||
| 297 | Common::ParamPackage params; | ||
| 298 | if (analog_x_axis != -1 && analog_y_axis != -1) { | ||
| 299 | params.Set("engine", "gcpad"); | ||
| 300 | params.Set("port", controller_number); | ||
| 301 | params.Set("axis_x", analog_x_axis); | ||
| 302 | params.Set("axis_y", analog_y_axis); | ||
| 303 | analog_x_axis = -1; | ||
| 304 | analog_y_axis = -1; | ||
| 305 | controller_number = -1; | ||
| 306 | return params; | ||
| 307 | } | ||
| 308 | return params; | ||
| 309 | } | ||
| 310 | } // namespace InputCommon | ||