Setting CursorGrabMode to Locked or Confined doesn't work during the first few frames (Ubuntu X11)
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 171
Description
## Bevy version
0.14.2
## \[Optional\] Relevant system information
Ubuntu 22.04.2 LTS
X11
## What you did
I am trying to set my cursor's CursorGrabMode to either Locked or Confined during Startup
```
pub fn lock_mouse(mut primary_window: Query<&mut Window, With>) {
let window = &mut primary_window.single_mut();
window.cursor.grab_mode = CursorGrabMode::Confined;
}
```
## What went wrong
with grabmode set to confined the mouse is not confined to the window and I get this error
```
ERROR bevy_winit::winit_windows: Unable to grab cursor: the requested operation is not supported by Winit
```
with grabmode locked the same thing happens and I get this error
```
ERROR bevy_winit::winit_windows: Unable to grab cursor: os error at /home/knar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.30.5/src/platform_impl/linux/x11/window.rs:1551: Cursor could not be confined: confine location not viewable
```
## Additional information
After some poking around I figured out that this was only happening if I tried to set the CursorGrabMode during startup or on the first frame in a normal Update system, but if I wait until the third frame to set the cursor grab mode it seems to work. Because of the cached state of the CursorGrabMode I can't just set it every frame.
I dumped the Window object into the console and noticed two properties were different when it works vs when it doesn't work:
When it doesn't work, Window.position is "Automatic" and Window.internal.physical_cursor_position is "None"
When the Window.position has an "At" position and Window.internal.physical_cursor_position has a "Some" value, there are no errors.
For now my workaround is to check that Window.position is not Automatic but that feels janky. It would be nice if setting CursorGrabMode did not cache the grab mode if there is an error while setting, or if I could detect the error and handle it myself.
Contributor guide
Research direction
Start in the bevy_winit::winit_windows handling of Window.cursor.grab_mode, then compare the first-frame behavior with winit-0.30.5's Linux X11 window.rs error paths. Reproduce on Ubuntu 22.04.2 under X11 using Startup and early Update systems, and verify that Locked or Confined works without the reported errors when the window state is initially Automatic with no physical cursor position.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- linux, rust
- Domain
- desktop, game-dev, operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100