We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2770

Summary: SDL_GetGlobalMouseState() returns incorrect mouse coordinates on certain dual monitor setup (X11)
Product: SDL Reporter: Dmitry 'RCL' Rekman <dmitry.rekman+sdlbugzilla>
Component: videoAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: icculus
Version: 2.0.3   
Hardware: x86_64   
OS: Linux   

Description Dmitry 'RCL' Rekman 2014-10-29 22:19:06 UTC
If the primary X11 screen has a non-zero offset (e.g. X=2560,Y=0) as returned by SDL_GetDisplayBounds(), mouse coordinates returned by SDL_GetGlobalMouseState() will be incorrect (the said offset of the primary screen will be added to them).

The patch below (also mailed to sdl@lists.libsdl.org - http://lists.libsdl.org/pipermail/sdl-libsdl.org/2014-October/096764.html) fixes that:

diff -r bfdc18891a60 src/video/x11/SDL_x11mouse.c
--- a/src/video/x11/SDL_x11mouse.c	Mon Oct 27 19:53:44 2014 -0400
+++ b/src/video/x11/SDL_x11mouse.c	Wed Oct 29 16:33:25 2014 -0400
@@ -382,8 +382,14 @@
                 retval |= (mask & Button1Mask) ? SDL_BUTTON_LMASK : 0;
                 retval |= (mask & Button2Mask) ? SDL_BUTTON_MMASK : 0;
                 retval |= (mask & Button3Mask) ? SDL_BUTTON_RMASK : 0;
-                *x = data->x + rootx;
-                *y = data->y + rooty;
+                /* SDL_DisplayData->x,y point to screen origin, and adding them to mouse coordinates relative to root window doesn't do the right thing
+                 * (observed on dual monitor setup with primary display being the rightmost one - mouse was offset to the right).
+                 *
+                 * Adding root position to root-relative coordinates seems to be a better way to get absolute position. */
+                XWindowAttributes root_attrs;
+                X11_XGetWindowAttributes(display, root, &root_attrs);
+                *x = root_attrs.x + rootx;
+                *y = root_attrs.y + rooty;
                 return retval;
             }
         }
Comment 1 Ryan C. Gordon 2014-10-30 17:15:08 UTC
This patch is now https://hg.libsdl.org/SDL/rev/f20d288435bd, thanks!

--ryan.