Add Deadzones for evdev gamepads (#3519)

This commit is contained in:
toccata10 2017-10-05 03:59:35 +02:00 committed by Ivan
parent cf83b796e1
commit 0c5cb4824b
2 changed files with 27 additions and 3 deletions

View File

@ -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<float>(value - range.first) / range.second) * 255;
return (deadzone(((static_cast<float>(value) - range.first) / range.second) * 255));
}
else
{
return value;
return (deadzone(static_cast<float>(value)));
}
}

View File

@ -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()
{