diff --git a/libs/scrap/src/wayland/pipewire.rs b/libs/scrap/src/wayland/pipewire.rs index 53540c7a8..ec164c10e 100644 --- a/libs/scrap/src/wayland/pipewire.rs +++ b/libs/scrap/src/wayland/pipewire.rs @@ -1053,6 +1053,10 @@ const RESTORE_TOKEN: &str = "restore_token"; const RESTORE_TOKEN_CONF_KEY: &str = "wayland-restore-token"; const PIPEWIRE_DISPLAY_OFFSET_CONF_KEY: &str = "wayland-pipewire-display-offset"; +pub fn has_restore_token() -> bool { + !config::LocalConfig::get_option(RESTORE_TOKEN_CONF_KEY).is_empty() +} + pub fn get_available_cursor_modes() -> Result { let conn = SyncConnection::new_session()?; let portal = get_portal(&conn); diff --git a/src/server/connection.rs b/src/server/connection.rs index 3e25d08ad..ee1b4d103 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -2199,13 +2199,20 @@ impl Connection { self.last_supported_encoding = Some(supported_encoding.clone()); log::info!("peer info supported_encoding: {:?}", supported_encoding); pi.encoding = Some(supported_encoding).into(); - if let Some(msg_out) = super::display_service::is_inited_msg() { - self.send(msg_out).await; - } + let select_screen_msg = super::display_service::is_inited_msg(); try_activate_screen(); - match super::display_service::update_get_sync_displays_on_login().await { + let displays_on_login = match select_screen_msg { + Some(msg_out) => { + super::display_service::update_get_sync_displays_on_login_with_msg( + self.send(msg_out), + ) + .await + } + None => super::display_service::update_get_sync_displays_on_login().await, + }; + match displays_on_login { Err(err) => { res.set_error(format!("{}", err)); } diff --git a/src/server/display_service.rs b/src/server/display_service.rs index 2f6d26773..120f7445a 100644 --- a/src/server/display_service.rs +++ b/src/server/display_service.rs @@ -895,6 +895,38 @@ pub fn is_inited_msg() -> Option { None } +/// `update_get_sync_displays_on_login` for a login that owes the peer `is_inited_msg`'s message; +/// `send_msg` delivers it, at once or, on Wayland with a restore token saved, only if the refresh +/// is still waiting for the capture after `is_inited_msg_delay_ms`. +pub async fn update_get_sync_displays_on_login_with_msg( + send_msg: impl std::future::Future, +) -> ResultType<(Vec, usize)> { + #[cfg(target_os = "linux")] + let delay_ms = super::wayland::is_inited_msg_delay_ms(); + #[cfg(not(target_os = "linux"))] + let delay_ms = 0; + late_unless_done(update_get_sync_displays_on_login(), delay_ms, send_msg).await +} + +async fn late_unless_done( + work: impl std::future::Future, + delay_ms: u64, + late: impl std::future::Future, +) -> T { + if delay_ms == 0 { + late.await; + return work.await; + } + tokio::pin!(work); + match timeout(delay_ms, &mut work).await { + Ok(res) => res, + Err(_) => { + late.await; + work.await + } + } +} + // Return the primary index with the refreshed list so login cannot mix display snapshots. pub async fn update_get_sync_displays_on_login() -> ResultType<(Vec, usize)> { #[cfg(target_os = "linux")] @@ -1029,6 +1061,47 @@ pub fn try_get_displays_(add_amyuni_headless: bool) -> ResultType> Ok(displays) } +#[cfg(test)] +mod login_msg_tests { + use super::late_unless_done; + use hbb_common::tokio; + use std::sync::atomic::{AtomicBool, Ordering}; + + // The restore token case: the capture comes up without anyone selecting a screen, so the peer + // is not told to go and select one. + #[tokio::test] + async fn the_message_is_dropped_when_the_work_ends_within_the_delay() { + let sent = AtomicBool::new(false); + let res = late_unless_done(async { 7 }, 60_000, async { + sent.store(true, Ordering::SeqCst); + }) + .await; + assert_eq!(res, 7); + assert!(!sent.load(Ordering::SeqCst)); + } + + // The portal turned the token down and waits at its picker: the peer still gets the message. + #[tokio::test] + async fn the_message_goes_out_when_the_work_outlasts_the_delay() { + let (answer, picker) = tokio::sync::oneshot::channel::<()>(); + let sent = AtomicBool::new(false); + let res = late_unless_done( + async { + picker.await.ok(); + 7 + }, + 10, + async { + sent.store(true, Ordering::SeqCst); + answer.send(()).ok(); + }, + ) + .await; + assert_eq!(res, 7); + assert!(sent.load(Ordering::SeqCst)); + } +} + #[cfg(all(test, target_os = "linux", feature = "drm"))] mod input_map_tests { use super::run_uinput_apply; diff --git a/src/server/wayland.rs b/src/server/wayland.rs index 424e8a9ea..fb0417fc3 100644 --- a/src/server/wayland.rs +++ b/src/server/wayland.rs @@ -425,6 +425,21 @@ pub(super) fn is_inited() -> Option { } } +// With a restore token saved the portal normally answers without showing its picker, and +// `is_inited`'s message would send the peer to select a screen nobody is asked to select. The +// login holds the message back this long and drops it if the capture is up by then. A portal that +// turned the token down is still showing its picker, and the message goes out. +const SELECT_SCREEN_MSG_DELAY_MS: u64 = 3_000; + +pub(super) fn is_inited_msg_delay_ms() -> u64 { + // Only the server process hands the token to the portal, see scrap's `is_server_running`. + if crate::is_server() && scrap::wayland::pipewire::has_restore_token() { + SELECT_SCREEN_MSG_DELAY_MS + } else { + 0 + } +} + pub(super) async fn check_init() -> ResultType<()> { if !is_x11() { // try_read for the same reason as is_inited; when busy, the same look waits off the worker.