forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

layer_shell.rs216 lines, 8.3 KiBHistoryRawDownload
1use smithay::delegate_layer_shell;
2use smithay::desktop::{layer_map_for_output, LayerSurface, PopupKind, WindowSurfaceType};
3use smithay::output::Output;
4use smithay::reexports::wayland_server::protocol::wl_output::WlOutput;
5use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
6use smithay::wayland::compositor::{get_parent, with_states};
7use smithay::wayland::shell::wlr_layer::{
8 self, Layer, LayerSurface as WlrLayerSurface, LayerSurfaceData, WlrLayerShellHandler,
9 WlrLayerShellState,
10};
11use smithay::wayland::shell::xdg::PopupSurface;
12
13use crate::layer::{MappedLayer, ResolvedLayerRules};
14use crate::niri::State;
15use crate::utils::{is_mapped, output_size, send_scale_transform};
16
17impl 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}
77delegate_layer_shell!(State);
78
79impl 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
129 let rules = &config.layer_rules;
130 let rules =
131 ResolvedLayerRules::compute(rules, layer, self.niri.is_at_startup);
132
133 let output_size = output_size(&output);
134 let scale = output.current_scale().fractional_scale();
135
136 let mapped = MappedLayer::new(
137 layer.clone(),
138 rules,
139 output_size,
140 scale,
141 self.niri.clock.clone(),
142 &config,
143 );
144
145 let prev = self
146 .niri
147 .mapped_layer_surfaces
148 .insert(layer.clone(), mapped);
149 if prev.is_some() {
150 error!("MappedLayer was present for an unmapped surface");
151 }
152 }
153
154 // Give focus to newly mapped on-demand surfaces. Some launchers like
155 // lxqt-runner rely on this behavior. While this behavior doesn't make much
156 // sense for other clients like panels, the consensus seems to be that it's not
157 // a big deal since panels generally only open once at the start of the
158 // session.
159 //
160 // Note that:
161 // 1) Exclusive layer surfaces already get focus automatically in
162 // update_keyboard_focus().
163 // 2) Same-layer exclusive layer surfaces are already preferred to on-demand
164 // surfaces in update_keyboard_focus(), so we don't need to check for that
165 // here.
166 //
167 // https://github.com/YaLTeR/niri/issues/641
168 let on_demand = layer.cached_state().keyboard_interactivity
169 == wlr_layer::KeyboardInteractivity::OnDemand;
170 if was_unmapped && on_demand {
171 // I guess it'd make sense to check that no higher-layer on-demand surface
172 // has focus, but Smithay's Layer doesn't implement Ord so this would be a
173 // little annoying.
174 self.niri.layer_shell_on_demand_focus = Some(layer.clone());
175 }
176 } else {
177 let was_mapped = self.niri.mapped_layer_surfaces.remove(layer).is_some();
178 self.niri.unmapped_layer_surfaces.insert(surface.clone());
179
180 // After layer surface unmaps it has to perform the initial commit-configure
181 // sequence again. This is a workaround until Smithay properly resets
182 // initial_configure_sent upon the surface unmapping itself as it does for
183 // toplevels.
184 if was_mapped {
185 with_states(surface, |states| {
186 let mut data = states
187 .data_map
188 .get::<LayerSurfaceData>()
189 .unwrap()
190 .lock()
191 .unwrap();
192 data.initial_configure_sent = false;
193 });
194 }
195 }
196 } else {
197 let scale = output.current_scale();
198 let transform = output.current_transform();
199 with_states(surface, |data| {
200 send_scale_transform(surface, data, scale, transform);
201 });
202
203 layer.layer_surface().send_configure();
204 }
205 drop(map);
206
207 // This will call queue_redraw() inside.
208 self.niri.output_resized(&output);
209 } else {
210 // This is an unsync layer-shell subsurface.
211 self.niri.queue_redraw(&output);
212 }
213
214 true
215 }
216}