Fixed X11 build, added code to print initial modifiers to checkkeys
authorSam Lantinga <slouken@libsdl.org>
Thu, 28 May 2015 09:52:48 -0700
changeset 9667e55dad50b3ae
parent 9666 7099e2df7b29
child 9668 6ab4d6f34afb
Fixed X11 build, added code to print initial modifiers to checkkeys
src/video/x11/SDL_x11events.c
test/checkkeys.c
     1.1 --- a/src/video/x11/SDL_x11events.c	Thu May 28 09:33:47 2015 -0700
     1.2 +++ b/src/video/x11/SDL_x11events.c	Thu May 28 09:52:48 2015 -0700
     1.3 @@ -334,6 +334,77 @@
     1.4  }
     1.5  #endif /* SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS */
     1.6  
     1.7 +static unsigned
     1.8 +X11_GetNumLockModifierMask(_THIS)
     1.9 +{
    1.10 +    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
    1.11 +    Display *display = viddata->display;
    1.12 +    unsigned num_mask = 0;
    1.13 +    int i, j;
    1.14 +    XModifierKeymap *xmods;
    1.15 +    unsigned n;
    1.16 +
    1.17 +    xmods = X11_XGetModifierMapping(display);
    1.18 +    n = xmods->max_keypermod;
    1.19 +    for(i = 3; i < 8; i++) {
    1.20 +        for(j = 0; j < n; j++) {
    1.21 +            KeyCode kc = xmods->modifiermap[i * n + j];
    1.22 +            if (viddata->key_layout[kc] == SDL_SCANCODE_NUMLOCKCLEAR) {
    1.23 +                num_mask = 1 << i;
    1.24 +                break;
    1.25 +            }
    1.26 +        }
    1.27 +    }
    1.28 +    X11_XFreeModifiermap(xmods);
    1.29 +
    1.30 +    return num_mask;
    1.31 +}
    1.32 +
    1.33 +static void
    1.34 +X11_ReconcileKeyboardState(_THIS, const SDL_WindowData *data)
    1.35 +{
    1.36 +    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
    1.37 +    Display *display = viddata->display;
    1.38 +    char keys[32];
    1.39 +    int keycode;
    1.40 +    Window junk_window;
    1.41 +    int x, y;
    1.42 +    unsigned int mask;
    1.43 +
    1.44 +    X11_XQueryKeymap(display, keys);
    1.45 +
    1.46 +    /* Get the keyboard modifier state */
    1.47 +    if (X11_XQueryPointer(display, DefaultRootWindow(display), &junk_window, &junk_window, &x, &y, &x, &y, &mask)) {
    1.48 +        unsigned num_mask = X11_GetNumLockModifierMask(_this);
    1.49 +        const Uint8 *keystate = SDL_GetKeyboardState(NULL);
    1.50 +        Uint8 capslockState = keystate[SDL_SCANCODE_CAPSLOCK];
    1.51 +        Uint8 numlockState = keystate[SDL_SCANCODE_NUMLOCKCLEAR];
    1.52 +
    1.53 +        /* Toggle key mod state if needed */
    1.54 +        if (!!(mask & LockMask) != !!(SDL_GetModState() & KMOD_CAPS)) {
    1.55 +            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
    1.56 +            if (capslockState == SDL_RELEASED) {
    1.57 +                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
    1.58 +            }
    1.59 +        }
    1.60 +
    1.61 +        if (!!(mask & num_mask) != !!(SDL_GetModState() & KMOD_NUM)) {
    1.62 +            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_NUMLOCKCLEAR);
    1.63 +            if (numlockState == SDL_RELEASED) {
    1.64 +                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_NUMLOCKCLEAR);
    1.65 +            }
    1.66 +        }
    1.67 +    }
    1.68 +
    1.69 +    for (keycode = 0; keycode < 256; ++keycode) {
    1.70 +        if (keys[keycode / 8] & (1 << (keycode % 8))) {
    1.71 +            SDL_SendKeyboardKey(SDL_PRESSED, viddata->key_layout[keycode]);
    1.72 +        } else {
    1.73 +            SDL_SendKeyboardKey(SDL_RELEASED, viddata->key_layout[keycode]);
    1.74 +        }
    1.75 +    }
    1.76 +}
    1.77 +
    1.78  
    1.79  static void
    1.80  X11_DispatchFocusIn(_THIS, SDL_WindowData *data)
    1.81 @@ -342,7 +413,7 @@
    1.82      printf("window %p: Dispatching FocusIn\n", data);
    1.83  #endif
    1.84      SDL_SetKeyboardFocus(data->window);
    1.85 -    ReconcileKeyboardState(_this, data);
    1.86 +    X11_ReconcileKeyboardState(_this, data);
    1.87  #ifdef X_HAVE_UTF8_STRING
    1.88      if (data->ic) {
    1.89          X11_XSetICFocus(data->ic);
    1.90 @@ -483,77 +554,6 @@
    1.91      return SDL_FALSE;
    1.92  }
    1.93  
    1.94 -static unsigned
    1.95 -GetNumLockModifierMask(_THIS)
    1.96 -{
    1.97 -    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
    1.98 -    Display *display = viddata->display;
    1.99 -    unsigned num_mask = 0;
   1.100 -    int i, j;
   1.101 -    XModifierKeymap *xmods;
   1.102 -    unsigned n;
   1.103 -
   1.104 -    xmods = X11_XGetModifierMapping(display);
   1.105 -    n = xmods->max_keypermod;
   1.106 -    for(i = 3; i < 8; i++) {
   1.107 -        for(j = 0; j < n; j++) {
   1.108 -            KeyCode kc = xmods->modifiermap[i * n + j];
   1.109 -            if (viddata->key_layout[kc] == SDL_SCANCODE_NUMLOCKCLEAR) {
   1.110 -                num_mask = 1 << i;
   1.111 -                break;
   1.112 -            }
   1.113 -        }
   1.114 -    }
   1.115 -    X11_XFreeModifiermap(xmods);
   1.116 -
   1.117 -    return num_mask;
   1.118 -}
   1.119 -
   1.120 -static void
   1.121 -ReconcileKeyboardState(_THIS, const SDL_WindowData *data)
   1.122 -{
   1.123 -    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
   1.124 -    Display *display = viddata->display;
   1.125 -    char keys[32];
   1.126 -    int keycode;
   1.127 -    Window junk_window;
   1.128 -    int x, y;
   1.129 -    unsigned int mask;
   1.130 -
   1.131 -    X11_XQueryKeymap(display, keys);
   1.132 -
   1.133 -    for (keycode = 0; keycode < 256; ++keycode) {
   1.134 -        if (keys[keycode / 8] & (1 << (keycode % 8))) {
   1.135 -            SDL_SendKeyboardKey(SDL_PRESSED, viddata->key_layout[keycode]);
   1.136 -        } else {
   1.137 -            SDL_SendKeyboardKey(SDL_RELEASED, viddata->key_layout[keycode]);
   1.138 -        }
   1.139 -    }
   1.140 -
   1.141 -    /* Get the keyboard modifier state */
   1.142 -    if (X11_XQueryPointer(display, DefaultRootWindow(display), &junk_window, &junk_window, &x, &y, &x, &y, &mask)) {
   1.143 -        unsigned num_mask = GetNumLockModifierMask(_this);
   1.144 -        const Uint8 *keystate = SDL_GetKeyboardState(NULL);
   1.145 -        Uint8 capslockState = keystate[SDL_SCANCODE_CAPSLOCK];
   1.146 -        Uint8 numlockState = keystate[SDL_SCANCODE_NUMLOCKCLEAR];
   1.147 -
   1.148 -        /* Toggle key mod state if needed */
   1.149 -        if (!!(mask & LockMask) != !!(SDL_GetModState() & KMOD_CAPS)) {
   1.150 -            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
   1.151 -            if (capslockState == SDL_RELEASED) {
   1.152 -                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
   1.153 -            }
   1.154 -        }
   1.155 -
   1.156 -        if (!!(mask & num_mask) != !!(SDL_GetModState() & KMOD_NUM)) {
   1.157 -            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_NUMLOCKCLEAR);
   1.158 -            if (numlockState == SDL_RELEASED) {
   1.159 -                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_NUMLOCKCLEAR);
   1.160 -            }
   1.161 -        }
   1.162 -    }
   1.163 -}
   1.164 -
   1.165  static void
   1.166  X11_DispatchEvent(_THIS)
   1.167  {
   1.168 @@ -737,7 +737,7 @@
   1.169              {
   1.170                  data->pending_focus = PENDING_FOCUS_NONE;
   1.171                  data->pending_focus_time = 0;
   1.172 -                X11_DispatchFocusOut(data);
   1.173 +                X11_DispatchFocusOut(_this, data);
   1.174              }
   1.175              else
   1.176              {
     2.1 --- a/test/checkkeys.c	Thu May 28 09:33:47 2015 -0700
     2.2 +++ b/test/checkkeys.c	Thu May 28 09:52:48 2015 -0700
     2.3 @@ -89,6 +89,20 @@
     2.4  }
     2.5  
     2.6  static void
     2.7 +PrintModifierState()
     2.8 +{
     2.9 +    char message[512];
    2.10 +    char *spot;
    2.11 +    size_t left;
    2.12 +
    2.13 +    spot = message;
    2.14 +    left = sizeof(message);
    2.15 +
    2.16 +    print_modifiers(&spot, &left);
    2.17 +    SDL_Log("Initial state:%s\n", message);
    2.18 +}
    2.19 +
    2.20 +static void
    2.21  PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
    2.22  {
    2.23      char message[512];
    2.24 @@ -200,6 +214,10 @@
    2.25  
    2.26      SDL_StartTextInput();
    2.27  
    2.28 +    /* Print initial modifier state */
    2.29 +    SDL_PumpEvents();
    2.30 +    PrintModifierState();
    2.31 +
    2.32      /* Watch keystrokes */
    2.33      done = 0;
    2.34