|
| 1 | +use std::{os::unix::net::UnixStream, path::PathBuf, process::exit}; |
| 2 | + |
| 3 | +use backlight_ipc::{BacklightCommand, DEFAULT_UNIX_SOCKET_PATH}; |
| 4 | +use clap::Parser; |
| 5 | + |
| 6 | +#[derive(Parser)] |
| 7 | +#[command(version, about, long_about = None)] |
| 8 | +struct BacklightctlCli { |
| 9 | + /// Set the brightness of all monitors (valid values examples: 50% +10% -10%) |
| 10 | + #[clap(short, long)] |
| 11 | + #[structopt(allow_hyphen_values = true)] |
| 12 | + brightness: Option<String>, |
| 13 | + |
| 14 | + /// Refresh the list of known monitors (called by the udev rule) |
| 15 | + #[clap(short, long, default_value_t = false)] |
| 16 | + refresh: bool, |
| 17 | + |
| 18 | + /// UNIX socket path (for test purposes) |
| 19 | + #[clap(short, long, default_value = DEFAULT_UNIX_SOCKET_PATH)] |
| 20 | + unix_socket_path: PathBuf, |
| 21 | +} |
| 22 | + |
| 23 | +fn main() { |
| 24 | + let cli = BacklightctlCli::parse(); |
| 25 | + |
| 26 | + let brightness_cmd = if let Some(brightness) = cli.brightness { |
| 27 | + if brightness.chars().last().is_some_and(|c| c != '%') { |
| 28 | + eprintln!("Brightness value is missing a % sign at the end"); |
| 29 | + exit(1); |
| 30 | + } |
| 31 | + |
| 32 | + let potential_brightness_modifier = brightness.chars().next(); |
| 33 | + |
| 34 | + if potential_brightness_modifier.is_some_and(|c| c == '+') { |
| 35 | + let brightness = brightness |
| 36 | + .chars() |
| 37 | + .skip(1) |
| 38 | + .take_while(|&c| c != '%') |
| 39 | + .collect::<String>(); |
| 40 | + |
| 41 | + let brightness = match brightness.parse::<u8>() { |
| 42 | + Ok(percent) => percent, |
| 43 | + Err(err) => { |
| 44 | + eprintln!("Unable to parse brightness value {brightness}: {err}"); |
| 45 | + exit(1); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + if brightness > 100 { |
| 50 | + eprintln!("Brightness value must be a percentage between -100% and 100%"); |
| 51 | + exit(1); |
| 52 | + } |
| 53 | + |
| 54 | + Some(BacklightCommand::IncreaseBrightness(brightness)) |
| 55 | + } else if potential_brightness_modifier.is_some_and(|c| c == '-') { |
| 56 | + let brightness = brightness |
| 57 | + .chars() |
| 58 | + .skip(1) |
| 59 | + .take_while(|&c| c != '%') |
| 60 | + .collect::<String>(); |
| 61 | + |
| 62 | + let brightness = match brightness.parse::<usize>() { |
| 63 | + Ok(percent) => percent, |
| 64 | + Err(err) => { |
| 65 | + eprintln!("Unable to parse brightness value {brightness}: {err}"); |
| 66 | + exit(1); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + if brightness > 100 { |
| 71 | + eprintln!("Brightness value must be a percentage between -100% and +100%"); |
| 72 | + exit(1); |
| 73 | + } |
| 74 | + |
| 75 | + Some(BacklightCommand::DecreaseBrightness(brightness as u8)) |
| 76 | + } else { |
| 77 | + let brightness = brightness |
| 78 | + .chars() |
| 79 | + .take_while(|&c| c != '%') |
| 80 | + .collect::<String>(); |
| 81 | + |
| 82 | + let brightness = match brightness.parse::<usize>() { |
| 83 | + Ok(percent) => percent, |
| 84 | + Err(err) => { |
| 85 | + eprintln!("Unable to parse brightness value {brightness}: {err}"); |
| 86 | + exit(1); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + if brightness > 100 { |
| 91 | + eprintln!("Brightness value must be a percentage between -100% and +100%"); |
| 92 | + exit(1); |
| 93 | + } |
| 94 | + |
| 95 | + Some(BacklightCommand::SetBrightness(brightness as u8)) |
| 96 | + } |
| 97 | + } else { |
| 98 | + None |
| 99 | + }; |
| 100 | + |
| 101 | + let stream = match UnixStream::connect(&cli.unix_socket_path) { |
| 102 | + Ok(stream) => stream, |
| 103 | + Err(err) => { |
| 104 | + eprintln!("{}: {err}", cli.unix_socket_path.display()); |
| 105 | + exit(1); |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + if cli.refresh { |
| 110 | + if let Err(err) = BacklightCommand::Refresh.serialize_into(&stream) { |
| 111 | + eprintln!("{err}"); |
| 112 | + exit(1); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if let Some(brightness_cmd) = brightness_cmd { |
| 117 | + if let Err(err) = brightness_cmd.serialize_into(&stream) { |
| 118 | + eprintln!("{err}"); |
| 119 | + exit(1); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments