From 53d85dfc50efa5bba6281ec9142898e69bdb7a91 Mon Sep 17 00:00:00 2001 From: Mohammad Aadil Shabier Date: Tue, 27 Feb 2024 19:17:59 +0530 Subject: [PATCH] [feat] add argument to ignore devices --ignoredevice takes the same format as --device and ignores the devices listed, separated by semicolon. If a device is listed both in the --device argument and --ignoredevice argument, it is not added. Also replaced default name from "" to "[unknown]" to prevent devices without valid names from being matched by empty device arguments. --- docs/swhkd.1.scd | 5 ++++- swhkd/src/daemon.rs | 22 +++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/docs/swhkd.1.scd b/docs/swhkd.1.scd index 8b3f3dc..5d1894b 100644 --- a/docs/swhkd.1.scd +++ b/docs/swhkd.1.scd @@ -38,9 +38,12 @@ More about the config file syntax in `swhkd(5)` *-d*, *--debug* Enable debug mode. -*-D, --device* +*-D, --device* Manually set the keyboard devices to use. Can occur multiple times. +*-I, --ignoredevice* + Manually set the keyboard devices to ignore. Can occur multiple times. + # SIGNALS - Reload config file: `sudo pkill -HUP swhkd` diff --git a/swhkd/src/daemon.rs b/swhkd/src/daemon.rs index eab40e8..e2ab179 100644 --- a/swhkd/src/daemon.rs +++ b/swhkd/src/daemon.rs @@ -63,6 +63,10 @@ struct Args { /// Take a list of devices from the user #[arg(short = 'D', long, num_args = 0.., value_delimiter = ' ')] device: Vec, + + /// Take a list of devices to ignore from the user + #[arg(short = 'I', long, num_args = 0.., value_delimiter = ' ')] + ignoredevice: Vec, } #[tokio::main] @@ -155,16 +159,20 @@ async fn main() -> Result<(), Box> { }; } - let arg_devices: Vec = args.device; + let arg_add_devices = args.device; + let arg_ignore_devices = args.ignoredevice; + + let to_add = |dev: &Device| arg_add_devices.contains(&dev.name().unwrap_or("[unknown]").to_string()); + let to_ignore = |dev: &Device| arg_ignore_devices.contains(&dev.name().unwrap_or("[unknown]").to_string()); let keyboard_devices: Vec<_> = { - if arg_devices.is_empty() { + if arg_add_devices.is_empty() { log::trace!("Attempting to find all keyboard file descriptors."); - evdev::enumerate().filter(|(_, dev)| check_device_is_keyboard(dev)).collect() - } else { evdev::enumerate() - .filter(|(_, dev)| arg_devices.contains(&dev.name().unwrap_or("").to_string())) + .filter(|(_, dev)| !to_ignore(dev) && check_device_is_keyboard(dev)) .collect() + } else { + evdev::enumerate().filter(|(_, dev)| !to_ignore(dev) && to_add(dev)).collect() } }; @@ -316,8 +324,8 @@ async fn main() -> Result<(), Box> { }, Ok(device) => device }; - let name = device.name().unwrap_or("[unknown]").to_string(); - if arg_devices.contains(&name) || check_device_is_keyboard(&device) { + if !to_ignore(&device) && (to_add(&device) || check_device_is_keyboard(&device)) { + let name = device.name().unwrap_or("[unknown]"); log::info!("Device '{}' at '{}' added.", name, node); let _ = device.grab(); keyboard_states.insert(node.to_string(), KeyboardState::new());