forge
Explore
Log in

Don't you dare go Hollow

Wait for lock surfaces before locking

Browse files at this commit

4f16be9e4d3b13925f5fb5368f4e24cba50227c6Ivan MolodetskikhParent: 0f30306
Fixes the red flash when locking.
src/handlers/compositor.rsmodified+13 -6
@@ -21,7 +21,7 @@ use smithay::{delegate_compositor, delegate_shm};
2121 use super::xdg_shell::add_mapped_toplevel_pre_commit_hook;
2222 use crate::handlers::XDG_ACTIVATION_TOKEN_TIMEOUT;
2323 use crate::layout::{ActivateWindow, AddWindowTarget};
24-use crate::niri::{ClientState, State};
24+use crate::niri::{ClientState, LockState, State};
2525 use crate::utils::transaction::Transaction;
2626 use crate::utils::{is_mapped, send_scale_transform};
2727 use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped};
@@ -408,16 +408,23 @@ impl CompositorHandler for State {
408408 }
409409
410410 // This might be a lock surface.
411- if self.niri.is_locked() {
412- for (output, state) in &self.niri.output_state {
413- if let Some(lock_surface) = &state.lock_surface {
414- if lock_surface.wl_surface() == &root_surface {
411+ for (output, state) in &self.niri.output_state {
412+ if let Some(lock_surface) = &state.lock_surface {
413+ if lock_surface.wl_surface() == &root_surface {
414+ if matches!(self.niri.lock_state, LockState::WaitingForSurfaces { .. }) {
415+ self.niri.maybe_continue_to_locking();
416+ } else {
415417 self.niri.queue_redraw(&output.clone());
416- return;
417418 }
419+
420+ return;
418421 }
419422 }
420423 }
424+
425+ // This message can trigger for lock surfaces that had a commit right after we unlocked
426+ // the session, but that's ok, we don't need to handle them.
427+ trace!("commit on an unrecognized surface: {surface:?}, root: {root_surface:?}");
421428 }
422429
423430 fn destroyed(&mut self, surface: &WlSurface) {
src/niri.rsmodified+112 -20
@@ -155,8 +155,9 @@ use crate::ui::screenshot_ui::{OutputScreenshot, ScreenshotUi, ScreenshotUiRende
155155 use crate::utils::scale::{closest_representable_scale, guess_monitor_scale};
156156 use crate::utils::spawning::CHILD_ENV;
157157 use crate::utils::{
158- center, center_f64, expand_home, get_monotonic_time, ipc_transform_to_smithay, logical_output,
159- make_screenshot_path, output_matches_name, output_size, send_scale_transform, write_png_rgba8,
158+ center, center_f64, expand_home, get_monotonic_time, ipc_transform_to_smithay, is_mapped,
159+ logical_output, make_screenshot_path, output_matches_name, output_size, send_scale_transform,
160+ write_png_rgba8,
160161 };
161162 use crate::window::mapped::MappedId;
162163 use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef};
@@ -474,10 +475,14 @@ pub struct PointContents {
474475 pub layer: Option<LayerSurface>,
475476 }
476477
477-#[derive(Default)]
478+#[derive(Debug, Default)]
478479 pub enum LockState {
479480 #[default]
480481 Unlocked,
482+ WaitingForSurfaces {
483+ confirmation: SessionLocker,
484+ deadline_token: RegistrationToken,
485+ },
481486 Locking(SessionLocker),
482487 Locked(ExtSessionLockV1),
483488 }
@@ -2505,7 +2510,10 @@ impl Niri {
25052510 self.lock_state = LockState::Locking(confirmation);
25062511 }
25072512 }
2508- lock_state => self.lock_state = lock_state,
2513+ lock_state => {
2514+ self.lock_state = lock_state;
2515+ self.maybe_continue_to_locking();
2516+ }
25092517 }
25102518
25112519 if self.screenshot_ui.close() {
@@ -2519,7 +2527,6 @@ impl Niri {
25192527 let output_size = output_size(output).to_i32_round();
25202528 let scale = output.current_scale();
25212529 let transform = output.current_transform();
2522- let is_locked = self.is_locked();
25232530
25242531 {
25252532 let mut layer_map = layer_map_for_output(output);
@@ -2537,10 +2544,8 @@ impl Niri {
25372544 state.background_buffer.resize(output_size);
25382545
25392546 state.lock_color_buffer.resize(output_size);
2540- if is_locked {
2541- if let Some(lock_surface) = &state.lock_surface {
2542- configure_lock_surface(lock_surface, output);
2543- }
2547+ if let Some(lock_surface) = &state.lock_surface {
2548+ configure_lock_surface(lock_surface, output);
25442549 }
25452550 }
25462551
@@ -4945,12 +4950,18 @@ impl Niri {
49454950 }
49464951
49474952 pub fn is_locked(&self) -> bool {
4948- !matches!(self.lock_state, LockState::Unlocked)
4953+ match self.lock_state {
4954+ LockState::Unlocked | LockState::WaitingForSurfaces { .. } => false,
4955+ LockState::Locking(_) | LockState::Locked(_) => true,
4956+ }
49494957 }
49504958
49514959 pub fn lock(&mut self, confirmation: SessionLocker) {
49524960 // Check if another client is in the process of locking.
4953- if matches!(self.lock_state, LockState::Locking(_)) {
4961+ if matches!(
4962+ self.lock_state,
4963+ LockState::WaitingForSurfaces { .. } | LockState::Locking(_)
4964+ ) {
49544965 info!("refusing lock as another client is currently locking");
49554966 return;
49564967 }
@@ -4963,30 +4974,111 @@ impl Niri {
49634974 }
49644975
49654976 // If the client had died, continue with the new lock.
4977+ info!("locking session (replacing existing dead lock)");
4978+
4979+ // Since the session was already locked, we know that the outputs are blanked, and
4980+ // can lock right away.
4981+ let lock = confirmation.ext_session_lock().clone();
4982+ confirmation.lock();
4983+ self.lock_state = LockState::Locked(lock);
4984+
4985+ return;
49664986 }
49674987
49684988 info!("locking session");
49694989
4970- self.screenshot_ui.close();
4971- self.cursor_manager
4972- .set_cursor_image(CursorImageStatus::default_named());
4973-
49744990 if self.output_state.is_empty() {
49754991 // There are no outputs, lock the session right away.
4992+ self.screenshot_ui.close();
4993+ self.cursor_manager
4994+ .set_cursor_image(CursorImageStatus::default_named());
4995+
49764996 let lock = confirmation.ext_session_lock().clone();
49774997 confirmation.lock();
49784998 self.lock_state = LockState::Locked(lock);
49794999 } else {
4980- // There are outputs, which we need to redraw before locking.
4981- self.lock_state = LockState::Locking(confirmation);
4982- self.queue_redraw_all();
5000+ // There are outputs which we need to redraw before locking. But before we do that,
5001+ // let's wait for the lock surfaces.
5002+ //
5003+ // Give them a second; swaylock can take its time to paint a big enough image.
5004+ let timer = Timer::from_duration(Duration::from_millis(1000));
5005+ let deadline_token = self
5006+ .event_loop
5007+ .insert_source(timer, |_, _, state| {
5008+ trace!("lock deadline expired, continuing");
5009+ state.niri.continue_to_locking();
5010+ TimeoutAction::Drop
5011+ })
5012+ .unwrap();
5013+
5014+ self.lock_state = LockState::WaitingForSurfaces {
5015+ confirmation,
5016+ deadline_token,
5017+ };
5018+ }
5019+ }
5020+
5021+ pub fn maybe_continue_to_locking(&mut self) {
5022+ if !matches!(self.lock_state, LockState::WaitingForSurfaces { .. }) {
5023+ // Not waiting.
5024+ return;
5025+ }
5026+
5027+ // Check if there are any outputs whose lock surfaces had not had a commit yet.
5028+ for state in self.output_state.values() {
5029+ let Some(surface) = &state.lock_surface else {
5030+ // Surface not created yet.
5031+ return;
5032+ };
5033+
5034+ if !is_mapped(surface.wl_surface()) {
5035+ return;
5036+ }
5037+ }
5038+
5039+ // All good.
5040+ trace!("lock surfaces are ready, continuing");
5041+ self.continue_to_locking();
5042+ }
5043+
5044+ fn continue_to_locking(&mut self) {
5045+ match mem::take(&mut self.lock_state) {
5046+ LockState::WaitingForSurfaces {
5047+ confirmation,
5048+ deadline_token,
5049+ } => {
5050+ self.event_loop.remove(deadline_token);
5051+
5052+ self.screenshot_ui.close();
5053+ self.cursor_manager
5054+ .set_cursor_image(CursorImageStatus::default_named());
5055+
5056+ if self.output_state.is_empty() {
5057+ // There are no outputs, lock the session right away.
5058+ let lock = confirmation.ext_session_lock().clone();
5059+ confirmation.lock();
5060+ self.lock_state = LockState::Locked(lock);
5061+ } else {
5062+ // There are outputs which we need to redraw before locking.
5063+ self.lock_state = LockState::Locking(confirmation);
5064+ self.queue_redraw_all();
5065+ }
5066+ }
5067+ other => {
5068+ error!("continue_to_locking() called with wrong lock state: {other:?}",);
5069+ self.lock_state = other;
5070+ }
49835071 }
49845072 }
49855073
49865074 pub fn unlock(&mut self) {
49875075 info!("unlocking session");
49885076
4989- self.lock_state = LockState::Unlocked;
5077+ let prev = mem::take(&mut self.lock_state);
5078+ if let LockState::WaitingForSurfaces { deadline_token, .. } = prev {
5079+ self.event_loop.remove(deadline_token);
5080+ }
5081+
49905082 for output_state in self.output_state.values_mut() {
49915083 output_state.lock_surface = None;
49925084 }
@@ -4994,7 +5086,7 @@ impl Niri {
49945086 }
49955087
49965088 pub fn new_lock_surface(&mut self, surface: LockSurface, output: &Output) {
4997- if !self.is_locked() {
5089+ if matches!(self.lock_state, LockState::Unlocked) {
49985090 error!("tried to add a lock surface on an unlocked session");
49995091 return;
50005092 }