Fixed bug 2736 - X11 doesn't set KMOD_NUM and KMOD_CAPS to system state
authorSam Lantinga <slouken@libsdl.org>
Thu, 28 May 2015 09:33:47 -0700
changeset 96667099e2df7b29
parent 9665 8d24b4bfa3dd
child 9667 e55dad50b3ae
Fixed bug 2736 - X11 doesn't set KMOD_NUM and KMOD_CAPS to system state

Zack Middleton

Using X11 (on Debian Wheezy), SDL_GetModState() & KMOD_NUM and KMOD_CAPS are not set to system state (numlock/capslock LEDs). Pressing numlock or capslock toggles the mod state, though if num/caps lock is enabled before starting the program it's still reversed from system state. This makes getting KMOD_NUM and KMOD_CAPS in programs unreliable. This can be seen using the checkkeys test program.

The function that appears to have handle this in SDL 1.2 is X11_SetKeyboardState. The function call is commented out with "FIXME:" in SDL 2.

Using Windows backend through WINE; on first key press if numlock and/or capslock is enabled on system, numlock/capslock SDL_SendKeyboardKey is run and toggles KMOD_NUM/KMOD_CAPS to the correct state. On X11 this does not happen.

The attached patch makes X11 backend set keyboard state on window focus if no window was previously focused. It sets all keys to system up/down state and toggles KMOD_NUM/KMOD_CAPS via SDL_SendKeyboardKey to match system if needed. The patch is based on SDL 1.2's X11_SetKeyboardState.
src/video/x11/SDL_x11events.c
     1.1 --- a/src/video/x11/SDL_x11events.c	Thu May 28 08:51:59 2015 -0700
     1.2 +++ b/src/video/x11/SDL_x11events.c	Thu May 28 09:33:47 2015 -0700
     1.3 @@ -293,27 +293,27 @@
     1.4      if (memcmp(uri,"file:/",6) == 0) uri += 6;      /* local file? */
     1.5      else if (strstr(uri,":/") != NULL) return file; /* wrong scheme */
     1.6  
     1.7 -    local = uri[0] != '/' || ( uri[0] != '\0' && uri[1] == '/' );
     1.8 +    local = uri[0] != '/' || (uri[0] != '\0' && uri[1] == '/');
     1.9  
    1.10      /* got a hostname? */
    1.11 -    if ( !local && uri[0] == '/' && uri[2] != '/' ) {
    1.12 -      char* hostname_end = strchr( uri+1, '/' );
    1.13 -      if ( hostname_end != NULL ) {
    1.14 +    if (!local && uri[0] == '/' && uri[2] != '/') {
    1.15 +      char* hostname_end = strchr(uri+1, '/');
    1.16 +      if (hostname_end != NULL) {
    1.17            char hostname[ 257 ];
    1.18 -          if ( gethostname( hostname, 255 ) == 0 ) {
    1.19 +          if (gethostname(hostname, 255) == 0) {
    1.20              hostname[ 256 ] = '\0';
    1.21 -            if ( memcmp( uri+1, hostname, hostname_end - ( uri+1 )) == 0 ) {
    1.22 +            if (memcmp(uri+1, hostname, hostname_end - (uri+1)) == 0) {
    1.23                  uri = hostname_end + 1;
    1.24                  local = SDL_TRUE;
    1.25              }
    1.26            }
    1.27        }
    1.28      }
    1.29 -    if ( local ) {
    1.30 +    if (local) {
    1.31        file = uri;
    1.32        /* Convert URI escape sequences to real characters */
    1.33        X11_URIDecode(file, 0);
    1.34 -      if ( uri[1] == '/' ) {
    1.35 +      if (uri[1] == '/') {
    1.36            file++;
    1.37        } else {
    1.38            file--;
    1.39 @@ -336,12 +336,13 @@
    1.40  
    1.41  
    1.42  static void
    1.43 -X11_DispatchFocusIn(SDL_WindowData *data)
    1.44 +X11_DispatchFocusIn(_THIS, SDL_WindowData *data)
    1.45  {
    1.46  #ifdef DEBUG_XEVENTS
    1.47      printf("window %p: Dispatching FocusIn\n", data);
    1.48  #endif
    1.49      SDL_SetKeyboardFocus(data->window);
    1.50 +    ReconcileKeyboardState(_this, data);
    1.51  #ifdef X_HAVE_UTF8_STRING
    1.52      if (data->ic) {
    1.53          X11_XSetICFocus(data->ic);
    1.54 @@ -353,7 +354,7 @@
    1.55  }
    1.56  
    1.57  static void
    1.58 -X11_DispatchFocusOut(SDL_WindowData *data)
    1.59 +X11_DispatchFocusOut(_THIS, SDL_WindowData *data)
    1.60  {
    1.61  #ifdef DEBUG_XEVENTS
    1.62      printf("window %p: Dispatching FocusOut\n", data);
    1.63 @@ -482,23 +483,74 @@
    1.64      return SDL_FALSE;
    1.65  }
    1.66  
    1.67 +static unsigned
    1.68 +GetNumLockModifierMask(_THIS)
    1.69 +{
    1.70 +    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
    1.71 +    Display *display = viddata->display;
    1.72 +    unsigned num_mask = 0;
    1.73 +    int i, j;
    1.74 +    XModifierKeymap *xmods;
    1.75 +    unsigned n;
    1.76 +
    1.77 +    xmods = X11_XGetModifierMapping(display);
    1.78 +    n = xmods->max_keypermod;
    1.79 +    for(i = 3; i < 8; i++) {
    1.80 +        for(j = 0; j < n; j++) {
    1.81 +            KeyCode kc = xmods->modifiermap[i * n + j];
    1.82 +            if (viddata->key_layout[kc] == SDL_SCANCODE_NUMLOCKCLEAR) {
    1.83 +                num_mask = 1 << i;
    1.84 +                break;
    1.85 +            }
    1.86 +        }
    1.87 +    }
    1.88 +    X11_XFreeModifiermap(xmods);
    1.89 +
    1.90 +    return num_mask;
    1.91 +}
    1.92 +
    1.93  static void
    1.94  ReconcileKeyboardState(_THIS, const SDL_WindowData *data)
    1.95  {
    1.96      SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
    1.97      Display *display = viddata->display;
    1.98      char keys[32];
    1.99 -    int keycode = 0;
   1.100 +    int keycode;
   1.101 +    Window junk_window;
   1.102 +    int x, y;
   1.103 +    unsigned int mask;
   1.104  
   1.105 -    X11_XQueryKeymap( display, keys );
   1.106 +    X11_XQueryKeymap(display, keys);
   1.107  
   1.108 -    while ( keycode < 256 ) {
   1.109 -        if ( keys[keycode / 8] & (1 << (keycode % 8)) ) {
   1.110 +    for (keycode = 0; keycode < 256; ++keycode) {
   1.111 +        if (keys[keycode / 8] & (1 << (keycode % 8))) {
   1.112              SDL_SendKeyboardKey(SDL_PRESSED, viddata->key_layout[keycode]);
   1.113          } else {
   1.114              SDL_SendKeyboardKey(SDL_RELEASED, viddata->key_layout[keycode]);
   1.115          }
   1.116 -        keycode++;
   1.117 +    }
   1.118 +
   1.119 +    /* Get the keyboard modifier state */
   1.120 +    if (X11_XQueryPointer(display, DefaultRootWindow(display), &junk_window, &junk_window, &x, &y, &x, &y, &mask)) {
   1.121 +        unsigned num_mask = GetNumLockModifierMask(_this);
   1.122 +        const Uint8 *keystate = SDL_GetKeyboardState(NULL);
   1.123 +        Uint8 capslockState = keystate[SDL_SCANCODE_CAPSLOCK];
   1.124 +        Uint8 numlockState = keystate[SDL_SCANCODE_NUMLOCKCLEAR];
   1.125 +
   1.126 +        /* Toggle key mod state if needed */
   1.127 +        if (!!(mask & LockMask) != !!(SDL_GetModState() & KMOD_CAPS)) {
   1.128 +            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
   1.129 +            if (capslockState == SDL_RELEASED) {
   1.130 +                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
   1.131 +            }
   1.132 +        }
   1.133 +
   1.134 +        if (!!(mask & num_mask) != !!(SDL_GetModState() & KMOD_NUM)) {
   1.135 +            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_NUMLOCKCLEAR);
   1.136 +            if (numlockState == SDL_RELEASED) {
   1.137 +                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_NUMLOCKCLEAR);
   1.138 +            }
   1.139 +        }
   1.140      }
   1.141  }
   1.142  
   1.143 @@ -648,15 +700,11 @@
   1.144  #ifdef DEBUG_XEVENTS
   1.145              printf("window %p: FocusIn!\n", data);
   1.146  #endif
   1.147 -            if (data->pending_focus == PENDING_FOCUS_OUT &&
   1.148 -                data->window == SDL_GetKeyboardFocus()) {
   1.149 -                ReconcileKeyboardState(_this, data);
   1.150 -            }
   1.151              if (!videodata->last_mode_change_deadline) /* no recent mode changes */
   1.152              {
   1.153                  data->pending_focus = PENDING_FOCUS_NONE;
   1.154                  data->pending_focus_time = 0;
   1.155 -                X11_DispatchFocusIn(data);
   1.156 +                X11_DispatchFocusIn(_this, data);
   1.157              }
   1.158              else
   1.159              {
   1.160 @@ -863,7 +911,7 @@
   1.161  
   1.162                  SDL_bool use_list = xevent.xclient.data.l[1] & 1;
   1.163                  data->xdnd_source = xevent.xclient.data.l[0];
   1.164 -                xdnd_version = ( xevent.xclient.data.l[1] >> 24);
   1.165 +                xdnd_version = (xevent.xclient.data.l[1] >> 24);
   1.166  #ifdef DEBUG_XEVENTS
   1.167                  printf("XID of source window : %ld\n", data->xdnd_source);
   1.168                  printf("Protocol version to use : %ld\n", xdnd_version);
   1.169 @@ -1250,10 +1298,10 @@
   1.170              if (data && data->pending_focus != PENDING_FOCUS_NONE) {
   1.171                  Uint32 now = SDL_GetTicks();
   1.172                  if (SDL_TICKS_PASSED(now, data->pending_focus_time)) {
   1.173 -                    if ( data->pending_focus == PENDING_FOCUS_IN ) {
   1.174 -                        X11_DispatchFocusIn(data);
   1.175 +                    if (data->pending_focus == PENDING_FOCUS_IN) {
   1.176 +                        X11_DispatchFocusIn(_this, data);
   1.177                      } else {
   1.178 -                        X11_DispatchFocusOut(data);
   1.179 +                        X11_DispatchFocusOut(_this, data);
   1.180                      }
   1.181                      data->pending_focus = PENDING_FOCUS_NONE;
   1.182                  }