From e23633ec562dc781a543c8e2b6ed0007a86e7158 Mon Sep 17 00:00:00 2001 From: illiliti Date: Sun, 6 Jun 2021 10:26:35 +0300 Subject: [PATCH] udev_device.c: more accurate keys/keyboard detection --- udev_device.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/udev_device.c b/udev_device.c index c61aab2..136b61c 100644 --- a/udev_device.c +++ b/udev_device.c @@ -403,6 +403,7 @@ static void set_properties_from_evdev(struct udev_device *udev_device) unsigned long ev_bits[BITS_TO_LONGS(EV_CNT)] = {0}; struct udev_device *parent; const char *subsystem; + unsigned long bit; subsystem = udev_device_get_subsystem(udev_device); @@ -481,13 +482,25 @@ static void set_properties_from_evdev(struct udev_device *udev_device) } } - // TODO do not assume keyboard if EV_KEY - if (test_bit(ev_bits, EV_KEY)) { + if (!test_bit(ev_bits, EV_KEY)) { + return; + } + + // iterate over key bits until BTN_* block is hit and test if bitmask has KEY_*. + // this way, we can check if device has keys or buttons. + // https://github.com/torvalds/linux/blob/f5b6eb1e018203913dfefcf6fa988649ad11ad6e/include/uapi/linux/input-event-codes.h#L76-L338 + for (bit = KEY_ESC; bit < BTN_MISC; bit++) { + if (!test_bit(key_bits, bit)) { + continue; + } + udev_list_entry_add(&udev_device->properties, "ID_INPUT_KEY", "1", 0); if (test_bit(key_bits, KEY_ENTER)) { udev_list_entry_add(&udev_device->properties, "ID_INPUT_KEYBOARD", "1", 0); } + + return; } }