Wait for lock surfaces before locking
Browse files at this commit4f16be9e4d3b13925f5fb5368f4e24cba50227c6Parent: 0f303062 changed files +125 -26
- src/handlers/compositor.rs +13 -6
- src/niri.rs +112 -20
src/handlers/compositor.rsmodified+13 -6
| @@ -21,7 +21,7 @@ use smithay::{delegate_compositor, delegate_shm}; | ||
| 21 | 21 | use super::xdg_shell::add_mapped_toplevel_pre_commit_hook; |
| 22 | 22 | use crate::handlers::XDG_ACTIVATION_TOKEN_TIMEOUT; |
| 23 | 23 | use crate::layout::{ActivateWindow, AddWindowTarget}; |
| 24 | -use crate::niri::{ClientState, State}; | |
| 24 | +use crate::niri::{ClientState, LockState, State}; | |
| 25 | 25 | use crate::utils::transaction::Transaction; |
| 26 | 26 | use crate::utils::{is_mapped, send_scale_transform}; |
| 27 | 27 | use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped}; |
| @@ -408,16 +408,23 @@ impl CompositorHandler for State { | ||
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | // 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 { | |
| 415 | 417 | self.niri.queue_redraw(&output.clone()); |
| 416 | - return; | |
| 417 | 418 | } |
| 419 | + | |
| 420 | + return; | |
| 418 | 421 | } |
| 419 | 422 | } |
| 420 | 423 | } |
| 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:?}"); | |
| 421 | 428 | } |
| 422 | 429 | |
| 423 | 430 | fn destroyed(&mut self, surface: &WlSurface) { |
src/niri.rsmodified+112 -20
| @@ -155,8 +155,9 @@ use crate::ui::screenshot_ui::{OutputScreenshot, ScreenshotUi, ScreenshotUiRende | ||
| 155 | 155 | use crate::utils::scale::{closest_representable_scale, guess_monitor_scale}; |
| 156 | 156 | use crate::utils::spawning::CHILD_ENV; |
| 157 | 157 | 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, | |
| 160 | 161 | }; |
| 161 | 162 | use crate::window::mapped::MappedId; |
| 162 | 163 | use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef}; |
| @@ -474,10 +475,14 @@ pub struct PointContents { | ||
| 474 | 475 | pub layer: Option<LayerSurface>, |
| 475 | 476 | } |
| 476 | 477 | |
| 477 | -#[derive(Default)] | |
| 478 | +#[derive(Debug, Default)] | |
| 478 | 479 | pub enum LockState { |
| 479 | 480 | #[default] |
| 480 | 481 | Unlocked, |
| 482 | + WaitingForSurfaces { | |
| 483 | + confirmation: SessionLocker, | |
| 484 | + deadline_token: RegistrationToken, | |
| 485 | + }, | |
| 481 | 486 | Locking(SessionLocker), |
| 482 | 487 | Locked(ExtSessionLockV1), |
| 483 | 488 | } |
| @@ -2505,7 +2510,10 @@ impl Niri { | ||
| 2505 | 2510 | self.lock_state = LockState::Locking(confirmation); |
| 2506 | 2511 | } |
| 2507 | 2512 | } |
| 2508 | - lock_state => self.lock_state = lock_state, | |
| 2513 | + lock_state => { | |
| 2514 | + self.lock_state = lock_state; | |
| 2515 | + self.maybe_continue_to_locking(); | |
| 2516 | + } | |
| 2509 | 2517 | } |
| 2510 | 2518 | |
| 2511 | 2519 | if self.screenshot_ui.close() { |
| @@ -2519,7 +2527,6 @@ impl Niri { | ||
| 2519 | 2527 | let output_size = output_size(output).to_i32_round(); |
| 2520 | 2528 | let scale = output.current_scale(); |
| 2521 | 2529 | let transform = output.current_transform(); |
| 2522 | - let is_locked = self.is_locked(); | |
| 2523 | 2530 | |
| 2524 | 2531 | { |
| 2525 | 2532 | let mut layer_map = layer_map_for_output(output); |
| @@ -2537,10 +2544,8 @@ impl Niri { | ||
| 2537 | 2544 | state.background_buffer.resize(output_size); |
| 2538 | 2545 | |
| 2539 | 2546 | 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); | |
| 2544 | 2549 | } |
| 2545 | 2550 | } |
| 2546 | 2551 | |
| @@ -4945,12 +4950,18 @@ impl Niri { | ||
| 4945 | 4950 | } |
| 4946 | 4951 | |
| 4947 | 4952 | 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 | + } | |
| 4949 | 4957 | } |
| 4950 | 4958 | |
| 4951 | 4959 | pub fn lock(&mut self, confirmation: SessionLocker) { |
| 4952 | 4960 | // 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 | + ) { | |
| 4954 | 4965 | info!("refusing lock as another client is currently locking"); |
| 4955 | 4966 | return; |
| 4956 | 4967 | } |
| @@ -4963,30 +4974,111 @@ impl Niri { | ||
| 4963 | 4974 | } |
| 4964 | 4975 | |
| 4965 | 4976 | // 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; | |
| 4966 | 4986 | } |
| 4967 | 4987 | |
| 4968 | 4988 | info!("locking session"); |
| 4969 | 4989 | |
| 4970 | - self.screenshot_ui.close(); | |
| 4971 | - self.cursor_manager | |
| 4972 | - .set_cursor_image(CursorImageStatus::default_named()); | |
| 4973 | - | |
| 4974 | 4990 | if self.output_state.is_empty() { |
| 4975 | 4991 | // 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 | + | |
| 4976 | 4996 | let lock = confirmation.ext_session_lock().clone(); |
| 4977 | 4997 | confirmation.lock(); |
| 4978 | 4998 | self.lock_state = LockState::Locked(lock); |
| 4979 | 4999 | } 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 | + } | |
| 4983 | 5071 | } |
| 4984 | 5072 | } |
| 4985 | 5073 | |
| 4986 | 5074 | pub fn unlock(&mut self) { |
| 4987 | 5075 | info!("unlocking session"); |
| 4988 | 5076 | |
| 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 | + | |
| 4990 | 5082 | for output_state in self.output_state.values_mut() { |
| 4991 | 5083 | output_state.lock_surface = None; |
| 4992 | 5084 | } |
| @@ -4994,7 +5086,7 @@ impl Niri { | ||
| 4994 | 5086 | } |
| 4995 | 5087 | |
| 4996 | 5088 | pub fn new_lock_surface(&mut self, surface: LockSurface, output: &Output) { |
| 4997 | - if !self.is_locked() { | |
| 5089 | + if matches!(self.lock_state, LockState::Unlocked) { | |
| 4998 | 5090 | error!("tried to add a lock surface on an unlocked session"); |
| 4999 | 5091 | return; |
| 5000 | 5092 | } |