Skip to content

Commit

Permalink
Implement --lock-now for instantly calling primary timer
Browse files Browse the repository at this point in the history
  • Loading branch information
yashsriv committed Mar 25, 2019
1 parent 1de9de5 commit 315af1d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version = "0.6.2"
clap = "2.32.0"
failure = "0.1.5"
mio = "0.6.16"
users = "0.9.1"

[dependencies.libpulse-sys]
optional = true
Expand Down
60 changes: 45 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[macro_use] extern crate clap;
#[macro_use] extern crate failure;
extern crate mio;
extern crate users;
extern crate x11;

#[cfg(feature = "pulse")] use std::sync::mpsc;
Expand All @@ -16,19 +17,21 @@ use nix::sys::{
};
use std::{
collections::HashMap,
env::temp_dir,
fs,
io::{self, prelude::*},
mem,
os::{
unix::{
io::AsRawFd,
net::UnixListener
net::{UnixListener, UnixStream},
}
},
path::Path,
process::Command,
time::Duration
};
use users::get_current_uid;
use x11::xss::{XScreenSaverAllocInfo, XScreenSaverInfo};

#[cfg(feature = "pulse")] mod pulse;
Expand Down Expand Up @@ -79,6 +82,12 @@ fn main() -> Result<(), Error> {
.help("Print the idle time to standard output. This is similar to xprintidle.")
.long("print")
)
.arg(
Arg::with_name("lock-now")
.help("Execute the primary timer immediately.")
.long("lock-now")
.conflicts_with("print")
)
.arg(
Arg::with_name("not-when-fullscreen")
.long_help("\
Expand All @@ -88,15 +97,17 @@ fn main() -> Result<(), Error> {
")
.long("not-when-fullscreen")
.conflicts_with("print")
.conflicts_with("lock-now")
)
.arg(
Arg::with_name("once")
.long_help("\
Exit after timer command has been invoked once. \
This does not include manual invoking using the socket. \
This does not include manual invoking using the socket or --lock-now. \
")
.long("once")
.conflicts_with("print")
.conflicts_with("lock-now")
)
// Options
.arg(
Expand All @@ -123,6 +134,7 @@ fn main() -> Result<(), Error> {
.multiple(true)
.required_unless("print")
.conflicts_with("print")
.conflicts_with("lock-now")
)
.arg(
Arg::with_name("socket")
Expand All @@ -146,6 +158,7 @@ fn main() -> Result<(), Error> {
.help("Don't invoke the timer when any audio is playing (PulseAudio specific)")
.long("not-when-audio")
.conflicts_with("print")
.conflicts_with("lock-now")
);
}
let matches = clap_app.get_matches();
Expand All @@ -160,6 +173,31 @@ fn main() -> Result<(), Error> {
return Ok(());
}

let mut temp_file;
let socket = match matches.value_of("socket") {
None => {
let uid = get_current_uid();
temp_file = temp_dir();
temp_file.push(format!("xidlehook{}", uid));
fs::create_dir(&temp_file).or_else(|err| {
if err.kind() == io::ErrorKind::AlreadyExists {
Ok(())
} else {
Err(err)
}
})?;
temp_file.push("socket");
temp_file.to_str().ok_or(io::Error::new(io::ErrorKind::Other, "Unable to get socket file location!"))
},
Some(socket) => Ok(socket)
}?;

if matches.is_present("lock-now") {
let mut socket = UnixStream::connect(&socket)?;
socket.write_all(b"\x02")?;
return Ok(());
}

#[cfg(feature = "nix")]
let mut signal = {
let mut mask = SigSet::empty();
Expand Down Expand Up @@ -247,19 +285,11 @@ fn main() -> Result<(), Error> {
#[cfg(feature = "nix")]
poll.register(&EventedFd(&signal.as_raw_fd()), TOKEN_SIGNAL, Ready::readable(), PollOpt::edge())?;

let mut _socket = None;
let mut listener = match matches.value_of("socket") {
None => None,
Some(socket) => {
let mut listener = UnixListener::bind(&socket)?;
_socket = Some(DeferRemove(socket)); // remove file when exiting

listener.set_nonblocking(true)?;
let listener = UnixListener::bind(&socket)?;
let _socket = DeferRemove(socket); // remove file when exiting
listener.set_nonblocking(true)?;
poll.register(&EventedFd(&listener.as_raw_fd()), TOKEN_SERVER, Ready::readable(), PollOpt::edge())?;

poll.register(&EventedFd(&listener.as_raw_fd()), TOKEN_SERVER, Ready::readable(), PollOpt::edge())?;
Some(listener)
}
};
let mut clients = HashMap::new();
let mut next_client = TOKEN_CLIENT.into();

Expand All @@ -286,7 +316,7 @@ fn main() -> Result<(), Error> {
match event.token() {
#[cfg(feature = "nix")]
TOKEN_SIGNAL => if signal.read_signal()?.is_some() { break 'main; },
TOKEN_SERVER => if let Some(listener) = listener.as_mut() {
TOKEN_SERVER => {
let (mut socket, _) = match maybe(listener.accept())? {
Some(socket) => socket,
None => continue
Expand Down

0 comments on commit 315af1d

Please sign in to comment.