From 9fb99578a3726eafde92a528ee68444fee755e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20H=C4=83loiu?= Date: Sun, 19 May 2024 11:09:59 +0100 Subject: [PATCH] Add `surface_enter`/`surface_leave` methods to `CompositorHandler` --- CHANGELOG.md | 1 + examples/data_device.rs | 20 +++++++++++++++++ examples/generic_simple_window.rs | 20 +++++++++++++++++ examples/image_viewer.rs | 20 +++++++++++++++++ examples/image_viewporter.rs | 20 +++++++++++++++++ examples/relative_pointer.rs | 20 +++++++++++++++++ examples/session_lock.rs | 20 +++++++++++++++++ examples/simple_layer.rs | 20 +++++++++++++++++ examples/simple_window.rs | 20 +++++++++++++++++ examples/themed_window.rs | 20 +++++++++++++++++ src/compositor.rs | 37 ++++++++++++++++++++++++++++++- 11 files changed, 217 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8754f69ed..71654f3c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ #### Breaking Changes - Updated `calloop` to 0.13.0, `calloop-wayland-source` to `0.3.0` +- Add `surface_enter`/`surface_leave` methods to `CompositorHandler` trait. #### Fixed diff --git a/examples/data_device.rs b/examples/data_device.rs index fd3381552..804fe052d 100644 --- a/examples/data_device.rs +++ b/examples/data_device.rs @@ -222,6 +222,26 @@ impl CompositorHandler for DataDeviceWindow { ) { self.draw(conn, qh); } + + fn surface_enter( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } + + fn surface_leave( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } } impl OutputHandler for DataDeviceWindow { diff --git a/examples/generic_simple_window.rs b/examples/generic_simple_window.rs index 80c1ea2a1..db88c15fc 100644 --- a/examples/generic_simple_window.rs +++ b/examples/generic_simple_window.rs @@ -163,6 +163,26 @@ impl CompositorHandler for SimpleWindow { ) { self.draw(conn, qh); } + + fn surface_enter( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } + + fn surface_leave( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } } impl OutputHandler for SimpleWindow { diff --git a/examples/image_viewer.rs b/examples/image_viewer.rs index 38841e511..1057ecfaa 100644 --- a/examples/image_viewer.rs +++ b/examples/image_viewer.rs @@ -159,6 +159,26 @@ impl CompositorHandler for State { ) { self.draw(conn, qh); } + + fn surface_enter( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } + + fn surface_leave( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } } impl OutputHandler for State { diff --git a/examples/image_viewporter.rs b/examples/image_viewporter.rs index 53080f75c..21e6a35b7 100644 --- a/examples/image_viewporter.rs +++ b/examples/image_viewporter.rs @@ -184,6 +184,26 @@ impl CompositorHandler for State { ) { self.draw(conn, qh); } + + fn surface_enter( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } + + fn surface_leave( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } } impl OutputHandler for State { diff --git a/examples/relative_pointer.rs b/examples/relative_pointer.rs index 1a7cee418..667d9edc8 100644 --- a/examples/relative_pointer.rs +++ b/examples/relative_pointer.rs @@ -159,6 +159,26 @@ impl CompositorHandler for SimpleWindow { ) { self.draw(conn, qh); } + + fn surface_enter( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } + + fn surface_leave( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } } impl OutputHandler for SimpleWindow { diff --git a/examples/session_lock.rs b/examples/session_lock.rs index 8516baf70..77f0d8b21 100644 --- a/examples/session_lock.rs +++ b/examples/session_lock.rs @@ -176,6 +176,26 @@ impl CompositorHandler for AppData { _time: u32, ) { } + + fn surface_enter( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } + + fn surface_leave( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } } impl OutputHandler for AppData { diff --git a/examples/simple_layer.rs b/examples/simple_layer.rs index 883f58862..481fd1ad3 100644 --- a/examples/simple_layer.rs +++ b/examples/simple_layer.rs @@ -150,6 +150,26 @@ impl CompositorHandler for SimpleLayer { ) { self.draw(qh); } + + fn surface_enter( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } + + fn surface_leave( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } } impl OutputHandler for SimpleLayer { diff --git a/examples/simple_window.rs b/examples/simple_window.rs index 49c20b421..5d20b0a1c 100644 --- a/examples/simple_window.rs +++ b/examples/simple_window.rs @@ -178,6 +178,26 @@ impl CompositorHandler for SimpleWindow { ) { self.draw(conn, qh); } + + fn surface_enter( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } + + fn surface_leave( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } } impl OutputHandler for SimpleWindow { diff --git a/examples/themed_window.rs b/examples/themed_window.rs index 0871797c7..5be0f9453 100644 --- a/examples/themed_window.rs +++ b/examples/themed_window.rs @@ -213,6 +213,26 @@ impl CompositorHandler for SimpleWindow { ) { self.draw(conn, qh); } + + fn surface_enter( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } + + fn surface_leave( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + _surface: &wl_surface::WlSurface, + _output: &wl_output::WlOutput, + ) { + // Not needed for this example. + } } impl OutputHandler for SimpleWindow { diff --git a/src/compositor.rs b/src/compositor.rs index 7218fd656..e3c57a896 100644 --- a/src/compositor.rs +++ b/src/compositor.rs @@ -57,6 +57,24 @@ pub trait CompositorHandler: Sized { surface: &wl_surface::WlSurface, time: u32, ); + + /// The surface has entered an output. + fn surface_enter( + &mut self, + conn: &Connection, + qh: &QueueHandle, + surface: &wl_surface::WlSurface, + output: &wl_output::WlOutput, + ); + + /// The surface has left an output. + fn surface_leave( + &mut self, + conn: &Connection, + qh: &QueueHandle, + surface: &wl_surface::WlSurface, + output: &wl_output::WlOutput, + ); } pub trait SurfaceDataExt: Send + Sync { @@ -293,12 +311,16 @@ where let data = data.surface_data(); let mut inner = data.inner.lock().unwrap(); + let mut enter_or_leave_output: Option<(wl_output::WlOutput, bool)> = None; + match event { wl_surface::Event::Enter { output } => { - inner.outputs.push(output); + inner.outputs.push(output.clone()); + enter_or_leave_output.replace((output, true)); } wl_surface::Event::Leave { output } => { inner.outputs.retain(|o| o != &output); + enter_or_leave_output.replace((output, false)); } wl_surface::Event::PreferredBufferScale { factor } => { let current_scale = data.scale_factor.load(Ordering::Relaxed); @@ -326,6 +348,13 @@ where // NOTE: with v6 we don't need any special handling of the scale factor, everything // was handled from the above, so return. if surface.version() >= 6 { + drop(inner); + match enter_or_leave_output { + Some((output, true)) => state.surface_enter(conn, qh, surface, &output), + Some((output, false)) => state.surface_leave(conn, qh, surface, &output), + None => {} + }; + return; } @@ -346,6 +375,12 @@ where }); dispatch_surface_state_updates(state, conn, qh, surface, data, inner); + + match enter_or_leave_output { + Some((output, true)) => state.surface_enter(conn, qh, surface, &output), + Some((output, false)) => state.surface_leave(conn, qh, surface, &output), + None => {} + }; } }