ohmu
·
2026-10-15
1#include "mouse.h"
2#include "common/io.h"
3
4#include <stdio.h>
5#include <fcntl.h>
6#include <usbhid.h>
7
8#if __has_include(<dev/usb/usb_ioctl.h>)
9 #include <dev/usb/usb_ioctl.h> // FreeBSD
10#else
11 #include <bus/u4b/usb_ioctl.h> // DragonFly
12#endif
13
14#define MAX_UHID_MICE 64
15
16const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */) {
17 char path[16];
18 for (int i = 0; i < MAX_UHID_MICE; i++) {
19 snprintf(path, ARRAY_SIZE(path), "/dev/uhid%d", i);
20 FF_AUTO_CLOSE_FD int fd = open(path, O_RDONLY | O_CLOEXEC);
21 if (fd < 0) {
22 if (errno == ENOENT) {
23 break; // No more devices
24 }
25 continue; // Device not found
26 }
27 report_desc_t repDesc = hid_get_report_desc(fd);
28 if (!repDesc) {
29 continue;
30 }
31
32 int reportId = hid_get_report_id(fd);
33
34 struct hid_data* hData = hid_start_parse(repDesc, 0, reportId);
35 if (hData) {
36 struct hid_item hItem;
37 while (hid_get_item(hData, &hItem) > 0) {
38 if (HID_PAGE(hItem.usage) != 1 || HID_USAGE(hItem.usage) != 2) {
39 continue;
40 }
41
42 struct usb_device_info di;
43 if (ioctl(fd, USB_GET_DEVICEINFO, &di) != -1) {
44 FFMouseDevice* device = FF_LIST_ADD(FFMouseDevice, *devices);
45 ffStrbufInitS(&device->serial, di.udi_serial);
46 ffStrbufInitS(&device->name, di.udi_product);
47 }
48 }
49 hid_end_parse(hData);
50 }
51
52 hid_dispose_report_desc(repDesc);
53 }
54
55 return nullptr;
56}