Skip to content

Commit

Permalink
Disable XInput2 keyboard events
Browse files Browse the repository at this point in the history
It turns out they're only delivered to the window with mouse focus, not keyboard focus.

Fixes #9374
  • Loading branch information
slouken committed Mar 28, 2024
1 parent fb5307c commit c8489a3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/video/x11/SDL_x11events.c
Expand Up @@ -1528,7 +1528,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
case KeyPress:
case KeyRelease:
{
if (data->using_xinput2) {
if (data->xinput2_keyboard_enabled) {
// This input is being handled by XInput2
break;
}
Expand All @@ -1538,7 +1538,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)

case MotionNotify:
{
if (data->using_xinput2) {
if (data->xinput2_mouse_enabled) {
// This input is being handled by XInput2
break;
}
Expand All @@ -1556,7 +1556,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)

case ButtonPress:
{
if (data->using_xinput2) {
if (data->xinput2_mouse_enabled) {
// This input is being handled by XInput2
break;
}
Expand All @@ -1567,7 +1567,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)

case ButtonRelease:
{
if (data->using_xinput2) {
if (data->xinput2_mouse_enabled) {
// This input is being handled by XInput2
break;
}
Expand Down
9 changes: 7 additions & 2 deletions src/video/x11/SDL_x11window.c
Expand Up @@ -796,9 +796,14 @@ int X11_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesI
{
unsigned int x11_keyboard_events = KeyPressMask | KeyReleaseMask;
unsigned int x11_pointer_events = ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
if (X11_Xinput2SelectMouseAndKeyboard(_this, window)) {
/* If XInput2 can handle pointer and keyboard events, we don't track them here */

X11_Xinput2SelectMouseAndKeyboard(_this, window);

/* If XInput2 can handle pointer and keyboard events, we don't track them here */
if (windowdata->xinput2_keyboard_enabled) {
x11_keyboard_events = 0;
}
if (windowdata->xinput2_mouse_enabled) {
x11_pointer_events = 0;
}

Expand Down
3 changes: 2 additions & 1 deletion src/video/x11/SDL_x11window.h
Expand Up @@ -59,7 +59,8 @@ struct SDL_WindowData
int border_right;
int border_top;
int border_bottom;
SDL_bool using_xinput2;
SDL_bool xinput2_mouse_enabled;
SDL_bool xinput2_keyboard_enabled;
SDL_bool mouse_grabbed;
Uint64 last_focus_event_time;
PendingFocusEnum pending_focus;
Expand Down
21 changes: 16 additions & 5 deletions src/video/x11/SDL_x11xinput2.c
Expand Up @@ -607,27 +607,38 @@ SDL_bool X11_Xinput2SelectMouseAndKeyboard(SDL_VideoDevice *_this, SDL_Window *w
eventmask.mask = mask;
eventmask.deviceid = XIAllDevices;

/* This is not enabled by default because these events are only delivered to the window with mouse focus, not keyboard focus */
#ifdef USE_XINPUT2_KEYBOARD
XISetMask(mask, XI_KeyPress);
XISetMask(mask, XI_KeyRelease);
windowdata->xinput2_keyboard_enabled = SDL_TRUE;
#endif

XISetMask(mask, XI_ButtonPress);
XISetMask(mask, XI_ButtonRelease);
XISetMask(mask, XI_Motion);
windowdata->xinput2_mouse_enabled = SDL_TRUE;

XISetMask(mask, XI_Enter);
XISetMask(mask, XI_Leave);

/* Hotplugging: */
XISetMask(mask, XI_DeviceChanged);
XISetMask(mask, XI_HierarchyChanged);
XISetMask(mask, XI_PropertyEvent); /* E.g., when swapping tablet pens */

if (X11_XISelectEvents(data->display, windowdata->xwindow, &eventmask, 1) == Success) {
windowdata->using_xinput2 = SDL_TRUE;
} else {
if (X11_XISelectEvents(data->display, windowdata->xwindow, &eventmask, 1) != Success) {
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Could not enable XInput2 event handling\n");
windowdata->using_xinput2 = SDL_FALSE;
windowdata->xinput2_keyboard_enabled = SDL_FALSE;
windowdata->xinput2_mouse_enabled = SDL_FALSE;
}
}
#endif
return windowdata->using_xinput2;

if (windowdata->xinput2_keyboard_enabled || windowdata->xinput2_mouse_enabled) {
return SDL_TRUE;
}
return SDL_FALSE;
}

int X11_Xinput2IsMultitouchSupported(void)
Expand Down

0 comments on commit c8489a3

Please sign in to comment.