Mouse / Keyboard: add new modules

This commit is contained in:
Carter Li
2024-10-09 09:45:09 +08:00
parent dbe7aaaa1b
commit a2b32ef564
19 changed files with 579 additions and 2 deletions
+9
View File
@@ -0,0 +1,9 @@
#include "fastfetch.h"
typedef struct FFMouseDevice
{
FFstrbuf serial;
FFstrbuf name;
} FFMouseDevice;
const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */);
+54
View File
@@ -0,0 +1,54 @@
#include "mouse.h"
#include "util/apple/cf_helpers.h"
#include "util/mallocHelper.h"
#include <IOKit/IOKitLib.h>
#include <IOKit/hid/IOHIDLib.h>
static void enumSet(IOHIDDeviceRef value, FFlist* results)
{
FFMouseDevice* device = (FFMouseDevice*) ffListAdd(results);
ffStrbufInit(&device->serial);
ffStrbufInit(&device->name);
CFStringRef manufacturer = IOHIDDeviceGetProperty(value, CFSTR(kIOHIDManufacturerKey));
ffCfStrGetString(manufacturer, &device->name);
CFStringRef product = IOHIDDeviceGetProperty(value, CFSTR(kIOHIDProductKey));
if (device->name.length)
{
ffCfStrGetString(product, &device->serial);
ffStrbufAppendC(&device->name, ' ');
ffStrbufAppend(&device->name, &device->serial);
}
else
{
ffCfStrGetString(product, &device->name);
}
CFStringRef serialNumber = IOHIDDeviceGetProperty(value, CFSTR(kIOHIDSerialNumberKey));
ffCfStrGetString(serialNumber, &device->serial);
}
const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */)
{
IOHIDManagerRef FF_CFTYPE_AUTO_RELEASE manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (IOHIDManagerOpen(manager, kIOHIDOptionsTypeNone) != kIOReturnSuccess)
return "IOHIDManagerOpen() failed";
CFDictionaryRef FF_CFTYPE_AUTO_RELEASE matching1 = CFDictionaryCreate(kCFAllocatorDefault, (const void **)(CFStringRef[]){
CFSTR(kIOHIDDeviceUsagePageKey),
CFSTR(kIOHIDDeviceUsageKey)
}, (const void **)(CFNumberRef[]){
ffCfCreateInt(kHIDPage_GenericDesktop),
ffCfCreateInt(kHIDUsage_GD_Mouse)
}, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
IOHIDManagerSetDeviceMatching(manager, matching1);
CFSetRef FF_CFTYPE_AUTO_RELEASE set = IOHIDManagerCopyDevices(manager);
if (set)
CFSetApplyFunction(set, (CFSetApplierFunction) &enumSet, devices);
IOHIDManagerClose(manager, kIOHIDOptionsTypeNone);
return NULL;
}
+44
View File
@@ -0,0 +1,44 @@
#include "mouse.h"
#include "common/io/io.h"
#include "util/stringUtils.h"
const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */)
{
FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/input/");
if (dirp == NULL)
return "opendir(\"/sys/class/input/\") == NULL";
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/sys/class/input/");
uint32_t baseLen = path.length;
struct dirent* entry;
while ((entry = readdir(dirp)) != NULL)
{
if (!ffStrStartsWith(entry->d_name, "mouse"))
continue;
if (!ffCharIsDigit(entry->d_name[strlen("mouse")]))
continue;
ffStrbufAppendS(&path, entry->d_name);
ffStrbufAppendS(&path, "/device/name");
FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
if (ffAppendFileBuffer(path.chars, &name))
{
ffStrbufTrimRightSpace(&name);
ffStrbufSubstrBefore(&path, path.length - 4);
FFMouseDevice* device = (FFMouseDevice*) ffListAdd(devices);
ffStrbufInitMove(&device->name, &name);
ffStrbufInit(&device->serial);
ffStrbufAppendS(&path, "uniq");
if (ffAppendFileBuffer(path.chars, &device->serial))
ffStrbufTrimRightSpace(&device->serial);
}
ffStrbufSubstrBefore(&path, baseLen);
}
return NULL;
}