Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL_GetGlobalMouseState reports weird coordinates (Linux, Xwayland) #5174

Closed
mgerhardy opened this issue Jan 4, 2022 · 7 comments
Closed
Milestone

Comments

@mgerhardy
Copy link
Contributor

mgerhardy commented Jan 4, 2022

I hope this isn't me just not understanding some of the concepts - but I received weird values for the global mouse state

Screen 0: minimum 16 x 16, current 5120 x 1440, maximum 32767 x 32767
XWAYLAND27 connected 2560x1440+0+0 (0x24) normal (normal left inverted right x axis y axis) 600mm x 340mm
	Identifier: 0x6d3
	Timestamp:  38592
	Subpixel:   unknown
	Gamma:      1.0:1.0:1.0
	Brightness: 0.0
	Clones:    
	CRTC:       1
	CRTCs:      1
	Transform:  1.000000 0.000000 0.000000
	            0.000000 1.000000 0.000000
	            0.000000 0.000000 1.000000
	           filter: 
	non-desktop: 0 
		supported: 0, 1
  2560x1440 (0x24) 312.000MHz -HSync +VSync *current +preferred
        h: width  2560 start 2752 end 3024 total 3488 skew    0 clock  89.45KHz
        v: height 1440 start 1443 end 1448 total 1493           clock  59.91Hz
XWAYLAND28 connected primary 2560x1440+2560+0 (0x24) normal (normal left inverted right x axis y axis) 600mm x 340mm
	Identifier: 0x6d5
	Timestamp:  38592
	Subpixel:   unknown
	Gamma:      1.0:1.0:1.0
	Brightness: 0.0
	Clones:    
	CRTC:       0
	CRTCs:      0
	Transform:  1.000000 0.000000 0.000000
	            0.000000 1.000000 0.000000
	            0.000000 0.000000 1.000000
	           filter: 
	non-desktop: 0 
		supported: 0, 1
  2560x1440 (0x24) 312.000MHz -HSync +VSync *current +preferred
        h: width  2560 start 2752 end 3024 total 3488 skew    0 clock  89.45KHz
        v: height 1440 start 1443 end 1448 total 1493           clock  59.91Hz
int windowX = 0, windowY = 0;
SDL_GetWindowPosition(mouseWindow, &windowX, &windowY);
io.MousePos = ImVec2((float)(mouseXGlobal - windowX), (float)(mouseYGlobal - windowY));

the window (0:0) the mouse is hovering is on SDL display 0 (0:0:2560:1440), but the global mouse position is at 3646:769 - this looks like a bug in SDL for me. I will report it there. Weird thing is that the SDL display 1 has a x position of 2560 but is the left display. Not sure how to convert the mouse coordinates properly.

When I create the window on the other display, I also get invalid(?) values:
display 1 is reported as x-offset 2560 - the global mouse coordinate was e.g. 956 while hovering the window at 2560:0 (fullscreen)


the imgui issue for reference: ocornut/imgui#2117 (comment)

@flibitijibibo
Copy link
Collaborator

flibitijibibo commented Jan 5, 2022

Both GetWindowPosition and global mouse state are unsupported in native Wayland, and should return -1 indicating this. This isn't the end of the world though, if I remember correctly their code already has a path to use the non-global GetMouseState while tracking the mouse enter/leave events.

Xwayland may be a different story but I would check native Wayland first, since that will be the priority in the near future anyhow.

@DanielGibson
Copy link
Contributor

and should return -1 indicating this

SDL_GetGlobalMouseState() returns an Uint32 so it can't really return -1, and if it returns 0xffffffff in case of error that's not documented.
It would indeed be nice to have some way to tell if the call succeeded or not

@flibitijibibo
Copy link
Collaborator

Oh, right, I was thinking of Set rather than Get. I think Get falls back to the window-relative version, so you can only detect it with something else like GetWindowPosition.

@DanielGibson
Copy link
Contributor

In dhewm3 I use SDL_GetGlobalMouseState() before creating the window, to create the Window on the display the cursor is currently on (with SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex)in SDL_CreateWindow()).
Seems to work on X11 and Windows, unsure about Mac, most probably not on Wayland..

@flibitijibibo flibitijibibo changed the title SDL_GetGlobalMouseState reports weird coordinates (Linux, Wayland) SDL_GetGlobalMouseState reports weird coordinates (Linux, Xwayland) Apr 4, 2022
@flibitijibibo
Copy link
Collaborator

On the native Wayland side I've been doing a bunch of work that makes the coordinates relatively sane for single-display setups - past that, global queries still can't be used (and multi-window is still a stretch if you try to write your own focus detection for some reason).

UE5 has been my test for the recent commits and I have to say, having a feature query for global window/mouse position does seem like a really good idea since the alternative is basically to hardcode checks for SDL_GetCurrentVideoDriver, which is a terrible idea and only a lazy person would do that.

@slouken slouken added this to the 3.2.0 milestone Nov 7, 2023
@Kontrabant
Copy link
Contributor

Kontrabant commented Dec 6, 2023

Is this still an issue? XWayland and the SDL Wayland backend have had a lot of work done on them over the past two years, I can't replicate the original issue on the current version XWayland, and the SDL Wayland backend manages to fake the global mouse coordinates well enough for the 99.9% of applications that just query them to manually transform the global pointer coordinates to relative (like the UE5 editor).

@slouken slouken added the waiting Waiting on user response label May 21, 2024
@slouken slouken removed the waiting Waiting on user response label Oct 6, 2024
@slouken
Copy link
Collaborator

slouken commented Oct 6, 2024

I think this is fixed, please let us know if that's not the case.

@slouken slouken closed this as completed Oct 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants