changeset 2129:047245361002

Key repeat is handled by the OS, since text input is now decoupled from physical key events.
author Sam Lantinga <slouken@libsdl.org>
date Sat, 16 Jun 2007 15:32:04 +0000
parents 04e9ad5318dc
children 3ee59c43d784
files include/SDL_compat.h include/SDL_keyboard.h src/SDL_compat.c src/events/SDL_events.c src/events/SDL_keyboard.c src/events/SDL_keyboard_c.h src/video/cocoa/SDL_cocoakeyboard.m
diffstat 7 files changed, 38 insertions(+), 158 deletions(-) [+]
line wrap: on
line diff
--- a/include/SDL_compat.h	Sat Jun 16 05:52:34 2007 +0000
+++ b/include/SDL_compat.h	Sat Jun 16 15:32:04 2007 +0000
@@ -64,6 +64,9 @@
 #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 @@
                                                   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
--- a/include/SDL_keyboard.h	Sat Jun 16 05:52:34 2007 +0000
+++ b/include/SDL_keyboard.h	Sat Jun 16 15:32:04 2007 +0000
@@ -93,32 +93,6 @@
 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)
  *
  * \brief Get a snapshot of the current state of the selected keyboard.
--- a/src/SDL_compat.c	Sat Jun 16 05:52:34 2007 +0000
+++ b/src/SDL_compat.c	Sat Jun 16 15:32:04 2007 +0000
@@ -240,6 +240,12 @@
             }
             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_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: */
--- a/src/events/SDL_events.c	Sat Jun 16 05:52:34 2007 +0000
+++ b/src/events/SDL_events.c	Sat Jun 16 15:32:04 2007 +0000
@@ -111,10 +111,6 @@
         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 @@
         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)) {
--- a/src/events/SDL_keyboard.c	Sat Jun 16 05:52:34 2007 +0000
+++ b/src/events/SDL_keyboard.c	Sat Jun 16 15:32:04 2007 +0000
@@ -349,7 +349,6 @@
             SDL_SendKeyboardKey(index, SDL_RELEASED, 0, key);
         }
     }
-    keyboard->repeat.timestamp = 0;
 }
 
 void
@@ -515,7 +514,7 @@
 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 @@
     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 @@
             keyboard->modstate |= KMOD_MODE;
             break;
         default:
-            repeatable = 1;
             break;
         }
     } else {
@@ -616,13 +613,6 @@
         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 @@
         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 @@
     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: */
--- a/src/events/SDL_keyboard_c.h	Sat Jun 16 05:52:34 2007 +0000
+++ b/src/events/SDL_keyboard_c.h	Sat Jun 16 15:32:04 2007 +0000
@@ -39,16 +39,6 @@
     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 @@
 /* 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);
 
--- a/src/video/cocoa/SDL_cocoakeyboard.m	Sat Jun 16 05:52:34 2007 +0000
+++ b/src/video/cocoa/SDL_cocoakeyboard.m	Sat Jun 16 15:32:04 2007 +0000
@@ -532,14 +532,15 @@
 
     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: