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 4255

Summary: SDL_GetGlobalMouseState() returns incorrect Y on secondary display
Product: SDL Reporter: Julian Raschke <julian>
Component: videoAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: rasky, sezeroz
Version: 2.0.8   
Hardware: x86_64   
OS: macOS 10.13   
Attachments: Fix for the bug on OSX

Description Julian Raschke 2018-09-09 09:49:45 UTC
I use an open Mac laptop with an additional external monitor. The coordinate spaces from SDL_GetGlobalMouseState() and SDL_GetWindowPosition() match on the primary display, but not on the secondary display.

Cocoa window coordinates are vertically flipped in relation to the primary display:

https://github.com/spurious/SDL-mirror/blob/release-2.0.8/src/video/cocoa/SDL_cocoawindow.m#L219-L222

However, Cocoa_GetGlobalMouseState inverts the cursor Y coordinate per-display:

https://github.com/spurious/SDL-mirror/blob/release-2.0.8/src/video/cocoa/SDL_cocoamouse.m#L320-L323

Suggested fix: Replace the for-loop with this simpler calculation:

    *x = (int) cocoaLocation.x;
    *y = (int) (CGDisplayPixelsHigh(kCGDirectMainDisplay) - cocoaLocation.y);

Related ticket:

https://bugzilla.libsdl.org/show_bug.cgi?id=2655
Comment 1 Giovanni Bajo 2018-12-13 15:14:13 UTC
Created attachment 3545 [details]
Fix for the bug on OSX

I have the same problem: SDL_GetGlobalMouseState() is wrong on double monitor setups on OSX.

The fix suggested in this bug perfectly works. I'm attaching a patch that allows me to use SDL_GetGlobalMouseState() on double-monitor setups correctly. Please apply the fix.
Comment 2 Giovanni Bajo 2018-12-30 08:22:57 UTC
Ping! Can we get this applied?
Comment 3 Sam Lantinga 2019-01-05 06:09:57 UTC
Patch added, thanks!
https://hg.libsdl.org/SDL/rev/74e8ace1b2c3