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.
This commit is contained in:
Sam Lantinga 2015-05-28 09:33:47 -07:00
parent 779d403563
commit 0694baf260

View file

@ -293,27 +293,27 @@ static char* X11_URIToLocal(char* uri) {
if (memcmp(uri,"file:/",6) == 0) uri += 6; /* local file? */ if (memcmp(uri,"file:/",6) == 0) uri += 6; /* local file? */
else if (strstr(uri,":/") != NULL) return file; /* wrong scheme */ else if (strstr(uri,":/") != NULL) return file; /* wrong scheme */
local = uri[0] != '/' || ( uri[0] != '\0' && uri[1] == '/' ); local = uri[0] != '/' || (uri[0] != '\0' && uri[1] == '/');
/* got a hostname? */ /* got a hostname? */
if ( !local && uri[0] == '/' && uri[2] != '/' ) { if (!local && uri[0] == '/' && uri[2] != '/') {
char* hostname_end = strchr( uri+1, '/' ); char* hostname_end = strchr(uri+1, '/');
if ( hostname_end != NULL ) { if (hostname_end != NULL) {
char hostname[ 257 ]; char hostname[ 257 ];
if ( gethostname( hostname, 255 ) == 0 ) { if (gethostname(hostname, 255) == 0) {
hostname[ 256 ] = '\0'; hostname[ 256 ] = '\0';
if ( memcmp( uri+1, hostname, hostname_end - ( uri+1 )) == 0 ) { if (memcmp(uri+1, hostname, hostname_end - (uri+1)) == 0) {
uri = hostname_end + 1; uri = hostname_end + 1;
local = SDL_TRUE; local = SDL_TRUE;
} }
} }
} }
} }
if ( local ) { if (local) {
file = uri; file = uri;
/* Convert URI escape sequences to real characters */ /* Convert URI escape sequences to real characters */
X11_URIDecode(file, 0); X11_URIDecode(file, 0);
if ( uri[1] == '/' ) { if (uri[1] == '/') {
file++; file++;
} else { } else {
file--; file--;
@ -336,12 +336,13 @@ static void X11_HandleGenericEvent(SDL_VideoData *videodata,XEvent event)
static void static void
X11_DispatchFocusIn(SDL_WindowData *data) X11_DispatchFocusIn(_THIS, SDL_WindowData *data)
{ {
#ifdef DEBUG_XEVENTS #ifdef DEBUG_XEVENTS
printf("window %p: Dispatching FocusIn\n", data); printf("window %p: Dispatching FocusIn\n", data);
#endif #endif
SDL_SetKeyboardFocus(data->window); SDL_SetKeyboardFocus(data->window);
ReconcileKeyboardState(_this, data);
#ifdef X_HAVE_UTF8_STRING #ifdef X_HAVE_UTF8_STRING
if (data->ic) { if (data->ic) {
X11_XSetICFocus(data->ic); X11_XSetICFocus(data->ic);
@ -353,7 +354,7 @@ X11_DispatchFocusIn(SDL_WindowData *data)
} }
static void static void
X11_DispatchFocusOut(SDL_WindowData *data) X11_DispatchFocusOut(_THIS, SDL_WindowData *data)
{ {
#ifdef DEBUG_XEVENTS #ifdef DEBUG_XEVENTS
printf("window %p: Dispatching FocusOut\n", data); printf("window %p: Dispatching FocusOut\n", data);
@ -482,23 +483,74 @@ ProcessHitTest(_THIS, const SDL_WindowData *data, const XEvent *xev)
return SDL_FALSE; return SDL_FALSE;
} }
static unsigned
GetNumLockModifierMask(_THIS)
{
SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
Display *display = viddata->display;
unsigned num_mask = 0;
int i, j;
XModifierKeymap *xmods;
unsigned n;
xmods = X11_XGetModifierMapping(display);
n = xmods->max_keypermod;
for(i = 3; i < 8; i++) {
for(j = 0; j < n; j++) {
KeyCode kc = xmods->modifiermap[i * n + j];
if (viddata->key_layout[kc] == SDL_SCANCODE_NUMLOCKCLEAR) {
num_mask = 1 << i;
break;
}
}
}
X11_XFreeModifiermap(xmods);
return num_mask;
}
static void static void
ReconcileKeyboardState(_THIS, const SDL_WindowData *data) ReconcileKeyboardState(_THIS, const SDL_WindowData *data)
{ {
SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata; SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
Display *display = viddata->display; Display *display = viddata->display;
char keys[32]; char keys[32];
int keycode = 0; int keycode;
Window junk_window;
int x, y;
unsigned int mask;
X11_XQueryKeymap( display, keys ); X11_XQueryKeymap(display, keys);
while ( keycode < 256 ) { for (keycode = 0; keycode < 256; ++keycode) {
if ( keys[keycode / 8] & (1 << (keycode % 8)) ) { if (keys[keycode / 8] & (1 << (keycode % 8))) {
SDL_SendKeyboardKey(SDL_PRESSED, viddata->key_layout[keycode]); SDL_SendKeyboardKey(SDL_PRESSED, viddata->key_layout[keycode]);
} else { } else {
SDL_SendKeyboardKey(SDL_RELEASED, viddata->key_layout[keycode]); SDL_SendKeyboardKey(SDL_RELEASED, viddata->key_layout[keycode]);
} }
keycode++; }
/* Get the keyboard modifier state */
if (X11_XQueryPointer(display, DefaultRootWindow(display), &junk_window, &junk_window, &x, &y, &x, &y, &mask)) {
unsigned num_mask = GetNumLockModifierMask(_this);
const Uint8 *keystate = SDL_GetKeyboardState(NULL);
Uint8 capslockState = keystate[SDL_SCANCODE_CAPSLOCK];
Uint8 numlockState = keystate[SDL_SCANCODE_NUMLOCKCLEAR];
/* Toggle key mod state if needed */
if (!!(mask & LockMask) != !!(SDL_GetModState() & KMOD_CAPS)) {
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
if (capslockState == SDL_RELEASED) {
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
}
}
if (!!(mask & num_mask) != !!(SDL_GetModState() & KMOD_NUM)) {
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_NUMLOCKCLEAR);
if (numlockState == SDL_RELEASED) {
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_NUMLOCKCLEAR);
}
}
} }
} }
@ -648,15 +700,11 @@ X11_DispatchEvent(_THIS)
#ifdef DEBUG_XEVENTS #ifdef DEBUG_XEVENTS
printf("window %p: FocusIn!\n", data); printf("window %p: FocusIn!\n", data);
#endif #endif
if (data->pending_focus == PENDING_FOCUS_OUT &&
data->window == SDL_GetKeyboardFocus()) {
ReconcileKeyboardState(_this, data);
}
if (!videodata->last_mode_change_deadline) /* no recent mode changes */ if (!videodata->last_mode_change_deadline) /* no recent mode changes */
{ {
data->pending_focus = PENDING_FOCUS_NONE; data->pending_focus = PENDING_FOCUS_NONE;
data->pending_focus_time = 0; data->pending_focus_time = 0;
X11_DispatchFocusIn(data); X11_DispatchFocusIn(_this, data);
} }
else else
{ {
@ -863,7 +911,7 @@ X11_DispatchEvent(_THIS)
SDL_bool use_list = xevent.xclient.data.l[1] & 1; SDL_bool use_list = xevent.xclient.data.l[1] & 1;
data->xdnd_source = xevent.xclient.data.l[0]; data->xdnd_source = xevent.xclient.data.l[0];
xdnd_version = ( xevent.xclient.data.l[1] >> 24); xdnd_version = (xevent.xclient.data.l[1] >> 24);
#ifdef DEBUG_XEVENTS #ifdef DEBUG_XEVENTS
printf("XID of source window : %ld\n", data->xdnd_source); printf("XID of source window : %ld\n", data->xdnd_source);
printf("Protocol version to use : %ld\n", xdnd_version); printf("Protocol version to use : %ld\n", xdnd_version);
@ -1250,10 +1298,10 @@ X11_HandleFocusChanges(_THIS)
if (data && data->pending_focus != PENDING_FOCUS_NONE) { if (data && data->pending_focus != PENDING_FOCUS_NONE) {
Uint32 now = SDL_GetTicks(); Uint32 now = SDL_GetTicks();
if (SDL_TICKS_PASSED(now, data->pending_focus_time)) { if (SDL_TICKS_PASSED(now, data->pending_focus_time)) {
if ( data->pending_focus == PENDING_FOCUS_IN ) { if (data->pending_focus == PENDING_FOCUS_IN) {
X11_DispatchFocusIn(data); X11_DispatchFocusIn(_this, data);
} else { } else {
X11_DispatchFocusOut(data); X11_DispatchFocusOut(_this, data);
} }
data->pending_focus = PENDING_FOCUS_NONE; data->pending_focus = PENDING_FOCUS_NONE;
} }