diff src/video/x11/SDL_x11events.c @ 4565:e2d46c5c7483

Fixed key repeat detection on X11, and simplified the code for everyone else.
author Sam Lantinga <slouken@libsdl.org>
date Wed, 21 Jul 2010 21:47:12 -0700
parents 95352c671a6e
children c24ba2cc9583
line wrap: on
line diff
--- a/src/video/x11/SDL_x11events.c	Wed Jul 21 00:11:56 2010 -0700
+++ b/src/video/x11/SDL_x11events.c	Wed Jul 21 21:47:12 2010 -0700
@@ -36,6 +36,24 @@
 
 /*#define DEBUG_XEVENTS*/
 
+/* Check to see if this is a repeated key.
+   (idea shamelessly lifted from GII -- thanks guys! :)
+ */
+static SDL_bool X11_KeyRepeat(Display *display, XEvent *event)
+{
+    XEvent peekevent;
+
+    if (XPending(display)) {
+        XPeekEvent(display, &peekevent);
+        if ((peekevent.type == KeyPress) &&
+            (peekevent.xkey.keycode == event->xkey.keycode) &&
+            ((peekevent.xkey.time-event->xkey.time) < 2)) {
+            return SDL_TRUE;
+        }
+    }
+    return SDL_FALSE;
+}
+
 static void
 X11_DispatchEvent(_THIS)
 {
@@ -176,14 +194,14 @@
     case KeyPress:{
             KeyCode keycode = xevent.xkey.keycode;
             KeySym keysym = NoSymbol;
+            SDL_scancode scancode;
             char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
             Status status = 0;
 
 #ifdef DEBUG_XEVENTS
             printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
 #endif
-            /* FIXME: How do we tell if this was a key repeat? */
-            SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode], SDL_FALSE);
+            SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
 #if 1
             if (videodata->key_layout[keycode] == SDLK_UNKNOWN) {
                 int min_keycode, max_keycode;
@@ -218,7 +236,11 @@
 #ifdef DEBUG_XEVENTS
             printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
 #endif
-            SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode], SDL_FALSE);
+            if (X11_KeyRepeat(display, &xevent)) {
+                /* We're about to get a repeated key down, ignore the key up */
+                break;
+            }
+            SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
         }
         break;