forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

layer_shell.rs240 lines, 9.1 KiBHistoryRawDownload
1use smithay::desktop::{layer_map_for_output, LayerSurface, PopupKind, WindowSurfaceType};
2use smithay::reexports::wayland_server::protocol::wl_output::WlOutput;
3use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
4use smithay::wayland::compositor::{add_pre_commit_hook, get_parent, with_states, HookId};
5use smithay::wayland::shell::wlr_layer::{
6 self, Layer, LayerSurface as WlrLayerSurface, LayerSurfaceCachedState, LayerSurfaceData,
7 WlrLayerShellHandler, WlrLayerShellState,
8};
9use smithay::wayland::shell::xdg::PopupSurface;
10
11use crate::layer::{MappedLayer, ResolvedLayerRules};
12use crate::niri::State;
13use crate::utils::{is_mapped, output_size, send_scale_transform};
14
15impl 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 = if let Some(wl_output) = &wl_output {
28 self.niri.output_from_resource(wl_output)
29 } else {
30 self.niri.layout.active_output().cloned()
31 };
32 let Some(output) = output else {
33 warn!("no output for new layer surface, closing");
34 surface.send_close();
35 return;
36 };
37
38 let wl_surface = surface.wl_surface().clone();
39 let is_new = self.niri.unmapped_layer_surfaces.insert(wl_surface);
40 assert!(is_new);
41
42 let mut map = layer_map_for_output(&output);
43 map.map_layer(&LayerSurface::new(surface, namespace))
44 .unwrap();
45 }
46
47 fn layer_destroyed(&mut self, surface: WlrLayerSurface) {
48 let wl_surface = surface.wl_surface();
49 self.niri.unmapped_layer_surfaces.remove(wl_surface);
50
51 let output = if let Some((output, mut map, layer)) =
52 self.niri.layout.outputs().find_map(|o| {
53 let map = layer_map_for_output(o);
54 let layer = map
55 .layers()
56 .find(|&layer| layer.layer_surface() == &surface)
57 .cloned();
58 layer.map(|layer| (o.clone(), map, layer))
59 }) {
60 map.unmap_layer(&layer);
61 self.niri.mapped_layer_surfaces.remove(&layer);
62 Some(output)
63 } else {
64 None
65 };
66 if let Some(output) = output {
67 self.niri.output_resized(&output);
68 }
69 }
70
71 fn new_popup(&mut self, _parent: WlrLayerSurface, popup: PopupSurface) {
72 self.unconstrain_popup(&PopupKind::Xdg(popup));
73 }
74}
75
76impl State {
77 pub fn layer_shell_handle_commit(&mut self, surface: &WlSurface) -> bool {
78 let mut root_surface = surface.clone();
79 while let Some(parent) = get_parent(&root_surface) {
80 root_surface = parent;
81 }
82
83 let output = self
84 .niri
85 .layout
86 .outputs()
87 .find(|o| {
88 let map = layer_map_for_output(o);
89 map.layer_for_surface(&root_surface, WindowSurfaceType::TOPLEVEL)
90 .is_some()
91 })
92 .cloned();
93 let Some(output) = output else {
94 return false;
95 };
96
97 if surface != &root_surface {
98 // This is an unsync layer-shell subsurface.
99 self.niri.queue_redraw(&output);
100 return true;
101 }
102
103 let mut map = layer_map_for_output(&output);
104
105 // Arrange the layers before sending the initial configure to respect any size the
106 // client may have sent.
107 map.arrange();
108
109 let layer = map
110 .layer_for_surface(surface, WindowSurfaceType::TOPLEVEL)
111 .unwrap();
112
113 if is_mapped(surface) {
114 let was_unmapped = self.niri.unmapped_layer_surfaces.remove(surface);
115
116 // Resolve rules for newly mapped layer surfaces.
117 if was_unmapped {
118 let config = self.niri.config.borrow();
119
120 let rules = &config.layer_rules;
121 let rules = ResolvedLayerRules::compute(rules, layer, self.niri.is_at_startup);
122
123 let output_size = output_size(&output);
124 let scale = output.current_scale().fractional_scale();
125
126 let hook = add_mapped_layer_pre_commit_hook(layer);
127 let mapped = MappedLayer::new(
128 layer.clone(),
129 hook,
130 rules,
131 output_size,
132 scale,
133 self.niri.clock.clone(),
134 &config,
135 );
136
137 let prev = self
138 .niri
139 .mapped_layer_surfaces
140 .insert(layer.clone(), mapped);
141 if prev.is_some() {
142 error!("MappedLayer was present for an unmapped surface");
143 }
144 } else {
145 // The surface remains mapped.
146 if let Some(mapped) = self.niri.mapped_layer_surfaces.get_mut(layer) {
147 // Check if the layer changed.
148 if mapped.take_recompute_rules_on_commit() {
149 let config = self.niri.config.borrow();
150 if mapped
151 .recompute_layer_rules(&config.layer_rules, self.niri.is_at_startup)
152 {
153 mapped.update_config(&config);
154 }
155 }
156 } else {
157 error!("MappedLayer missing for a mapped surface");
158 }
159 }
160
161 // Give focus to newly mapped on-demand surfaces. Some launchers like lxqt-runner rely
162 // on this behavior. While this behavior doesn't make much sense for other clients like
163 // panels, the consensus seems to be that it's not a big deal since panels generally
164 // only open once at the start of the session.
165 //
166 // Note that:
167 // 1) Exclusive layer surfaces already get focus automatically in
168 // update_keyboard_focus().
169 // 2) Same-layer exclusive layer surfaces are already preferred to on-demand surfaces in
170 // update_keyboard_focus(), so we don't need to check for that here.
171 //
172 // https://github.com/niri-wm/niri/issues/641
173 let on_demand = layer.cached_state().keyboard_interactivity
174 == wlr_layer::KeyboardInteractivity::OnDemand;
175 if was_unmapped && on_demand {
176 // I guess it'd make sense to check that no higher-layer on-demand surface
177 // has focus, but Smithay's Layer doesn't implement Ord so this would be a
178 // little annoying.
179 self.niri.layer_shell_on_demand_focus = Some(layer.clone());
180 }
181 } else {
182 // The surface is unmapped.
183 if self.niri.mapped_layer_surfaces.remove(layer).is_some() {
184 // A mapped surface got unmapped via a null commit. Now it needs to do a new
185 // initial commit again.
186 self.niri.unmapped_layer_surfaces.insert(surface.clone());
187 } else {
188 // An unmapped surface remains unmapped. If we haven't sent an initial configure
189 // yet, we should do so.
190 let initial_configure_sent = with_states(surface, |states| {
191 states
192 .data_map
193 .get::<LayerSurfaceData>()
194 .unwrap()
195 .lock()
196 .unwrap()
197 .initial_configure_sent
198 });
199 if !initial_configure_sent {
200 let scale = output.current_scale();
201 let transform = output.current_transform();
202 with_states(surface, |data| {
203 send_scale_transform(surface, data, scale, transform);
204 });
205
206 layer.layer_surface().send_configure();
207 }
208 // If we already sent an initial configure, then map.arrange() above had just sent
209 // it a new configure, if needed.
210 }
211 }
212
213 drop(map);
214
215 // This will call queue_redraw() inside.
216 self.niri.output_resized(&output);
217
218 true
219 }
220}
221
222fn add_mapped_layer_pre_commit_hook(layer: &LayerSurface) -> HookId {
223 add_pre_commit_hook::<State, _>(layer.wl_surface(), move |state, _dh, surface| {
224 let layer_changed = with_states(surface, |states| {
225 let mut guard = states.cached_state.get::<LayerSurfaceCachedState>();
226 let pending_layer = guard.pending().layer;
227 let current_layer = guard.current().layer;
228 pending_layer != current_layer
229 });
230
231 if layer_changed {
232 for mapped in state.niri.mapped_layer_surfaces.values_mut() {
233 if mapped.surface().wl_surface() == surface {
234 mapped.set_recompute_rules_on_commit();
235 break;
236 }
237 }
238 }
239 })
240}