summaryrefslogtreecommitdiffstats
path: root/src/detection/keyboard/keyboard_bsd.c
diff options
context:
space:
mode:
authorsumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
committersumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
commit76424950e373d3b04ac3dd13019151bfba3e8423 (patch)
tree4c3cfdbda039e592b9186be3e28f8d7cfd439e8a /src/detection/keyboard/keyboard_bsd.c
Add the files
Diffstat (limited to 'src/detection/keyboard/keyboard_bsd.c')
-rw-r--r--src/detection/keyboard/keyboard_bsd.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/detection/keyboard/keyboard_bsd.c b/src/detection/keyboard/keyboard_bsd.c
new file mode 100644
index 0000000..4b0ee57
--- /dev/null
+++ b/src/detection/keyboard/keyboard_bsd.c
@@ -0,0 +1,92 @@
+#include "keyboard.h"
+#include "common/io.h"
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <usbhid.h>
+#include <sys/kbio.h>
+
+#if __has_include(<dev/usb/usb_ioctl.h>)
+ #include <dev/usb/usb_ioctl.h> // FreeBSD
+#else
+ #include <bus/u4b/usb_ioctl.h> // DragonFly
+#endif
+
+static const char* detectByIoctl(FFlist* devices) {
+ keyboard_info_t kbdInfo;
+ if (ioctl(STDIN_FILENO, KDGKBINFO, &kbdInfo) != 0) {
+ return "ioctl(KDGKBINFO) failed";
+ }
+
+ FFKeyboardDevice* device = FF_LIST_ADD(FFKeyboardDevice, *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_KBDS; i++) {
+ snprintf(path, ARRAY_SIZE(path), "/dev/uhid%d", i);
+ FF_AUTO_CLOSE_FD int fd = open(path, O_RDONLY | O_CLOEXEC);
+ if (fd < 0) {
+ if (errno == ENOENT) {
+ break; // No more devices
+ }
+ continue; // Device not found
+ }
+
+ report_desc_t repDesc = hid_get_report_desc(fd);
+ if (!repDesc) {
+ continue;
+ }
+
+ int reportId = hid_get_report_id(fd);
+
+ struct hid_data* hData = hid_start_parse(repDesc, 0, reportId);
+ if (hData) {
+ struct hid_item hItem;
+ while (hid_get_item(hData, &hItem) > 0) {
+ if (HID_PAGE(hItem.usage) != 1 || HID_USAGE(hItem.usage) != 6) {
+ continue;
+ }
+
+ struct usb_device_info di;
+ if (ioctl(fd, USB_GET_DEVICEINFO, &di) != -1) {
+ FFKeyboardDevice* device = FF_LIST_ADD(FFKeyboardDevice, *devices);
+ ffStrbufInitS(&device->serial, di.udi_serial);
+ ffStrbufInitS(&device->name, di.udi_product);
+ }
+ }
+ hid_end_parse(hData);
+ }
+
+ hid_dispose_report_desc(repDesc);
+ }
+
+ return NULL;
+}
+
+const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) {
+ detectByUsbhid(devices);
+ if (devices->length > 0) {
+ return NULL;
+ }
+ return detectByIoctl(devices);
+}