From 0ff66268e71a45a4e78773fdce77d1f40db99a08 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 28 Jan 2013 18:06:30 +0900 Subject: [PATCH] Redo the button axis setup to use key names. Johnny's number->J_AXISn mapping is preserved, but I had intended for any key to be supported (J_AXISn was just to ensure free keys were available). This gives both methods (and some range checking on the axis button number). --- libs/video/targets/joy.c | 50 ++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/libs/video/targets/joy.c b/libs/video/targets/joy.c index 705f1bc06..9262dce9c 100644 --- a/libs/video/targets/joy.c +++ b/libs/video/targets/joy.c @@ -266,6 +266,40 @@ JOY_GetDest_i (const char *c) return -1; // Failure code; } +static void +in_joy_button_add_f (int ax, int index) +{ + int n; + size_t size; + const char *key = Cmd_Argv (index); + int keynum; + const char *thrsh = Cmd_Argv (index + 1); + float threshold; + char *end = 0; + + keynum = strtol (key, &end, 10) + QFJ_AXIS1; + if (*end || keynum < QFJ_AXIS1 || keynum > QFJ_AXIS32) { + // if the key is not valid, try a key name + keynum = Key_StringToKeynum (key); + } + if (keynum == -1) { + Sys_Printf ("\"%s\" isn't a valid key\n", key); + } + threshold = strtof (thrsh, &end); + if (*end) { + Sys_Printf ("invalid threshold: %s\n", thrsh); + keynum = -1; + } + if (keynum == -1) + return; + + n = joy_axes[ax].num_buttons++; + size = n * sizeof (joy_axes[ax].axis_buttons); + joy_axes[ax].axis_buttons = realloc (joy_axes[ax].axis_buttons, size); + joy_axes[ax].axis_buttons[n].key = keynum; + joy_axes[ax].axis_buttons[n].threshold = threshold; +} + static void in_joy_f (void) { @@ -361,16 +395,8 @@ in_joy_f (void) case js_axis_button: arg = Cmd_Argv (i++); if (!strcmp ("add", arg)) { - int n = joy_axes[ax].num_buttons++; - - joy_axes[ax].axis_buttons = - realloc (joy_axes[ax].axis_buttons, - n * sizeof (joy_axes[ax].axis_buttons)); - - joy_axes[ax].axis_buttons[n].key = - strtol (Cmd_Argv (i++), NULL, 10) + QFJ_AXIS1; - joy_axes[ax].axis_buttons[n].threshold = - strtof (Cmd_Argv (i++), NULL); + in_joy_button_add_f (ax, i); + i += 2; } break; default: @@ -421,8 +447,8 @@ Joy_WriteBindings (QFile * f) int n; for (n = 0; n < joy_axes[i].num_buttons; n++) { - Qprintf (f, "in_joy %i button add %i %.9g\n", i, - joy_axes[i].axis_buttons[n].key - QFJ_AXIS1, + Qprintf (f, "in_joy %i button add %s %.9g\n", i, + Key_KeynumToString (joy_axes[i].axis_buttons[n].key), joy_axes[i].axis_buttons[n].threshold); } }