cosmic-comp/src/dbus/logind.rs
Ian Douglas Scott 4eaaf4f55c dbus: Refactor to share DBus connections
We should avoid creating more than one session connection or more than
one system connection. We should also ideally avoid blocking the main
thread.

For now this still uses blocking in a couple places, but wrapping async
code (which is how `zbus::blocking` is implemented anyway).

This moves the `NameOwners` creation out of `A11yKeyboardMonitorState`,
so it can be shared with other things. We will likely want that for
https://github.com/pop-os/cosmic-comp/pull/465 to define a secured
protocol to pass an fd to the portal, and potentially any other
sensitive DBus protocols implemented by the compositor.
2026-06-08 19:03:50 +02:00

34 lines
936 B
Rust

use std::os::fd::OwnedFd;
use anyhow::{Context, Result};
use logind_zbus::manager::{InhibitType::HandleLidSwitch, ManagerProxy};
use crate::state::Common;
pub fn inhibit_lid(common: &Common) -> Result<OwnedFd> {
let fd = futures_executor::block_on(async {
let conn = common.dbus_state.system_conn().await?;
let manager = ManagerProxy::new(conn).await?;
manager
.inhibit(
HandleLidSwitch,
"cosmic-comp",
"External output connected",
"block",
)
.await
})?;
Ok(fd.into())
}
pub fn lid_closed(common: &Common) -> Result<bool> {
futures_executor::block_on(async {
let conn = common.dbus_state.system_conn().await?;
let manager = ManagerProxy::new(conn).await?;
manager
.lid_closed()
.await
.context("Failed to talk to logind")
})
}