master ohmu/fastfetch-openbsd / src / detection / keyboard / keyboard_bsd.c
ohmu  ·  2026-10-15
 1#include "keyboard.h"
 2#include "common/debug.h"
 3#include "common/io.h"
 4
 5#include <string.h>
 6#include <stdio.h>
 7#include <fcntl.h>
 8#include <usbhid.h>
 9#include <sys/kbio.h>
10
11#if __has_include(<dev/usb/usb_ioctl.h>)
12    #include <dev/usb/usb_ioctl.h> // FreeBSD
13#else
14    #include <bus/u4b/usb_ioctl.h> // DragonFly
15#endif
16
17static const char* detectByIoctl(FFlist* devices) {
18    keyboard_info_t kbdInfo;
19    if (ioctl(STDIN_FILENO, KDGKBINFO, &kbdInfo) != 0) {
20        FF_DEBUG("ioctl(KDGKBINFO) failed: %s", strerror(errno));
21        return "ioctl(KDGKBINFO) failed";
22    }
23
24    FFKeyboardDevice* device = FF_LIST_ADD(FFKeyboardDevice, *devices);
25
26    switch (kbdInfo.kb_type) {
27        case KB_84:
28            ffStrbufInitS(&device->name, "AT 84-key keyboard");
29            break;
30        case KB_101:
31            ffStrbufInitS(&device->name, "AT 101/102-key keyboard");
32            break;
33        default:
34            ffStrbufInitS(&device->name, "Unknown keyboard");
35            break;
36    }
37
38    ffStrbufAppendF(&device->name, " (kbd%d)", kbdInfo.kb_index);
39
40    ffStrbufInit(&device->serial);
41    return nullptr;
42}
43
44#define MAX_UHID_KBDS 64
45
46static const char* detectByUsbhid(FFlist* devices) {
47    char path[16];
48    for (int i = 0; i < MAX_UHID_KBDS; i++) {
49        snprintf(path, ARRAY_SIZE(path), "/dev/uhid%d", i);
50        FF_AUTO_CLOSE_FD int fd = open(path, O_RDONLY | O_CLOEXEC);
51        if (fd < 0) {
52            if (errno == ENOENT) {
53                break; // No more devices
54            }
55            continue; // Device not found
56        }
57
58        report_desc_t repDesc = hid_get_report_desc(fd);
59        if (!repDesc) {
60            continue;
61        }
62
63        int reportId = hid_get_report_id(fd);
64
65        struct hid_data* hData = hid_start_parse(repDesc, 0, reportId);
66        if (hData) {
67            struct hid_item hItem;
68            while (hid_get_item(hData, &hItem) > 0) {
69                if (HID_PAGE(hItem.usage) != 1 || HID_USAGE(hItem.usage) != 6) {
70                    continue;
71                }
72
73                struct usb_device_info di;
74                if (ioctl(fd, USB_GET_DEVICEINFO, &di) != -1) {
75                    FFKeyboardDevice* device = FF_LIST_ADD(FFKeyboardDevice, *devices);
76                    ffStrbufInitS(&device->serial, di.udi_serial);
77                    ffStrbufInitS(&device->name, di.udi_product);
78                }
79            }
80            hid_end_parse(hData);
81        }
82
83        hid_dispose_report_desc(repDesc);
84    }
85
86    return nullptr;
87}
88
89const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) {
90    detectByUsbhid(devices);
91    if (devices->length > 0) {
92        return nullptr;
93    }
94    return detectByIoctl(devices);
95}