parent
b63e6179a3
commit
347d4bd409
@ -1 +0,0 @@
|
||||
../Makefile
|
@ -1,24 +0,0 @@
|
||||
use std::{
|
||||
os::unix::net::UnixStream,
|
||||
process::exit,
|
||||
io::Write,
|
||||
env,
|
||||
};
|
||||
|
||||
fn sock_send(command: &str) -> std::io::Result<()> {
|
||||
let mut stream = UnixStream::connect("/tmp/sohkctl.sock")?;
|
||||
stream.write_all(command.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut args_vec: Vec<String> = env::args().collect();
|
||||
args_vec.remove(0);
|
||||
let args = args_vec.join(" ");
|
||||
if args.len() == 0 {
|
||||
println!("You cannot send an empty command");
|
||||
exit(1);
|
||||
} else {
|
||||
let _ = sock_send(&args);
|
||||
}
|
||||
}
|
@ -1,345 +0,0 @@
|
||||
use clap::{arg, App};
|
||||
use config::{
|
||||
Hotkey,
|
||||
Modifier,
|
||||
};
|
||||
use rdev::{
|
||||
Event,
|
||||
EventType::{
|
||||
KeyPress,
|
||||
KeyRelease,
|
||||
},
|
||||
Key,
|
||||
};
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
thread,
|
||||
sync::{
|
||||
Mutex,
|
||||
Arc,
|
||||
mpsc::{
|
||||
sync_channel,
|
||||
SyncSender,
|
||||
},
|
||||
},
|
||||
env, fs,
|
||||
io::prelude::*,
|
||||
os::unix::net::UnixStream,
|
||||
os::unix::net::UnixListener,
|
||||
path::Path,
|
||||
process::{exit, id},
|
||||
time::SystemTime,
|
||||
};
|
||||
use sysinfo::{ProcessExt, System, SystemExt};
|
||||
|
||||
mod config;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub struct LastHotkey {
|
||||
hotkey: config::Hotkey,
|
||||
ran_at: SystemTime,
|
||||
}
|
||||
|
||||
pub fn key_listener(am_mode: Arc<Mutex<Option<String>>>, tx: SyncSender<Hotkey>) {
|
||||
let args = set_flags().get_matches();
|
||||
env::set_var("RUST_LOG", "sohkd=warn");
|
||||
|
||||
if args.is_present("debug") {
|
||||
env::set_var("RUST_LOG", "sohkd=trace");
|
||||
}
|
||||
|
||||
|
||||
let pidfile: String = String::from("/tmp/sohkd.pid");
|
||||
if Path::new(&pidfile).exists() {
|
||||
log::trace!("Reading {} file and checking for running instances.", pidfile);
|
||||
let sohkd_pid = match fs::read_to_string(&pidfile) {
|
||||
Ok(sohkd_pid) => sohkd_pid,
|
||||
Err(e) => {
|
||||
log::error!("Unable to read {} to check all running instances", e);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
log::debug!("Previous PID: {}", sohkd_pid);
|
||||
|
||||
let mut sys = System::new_all();
|
||||
sys.refresh_all();
|
||||
for (pid, process) in sys.processes() {
|
||||
if pid.to_string() == sohkd_pid && process.exe() == env::current_exe().unwrap() {
|
||||
log::error!("sohkd is already running!");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match fs::write(&pidfile, id().to_string()) {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::error!("Unable to write to {}: {}", pidfile, e);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
let config_file_path: std::path::PathBuf;
|
||||
if args.is_present("config") {
|
||||
config_file_path = Path::new(args.value_of("config").unwrap()).to_path_buf();
|
||||
} else {
|
||||
config_file_path = check_config_xdg();
|
||||
}
|
||||
log::debug!("Using config file path: {:#?}", config_file_path);
|
||||
|
||||
if !config_file_path.exists() {
|
||||
log::error!("{:#?} doesn't exist", config_file_path);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
let hotkeys = match config::load(config_file_path) {
|
||||
Err(e) => {
|
||||
log::error!("Config Error: {}", e);
|
||||
exit(1);
|
||||
}
|
||||
Ok(out) => out,
|
||||
};
|
||||
|
||||
for hotkey in &hotkeys {
|
||||
log::debug!("hotkey: {:#?}", hotkey);
|
||||
}
|
||||
|
||||
let current_keys: RefCell<Vec<Key>> = RefCell::new(Vec::new());
|
||||
let last_keys: RefCell<Vec<Key>> = RefCell::new(Vec::new());
|
||||
|
||||
let rgrab = rdev::grab(move |ev| {
|
||||
// If this is a repeated key sequence (i.e., polled 2nd+ time while same combo is held), then
|
||||
if !is_new_key_event(&ev, &mut current_keys.borrow_mut(), &mut last_keys.borrow_mut()) {
|
||||
return Some(ev);
|
||||
}
|
||||
|
||||
let mode = am_mode.lock().expect("thread crashed due to not being able to lock mutex!");
|
||||
let hotkey = hotkey_match(¤t_keys.borrow(), &mode, &hotkeys);
|
||||
match hotkey {
|
||||
Some(hk) => {
|
||||
// needed so the value can be used after sending it down the channel
|
||||
let consume = hk.consume.clone();
|
||||
match tx.send(hk) {
|
||||
Ok(_) => log::trace!("Hotkey sent down channel successfully!"),
|
||||
Err(e) => log::error!("Hotkey could not be sent down channel! {:?}", e),
|
||||
}
|
||||
if consume { return None; }
|
||||
else { return Some(ev); }
|
||||
},
|
||||
None => {
|
||||
log::trace!("hotkey not found");
|
||||
return Some(ev);
|
||||
}
|
||||
}
|
||||
});
|
||||
match rgrab {
|
||||
Ok(_) => log::debug!("rdev::grab has run successfully!"),
|
||||
Err(e) => log::error!("rdev::grab returned an error: {:?}", e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn key_to_mod(key: &Key) -> Option<Modifier> {
|
||||
match key {
|
||||
Key::MetaLeft => Some(config::Modifier::Super),
|
||||
Key::MetaRight => Some(config::Modifier::Super),
|
||||
Key::Alt => Some(config::Modifier::Alt),
|
||||
Key::AltGr => Some(config::Modifier::Alt),
|
||||
Key::ControlLeft => Some(config::Modifier::Control),
|
||||
Key::ControlRight => Some(config::Modifier::Control),
|
||||
Key::ShiftLeft => Some(config::Modifier::Shift),
|
||||
Key::ShiftRight => Some(config::Modifier::Shift),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_modifiers(keys: &Vec<Key>) -> Vec<Modifier> {
|
||||
keys.iter().filter_map(key_to_mod).collect()
|
||||
}
|
||||
|
||||
pub fn is_mod(key: &Key) -> bool {
|
||||
match key {
|
||||
Key::MetaLeft => true,
|
||||
Key::MetaRight => true,
|
||||
Key::Alt => true,
|
||||
Key::AltGr => true,
|
||||
Key::ControlLeft => true,
|
||||
Key::ControlRight => true,
|
||||
Key::ShiftLeft => true,
|
||||
Key::ShiftRight => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_keysym(keys: &Vec<Key>) -> Option<Key> {
|
||||
for key in keys {
|
||||
if !is_mod(key) { return Some(*key); }
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn hotkey_match(current_keys: &Vec<Key>, mode: &Option<String>, hotkeys: &Vec<config::Hotkey>) -> Option<config::Hotkey> {
|
||||
for key in hotkeys {
|
||||
log::trace!("hotkey info: {:#?}", key);
|
||||
let keysym = get_keysym(¤t_keys);
|
||||
log::trace!("\tkeysym: {:#?}", keysym);
|
||||
log::trace!("\tmods: {:#?}", get_modifiers(¤t_keys));
|
||||
log::trace!("\tmode: {:#?}", mode);
|
||||
if key.modifiers == get_modifiers(¤t_keys)
|
||||
&& (keysym.is_some() && key.keysym == keysym.unwrap())
|
||||
&& (key.mode.is_none() || key.mode == *mode) {
|
||||
return Some(key.clone());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn is_new_key_event(event: &Event, current_keys: &mut Vec<Key>, last_keys: &mut Vec<Key>) -> bool {
|
||||
match event.event_type {
|
||||
KeyPress(x) => {
|
||||
log::trace!("KeyPress: {:#?}", x);
|
||||
*last_keys = current_keys.clone();
|
||||
current_keys.push(x);
|
||||
current_keys.dedup();
|
||||
// if there is a new key pressed/released and it is not a repeat event
|
||||
if last_keys != current_keys {
|
||||
log::trace!("KEYS: {:?}", current_keys);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
},
|
||||
KeyRelease(x) => {
|
||||
log::trace!("KeyRelease: {:#?}", x);
|
||||
*last_keys = current_keys.clone();
|
||||
// remove just released key from curent keys
|
||||
current_keys.retain(|&k| k != x);
|
||||
false
|
||||
},
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
static MODE_SOCK: &str = "/tmp/sohkctl.sock";
|
||||
|
||||
pub fn listen_for_mode_change(mode_mutex: Arc<Mutex<Option<String>>>) -> std::io::Result<()> {
|
||||
if Path::new(MODE_SOCK).exists() {
|
||||
log::trace!("Sockfile exists, attempting to remove it.");
|
||||
match fs::remove_file(MODE_SOCK) {
|
||||
Ok(_) => {
|
||||
log::debug!("Removed old socket file");
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Error removing the socket file!: {}", e);
|
||||
log::error!("You can manually remove the socket file: {}", MODE_SOCK);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
}
|
||||
let listener = UnixListener::bind(MODE_SOCK)?;
|
||||
loop {
|
||||
match listener.accept() {
|
||||
Ok((mut socket, addr)) => {
|
||||
let mut response = String::new();
|
||||
socket.read_to_string(&mut response)?;
|
||||
let mut mode = mode_mutex.lock().expect("socket thread dies because of inability to lock mode!");
|
||||
log::debug!("socket: {:?} Address: {:?}, response: {:?}", socket, addr, response);
|
||||
*mode = Some(response);
|
||||
},
|
||||
Err(e) => log::error!("accept function failed! {:?}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
env_logger::init();
|
||||
log::trace!("Logger initialized.");
|
||||
let am_mode: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));
|
||||
let k_am_mode = Arc::clone(&am_mode);
|
||||
let m_am_mode = Arc::clone(&am_mode);
|
||||
let (tx, rx) = sync_channel::<Hotkey>(1024);
|
||||
|
||||
let key_receiver = thread::spawn(move || {
|
||||
loop {
|
||||
match rx.recv() {
|
||||
Ok(hk) => {
|
||||
match sock_send(&hk.command) {
|
||||
Ok(_) => {
|
||||
log::debug!("Successful sending of command to server process.");
|
||||
}
|
||||
Err(e) => {
|
||||
log::debug!("Error sending a command over the socket! {:?}", e);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
log::debug!("Error receiving key on key_sender thread. {:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let key_handler_listener = thread::spawn(move || {
|
||||
key_listener(k_am_mode, tx);
|
||||
});
|
||||
let mode_updater = thread::spawn(move || {
|
||||
let res = listen_for_mode_change(m_am_mode);
|
||||
match res {
|
||||
Ok(_) => log::trace!("Finished mode changer!"),
|
||||
Err(e) => log::error!("Error setting up mode socket: {}", e),
|
||||
}
|
||||
});
|
||||
log::trace!("Threads set up");
|
||||
mode_updater.join().unwrap();
|
||||
key_receiver.join().unwrap();
|
||||
key_handler_listener.join().unwrap();
|
||||
}
|
||||
|
||||
pub fn set_flags() -> App<'static> {
|
||||
let app = App::new("sohkd")
|
||||
.version(env!("CARGO_PKG_VERSION"))
|
||||
.author(env!("CARGO_PKG_AUTHORS"))
|
||||
.about("Simple Wayland HotKey Daemon")
|
||||
.arg(
|
||||
arg!(-c --config <CONFIG_FILE_PATH>)
|
||||
.required(false)
|
||||
.takes_value(true)
|
||||
.help("Set a custom config file path."),
|
||||
)
|
||||
.arg(
|
||||
arg!(-C --cooldown <COOLDOWN_IN_MS>)
|
||||
.required(false)
|
||||
.takes_value(true)
|
||||
.help("Set a custom repeat cooldown duration. Default is 250ms."),
|
||||
)
|
||||
.arg(arg!(-d - -debug).required(false).help("Enable debug mode."));
|
||||
app
|
||||
}
|
||||
|
||||
pub fn check_config_xdg() -> std::path::PathBuf {
|
||||
let config_file_path: std::path::PathBuf;
|
||||
match env::var("XDG_CONFIG_HOME") {
|
||||
Ok(val) => {
|
||||
config_file_path = Path::new(&val).join("sohkd/sohkdrc");
|
||||
log::debug!("XDG_CONFIG_HOME exists: {:#?}", val);
|
||||
return config_file_path;
|
||||
}
|
||||
Err(_) => {
|
||||
log::error!("XDG_CONFIG_HOME has not been set.");
|
||||
config_file_path = Path::new("/etc/sohkd/sohkdrc").to_path_buf();
|
||||
log::warn!(
|
||||
"Note: Due to the design of the application, the invoking user is always root."
|
||||
);
|
||||
log::warn!("You can set a custom config file with the -c option.");
|
||||
log::warn!("Adding your user to the input group could solve this.");
|
||||
log::warn!("However that's a massive security flaw and basically defeats the purpose of using wayland.");
|
||||
log::warn!("The following issue may be addressed in the future, but it is certainly not a priority right now.");
|
||||
}
|
||||
}
|
||||
config_file_path
|
||||
}
|
||||
|
||||
fn sock_send(command: &str) -> std::io::Result<()> {
|
||||
let mut stream = UnixStream::connect("/tmp/sohkd.sock")?;
|
||||
stream.write_all(command.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
@ -1,89 +0,0 @@
|
||||
use std::io::prelude::*;
|
||||
use std::os::unix::net::UnixListener;
|
||||
use std::{
|
||||
env, fs,
|
||||
path::Path,
|
||||
process::{exit, id, Command, Stdio},
|
||||
};
|
||||
use sysinfo::{ProcessExt, System, SystemExt};
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
env::set_var("RUST_LOG", "swhks=trace");
|
||||
env_logger::init();
|
||||
|
||||
let pidfile: String = String::from("/tmp/sohks.pid");
|
||||
let sockfile: String = String::from("/tmp/sohkd.sock");
|
||||
|
||||
if Path::new(&pidfile).exists() {
|
||||
log::trace!("Reading {} file and checking for running instances.", pidfile);
|
||||
let sohkd_pid = match fs::read_to_string(&pidfile) {
|
||||
Ok(sohkd_pid) => sohkd_pid,
|
||||
Err(e) => {
|
||||
log::error!("Unable to read {} to check all running instances", e);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
log::debug!("Previous PID: {}", sohkd_pid);
|
||||
|
||||
let mut sys = System::new_all();
|
||||
sys.refresh_all();
|
||||
for (pid, process) in sys.processes() {
|
||||
if pid.to_string() == sohkd_pid && process.exe() == env::current_exe().unwrap() {
|
||||
log::error!("Server is already running!");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if Path::new(&sockfile).exists() {
|
||||
log::trace!("Sockfile exists, attempting to remove it.");
|
||||
match fs::remove_file(&sockfile) {
|
||||
Ok(_) => {
|
||||
log::debug!("Removed old socket file");
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Error removing the socket file!: {}", e);
|
||||
log::error!("You can manually remove the socket file: {}", sockfile);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
match fs::write(&pidfile, id().to_string()) {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::error!("Unable to write to {}: {}", pidfile, e);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn run_system_command(command: &str) {
|
||||
match Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(command)
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::error!("Failed to execute {}", command);
|
||||
log::error!("Error, {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let listener = UnixListener::bind(sockfile)?;
|
||||
loop {
|
||||
match listener.accept() {
|
||||
Ok((mut socket, addr)) => {
|
||||
let mut response = String::new();
|
||||
socket.read_to_string(&mut response)?;
|
||||
run_system_command(&response);
|
||||
log::debug!("Socket: {:?} Address: {:?} Response: {}", socket, addr, response);
|
||||
}
|
||||
Err(e) => log::error!("accept function failed: {:?}", e),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue