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.
This commit is contained in:
Ian Douglas Scott 2026-03-05 15:56:06 -08:00 committed by Victoria Brekenfeld
parent c8d9ff1215
commit 4eaaf4f55c
9 changed files with 238 additions and 173 deletions

View file

@ -1,24 +1,34 @@
use std::os::fd::OwnedFd;
use anyhow::{Context, Result};
use logind_zbus::manager::{InhibitType::HandleLidSwitch, ManagerProxyBlocking};
use zbus::blocking::Connection;
use logind_zbus::manager::{InhibitType::HandleLidSwitch, ManagerProxy};
pub fn inhibit_lid() -> Result<OwnedFd> {
let conn = Connection::system()?;
let proxy = ManagerProxyBlocking::new(&conn)?;
let fd = proxy.inhibit(
HandleLidSwitch,
"cosmic-comp",
"External output connected",
"block",
)?;
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() -> Result<bool> {
let conn = Connection::system()?;
let proxy = ManagerProxyBlocking::new(&conn)?;
proxy.lid_closed().context("Failed to talk to logind")
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")
})
}