From 572b39795250198e01d1278ec17667b9fb655e3c Mon Sep 17 00:00:00 2001 From: Bryan Baraoidan Date: Sun, 3 Dec 2017 20:43:12 -0500 Subject: [PATCH] added options for deadzones, a default deadzone, and adjustments to the raw axis value before scaling --- src/host/sdl_host.c | 17 +++++++++++- src/options.c | 66 ++++++++++++++++++++++++++++----------------- src/options.h | 6 +++++ 3 files changed, 63 insertions(+), 26 deletions(-) diff --git a/src/host/sdl_host.c b/src/host/sdl_host.c index f3890121..c0fb599b 100644 --- a/src/host/sdl_host.c +++ b/src/host/sdl_host.c @@ -509,6 +509,21 @@ static int translate_sdl_key(SDL_Keysym keysym) { return out; } +static int16_t filter_sdl_motion(int16_t value, int deadzone) { + CHECK(deadzone >= 0 && deadzone <= INT16_MAX); + + /* maximum input value after accounting for the deadzone */ + float deadmax = (float)(INT16_MAX - deadzone); + + if (value < -deadzone) { + value = (int16_t)((((float)value + deadzone) / deadmax) * INT16_MAX); + } else if (value > deadzone) { + value = (int16_t)((((float)value - deadzone) / deadmax) * INT16_MAX); + } + + return value; +} + static int input_find_controller_port(struct host *host, int instance_id) { for (int port = 0; port < INPUT_MAX_CONTROLLERS; port++) { SDL_GameController *ctrl = host->input.controllers[port]; @@ -841,7 +856,6 @@ static void host_poll_events(struct host *host) { case SDL_CONTROLLERAXISMOTION: { int port = input_find_controller_port(host, ev.caxis.which); int key = K_UNKNOWN; - int16_t value = ev.caxis.value; switch (ev.caxis.axis) { case SDL_CONTROLLER_AXIS_LEFTX: @@ -859,6 +873,7 @@ static void host_poll_events(struct host *host) { } if (port != -1 && key != K_UNKNOWN) { + int16_t value = filter_sdl_motion(ev.caxis.value, *DEADZONES[port]); input_keydown(host, port, key, value); } } break; diff --git a/src/options.c b/src/options.c index 11e53a5a..e8c25b43 100644 --- a/src/options.c +++ b/src/options.c @@ -2,8 +2,12 @@ #include "core/core.h" #include "host/keycode.h" -/* clang-format off */ +/* default deadzone taken from: https://forums.libsdl.org/viewtopic.php?p=39985 + this thread is specifically talking about 360 controllers, but other sources + also have a default of around this */ +#define DEFAULT_DEADZONE 4096 +/* clang-format off */ const char *ASPECT_RATIOS[] = { "stretch", "16:9", "4:3", }; @@ -28,39 +32,51 @@ struct button_map BUTTONS[] = { }; const int NUM_BUTTONS = ARRAY_SIZE(BUTTONS); +int *DEADZONES[] = { + &OPTION_deadzone_0, + &OPTION_deadzone_1, + &OPTION_deadzone_2, + &OPTION_deadzone_3 +}; + /* host */ -DEFINE_OPTION_STRING(sync, "av", "Sync control"); -DEFINE_OPTION_INT(bios, 0, "Boot to bios"); -DEFINE_PERSISTENT_OPTION_INT(fullscreen, 0, "Start window fullscreen"); -DEFINE_PERSISTENT_OPTION_INT(key_a, 'l', "A button mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_b, 'p', "B button mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_x, 'k', "X button mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_y, 'o', "Y button mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_start, ' ', "Start button mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_dup, 't', "DPAD Up mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_ddown, 'g', "DPAD Down mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_dleft, 'f', "DPAD Left mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_dright, 'h', "DPAD Right mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_joyx_neg, 'a', "Joystick -X axis mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_joyx_pos, 'd', "Joystick +X axis mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_joyy_neg, 'w', "Joystick -Y axis mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_joyy_pos, 's', "Joystick +Y axis mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_ltrig, '[', "Left trigger mapping"); -DEFINE_PERSISTENT_OPTION_INT(key_rtrig, ']', "Right trigger mapping"); +DEFINE_OPTION_STRING(sync, "av", "Sync control"); +DEFINE_OPTION_INT(bios, 0, "Boot to bios"); +DEFINE_PERSISTENT_OPTION_INT(fullscreen, 0, "Start window fullscreen"); +DEFINE_PERSISTENT_OPTION_INT(key_a, 'l', "A button mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_b, 'p', "B button mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_x, 'k', "X button mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_y, 'o', "Y button mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_start, ' ', "Start button mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_dup, 't', "DPAD Up mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_ddown, 'g', "DPAD Down mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_dleft, 'f', "DPAD Left mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_dright, 'h', "DPAD Right mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_joyx_neg, 'a', "Joystick -X axis mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_joyx_pos, 'd', "Joystick +X axis mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_joyy_neg, 'w', "Joystick -Y axis mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_joyy_pos, 's', "Joystick +Y axis mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_ltrig, '[', "Left trigger mapping"); +DEFINE_PERSISTENT_OPTION_INT(key_rtrig, ']', "Right trigger mapping"); + +DEFINE_PERSISTENT_OPTION_INT(deadzone_0, DEFAULT_DEADZONE, "Deadzone for controller on port 0"); +DEFINE_PERSISTENT_OPTION_INT(deadzone_1, DEFAULT_DEADZONE, "Deadzone for controller on port 1"); +DEFINE_PERSISTENT_OPTION_INT(deadzone_2, DEFAULT_DEADZONE, "Deadzone for controller on port 2"); +DEFINE_PERSISTENT_OPTION_INT(deadzone_3, DEFAULT_DEADZONE, "Deadzone for controller on port 3"); /* emulator */ -DEFINE_PERSISTENT_OPTION_STRING(aspect, "4:3", "Video aspect ratio"); +DEFINE_PERSISTENT_OPTION_STRING(aspect, "4:3", "Video aspect ratio"); /* bios */ -DEFINE_PERSISTENT_OPTION_STRING(region, "usa", "System region"); -DEFINE_PERSISTENT_OPTION_STRING(language, "english", "System language"); -DEFINE_PERSISTENT_OPTION_STRING(broadcast, "ntsc", "System broadcast mode"); +DEFINE_PERSISTENT_OPTION_STRING(region, "usa", "System region"); +DEFINE_PERSISTENT_OPTION_STRING(language, "english", "System language"); +DEFINE_PERSISTENT_OPTION_STRING(broadcast, "ntsc", "System broadcast mode"); /* jit */ -DEFINE_OPTION_INT(perf, 0, "Create maps for compiled code for use with perf"); +DEFINE_OPTION_INT(perf, 0, "Create maps for compiled code for use with perf"); /* ui */ -DEFINE_PERSISTENT_OPTION_STRING(gamedir, "", "Directories to scan for games"); +DEFINE_PERSISTENT_OPTION_STRING(gamedir, "", "Directories to scan for games"); /* clang-format on */ diff --git a/src/options.h b/src/options.h index 0544c107..203157f3 100644 --- a/src/options.h +++ b/src/options.h @@ -22,6 +22,8 @@ extern const int NUM_ASPECT_RATIOS; extern struct button_map BUTTONS[]; extern const int NUM_BUTTONS; +extern int *DEADZONES[]; + /* host */ DECLARE_OPTION_STRING(sync); DECLARE_OPTION_INT(bios); @@ -41,6 +43,10 @@ DECLARE_OPTION_INT(key_joyy_neg); DECLARE_OPTION_INT(key_joyy_pos); DECLARE_OPTION_INT(key_ltrig); DECLARE_OPTION_INT(key_rtrig); +DECLARE_OPTION_INT(deadzone_0); +DECLARE_OPTION_INT(deadzone_1); +DECLARE_OPTION_INT(deadzone_2); +DECLARE_OPTION_INT(deadzone_3); /* emulator */ DECLARE_OPTION_STRING(aspect);