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 5238

Summary: SDL_SetRelativeMouseMode closes application on Wayland
Product: SDL Reporter: M Stoeckl <sdlbug>
Component: videoAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: HG 2.1   
Hardware: x86_64   
OS: Linux   
Attachments: Test program; runs until keypress (or until SDL_RelativeMouseMode called)
Test program output, with env WAYLAND_DEBUG=1
Interactive version of test program with two windows
Patch: do not simultaneously lock and confine pointer
Patch #2: wayland, Clean up dangling pointers when destroying window
Interactive version of test program

Description M Stoeckl 2020-07-18 00:59:41 UTC
Applications running with the Wayland backend that call SDL_SetRelativeMouseMode(1) will create a protocol violation (trying to both lock a pointer to a position and confine a pointer to a region), making compositors close the application. As documented at https://github.com/wayland-project/wayland-protocols/blob/master/unstable/pointer-constraints/pointer-constraints-unstable-v1.xml#L59-L63, an application may only lock/confine once per surface.

This issue occurs with recent SDL from the default branch of HG, possibly since rev 13805: https://hg.libsdl.org/SDL/rev/4709c1dfeabb .

See attached `pointer_bug.c` for a test case that should work fine on X11, but fails on Wayland. I've tested with Weston, and have attached a log of the Wayland protocol messages from an example run.
Comment 1 M Stoeckl 2020-07-18 01:01:27 UTC
Created attachment 4420 [details]
Test program; runs until keypress (or until SDL_RelativeMouseMode called)
Comment 2 M Stoeckl 2020-07-18 01:01:59 UTC
Created attachment 4421 [details]
Test program output, with env WAYLAND_DEBUG=1
Comment 3 M Stoeckl 2020-07-31 02:09:38 UTC
Created attachment 4429 [details]
Interactive version of test program with two windows
Comment 4 M Stoeckl 2020-07-31 02:14:27 UTC
Created attachment 4430 [details]
Patch: do not simultaneously lock and confine pointer
Comment 5 M Stoeckl 2020-07-31 02:24:32 UTC
I've attached a patch that possibly fixes this issue, by keeping the pointer confinement disabled (not created) as long as there is a conflicting pointer lock.

In the process of creating the patch, I discovered that SDL_WaylandInput::pointer_focus and SDL_WaylandInput::keyboard_focus are never explicitly cleaned up by the Wayland backend. I don't have enough time right now to track down whether or not SDL_video.c guarantees that these are cleared before the object in memory being pointed to is destroyed. In the patch, I introduced a similar variable SDL_WaylandInput::confined_window, about whose cleanup I am similarly uncertain. See also the TODO comment.
Comment 6 M Stoeckl 2020-08-02 22:38:19 UTC
Created attachment 4432 [details]
Patch #2: wayland, Clean up dangling pointers when destroying window
Comment 7 M Stoeckl 2020-08-02 22:40:52 UTC
Created attachment 4433 [details]
Interactive version of test program
Comment 8 M Stoeckl 2020-08-02 22:45:19 UTC
OK, Patch #2 now fixes the TODO comment.
Comment 9 M Stoeckl 2020-12-22 22:49:17 UTC
Fixed as of 2.0.14.