diff options
Diffstat (limited to 'src/input_common')
| -rw-r--r-- | src/input_common/gcadapter/gc_poller.cpp | 28 | ||||
| -rw-r--r-- | src/input_common/mouse/mouse_poller.cpp | 25 | ||||
| -rw-r--r-- | src/input_common/sdl/sdl_impl.cpp | 73 | ||||
| -rw-r--r-- | src/input_common/udp/client.cpp | 5 |
4 files changed, 112 insertions, 19 deletions
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index 4d1052414..9670bdeb2 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp | |||
| @@ -139,10 +139,10 @@ void GCButtonFactory::EndConfiguration() { | |||
| 139 | 139 | ||
| 140 | class GCAnalog final : public Input::AnalogDevice { | 140 | class GCAnalog final : public Input::AnalogDevice { |
| 141 | public: | 141 | public: |
| 142 | explicit GCAnalog(u32 port_, u32 axis_x_, u32 axis_y_, float deadzone_, | 142 | explicit GCAnalog(u32 port_, u32 axis_x_, u32 axis_y_, bool invert_x_, bool invert_y_, |
| 143 | const GCAdapter::Adapter* adapter, float range_) | 143 | float deadzone_, float range_, const GCAdapter::Adapter* adapter) |
| 144 | : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter), | 144 | : port(port_), axis_x(axis_x_), axis_y(axis_y_), invert_x(invert_x_), invert_y(invert_y_), |
| 145 | range(range_) {} | 145 | deadzone(deadzone_), range(range_), gcadapter(adapter) {} |
| 146 | 146 | ||
| 147 | float GetAxis(u32 axis) const { | 147 | float GetAxis(u32 axis) const { |
| 148 | if (gcadapter->DeviceConnected(port)) { | 148 | if (gcadapter->DeviceConnected(port)) { |
| @@ -157,7 +157,12 @@ public: | |||
| 157 | std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const { | 157 | std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const { |
| 158 | float x = GetAxis(analog_axis_x); | 158 | float x = GetAxis(analog_axis_x); |
| 159 | float y = GetAxis(analog_axis_y); | 159 | float y = GetAxis(analog_axis_y); |
| 160 | 160 | if (invert_x) { | |
| 161 | x = -x; | ||
| 162 | } | ||
| 163 | if (invert_y) { | ||
| 164 | y = -y; | ||
| 165 | } | ||
| 161 | // Make sure the coordinates are in the unit circle, | 166 | // Make sure the coordinates are in the unit circle, |
| 162 | // otherwise normalize it. | 167 | // otherwise normalize it. |
| 163 | float r = x * x + y * y; | 168 | float r = x * x + y * y; |
| @@ -200,9 +205,11 @@ private: | |||
| 200 | const u32 port; | 205 | const u32 port; |
| 201 | const u32 axis_x; | 206 | const u32 axis_x; |
| 202 | const u32 axis_y; | 207 | const u32 axis_y; |
| 208 | const bool invert_x; | ||
| 209 | const bool invert_y; | ||
| 203 | const float deadzone; | 210 | const float deadzone; |
| 204 | const GCAdapter::Adapter* gcadapter; | ||
| 205 | const float range; | 211 | const float range; |
| 212 | const GCAdapter::Adapter* gcadapter; | ||
| 206 | mutable std::mutex mutex; | 213 | mutable std::mutex mutex; |
| 207 | }; | 214 | }; |
| 208 | 215 | ||
| @@ -223,8 +230,13 @@ std::unique_ptr<Input::AnalogDevice> GCAnalogFactory::Create(const Common::Param | |||
| 223 | const auto axis_y = static_cast<u32>(params.Get("axis_y", 1)); | 230 | const auto axis_y = static_cast<u32>(params.Get("axis_y", 1)); |
| 224 | const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); | 231 | const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); |
| 225 | const auto range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); | 232 | const auto range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); |
| 233 | const std::string invert_x_value = params.Get("invert_x", "+"); | ||
| 234 | const std::string invert_y_value = params.Get("invert_y", "+"); | ||
| 235 | const bool invert_x = invert_x_value == "-"; | ||
| 236 | const bool invert_y = invert_y_value == "-"; | ||
| 226 | 237 | ||
| 227 | return std::make_unique<GCAnalog>(port, axis_x, axis_y, deadzone, adapter.get(), range); | 238 | return std::make_unique<GCAnalog>(port, axis_x, axis_y, invert_x, invert_y, deadzone, range, |
| 239 | adapter.get()); | ||
| 228 | } | 240 | } |
| 229 | 241 | ||
| 230 | void GCAnalogFactory::BeginConfiguration() { | 242 | void GCAnalogFactory::BeginConfiguration() { |
| @@ -282,6 +294,8 @@ Common::ParamPackage GCAnalogFactory::GetNextInput() { | |||
| 282 | params.Set("port", controller_number); | 294 | params.Set("port", controller_number); |
| 283 | params.Set("axis_x", analog_x_axis); | 295 | params.Set("axis_x", analog_x_axis); |
| 284 | params.Set("axis_y", analog_y_axis); | 296 | params.Set("axis_y", analog_y_axis); |
| 297 | params.Set("invert_x", "+"); | ||
| 298 | params.Set("invert_y", "+"); | ||
| 285 | analog_x_axis = -1; | 299 | analog_x_axis = -1; |
| 286 | analog_y_axis = -1; | 300 | analog_y_axis = -1; |
| 287 | controller_number = -1; | 301 | controller_number = -1; |
diff --git a/src/input_common/mouse/mouse_poller.cpp b/src/input_common/mouse/mouse_poller.cpp index 7445ad3ad..508eb0c7d 100644 --- a/src/input_common/mouse/mouse_poller.cpp +++ b/src/input_common/mouse/mouse_poller.cpp | |||
| @@ -62,10 +62,10 @@ void MouseButtonFactory::EndConfiguration() { | |||
| 62 | 62 | ||
| 63 | class MouseAnalog final : public Input::AnalogDevice { | 63 | class MouseAnalog final : public Input::AnalogDevice { |
| 64 | public: | 64 | public: |
| 65 | explicit MouseAnalog(u32 port_, u32 axis_x_, u32 axis_y_, float deadzone_, float range_, | 65 | explicit MouseAnalog(u32 port_, u32 axis_x_, u32 axis_y_, bool invert_x_, bool invert_y_, |
| 66 | const MouseInput::Mouse* mouse_input_) | 66 | float deadzone_, float range_, const MouseInput::Mouse* mouse_input_) |
| 67 | : button(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), range(range_), | 67 | : button(port_), axis_x(axis_x_), axis_y(axis_y_), invert_x(invert_x_), invert_y(invert_y_), |
| 68 | mouse_input(mouse_input_) {} | 68 | deadzone(deadzone_), range(range_), mouse_input(mouse_input_) {} |
| 69 | 69 | ||
| 70 | float GetAxis(u32 axis) const { | 70 | float GetAxis(u32 axis) const { |
| 71 | std::lock_guard lock{mutex}; | 71 | std::lock_guard lock{mutex}; |
| @@ -77,6 +77,12 @@ public: | |||
| 77 | std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const { | 77 | std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const { |
| 78 | float x = GetAxis(analog_axis_x); | 78 | float x = GetAxis(analog_axis_x); |
| 79 | float y = GetAxis(analog_axis_y); | 79 | float y = GetAxis(analog_axis_y); |
| 80 | if (invert_x) { | ||
| 81 | x = -x; | ||
| 82 | } | ||
| 83 | if (invert_y) { | ||
| 84 | y = -y; | ||
| 85 | } | ||
| 80 | 86 | ||
| 81 | // Make sure the coordinates are in the unit circle, | 87 | // Make sure the coordinates are in the unit circle, |
| 82 | // otherwise normalize it. | 88 | // otherwise normalize it. |
| @@ -104,6 +110,8 @@ private: | |||
| 104 | const u32 button; | 110 | const u32 button; |
| 105 | const u32 axis_x; | 111 | const u32 axis_x; |
| 106 | const u32 axis_y; | 112 | const u32 axis_y; |
| 113 | const bool invert_x; | ||
| 114 | const bool invert_y; | ||
| 107 | const float deadzone; | 115 | const float deadzone; |
| 108 | const float range; | 116 | const float range; |
| 109 | const MouseInput::Mouse* mouse_input; | 117 | const MouseInput::Mouse* mouse_input; |
| @@ -128,8 +136,13 @@ std::unique_ptr<Input::AnalogDevice> MouseAnalogFactory::Create( | |||
| 128 | const auto axis_y = static_cast<u32>(params.Get("axis_y", 1)); | 136 | const auto axis_y = static_cast<u32>(params.Get("axis_y", 1)); |
| 129 | const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); | 137 | const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); |
| 130 | const auto range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); | 138 | const auto range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); |
| 139 | const std::string invert_x_value = params.Get("invert_x", "+"); | ||
| 140 | const std::string invert_y_value = params.Get("invert_y", "+"); | ||
| 141 | const bool invert_x = invert_x_value == "-"; | ||
| 142 | const bool invert_y = invert_y_value == "-"; | ||
| 131 | 143 | ||
| 132 | return std::make_unique<MouseAnalog>(port, axis_x, axis_y, deadzone, range, mouse_input.get()); | 144 | return std::make_unique<MouseAnalog>(port, axis_x, axis_y, invert_x, invert_y, deadzone, range, |
| 145 | mouse_input.get()); | ||
| 133 | } | 146 | } |
| 134 | 147 | ||
| 135 | void MouseAnalogFactory::BeginConfiguration() { | 148 | void MouseAnalogFactory::BeginConfiguration() { |
| @@ -153,6 +166,8 @@ Common::ParamPackage MouseAnalogFactory::GetNextInput() const { | |||
| 153 | params.Set("port", static_cast<u16>(pad.button)); | 166 | params.Set("port", static_cast<u16>(pad.button)); |
| 154 | params.Set("axis_x", 0); | 167 | params.Set("axis_x", 0); |
| 155 | params.Set("axis_y", 1); | 168 | params.Set("axis_y", 1); |
| 169 | params.Set("invert_x", "+"); | ||
| 170 | params.Set("invert_y", "+"); | ||
| 156 | return params; | 171 | return params; |
| 157 | } | 172 | } |
| 158 | } | 173 | } |
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index 7827e324c..d32eb732a 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp | |||
| @@ -352,13 +352,20 @@ private: | |||
| 352 | class SDLAnalog final : public Input::AnalogDevice { | 352 | class SDLAnalog final : public Input::AnalogDevice { |
| 353 | public: | 353 | public: |
| 354 | explicit SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, | 354 | explicit SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, |
| 355 | float deadzone_, float range_) | 355 | bool invert_x_, bool invert_y_, float deadzone_, float range_) |
| 356 | : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), | 356 | : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), invert_x(invert_x_), |
| 357 | range(range_) {} | 357 | invert_y(invert_y_), deadzone(deadzone_), range(range_) {} |
| 358 | 358 | ||
| 359 | std::tuple<float, float> GetStatus() const override { | 359 | std::tuple<float, float> GetStatus() const override { |
| 360 | const auto [x, y] = joystick->GetAnalog(axis_x, axis_y, range); | 360 | auto [x, y] = joystick->GetAnalog(axis_x, axis_y, range); |
| 361 | const float r = std::sqrt((x * x) + (y * y)); | 361 | const float r = std::sqrt((x * x) + (y * y)); |
| 362 | if (invert_x) { | ||
| 363 | x = -x; | ||
| 364 | } | ||
| 365 | if (invert_y) { | ||
| 366 | y = -y; | ||
| 367 | } | ||
| 368 | |||
| 362 | if (r > deadzone) { | 369 | if (r > deadzone) { |
| 363 | return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), | 370 | return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), |
| 364 | y / r * (r - deadzone) / (1 - deadzone)); | 371 | y / r * (r - deadzone) / (1 - deadzone)); |
| @@ -386,6 +393,8 @@ private: | |||
| 386 | std::shared_ptr<SDLJoystick> joystick; | 393 | std::shared_ptr<SDLJoystick> joystick; |
| 387 | const int axis_x; | 394 | const int axis_x; |
| 388 | const int axis_y; | 395 | const int axis_y; |
| 396 | const bool invert_x; | ||
| 397 | const bool invert_y; | ||
| 389 | const float deadzone; | 398 | const float deadzone; |
| 390 | const float range; | 399 | const float range; |
| 391 | }; | 400 | }; |
| @@ -572,12 +581,17 @@ public: | |||
| 572 | const int axis_y = params.Get("axis_y", 1); | 581 | const int axis_y = params.Get("axis_y", 1); |
| 573 | const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); | 582 | const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); |
| 574 | const float range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); | 583 | const float range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); |
| 584 | const std::string invert_x_value = params.Get("invert_x", "+"); | ||
| 585 | const std::string invert_y_value = params.Get("invert_y", "+"); | ||
| 586 | const bool invert_x = invert_x_value == "-"; | ||
| 587 | const bool invert_y = invert_y_value == "-"; | ||
| 575 | auto joystick = state.GetSDLJoystickByGUID(guid, port); | 588 | auto joystick = state.GetSDLJoystickByGUID(guid, port); |
| 576 | 589 | ||
| 577 | // This is necessary so accessing GetAxis with axis_x and axis_y won't crash | 590 | // This is necessary so accessing GetAxis with axis_x and axis_y won't crash |
| 578 | joystick->SetAxis(axis_x, 0); | 591 | joystick->SetAxis(axis_x, 0); |
| 579 | joystick->SetAxis(axis_y, 0); | 592 | joystick->SetAxis(axis_y, 0); |
| 580 | return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y, deadzone, range); | 593 | return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y, invert_x, invert_y, deadzone, |
| 594 | range); | ||
| 581 | } | 595 | } |
| 582 | 596 | ||
| 583 | private: | 597 | private: |
| @@ -886,6 +900,8 @@ Common::ParamPackage BuildParamPackageForAnalog(int port, const std::string& gui | |||
| 886 | params.Set("guid", guid); | 900 | params.Set("guid", guid); |
| 887 | params.Set("axis_x", axis_x); | 901 | params.Set("axis_x", axis_x); |
| 888 | params.Set("axis_y", axis_y); | 902 | params.Set("axis_y", axis_y); |
| 903 | params.Set("invert_x", "+"); | ||
| 904 | params.Set("invert_y", "+"); | ||
| 889 | return params; | 905 | return params; |
| 890 | } | 906 | } |
| 891 | } // Anonymous namespace | 907 | } // Anonymous namespace |
| @@ -1014,11 +1030,44 @@ public: | |||
| 1014 | } | 1030 | } |
| 1015 | return {}; | 1031 | return {}; |
| 1016 | } | 1032 | } |
| 1017 | [[nodiscard]] std::optional<Common::ParamPackage> FromEvent(const SDL_Event& event) const { | 1033 | [[nodiscard]] std::optional<Common::ParamPackage> FromEvent(SDL_Event& event) { |
| 1018 | switch (event.type) { | 1034 | switch (event.type) { |
| 1019 | case SDL_JOYAXISMOTION: | 1035 | case SDL_JOYAXISMOTION: |
| 1020 | if (std::abs(event.jaxis.value / 32767.0) < 0.5) { | 1036 | if (!axis_memory.count(event.jaxis.which) || |
| 1037 | !axis_memory[event.jaxis.which].count(event.jaxis.axis)) { | ||
| 1038 | axis_memory[event.jaxis.which][event.jaxis.axis] = event.jaxis.value; | ||
| 1039 | axis_event_count[event.jaxis.which][event.jaxis.axis] = 1; | ||
| 1021 | break; | 1040 | break; |
| 1041 | } else { | ||
| 1042 | axis_event_count[event.jaxis.which][event.jaxis.axis]++; | ||
| 1043 | // The joystick and axis exist in our map if we take this branch, so no checks | ||
| 1044 | // needed | ||
| 1045 | if (std::abs( | ||
| 1046 | (event.jaxis.value - axis_memory[event.jaxis.which][event.jaxis.axis]) / | ||
| 1047 | 32767.0) < 0.5) { | ||
| 1048 | break; | ||
| 1049 | } else { | ||
| 1050 | if (axis_event_count[event.jaxis.which][event.jaxis.axis] == 2 && | ||
| 1051 | IsAxisAtPole(event.jaxis.value) && | ||
| 1052 | IsAxisAtPole(axis_memory[event.jaxis.which][event.jaxis.axis])) { | ||
| 1053 | // If we have exactly two events and both are near a pole, this is | ||
| 1054 | // likely a digital input masquerading as an analog axis; Instead of | ||
| 1055 | // trying to look at the direction the axis travelled, assume the first | ||
| 1056 | // event was press and the second was release; This should handle most | ||
| 1057 | // digital axes while deferring to the direction of travel for analog | ||
| 1058 | // axes | ||
| 1059 | event.jaxis.value = static_cast<Sint16>( | ||
| 1060 | std::copysign(32767, axis_memory[event.jaxis.which][event.jaxis.axis])); | ||
| 1061 | } else { | ||
| 1062 | // There are more than two events, so this is likely a true analog axis, | ||
| 1063 | // check the direction it travelled | ||
| 1064 | event.jaxis.value = static_cast<Sint16>(std::copysign( | ||
| 1065 | 32767, | ||
| 1066 | event.jaxis.value - axis_memory[event.jaxis.which][event.jaxis.axis])); | ||
| 1067 | } | ||
| 1068 | axis_memory.clear(); | ||
| 1069 | axis_event_count.clear(); | ||
| 1070 | } | ||
| 1022 | } | 1071 | } |
| 1023 | [[fallthrough]]; | 1072 | [[fallthrough]]; |
| 1024 | case SDL_JOYBUTTONUP: | 1073 | case SDL_JOYBUTTONUP: |
| @@ -1027,6 +1076,16 @@ public: | |||
| 1027 | } | 1076 | } |
| 1028 | return std::nullopt; | 1077 | return std::nullopt; |
| 1029 | } | 1078 | } |
| 1079 | |||
| 1080 | private: | ||
| 1081 | // Determine whether an axis value is close to an extreme or center | ||
| 1082 | // Some controllers have a digital D-Pad as a pair of analog sticks, with 3 possible values per | ||
| 1083 | // axis, which is why the center must be considered a pole | ||
| 1084 | bool IsAxisAtPole(int16_t value) const { | ||
| 1085 | return std::abs(value) >= 32767 || std::abs(value) < 327; | ||
| 1086 | } | ||
| 1087 | std::unordered_map<SDL_JoystickID, std::unordered_map<uint8_t, int16_t>> axis_memory; | ||
| 1088 | std::unordered_map<SDL_JoystickID, std::unordered_map<uint8_t, uint32_t>> axis_event_count; | ||
| 1030 | }; | 1089 | }; |
| 1031 | 1090 | ||
| 1032 | class SDLMotionPoller final : public SDLPoller { | 1091 | class SDLMotionPoller final : public SDLPoller { |
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp index 17a9225d7..412d57896 100644 --- a/src/input_common/udp/client.cpp +++ b/src/input_common/udp/client.cpp | |||
| @@ -225,6 +225,11 @@ void Client::OnPortInfo([[maybe_unused]] Response::PortInfo data) { | |||
| 225 | } | 225 | } |
| 226 | 226 | ||
| 227 | void Client::OnPadData(Response::PadData data, std::size_t client) { | 227 | void Client::OnPadData(Response::PadData data, std::size_t client) { |
| 228 | // Accept packets only for the correct pad | ||
| 229 | if (static_cast<u8>(clients[client].pad_index) != data.info.id) { | ||
| 230 | return; | ||
| 231 | } | ||
| 232 | |||
| 228 | LOG_TRACE(Input, "PadData packet received"); | 233 | LOG_TRACE(Input, "PadData packet received"); |
| 229 | if (data.packet_counter == clients[client].packet_sequence) { | 234 | if (data.packet_counter == clients[client].packet_sequence) { |
| 230 | LOG_WARNING( | 235 | LOG_WARNING( |