11use std:: { os:: unix:: net:: UnixStream , path:: PathBuf , process:: exit} ;
22
3- use backlight_ipc:: { BacklightCommand , DEFAULT_UNIX_SOCKET_PATH } ;
4- use clap:: Parser ;
3+ use backlight_ipc:: { BacklightCommand , BacklightMode , DEFAULT_UNIX_SOCKET_PATH } ;
4+ use clap:: { error :: ErrorKind , CommandFactory , Parser } ;
55
66#[ derive( Parser ) ]
77#[ command( version, about, long_about = None ) ]
@@ -11,6 +11,10 @@ struct BacklightctlCli {
1111 #[ structopt( allow_hyphen_values = true ) ]
1212 brightness : Option < String > ,
1313
14+ /// Set backlightd mode to auto (the daemon will automatically adjust brightness)
15+ #[ clap( short, long, default_value_t = false ) ]
16+ auto : bool ,
17+
1418 /// Refresh the list of known monitors (called by the udev rule)
1519 #[ clap( short, long, default_value_t = false ) ]
1620 refresh : bool ,
@@ -23,10 +27,23 @@ struct BacklightctlCli {
2327fn main ( ) {
2428 let cli = BacklightctlCli :: parse ( ) ;
2529
30+ if cli. auto && cli. brightness . is_some ( ) {
31+ BacklightctlCli :: command ( )
32+ . error (
33+ ErrorKind :: ArgumentConflict ,
34+ "You cannot use both --brightness and --auto" ,
35+ )
36+ . exit ( ) ;
37+ }
38+
2639 let brightness_cmd = if let Some ( brightness) = cli. brightness {
2740 if brightness. chars ( ) . last ( ) . is_some_and ( |c| c != '%' ) {
28- eprintln ! ( "Brightness value is missing a % sign at the end" ) ;
29- exit ( 1 ) ;
41+ BacklightctlCli :: command ( )
42+ . error (
43+ ErrorKind :: InvalidValue ,
44+ "Brightness value is missing a % sign at the end" ,
45+ )
46+ . exit ( ) ;
3047 }
3148
3249 let potential_brightness_modifier = brightness. chars ( ) . next ( ) ;
@@ -41,14 +58,22 @@ fn main() {
4158 let brightness = match brightness. parse :: < u8 > ( ) {
4259 Ok ( percent) => percent,
4360 Err ( err) => {
44- eprintln ! ( "Unable to parse brightness value {brightness}: {err}" ) ;
45- exit ( 1 ) ;
61+ BacklightctlCli :: command ( )
62+ . error (
63+ ErrorKind :: InvalidValue ,
64+ format ! ( "Unable to parse brightness value {brightness}: {err}" ) ,
65+ )
66+ . exit ( ) ;
4667 }
4768 } ;
4869
4970 if brightness > 100 {
50- eprintln ! ( "Brightness value must be a percentage between -100% and 100%" ) ;
51- exit ( 1 ) ;
71+ BacklightctlCli :: command ( )
72+ . error (
73+ ErrorKind :: InvalidValue ,
74+ "Brightness value must be a percentage between -100% and 100%" ,
75+ )
76+ . exit ( ) ;
5277 }
5378
5479 Some ( BacklightCommand :: IncreaseBrightness ( brightness) )
@@ -62,14 +87,22 @@ fn main() {
6287 let brightness = match brightness. parse :: < usize > ( ) {
6388 Ok ( percent) => percent,
6489 Err ( err) => {
65- eprintln ! ( "Unable to parse brightness value {brightness}: {err}" ) ;
66- exit ( 1 ) ;
90+ BacklightctlCli :: command ( )
91+ . error (
92+ ErrorKind :: InvalidValue ,
93+ format ! ( "Unable to parse brightness value {brightness}: {err}" ) ,
94+ )
95+ . exit ( ) ;
6796 }
6897 } ;
6998
7099 if brightness > 100 {
71- eprintln ! ( "Brightness value must be a percentage between -100% and +100%" ) ;
72- exit ( 1 ) ;
100+ BacklightctlCli :: command ( )
101+ . error (
102+ ErrorKind :: InvalidValue ,
103+ "Brightness value must be a percentage between -100% and 100%" ,
104+ )
105+ . exit ( ) ;
73106 }
74107
75108 Some ( BacklightCommand :: DecreaseBrightness ( brightness as u8 ) )
@@ -82,14 +115,22 @@ fn main() {
82115 let brightness = match brightness. parse :: < usize > ( ) {
83116 Ok ( percent) => percent,
84117 Err ( err) => {
85- eprintln ! ( "Unable to parse brightness value {brightness}: {err}" ) ;
86- exit ( 1 ) ;
118+ BacklightctlCli :: command ( )
119+ . error (
120+ ErrorKind :: InvalidValue ,
121+ format ! ( "Unable to parse brightness value {brightness}: {err}" ) ,
122+ )
123+ . exit ( ) ;
87124 }
88125 } ;
89126
90127 if brightness > 100 {
91- eprintln ! ( "Brightness value must be a percentage between -100% and +100%" ) ;
92- exit ( 1 ) ;
128+ BacklightctlCli :: command ( )
129+ . error (
130+ ErrorKind :: InvalidValue ,
131+ "Brightness value must be a percentage between -100% and 100%" ,
132+ )
133+ . exit ( ) ;
93134 }
94135
95136 Some ( BacklightCommand :: SetBrightness ( brightness as u8 ) )
@@ -113,6 +154,13 @@ fn main() {
113154 }
114155 }
115156
157+ if cli. auto {
158+ if let Err ( err) = BacklightCommand :: SetMode ( BacklightMode :: Auto ) . serialize_into ( & stream) {
159+ eprintln ! ( "{err}" ) ;
160+ exit ( 1 ) ;
161+ }
162+ }
163+
116164 if let Some ( brightness_cmd) = brightness_cmd {
117165 if let Err ( err) = brightness_cmd. serialize_into ( & stream) {
118166 eprintln ! ( "{err}" ) ;
0 commit comments