diff --git a/Cargo.lock b/Cargo.lock index 029590c1..0af5da19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -544,6 +544,7 @@ checksum = "4dbf9978365bac10f54d1d4b04f7ce4427e51f71d61f2fe15e3fed5166474df7" dependencies = [ "async-task", "bitflags 2.11.0", + "futures-core", "polling", "rustix 1.1.4", "slab", diff --git a/Cargo.toml b/Cargo.toml index 9e74d6ea..ae7f67eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/src/dbus/mod.rs b/src/dbus/mod.rs index dcb7f257..f0f30d6d 100644 --- a/src/dbus/mod.rs +++ b/src/dbus/mod.rs @@ -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> { +pub fn init(evlh: &LoopHandle<'static, State>) -> Result> { 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::>() @@ -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"); diff --git a/src/state.rs b/src/state.rs index 935261ac..af0d1e2e 100644 --- a/src/state.rs +++ b/src/state.rs @@ -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"); }