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.
  • Loading branch information
aadilshabier committed Feb 27, 2024
1 parent 34b9619 commit 49fa998
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,25 @@ 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 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()
}
};
Expand Down Expand Up @@ -309,7 +319,7 @@ 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 !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());
Expand Down Expand Up @@ -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 <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.",
),
);
)
.arg(arg!(-I --ignore-device <DEVICE_NAMES>)
.required(false)
.takes_value(true)
.multiple_occurrences(true)
.help(
"Specific keyboard devices to ignore. Seperate multiple devices with semicolon."
)
);
app
}

Expand Down

0 comments on commit 49fa998

Please sign in to comment.