SDL: Treat a button press location as an implicit mouse move

From cbbad4051b2781fb96a9327ebf78fa02a10747ad Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 15 Oct 2024 22:25:47 -0700
Subject: [PATCH] Treat a button press location as an implicit mouse move

This fixes having the incorrect location for mouse button presses on Steam Deck under gamescope. This works around a bug where the mouse motion appears to come at the next button press, and comes from the xwayland-pointer device instead of the xwayland-relative-pointer device which is delivering mouse button events.
---
 src/video/x11/SDL_x11events.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c
index 8d13c22c1c5dc..f0afe172c27ce 100644
--- a/src/video/x11/SDL_x11events.c
+++ b/src/video/x11/SDL_x11events.c
@@ -894,6 +894,13 @@ void X11_HandleButtonPress(SDL_VideoDevice *_this, SDL_WindowData *windowdata, S
 #ifdef DEBUG_XEVENTS
     SDL_Log("window 0x%lx: ButtonPress (X11 button = %d)\n", windowdata->xwindow, button);
 #endif
+
+    SDL_Mouse *mouse = SDL_GetMouse();
+    if ((!mouse->relative_mode || mouse->relative_mode_warp) && (x != mouse->x || y != mouse->y)) {
+        X11_ProcessHitTest(_this, windowdata, x, y, false);
+        SDL_SendMouseMotion(0, window, mouseID, false, x, y);
+    }
+
     if (X11_IsWheelEvent(display, button, &xticks, &yticks)) {
         SDL_SendMouseWheel(0, window, mouseID, (float)-xticks, (float)yticks, SDL_MOUSEWHEEL_NORMAL);
     } else {