@@ -7,6 +7,7 @@ use std::{
7
7
collections:: HashMap ,
8
8
error:: Error ,
9
9
process:: { Child , Command } ,
10
+ sync:: atomic:: AtomicBool ,
10
11
sync:: { Arc , Mutex } ,
11
12
thread:: sleep,
12
13
time:: { Duration , Instant } ,
@@ -16,20 +17,17 @@ trait DBusInterface {
16
17
fn add_match ( & self ) -> Result < ( ) , Box < dyn Error > > ;
17
18
}
18
19
19
- trait CommandCaller {
20
- fn execute_command ( & self ) -> Result < ( ) , Box < dyn Error > > ;
21
- }
22
20
struct DBusRunner {
23
21
connection : Arc < Connection > ,
24
- good_to_send : Arc < Mutex < bool > > ,
22
+ good_to_send : Arc < AtomicBool > ,
25
23
}
26
24
27
25
impl DBusRunner {
28
26
pub fn new ( ) -> Result < Self , Box < dyn Error > > {
29
27
let connection = Connection :: new_session ( ) ?;
30
28
Ok ( DBusRunner {
31
29
connection : Arc :: new ( connection) ,
32
- good_to_send : Arc :: new ( Mutex :: new ( false ) ) ,
30
+ good_to_send : Arc :: new ( AtomicBool :: new ( false ) ) ,
33
31
} )
34
32
}
35
33
}
@@ -40,21 +38,17 @@ impl DBusInterface for DBusRunner {
40
38
. with_interface ( INTERFACE_NAME )
41
39
. with_namespaced_path ( DBUS_NAMESPACE ) ;
42
40
43
- let sending_clone = Arc :: clone ( & self . good_to_send ) ;
41
+ let good_to_send = Arc :: clone ( & self . good_to_send ) ;
44
42
self . connection . add_match ( rule, move |_: ( ) , _, msg| {
45
43
let items: HashMap < String , Variant < Box < dyn RefArg > > > =
46
44
msg. read3 :: < String , HashMap < _ , _ > , Vec < String > > ( ) . unwrap ( ) . 1 ;
47
45
if let Some ( playback_status) = items. get ( "PlaybackStatus" ) {
48
46
if let Some ( status) = playback_status. 0 . as_str ( ) {
49
47
if status == "Paused" {
50
- if let Ok ( mut send_it) = sending_clone. lock ( ) {
51
- * send_it = false ;
52
- }
48
+ good_to_send. store ( false , std:: sync:: atomic:: Ordering :: SeqCst ) ;
53
49
}
54
50
if status == "Playing" {
55
- if let Ok ( mut send_it) = sending_clone. lock ( ) {
56
- * send_it = true ;
57
- }
51
+ good_to_send. store ( true , std:: sync:: atomic:: Ordering :: SeqCst ) ;
58
52
}
59
53
}
60
54
}
@@ -101,19 +95,21 @@ impl IdleApp {
101
95
. process ( Duration :: from_millis ( 1000 ) )
102
96
{
103
97
Ok ( _) => {
104
- if let Ok ( block) = self . dbus_runner . good_to_send . lock ( ) {
105
- if * block {
106
- match self . run_cmd ( ) {
107
- Ok ( child) => inhibit. 0 = Some ( child) ,
108
- Err ( e) => {
109
- eprintln ! ( "unable to block swayidle :: {:?}" , e)
110
- }
111
- }
112
- } else if !* block {
113
- if let Some ( mut killing) = inhibit. 0 . take ( ) {
114
- let _ = killing. kill ( ) ;
98
+ let block = self
99
+ . dbus_runner
100
+ . good_to_send
101
+ . load ( std:: sync:: atomic:: Ordering :: SeqCst ) ;
102
+ if block {
103
+ match self . run_cmd ( ) {
104
+ Ok ( child) => inhibit. 0 = Some ( child) ,
105
+ Err ( e) => {
106
+ eprintln ! ( "unable to block swayidle :: {:?}" , e)
115
107
}
116
108
}
109
+ } else if !block {
110
+ if let Some ( mut killing) = inhibit. 0 . take ( ) {
111
+ let _ = killing. kill ( ) ;
112
+ }
117
113
}
118
114
}
119
115
Err ( e) => eprintln ! ( "Error handling D-Bus message: {:?}" , e) ,
@@ -199,23 +195,3 @@ mod idle_app_tests {
199
195
assert ! ( app. is_ok( ) ) ;
200
196
}
201
197
}
202
-
203
- #[ cfg( test) ]
204
- mod command_caller_tests {
205
- use super :: * ;
206
-
207
- struct MockCommandCaller ;
208
-
209
- impl CommandCaller for MockCommandCaller {
210
- fn execute_command ( & self ) -> Result < ( ) , Box < dyn Error > > {
211
- Ok ( ( ) )
212
- }
213
- }
214
-
215
- #[ test]
216
- fn test_execute_command ( ) {
217
- let mock_caller = MockCommandCaller ;
218
- let result = mock_caller. execute_command ( ) ;
219
- assert ! ( result. is_ok( ) ) ;
220
- }
221
- }
0 commit comments