From bdaf303da4f6f505e9ef30add3e7d93a2fb8ca4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 27 Mar 2025 14:14:05 +0800 Subject: [PATCH] Keyboard (FreeBSD): fall back to KDGKBINFO if usbhid fails --- src/detection/keyboard/keyboard_bsd.c | 42 +++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/detection/keyboard/keyboard_bsd.c b/src/detection/keyboard/keyboard_bsd.c index 31dc3aa24..3d9fb66f5 100644 --- a/src/detection/keyboard/keyboard_bsd.c +++ b/src/detection/keyboard/keyboard_bsd.c @@ -4,6 +4,7 @@ #include #include #include +#include #if __has_include() #include // FreeBSD @@ -11,12 +12,38 @@ #include // DragonFly #endif -#define MAX_UHID_JOYS 64 +static const char* detectByIoctl(FFlist* devices) +{ + keyboard_info_t kbdInfo; + if (ioctl(STDIN_FILENO, KDGKBINFO, &kbdInfo) != 0) + return "ioctl(KDGKBINFO) failed"; -const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) + FFKeyboardDevice* device = (FFKeyboardDevice*) ffListAdd(devices); + + switch (kbdInfo.kb_type) { + case KB_84: + ffStrbufInitS(&device->name, "AT 84-key keyboard"); + break; + case KB_101: + ffStrbufInitS(&device->name, "AT 101/102-key keyboard"); + break; + default: + ffStrbufInitS(&device->name, "Unknown keyboard"); + break; + } + + ffStrbufAppendF(&device->name, " (kbd%d)", kbdInfo.kb_index); + + ffStrbufInit(&device->serial); + return NULL; +} + +#define MAX_UHID_KBDS 64 + +static const char* detectByUsbhid(FFlist* devices) { char path[16]; - for (int i = 0; i < MAX_UHID_JOYS; i++) + for (int i = 0; i < MAX_UHID_KBDS; i++) { snprintf(path, ARRAY_SIZE(path), "/dev/uhid%d", i); FF_AUTO_CLOSE_FD int fd = open(path, O_RDONLY | O_CLOEXEC); @@ -43,6 +70,7 @@ const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) ffStrbufInitS(&device->name, di.udi_product); } } + hid_end_parse(hData); } hid_dispose_report_desc(repDesc); @@ -50,3 +78,11 @@ const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) return NULL; } + +const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) +{ + detectByUsbhid(devices); + if (devices->length > 0) + return NULL; + return detectByIoctl(devices); +}