2024-10-10 09:30:20 +08:00
|
|
|
#include "keyboard.h"
|
2026-01-06 09:48:38 +08:00
|
|
|
#include "common/io.h"
|
2024-10-10 09:30:20 +08:00
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <usbhid.h>
|
2025-03-27 14:14:05 +08:00
|
|
|
#include <sys/kbio.h>
|
2024-11-03 08:35:11 +08:00
|
|
|
|
|
|
|
|
#if __has_include(<dev/usb/usb_ioctl.h>)
|
2026-04-15 16:16:46 +08:00
|
|
|
#include <dev/usb/usb_ioctl.h> // FreeBSD
|
2024-11-03 08:35:11 +08:00
|
|
|
#else
|
2026-04-15 16:16:46 +08:00
|
|
|
#include <bus/u4b/usb_ioctl.h> // DragonFly
|
2024-11-03 08:35:11 +08:00
|
|
|
#endif
|
2024-10-10 09:30:20 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
static const char* detectByIoctl(FFlist* devices) {
|
2025-03-27 14:14:05 +08:00
|
|
|
keyboard_info_t kbdInfo;
|
2026-03-29 09:28:49 +08:00
|
|
|
if (ioctl(STDIN_FILENO, KDGKBINFO, &kbdInfo) != 0) {
|
2025-03-27 14:14:05 +08:00
|
|
|
return "ioctl(KDGKBINFO) failed";
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2024-10-10 09:30:20 +08:00
|
|
|
|
2026-04-14 10:41:18 +08:00
|
|
|
FFKeyboardDevice* device = FF_LIST_ADD(FFKeyboardDevice, *devices);
|
2025-03-27 14:14:05 +08:00
|
|
|
|
|
|
|
|
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);
|
2026-07-10 18:08:04 +08:00
|
|
|
return nullptr;
|
2025-03-27 14:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define MAX_UHID_KBDS 64
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
static const char* detectByUsbhid(FFlist* devices) {
|
2024-10-10 09:30:20 +08:00
|
|
|
char path[16];
|
2026-03-29 09:28:49 +08:00
|
|
|
for (int i = 0; i < MAX_UHID_KBDS; i++) {
|
2024-10-18 16:15:48 +08:00
|
|
|
snprintf(path, ARRAY_SIZE(path), "/dev/uhid%d", i);
|
2024-10-10 09:30:20 +08:00
|
|
|
FF_AUTO_CLOSE_FD int fd = open(path, O_RDONLY | O_CLOEXEC);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
|
if (errno == ENOENT) {
|
2025-03-27 14:40:33 +08:00
|
|
|
break; // No more devices
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2025-03-27 14:40:33 +08:00
|
|
|
continue; // Device not found
|
|
|
|
|
}
|
2024-10-10 09:30:20 +08:00
|
|
|
|
|
|
|
|
report_desc_t repDesc = hid_get_report_desc(fd);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (!repDesc) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-10-10 09:30:20 +08:00
|
|
|
|
|
|
|
|
int reportId = hid_get_report_id(fd);
|
|
|
|
|
|
|
|
|
|
struct hid_data* hData = hid_start_parse(repDesc, 0, reportId);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (hData) {
|
2024-10-10 09:30:20 +08:00
|
|
|
struct hid_item hItem;
|
2026-03-29 09:28:49 +08:00
|
|
|
while (hid_get_item(hData, &hItem) > 0) {
|
|
|
|
|
if (HID_PAGE(hItem.usage) != 1 || HID_USAGE(hItem.usage) != 6) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-10-10 09:30:20 +08:00
|
|
|
|
|
|
|
|
struct usb_device_info di;
|
2026-03-29 09:28:49 +08:00
|
|
|
if (ioctl(fd, USB_GET_DEVICEINFO, &di) != -1) {
|
2026-04-14 10:41:18 +08:00
|
|
|
FFKeyboardDevice* device = FF_LIST_ADD(FFKeyboardDevice, *devices);
|
2024-10-10 09:30:20 +08:00
|
|
|
ffStrbufInitS(&device->serial, di.udi_serial);
|
|
|
|
|
ffStrbufInitS(&device->name, di.udi_product);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-27 14:14:05 +08:00
|
|
|
hid_end_parse(hData);
|
2024-10-10 09:30:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hid_dispose_report_desc(repDesc);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-10 18:08:04 +08:00
|
|
|
return nullptr;
|
2024-10-10 09:30:20 +08:00
|
|
|
}
|
2025-03-27 14:14:05 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) {
|
2025-03-27 14:14:05 +08:00
|
|
|
detectByUsbhid(devices);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (devices->length > 0) {
|
2026-07-10 18:08:04 +08:00
|
|
|
return nullptr;
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2025-03-27 14:14:05 +08:00
|
|
|
return detectByIoctl(devices);
|
|
|
|
|
}
|