Fixed race condition and potential b/o in char input

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2528 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
avolkov
2006-11-24 14:50:52 +00:00
parent 2d32359778
commit 9e0dea0293
+16 -6
View File
@@ -35,7 +35,8 @@
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]; static int kbdstate[SDLK_LAST + 1];
// +1 for tracking all yet unknown keys
static BOOLEAN InputInitialized = FALSE; static BOOLEAN InputInitialized = FALSE;
@@ -315,24 +316,33 @@ ProcessInputEvent (const SDL_Event *Event)
ProcessMouseEvent (Event); ProcessMouseEvent (Event);
VControl_HandleEvent (Event); VControl_HandleEvent (Event);
if (Event->type == SDL_KEYDOWN || Event->type == SDL_KEYUP)
{ // process character input event, if any { // process character input event, if any
SDLKey k = Event->key.keysym.sym; SDLKey k = Event->key.keysym.sym;
wchar_t map_key = Event->key.keysym.unicode; wchar_t map_key = Event->key.keysym.unicode;
if (k < 0 || k > SDLK_LAST)
k = SDLK_LAST; // for unknown keys
if (Event->type == SDL_KEYDOWN) if (Event->type == SDL_KEYDOWN)
{ {
int newtail;
// dont care about the non-printable, non-char // dont care about the non-printable, non-char
if (!map_key) if (!map_key)
return; return;
kbdstate[k]++; kbdstate[k]++;
ImmediateInputState.menu[KEY_MENU_ANY]++;
lastchar = map_key; newtail = (kbdtail + 1) & (KBDBUFSIZE - 1);
// ignore the char if the buffer is full
if (newtail != kbdhead)
{
kbdbuf[kbdtail] = map_key; kbdbuf[kbdtail] = map_key;
kbdtail = (kbdtail + 1) & (KBDBUFSIZE - 1); kbdtail = newtail;
if (kbdtail == kbdhead) lastchar = map_key;
kbdhead = (kbdhead + 1) & (KBDBUFSIZE - 1); ImmediateInputState.menu[KEY_MENU_ANY]++;
}
} }
else if (Event->type == SDL_KEYUP) else if (Event->type == SDL_KEYUP)
{ {