niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·f51dd67·Ivan Molodetskikh·2 years ago·981 commits
| 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::{send_surface_state, 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 | |
| 15 | impl WlrLayerShellHandler for State { |
| 16 | fn shell_state(&mut self) -> &mut WlrLayerShellState { |
| 17 | &mut self.niri.layer_shell_state |
| 18 | } |
| 19 | |
| 20 | fn new_layer_surface( |
| 21 | &mut self, |
| 22 | surface: WlrLayerSurface, |
| 23 | wl_output: Option<WlOutput>, |
| 24 | _layer: Layer, |
| 25 | namespace: String, |
| 26 | ) { |
| 27 | let output = wl_output |
| 28 | .as_ref() |
| 29 | .and_then(Output::from_resource) |
| 30 | .or_else(|| self.niri.layout.active_output().cloned()) |
| 31 | .unwrap(); |
| 32 | let mut map = layer_map_for_output(&output); |
| 33 | map.map_layer(&LayerSurface::new(surface, namespace)) |
| 34 | .unwrap(); |
| 35 | } |
| 36 | |
| 37 | fn layer_destroyed(&mut self, surface: WlrLayerSurface) { |
| 38 | let output = if let Some((output, mut map, layer)) = |
| 39 | self.niri.layout.outputs().find_map(|o| { |
| 40 | let map = layer_map_for_output(o); |
| 41 | let layer = map |
| 42 | .layers() |
| 43 | .find(|&layer| layer.layer_surface() == &surface) |
| 44 | .cloned(); |
| 45 | layer.map(|layer| (o.clone(), map, layer)) |
| 46 | }) { |
| 47 | map.unmap_layer(&layer); |
| 48 | Some(output) |
| 49 | } else { |
| 50 | None |
| 51 | }; |
| 52 | if let Some(output) = output { |
| 53 | self.niri.output_resized(&output); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | fn new_popup(&mut self, _parent: WlrLayerSurface, popup: PopupSurface) { |
| 58 | self.unconstrain_popup(&popup); |
| 59 | } |
| 60 | } |
| 61 | delegate_layer_shell!(State); |
| 62 | |
| 63 | impl State { |
| 64 | pub fn layer_shell_handle_commit(&mut self, surface: &WlSurface) { |
| 65 | let Some(output) = self |
| 66 | .niri |
| 67 | .layout |
| 68 | .outputs() |
| 69 | .find(|o| { |
| 70 | let map = layer_map_for_output(o); |
| 71 | map.layer_for_surface(surface, WindowSurfaceType::TOPLEVEL) |
| 72 | .is_some() |
| 73 | }) |
| 74 | .cloned() |
| 75 | else { |
| 76 | return; |
| 77 | }; |
| 78 | |
| 79 | let initial_configure_sent = with_states(surface, |states| { |
| 80 | states |
| 81 | .data_map |
| 82 | .get::<LayerSurfaceData>() |
| 83 | .unwrap() |
| 84 | .lock() |
| 85 | .unwrap() |
| 86 | .initial_configure_sent |
| 87 | }); |
| 88 | |
| 89 | let mut map = layer_map_for_output(&output); |
| 90 | |
| 91 | // arrange the layers before sending the initial configure |
| 92 | // to respect any size the client may have sent |
| 93 | map.arrange(); |
| 94 | // send the initial configure if relevant |
| 95 | if !initial_configure_sent { |
| 96 | let layer = map |
| 97 | .layer_for_surface(surface, WindowSurfaceType::TOPLEVEL) |
| 98 | .unwrap(); |
| 99 | |
| 100 | let scale = output.current_scale().integer_scale(); |
| 101 | let transform = output.current_transform(); |
| 102 | with_states(surface, |data| { |
| 103 | send_surface_state(surface, data, scale, transform); |
| 104 | }); |
| 105 | |
| 106 | layer.layer_surface().send_configure(); |
| 107 | } |
| 108 | drop(map); |
| 109 | |
| 110 | self.niri.output_resized(&output); |
| 111 | } |
| 112 | } |