From 5beebeb17067cb39a6f43c231d8f91f555509ff5 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Tue, 24 Jan 2023 23:28:30 -0800 Subject: [PATCH] cli: fall back to screensaver api for linux no-sleep (#172353) --- cli/src/tunnels/nosleep_linux.rs | 39 ++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/cli/src/tunnels/nosleep_linux.rs b/cli/src/tunnels/nosleep_linux.rs index ce0f5b83827..818910fee8e 100644 --- a/cli/src/tunnels/nosleep_linux.rs +++ b/cli/src/tunnels/nosleep_linux.rs @@ -28,6 +28,18 @@ trait PMInhibitor { fn inhibit(&self, what: &str, why: &str) -> zbus::Result; } +/// A slightly better documented version which seems commonly used. +#[dbus_proxy( + interface = "org.freedesktop.ScreenSaver", + gen_blocking = false, + default_service = "org.freedesktop.ScreenSaver", + default_path = "/org/freedesktop/ScreenSaver" +)] +trait ScreenSaver { + #[dbus_proxy(name = "Inhibit")] + fn inhibit(&self, what: &str, why: &str) -> zbus::Result; +} + pub struct SleepInhibitor { _connection: Connection, // Inhibition is released when the connection is closed } @@ -38,14 +50,27 @@ impl SleepInhibitor { .await .map_err(|e| wrap(e, "error creating dbus session"))?; - let proxy = PMInhibitorProxy::new(&connection) - .await - .map_err(|e| wrap(e, "error getting proxy"))?; + macro_rules! try_inhibit { + ($proxy:ident) => { + match $proxy::new(&connection).await { + Ok(proxy) => proxy.inhibit(APPLICATION_NAME, "running tunnel").await, + Err(e) => Err(e), + } + }; + } - proxy - .inhibit(APPLICATION_NAME, "running tunnel") - .await - .map_err(|e| wrap(e, "error requesting sleep inhibition"))?; + if let Err(e1) = try_inhibit!(PMInhibitorProxy) { + if let Err(e2) = try_inhibit!(ScreenSaverProxy) { + return Err(wrap( + e2, + format!( + "error requesting sleep inhibition, pminhibitor gave {}, screensaver gave", + e1 + ), + ) + .into()); + } + } Ok(SleepInhibitor { _connection: connection,