1
- use crate :: delayed :: Delayed ;
1
+ use crate :: commands :: Command ;
2
2
use figment:: {
3
3
providers:: { Env , Format , Toml } ,
4
4
Figment ,
5
5
} ;
6
6
use serde:: Deserialize ;
7
- use std:: { process:: Output , sync:: Mutex } ;
8
7
9
8
#[ derive( Deserialize , Debug ) ]
10
9
pub struct Config {
@@ -74,14 +73,6 @@ impl BLEConnection {
74
73
}
75
74
}
76
75
77
- pub fn run ( cmd : & str ) -> anyhow:: Result < Output > {
78
- let output = std:: process:: Command :: new ( "sh" )
79
- . arg ( "-c" )
80
- . arg ( cmd)
81
- . output ( ) ?;
82
- Ok ( output)
83
- }
84
-
85
76
#[ derive( Deserialize , Debug , Clone ) ]
86
77
#[ serde( tag = "type" ) ]
87
78
#[ serde( rename_all = "lowercase" ) ]
@@ -97,59 +88,6 @@ pub struct ProximityAction {
97
88
pub command : Command ,
98
89
}
99
90
100
- #[ derive( Debug , Deserialize , Clone ) ]
101
- #[ serde( rename_all = "lowercase" ) ]
102
- pub enum Command {
103
- Unlock ,
104
- Lock ,
105
- String ( String ) ,
106
- }
107
-
108
- // fixme: don't use sudo, use proper permissions
109
- // fixme: use dbus to lock/unlock
110
- static LOCKED : Mutex < bool > = Mutex :: new ( false ) ;
111
- static DELAYED_LOCK : Mutex < Option < Delayed > > = Mutex :: new ( None ) ;
112
-
113
- impl Command {
114
- pub fn run ( & self ) -> anyhow:: Result < ( ) > {
115
- let locked = * LOCKED . lock ( ) . unwrap ( ) ;
116
- match self {
117
- Command :: Unlock => {
118
- // cancel the delayed lock
119
- if let Some ( delayed) = & mut * DELAYED_LOCK . lock ( ) . unwrap ( ) {
120
- delayed. cancel ( ) ;
121
- }
122
- if locked {
123
- println ! ( "Unlocking desktop..." ) ;
124
- run ( "sudo loginctl unlock-sessions" ) ?;
125
- * LOCKED . lock ( ) . unwrap ( ) = false ;
126
- } ;
127
- }
128
-
129
- // fixme: unlocking might not be good idea if it wasn't locked automatically
130
- Command :: Lock => {
131
- let duration = std:: time:: Duration :: from_secs ( 15 ) ;
132
- if !locked {
133
- println ! ( "Locking desktop in {:?}" , duration) ;
134
- if let Some ( delayed) = & mut * DELAYED_LOCK . lock ( ) . unwrap ( ) {
135
- delayed. cancel ( ) ;
136
- }
137
- // wait before actually locking the desktop
138
- let delayed = Delayed :: new ( duration, || async {
139
- run ( "sudo loginctl lock-sessions" ) . expect ( "error running lock command" ) ;
140
- * LOCKED . lock ( ) . unwrap ( ) = true ;
141
- } ) ;
142
- * DELAYED_LOCK . lock ( ) . unwrap ( ) = Some ( delayed) ;
143
- }
144
- }
145
- Command :: String ( cmd) => {
146
- run ( cmd) ?;
147
- }
148
- } ;
149
- Ok ( ( ) )
150
- }
151
- }
152
-
153
91
const APP_NAME : & str = "nearby" ;
154
92
pub fn get_config ( ) -> anyhow:: Result < Config > {
155
93
let config_dir = dirs:: config_dir ( ) . expect ( "Could not find config directory" ) ;
0 commit comments