11use crate :: state:: { BackendData , Common , State } ;
22use anyhow:: { Context , Result } ;
33use calloop:: { InsertError , LoopHandle , RegistrationToken } ;
4+ use futures_executor:: { block_on, ThreadPool } ;
5+ use futures_util:: stream:: StreamExt ;
46use std:: collections:: HashMap ;
57use zbus:: blocking:: { fdo:: DBusProxy , Connection } ;
68
79mod power;
810
9- pub fn init ( evlh : & LoopHandle < ' static , State > ) -> Result < Vec < RegistrationToken > > {
11+ pub fn init (
12+ evlh : & LoopHandle < ' static , State > ,
13+ executor : & ThreadPool ,
14+ ) -> Result < Vec < RegistrationToken > > {
1015 let mut tokens = Vec :: new ( ) ;
1116
12- match power:: init ( ) {
17+ match block_on ( power:: init ( ) ) {
1318 Ok ( power_daemon) => {
1419 let ( tx, rx) = calloop:: channel:: channel ( ) ;
1520
@@ -35,29 +40,17 @@ pub fn init(evlh: &LoopHandle<'static, State>) -> Result<Vec<RegistrationToken>>
3540 . with_context ( || "Failed to add channel to event_loop" ) ?;
3641
3742 // start helper thread
38- let result = std:: thread:: Builder :: new ( )
39- . name ( "system76-power-hotplug" . to_string ( ) )
40- . spawn ( move || {
41- if let Ok ( mut msg_iter) = power_daemon. receive_hot_plug_detect ( ) {
42- while let Some ( msg) = msg_iter. next ( ) {
43- if tx. send ( msg) . is_err ( ) {
44- break ;
45- }
43+ executor. spawn_ok ( async move {
44+ if let Ok ( mut msg_iter) = power_daemon. receive_hot_plug_detect ( ) . await {
45+ while let Some ( msg) = msg_iter. next ( ) . await {
46+ if tx. send ( msg) . is_err ( ) {
47+ break ;
4648 }
4749 }
48- } )
49- . with_context ( || "Failed to start helper thread" ) ;
50-
51- match result {
52- Ok ( _handle) => {
53- tokens. push ( token) ;
54- // detach thread
5550 }
56- Err ( err) => {
57- evlh. remove ( token) ;
58- return Err ( err) ;
59- }
60- }
51+ } ) ;
52+
53+ tokens. push ( token) ;
6154 }
6255 Err ( err) => {
6356 tracing:: info!( ?err, "Failed to connect to com.system76.PowerDaemon" ) ;
0 commit comments