From 80dcd123dc5c7ee88936bfbe28575355a12dfd95 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 16 Jun 2007 15:32:04 +0000 Subject: [PATCH] Key repeat is handled by the OS, since text input is now decoupled from physical key events. --HG-- extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%402368 --- include/SDL_compat.h | 5 ++ include/SDL_keyboard.h | 26 ------- src/SDL_compat.c | 24 +++++++ src/events/SDL_events.c | 8 --- src/events/SDL_keyboard.c | 105 +--------------------------- src/events/SDL_keyboard_c.h | 13 ---- src/video/cocoa/SDL_cocoakeyboard.m | 15 ++-- 7 files changed, 38 insertions(+), 158 deletions(-) diff --git a/include/SDL_compat.h b/include/SDL_compat.h index 3cb7f0a11..bac3ca225 100644 --- a/include/SDL_compat.h +++ b/include/SDL_compat.h @@ -64,6 +64,9 @@ extern "C" { #define SDL_BUTTON_WHEELUP 4 #define SDL_BUTTON_WHEELDOWN 5 +#define SDL_DEFAULT_REPEAT_DELAY 500 +#define SDL_DEFAULT_REPEAT_INTERVAL 30 + typedef struct SDL_VideoInfo { Uint32 hw_available:1; @@ -173,6 +176,8 @@ extern DECLSPEC int SDLCALL SDL_DisplayYUVOverlay(SDL_Overlay * overlay, SDL_Rect * dstrect); extern DECLSPEC void SDLCALL SDL_FreeYUVOverlay(SDL_Overlay * overlay); extern DECLSPEC void SDLCALL SDL_GL_SwapBuffers(void); +extern DECLSPEC int SDLCALL SDL_EnableKeyRepeat(int delay, int interval); +extern DECLSPEC void SDLCALL SDL_GetKeyRepeat(int *delay, int *interval); /* Ends C function definitions when using C++ */ #ifdef __cplusplus diff --git a/include/SDL_keyboard.h b/include/SDL_keyboard.h index 9652dfb4f..089389a54 100644 --- a/include/SDL_keyboard.h +++ b/include/SDL_keyboard.h @@ -92,32 +92,6 @@ extern DECLSPEC int SDLCALL SDL_SelectKeyboard(int index); */ extern DECLSPEC int SDLCALL SDL_EnableUNICODE(int enable); -/** - * \fn int SDL_EnableKeyRepeat(int delay, int interval) - * - * \brief Enable keyboard repeat for the selected keyboard. - * - * \param delay The initial delay in milliseconds between the time when a - * key is pressed and keyboard repeat begins. Setting a delay - * of 0 will disable keyboard repeat. - * \param interval The time in milliseconds between keyboard repeat events. - * - * \return 0 on success, or -1 if there was an error. - * - * \note Keyboard repeat defaults to off. - */ -#define SDL_DEFAULT_REPEAT_DELAY 500 -#define SDL_DEFAULT_REPEAT_INTERVAL 30 - /**/ - extern DECLSPEC int SDLCALL SDL_EnableKeyRepeat(int delay, int interval); - -/** - * \fn void SDL_GetKeyRepeat(int *delay, int *interval) - * - * \brief Get the current keyboard repeat setting for the selected keyboard. - */ -extern DECLSPEC void SDLCALL SDL_GetKeyRepeat(int *delay, int *interval); - /** * \fn Uint8 *SDL_GetKeyState(int *numkeys) * diff --git a/src/SDL_compat.c b/src/SDL_compat.c index e2d51d0a5..a29a1a7ce 100644 --- a/src/SDL_compat.c +++ b/src/SDL_compat.c @@ -240,6 +240,12 @@ SDL_CompatEventFilter(void *userdata, SDL_Event * event) } break; } + case SDL_TEXTINPUT: + { + /* FIXME: Generate an old style key repeat event if needed */ + printf("TEXTINPUT: '%s'\n", event->text.text); + break; + } case SDL_MOUSEWHEEL: { Uint8 button; @@ -1447,4 +1453,22 @@ SDL_GL_SwapBuffers(void) SDL_GL_SwapWindow(SDL_VideoWindow); } + +int +SDL_EnableKeyRepeat(int delay, int interval) +{ + return 0; +} + +void +SDL_GetKeyRepeat(int *delay, int *interval) +{ + if (delay) { + *delay = SDL_DEFAULT_REPEAT_DELAY; + } + if (interval) { + *interval = SDL_DEFAULT_REPEAT_INTERVAL; + } +} + /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c index ea96fdcc7..364182eb3 100644 --- a/src/events/SDL_events.c +++ b/src/events/SDL_events.c @@ -111,10 +111,6 @@ SDL_GobbleEvents(void *unused) if (_this) { _this->PumpEvents(_this); } - - /* Queue pending key-repeat events */ - SDL_CheckKeyRepeat(); - #if !SDL_JOYSTICK_DISABLED /* Check for joystick state change */ if (SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK)) { @@ -386,10 +382,6 @@ SDL_PumpEvents(void) if (_this) { _this->PumpEvents(_this); } - - /* Queue pending key-repeat events */ - SDL_CheckKeyRepeat(); - #if !SDL_JOYSTICK_DISABLED /* Check for joystick state change */ if (SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK)) { diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c index 77f878923..87db81bdc 100644 --- a/src/events/SDL_keyboard.c +++ b/src/events/SDL_keyboard.c @@ -349,7 +349,6 @@ SDL_ResetKeyboard(int index) SDL_SendKeyboardKey(index, SDL_RELEASED, 0, key); } } - keyboard->repeat.timestamp = 0; } void @@ -515,7 +514,7 @@ int SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key) { SDL_Keyboard *keyboard = SDL_GetKeyboard(index); - int posted, repeatable; + int posted; Uint16 modstate; Uint8 type; @@ -526,7 +525,6 @@ SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key) printf("The '%s' key has been %s\n", SDL_GetKeyName(key), state == SDL_PRESSED ? "pressed" : "released"); #endif - repeatable = 0; if (state == SDL_PRESSED) { modstate = keyboard->modstate; switch (key) { @@ -566,7 +564,6 @@ SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key) keyboard->modstate |= KMOD_MODE; break; default: - repeatable = 1; break; } } else { @@ -616,13 +613,6 @@ SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key) break; case SDL_RELEASED: type = SDL_KEYUP; - /* - * jk 991215 - Added - */ - if (keyboard->repeat.timestamp && - keyboard->repeat.evt.key.keysym.sym == key) { - keyboard->repeat.timestamp = 0; - } break; default: /* Invalid state -- bail */ @@ -654,19 +644,6 @@ SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key) event.key.keysym.mod = modstate; event.key.keysym.unicode = 0; event.key.windowID = keyboard->focus; - /* FIXME: This doesn't make sense anymore... */ - /* - * jk 991215 - Added - */ - if (repeatable && (keyboard->repeat.delay != 0)) { - Uint32 timestamp = SDL_GetTicks(); - if (!timestamp) { - timestamp = 1; - } - keyboard->repeat.evt = event; - keyboard->repeat.firsttime = 1; - keyboard->repeat.timestamp = 1; - } posted = (SDL_PushEvent(&event) > 0); } return (posted); @@ -695,84 +672,4 @@ SDL_SendKeyboardText(int index, const char *text) return (posted); } -/* - * jk 991215 - Added - */ -void -SDL_CheckKeyRepeat(void) -{ - int i; - - for (i = 0; i < SDL_num_keyboards; ++i) { - SDL_Keyboard *keyboard = SDL_keyboards[i]; - - if (!keyboard) { - continue; - } - - if (keyboard->repeat.timestamp) { - Uint32 now, interval; - - now = SDL_GetTicks(); - interval = (now - keyboard->repeat.timestamp); - if (keyboard->repeat.firsttime) { - if (interval > (Uint32) keyboard->repeat.delay) { - keyboard->repeat.timestamp = now; - keyboard->repeat.firsttime = 0; - } - } else { - if (interval > (Uint32) keyboard->repeat.interval) { - keyboard->repeat.timestamp = now; - SDL_PushEvent(&keyboard->repeat.evt); - } - } - } - } -} - -int -SDL_EnableKeyRepeat(int delay, int interval) -{ - SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard); - - if (!keyboard) { - SDL_SetError("No keyboard is currently selected"); - return -1; - } - - if ((delay < 0) || (interval < 0)) { - SDL_SetError("keyboard repeat value less than zero"); - return -1; - } - - keyboard->repeat.firsttime = 0; - keyboard->repeat.delay = delay; - keyboard->repeat.interval = interval; - keyboard->repeat.timestamp = 0; - - return 0; -} - -void -SDL_GetKeyRepeat(int *delay, int *interval) -{ - SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard); - - if (!keyboard) { - if (delay) { - *delay = 0; - } - if (interval) { - *interval = 0; - } - return; - } - if (delay) { - *delay = keyboard->repeat.delay; - } - if (interval) { - *interval = keyboard->repeat.interval; - } -} - /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/events/SDL_keyboard_c.h b/src/events/SDL_keyboard_c.h index 0d0b58627..0158ee215 100644 --- a/src/events/SDL_keyboard_c.h +++ b/src/events/SDL_keyboard_c.h @@ -39,16 +39,6 @@ struct SDL_Keyboard Uint16 modstate; Uint8 keystate[SDLK_LAST]; - struct - { - int firsttime; /* if we check against the delay or repeat value */ - int delay; /* the delay before we start repeating */ - int interval; /* the delay between key repeat events */ - Uint32 timestamp; /* the time the first keydown event occurred */ - - SDL_Event evt; /* the event we are supposed to repeat */ - } repeat; - void *driverdata; }; @@ -85,9 +75,6 @@ extern int SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, /* Send keyboard text input for a keyboard at an index */ extern int SDL_SendKeyboardText(int index, const char *text); -/* Used by the event loop to queue pending keyboard repeat events */ -extern void SDL_CheckKeyRepeat(void); - /* Shutdown the keyboard subsystem */ extern void SDL_KeyboardQuit(void); diff --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m index 17da0694c..89a9a0180 100644 --- a/src/video/cocoa/SDL_cocoakeyboard.m +++ b/src/video/cocoa/SDL_cocoakeyboard.m @@ -532,14 +532,15 @@ Cocoa_HandleKeyEvent(_THIS, NSEvent *event) switch ([event type]) { case NSKeyDown: - if ([event isARepeat]) { - break; + if (![event isARepeat]) { + SDL_SendKeyboardKey(data->keyboard, SDL_PRESSED, (Uint8)scancode, + data->keymap[scancode]); } - SDL_SendKeyboardKey(data->keyboard, SDL_PRESSED, (Uint8)scancode, - data->keymap[scancode]); - text = [[event characters] UTF8String]; - if(text && *text) { - SDL_SendKeyboardText(data->keyboard, text); + if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) { + text = [[event characters] UTF8String]; + if(text && *text) { + SDL_SendKeyboardText(data->keyboard, text); + } } break; case NSKeyUp: