[X11] Reconcile logical keyboard state with physical state on FocusIn
authorPierre-Loup A. Griffais <pgriffais@valvesoftware.com>
Thu, 11 Sep 2014 19:24:42 -0700
changeset 913352f185c2d019
parent 9129 d183503df2e8
child 9134 dbd8fe54ebb9
[X11] Reconcile logical keyboard state with physical state on FocusIn
since the window system doesn't do it for us like other platforms.

This prevents sticky keys and missed keys when going in and out
of focus, for example Alt would appear to stick if switching away
from an SDL app with Alt-Tab and had to be pressed again.

CR: Sam
src/video/x11/SDL_x11events.c
     1.1 --- a/src/video/x11/SDL_x11events.c	Wed Sep 10 14:05:27 2014 -0700
     1.2 +++ b/src/video/x11/SDL_x11events.c	Thu Sep 11 19:24:42 2014 -0700
     1.3 @@ -508,6 +508,27 @@
     1.4  }
     1.5  
     1.6  static void
     1.7 +ReconcileKeyboardState(_THIS, const SDL_WindowData *data)
     1.8 +{
     1.9 +    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
    1.10 +    SDL_Window* window = data->window;
    1.11 +    Display *display = viddata->display;
    1.12 +    char keys[32];
    1.13 +    int keycode = 0;
    1.14 +
    1.15 +    X11_XQueryKeymap( display, keys );
    1.16 +
    1.17 +    while ( keycode < 256 ) {
    1.18 +        if ( keys[keycode / 8] & (1 << (keycode % 8)) ) {
    1.19 +            SDL_SendKeyboardKey(SDL_PRESSED, viddata->key_layout[keycode]);
    1.20 +        } else {
    1.21 +            SDL_SendKeyboardKey(SDL_RELEASED, viddata->key_layout[keycode]);
    1.22 +        }
    1.23 +        keycode++;
    1.24 +    }
    1.25 +}
    1.26 +
    1.27 +static void
    1.28  X11_DispatchEvent(_THIS)
    1.29  {
    1.30      SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
    1.31 @@ -655,16 +676,7 @@
    1.32  #endif
    1.33              if (data->pending_focus == PENDING_FOCUS_OUT &&
    1.34                  data->window == SDL_GetKeyboardFocus()) {
    1.35 -                /* We want to reset the keyboard here, because we may have
    1.36 -                   missed keyboard messages after our previous FocusOut.
    1.37 -                 */
    1.38 -                /* Actually, if we do this we clear the ALT key on Unity
    1.39 -                   because it briefly takes focus for their dashboard.
    1.40 -
    1.41 -                   I think it's better to think the ALT key is held down
    1.42 -                   when it's not, then always lose the ALT modifier on Unity.
    1.43 -                 */
    1.44 -                /* SDL_ResetKeyboard(); */
    1.45 +                ReconcileKeyboardState(_this, data);
    1.46              }
    1.47              data->pending_focus = PENDING_FOCUS_IN;
    1.48              data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_IN_TIME;