bscreensaver/command/src/main.rs

77 lines
2.4 KiB
Rust
Raw Normal View History

use clap::{Arg, Command};
use std::{env, io, process::exit, time::Duration};
use bscreensaver_command::{BCommand, Error, bscreensaver_command};
fn main() -> io::Result<()> {
let mut command = Command::new("bscreensaver-command")
.author(env!("CARGO_PKG_AUTHORS"))
.version(env!("CARGO_PKG_VERSION"))
.about("Send commands to the running bscreensaver instance")
.arg(
Arg::new("blank")
.long("blank")
.short('b')
.help("Blanks the screen right now")
)
.arg(
Arg::new("lock")
.long("lock")
.short('l')
.help("Lock the screen right now")
)
.arg(
Arg::new("deactivate")
.long("deactivate")
.short('d')
.help("Deactivates the screen lock, presenting the unlock dialog if needed. This can be used to 'reset' things so the screensaver thinks there has been user input")
)
.arg(
Arg::new("restart")
.long("restart")
.short('r')
.help("Restarts the bscreensaver daemon")
)
.arg(
Arg::new("exit")
.long("exit")
.short('x')
.help("Causes the bscreensaver daemon to exit now, even if the screen is locked")
);
let args = command.get_matches_mut();
let command =
if args.is_present("blank") {
BCommand::Blank
} else if args.is_present("lock") {
BCommand::Lock
} else if args.is_present("deactivate") {
BCommand::Deactivate
} else if args.is_present("restart") {
BCommand::Restart
} else if args.is_present("exit") {
BCommand::Exit
} else {
command.print_help()?;
exit(1);
};
match bscreensaver_command(command, Some(Duration::from_secs(4))) {
Err(Error::NotRunning) => {
eprintln!("bscreensaver is not running");
exit(1);
},
Err(Error::X(err)) => {
eprintln!("Failed to communicate with X server: {}", err);
exit(1);
},
Err(err) => {
eprintln!("Failed to communicate with X server: {}", err);
exit(1);
},
Ok(_) => (),
}
Ok(())
}