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: better heuristic for detecting keyboard #269

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Loading