diff options
| author | 2021-09-20 17:22:07 -0500 | |
|---|---|---|
| committer | 2021-11-24 20:30:22 -0600 | |
| commit | 395e9a449d338e56ade9ea88ffeed297a7d86b10 (patch) | |
| tree | 74b7257d84bbb368d180fd230293501535509da1 /src/input_common/gcadapter/gc_poller.cpp | |
| parent | input_common: Rewrite touch (diff) | |
| download | yuzu-395e9a449d338e56ade9ea88ffeed297a7d86b10.tar.gz yuzu-395e9a449d338e56ade9ea88ffeed297a7d86b10.tar.xz yuzu-395e9a449d338e56ade9ea88ffeed297a7d86b10.zip | |
input_common: Rewrite gc_adapter
Diffstat (limited to 'src/input_common/gcadapter/gc_poller.cpp')
| -rw-r--r-- | src/input_common/gcadapter/gc_poller.cpp | 356 |
1 files changed, 0 insertions, 356 deletions
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp deleted file mode 100644 index 1b6ded8d6..000000000 --- a/src/input_common/gcadapter/gc_poller.cpp +++ /dev/null | |||
| @@ -1,356 +0,0 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <atomic> | ||
| 6 | #include <list> | ||
| 7 | #include <mutex> | ||
| 8 | #include <utility> | ||
| 9 | #include "common/assert.h" | ||
| 10 | #include "common/threadsafe_queue.h" | ||
| 11 | #include "input_common/gcadapter/gc_adapter.h" | ||
| 12 | #include "input_common/gcadapter/gc_poller.h" | ||
| 13 | |||
| 14 | namespace InputCommon { | ||
| 15 | |||
| 16 | class GCButton final : public Input::ButtonDevice { | ||
| 17 | public: | ||
| 18 | explicit GCButton(u32 port_, s32 button_, const GCAdapter::Adapter* adapter) | ||
| 19 | : port(port_), button(button_), gcadapter(adapter) {} | ||
| 20 | |||
| 21 | ~GCButton() override; | ||
| 22 | |||
| 23 | bool GetStatus() const override { | ||
| 24 | if (gcadapter->DeviceConnected(port)) { | ||
| 25 | return (gcadapter->GetPadState(port).buttons & button) != 0; | ||
| 26 | } | ||
| 27 | return false; | ||
| 28 | } | ||
| 29 | |||
| 30 | private: | ||
| 31 | const u32 port; | ||
| 32 | const s32 button; | ||
| 33 | const GCAdapter::Adapter* gcadapter; | ||
| 34 | }; | ||
| 35 | |||
| 36 | class GCAxisButton final : public Input::ButtonDevice { | ||
| 37 | public: | ||
| 38 | explicit GCAxisButton(u32 port_, u32 axis_, float threshold_, bool trigger_if_greater_, | ||
| 39 | const GCAdapter::Adapter* adapter) | ||
| 40 | : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), | ||
| 41 | gcadapter(adapter) {} | ||
| 42 | |||
| 43 | bool GetStatus() const override { | ||
| 44 | if (gcadapter->DeviceConnected(port)) { | ||
| 45 | const float current_axis_value = gcadapter->GetPadState(port).axis_values.at(axis); | ||
| 46 | const float axis_value = current_axis_value / 128.0f; | ||
| 47 | if (trigger_if_greater) { | ||
| 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; | ||
| 53 | } | ||
| 54 | return false; | ||
| 55 | } | ||
| 56 | |||
| 57 | private: | ||
| 58 | const u32 port; | ||
| 59 | const u32 axis; | ||
| 60 | float threshold; | ||
| 61 | bool trigger_if_greater; | ||
| 62 | const GCAdapter::Adapter* gcadapter; | ||
| 63 | }; | ||
| 64 | |||
| 65 | GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_) | ||
| 66 | : adapter(std::move(adapter_)) {} | ||
| 67 | |||
| 68 | GCButton::~GCButton() = default; | ||
| 69 | |||
| 70 | std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) { | ||
| 71 | const auto button_id = params.Get("button", 0); | ||
| 72 | const auto port = static_cast<u32>(params.Get("port", 0)); | ||
| 73 | |||
| 74 | constexpr s32 PAD_STICK_ID = static_cast<s32>(GCAdapter::PadButton::Stick); | ||
| 75 | |||
| 76 | // button is not an axis/stick button | ||
| 77 | if (button_id != PAD_STICK_ID) { | ||
| 78 | return std::make_unique<GCButton>(port, button_id, adapter.get()); | ||
| 79 | } | ||
| 80 | |||
| 81 | // For Axis buttons, used by the binary sticks. | ||
| 82 | if (button_id == PAD_STICK_ID) { | ||
| 83 | const int axis = params.Get("axis", 0); | ||
| 84 | const float threshold = params.Get("threshold", 0.25f); | ||
| 85 | const std::string direction_name = params.Get("direction", ""); | ||
| 86 | bool trigger_if_greater; | ||
| 87 | if (direction_name == "+") { | ||
| 88 | trigger_if_greater = true; | ||
| 89 | } else if (direction_name == "-") { | ||
| 90 | trigger_if_greater = false; | ||
| 91 | } else { | ||
| 92 | trigger_if_greater = true; | ||
| 93 | LOG_ERROR(Input, "Unknown direction {}", direction_name); | ||
| 94 | } | ||
| 95 | return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater, | ||
| 96 | adapter.get()); | ||
| 97 | } | ||
| 98 | |||
| 99 | return nullptr; | ||
| 100 | } | ||
| 101 | |||
| 102 | Common::ParamPackage GCButtonFactory::GetNextInput() const { | ||
| 103 | Common::ParamPackage params; | ||
| 104 | GCAdapter::GCPadStatus pad; | ||
| 105 | auto& queue = adapter->GetPadQueue(); | ||
| 106 | while (queue.Pop(pad)) { | ||
| 107 | // This while loop will break on the earliest detected button | ||
| 108 | params.Set("engine", "gcpad"); | ||
| 109 | params.Set("port", static_cast<s32>(pad.port)); | ||
| 110 | if (pad.button != GCAdapter::PadButton::Undefined) { | ||
| 111 | params.Set("button", static_cast<u16>(pad.button)); | ||
| 112 | } | ||
| 113 | |||
| 114 | // For Axis button implementation | ||
| 115 | if (pad.axis != GCAdapter::PadAxes::Undefined) { | ||
| 116 | params.Set("axis", static_cast<u8>(pad.axis)); | ||
| 117 | params.Set("button", static_cast<u16>(GCAdapter::PadButton::Stick)); | ||
| 118 | params.Set("threshold", "0.25"); | ||
| 119 | if (pad.axis_value > 0) { | ||
| 120 | params.Set("direction", "+"); | ||
| 121 | } else { | ||
| 122 | params.Set("direction", "-"); | ||
| 123 | } | ||
| 124 | break; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | return params; | ||
| 128 | } | ||
| 129 | |||
| 130 | void GCButtonFactory::BeginConfiguration() { | ||
| 131 | polling = true; | ||
| 132 | adapter->BeginConfiguration(); | ||
| 133 | } | ||
| 134 | |||
| 135 | void GCButtonFactory::EndConfiguration() { | ||
| 136 | polling = false; | ||
| 137 | adapter->EndConfiguration(); | ||
| 138 | } | ||
| 139 | |||
| 140 | class GCAnalog final : public Input::AnalogDevice { | ||
| 141 | public: | ||
| 142 | explicit GCAnalog(u32 port_, u32 axis_x_, u32 axis_y_, bool invert_x_, bool invert_y_, | ||
| 143 | float deadzone_, float range_, const GCAdapter::Adapter* adapter) | ||
| 144 | : port(port_), axis_x(axis_x_), axis_y(axis_y_), invert_x(invert_x_), invert_y(invert_y_), | ||
| 145 | deadzone(deadzone_), range(range_), gcadapter(adapter) {} | ||
| 146 | |||
| 147 | float GetAxis(u32 axis) const { | ||
| 148 | if (gcadapter->DeviceConnected(port)) { | ||
| 149 | std::lock_guard lock{mutex}; | ||
| 150 | const auto axis_value = | ||
| 151 | static_cast<float>(gcadapter->GetPadState(port).axis_values.at(axis)); | ||
| 152 | return (axis_value) / (100.0f * range); | ||
| 153 | } | ||
| 154 | return 0.0f; | ||
| 155 | } | ||
| 156 | |||
| 157 | std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const { | ||
| 158 | float x = GetAxis(analog_axis_x); | ||
| 159 | float y = GetAxis(analog_axis_y); | ||
| 160 | if (invert_x) { | ||
| 161 | x = -x; | ||
| 162 | } | ||
| 163 | if (invert_y) { | ||
| 164 | y = -y; | ||
| 165 | } | ||
| 166 | // Make sure the coordinates are in the unit circle, | ||
| 167 | // otherwise normalize it. | ||
| 168 | float r = x * x + y * y; | ||
| 169 | if (r > 1.0f) { | ||
| 170 | r = std::sqrt(r); | ||
| 171 | x /= r; | ||
| 172 | y /= r; | ||
| 173 | } | ||
| 174 | |||
| 175 | return {x, y}; | ||
| 176 | } | ||
| 177 | |||
| 178 | std::tuple<float, float> GetStatus() const override { | ||
| 179 | const auto [x, y] = GetAnalog(axis_x, axis_y); | ||
| 180 | const float r = std::sqrt((x * x) + (y * y)); | ||
| 181 | if (r > deadzone) { | ||
| 182 | return {x / r * (r - deadzone) / (1 - deadzone), | ||
| 183 | y / r * (r - deadzone) / (1 - deadzone)}; | ||
| 184 | } | ||
| 185 | return {0.0f, 0.0f}; | ||
| 186 | } | ||
| 187 | |||
| 188 | std::tuple<float, float> GetRawStatus() const override { | ||
| 189 | const float x = GetAxis(axis_x); | ||
| 190 | const float y = GetAxis(axis_y); | ||
| 191 | return {x, y}; | ||
| 192 | } | ||
| 193 | |||
| 194 | Input::AnalogProperties GetAnalogProperties() const override { | ||
| 195 | return {deadzone, range, 0.5f}; | ||
| 196 | } | ||
| 197 | |||
| 198 | bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { | ||
| 199 | const auto [x, y] = GetStatus(); | ||
| 200 | const float directional_deadzone = 0.5f; | ||
| 201 | switch (direction) { | ||
| 202 | case Input::AnalogDirection::RIGHT: | ||
| 203 | return x > directional_deadzone; | ||
| 204 | case Input::AnalogDirection::LEFT: | ||
| 205 | return x < -directional_deadzone; | ||
| 206 | case Input::AnalogDirection::UP: | ||
| 207 | return y > directional_deadzone; | ||
| 208 | case Input::AnalogDirection::DOWN: | ||
| 209 | return y < -directional_deadzone; | ||
| 210 | } | ||
| 211 | return false; | ||
| 212 | } | ||
| 213 | |||
| 214 | private: | ||
| 215 | const u32 port; | ||
| 216 | const u32 axis_x; | ||
| 217 | const u32 axis_y; | ||
| 218 | const bool invert_x; | ||
| 219 | const bool invert_y; | ||
| 220 | const float deadzone; | ||
| 221 | const float range; | ||
| 222 | const GCAdapter::Adapter* gcadapter; | ||
| 223 | mutable std::mutex mutex; | ||
| 224 | }; | ||
| 225 | |||
| 226 | /// An analog device factory that creates analog devices from GC Adapter | ||
| 227 | GCAnalogFactory::GCAnalogFactory(std::shared_ptr<GCAdapter::Adapter> adapter_) | ||
| 228 | : adapter(std::move(adapter_)) {} | ||
| 229 | |||
| 230 | /** | ||
| 231 | * Creates analog device from joystick axes | ||
| 232 | * @param params contains parameters for creating the device: | ||
| 233 | * - "port": the nth gcpad on the adapter | ||
| 234 | * - "axis_x": the index of the axis to be bind as x-axis | ||
| 235 | * - "axis_y": the index of the axis to be bind as y-axis | ||
| 236 | */ | ||
| 237 | std::unique_ptr<Input::AnalogDevice> GCAnalogFactory::Create(const Common::ParamPackage& params) { | ||
| 238 | const auto port = static_cast<u32>(params.Get("port", 0)); | ||
| 239 | const auto axis_x = static_cast<u32>(params.Get("axis_x", 0)); | ||
| 240 | const auto axis_y = static_cast<u32>(params.Get("axis_y", 1)); | ||
| 241 | const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); | ||
| 242 | const auto range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); | ||
| 243 | const std::string invert_x_value = params.Get("invert_x", "+"); | ||
| 244 | const std::string invert_y_value = params.Get("invert_y", "+"); | ||
| 245 | const bool invert_x = invert_x_value == "-"; | ||
| 246 | const bool invert_y = invert_y_value == "-"; | ||
| 247 | |||
| 248 | return std::make_unique<GCAnalog>(port, axis_x, axis_y, invert_x, invert_y, deadzone, range, | ||
| 249 | adapter.get()); | ||
| 250 | } | ||
| 251 | |||
| 252 | void GCAnalogFactory::BeginConfiguration() { | ||
| 253 | polling = true; | ||
| 254 | adapter->BeginConfiguration(); | ||
| 255 | } | ||
| 256 | |||
| 257 | void GCAnalogFactory::EndConfiguration() { | ||
| 258 | polling = false; | ||
| 259 | adapter->EndConfiguration(); | ||
| 260 | } | ||
| 261 | |||
| 262 | Common::ParamPackage GCAnalogFactory::GetNextInput() { | ||
| 263 | GCAdapter::GCPadStatus pad; | ||
| 264 | Common::ParamPackage params; | ||
| 265 | auto& queue = adapter->GetPadQueue(); | ||
| 266 | while (queue.Pop(pad)) { | ||
| 267 | if (pad.button != GCAdapter::PadButton::Undefined) { | ||
| 268 | params.Set("engine", "gcpad"); | ||
| 269 | params.Set("port", static_cast<s32>(pad.port)); | ||
| 270 | params.Set("button", static_cast<u16>(pad.button)); | ||
| 271 | return params; | ||
| 272 | } | ||
| 273 | if (pad.axis == GCAdapter::PadAxes::Undefined || | ||
| 274 | std::abs(static_cast<float>(pad.axis_value) / 128.0f) < 0.1f) { | ||
| 275 | continue; | ||
| 276 | } | ||
| 277 | // An analog device needs two axes, so we need to store the axis for later and wait for | ||
| 278 | // a second input event. The axes also must be from the same joystick. | ||
| 279 | const u8 axis = static_cast<u8>(pad.axis); | ||
| 280 | if (axis == 0 || axis == 1) { | ||
| 281 | analog_x_axis = 0; | ||
| 282 | analog_y_axis = 1; | ||
| 283 | controller_number = static_cast<s32>(pad.port); | ||
| 284 | break; | ||
| 285 | } | ||
| 286 | if (axis == 2 || axis == 3) { | ||
| 287 | analog_x_axis = 2; | ||
| 288 | analog_y_axis = 3; | ||
| 289 | controller_number = static_cast<s32>(pad.port); | ||
| 290 | break; | ||
| 291 | } | ||
| 292 | |||
| 293 | if (analog_x_axis == -1) { | ||
| 294 | analog_x_axis = axis; | ||
| 295 | controller_number = static_cast<s32>(pad.port); | ||
| 296 | } else if (analog_y_axis == -1 && analog_x_axis != axis && | ||
| 297 | controller_number == static_cast<s32>(pad.port)) { | ||
| 298 | analog_y_axis = axis; | ||
| 299 | break; | ||
| 300 | } | ||
| 301 | } | ||
| 302 | if (analog_x_axis != -1 && analog_y_axis != -1) { | ||
| 303 | params.Set("engine", "gcpad"); | ||
| 304 | params.Set("port", controller_number); | ||
| 305 | params.Set("axis_x", analog_x_axis); | ||
| 306 | params.Set("axis_y", analog_y_axis); | ||
| 307 | params.Set("invert_x", "+"); | ||
| 308 | params.Set("invert_y", "+"); | ||
| 309 | analog_x_axis = -1; | ||
| 310 | analog_y_axis = -1; | ||
| 311 | controller_number = -1; | ||
| 312 | return params; | ||
| 313 | } | ||
| 314 | return params; | ||
| 315 | } | ||
| 316 | |||
| 317 | class GCVibration final : public Input::VibrationDevice { | ||
| 318 | public: | ||
| 319 | explicit GCVibration(u32 port_, GCAdapter::Adapter* adapter) | ||
| 320 | : port(port_), gcadapter(adapter) {} | ||
| 321 | |||
| 322 | u8 GetStatus() const override { | ||
| 323 | return gcadapter->RumblePlay(port, 0); | ||
| 324 | } | ||
| 325 | |||
| 326 | bool SetRumblePlay(f32 amp_low, [[maybe_unused]] f32 freq_low, f32 amp_high, | ||
| 327 | [[maybe_unused]] f32 freq_high) const override { | ||
| 328 | const auto mean_amplitude = (amp_low + amp_high) * 0.5f; | ||
| 329 | const auto processed_amplitude = | ||
| 330 | static_cast<u8>((mean_amplitude + std::pow(mean_amplitude, 0.3f)) * 0.5f * 0x8); | ||
| 331 | |||
| 332 | return gcadapter->RumblePlay(port, processed_amplitude); | ||
| 333 | } | ||
| 334 | |||
| 335 | private: | ||
| 336 | const u32 port; | ||
| 337 | GCAdapter::Adapter* gcadapter; | ||
| 338 | }; | ||
| 339 | |||
| 340 | /// An vibration device factory that creates vibration devices from GC Adapter | ||
| 341 | GCVibrationFactory::GCVibrationFactory(std::shared_ptr<GCAdapter::Adapter> adapter_) | ||
| 342 | : adapter(std::move(adapter_)) {} | ||
| 343 | |||
| 344 | /** | ||
| 345 | * Creates a vibration device from a joystick | ||
| 346 | * @param params contains parameters for creating the device: | ||
| 347 | * - "port": the nth gcpad on the adapter | ||
| 348 | */ | ||
| 349 | std::unique_ptr<Input::VibrationDevice> GCVibrationFactory::Create( | ||
| 350 | const Common::ParamPackage& params) { | ||
| 351 | const auto port = static_cast<u32>(params.Get("port", 0)); | ||
| 352 | |||
| 353 | return std::make_unique<GCVibration>(port, adapter.get()); | ||
| 354 | } | ||
| 355 | |||
| 356 | } // namespace InputCommon | ||