Skip to content

Commit

Permalink
feat: better heuristic for detecting keyboard
Browse files Browse the repository at this point in the history
### Changes
- Heuristic value decreases when features match that of a real keyboard
- A threshold, carefully chosen after some trial and error, is used to
predict if a device is a keyboard
  • Loading branch information
lavafroth committed Sep 14, 2024
1 parent f2bee30 commit 2b87aca
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,17 +418,44 @@ pub fn check_input_group() -> Result<(), Box<dyn Error>> {
}
}

pub fn check_device_is_keyboard(device: &Device) -> bool {
if device.supported_keys().map_or(false, |keys| keys.contains(Key::KEY_ENTER)) {
if device.name() == Some("swhkd virtual output") {
return false;
}
log::debug!("Keyboard: {}", device.name().unwrap(),);
true
} else {
log::trace!("Other: {}", device.name().unwrap(),);
false
fn check_device_is_keyboard(device: &Device) -> bool {
let name = device.name();
if name == Some("swhkd virtual output") {
return false;
}

let unique_name = device.unique_name();
let properties = device.properties();
let events = device.supported_events();

let mut heuristic = 5;

if let Some(name) = name {
heuristic -= name.to_lowercase().contains("keyboard") as i8;
}

if let Some(name) = unique_name {
heuristic -= name.to_lowercase().contains("keyboard") as i8;
}

// properties a keyboard generally has:
// keys
heuristic -= events.contains(evdev::EventType::KEY) as i8;
// leds (capslocks, numpads)
heuristic -= events.contains(evdev::EventType::LED) as i8;
// repeat: hold a key to spam it
heuristic -= events.contains(evdev::EventType::REPEAT) as i8;

heuristic += events.contains(evdev::EventType::ABSOLUTE) as i8;
heuristic += events.contains(evdev::EventType::RELATIVE) as i8;
heuristic += events.contains(evdev::EventType::SWITCH) as i8;
heuristic += properties.contains(evdev::PropType::POINTER) as i8;
heuristic += properties.contains(evdev::PropType::BUTTONPAD) as i8;

log::debug!("name: {:?}, heuristic: {}", name, heuristic);

// Increase this threshold to be more lenient
heuristic < 3
}

pub fn setup_swhkd(invoking_uid: u32, runtime_path: String) {
Expand Down

0 comments on commit 2b87aca

Please sign in to comment.