67 lines
1.9 KiB
Rust

use clap::{CommandFactory, Parser};
use std::{io, process::exit, time::Duration};
use bscreensaver_command::{BCommand, Error, bscreensaver_command};
#[derive(Parser, Debug)]
#[command(name = "bscreensaver-command", version, author, about = "Send commands to the running bscreensaver instance", long_about = None)]
struct Args {
/// Blanks the screen right now
#[arg(short = 'b', long = "blank")]
blank: bool,
/// Lock the screen right now
#[arg(short = 'l', long = "lock")]
lock: bool,
/// 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(short = 'd', long = "deactivate")]
deactivate: bool,
/// Restarts the bscreensaver daemon
#[arg(short = 'r', long = "restart")]
restart: bool,
/// Causes the bscreensaver daemon to exit now, even if the screen is locked
#[arg(short = 'x', long = "exit")]
exit: bool,
}
fn main() -> io::Result<()> {
let args = Args::parse();
let command =
if args.blank {
BCommand::Blank
} else if args.lock {
BCommand::Lock
} else if args.deactivate {
BCommand::Deactivate
} else if args.restart {
BCommand::Restart
} else if args.exit {
BCommand::Exit
} else {
Args::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(())
}