Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a client for communication to the daemon via sockets #18

Merged
merged 3 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#cargo-features = [ "default-run" ]

[package]
authors = ["jD91mZM2 <me@krake.one>"]
description = "xautolock rewrite in Rust, with a few extra features"
Expand All @@ -6,6 +8,8 @@ name = "xidlehook"
readme = "README.md"
repository = "https://github.com/jD91mZM2/xidlehook"
version = "0.6.2"
#default-run = "xidlehook"

[dependencies]
clap = "2.32.0"
failure = "0.1.5"
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,22 @@ whatever you want), and then you can send one of the following bytes:
| 1 | Activate |
| 2 | Run the timer command immediately |

For convenience, there is now an xidlehook-client (see
[#18](https://github.com/jD91mZM2/xidlehook/pull/18)), which will communicate
with this API for you. See
```
xidlehook-client --help
```
for details.

A common use case of `xidlehook` is using it to run a lockscreen. To then
manually lock the screen, you could bind this bash command to a shortcut:
```
xidlehook-client --trigger --socket /path/to/xidlehook.sock
```

```sh
echo -ne "\x2" | socat - /path/to/xidlehook.sock
Alternatively, you can use the API directly using, for example, `socat`:
```
echo -ne "\x0" | socat - UNIX-CONNECT:/path/to/xidlehook.sock
```
(You have no reason to run this for the most cases anymore)
54 changes: 54 additions & 0 deletions src/bin/xidlehook-client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#[macro_use] extern crate clap;
extern crate failure;

use clap::Arg;
use failure::Error;
use std::{io::Write, os::unix::net::UnixStream};

fn main() -> Result<(), Error> {
let matches = app_from_crate!()
// Flags
.arg(
Arg::with_name("disable")
.help("Disable xidlehook timers")
.long("disable")
.required_unless_one(&["enable", "trigger"]),
)
.arg(
Arg::with_name("enable")
.help("Enable xidlehook timers")
.long("enable")
.required_unless_one(&["disable", "trigger"]),
)
.arg(
Arg::with_name("trigger")
.help("Execute the primary timer immediately")
.long("trigger")
.required_unless_one(&["disable", "enable"]),
)
// Options
.arg(
Arg::with_name("socket")
.help("Specify which socket the client should communicate over")
.long("socket")
.takes_value(true)
.required(true),
)
.get_matches();

let socket = matches.value_of("socket").unwrap();
let mut socket = UnixStream::connect(&socket)?;

let control_byte = if matches.is_present("disable") {
0
} else if matches.is_present("enable") {
1
} else if matches.is_present("trigger") {
2
} else {
unreachable!("One of --enable, --disable or --trigger should have been set at this point");
};
socket.write_all(&[control_byte])?;

return Ok(());
}
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extern crate mio;
extern crate x11;

#[cfg(feature = "pulse")] use std::sync::mpsc;
use clap::{App as ClapApp, Arg};
use clap::Arg;
use failure::Error;
use mio::{*, unix::EventedFd};
#[cfg(feature = "nix")]
Expand Down Expand Up @@ -70,9 +70,7 @@ fn maybe<T>(res: io::Result<T>) -> io::Result<Option<T>> {
}

fn main() -> Result<(), Error> {
let clap_app = ClapApp::new(crate_name!())
.author(crate_authors!())
.version(crate_version!())
let clap_app = app_from_crate!()
// Flags
.arg(
Arg::with_name("print")
Expand Down