diff --git a/sc2/ChangeLog b/sc2/ChangeLog index 9015b0565..6a8982499 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,7 @@ Changes towards version 0.7: +- No longer depend on SDLK_LAST statically; key input should now be safe + if compiled with a different version of SDL than the one running the + program (Bug #936, possibly also #883) - Michael - Unix build scripts now work under LC_CTYPE=tr_TR - SvdB - Flush write buffer when doing a uio_fclose() after doing only uio_fwrite() operations. - SvdB diff --git a/sc2/src/sc2code/libs/input/sdl/input.c b/sc2/src/sc2code/libs/input/sdl/input.c index b8436e648..d70cdceab 100644 --- a/sc2/src/sc2code/libs/input/sdl/input.c +++ b/sc2/src/sc2code/libs/input/sdl/input.c @@ -24,6 +24,7 @@ #include "libs/input/input_common.h" #include "libs/input/sdl/vcontrol.h" #include "libs/input/sdl/keynames.h" +#include "misc.h" #include "controls.h" #include "libs/file.h" #include "libs/log.h" @@ -35,8 +36,9 @@ static int kbdhead=0, kbdtail=0; static wchar_t kbdbuf[KBDBUFSIZE]; static wchar_t lastchar; -static int kbdstate[SDLK_LAST + 1]; - // +1 for tracking all yet unknown keys +static int num_keys = 0; +static int *kbdstate = NULL; + // Holds all SDL keys +1 for holding invalid values static BOOLEAN InputInitialized = FALSE; @@ -191,7 +193,7 @@ initKeyConfig (void) static void resetKeyboardState (void) { - memset (kbdstate, 0, sizeof (kbdstate)); + memset (kbdstate, 0, sizeof (int) * num_keys); ImmediateInputState.menu[KEY_MENU_ANY] = 0; } @@ -206,6 +208,9 @@ TFB_InitInput (int driver, int flags) GamePaused = ExitRequested = FALSE; SDL_EnableUNICODE(1); + (void)SDL_GetKeyState (&num_keys); + kbdstate = (int *)HMalloc (sizeof (int) * num_keys); + #ifdef HAVE_JOYSTICK if ((SDL_InitSubSystem(SDL_INIT_JOYSTICK)) == -1) @@ -249,6 +254,7 @@ void TFB_UninitInput (void) { VControl_Uninit (); + HFree (kbdstate); } // XXX: not currently used -- character mode is always on diff --git a/sc2/src/sc2code/libs/input/sdl/vcontrol.c b/sc2/src/sc2code/libs/input/sdl/vcontrol.c index 8cd2b3b2c..0d34a323c 100644 --- a/sc2/src/sc2code/libs/input/sdl/vcontrol.c +++ b/sc2/src/sc2code/libs/input/sdl/vcontrol.c @@ -67,7 +67,8 @@ static joystick *joysticks; #endif /* HAVE_JOYSTICK */ static int joycount; -static keybinding *bindings[SDLK_LAST]; +static int num_sdl_keys = 0; +static keybinding **bindings = NULL; static keypool *pool; static VControl_NameBinding *nametable; @@ -194,7 +195,9 @@ key_init (void) { int i; pool = allocate_key_chunk (); - for (i = 0; i < SDLK_LAST; i++) + (void)SDL_GetKeyState (&num_sdl_keys); + bindings = (keybinding **) vctrl_malloc (sizeof (keybinding *) * num_sdl_keys); + for (i = 0; i < num_sdl_keys; i++) bindings[i] = NULL; #ifdef HAVE_JOYSTICK @@ -227,8 +230,9 @@ key_uninit (void) { int i; free_key_pool (pool); - for (i = 0; i < SDLK_LAST; i++) + for (i = 0; i < num_sdl_keys; i++) bindings[i] = NULL; + vctrl_free (bindings); pool = NULL; #ifdef HAVE_JOYSTICK @@ -516,7 +520,7 @@ VControl_RemoveGestureBinding (VCONTROL_GESTURE *g, int *target) int VControl_AddKeyBinding (SDLKey symbol, int *target) { - if ((symbol < 0) || (symbol >= SDLK_LAST)) { + if ((symbol < 0) || (symbol >= num_sdl_keys)) { log_add (log_Warning, "VControl: Illegal key index %d", symbol); return -1; } @@ -527,7 +531,7 @@ VControl_AddKeyBinding (SDLKey symbol, int *target) void VControl_RemoveKeyBinding (SDLKey symbol, int *target) { - if ((symbol < 0) || (symbol >= SDLK_LAST)) { + if ((symbol < 0) || (symbol >= num_sdl_keys)) { log_add (log_Warning, "VControl: Illegal key index %d", symbol); return; } @@ -765,7 +769,7 @@ VControl_RemoveAllBindings () void VControl_ProcessKeyDown (SDLKey symbol) { - if ((symbol < 0) || (symbol >= SDLK_LAST)) { + if ((symbol < 0) || (symbol >= num_sdl_keys)) { log_add (log_Warning, "VControl: Got unknown key index %d", symbol); return; } @@ -776,7 +780,7 @@ VControl_ProcessKeyDown (SDLKey symbol) void VControl_ProcessKeyUp (SDLKey symbol) { - if ((symbol < 0) || (symbol >= SDLK_LAST)) + if ((symbol < 0) || (symbol >= num_sdl_keys)) return; deactivate (bindings[symbol]); @@ -1005,7 +1009,7 @@ VControl_Dump (uio_Stream *out) PutResFileNewline (out); /* Print out keyboard bindings */ - for (i = 0; i < SDLK_LAST; i++) + for (i = 0; i < num_sdl_keys; i++) { keybinding *kb = bindings[i]; if (kb != NULL) @@ -1118,7 +1122,7 @@ VControl_NextBinding (VCONTROL_GESTURE *gesture) } kb = kb->next; } - if (++iter_index == SDLK_LAST) + if (++iter_index == num_sdl_keys) { iter_device = 0; iter_index = 0;