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

Identify more devices as keyboards #1419

Merged
merged 2 commits into from
Dec 17, 2024
Merged
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
32 changes: 20 additions & 12 deletions src/oskbd/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![cfg_attr(feature = "simulated_output", allow(dead_code, unused_imports))]

pub use evdev::BusType;
use evdev::{uinput, Device, EventType, InputEvent, PropType, RelativeAxisType};
use evdev::{uinput, Device, EventType, InputEvent, Key, PropType, RelativeAxisType};
use inotify::{Inotify, WatchMask};
use mio::{unix::SourceFd, Events, Interest, Poll, Token};
use nix::ioctl_read_buf;
Expand Down Expand Up @@ -255,13 +255,10 @@ enum DeviceType {
}

pub fn is_input_device(device: &Device, detect_mode: DeviceDetectMode) -> bool {
use evdev::Key;
if device.name() == Some("kanata") {
return false;
}
let is_keyboard = device
.supported_keys()
.map_or(false, |keys| keys.contains(Key::KEY_ENTER));
let is_keyboard = device.supported_keys().map_or(false, has_keyboard_keys);
let is_mouse = device
.supported_relative_axes()
.map_or(false, |axes| axes.contains(RelativeAxisType::REL_X));
Expand All @@ -273,13 +270,6 @@ pub fn is_input_device(device: &Device, detect_mode: DeviceDetectMode) -> bool {
};
let device_name = device.name().unwrap_or("unknown device name");
match (detect_mode, device_type) {
(_, DeviceType::Other) => {
log::debug!(
"Use for input autodetect: false. Non-input device: {}",
device_name,
);
false
}
(DeviceDetectMode::Any, _)
| (DeviceDetectMode::KeyboardMice, DeviceType::Keyboard | DeviceType::KeyboardMouse)
| (DeviceDetectMode::KeyboardOnly, DeviceType::Keyboard) => {
Expand All @@ -292,6 +282,13 @@ pub fn is_input_device(device: &Device, detect_mode: DeviceDetectMode) -> bool {
);
use_input
}
(_, DeviceType::Other) => {
log::debug!(
"Use for input autodetect: false. Non-input device: {}",
device_name,
);
false
}
_ => {
let use_input = false;
log::debug!(
Expand All @@ -305,6 +302,17 @@ pub fn is_input_device(device: &Device, detect_mode: DeviceDetectMode) -> bool {
}
}

fn has_keyboard_keys(keys: &evdev::AttributeSetRef<Key>) -> bool {
const SENSIBLE_KEYBOARD_SCANCODE_LOWER_BOUND: u16 = 1;
// The next one is power button. Some keyboards have it,
// but so does the power button...
const SENSIBLE_KEYBOARD_SCANCODE_UPPER_BOUND: u16 = 115;
let mut sensible_keyboard_keys = (SENSIBLE_KEYBOARD_SCANCODE_LOWER_BOUND
..=SENSIBLE_KEYBOARD_SCANCODE_UPPER_BOUND)
.map(Key::new);
sensible_keyboard_keys.any(|k| keys.contains(k))
}

impl TryFrom<InputEvent> for KeyEvent {
type Error = ();
fn try_from(item: InputEvent) -> Result<Self, Self::Error> {
Expand Down
Loading