Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] add argument to ignore devices #245

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, --ignoredevice* <DEVICE_NAMES>
Manually set the keyboard devices to ignore. Can occur multiple times.

# SIGNALS

- Reload config file: `sudo pkill -HUP swhkd`
Expand Down
24 changes: 17 additions & 7 deletions swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ struct Args {
/// Take a list of devices from the user
#[arg(short = 'D', long, num_args = 0.., value_delimiter = ' ')]
device: Vec<String>,

/// Take a list of devices to ignore from the user
#[arg(short = 'I', long, num_args = 0.., value_delimiter = ' ')]
ignoredevice: Vec<String>,
}

#[tokio::main]
Expand Down Expand Up @@ -110,16 +114,22 @@ async fn main() -> Result<(), Box<dyn Error>> {

let mut modes = load_config();
let mut mode_stack: Vec<usize> = vec![0];
let arg_devices: Vec<String> = 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()
}
};

Expand Down Expand Up @@ -271,8 +281,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
},
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());
Expand Down
Loading