From 543735fa27596df2c9f2e1a5fa3af967d6098648 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 --ignore-device 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 --ignore-device argument, it is not added. --- docs/swhkd.1.scd | 5 ++++- swhkd/src/daemon.rs | 34 ++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/docs/swhkd.1.scd b/docs/swhkd.1.scd index 8b3f3dc..2aa0837 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, --ignore-device* + 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 af75dd4..0b8ddbc 100644 --- a/swhkd/src/daemon.rs +++ b/swhkd/src/daemon.rs @@ -143,15 +143,25 @@ async fn main() -> Result<(), Box> { }; } - let arg_devices: Vec<&str> = args.values_of("device").unwrap_or_default().collect(); + let arg_add_devices: Vec<&str> = args.values_of("device").unwrap_or_default().collect(); + let arg_ignore_devices: Vec<&str> = + args.values_of("ignore-device").unwrap_or_default().collect(); 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() + evdev::enumerate() + .filter(|(_, dev)| { + !arg_ignore_devices.contains(&dev.name().unwrap_or("")) + && check_device_is_keyboard(dev) + }) + .collect() } else { evdev::enumerate() - .filter(|(_, dev)| arg_devices.contains(&dev.name().unwrap_or(""))) + .filter(|(_, dev)| { + !arg_ignore_devices.contains(&dev.name().unwrap_or("")) + && arg_add_devices.contains(&dev.name().unwrap_or("")) + }) .collect() } }; @@ -309,7 +319,7 @@ async fn main() -> Result<(), Box> { Ok(device) => device }; let name = device.name().unwrap_or("[unknown]"); - if arg_devices.contains(&name) || check_device_is_keyboard(&device) { + if !arg_ignore_devices.contains(&name) && (arg_add_devices.contains(&name) || check_device_is_keyboard(&device)) { log::info!("Device '{}' at '{}' added.", name, node); let _ = device.grab(); keyboard_states.insert(node.to_string(), KeyboardState::new()); @@ -490,14 +500,22 @@ pub fn set_command_line_args() -> Command<'static> { ) .arg(arg!(-d - -debug).required(false).help("Enable debug mode.")) .arg( - arg!(-D --device ) + arg!(-D --device ) .required(false) .takes_value(true) .multiple_occurrences(true) .help( - "Specific keyboard devices to use. Seperate multiple devices with semicolon.", + "Specific keyboard devices to use. Separate multiple devices with semicolon.", ), - ); + ) + .arg(arg!(-I --ignore-device ) + .required(false) + .takes_value(true) + .multiple_occurrences(true) + .help( + "Specific keyboard devices to ignore. Separate multiple devices with semicolon." + ) + ); app }