ohmu
·
2026-10-15
1#include "keyboard.h"
2#include "common/apple/cf_helpers.h"
3#include "common/debug.h"
4#include "common/mallocHelper.h"
5
6#include <IOKit/IOKitLib.h>
7#include <IOKit/hid/IOHIDLib.h>
8
9static void enumSet(IOHIDDeviceRef value, FFlist* results) {
10 FFKeyboardDevice* device = FF_LIST_ADD(FFKeyboardDevice, *results);
11 ffStrbufInit(&device->serial);
12 ffStrbufInit(&device->name);
13
14 CFStringRef product = IOHIDDeviceGetProperty(value, CFSTR(kIOHIDProductKey));
15 ffCfStrGetString(product, &device->name);
16
17 CFStringRef serialNumber = IOHIDDeviceGetProperty(value, CFSTR(kIOHIDSerialNumberKey));
18 ffCfStrGetString(serialNumber, &device->serial);
19}
20
21const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) {
22 FF_CFTYPE_AUTO_RELEASE IOHIDManagerRef manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
23 if (IOHIDManagerOpen(manager, kIOHIDOptionsTypeNone) != kIOReturnSuccess) {
24 FF_DEBUG("IOHIDManagerOpen() failed");
25 return "IOHIDManagerOpen() failed";
26 }
27
28 FF_CFTYPE_AUTO_RELEASE CFDictionaryRef matching1 = CFDictionaryCreate(kCFAllocatorDefault, (const void**) (CFStringRef[]) { CFSTR(kIOHIDDeviceUsagePageKey), CFSTR(kIOHIDDeviceUsageKey) }, (const void**) (CFNumberRef[]) { ffCfCreateInt(kHIDPage_GenericDesktop), ffCfCreateInt(kHIDUsage_GD_Keyboard) }, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
29 IOHIDManagerSetDeviceMatching(manager, matching1);
30
31 FF_CFTYPE_AUTO_RELEASE CFSetRef set = IOHIDManagerCopyDevices(manager);
32 if (set) {
33 CFSetApplyFunction(set, (CFSetApplierFunction) &enumSet, devices);
34 }
35 IOHIDManagerClose(manager, kIOHIDOptionsTypeNone);
36
37 return nullptr;
38}