Skip to content

Commit

Permalink
[feat] add argument to ignore devices
Browse files Browse the repository at this point in the history
--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.

Also replaced default name from "" to "[unknown]" to prevent devices
without valid names from being matched by empty device arguments.
  • Loading branch information
aadilshabier committed Feb 28, 2024
1 parent 994b74f commit 9fce949
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
5 changes: 4 additions & 1 deletion docs/swhkd.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ More about the config file syntax in `swhkd(5)`
*-d*, *--debug*
Enable debug mode.

*-D, --device* <DEVICE_NAME>
*-D, --device* <DEVICE_NAMES>
Manually set the keyboard devices to use. Can occur multiple times.

*-I, --ignore-device* <DEVICE_NAMES>
Manually set the keyboard devices to ignore. Can occur multiple times.

# SIGNALS

- Reload config file: `sudo pkill -HUP swhkd`
Expand Down
33 changes: 23 additions & 10 deletions swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,21 @@ async fn main() -> Result<(), Box<dyn Error>> {
};
}

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 to_add = |dev: &Device| arg_add_devices.contains(&dev.name().unwrap_or("[unknown]"));
let to_ignore = |dev: &Device| arg_ignore_devices.contains(&dev.name().unwrap_or("[unknown]"));

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("")))
.filter(|(_, dev)| !to_ignore(dev) && check_device_is_keyboard(dev))
.collect()
} else {
evdev::enumerate().filter(|(_, dev)| !to_ignore(dev) && to_add(dev)).collect()
}
};

Expand Down Expand Up @@ -308,8 +313,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
},
Ok(device) => device
};
let name = device.name().unwrap_or("[unknown]");
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());
Expand Down Expand Up @@ -490,14 +495,22 @@ pub fn set_command_line_args() -> Command<'static> {
)
.arg(arg!(-d - -debug).required(false).help("Enable debug mode."))
.arg(
arg!(-D --device <DEVICE_NAME>)
arg!(-D --device <DEVICE_NAMES>)
.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 <DEVICE_NAMES>)
.required(false)
.takes_value(true)
.multiple_occurrences(true)
.help(
"Specific keyboard devices to ignore. Separate multiple devices with semicolon."
)
);
app
}

Expand Down

0 comments on commit 9fce949

Please sign in to comment.