@@ -5,6 +5,8 @@ use crate::{
55use anyhow:: { Context , Result } ;
66use calloop:: { InsertError , LoopHandle , RegistrationToken } ;
77use cosmic_comp_config:: output:: comp:: OutputState ;
8+ use futures_executor:: { block_on, ThreadPool } ;
9+ use futures_util:: stream:: StreamExt ;
810use std:: collections:: HashMap ;
911use tracing:: { error, warn} ;
1012use zbus:: blocking:: { fdo:: DBusProxy , Connection } ;
@@ -13,10 +15,13 @@ use zbus::blocking::{fdo::DBusProxy, Connection};
1315pub mod logind;
1416mod power;
1517
16- pub fn init ( evlh : & LoopHandle < ' static , State > ) -> Result < Vec < RegistrationToken > > {
18+ pub fn init (
19+ evlh : & LoopHandle < ' static , State > ,
20+ executor : & ThreadPool ,
21+ ) -> Result < Vec < RegistrationToken > > {
1722 let mut tokens = Vec :: new ( ) ;
1823
19- match power:: init ( ) {
24+ match block_on ( power:: init ( ) ) {
2025 Ok ( power_daemon) => {
2126 let ( tx, rx) = calloop:: channel:: channel ( ) ;
2227
@@ -58,29 +63,17 @@ pub fn init(evlh: &LoopHandle<'static, State>) -> Result<Vec<RegistrationToken>>
5863 . with_context ( || "Failed to add channel to event_loop" ) ?;
5964
6065 // start helper thread
61- let result = std:: thread:: Builder :: new ( )
62- . name ( "system76-power-hotplug" . to_string ( ) )
63- . spawn ( move || {
64- if let Ok ( mut msg_iter) = power_daemon. receive_hot_plug_detect ( ) {
65- while let Some ( msg) = msg_iter. next ( ) {
66- if tx. send ( msg) . is_err ( ) {
67- break ;
68- }
66+ executor. spawn_ok ( async move {
67+ if let Ok ( mut msg_iter) = power_daemon. receive_hot_plug_detect ( ) . await {
68+ while let Some ( msg) = msg_iter. next ( ) . await {
69+ if tx. send ( msg) . is_err ( ) {
70+ break ;
6971 }
7072 }
71- } )
72- . with_context ( || "Failed to start helper thread" ) ;
73-
74- match result {
75- Ok ( _handle) => {
76- tokens. push ( token) ;
77- // detach thread
7873 }
79- Err ( err) => {
80- evlh. remove ( token) ;
81- return Err ( err) ;
82- }
83- }
74+ } ) ;
75+
76+ tokens. push ( token) ;
8477 }
8578 Err ( err) => {
8679 tracing:: info!( ?err, "Failed to connect to com.system76.PowerDaemon" ) ;
0 commit comments