summaryrefslogtreecommitdiff
path: root/engine/src/windowing.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-11-27 17:00:24 +0100
committerHampusM <hampus@hampusmat.com>2026-11-27 17:00:24 +0100
commit5411ac376b64c34b0e2c57b86edd859bcd196d26 (patch)
treecf7749b881a8ca93cdacd8a03f57470cced35379 /engine/src/windowing.rs
parentaeb0b83b49c6da92114062b9ff436b8685d36a34 (diff)
refactor(engine): only use custom cursor locking implementation on x11HEADmaster
Diffstat (limited to 'engine/src/windowing.rs')
-rw-r--r--engine/src/windowing.rs141
1 files changed, 102 insertions, 39 deletions
diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs
index 49c5f02..54947eb 100644
--- a/engine/src/windowing.rs
+++ b/engine/src/windowing.rs
@@ -445,7 +445,7 @@ fn handle_window_changed(
continue;
};
- let is_window_settings_changed =
+ let window_settings_change_flags =
window.apply_to_settings(&mut window_metadata.settings);
if window_metadata.pending_resize_req.is_none()
@@ -470,12 +470,13 @@ fn handle_window_changed(
}
}
- if is_window_settings_changed {
+ if !window_settings_change_flags.is_empty() {
let new_window_settings = window_metadata.settings.clone();
context.try_send_message_to_app(MessageToApp::SetWindowSettings(
window.wid(),
new_window_settings,
+ window_settings_change_flags,
));
}
}
@@ -817,7 +818,7 @@ enum MessageToApp
{
CreateWindow(Uid, WindowCreationAttributes),
DestroyWindow(WindowId),
- SetWindowSettings(WindowId, WindowSettings),
+ SetWindowSettings(WindowId, WindowSettings, WindowSettingsChangeFlags),
}
#[derive(Debug)]
@@ -948,11 +949,14 @@ impl App
},
);
- window_creation_attrs.apply_extra_attrs_to_window(&winit_window);
-
let window_settings =
WindowSettings::from_window_creation_attrs(window_creation_attrs);
+ window_settings.apply_to_winit_window(
+ &winit_window,
+ WindowSettingsChangeFlags::all(),
+ );
+
self.windows.insert(
WindowId::from_inner(winit_window.id()),
(winit_window.clone(), window_settings.clone()),
@@ -967,7 +971,11 @@ impl App
MessageToApp::DestroyWindow(window_id) => {
self.windows.remove(window_id);
}
- MessageToApp::SetWindowSettings(window_id, new_window_settings) => {
+ MessageToApp::SetWindowSettings(
+ window_id,
+ new_window_settings,
+ window_settings_change_flags,
+ ) => {
let Some((winit_window, window_settings)) =
self.windows.get_mut(window_id)
else {
@@ -979,8 +987,10 @@ impl App
continue;
};
- new_window_settings
- .apply_to_winit_window(&winit_window, window_settings);
+ new_window_settings.apply_to_winit_window(
+ &winit_window,
+ window_settings_change_flags,
+ );
*window_settings = new_window_settings;
}
@@ -1055,7 +1065,12 @@ impl App
input.keys.clear_and_set(key as usize, key_state_bits, 0);
}
- fn handle_cursor_moved(&self, position: PhysicalPosition<f64>, window_id: WindowId)
+ fn handle_cursor_moved(
+ &self,
+ position: PhysicalPosition<f64>,
+ window_id: WindowId,
+ event_loop: &ActiveEventLoop,
+ )
{
{
let Some(mut input) = self.lock_input() else {
@@ -1066,35 +1081,39 @@ impl App
input.absolute_mouse_pos = position;
}
- let Some((winit_window, window_settings)) = self.windows.get(window_id) else {
- cold_path();
- return;
- };
-
- if window_settings.cursor_grab_mode != CursorGrabMode::Locked {
- return;
- }
+ // winit does not have an implemention of cursor grabbing for x11 so this is a
+ // necessary substitute
+ #[cfg(target_os = "linux")]
+ if winit::platform::x11::ActiveEventLoopExtX11::is_x11(event_loop) {
+ let Some((winit_window, window_settings)) = self.windows.get(window_id)
+ else {
+ cold_path();
+ return;
+ };
- if !winit_window.has_focus() {
- return;
- }
+ if window_settings.cursor_grab_mode != CursorGrabMode::Locked
+ || !winit_window.has_focus()
+ {
+ return;
+ }
- let window_size = winit_window
- .inner_size()
- .to_logical::<f64>(winit_window.scale_factor());
+ let window_size = winit_window
+ .inner_size()
+ .to_logical::<f64>(winit_window.scale_factor());
- if let Err(err) =
- winit_window.set_cursor_position(Position::Logical(LogicalPosition {
- x: window_size.width / 2.0,
- y: window_size.height / 2.0,
- }))
- {
- cold_path();
- tracing::error!(
- window_id=?window_id,
- "Failed to lock cursor position: {:#}",
- crate::Error::new(err)
- );
+ if let Err(err) =
+ winit_window.set_cursor_position(Position::Logical(LogicalPosition {
+ x: window_size.width / 2.0,
+ y: window_size.height / 2.0,
+ }))
+ {
+ cold_path();
+ tracing::error!(
+ window_id=?window_id,
+ "Failed to lock cursor position: {:#}",
+ crate::Error::new(err)
+ );
+ }
}
}
@@ -1205,7 +1224,7 @@ impl ApplicationHandler for App
#[tracing::instrument(skip_all)]
fn window_event(
&mut self,
- _event_loop: &ActiveEventLoop,
+ event_loop: &ActiveEventLoop,
window_id: WinitWindowId,
event: WindowEvent,
)
@@ -1230,6 +1249,7 @@ impl ApplicationHandler for App
self.handle_cursor_moved(
position.into(),
WindowId::from_inner(window_id),
+ event_loop,
);
}
WindowEvent::MouseWheel { device_id: _, delta, phase: _ } => {
@@ -1289,15 +1309,58 @@ impl WindowSettings
}
}
- fn apply_to_winit_window(&self, winit_window: &WinitWindow, old_settings: &Self)
+ fn apply_to_winit_window(
+ &self,
+ winit_window: &WinitWindow,
+ change_flags: WindowSettingsChangeFlags,
+ )
{
- if self.title != old_settings.title {
+ if change_flags.contains(WindowSettingsChangeFlags::TITLE) {
winit_window.set_title(&self.title);
}
- if self.cursor_visible != old_settings.cursor_visible {
+ if change_flags.contains(WindowSettingsChangeFlags::CURSOR_VISIBLE) {
winit_window.set_cursor_visible(self.cursor_visible);
}
+
+ if change_flags.contains(WindowSettingsChangeFlags::CURSOR_GRAB_MODE) {
+ let res = winit_window.set_cursor_grab(match self.cursor_grab_mode {
+ CursorGrabMode::None => winit::window::CursorGrabMode::None,
+ CursorGrabMode::Locked if window_is_x11(winit_window) => {
+ winit::window::CursorGrabMode::None
+ }
+ CursorGrabMode::Locked => winit::window::CursorGrabMode::Locked,
+ });
+
+ if let Err(err) = res {
+ cold_path();
+ tracing::error!(
+ "Failed to set cursor grab mode to {:?}: {err}",
+ self.cursor_grab_mode
+ );
+ }
+ }
+ }
+}
+
+bitflags! {
+#[derive(Debug, Clone, Copy)]
+struct WindowSettingsChangeFlags: u8
+{
+ const TITLE = 1 << 0;
+ const CURSOR_VISIBLE = 1 << 1;
+ const CURSOR_GRAB_MODE = 1 << 2;
+}
+}
+
+fn window_is_x11(winit_window: &WinitWindow) -> bool
+{
+ cfg_select! {
+ target_os = "linux" => {
+ winit::platform::wayland::WindowExtWayland::xdg_toplevel(winit_window)
+ .is_none()
+ }
+ _ => false,
}
}