No longer depend on SDLK_LAST statically.

Key input should now be safe even if compiled with a different version 
of SDL than the one running the program (Bug #936, possibly also #883)


git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2659 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
mcmartin
2007-01-13 01:35:05 +00:00
parent 13d361f218
commit 1cee0aa1e8
3 changed files with 25 additions and 12 deletions
+3
View File
@@ -1,4 +1,7 @@
Changes towards version 0.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 - Unix build scripts now work under LC_CTYPE=tr_TR - SvdB
- Flush write buffer when doing a uio_fclose() after doing only - Flush write buffer when doing a uio_fclose() after doing only
uio_fwrite() operations. - SvdB uio_fwrite() operations. - SvdB
+9 -3
View File
@@ -24,6 +24,7 @@
#include "libs/input/input_common.h" #include "libs/input/input_common.h"
#include "libs/input/sdl/vcontrol.h" #include "libs/input/sdl/vcontrol.h"
#include "libs/input/sdl/keynames.h" #include "libs/input/sdl/keynames.h"
#include "misc.h"
#include "controls.h" #include "controls.h"
#include "libs/file.h" #include "libs/file.h"
#include "libs/log.h" #include "libs/log.h"
@@ -35,8 +36,9 @@
static int kbdhead=0, kbdtail=0; static int kbdhead=0, kbdtail=0;
static wchar_t kbdbuf[KBDBUFSIZE]; static wchar_t kbdbuf[KBDBUFSIZE];
static wchar_t lastchar; static wchar_t lastchar;
static int kbdstate[SDLK_LAST + 1]; static int num_keys = 0;
// +1 for tracking all yet unknown keys static int *kbdstate = NULL;
// Holds all SDL keys +1 for holding invalid values
static BOOLEAN InputInitialized = FALSE; static BOOLEAN InputInitialized = FALSE;
@@ -191,7 +193,7 @@ initKeyConfig (void)
static void static void
resetKeyboardState (void) resetKeyboardState (void)
{ {
memset (kbdstate, 0, sizeof (kbdstate)); memset (kbdstate, 0, sizeof (int) * num_keys);
ImmediateInputState.menu[KEY_MENU_ANY] = 0; ImmediateInputState.menu[KEY_MENU_ANY] = 0;
} }
@@ -206,6 +208,9 @@ TFB_InitInput (int driver, int flags)
GamePaused = ExitRequested = FALSE; GamePaused = ExitRequested = FALSE;
SDL_EnableUNICODE(1); SDL_EnableUNICODE(1);
(void)SDL_GetKeyState (&num_keys);
kbdstate = (int *)HMalloc (sizeof (int) * num_keys);
#ifdef HAVE_JOYSTICK #ifdef HAVE_JOYSTICK
if ((SDL_InitSubSystem(SDL_INIT_JOYSTICK)) == -1) if ((SDL_InitSubSystem(SDL_INIT_JOYSTICK)) == -1)
@@ -249,6 +254,7 @@ void
TFB_UninitInput (void) TFB_UninitInput (void)
{ {
VControl_Uninit (); VControl_Uninit ();
HFree (kbdstate);
} }
// XXX: not currently used -- character mode is always on // XXX: not currently used -- character mode is always on
+13 -9
View File
@@ -67,7 +67,8 @@ static joystick *joysticks;
#endif /* HAVE_JOYSTICK */ #endif /* HAVE_JOYSTICK */
static int joycount; static int joycount;
static keybinding *bindings[SDLK_LAST]; static int num_sdl_keys = 0;
static keybinding **bindings = NULL;
static keypool *pool; static keypool *pool;
static VControl_NameBinding *nametable; static VControl_NameBinding *nametable;
@@ -194,7 +195,9 @@ key_init (void)
{ {
int i; int i;
pool = allocate_key_chunk (); 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; bindings[i] = NULL;
#ifdef HAVE_JOYSTICK #ifdef HAVE_JOYSTICK
@@ -227,8 +230,9 @@ key_uninit (void)
{ {
int i; int i;
free_key_pool (pool); free_key_pool (pool);
for (i = 0; i < SDLK_LAST; i++) for (i = 0; i < num_sdl_keys; i++)
bindings[i] = NULL; bindings[i] = NULL;
vctrl_free (bindings);
pool = NULL; pool = NULL;
#ifdef HAVE_JOYSTICK #ifdef HAVE_JOYSTICK
@@ -516,7 +520,7 @@ VControl_RemoveGestureBinding (VCONTROL_GESTURE *g, int *target)
int int
VControl_AddKeyBinding (SDLKey symbol, int *target) 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); log_add (log_Warning, "VControl: Illegal key index %d", symbol);
return -1; return -1;
} }
@@ -527,7 +531,7 @@ VControl_AddKeyBinding (SDLKey symbol, int *target)
void void
VControl_RemoveKeyBinding (SDLKey symbol, int *target) 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); log_add (log_Warning, "VControl: Illegal key index %d", symbol);
return; return;
} }
@@ -765,7 +769,7 @@ VControl_RemoveAllBindings ()
void void
VControl_ProcessKeyDown (SDLKey symbol) 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); log_add (log_Warning, "VControl: Got unknown key index %d", symbol);
return; return;
} }
@@ -776,7 +780,7 @@ VControl_ProcessKeyDown (SDLKey symbol)
void void
VControl_ProcessKeyUp (SDLKey symbol) VControl_ProcessKeyUp (SDLKey symbol)
{ {
if ((symbol < 0) || (symbol >= SDLK_LAST)) if ((symbol < 0) || (symbol >= num_sdl_keys))
return; return;
deactivate (bindings[symbol]); deactivate (bindings[symbol]);
@@ -1005,7 +1009,7 @@ VControl_Dump (uio_Stream *out)
PutResFileNewline (out); PutResFileNewline (out);
/* Print out keyboard bindings */ /* Print out keyboard bindings */
for (i = 0; i < SDLK_LAST; i++) for (i = 0; i < num_sdl_keys; i++)
{ {
keybinding *kb = bindings[i]; keybinding *kb = bindings[i];
if (kb != NULL) if (kb != NULL)
@@ -1118,7 +1122,7 @@ VControl_NextBinding (VCONTROL_GESTURE *gesture)
} }
kb = kb->next; kb = kb->next;
} }
if (++iter_index == SDLK_LAST) if (++iter_index == num_sdl_keys)
{ {
iter_device = 0; iter_device = 0;
iter_index = 0; iter_index = 0;