x11: Reject click-through button events based on serial

XInput2 may send mouse buttons presses on both the master and slave devices, and the click-through button event should be ignored on both if required.
This commit is contained in:
Frank Praznik
2026-05-20 11:01:40 -04:00
committed by Ryan C. Gordon
parent 9a56bc66b5
commit 8371c09aa7
4 changed files with 10 additions and 7 deletions

View File

@@ -1130,7 +1130,7 @@ void X11_HandleKeyEvent(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_
}
}
void X11_HandleButtonPress(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_MouseID mouseID, int button, float x, float y, unsigned long time)
void X11_HandleButtonPress(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_MouseID mouseID, int button, float x, float y, unsigned long time, unsigned long serial)
{
SDL_Window *window = windowdata->window;
int xticks = 0, yticks = 0;
@@ -1149,7 +1149,6 @@ void X11_HandleButtonPress(SDL_VideoDevice *_this, SDL_WindowData *windowdata, S
if (X11_IsWheelEvent(button, &xticks, &yticks)) {
SDL_SendMouseWheel(timestamp, window, mouseID, (float)-xticks, (float)yticks, SDL_MOUSEWHEEL_NORMAL);
} else {
bool ignore_click = false;
if (button > 7) {
/* X button values 4-7 are used for scrolling, so X1 is 8, X2 is 9, ...
=> subtract (8-SDL_BUTTON_X1) to get value SDL expects */
@@ -1164,11 +1163,14 @@ void X11_HandleButtonPress(SDL_VideoDevice *_this, SDL_WindowData *windowdata, S
if (windowdata->last_focus_event_time) {
const int X11_FOCUS_CLICK_TIMEOUT = 10;
if (SDL_GetTicks() < (windowdata->last_focus_event_time + X11_FOCUS_CLICK_TIMEOUT)) {
ignore_click = !SDL_GetHintBoolean(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, false);
if (!SDL_GetHintBoolean(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, false)) {
// Ignore all press events with this serial.
windowdata->ignore_button_press_serial = serial;
}
}
windowdata->last_focus_event_time = 0;
}
if (!ignore_click) {
if (serial != windowdata->ignore_button_press_serial) {
SDL_SendMouseButton(timestamp, window, mouseID, button, true);
}
}
@@ -1872,7 +1874,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
}
X11_HandleButtonPress(_this, data, SDL_GLOBAL_MOUSE_ID, xevent->xbutton.button,
xevent->xbutton.x, xevent->xbutton.y, xevent->xbutton.time);
xevent->xbutton.x, xevent->xbutton.y, xevent->xbutton.time, xevent->xbutton.serial);
} break;
case ButtonRelease:

View File

@@ -31,7 +31,7 @@ extern void X11_ReconcileKeyboardState(SDL_VideoDevice *_this);
extern void X11_GetBorderValues(SDL_WindowData *data);
extern Uint64 X11_GetEventTimestamp(unsigned long time);
extern void X11_HandleKeyEvent(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_KeyboardID keyboardID, XEvent *xevent);
extern void X11_HandleButtonPress(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_MouseID mouseID, int button, float x, float y, unsigned long time);
extern void X11_HandleButtonPress(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_MouseID mouseID, int button, float x, float y, unsigned long time, unsigned long serial);
extern void X11_HandleButtonRelease(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_MouseID mouseID, int button, unsigned long time);
extern SDL_WindowData *X11_FindWindow(SDL_VideoData *videodata, Window window);
extern bool X11_ProcessHitTest(SDL_VideoDevice *_this, SDL_WindowData *data, const float x, const float y, bool force_new_result);

View File

@@ -63,6 +63,7 @@ struct SDL_WindowData
bool xinput2_keyboard_enabled;
bool mouse_grabbed;
Uint64 last_focus_event_time;
unsigned long ignore_button_press_serial;
PendingFocusEnum pending_focus;
Uint64 pending_focus_time;
bool pending_move;

View File

@@ -646,7 +646,7 @@ void X11_HandleXinput2Event(SDL_VideoDevice *_this, XGenericEventCookie *cookie)
if (down) {
X11_HandleButtonPress(_this, windowdata, (SDL_MouseID)xev->sourceid, button,
(float)xev->event_x, (float)xev->event_y, xev->time);
(float)xev->event_x, (float)xev->event_y, xev->time, xev->serial);
} else {
X11_HandleButtonRelease(_this, windowdata, (SDL_MouseID)xev->sourceid, button, xev->time);
}