@@ -3,6 +3,7 @@ use dbus::{
3
3
blocking:: Connection ,
4
4
message:: MatchRule ,
5
5
} ;
6
+ use env_logger:: Env ;
6
7
use std:: {
7
8
collections:: HashMap ,
8
9
error:: Error ,
@@ -39,7 +40,7 @@ impl DBusInterface for DBusRunner {
39
40
. with_namespaced_path ( DBUS_NAMESPACE ) ;
40
41
41
42
let good_to_send = Arc :: clone ( & self . good_to_send ) ;
42
- self . connection . add_match ( rule, move |_ : ( ) , _, msg| {
43
+ self . connection . add_match ( rule, move |( ) , _, msg| {
43
44
let items: HashMap < String , Variant < Box < dyn RefArg > > > =
44
45
msg. read3 :: < String , HashMap < _ , _ > , Vec < String > > ( ) . unwrap ( ) . 1 ;
45
46
if let Some ( playback_status) = items. get ( "PlaybackStatus" ) {
@@ -63,6 +64,7 @@ struct IdleApp {
63
64
inhibit_duration : i64 ,
64
65
last_block_time : Arc < Mutex < Option < Instant > > > ,
65
66
inhibit_process : Arc < Mutex < ( Option < Child > , Instant ) > > ,
67
+ process_running : Arc < AtomicBool > ,
66
68
}
67
69
68
70
impl IdleApp {
@@ -73,6 +75,7 @@ impl IdleApp {
73
75
inhibit_duration,
74
76
last_block_time : Arc :: new ( Mutex :: new ( None ) ) ,
75
77
inhibit_process : Arc :: new ( Mutex :: new ( ( None :: < Child > , Instant :: now ( ) ) ) ) ,
78
+ process_running : Arc :: new ( AtomicBool :: new ( false ) ) ,
76
79
} )
77
80
}
78
81
@@ -86,8 +89,11 @@ impl IdleApp {
86
89
if current_time >= next_check {
87
90
if let Ok ( mut inhibit) = self . inhibit_process . lock ( ) {
88
91
if inhibit. 0 . is_none ( ) || current_time >= inhibit. 1 {
92
+ let process_running = Arc :: clone ( & self . process_running ) ;
89
93
if let Some ( mut child) = inhibit. 0 . take ( ) {
94
+ child. wait ( ) ?;
90
95
let _ = child. kill ( ) ;
96
+ process_running. store ( false , std:: sync:: atomic:: Ordering :: SeqCst ) ;
91
97
}
92
98
match self
93
99
. dbus_runner
@@ -99,16 +105,36 @@ impl IdleApp {
99
105
. dbus_runner
100
106
. good_to_send
101
107
. load ( std:: sync:: atomic:: Ordering :: SeqCst ) ;
102
- if block {
108
+ // Only spawn a single child if its blocking already, move on
109
+ log:: debug!(
110
+ "should_block = {} - process_running = {:?}" ,
111
+ block,
112
+ process_running,
113
+ ) ;
114
+ if block
115
+ && !process_running. load ( std:: sync:: atomic:: Ordering :: SeqCst )
116
+ {
117
+ if let Some ( mut killing) = inhibit. 0 . take ( ) {
118
+ killing. wait ( ) ?;
119
+ killing. kill ( ) ?;
120
+ }
103
121
match self . run_cmd ( ) {
104
- Ok ( child) => inhibit. 0 = Some ( child) ,
122
+ Ok ( child) => {
123
+ log:: debug!( "Swayidle is inhibiting now!" ) ;
124
+ inhibit. 0 = Some ( child) ;
125
+ process_running
126
+ . store ( true , std:: sync:: atomic:: Ordering :: SeqCst ) ;
127
+ }
105
128
Err ( e) => {
106
129
eprintln ! ( "unable to block swayidle :: {:?}" , e)
107
130
}
108
131
}
109
132
} else if !block {
110
133
if let Some ( mut killing) = inhibit. 0 . take ( ) {
111
- let _ = killing. kill ( ) ;
134
+ killing. wait ( ) ?;
135
+ killing. kill ( ) ?;
136
+ process_running
137
+ . store ( false , std:: sync:: atomic:: Ordering :: SeqCst ) ;
112
138
}
113
139
}
114
140
}
@@ -126,6 +152,7 @@ impl IdleApp {
126
152
}
127
153
128
154
fn run_cmd ( & self ) -> Result < Child , Box < dyn Error > > {
155
+ log:: debug!( "command is spawning" ) ;
129
156
match Command :: new ( "systemd-inhibit" )
130
157
. arg ( "--what" )
131
158
. arg ( "idle" )
@@ -163,6 +190,8 @@ const INHIBIT_DURATION: u64 = 55;
163
190
const OVERLAP_DURATION : u64 = 5 ;
164
191
165
192
fn main ( ) -> Result < ( ) , Box < dyn Error > > {
193
+ env_logger:: Builder :: from_env ( Env :: default ( ) . default_filter_or ( "info" ) ) . init ( ) ;
194
+ log:: debug!( "Swaddle starting up" ) ;
166
195
let app = IdleApp :: new ( 60 ) ?;
167
196
app. run ( )
168
197
}
0 commit comments