summaryrefslogtreecommitdiff
path: root/src/input_common/sdl/sdl_impl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
-rw-r--r--src/input_common/sdl/sdl_impl.cpp73
1 files changed, 66 insertions, 7 deletions
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:
352class SDLAnalog final : public Input::AnalogDevice { 352class SDLAnalog final : public Input::AnalogDevice {
353public: 353public:
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
583private: 597private:
@@ -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
1080private:
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
1032class SDLMotionPoller final : public SDLPoller { 1091class SDLMotionPoller final : public SDLPoller {