From 68ad9759bd63524258e30dfca799e614142996d2 Mon Sep 17 00:00:00 2001 From: VallierDev <24893073+VallierDev@users.noreply.github.com> Date: Tue, 20 Oct 2026 12:12:28 +0800 Subject: [PATCH] KDE idle inhibition during remote sessions; lock and X11-in-Wayland hints (#61) - KDE portal sessions hold an org.freedesktop.ScreenSaver inhibitor while a desktop connection is active (GNOME keeps SessionManager.Inhibit), so the idle timeout no longer locks mid-session. - Log a hint when the desktop locks: viewers only see black frames until it is unlocked. - Warn when the X11 backend is selected while a Wayland session is running (XWayland root capture is all black without any error). Co-Authored-By: Claude Opus 5.5 --- c_engine/desktop_power.c | 43 ++++++++++++++++++++++++++++++++++++---- src/desktop_backend.rs | 10 ++++++++++ src/desktop_power.rs | 24 +++++++++++++--------- src/main.rs | 4 ++++ src/session_lock.rs | 2 ++ 5 files changed, 70 insertions(+), 13 deletions(-) diff --git a/c_engine/desktop_power.c b/c_engine/desktop_power.c index 9f118af..c9e95db 100644 --- a/c_engine/desktop_power.c +++ b/c_engine/desktop_power.c @@ -7,6 +7,8 @@ #define SESSION_DEST "org.gnome.SessionManager" #define SESSION_PATH "/org/gnome/SessionManager" #define SESSION_IFACE "org.gnome.SessionManager" +#define SCREENSAVER_DEST "org.freedesktop.ScreenSaver" +#define SCREENSAVER_PATH "/org/freedesktop/ScreenSaver" enum { UU_POWER_OK = 0, @@ -17,6 +19,7 @@ enum { struct uu_idle_inhibitor { GDBusConnection *connection; guint32 cookie; + int freedesktop; /* org.freedesktop.ScreenSaver (KDE) instead of org.gnome.SessionManager */ }; struct setup_timeout { @@ -138,13 +141,45 @@ struct uu_idle_inhibitor *uu_desktop_idle_inhibitor_acquire(int *error_code) { return inhibitor; } +/* KDE (and other desktops implementing the freedesktop idle-inhibition spec): the inhibition is + also dropped by the screensaver when this private connection closes. */ +struct uu_idle_inhibitor *uu_desktop_screensaver_inhibitor_acquire(int *error_code) { + if (!error_code) return NULL; + *error_code = UU_POWER_OK; + GDBusConnection *connection = private_session_bus(error_code); + if (!connection) return NULL; + GError *error = NULL; + GVariant *reply = g_dbus_connection_call_sync( + connection, SCREENSAVER_DEST, SCREENSAVER_PATH, SCREENSAVER_DEST, "Inhibit", + g_variant_new("(ss)", "uu-remote-linux", "Remote desktop session is active"), + G_VARIANT_TYPE("(u)"), G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error); + g_clear_error(&error); + struct uu_idle_inhibitor *inhibitor = reply ? calloc(1, sizeof(*inhibitor)) : NULL; + if (!inhibitor) { + *error_code = UU_POWER_CALL_FAILED; + if (reply) g_variant_unref(reply); + close_private_bus(connection); + return NULL; + } + g_variant_get(reply, "(u)", &inhibitor->cookie); + g_variant_unref(reply); + inhibitor->connection = connection; + inhibitor->freedesktop = 1; + return inhibitor; +} + void uu_desktop_idle_inhibitor_release(struct uu_idle_inhibitor *inhibitor) { if (!inhibitor) return; GError *error = NULL; - GVariant *reply = g_dbus_connection_call_sync( - inhibitor->connection, SESSION_DEST, SESSION_PATH, SESSION_IFACE, "Uninhibit", - g_variant_new("(u)", inhibitor->cookie), G_VARIANT_TYPE("()"), - G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error); + GVariant *reply = inhibitor->freedesktop + ? g_dbus_connection_call_sync( + inhibitor->connection, SCREENSAVER_DEST, SCREENSAVER_PATH, SCREENSAVER_DEST, "UnInhibit", + g_variant_new("(u)", inhibitor->cookie), G_VARIANT_TYPE("()"), + G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error) + : g_dbus_connection_call_sync( + inhibitor->connection, SESSION_DEST, SESSION_PATH, SESSION_IFACE, "Uninhibit", + g_variant_new("(u)", inhibitor->cookie), G_VARIANT_TYPE("()"), + G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error); if (reply) g_variant_unref(reply); g_clear_error(&error); diff --git a/src/desktop_backend.rs b/src/desktop_backend.rs index c44bebe..3a66fd0 100644 --- a/src/desktop_backend.rs +++ b/src/desktop_backend.rs @@ -6,6 +6,16 @@ pub enum DesktopBackend { X11, Wayland, Portal, Kms } /// port mapping stay available (servers without a monitor or logged-in session). pub fn headless() -> bool { std::env::var("UU_DESKTOP_BACKEND").is_ok_and(|v| v == "none") } +/// A Wayland compositor socket exists for this user or the environment says so, even when this +/// process (e.g. a systemd unit with only DISPLAY set) would select X11. +pub fn wayland_session_running() -> bool { + std::env::var("XDG_SESSION_TYPE").is_ok_and(|v| v == "wayland") + || std::env::var("WAYLAND_DISPLAY").is_ok_and(|v| !v.is_empty()) + || std::env::var_os("XDG_RUNTIME_DIR").and_then(|dir| std::fs::read_dir(dir).ok()) + .is_some_and(|entries| entries.flatten().any(|e| e.file_name().to_str() + .is_some_and(|n| n.starts_with("wayland-") && !n.ends_with(".lock")))) +} + pub fn detect() -> Result { let backend=select(std::env::var("UU_DESKTOP_BACKEND").ok().as_deref(), std::env::var("XDG_SESSION_TYPE").ok().as_deref(), diff --git a/src/desktop_power.rs b/src/desktop_power.rs index f9f22db..ee753dd 100644 --- a/src/desktop_power.rs +++ b/src/desktop_power.rs @@ -8,6 +8,7 @@ mod native { fn uu_desktop_power_wake() -> i32; fn uu_desktop_idle_inhibitor_acquire(error: *mut i32) -> *mut c_void; fn uu_desktop_idle_inhibitor_release(inhibitor: *mut c_void); + fn uu_desktop_screensaver_inhibitor_acquire(error: *mut i32) -> *mut c_void; } pub struct Handle(NonNull); @@ -16,9 +17,9 @@ mod native { unsafe impl Send for Handle {} impl Handle { - pub fn acquire() -> Result { + pub fn acquire(gnome: bool) -> Result { let mut error = 0; - let pointer = unsafe { uu_desktop_idle_inhibitor_acquire(&mut error) }; + let pointer = unsafe { if gnome { uu_desktop_idle_inhibitor_acquire(&mut error) } else { uu_desktop_screensaver_inhibitor_acquire(&mut error) } }; NonNull::new(pointer).map(Self).ok_or_else(|| super::error("idle inhibition", error)) } @@ -50,7 +51,9 @@ pub fn wake() -> Result<(), String> { } } -/// Holds a GNOME SessionManager idle-only inhibitor until dropped. +/// Holds an idle-only inhibitor until dropped: GNOME SessionManager on GNOME, the freedesktop +/// ScreenSaver interface on KDE (remote input through the portal does not count as activity +/// there, so the session locked mid-session, #61). /// /// Acquire and drop perform blocking D-Bus work. Session-bus setup and each method call are /// bounded to three seconds. Callers on an async runtime should move both operations to a @@ -62,7 +65,8 @@ pub struct IdleInhibitor { impl IdleInhibitor { pub fn acquire() -> Result { - if !gnome_portal_active() { + let kde = portal_desktop_is("kde"); + if !gnome_portal_active() && !kde { return Ok(Self { #[cfg(all(target_os = "linux", feature = "portal"))] _handle: None, @@ -70,7 +74,7 @@ impl IdleInhibitor { } #[cfg(all(target_os = "linux", feature = "portal"))] { - native::Handle::acquire().map(|handle| Self { _handle: Some(handle) }) + native::Handle::acquire(!kde).map(|handle| Self { _handle: Some(handle) }) } #[cfg(not(all(target_os = "linux", feature = "portal")))] { @@ -79,10 +83,12 @@ impl IdleInhibitor { } } -fn gnome_portal_active() -> bool { - let gnome = std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default().split(':') - .any(|desktop| desktop.eq_ignore_ascii_case("gnome")); - gnome && crate::desktop_backend::detect().is_ok_and(|backend| backend == crate::desktop_backend::DesktopBackend::Portal) +fn gnome_portal_active() -> bool { portal_desktop_is("gnome") } + +fn portal_desktop_is(name: &str) -> bool { + let matches = std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default().split(':') + .any(|desktop| desktop.eq_ignore_ascii_case(name)); + matches && crate::desktop_backend::detect().is_ok_and(|backend| backend == crate::desktop_backend::DesktopBackend::Portal) } #[cfg(all(target_os = "linux", feature = "portal"))] diff --git a/src/main.rs b/src/main.rs index 99486a3..e0c4ffb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -410,6 +410,10 @@ async fn main() -> Result<(), Box> { let _audio_cleanup=virtual_audio::Cleanup; log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION")); + if !desktop_backend::headless() && desktop_backend::detect()? == desktop_backend::DesktopBackend::X11 && desktop_backend::wayland_session_running() { + // X11 capture inside a Wayland session reads XWayland's root window: all black, no error (#61). + log::warn!("A Wayland session is running but the X11 backend is selected: the picture will be black. Run inside the Wayland session (WAYLAND_DISPLAY set, UU_DESKTOP_BACKEND=auto) or log in to the Xorg session"); + } let capture = Arc::new((if desktop_backend::headless() { Err("headless mode (UU_DESKTOP_BACKEND=none)".to_owned()) } else if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal { diff --git a/src/session_lock.rs b/src/session_lock.rs index 4591ab3..40258a0 100644 --- a/src/session_lock.rs +++ b/src/session_lock.rs @@ -92,6 +92,8 @@ pub fn watch_locked() -> watch::Receiver> { if locked.is_none() { session = None; } // session ended or changed: look it up again sender.send_if_modified(|current| if *current != locked { log::info!("Desktop lock state: {}", match locked { Some(true) => "locked", Some(false) => "unlocked", None => "unknown" }); + // The viewer only sees black frames while locked and gives no hint (#61). + if locked == Some(true) { log::info!("Desktop is locked: viewers see a black screen until it is unlocked (viewer 自动解锁被控端, or `loginctl unlock-session` on the host)"); } *current = locked; true } else { false }); std::thread::sleep(Duration::from_secs(1));