master ohmu/fastfetch-openbsd / src / detection / mouse / mouse_apple.c
ohmu  ·  2026-10-15
 1#include "mouse.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    FFMouseDevice* device = FF_LIST_ADD(FFMouseDevice, *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* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */) {
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_Mouse) }, 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}