Add initial layer-shell implementation
Browse files at this commit37cbed890f86ba148717b2e85a75337a17bd0167Parent: 8cdd37b5 changed files +216 -19
- src/handlers/compositor.rs +3 -0
- src/handlers/layer_shell.rs +100 -0
- src/handlers/mod.rs +1 -0
- src/layout.rs +10 -0
- src/niri.rs +102 -19
src/handlers/compositor.rsmodified+3 -0
| @@ -112,6 +112,9 @@ impl CompositorHandler for Niri { | |||
| 112 | } | 112 | } |
| 113 | } | 113 | } |
| 114 | } | 114 | } |
| 115 | + | ||
| 116 | + // This might be a layer-shell surface. | ||
| 117 | + self.layer_shell_handle_commit(surface); | ||
| 115 | } | 118 | } |
| 116 | } | 119 | } |
| 117 | 120 | ||
src/handlers/layer_shell.rsadded+100 -0
| @@ -0,0 +1,100 @@ | |||
| 1 | +use smithay::delegate_layer_shell; | ||
| 2 | +use smithay::desktop::{layer_map_for_output, LayerSurface, WindowSurfaceType}; | ||
| 3 | +use smithay::output::Output; | ||
| 4 | +use smithay::reexports::wayland_server::protocol::wl_output::WlOutput; | ||
| 5 | +use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | ||
| 6 | +use smithay::wayland::compositor::with_states; | ||
| 7 | +use smithay::wayland::shell::wlr_layer::{ | ||
| 8 | + Layer, LayerSurface as WlrLayerSurface, LayerSurfaceData, WlrLayerShellHandler, | ||
| 9 | + WlrLayerShellState, | ||
| 10 | +}; | ||
| 11 | + | ||
| 12 | +use crate::niri::Niri; | ||
| 13 | + | ||
| 14 | +impl WlrLayerShellHandler for Niri { | ||
| 15 | + fn shell_state(&mut self) -> &mut WlrLayerShellState { | ||
| 16 | + &mut self.layer_shell_state | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + fn new_layer_surface( | ||
| 20 | + &mut self, | ||
| 21 | + surface: WlrLayerSurface, | ||
| 22 | + wl_output: Option<WlOutput>, | ||
| 23 | + _layer: Layer, | ||
| 24 | + namespace: String, | ||
| 25 | + ) { | ||
| 26 | + let output = wl_output | ||
| 27 | + .as_ref() | ||
| 28 | + .and_then(Output::from_resource) | ||
| 29 | + .or_else(|| self.monitor_set.active_output().cloned()) | ||
| 30 | + .unwrap(); | ||
| 31 | + let mut map = layer_map_for_output(&output); | ||
| 32 | + map.map_layer(&LayerSurface::new(surface, namespace)) | ||
| 33 | + .unwrap(); | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + fn layer_destroyed(&mut self, surface: WlrLayerSurface) { | ||
| 37 | + let output = if let Some((output, mut map, layer)) = | ||
| 38 | + self.monitor_set.outputs().find_map(|o| { | ||
| 39 | + let map = layer_map_for_output(o); | ||
| 40 | + let layer = map | ||
| 41 | + .layers() | ||
| 42 | + .find(|&layer| layer.layer_surface() == &surface) | ||
| 43 | + .cloned(); | ||
| 44 | + layer.map(|layer| (o.clone(), map, layer)) | ||
| 45 | + }) { | ||
| 46 | + map.unmap_layer(&layer); | ||
| 47 | + Some(output) | ||
| 48 | + } else { | ||
| 49 | + None | ||
| 50 | + }; | ||
| 51 | + if let Some(output) = output { | ||
| 52 | + self.queue_redraw(output); | ||
| 53 | + } | ||
| 54 | + } | ||
| 55 | +} | ||
| 56 | +delegate_layer_shell!(Niri); | ||
| 57 | + | ||
| 58 | +impl Niri { | ||
| 59 | + pub fn layer_shell_handle_commit(&mut self, surface: &WlSurface) { | ||
| 60 | + let Some(output) = self | ||
| 61 | + .monitor_set | ||
| 62 | + .outputs() | ||
| 63 | + .find(|o| { | ||
| 64 | + let map = layer_map_for_output(o); | ||
| 65 | + map.layer_for_surface(surface, WindowSurfaceType::TOPLEVEL) | ||
| 66 | + .is_some() | ||
| 67 | + }) | ||
| 68 | + .cloned() | ||
| 69 | + else { | ||
| 70 | + return; | ||
| 71 | + }; | ||
| 72 | + | ||
| 73 | + let initial_configure_sent = with_states(surface, |states| { | ||
| 74 | + states | ||
| 75 | + .data_map | ||
| 76 | + .get::<LayerSurfaceData>() | ||
| 77 | + .unwrap() | ||
| 78 | + .lock() | ||
| 79 | + .unwrap() | ||
| 80 | + .initial_configure_sent | ||
| 81 | + }); | ||
| 82 | + | ||
| 83 | + let mut map = layer_map_for_output(&output); | ||
| 84 | + | ||
| 85 | + // arrange the layers before sending the initial configure | ||
| 86 | + // to respect any size the client may have sent | ||
| 87 | + map.arrange(); | ||
| 88 | + // send the initial configure if relevant | ||
| 89 | + if !initial_configure_sent { | ||
| 90 | + let layer = map | ||
| 91 | + .layer_for_surface(surface, WindowSurfaceType::TOPLEVEL) | ||
| 92 | + .unwrap(); | ||
| 93 | + | ||
| 94 | + layer.layer_surface().send_configure(); | ||
| 95 | + } | ||
| 96 | + drop(map); | ||
| 97 | + | ||
| 98 | + self.queue_redraw(output); | ||
| 99 | + } | ||
| 100 | +} | ||
src/handlers/mod.rsmodified+1 -0
| @@ -1,4 +1,5 @@ | |||
| 1 | mod compositor; | 1 | mod compositor; |
| 2 | +mod layer_shell; | ||
| 2 | mod xdg_shell; | 3 | mod xdg_shell; |
| 3 | 4 | ||
| 4 | // | 5 | // |
src/layout.rsmodified+10 -0
| @@ -566,6 +566,16 @@ impl<W: LayoutElement> MonitorSet<W> { | |||
| 566 | .find(|monitor| &monitor.output == output) | 566 | .find(|monitor| &monitor.output == output) |
| 567 | } | 567 | } |
| 568 | 568 | ||
| 569 | + pub fn outputs(&self) -> impl Iterator<Item = &Output> + '_ { | ||
| 570 | + let monitors = if let MonitorSet::Normal { monitors, .. } = self { | ||
| 571 | + &monitors[..] | ||
| 572 | + } else { | ||
| 573 | + &[][..] | ||
| 574 | + }; | ||
| 575 | + | ||
| 576 | + monitors.iter().map(|mon| &mon.output) | ||
| 577 | + } | ||
| 578 | + | ||
| 569 | pub fn move_left(&mut self) { | 579 | pub fn move_left(&mut self) { |
| 570 | let Some(monitor) = self.active_monitor() else { | 580 | let Some(monitor) = self.active_monitor() else { |
| 571 | return; | 581 | return; |
src/niri.rsmodified+102 -19
| @@ -3,10 +3,13 @@ use std::os::unix::io::AsRawFd; | |||
| 3 | use std::sync::Arc; | 3 | use std::sync::Arc; |
| 4 | use std::time::Duration; | 4 | use std::time::Duration; |
| 5 | 5 | ||
| 6 | -use smithay::backend::renderer::element::render_elements; | ||
| 7 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; | 6 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; |
| 7 | +use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; | ||
| 8 | +use smithay::backend::renderer::element::{render_elements, AsRenderElements}; | ||
| 8 | use smithay::backend::renderer::ImportAll; | 9 | use smithay::backend::renderer::ImportAll; |
| 9 | -use smithay::desktop::{PopupManager, Space, Window, WindowSurfaceType}; | 10 | +use smithay::desktop::{ |
| 11 | + layer_map_for_output, LayerSurface, PopupManager, Space, Window, WindowSurfaceType, | ||
| 12 | +}; | ||
| 10 | use smithay::input::keyboard::XkbConfig; | 13 | use smithay::input::keyboard::XkbConfig; |
| 11 | use smithay::input::{Seat, SeatState}; | 14 | use smithay::input::{Seat, SeatState}; |
| 12 | use smithay::output::Output; | 15 | use smithay::output::Output; |
| @@ -18,10 +21,12 @@ use smithay::reexports::wayland_server::backend::{ | |||
| 18 | }; | 21 | }; |
| 19 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | 22 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 20 | use smithay::reexports::wayland_server::{Display, DisplayHandle}; | 23 | use smithay::reexports::wayland_server::{Display, DisplayHandle}; |
| 21 | -use smithay::utils::{Logical, Point, SERIAL_COUNTER}; | 24 | +use smithay::utils::{Logical, Point, Scale, SERIAL_COUNTER}; |
| 22 | use smithay::wayland::compositor::{CompositorClientState, CompositorState}; | 25 | use smithay::wayland::compositor::{CompositorClientState, CompositorState}; |
| 23 | use smithay::wayland::data_device::DataDeviceState; | 26 | use smithay::wayland::data_device::DataDeviceState; |
| 24 | use smithay::wayland::output::OutputManagerState; | 27 | use smithay::wayland::output::OutputManagerState; |
| 28 | +use smithay::wayland::seat::WaylandFocus; | ||
| 29 | +use smithay::wayland::shell::wlr_layer::{Layer, WlrLayerShellState}; | ||
| 25 | use smithay::wayland::shell::xdg::XdgShellState; | 30 | use smithay::wayland::shell::xdg::XdgShellState; |
| 26 | use smithay::wayland::shm::ShmState; | 31 | use smithay::wayland::shm::ShmState; |
| 27 | use smithay::wayland::socket::ListeningSocketSource; | 32 | use smithay::wayland::socket::ListeningSocketSource; |
| @@ -53,6 +58,7 @@ pub struct Niri { | |||
| 53 | // Smithay state. | 58 | // Smithay state. |
| 54 | pub compositor_state: CompositorState, | 59 | pub compositor_state: CompositorState, |
| 55 | pub xdg_shell_state: XdgShellState, | 60 | pub xdg_shell_state: XdgShellState, |
| 61 | + pub layer_shell_state: WlrLayerShellState, | ||
| 56 | pub shm_state: ShmState, | 62 | pub shm_state: ShmState, |
| 57 | pub output_manager_state: OutputManagerState, | 63 | pub output_manager_state: OutputManagerState, |
| 58 | pub seat_state: SeatState<Self>, | 64 | pub seat_state: SeatState<Self>, |
| @@ -95,6 +101,7 @@ impl Niri { | |||
| 95 | WmCapabilities::WindowMenu, | 101 | WmCapabilities::WindowMenu, |
| 96 | ], | 102 | ], |
| 97 | ); | 103 | ); |
| 104 | + let layer_shell_state = WlrLayerShellState::new::<Self>(&display_handle); | ||
| 98 | let shm_state = ShmState::new::<Self>(&display_handle, vec![]); | 105 | let shm_state = ShmState::new::<Self>(&display_handle, vec![]); |
| 99 | let output_manager_state = OutputManagerState::new_with_xdg_output::<Self>(&display_handle); | 106 | let output_manager_state = OutputManagerState::new_with_xdg_output::<Self>(&display_handle); |
| 100 | let mut seat_state = SeatState::new(); | 107 | let mut seat_state = SeatState::new(); |
| @@ -155,6 +162,7 @@ impl Niri { | |||
| 155 | 162 | ||
| 156 | compositor_state, | 163 | compositor_state, |
| 157 | xdg_shell_state, | 164 | xdg_shell_state, |
| 165 | + layer_shell_state, | ||
| 158 | shm_state, | 166 | shm_state, |
| 159 | output_manager_state, | 167 | output_manager_state, |
| 160 | seat_state, | 168 | seat_state, |
| @@ -203,6 +211,7 @@ impl Niri { | |||
| 203 | 211 | ||
| 204 | pub fn output_resized(&mut self, output: Output) { | 212 | pub fn output_resized(&mut self, output: Output) { |
| 205 | self.monitor_set.update_output(&output); | 213 | self.monitor_set.update_output(&output); |
| 214 | + layer_map_for_output(&output).arrange(); | ||
| 206 | self.queue_redraw(output); | 215 | self.queue_redraw(output); |
| 207 | } | 216 | } |
| 208 | 217 | ||
| @@ -256,11 +265,23 @@ impl Niri { | |||
| 256 | self.global_space.output_under(pos).next().cloned() | 265 | self.global_space.output_under(pos).next().cloned() |
| 257 | } | 266 | } |
| 258 | 267 | ||
| 268 | + fn layer_surface_focus(&self) -> Option<WlSurface> { | ||
| 269 | + let output = self.monitor_set.active_output()?; | ||
| 270 | + let layers = layer_map_for_output(output); | ||
| 271 | + let surface = layers | ||
| 272 | + .layers_on(Layer::Overlay) | ||
| 273 | + .chain(layers.layers_on(Layer::Top)) | ||
| 274 | + .find(|surface| surface.can_receive_keyboard_focus())?; | ||
| 275 | + | ||
| 276 | + Some(surface.wl_surface().clone()) | ||
| 277 | + } | ||
| 278 | + | ||
| 259 | pub fn update_focus(&mut self) { | 279 | pub fn update_focus(&mut self) { |
| 260 | - let focus = self | 280 | + let focus = self.layer_surface_focus().or_else(|| { |
| 261 | - .monitor_set | 281 | + self.monitor_set |
| 262 | - .focus() | 282 | + .focus() |
| 263 | - .map(|win| win.toplevel().wl_surface().clone()); | 283 | + .map(|win| win.toplevel().wl_surface().clone()) |
| 284 | + }); | ||
| 264 | let keyboard = self.seat.get_keyboard().unwrap(); | 285 | let keyboard = self.seat.get_keyboard().unwrap(); |
| 265 | if keyboard.current_focus() != focus { | 286 | if keyboard.current_focus() != focus { |
| 266 | keyboard.set_focus(self, focus, SERIAL_COUNTER.next_serial()); | 287 | keyboard.set_focus(self, focus, SERIAL_COUNTER.next_serial()); |
| @@ -306,38 +327,100 @@ impl Niri { | |||
| 306 | assert!(state.queued_redraw.take().is_some()); | 327 | assert!(state.queued_redraw.take().is_some()); |
| 307 | assert!(!state.waiting_for_vblank); | 328 | assert!(!state.waiting_for_vblank); |
| 308 | 329 | ||
| 330 | + let renderer = backend.renderer(); | ||
| 331 | + | ||
| 309 | let mon = self.monitor_set.monitor_for_output_mut(output).unwrap(); | 332 | let mon = self.monitor_set.monitor_for_output_mut(output).unwrap(); |
| 310 | mon.advance_animations(presentation_time); | 333 | mon.advance_animations(presentation_time); |
| 311 | - let elements = mon.render_elements(backend.renderer()); | 334 | + // Get monitor elements. |
| 335 | + let monitor_elements = mon.render_elements(renderer); | ||
| 312 | 336 | ||
| 313 | let output_pos = self.global_space.output_geometry(output).unwrap().loc; | 337 | let output_pos = self.global_space.output_geometry(output).unwrap().loc; |
| 314 | let pointer_pos = self.seat.get_pointer().unwrap().current_location() - output_pos.to_f64(); | 338 | let pointer_pos = self.seat.get_pointer().unwrap().current_location() - output_pos.to_f64(); |
| 315 | 339 | ||
| 316 | - let mut elements: Vec<_> = elements | 340 | + // Get layer-shell elements. |
| 317 | - .into_iter() | 341 | + let layer_map = layer_map_for_output(output); |
| 318 | - .map(OutputRenderElements::from) | 342 | + let (lower, upper): (Vec<&LayerSurface>, Vec<&LayerSurface>) = layer_map |
| 319 | - .collect(); | 343 | + .layers() |
| 320 | - elements.insert( | 344 | + .rev() |
| 321 | - 0, | 345 | + .partition(|s| matches!(s.layer(), Layer::Background | Layer::Bottom)); |
| 322 | - OutputRenderElements::Pointer(SolidColorRenderElement::from_buffer( | 346 | + |
| 347 | + // The pointer goes on the top. | ||
| 348 | + let mut elements = vec![OutputRenderElements::Pointer( | ||
| 349 | + SolidColorRenderElement::from_buffer( | ||
| 323 | &self.pointer_buffer, | 350 | &self.pointer_buffer, |
| 324 | pointer_pos.to_physical_precise_round(1.), | 351 | pointer_pos.to_physical_precise_round(1.), |
| 325 | 1., | 352 | 1., |
| 326 | 1., | 353 | 1., |
| 327 | - )), | 354 | + ), |
| 355 | + )]; | ||
| 356 | + | ||
| 357 | + // Then the upper layer-shell elements. | ||
| 358 | + elements.extend( | ||
| 359 | + upper | ||
| 360 | + .into_iter() | ||
| 361 | + .filter_map(|surface| { | ||
| 362 | + layer_map | ||
| 363 | + .layer_geometry(surface) | ||
| 364 | + .map(|geo| (geo.loc, surface)) | ||
| 365 | + }) | ||
| 366 | + .flat_map(|(loc, surface)| { | ||
| 367 | + surface | ||
| 368 | + .render_elements( | ||
| 369 | + renderer, | ||
| 370 | + loc.to_physical_precise_round(1.), | ||
| 371 | + Scale::from(1.), | ||
| 372 | + 1., | ||
| 373 | + ) | ||
| 374 | + .into_iter() | ||
| 375 | + .map(OutputRenderElements::Wayland) | ||
| 376 | + }), | ||
| 377 | + ); | ||
| 378 | + | ||
| 379 | + // Then the regular monitor elements. | ||
| 380 | + elements.extend(monitor_elements.into_iter().map(OutputRenderElements::from)); | ||
| 381 | + | ||
| 382 | + // Then the lower layer-shell elements. | ||
| 383 | + elements.extend( | ||
| 384 | + lower | ||
| 385 | + .into_iter() | ||
| 386 | + .filter_map(|surface| { | ||
| 387 | + layer_map | ||
| 388 | + .layer_geometry(surface) | ||
| 389 | + .map(|geo| (geo.loc, surface)) | ||
| 390 | + }) | ||
| 391 | + .flat_map(|(loc, surface)| { | ||
| 392 | + surface | ||
| 393 | + .render_elements( | ||
| 394 | + renderer, | ||
| 395 | + loc.to_physical_precise_round(1.), | ||
| 396 | + Scale::from(1.), | ||
| 397 | + 1., | ||
| 398 | + ) | ||
| 399 | + .into_iter() | ||
| 400 | + .map(OutputRenderElements::Wayland) | ||
| 401 | + }), | ||
| 328 | ); | 402 | ); |
| 329 | 403 | ||
| 404 | + // Hand it over to the backend. | ||
| 330 | backend.render(self, output, &elements); | 405 | backend.render(self, output, &elements); |
| 331 | 406 | ||
| 332 | - self.monitor_set | 407 | + // Send frame callbacks. |
| 333 | - .send_frame(output, self.start_time.elapsed()); | 408 | + let frame_callback_time = self.start_time.elapsed(); |
| 409 | + self.monitor_set.send_frame(output, frame_callback_time); | ||
| 410 | + | ||
| 411 | + for surface in layer_map.layers() { | ||
| 412 | + surface.send_frame(output, frame_callback_time, None, |_, _| { | ||
| 413 | + Some(output.clone()) | ||
| 414 | + }); | ||
| 415 | + } | ||
| 334 | } | 416 | } |
| 335 | } | 417 | } |
| 336 | 418 | ||
| 337 | render_elements! { | 419 | render_elements! { |
| 338 | #[derive(Debug)] | 420 | #[derive(Debug)] |
| 339 | pub OutputRenderElements<R> where R: ImportAll; | 421 | pub OutputRenderElements<R> where R: ImportAll; |
| 340 | - Wayland = MonitorRenderElement<R>, | 422 | + Monitor = MonitorRenderElement<R>, |
| 423 | + Wayland = WaylandSurfaceRenderElement<R>, | ||
| 341 | Pointer = SolidColorRenderElement, | 424 | Pointer = SolidColorRenderElement, |
| 342 | } | 425 | } |
| 343 | 426 | ||