dbus: Use FutureSource instead of a channel

This commit is contained in:
Ian Douglas Scott 2026-02-13 12:48:48 -08:00 committed by Victoria Brekenfeld
parent 2f7c34f29a
commit 3c834e9c85
4 changed files with 15 additions and 31 deletions

1
Cargo.lock generated
View file

@ -544,6 +544,7 @@ checksum = "4dbf9978365bac10f54d1d4b04f7ce4427e51f71d61f2fe15e3fed5166474df7"
dependencies = [
"async-task",
"bitflags 2.11.0",
"futures-core",
"polling",
"rustix 1.1.4",
"slab",

View file

@ -12,7 +12,7 @@ members = ["cosmic-comp-config"]
[dependencies]
anyhow = { version = "1.0.102", features = ["backtrace"] }
bitflags = "2.11.0"
calloop = { version = "0.14.4", features = ["executor"] }
calloop = { version = "0.14.4", features = ["executor", "stream"] }
cosmic-comp-config = { path = "cosmic-comp-config", features = [
"libdisplay-info",
"output",

View file

@ -3,10 +3,9 @@ use crate::{
utils::prelude::OutputExt,
};
use anyhow::{Context, Result};
use calloop::{InsertError, LoopHandle, RegistrationToken};
use calloop::{InsertError, LoopHandle, RegistrationToken, stream::StreamSource};
use cosmic_comp_config::output::comp::OutputState;
use futures_executor::{ThreadPool, block_on};
use futures_util::stream::StreamExt;
use futures_executor::block_on;
use std::collections::HashMap;
use tracing::{error, warn};
use zbus::blocking::{Connection, fdo::DBusProxy};
@ -17,19 +16,15 @@ pub mod logind;
mod name_owners;
mod power;
pub fn init(
evlh: &LoopHandle<'static, State>,
executor: &ThreadPool,
) -> Result<Vec<RegistrationToken>> {
pub fn init(evlh: &LoopHandle<'static, State>) -> Result<Vec<RegistrationToken>> {
let mut tokens = Vec::new();
match block_on(power::init()) {
Ok(power_daemon) => {
let (tx, rx) = calloop::channel::channel();
let token = evlh
.insert_source(rx, |event, _, state| match event {
calloop::channel::Event::Msg(_) => {
if let Ok(stream) = block_on(power_daemon.receive_hot_plug_detect()) {
let source = StreamSource::new(stream).unwrap();
let token = evlh
.insert_source(source, |_, _, state| {
let nodes = match &mut state.backend {
BackendData::Kms(kms) => {
kms.drm_devices.keys().cloned().collect::<Vec<_>>()
@ -56,24 +51,12 @@ pub fn init(
}
}
}
}
calloop::channel::Event::Closed => (),
})
.map_err(|InsertError { error, .. }| error)
.with_context(|| "Failed to add channel to event_loop")?;
})
.map_err(|InsertError { error, .. }| error)
.with_context(|| "Failed to add channel to event_loop")?;
// start helper thread
executor.spawn_ok(async move {
if let Ok(mut msg_iter) = power_daemon.receive_hot_plug_detect().await {
while let Some(msg) = msg_iter.next().await {
if tx.send(msg).is_err() {
break;
}
}
}
});
tokens.push(token);
tokens.push(token);
}
}
Err(err) => {
tracing::info!(?err, "Failed to connect to com.system76.PowerDaemon");

View file

@ -729,7 +729,7 @@ impl State {
let async_executor = ThreadPool::builder().pool_size(1).create().unwrap();
if let Err(err) = crate::dbus::init(&handle, &async_executor) {
if let Err(err) = crate::dbus::init(&handle) {
tracing::warn!(?err, "Failed to initialize dbus handlers");
}