forge
Explore
Log in

Don't you dare go Hollow

Implement ext-session-lock

Browse files at this commit

b20d8e7062ce1cbbffdfa64a7b32d2393237ea26Ivan MolodetskikhParent: 3fd421f
src/handlers/compositor.rsmodified+12 -0
@@ -160,6 +160,18 @@ impl CompositorHandler for State {
160 // FIXME: granular redraws for cursors.160 // FIXME: granular redraws for cursors.
161 self.niri.queue_redraw_all();161 self.niri.queue_redraw_all();
162 }162 }
163+
164+ // This might be a lock surface.
165+ if self.niri.is_locked() {
166+ for (output, state) in &self.niri.output_state {
167+ if let Some(lock_surface) = &state.lock_surface {
168+ if lock_surface.wl_surface() == surface {
169+ self.niri.queue_redraw(output.clone());
170+ break;
171+ }
172+ }
173+ }
174+ }
163 }175 }
164 }176 }
165 177
src/handlers/mod.rsmodified+41 -2
@@ -13,10 +13,12 @@ use smithay::backend::renderer::ImportDma;
13 use smithay::desktop::PopupKind;13 use smithay::desktop::PopupKind;
14 use smithay::input::pointer::CursorImageStatus;14 use smithay::input::pointer::CursorImageStatus;
15 use smithay::input::{Seat, SeatHandler, SeatState};15 use smithay::input::{Seat, SeatHandler, SeatState};
16+use smithay::output::Output;
16 use smithay::reexports::wayland_server::protocol::wl_data_source::WlDataSource;17 use smithay::reexports::wayland_server::protocol::wl_data_source::WlDataSource;
18+use smithay::reexports::wayland_server::protocol::wl_output::WlOutput;
17 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;19 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
18 use smithay::reexports::wayland_server::Resource;20 use smithay::reexports::wayland_server::Resource;
19-use smithay::utils::{Logical, Rectangle};21+use smithay::utils::{Logical, Rectangle, Size};
20 use smithay::wayland::dmabuf::{DmabufGlobal, DmabufHandler, DmabufState, ImportError};22 use smithay::wayland::dmabuf::{DmabufGlobal, DmabufHandler, DmabufState, ImportError};
21 use smithay::wayland::input_method::{InputMethodHandler, PopupSurface};23 use smithay::wayland::input_method::{InputMethodHandler, PopupSurface};
22 use smithay::wayland::selection::data_device::{24 use smithay::wayland::selection::data_device::{
@@ -28,13 +30,17 @@ use smithay::wayland::selection::primary_selection::{
28 };30 };
29 use smithay::wayland::selection::wlr_data_control::{DataControlHandler, DataControlState};31 use smithay::wayland::selection::wlr_data_control::{DataControlHandler, DataControlState};
30 use smithay::wayland::selection::{SelectionHandler, SelectionTarget};32 use smithay::wayland::selection::{SelectionHandler, SelectionTarget};
33+use smithay::wayland::session_lock::{
34+ LockSurface, SessionLockHandler, SessionLockManagerState, SessionLocker,
35+};
31 use smithay::{36 use smithay::{
32 delegate_data_control, delegate_data_device, delegate_dmabuf, delegate_input_method_manager,37 delegate_data_control, delegate_data_device, delegate_dmabuf, delegate_input_method_manager,
33 delegate_output, delegate_pointer_gestures, delegate_presentation, delegate_primary_selection,38 delegate_output, delegate_pointer_gestures, delegate_presentation, delegate_primary_selection,
34- delegate_seat, delegate_tablet_manager, delegate_text_input_manager,39+ delegate_seat, delegate_session_lock, delegate_tablet_manager, delegate_text_input_manager,
35 delegate_virtual_keyboard_manager,40 delegate_virtual_keyboard_manager,
36 };41 };
37 42
43+use crate::layout::output_size;
38 use crate::niri::State;44 use crate::niri::State;
39 45
40 impl SeatHandler for State {46 impl SeatHandler for State {
@@ -174,3 +180,36 @@ impl DmabufHandler for State {
174 }180 }
175 }181 }
176 delegate_dmabuf!(State);182 delegate_dmabuf!(State);
183+
184+impl SessionLockHandler for State {
185+ fn lock_state(&mut self) -> &mut SessionLockManagerState {
186+ &mut self.niri.session_lock_state
187+ }
188+
189+ fn lock(&mut self, confirmation: SessionLocker) {
190+ self.niri.lock(confirmation);
191+ }
192+
193+ fn unlock(&mut self) {
194+ self.niri.unlock();
195+ }
196+
197+ fn new_surface(&mut self, surface: LockSurface, output: WlOutput) {
198+ let Some(output) = Output::from_resource(&output) else {
199+ error!("no Output matching WlOutput");
200+ return;
201+ };
202+
203+ configure_lock_surface(&surface, &output);
204+ self.niri.new_lock_surface(surface, &output);
205+ }
206+}
207+delegate_session_lock!(State);
208+
209+pub fn configure_lock_surface(surface: &LockSurface, output: &Output) {
210+ surface.with_pending_state(|states| {
211+ let size = output_size(output);
212+ states.size = Some(Size::from((size.w as u32, size.h as u32)));
213+ });
214+ surface.send_configure();
215+}
src/input.rsmodified+14 -1
@@ -136,7 +136,7 @@ impl State {
136 let serial = SERIAL_COUNTER.next_serial();136 let serial = SERIAL_COUNTER.next_serial();
137 let time = Event::time_msec(&event);137 let time = Event::time_msec(&event);
138 138
139- let action = self.niri.seat.get_keyboard().unwrap().input(139+ let mut action = self.niri.seat.get_keyboard().unwrap().input(
140 self,140 self,
141 event.key_code(),141 event.key_code(),
142 event.state(),142 event.state(),
@@ -152,6 +152,19 @@ impl State {
152 },152 },
153 );153 );
154 154
155+ // Filter actions when the session is locked.
156+ if self.niri.is_locked() {
157+ match action {
158+ Some(
159+ Action::Quit
160+ | Action::ChangeVt(_)
161+ | Action::Suspend
162+ | Action::PowerOffMonitors,
163+ ) => (),
164+ _ => action = None,
165+ }
166+ }
167+
155 if let Some(action) = action {168 if let Some(action) = action {
156 match action {169 match action {
157 Action::None => unreachable!(),170 Action::None => unreachable!(),
src/niri.rsmodified+289 -15
@@ -11,6 +11,7 @@ use _server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as
11 use anyhow::Context;11 use anyhow::Context;
12 use image::ImageFormat;12 use image::ImageFormat;
13 use smithay::backend::allocator::Fourcc;13 use smithay::backend::allocator::Fourcc;
14+use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement};
14 use smithay::backend::renderer::element::surface::{15 use smithay::backend::renderer::element::surface::{
15 render_elements_from_surface_tree, WaylandSurfaceRenderElement,16 render_elements_from_surface_tree, WaylandSurfaceRenderElement,
16 };17 };
@@ -25,7 +26,7 @@ use smithay::desktop::utils::{
25 bbox_from_surface_tree, output_update, send_dmabuf_feedback_surface_tree,26 bbox_from_surface_tree, output_update, send_dmabuf_feedback_surface_tree,
26 send_frames_surface_tree, surface_presentation_feedback_flags_from_states,27 send_frames_surface_tree, surface_presentation_feedback_flags_from_states,
27 surface_primary_scanout_output, take_presentation_feedback_surface_tree,28 surface_primary_scanout_output, take_presentation_feedback_surface_tree,
28- update_surface_primary_scanout_output, OutputPresentationFeedback,29+ under_from_surface_tree, update_surface_primary_scanout_output, OutputPresentationFeedback,
29 };30 };
30 use smithay::desktop::{layer_map_for_output, PopupManager, Space, Window, WindowSurfaceType};31 use smithay::desktop::{layer_map_for_output, PopupManager, Space, Window, WindowSurfaceType};
31 use smithay::input::keyboard::XkbConfig;32 use smithay::input::keyboard::XkbConfig;
@@ -62,6 +63,7 @@ use smithay::wayland::selection::primary_selection::{
62 set_primary_selection, PrimarySelectionState,63 set_primary_selection, PrimarySelectionState,
63 };64 };
64 use smithay::wayland::selection::wlr_data_control::DataControlState;65 use smithay::wayland::selection::wlr_data_control::DataControlState;
66+use smithay::wayland::session_lock::{LockSurface, SessionLockManagerState, SessionLocker};
65 use smithay::wayland::shell::kde::decoration::KdeDecorationState;67 use smithay::wayland::shell::kde::decoration::KdeDecorationState;
66 use smithay::wayland::shell::wlr_layer::{Layer, WlrLayerShellState};68 use smithay::wayland::shell::wlr_layer::{Layer, WlrLayerShellState};
67 use smithay::wayland::shell::xdg::decoration::XdgDecorationState;69 use smithay::wayland::shell::xdg::decoration::XdgDecorationState;
@@ -72,7 +74,7 @@ use smithay::wayland::tablet_manager::TabletManagerState;
72 use smithay::wayland::text_input::TextInputManagerState;74 use smithay::wayland::text_input::TextInputManagerState;
73 use smithay::wayland::virtual_keyboard::VirtualKeyboardManagerState;75 use smithay::wayland::virtual_keyboard::VirtualKeyboardManagerState;
74 76
75-use crate::backend::{Backend, Tty, Winit};77+use crate::backend::{Backend, RenderResult, Tty, Winit};
76 use crate::config::Config;78 use crate::config::Config;
77 use crate::cursor::Cursor;79 use crate::cursor::Cursor;
78 #[cfg(feature = "dbus")]80 #[cfg(feature = "dbus")]
@@ -80,11 +82,13 @@ use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri};
80 #[cfg(feature = "xdp-gnome-screencast")]82 #[cfg(feature = "xdp-gnome-screencast")]
81 use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri};83 use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri};
82 use crate::frame_clock::FrameClock;84 use crate::frame_clock::FrameClock;
85+use crate::handlers::configure_lock_surface;
83 use crate::layout::{output_size, Layout, MonitorRenderElement};86 use crate::layout::{output_size, Layout, MonitorRenderElement};
84 use crate::pw_utils::{Cast, PipeWire};87 use crate::pw_utils::{Cast, PipeWire};
85 use crate::utils::{center, get_monotonic_time, make_screenshot_path};88 use crate::utils::{center, get_monotonic_time, make_screenshot_path};
86 89
87 pub const CLEAR_COLOR: [f32; 4] = [0.2, 0.2, 0.2, 1.];90 pub const CLEAR_COLOR: [f32; 4] = [0.2, 0.2, 0.2, 1.];
91+pub const CLEAR_COLOR_LOCKED: [f32; 4] = [0.3, 0.1, 0.1, 1.];
88 92
89 pub struct Niri {93 pub struct Niri {
90 pub config: Rc<RefCell<Config>>,94 pub config: Rc<RefCell<Config>>,
@@ -117,6 +121,7 @@ pub struct Niri {
117 pub xdg_decoration_state: XdgDecorationState,121 pub xdg_decoration_state: XdgDecorationState,
118 pub kde_decoration_state: KdeDecorationState,122 pub kde_decoration_state: KdeDecorationState,
119 pub layer_shell_state: WlrLayerShellState,123 pub layer_shell_state: WlrLayerShellState,
124+ pub session_lock_state: SessionLockManagerState,
120 pub shm_state: ShmState,125 pub shm_state: ShmState,
121 pub output_manager_state: OutputManagerState,126 pub output_manager_state: OutputManagerState,
122 pub seat_state: SeatState<State>,127 pub seat_state: SeatState<State>,
@@ -138,6 +143,8 @@ pub struct Niri {
138 pub dnd_icon: Option<WlSurface>,143 pub dnd_icon: Option<WlSurface>,
139 pub pointer_focus: Option<PointerFocus>,144 pub pointer_focus: Option<PointerFocus>,
140 145
146+ pub lock_state: LockState,
147+
141 #[cfg(feature = "dbus")]148 #[cfg(feature = "dbus")]
142 pub dbus: Option<crate::dbus::DBusServers>,149 pub dbus: Option<crate::dbus::DBusServers>,
143 #[cfg(feature = "dbus")]150 #[cfg(feature = "dbus")]
@@ -163,6 +170,9 @@ pub struct OutputState {
163 /// If there are no commits, then we won't have a timer running, so the estimated sequence will170 /// If there are no commits, then we won't have a timer running, so the estimated sequence will
164 /// not increase.171 /// not increase.
165 pub current_estimated_sequence: Option<u32>,172 pub current_estimated_sequence: Option<u32>,
173+ pub lock_render_state: LockRenderState,
174+ pub lock_surface: Option<LockSurface>,
175+ pub lock_color_buffer: SolidColorBuffer,
166 }176 }
167 177
168 #[derive(Default)]178 #[derive(Default)]
@@ -186,6 +196,22 @@ pub struct PointerFocus {
186 pub surface: (WlSurface, Point<i32, Logical>),196 pub surface: (WlSurface, Point<i32, Logical>),
187 }197 }
188 198
199+#[derive(Default)]
200+pub enum LockState {
201+ #[default]
202+ Unlocked,
203+ Locking(SessionLocker),
204+ Locked,
205+}
206+
207+#[derive(PartialEq, Eq)]
208+pub enum LockRenderState {
209+ /// The output displays a normal session frame.
210+ Unlocked,
211+ /// The output displays a locked frame.
212+ Locked,
213+}
214+
189 // Not related to the one in Smithay.215 // Not related to the one in Smithay.
190 //216 //
191 // This state keeps track of when a surface last received a frame callback.217 // This state keeps track of when a surface last received a frame callback.
@@ -327,12 +353,17 @@ impl State {
327 }353 }
328 354
329 pub fn update_focus(&mut self) {355 pub fn update_focus(&mut self) {
330- let focus = self.niri.layer_surface_focus().or_else(|| {356+ let focus = if self.niri.is_locked() {
331- self.niri357+ self.niri.lock_surface_focus()
332- .layout358+ } else {
333- .focus()359+ self.niri.layer_surface_focus().or_else(|| {
334- .map(|win| win.toplevel().wl_surface().clone())360+ self.niri
335- });361+ .layout
362+ .focus()
363+ .map(|win| win.toplevel().wl_surface().clone())
364+ })
365+ };
366+
336 let keyboard = self.niri.seat.get_keyboard().unwrap();367 let keyboard = self.niri.seat.get_keyboard().unwrap();
337 if keyboard.current_focus() != focus {368 if keyboard.current_focus() != focus {
338 keyboard.set_focus(self, focus, SERIAL_COUNTER.next_serial());369 keyboard.set_focus(self, focus, SERIAL_COUNTER.next_serial());
@@ -492,6 +523,8 @@ impl Niri {
492 },523 },
493 );524 );
494 let layer_shell_state = WlrLayerShellState::new::<State>(&display_handle);525 let layer_shell_state = WlrLayerShellState::new::<State>(&display_handle);
526+ let session_lock_state =
527+ SessionLockManagerState::new::<State, _>(&display_handle, |_| true);
495 let shm_state = ShmState::new::<State>(&display_handle, vec![]);528 let shm_state = ShmState::new::<State>(&display_handle, vec![]);
496 let output_manager_state =529 let output_manager_state =
497 OutputManagerState::new_with_xdg_output::<State>(&display_handle);530 OutputManagerState::new_with_xdg_output::<State>(&display_handle);
@@ -584,6 +617,7 @@ impl Niri {
584 xdg_decoration_state,617 xdg_decoration_state,
585 kde_decoration_state,618 kde_decoration_state,
586 layer_shell_state,619 layer_shell_state,
620+ session_lock_state,
587 text_input_state,621 text_input_state,
588 input_method_state,622 input_method_state,
589 virtual_keyboard_state,623 virtual_keyboard_state,
@@ -604,6 +638,8 @@ impl Niri {
604 dnd_icon: None,638 dnd_icon: None,
605 pointer_focus: None,639 pointer_focus: None,
606 640
641+ lock_state: LockState::Unlocked,
642+
607 #[cfg(feature = "dbus")]643 #[cfg(feature = "dbus")]
608 dbus: None,644 dbus: None,
609 #[cfg(feature = "dbus")]645 #[cfg(feature = "dbus")]
@@ -701,12 +737,22 @@ impl Niri {
701 self.layout.add_output(output.clone());737 self.layout.add_output(output.clone());
702 output.change_current_state(None, None, None, Some(position));738 output.change_current_state(None, None, None, Some(position));
703 739
740+ let lock_render_state = if self.is_locked() {
741+ // We haven't rendered anything yet so it's as good as locked.
742+ LockRenderState::Locked
743+ } else {
744+ LockRenderState::Unlocked
745+ };
746+
704 let state = OutputState {747 let state = OutputState {
705 global,748 global,
706 redraw_state: RedrawState::Idle,749 redraw_state: RedrawState::Idle,
707 unfinished_animations_remain: false,750 unfinished_animations_remain: false,
708 frame_clock: FrameClock::new(refresh_interval),751 frame_clock: FrameClock::new(refresh_interval),
709 current_estimated_sequence: None,752 current_estimated_sequence: None,
753+ lock_render_state,
754+ lock_surface: None,
755+ lock_color_buffer: SolidColorBuffer::new(size, CLEAR_COLOR_LOCKED),
710 };756 };
711 let rv = self.output_state.insert(output.clone(), state);757 let rv = self.output_state.insert(output.clone(), state);
712 assert!(rv.is_none(), "output was already tracked");758 assert!(rv.is_none(), "output was already tracked");
@@ -749,11 +795,41 @@ impl Niri {
749 },795 },
750 )796 )
751 .unwrap();797 .unwrap();
798+
799+ match mem::take(&mut self.lock_state) {
800+ LockState::Locking(confirmation) => {
801+ // We're locking and an output was removed, check if the requirements are now met.
802+ let all_locked = self
803+ .output_state
804+ .values()
805+ .all(|state| state.lock_render_state == LockRenderState::Locked);
806+
807+ if all_locked {
808+ confirmation.lock();
809+ self.lock_state = LockState::Locked;
810+ } else {
811+ // Still waiting.
812+ self.lock_state = LockState::Locking(confirmation);
813+ }
814+ }
815+ lock_state => self.lock_state = lock_state,
816+ }
752 }817 }
753 818
754 pub fn output_resized(&mut self, output: Output) {819 pub fn output_resized(&mut self, output: Output) {
755 layer_map_for_output(&output).arrange();820 layer_map_for_output(&output).arrange();
756 self.layout.update_output_size(&output);821 self.layout.update_output_size(&output);
822+
823+ let is_locked = self.is_locked();
824+ if let Some(state) = self.output_state.get_mut(&output) {
825+ state.lock_color_buffer.resize(output_size(&output));
826+ if is_locked {
827+ if let Some(lock_surface) = &state.lock_surface {
828+ configure_lock_surface(lock_surface, &output);
829+ }
830+ }
831+ }
832+
757 self.queue_redraw(output);833 self.queue_redraw(output);
758 }834 }
759 835
@@ -791,6 +867,10 @@ impl Niri {
791 }867 }
792 868
793 pub fn window_under_cursor(&self) -> Option<&Window> {869 pub fn window_under_cursor(&self) -> Option<&Window> {
870+ if self.is_locked() {
871+ return None;
872+ }
873+
794 let pos = self.seat.get_pointer().unwrap().current_location();874 let pos = self.seat.get_pointer().unwrap().current_location();
795 let (output, pos_within_output) = self.output_under(pos)?;875 let (output, pos_within_output) = self.output_under(pos)?;
796 let (window, _loc) = self.layout.window_under(output, pos_within_output)?;876 let (window, _loc) = self.layout.window_under(output, pos_within_output)?;
@@ -809,6 +889,23 @@ impl Niri {
809 let (output, pos_within_output) = self.output_under(pos)?;889 let (output, pos_within_output) = self.output_under(pos)?;
810 let output = output.clone();890 let output = output.clone();
811 891
892+ if self.is_locked() {
893+ let state = self.output_state.get(&output)?;
894+ let surface = state.lock_surface.as_ref()?;
895+ // We put lock surfaces at (0, 0).
896+ let point = pos_within_output;
897+ let (surface, point) = under_from_surface_tree(
898+ surface.wl_surface(),
899+ point,
900+ (0, 0),
901+ WindowSurfaceType::ALL,
902+ )?;
903+ return Some(PointerFocus {
904+ output,
905+ surface: (surface, point),
906+ });
907+ }
908+
812 let (window, win_pos_within_output) =909 let (window, win_pos_within_output) =
813 self.layout.window_under(&output, pos_within_output)?;910 self.layout.window_under(&output, pos_within_output)?;
814 911
@@ -908,6 +1005,17 @@ impl Niri {
908 .or_else(|| self.global_space.outputs().next())1005 .or_else(|| self.global_space.outputs().next())
909 }1006 }
910 1007
1008+ fn lock_surface_focus(&self) -> Option<WlSurface> {
1009+ let output_under_cursor = self.output_under_cursor();
1010+ let output = output_under_cursor
1011+ .as_ref()
1012+ .or_else(|| self.layout.active_output())
1013+ .or_else(|| self.global_space.outputs().next())?;
1014+
1015+ let state = self.output_state.get(output)?;
1016+ state.lock_surface.as_ref().map(|s| s.wl_surface()).cloned()
1017+ }
1018+
911 fn layer_surface_focus(&self) -> Option<WlSurface> {1019 fn layer_surface_focus(&self) -> Option<WlSurface> {
912 let output = self.layout.active_output()?;1020 let output = self.layout.active_output()?;
913 let layers = layer_map_for_output(output);1021 let layers = layer_map_for_output(output);
@@ -1122,16 +1230,45 @@ impl Niri {
1122 1230
1123 let output_scale = Scale::from(output.current_scale().fractional_scale());1231 let output_scale = Scale::from(output.current_scale().fractional_scale());
1124 1232
1125- // Get monitor elements.
1126- let mon = self.layout.monitor_for_output(output).unwrap();
1127- let monitor_elements = mon.render_elements(renderer);
1128-
1129 // The pointer goes on the top.1233 // The pointer goes on the top.
1130 let mut elements = vec![];1234 let mut elements = vec![];
1131 if include_pointer {1235 if include_pointer {
1132 elements = self.pointer_element(renderer, output);1236 elements = self.pointer_element(renderer, output);
1133 }1237 }
1134 1238
1239+ // If the session is locked, draw the lock surface.
1240+ if self.is_locked() {
1241+ let state = self.output_state.get_mut(output).unwrap();
1242+ if let Some(surface) = state.lock_surface.as_ref() {
1243+ elements.extend(render_elements_from_surface_tree(
1244+ renderer,
1245+ surface.wl_surface(),
1246+ (0, 0),
1247+ output_scale,
1248+ 1.,
1249+ Kind::Unspecified,
1250+ ));
1251+ }
1252+
1253+ // Draw the solid color background.
1254+ elements.push(
1255+ SolidColorRenderElement::from_buffer(
1256+ &state.lock_color_buffer,
1257+ (0, 0),
1258+ output_scale,
1259+ 1.,
1260+ Kind::Unspecified,
1261+ )
1262+ .into(),
1263+ );
1264+
1265+ return elements;
1266+ }
1267+
1268+ // Get monitor elements.
1269+ let mon = self.layout.monitor_for_output(output).unwrap();
1270+ let monitor_elements = mon.render_elements(renderer);
1271+
1135 // Get layer-shell elements.1272 // Get layer-shell elements.
1136 let layer_map = layer_map_for_output(output);1273 let layer_map = layer_map_for_output(output);
1137 let mut extend_from_layer = |elements: &mut Vec<OutputRenderElements<GlesRenderer>>,1274 let mut extend_from_layer = |elements: &mut Vec<OutputRenderElements<GlesRenderer>>,
@@ -1175,13 +1312,17 @@ impl Niri {
1175 fn redraw(&mut self, backend: &mut Backend, output: &Output) {1312 fn redraw(&mut self, backend: &mut Backend, output: &Output) {
1176 let _span = tracy_client::span!("Niri::redraw");1313 let _span = tracy_client::span!("Niri::redraw");
1177 1314
1315+ let monitors_active = self.monitors_active;
1316+
1178 let state = self.output_state.get_mut(output).unwrap();1317 let state = self.output_state.get_mut(output).unwrap();
1179 assert!(matches!(1318 assert!(matches!(
1180 state.redraw_state,1319 state.redraw_state,
1181 RedrawState::Queued(_) | RedrawState::WaitingForEstimatedVBlankAndQueued(_)1320 RedrawState::Queued(_) | RedrawState::WaitingForEstimatedVBlankAndQueued(_)
1182 ));1321 ));
1183 1322
1323+ // FIXME: make this not cursed.
1184 let mut reset = || {1324 let mut reset = || {
1325+ let state = self.output_state.get_mut(output).unwrap();
1185 state.redraw_state =1326 state.redraw_state =
1186 if let RedrawState::WaitingForEstimatedVBlankAndQueued((token, _)) =1327 if let RedrawState::WaitingForEstimatedVBlankAndQueued((token, _)) =
1187 state.redraw_state1328 state.redraw_state
@@ -1189,10 +1330,17 @@ impl Niri {
1189 RedrawState::WaitingForEstimatedVBlank(token)1330 RedrawState::WaitingForEstimatedVBlank(token)
1190 } else {1331 } else {
1191 RedrawState::Idle1332 RedrawState::Idle
1192- }1333+ };
1334+
1335+ if matches!(self.lock_state, LockState::Locking { .. })
1336+ && state.lock_render_state == LockRenderState::Unlocked
1337+ {
1338+ // We needed to redraw this output for locking and failed.
1339+ self.unlock();
1340+ }
1193 };1341 };
1194 1342
1195- if !self.monitors_active {1343+ if !monitors_active {
1196 reset();1344 reset();
1197 return;1345 return;
1198 }1346 }
@@ -1207,6 +1355,7 @@ impl Niri {
1207 return;1355 return;
1208 };1356 };
1209 1357
1358+ let state = self.output_state.get_mut(output).unwrap();
1210 let presentation_time = state.frame_clock.next_presentation_time();1359 let presentation_time = state.frame_clock.next_presentation_time();
1211 1360
1212 // Update from the config and advance the animations.1361 // Update from the config and advance the animations.
@@ -1221,7 +1370,48 @@ impl Niri {
1221 let elements = self.render(renderer, output, true);1370 let elements = self.render(renderer, output, true);
1222 1371
1223 // Hand it over to the backend.1372 // Hand it over to the backend.
1224- backend.render(self, output, &elements, presentation_time);1373+ let res = backend.render(self, output, &elements, presentation_time);
1374+
1375+ // Update the lock render state on successful render.
1376+ let is_locked = self.is_locked();
1377+ let state = self.output_state.get_mut(output).unwrap();
1378+ if res != RenderResult::Error {
1379+ state.lock_render_state = if is_locked {
1380+ LockRenderState::Locked
1381+ } else {
1382+ LockRenderState::Unlocked
1383+ };
1384+ }
1385+
1386+ // If we're in process of locking the session, check if the requirements were met.
1387+ match mem::take(&mut self.lock_state) {
1388+ LockState::Locking(confirmation) => {
1389+ if res == RenderResult::Error {
1390+ if state.lock_render_state == LockRenderState::Unlocked {
1391+ // We needed to render a locked frame on this output but failed.
1392+ self.unlock();
1393+ } else {
1394+ // Rendering failed but this output is already locked, so it's fine.
1395+ self.lock_state = LockState::Locking(confirmation);
1396+ }
1397+ } else {
1398+ // Rendering succeeded, check if this was the last output.
1399+ let all_locked = self
1400+ .output_state
1401+ .values()
1402+ .all(|state| state.lock_render_state == LockRenderState::Locked);
1403+
1404+ if all_locked {
1405+ confirmation.lock();
1406+ self.lock_state = LockState::Locked;
1407+ } else {
1408+ // Still waiting.
1409+ self.lock_state = LockState::Locking(confirmation);
1410+ }
1411+ }
1412+ }
1413+ lock_state => self.lock_state = lock_state,
1414+ }
1225 1415
1226 // Send the frame callbacks.1416 // Send the frame callbacks.
1227 //1417 //
@@ -1324,6 +1514,24 @@ impl Niri {
1324 );1514 );
1325 });1515 });
1326 }1516 }
1517+
1518+ if let Some(surface) = &self.output_state[output].lock_surface {
1519+ with_surface_tree_downward(
1520+ surface.wl_surface(),
1521+ (),
1522+ |_, _, _| TraversalAction::DoChildren(()),
1523+ |surface, states, _| {
1524+ update_surface_primary_scanout_output(
1525+ surface,
1526+ output,
1527+ states,
1528+ render_element_states,
1529+ default_primary_scanout_output_compare,
1530+ );
1531+ },
1532+ |_, _, _| true,
1533+ );
1534+ }
1327 }1535 }
1328 1536
1329 pub fn send_dmabuf_feedbacks(&self, output: &Output, feedback: &DmabufFeedback) {1537 pub fn send_dmabuf_feedbacks(&self, output: &Output, feedback: &DmabufFeedback) {
@@ -1340,6 +1548,15 @@ impl Niri {
1340 surface.send_dmabuf_feedback(output, |_, _| Some(output.clone()), |_, _| feedback);1548 surface.send_dmabuf_feedback(output, |_, _| Some(output.clone()), |_, _| feedback);
1341 }1549 }
1342 1550
1551+ if let Some(surface) = &self.output_state[output].lock_surface {
1552+ send_dmabuf_feedback_surface_tree(
1553+ surface.wl_surface(),
1554+ output,
1555+ |_, _| Some(output.clone()),
1556+ |_, _| feedback,
1557+ );
1558+ }
1559+
1343 if let Some(surface) = &self.dnd_icon {1560 if let Some(surface) = &self.dnd_icon {
1344 send_dmabuf_feedback_surface_tree(1561 send_dmabuf_feedback_surface_tree(
1345 surface,1562 surface,
@@ -1411,6 +1628,16 @@ impl Niri {
1411 surface.send_frame(output, frame_callback_time, None, should_send);1628 surface.send_frame(output, frame_callback_time, None, should_send);
1412 }1629 }
1413 1630
1631+ if let Some(surface) = &self.output_state[output].lock_surface {
1632+ send_frames_surface_tree(
1633+ surface.wl_surface(),
1634+ output,
1635+ frame_callback_time,
1636+ None,
1637+ should_send,
1638+ );
1639+ }
1640+
1414 if let Some(surface) = &self.dnd_icon {1641 if let Some(surface) = &self.dnd_icon {
1415 send_frames_surface_tree(surface, output, frame_callback_time, None, should_send);1642 send_frames_surface_tree(surface, output, frame_callback_time, None, should_send);
1416 }1643 }
@@ -1469,6 +1696,17 @@ impl Niri {
1469 );1696 );
1470 }1697 }
1471 1698
1699+ if let Some(surface) = &self.output_state[output].lock_surface {
1700+ take_presentation_feedback_surface_tree(
1701+ surface.wl_surface(),
1702+ &mut feedback,
1703+ surface_primary_scanout_output,
1704+ |surface, _| {
1705+ surface_presentation_feedback_flags_from_states(surface, render_element_states)
1706+ },
1707+ );
1708+ }
1709+
1472 feedback1710 feedback
1473 }1711 }
1474 1712
@@ -1722,6 +1960,41 @@ impl Niri {
1722 1960
1723 Ok(())1961 Ok(())
1724 }1962 }
1963+
1964+ pub fn is_locked(&self) -> bool {
1965+ !matches!(self.lock_state, LockState::Unlocked)
1966+ }
1967+
1968+ pub fn lock(&mut self, confirmation: SessionLocker) {
1969+ info!("locking session");
1970+
1971+ self.lock_state = LockState::Locking(confirmation);
1972+ self.queue_redraw_all();
1973+ }
1974+
1975+ pub fn unlock(&mut self) {
1976+ info!("unlocking session");
1977+
1978+ self.lock_state = LockState::Unlocked;
1979+ for output_state in self.output_state.values_mut() {
1980+ output_state.lock_surface = None;
1981+ }
1982+ self.queue_redraw_all();
1983+ }
1984+
1985+ pub fn new_lock_surface(&mut self, surface: LockSurface, output: &Output) {
1986+ if !self.is_locked() {
1987+ error!("tried to add a lock surface on an unlocked session");
1988+ return;
1989+ }
1990+
1991+ let Some(output_state) = self.output_state.get_mut(output) else {
1992+ error!("missing output state");
1993+ return;
1994+ };
1995+
1996+ output_state.lock_surface = Some(surface);
1997+ }
1725 }1998 }
1726 1999
1727 render_elements! {2000 render_elements! {
@@ -1730,6 +2003,7 @@ render_elements! {
1730 Monitor = MonitorRenderElement<R>,2003 Monitor = MonitorRenderElement<R>,
1731 Wayland = WaylandSurfaceRenderElement<R>,2004 Wayland = WaylandSurfaceRenderElement<R>,
1732 DefaultPointer = TextureRenderElement<<R as Renderer>::TextureId>,2005 DefaultPointer = TextureRenderElement<<R as Renderer>::TextureId>,
2006+ SolidColor = SolidColorRenderElement,
1733 }2007 }
1734 2008
1735 #[derive(Default)]2009 #[derive(Default)]