| 1 | use smithay::delegate_layer_shell; |
| 2 | use smithay::desktop::{layer_map_for_output, LayerSurface, PopupKind, 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 | use smithay::wayland::shell::xdg::PopupSurface; |
| 12 | |
| 13 | use crate::niri::State; |
| 14 | use crate::utils::send_scale_transform; |
| 15 | |
| 16 | impl WlrLayerShellHandler for State { |
| 17 | fn shell_state(&mut self) -> &mut WlrLayerShellState { |
| 18 | &mut self.niri.layer_shell_state |
| 19 | } |
| 20 | |
| 21 | fn new_layer_surface( |
| 22 | &mut self, |
| 23 | surface: WlrLayerSurface, |
| 24 | wl_output: Option<WlOutput>, |
| 25 | _layer: Layer, |
| 26 | namespace: String, |
| 27 | ) { |
| 28 | let output = if let Some(wl_output) = &wl_output { |
| 29 | Output::from_resource(wl_output) |
| 30 | } else { |
| 31 | self.niri.layout.active_output().cloned() |
| 32 | }; |
| 33 | let Some(output) = output else { |
| 34 | warn!("no output for new layer surface, closing"); |
| 35 | surface.send_close(); |
| 36 | return; |
| 37 | }; |
| 38 | |
| 39 | let mut map = layer_map_for_output(&output); |
| 40 | map.map_layer(&LayerSurface::new(surface, namespace)) |
| 41 | .unwrap(); |
| 42 | } |
| 43 | |
| 44 | fn layer_destroyed(&mut self, surface: WlrLayerSurface) { |
| 45 | let output = if let Some((output, mut map, layer)) = |
| 46 | self.niri.layout.outputs().find_map(|o| { |
| 47 | let map = layer_map_for_output(o); |
| 48 | let layer = map |
| 49 | .layers() |
| 50 | .find(|&layer| layer.layer_surface() == &surface) |
| 51 | .cloned(); |
| 52 | layer.map(|layer| (o.clone(), map, layer)) |
| 53 | }) { |
| 54 | map.unmap_layer(&layer); |
| 55 | Some(output) |
| 56 | } else { |
| 57 | None |
| 58 | }; |
| 59 | if let Some(output) = output { |
| 60 | self.niri.output_resized(&output); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | fn new_popup(&mut self, _parent: WlrLayerSurface, popup: PopupSurface) { |
| 65 | self.unconstrain_popup(&PopupKind::Xdg(popup)); |
| 66 | } |
| 67 | } |
| 68 | delegate_layer_shell!(State); |
| 69 | |
| 70 | impl State { |
| 71 | pub fn layer_shell_handle_commit(&mut self, surface: &WlSurface) { |
| 72 | let Some(output) = self |
| 73 | .niri |
| 74 | .layout |
| 75 | .outputs() |
| 76 | .find(|o| { |
| 77 | let map = layer_map_for_output(o); |
| 78 | map.layer_for_surface(surface, WindowSurfaceType::TOPLEVEL) |
| 79 | .is_some() |
| 80 | }) |
| 81 | .cloned() |
| 82 | else { |
| 83 | return; |
| 84 | }; |
| 85 | |
| 86 | let initial_configure_sent = with_states(surface, |states| { |
| 87 | states |
| 88 | .data_map |
| 89 | .get::<LayerSurfaceData>() |
| 90 | .unwrap() |
| 91 | .lock() |
| 92 | .unwrap() |
| 93 | .initial_configure_sent |
| 94 | }); |
| 95 | |
| 96 | let mut map = layer_map_for_output(&output); |
| 97 | |
| 98 | |
| 99 | |
| 100 | map.arrange(); |
| 101 | |
| 102 | if !initial_configure_sent { |
| 103 | let layer = map |
| 104 | .layer_for_surface(surface, WindowSurfaceType::TOPLEVEL) |
| 105 | .unwrap(); |
| 106 | |
| 107 | let scale = output.current_scale(); |
| 108 | let transform = output.current_transform(); |
| 109 | with_states(surface, |data| { |
| 110 | send_scale_transform(surface, data, scale, transform); |
| 111 | }); |
| 112 | |
| 113 | layer.layer_surface().send_configure(); |
| 114 | } |
| 115 | drop(map); |
| 116 | |
| 117 | self.niri.output_resized(&output); |
| 118 | } |
| 119 | } |