11use {
22 crate :: {
3- cli:: { duration:: parse_duration, GlobalArgs , IdleArgs , IdleCmd , IdleSetArgs } ,
3+ cli:: { duration:: parse_duration, GlobalArgs , IdleArgs } ,
44 tools:: tool_client:: { with_tool_client, Handle , ToolClient } ,
5- utils:: stack:: Stack ,
5+ utils:: { debug_fn :: debug_fn , stack:: Stack } ,
66 wire:: { jay_compositor, jay_idle, JayIdleId , WlSurfaceId } ,
77 } ,
8+ clap:: { Args , Subcommand } ,
89 std:: { cell:: Cell , rc:: Rc } ,
910} ;
1011
12+ #[ derive( Subcommand , Debug ) ]
13+ pub enum IdleCmd {
14+ /// Print the idle status.
15+ Status ,
16+ /// Set the idle interval.
17+ Set ( IdleSetArgs ) ,
18+ /// Set the idle grace period.
19+ SetGracePeriod ( IdleSetGracePeriodArgs ) ,
20+ }
21+
22+ impl Default for IdleCmd {
23+ fn default ( ) -> Self {
24+ Self :: Status
25+ }
26+ }
27+
28+ #[ derive( Args , Debug ) ]
29+ pub struct IdleSetArgs {
30+ /// The interval of inactivity after which to disable the screens.
31+ ///
32+ /// This can be either a number in minutes and seconds or the keyword `disabled` to
33+ /// disable the screensaver.
34+ ///
35+ /// Minutes and seconds can be specified in any of the following formats:
36+ ///
37+ /// * 1m
38+ /// * 1m5s
39+ /// * 1m 5s
40+ /// * 1min 5sec
41+ /// * 1 minute 5 seconds
42+ #[ clap( verbatim_doc_comment, required = true ) ]
43+ pub interval : Vec < String > ,
44+ }
45+
46+ #[ derive( Args , Debug ) ]
47+ pub struct IdleSetGracePeriodArgs {
48+ /// The grace period after the idle timeout expires.
49+ ///
50+ /// During this period, after the idle timeout expires, the screen only goes black
51+ /// but is not yet disabled or locked.
52+ ///
53+ /// This uses the same formatting options as the idle timeout itself.
54+ #[ clap( verbatim_doc_comment, required = true ) ]
55+ pub period : Vec < String > ,
56+ }
57+
1158pub fn main ( global : GlobalArgs , args : IdleArgs ) {
1259 with_tool_client ( global. log_level . into ( ) , |tc| async move {
1360 let idle = Idle { tc : tc. clone ( ) } ;
@@ -31,16 +78,21 @@ impl Idle {
3178 match args. command . unwrap_or_default ( ) {
3279 IdleCmd :: Status => self . status ( idle) . await ,
3380 IdleCmd :: Set ( args) => self . set ( idle, args) . await ,
81+ IdleCmd :: SetGracePeriod ( args) => self . set_grace_period ( idle, args) . await ,
3482 }
3583 }
3684
3785 async fn status ( self , idle : JayIdleId ) {
3886 let tc = & self . tc ;
3987 tc. send ( jay_idle:: GetStatus { self_id : idle } ) ;
40- let interval = Rc :: new ( Cell :: new ( 0u64 ) ) ;
41- jay_idle:: Interval :: handle ( tc, idle, interval . clone ( ) , |iv, msg| {
88+ let timeout = Rc :: new ( Cell :: new ( 0u64 ) ) ;
89+ jay_idle:: Interval :: handle ( tc, idle, timeout . clone ( ) , |iv, msg| {
4290 iv. set ( msg. interval ) ;
4391 } ) ;
92+ let grace = Rc :: new ( Cell :: new ( 0u64 ) ) ;
93+ jay_idle:: GracePeriod :: handle ( tc, idle, grace. clone ( ) , |iv, msg| {
94+ iv. set ( msg. period ) ;
95+ } ) ;
4496 struct Inhibitor {
4597 surface : WlSurfaceId ,
4698 _client_id : u64 ,
@@ -57,26 +109,31 @@ impl Idle {
57109 } ) ;
58110 } ) ;
59111 tc. round_trip ( ) . await ;
60- let minutes = interval. get ( ) / 60 ;
61- let seconds = interval. get ( ) % 60 ;
62- print ! ( "Interval:" ) ;
63- if minutes == 0 && seconds == 0 {
64- print ! ( " disabled" ) ;
65- } else {
66- if minutes > 0 {
67- print ! ( " {} minute" , minutes) ;
68- if minutes > 1 {
69- print ! ( "s" ) ;
112+ let interval = |iv : u64 | {
113+ debug_fn ( move |f| {
114+ let minutes = iv / 60 ;
115+ let seconds = iv % 60 ;
116+ if minutes == 0 && seconds == 0 {
117+ write ! ( f, " disabled" ) ?;
118+ } else {
119+ if minutes > 0 {
120+ write ! ( f, " {} minute" , minutes) ?;
121+ if minutes > 1 {
122+ write ! ( f, "s" ) ?;
123+ }
124+ }
125+ if seconds > 0 {
126+ write ! ( f, " {} second" , seconds) ?;
127+ if seconds > 1 {
128+ write ! ( f, "s" ) ?;
129+ }
130+ }
70131 }
71- }
72- if seconds > 0 {
73- print ! ( " {} second" , seconds) ;
74- if seconds > 1 {
75- print ! ( "s" ) ;
76- }
77- }
78- }
79- println ! ( ) ;
132+ Ok ( ( ) )
133+ } )
134+ } ;
135+ println ! ( "Interval:{}" , interval( timeout. get( ) ) ) ;
136+ println ! ( "Grace period:{}" , interval( grace. get( ) ) ) ;
80137 let mut inhibitors = inhibitors. take ( ) ;
81138 inhibitors. sort_by_key ( |i| i. pid ) ;
82139 inhibitors. sort_by_key ( |i| i. surface ) ;
@@ -93,15 +150,27 @@ impl Idle {
93150
94151 async fn set ( self , idle : JayIdleId , args : IdleSetArgs ) {
95152 let tc = & self . tc ;
96- let interval = if args. interval . len ( ) == 1 && args. interval [ 0 ] == "disabled" {
97- 0
98- } else {
99- parse_duration ( & args. interval ) . as_secs ( ) as u64
100- } ;
101153 tc. send ( jay_idle:: SetInterval {
102154 self_id : idle,
103- interval,
155+ interval : parse_idle_time ( & args. interval ) ,
156+ } ) ;
157+ tc. round_trip ( ) . await ;
158+ }
159+
160+ async fn set_grace_period ( self , idle : JayIdleId , args : IdleSetGracePeriodArgs ) {
161+ let tc = & self . tc ;
162+ tc. send ( jay_idle:: SetGracePeriod {
163+ self_id : idle,
164+ period : parse_idle_time ( & args. period ) ,
104165 } ) ;
105166 tc. round_trip ( ) . await ;
106167 }
107168}
169+
170+ fn parse_idle_time ( time : & [ String ] ) -> u64 {
171+ if time. len ( ) == 1 && time[ 0 ] == "disabled" {
172+ 0
173+ } else {
174+ parse_duration ( time) . as_secs ( ) as u64
175+ }
176+ }
0 commit comments