pygame-community / pygame-ce

🐍🎮 pygame - Community Edition is a FOSS Python library for multimedia applications (like games). Built on top of the excellent SDL library.
https://pyga.me
773 stars 120 forks source link

pygame.mouse.set_pos() is not working under wayland #2799

Closed mzivic7 closed 2 months ago

mzivic7 commented 2 months ago

Environment:

Issue:

pygame.mouse.set_pos() is not working under wayland. Yes, wayland does not allow applications to set cursor position, but application ran through xwayland can (as stated here).

As a proof there is blender, it also uses system SDL2 (2.30.2), here is code for setting mouse position in blender source.

And here is pygame-ce (SDL 2.28.5) code for same thing. When i run pygame it says that it "seems to be running through X11 on top of wayland", which I assume is xwayland.

Looking at xwayland source, it indeed supports cursor warping, here.

To check if this is pipenv problem, I installed python-pygame 2.5.2 (that uses system SDL 2.30.2) package through pacman, because python is externally managed on Arch and pygame-ce is not available on pacman (even on AUR). pygame.mouse.set_pos() does not work here too, so pipenv is not causing problems.

Same issue is happening under weston running with --xwayland flag.

I built pygame-ce from source (with missing porttime if that matters). Now its pygame-ce 2.5.0.dev1 (SDL 2.30.2, Python 3.11.8) still in pipenv. No more reports about running through X11, but pygame.mouse.set_pos() is not working here too.

wayland-debug -r pipenv run python mouse_test.py shows no output (mouse_test.py is code that has pygame.mouse.set_pos() in it). Application ran through wayland would show huge amount of wayland events. This is suggesting that pygame is still running through xwayland.

Running same code under just weston opens new window outside simulated area, but weston --xwayland opens window inside that area, which confirms that pygame is running through xwayland.

robertpfeiffer commented 2 months ago

Like I said on discord, this should work on xwayland, I think.

The "seems to be running through X11 on top of wayland" thing was added by me to help with bug reports, and it's not terribly accurate. It just checks if wayland env vars are set but we are in X11. It could be triggered by running a nested Xorg in a window inside gnome shell, or by manually setting some env vars on plain Xorg, but I assume you'd know if you did that.

Thank you for this bug report, in any case

mzivic7 commented 2 months ago

I reported this issue upstream to SDL: here

Until it get resolved, quick fix for wayland mouse warping is to hide mouse while warping it, by adding mouse.set_visible around mouse.set_pos:


pygame.mouse.set_visible(False)
pygame.mouse.set_pos(x, y)
pygame.mouse.set_visible(True)
mzivic7 commented 2 months ago

This will be fixed with new SLD2 vesion release and when pygame-ce stars using it. Closing this issue.