niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·99bf2df·Ivan Molodetskikh·1 year ago·2106 commits
| 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::{get_parent, with_states}; |
| 7 | use smithay::wayland::shell::wlr_layer::{ |
| 8 | self, Layer, LayerSurface as WlrLayerSurface, LayerSurfaceData, WlrLayerShellHandler, |
| 9 | WlrLayerShellState, |
| 10 | }; |
| 11 | use smithay::wayland::shell::xdg::PopupSurface; |
| 12 | |
| 13 | use crate::layer::{MappedLayer, ResolvedLayerRules}; |
| 14 | use crate::niri::State; |
| 15 | use crate::utils::{is_mapped, send_scale_transform}; |
| 16 | |
| 17 | impl WlrLayerShellHandler for State { |
| 18 | fn shell_state(&mut self) -> &mut WlrLayerShellState { |
| 19 | &mut self.niri.layer_shell_state |
| 20 | } |
| 21 | |
| 22 | fn new_layer_surface( |
| 23 | &mut self, |
| 24 | surface: WlrLayerSurface, |
| 25 | wl_output: Option<WlOutput>, |
| 26 | _layer: Layer, |
| 27 | namespace: String, |
| 28 | ) { |
| 29 | let output = if let Some(wl_output) = &wl_output { |
| 30 | Output::from_resource(wl_output) |
| 31 | } else { |
| 32 | self.niri.layout.active_output().cloned() |
| 33 | }; |
| 34 | let Some(output) = output else { |
| 35 | warn!("no output for new layer surface, closing"); |
| 36 | surface.send_close(); |
| 37 | return; |
| 38 | }; |
| 39 | |
| 40 | let wl_surface = surface.wl_surface().clone(); |
| 41 | let is_new = self.niri.unmapped_layer_surfaces.insert(wl_surface); |
| 42 | assert!(is_new); |
| 43 | |
| 44 | let mut map = layer_map_for_output(&output); |
| 45 | map.map_layer(&LayerSurface::new(surface, namespace)) |
| 46 | .unwrap(); |
| 47 | } |
| 48 | |
| 49 | fn layer_destroyed(&mut self, surface: WlrLayerSurface) { |
| 50 | let wl_surface = surface.wl_surface(); |
| 51 | self.niri.unmapped_layer_surfaces.remove(wl_surface); |
| 52 | |
| 53 | let output = if let Some((output, mut map, layer)) = |
| 54 | self.niri.layout.outputs().find_map(|o| { |
| 55 | let map = layer_map_for_output(o); |
| 56 | let layer = map |
| 57 | .layers() |
| 58 | .find(|&layer| layer.layer_surface() == &surface) |
| 59 | .cloned(); |
| 60 | layer.map(|layer| (o.clone(), map, layer)) |
| 61 | }) { |
| 62 | map.unmap_layer(&layer); |
| 63 | self.niri.mapped_layer_surfaces.remove(&layer); |
| 64 | Some(output) |
| 65 | } else { |
| 66 | None |
| 67 | }; |
| 68 | if let Some(output) = output { |
| 69 | self.niri.output_resized(&output); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | fn new_popup(&mut self, _parent: WlrLayerSurface, popup: PopupSurface) { |
| 74 | self.unconstrain_popup(&PopupKind::Xdg(popup)); |
| 75 | } |
| 76 | } |
| 77 | delegate_layer_shell!(State); |
| 78 | |
| 79 | impl State { |
| 80 | pub fn layer_shell_handle_commit(&mut self, surface: &WlSurface) -> bool { |
| 81 | let mut root_surface = surface.clone(); |
| 82 | while let Some(parent) = get_parent(&root_surface) { |
| 83 | root_surface = parent; |
| 84 | } |
| 85 | |
| 86 | let output = self |
| 87 | .niri |
| 88 | .layout |
| 89 | .outputs() |
| 90 | .find(|o| { |
| 91 | let map = layer_map_for_output(o); |
| 92 | map.layer_for_surface(&root_surface, WindowSurfaceType::TOPLEVEL) |
| 93 | .is_some() |
| 94 | }) |
| 95 | .cloned(); |
| 96 | let Some(output) = output else { |
| 97 | return false; |
| 98 | }; |
| 99 | |
| 100 | if surface == &root_surface { |
| 101 | let initial_configure_sent = with_states(surface, |states| { |
| 102 | states |
| 103 | .data_map |
| 104 | .get::<LayerSurfaceData>() |
| 105 | .unwrap() |
| 106 | .lock() |
| 107 | .unwrap() |
| 108 | .initial_configure_sent |
| 109 | }); |
| 110 | |
| 111 | let mut map = layer_map_for_output(&output); |
| 112 | |
| 113 | // Arrange the layers before sending the initial configure to respect any size the |
| 114 | // client may have sent. |
| 115 | map.arrange(); |
| 116 | |
| 117 | let layer = map |
| 118 | .layer_for_surface(surface, WindowSurfaceType::TOPLEVEL) |
| 119 | .unwrap(); |
| 120 | |
| 121 | if initial_configure_sent { |
| 122 | if is_mapped(surface) { |
| 123 | let was_unmapped = self.niri.unmapped_layer_surfaces.remove(surface); |
| 124 | |
| 125 | // Resolve rules for newly mapped layer surfaces. |
| 126 | if was_unmapped { |
| 127 | let config = self.niri.config.borrow(); |
| 128 | let rules = &config.layer_rules; |
| 129 | let rules = |
| 130 | ResolvedLayerRules::compute(rules, layer, self.niri.is_at_startup); |
| 131 | let mapped = MappedLayer::new(layer.clone(), rules, &config); |
| 132 | let prev = self |
| 133 | .niri |
| 134 | .mapped_layer_surfaces |
| 135 | .insert(layer.clone(), mapped); |
| 136 | if prev.is_some() { |
| 137 | error!("MappedLayer was present for an unmapped surface"); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Give focus to newly mapped on-demand surfaces. Some launchers like |
| 142 | // lxqt-runner rely on this behavior. While this behavior doesn't make much |
| 143 | // sense for other clients like panels, the consensus seems to be that it's not |
| 144 | // a big deal since panels generally only open once at the start of the |
| 145 | // session. |
| 146 | // |
| 147 | // Note that: |
| 148 | // 1) Exclusive layer surfaces already get focus automatically in |
| 149 | // update_keyboard_focus(). |
| 150 | // 2) Same-layer exclusive layer surfaces are already preferred to on-demand |
| 151 | // surfaces in update_keyboard_focus(), so we don't need to check for that |
| 152 | // here. |
| 153 | // |
| 154 | // https://github.com/YaLTeR/niri/issues/641 |
| 155 | let on_demand = layer.cached_state().keyboard_interactivity |
| 156 | == wlr_layer::KeyboardInteractivity::OnDemand; |
| 157 | if was_unmapped && on_demand { |
| 158 | // I guess it'd make sense to check that no higher-layer on-demand surface |
| 159 | // has focus, but Smithay's Layer doesn't implement Ord so this would be a |
| 160 | // little annoying. |
| 161 | self.niri.layer_shell_on_demand_focus = Some(layer.clone()); |
| 162 | } |
| 163 | } else { |
| 164 | self.niri.mapped_layer_surfaces.remove(layer); |
| 165 | self.niri.unmapped_layer_surfaces.insert(surface.clone()); |
| 166 | } |
| 167 | } else { |
| 168 | let scale = output.current_scale(); |
| 169 | let transform = output.current_transform(); |
| 170 | with_states(surface, |data| { |
| 171 | send_scale_transform(surface, data, scale, transform); |
| 172 | }); |
| 173 | |
| 174 | layer.layer_surface().send_configure(); |
| 175 | } |
| 176 | drop(map); |
| 177 | |
| 178 | // This will call queue_redraw() inside. |
| 179 | self.niri.output_resized(&output); |
| 180 | } else { |
| 181 | // This is an unsync layer-shell subsurface. |
| 182 | self.niri.queue_redraw(&output); |
| 183 | } |
| 184 | |
| 185 | true |
| 186 | } |
| 187 | } |