bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.09k stars 3.56k forks source link

Error setting CursorGrabMode after unfocusing the window and switching from Locked/Confined to None, then switching back #16238

Open Knar33 opened 1 day ago

Knar33 commented 1 day ago

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 after switching tabs (and setting the CursorGrabMode to None) then switching back (and changing to Locked or Confined).

pub fn lock_mouse(mut primary_window: Query<&mut Window, With<PrimaryWindow>>) {
    let window = &mut primary_window.single_mut();
    if window.focused {
        if window.position != WindowPosition::Automatic {
            window.cursor.grab_mode = CursorGrabMode::Confined;
        }
    } else {
        window.cursor.grab_mode = CursorGrabMode::None;
    }
}

Note: The check for WindowPosition::Automatic is a workaround for another bug #16237

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: already confined by another client

Additional information

I haven't found a workaround for this issue yet :(

Knar33 commented 12 hours ago

Similarly to https://github.com/bevyengine/bevy/issues/16237 I can work around the issue by adding a 5-frame delay to grabbing the cursor.