feat: prevent system sleep during playback via XDG portal

Add idle_inhibit module using ashpd's Inhibit portal to prevent
screen blanking and system suspend while audio is playing.

- Acquires Idle+Suspend inhibit when playback starts
- Releases when paused, stopped, or track cleared
- Works on GNOME, KDE, Sway, XFCE
- Works inside Flatpak/Snap (no extra permissions needed)
- Graceful fallback if portal unavailable

Addresses #229

cc
This commit is contained in:
victorrhgap committed 2026-03-31 12:41:21 -06:00
1 parent 2900fb882c
commit 080b916c64
4 files changed
+85 -40

No files matched your search

+5 -39
View File
@@ -230,6 +230,7 @@ dependencies = [
"futures-util",
"getrandom 0.4.2",
"serde",
"serde_repr",
"tokio",
"zbus",
]
@@ -3155,21 +3156,6 @@ dependencies = [
"scopeguard",
]
[[package]]
name = "lofty"
version = "0.18.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f75066eb1d25a7047fb2667edb410ae2592439ed81546f95c28b0a1c7d7d3818"
dependencies = [
"byteorder",
"data-encoding",
"flate2",
"lofty_attr 0.9.0",
"log",
"ogg_pager 0.6.1",
"paste",
]
[[package]]
name = "lofty"
version = "0.23.3"
@@ -3179,23 +3165,12 @@ dependencies = [
"byteorder",
"data-encoding",
"flate2",
"lofty_attr 0.12.0",
"lofty_attr",
"log",
"ogg_pager 0.7.1",
"ogg_pager",
"paste",
]
[[package]]
name = "lofty_attr"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "764b60e1ddd07e5665a6a17636a95cd7d8f3b86c73503a69c32979d05f72f3cf"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.117",
]
[[package]]
name = "lofty_attr"
version = "0.12.0"
@@ -3805,15 +3780,6 @@ dependencies = [
"objc2-foundation",
]
[[package]]
name = "ogg_pager"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87b0bef808533c5890ab77279538212efdbbbd9aa4ef1ccdfcfbf77a42f7e6fa"
dependencies = [
"byteorder",
]
[[package]]
name = "ogg_pager"
version = "0.7.1"
@@ -4606,7 +4572,7 @@ dependencies = [
"dirs 5.0.1",
"glob",
"image",
"lofty 0.18.2",
"lofty",
"log",
"rusqlite",
"serde",
@@ -4651,7 +4617,7 @@ dependencies = [
"image",
"keyring",
"lazy_static",
"lofty 0.23.3",
"lofty",
"log",
"md-5",
"mupdf",
+1 -1
View File
@@ -145,7 +145,7 @@ futures-util = "0.3"
# Platform-specific dependencies
[target.'cfg(target_os = "linux")'.dependencies]
tauri-plugin-dialog = { version = "2.6.0" }
ashpd = { version = "0.13", default-features = false, features = ["tokio", "notification"] }
ashpd = { version = "0.13", default-features = false, features = ["tokio", "notification", "inhibit"] }
# Direct ALSA access for bit-perfect playback (bypasses CPAL limitations)
alsa = "0.9"
+66
View File
@@ -0,0 +1,66 @@
//! Idle/sleep inhibitor via XDG Desktop Portal.
//!
//! Prevents screen blanking and system suspend while audio is playing.
//! Works on GNOME, KDE, Sway, XFCE — and inside Flatpak/Snap sandboxes.
use ashpd::desktop::inhibit::{InhibitFlags, InhibitOptions, InhibitProxy};
use ashpd::desktop::Request;
use std::sync::Arc;
use tokio::sync::Mutex;
/// Manages the XDG portal idle inhibit lifecycle.
pub struct IdleInhibitor {
handle: Arc<Mutex<Option<Request<()>>>>,
}
impl IdleInhibitor {
pub fn new() -> Self {
Self {
handle: Arc::new(Mutex::new(None)),
}
}
/// Acquire idle+suspend inhibit. No-op if already held.
pub async fn inhibit(&self) {
let mut guard = self.handle.lock().await;
if guard.is_some() {
return;
}
match try_inhibit().await {
Ok(request) => {
log::info!("[IdleInhibit] Acquired idle+suspend inhibit via XDG portal");
*guard = Some(request);
}
Err(e) => {
log::debug!("[IdleInhibit] Could not acquire inhibit: {e}");
}
}
}
/// Release the inhibit. No-op if not held.
pub async fn uninhibit(&self) {
let mut guard = self.handle.lock().await;
if guard.take().is_some() {
log::info!("[IdleInhibit] Released idle+suspend inhibit");
}
}
}
async fn try_inhibit() -> Result<Request<()>, ashpd::Error> {
let proxy = InhibitProxy::new().await?;
let request = proxy
.inhibit(
None,
InhibitFlags::Idle | InhibitFlags::Suspend,
InhibitOptions::default(),
)
.await?;
Ok(request)
}
impl Default for IdleInhibitor {
fn default() -> Self {
Self::new()
}
}
+13
View File
@@ -27,6 +27,7 @@ pub mod config;
pub mod credentials;
pub mod discogs;
pub mod flatpak;
pub mod idle_inhibit;
pub mod image_cache;
pub mod lastfm;
pub mod library;
@@ -83,6 +84,7 @@ pub struct AppState {
pub lastfm: Arc<Mutex<LastFmClient>>,
pub songlink: SongLinkClient,
pub visualizer: Visualizer,
pub idle_inhibitor: idle_inhibit::IdleInhibitor,
}
impl AppState {
@@ -139,6 +141,7 @@ impl AppState {
lastfm: Arc::new(Mutex::new(LastFmClient::default())),
songlink: SongLinkClient::new(),
visualizer,
idle_inhibitor: idle_inhibit::IdleInhibitor::new(),
}
}
}
@@ -1013,6 +1016,16 @@ pub fn run() {
}
}
// Idle inhibit: prevent screen/suspend while playing
if is_playing != last_is_playing || track_cleared {
let inhibitor = &app_handle.state::<AppState>().idle_inhibitor;
if is_playing {
inhibitor.inhibit().await;
} else {
inhibitor.uninhibit().await;
}
}
// Adaptive polling:
// - fast (250ms) when playing - improves seekbar/lyrics sync
// - slow (1000ms) when paused/stopped with a track loaded