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
+1 -1
View File
@@ -68,7 +68,7 @@ const char* ffDetectGamepad(FFlist* devices /* List of FFGamepadDevice */)
{
if (!ffStrStartsWith(entry->d_name, "js"))
continue;
if (!ffCharIsDigit(entry->d_name[2]))
if (!ffCharIsDigit(entry->d_name[strlen("js")]))
continue;
ffStrbufAppendS(&path, entry->d_name);
+9
View File
@@ -0,0 +1,9 @@
#include "fastfetch.h"
typedef struct FFKeyboardDevice
{
FFstrbuf serial;
FFstrbuf name;
} FFKeyboardDevice;
const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */);
+54
View File
@@ -0,0 +1,54 @@
#include "keyboard.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)
{
FFKeyboardDevice* device = (FFKeyboardDevice*) 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* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */)
{
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_Keyboard)
}, 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;
}
+57
View File
@@ -0,0 +1,57 @@
#include "keyboard.h"
#include "common/io/io.h"
#include "util/stringUtils.h"
const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */)
{
// There is no /sys/class/input/kbd* on Linux
FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/dev/input/by-path/");
if (dirp == NULL)
return "opendir(\"/dev/input/by-path/\") == NULL";
uint64_t flags = 0;
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreate();
struct dirent* entry;
while ((entry = readdir(dirp)) != NULL)
{
if (!ffStrEndsWith(entry->d_name, "-event-kbd"))
continue;
char buffer[32]; // `../eventX`
ssize_t len = readlinkat(dirfd(dirp), entry->d_name, buffer, sizeof(buffer) - 1);
if (len != strlen("../eventX") || !ffStrStartsWith(buffer, "../event")) continue;
buffer[len] = 0;
const char* eventid = buffer + strlen("../event");
char* pend = NULL;
uint32_t index = (uint32_t) strtoul(eventid, &pend, 10);
if (pend == eventid) continue;
// Ignore duplicate entries
if (flags & (1 << index))
continue;
flags |= (1 << index);
ffStrbufSetF(&path, "/sys/class/input/event%s/device/name", eventid);
FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
if (ffAppendFileBuffer(path.chars, &name))
{
ffStrbufTrimRightSpace(&name);
ffStrbufSubstrBefore(&path, path.length - 4);
FFKeyboardDevice* device = (FFKeyboardDevice*) ffListAdd(devices);
ffStrbufInitMove(&device->name, &name);
ffStrbufInit(&device->serial);
ffStrbufAppendS(&path, "uniq");
if (ffAppendFileBuffer(path.chars, &device->serial))
ffStrbufTrimRightSpace(&device->serial);
}
}
return NULL;
}
+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;
}