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

per display command.pipe and allow for different base directory #11

Merged
merged 3 commits into from
Feb 19, 2022
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
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions lefthk-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lefthk-core"
version = "0.1.0"
version = "0.1.1"
edition = "2018"
license = "BSD-3-Clause"
readme = "README.md"
Expand All @@ -9,7 +9,7 @@ description = "A hotkey daemon for Adventurers"

[dependencies]
log = "0.4.14"
mio = "0.7.14"
mio = "0.8.0"
nix = "0.23.0"
thiserror = "1.0.30"
tokio = { version = "1.14.0", features = ["fs", "io-util", "macros", "net", "rt-multi-thread", "sync", "time"] }
Expand Down
9 changes: 9 additions & 0 deletions lefthk-core/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ impl Pipe {
Ok(Self { pipe_file, rx })
}

pub fn pipe_name() -> PathBuf {
let display = std::env::var("DISPLAY")
.ok()
.and_then(|d| d.rsplit_once(':').map(|(_, r)| r.to_owned()))
.unwrap_or_else(|| "0".to_string());

PathBuf::from(format!("command-{}.pipe", display))
}

pub async fn read_command(&mut self) -> Option<Command> {
self.rx.recv().await
}
Expand Down
32 changes: 27 additions & 5 deletions lefthk-core/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ use crate::errors::{self, Error, LeftError};
use crate::ipc::Pipe;
use crate::xkeysym_lookup;
use crate::xwrap::{self, XWrap};
#[cfg(feature = "watcher")]
use std::path::PathBuf;
use std::process::{Command, Stdio};
use x11_dl::xlib;
use xdg::BaseDirectories;

pub struct Worker {
pub keybinds: Vec<Keybind>,
#[cfg(feature = "watcher")]
pub config_file: PathBuf,
pub base_directory: BaseDirectories,
pub xwrap: XWrap,
pub reload_requested: bool,
pub kill_requested: bool,
Expand All @@ -25,10 +28,16 @@ impl Drop for Worker {
}

impl Worker {
pub fn new(keybinds: Vec<Keybind>, config_file: PathBuf) -> Self {
#[cfg(feature = "watcher")]
pub fn new(
keybinds: Vec<Keybind>,
config_file: PathBuf,
base_directory: BaseDirectories,
) -> Self {
Self {
keybinds,
config_file,
base_directory,
xwrap: XWrap::new(),
reload_requested: false,
kill_requested: false,
Expand All @@ -42,9 +51,9 @@ impl Worker {
use crate::config::watcher::Watcher;

self.xwrap.grab_keys(&self.keybinds);
let path = errors::exit_on_error!(BaseDirectories::with_prefix("lefthk"));
let mut watcher = errors::exit_on_error!(Watcher::new(&self.config_file));
let pipe_file = errors::exit_on_error!(path.place_runtime_file("commands.pipe"));
let pipe_name = Pipe::pipe_name();
let pipe_file = errors::exit_on_error!(self.base_directory.place_runtime_file(pipe_name));
let mut pipe = errors::exit_on_error!(Pipe::new(pipe_file).await);
loop {
if self.kill_requested || self.reload_requested {
Expand Down Expand Up @@ -88,11 +97,24 @@ impl Worker {
}
}

#[cfg(not(feature = "watcher"))]
pub fn new(keybinds: Vec<Keybind>, base_directory: BaseDirectories) -> Self {
Self {
keybinds,
base_directory,
xwrap: XWrap::new(),
reload_requested: false,
kill_requested: false,
chord_keybinds: None,
chord_elapsed: false,
}
}

#[cfg(not(feature = "watcher"))]
pub async fn event_loop(&mut self) {
self.xwrap.grab_keys(&self.keybinds);
let path = errors::exit_on_error!(BaseDirectories::with_prefix("lefthk"));
let pipe_file = errors::exit_on_error!(path.place_runtime_file("commands.pipe"));
let pipe_name = Pipe::pipe_name();
let pipe_file = errors::exit_on_error!(self.base_directory.place_runtime_file(pipe_name));
let mut pipe = errors::exit_on_error!(Pipe::new(pipe_file).await);
loop {
if self.kill_requested || self.reload_requested {
Expand Down
4 changes: 2 additions & 2 deletions lefthk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lefthk"
version = "0.1.0"
version = "0.1.1"
edition = "2018"
license = "BSD-3-Clause"
readme = "README.md"
Expand All @@ -10,7 +10,7 @@ description = "A hotkey daemon for Adventurers"
[dependencies]
clap = "2.3.0"
kdl = "3.0.0"
lefthk-core = { path = "../lefthk-core", version = '0.1.0'}
lefthk-core = { path = "../lefthk-core", version = '0.1.1'}
log = "0.4.14"
pretty_env_logger = "0.4.0"
serde = { version = "1.0.130", features = ["derive"] }
Expand Down
14 changes: 11 additions & 3 deletions lefthk/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::errors::LeftError;
use clap::{App, Arg};
use lefthk_core::ipc::Pipe;
use lefthk_core::{config::Config, worker::Worker};
use std::fs;
use std::io::Write;
Expand Down Expand Up @@ -36,6 +37,7 @@ fn main() {
pretty_env_logger::init();
let mut old_config = None;
let path = errors::exit_on_error!(BaseDirectories::with_prefix("lefthk"));
#[cfg(feature = "watcher")]
let config_file = errors::exit_on_error!(path.place_config_file("config.kdl"));
loop {
let config = match config::load() {
Expand All @@ -52,7 +54,12 @@ fn main() {
let completed = std::panic::catch_unwind(|| {
let rt = errors::return_on_error!(tokio::runtime::Runtime::new());
let _rt_guard = rt.enter();
let mut worker = Worker::new(config.mapped_bindings(), config_file.clone());
#[cfg(feature = "watcher")]
let mut worker =
Worker::new(config.mapped_bindings(), config_file.clone(), path.clone());

#[cfg(not(feature = "watcher"))]
let mut worker = Worker::new(config.mapped_bindings(), path.clone());

rt.block_on(worker.event_loop());
kill_requested.store(worker.kill_requested, Ordering::SeqCst);
Expand All @@ -72,7 +79,8 @@ fn main() {

fn send_command(command: &str) {
let path = errors::exit_on_error!(BaseDirectories::with_prefix("lefthk"));
let pipe_file = errors::exit_on_error!(path.place_runtime_file("commands.pipe"));
let pipe_name = Pipe::pipe_name();
let pipe_file = errors::exit_on_error!(path.place_runtime_file(pipe_name));
let mut pipe = fs::OpenOptions::new().write(true).open(&pipe_file).unwrap();
writeln!(&mut pipe, "{}", command).unwrap();
writeln!(pipe, "{}", command).unwrap();
}