From 0c5cb4824b402f6cbd3616c95c6702638bf8e0e9 Mon Sep 17 00:00:00 2001 From: toccata10 Date: Thu, 5 Oct 2017 03:59:35 +0200 Subject: [PATCH] Add Deadzones for evdev gamepads (#3519) --- rpcs3/evdev_joystick_handler.cpp | 29 ++++++++++++++++++++++++++--- rpcs3/evdev_joystick_handler.h | 1 + 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/rpcs3/evdev_joystick_handler.cpp b/rpcs3/evdev_joystick_handler.cpp index 252ea7244b..e1572fa41a 100644 --- a/rpcs3/evdev_joystick_handler.cpp +++ b/rpcs3/evdev_joystick_handler.cpp @@ -168,6 +168,30 @@ void evdev_joystick_handler::Close() } } +inline float deadzone(f32 input) +{ + //when we're just above the deadzone, the output should be 128, not 128+deadzone + //otherwise, there'll be a jump. So, we need to recalculate the output with a linear function + float deadzone_slope, deadzone_origin; + if (input >= 127.5 - g_evdev_joystick_config.deadzone && input <= 127.5 + g_evdev_joystick_config.deadzone) + { + return 127.5; + } + else + { + if (input > 127.5 + g_evdev_joystick_config.deadzone) + { + deadzone_slope = 127.5 / (127.5 - g_evdev_joystick_config.deadzone); + deadzone_origin = 255.0 * (1.0 - deadzone_slope); + return (deadzone_slope * input + deadzone_origin); + } + if (input < 127.5 - g_evdev_joystick_config.deadzone) + { + return (127.5 / (127.5 - g_evdev_joystick_config.deadzone)) * input; + } + } +} + int evdev_joystick_handler::scale_axis(int axis, int value) { auto range = axis_ranges[axis]; @@ -181,12 +205,11 @@ int evdev_joystick_handler::scale_axis(int axis, int value) range.second += -range.first; range.first = 0; } - - return (static_cast(value - range.first) / range.second) * 255; + return (deadzone(((static_cast(value) - range.first) / range.second) * 255)); } else { - return value; + return (deadzone(static_cast(value))); } } diff --git a/rpcs3/evdev_joystick_handler.h b/rpcs3/evdev_joystick_handler.h index 20b4ea9edb..a962512331 100644 --- a/rpcs3/evdev_joystick_handler.h +++ b/rpcs3/evdev_joystick_handler.h @@ -46,6 +46,7 @@ struct evdev_joystick_config final : cfg::node cfg::_bool axistrigger{this, "Z axis triggers", true}; cfg::_bool squirclejoysticks{this, "Squircle Joysticks", true}; cfg::int32 squirclefactor{this, "Squircle Factor", 5000}; + cfg::int32 deadzone{this, "Deadzone", 10}; bool load() {