master ohmu/fastfetch-openbsd / src / detection / mouse / mouse_linux.c
ohmu  ·  2026-10-15
 1#include "mouse.h"
 2#include "common/debug.h"
 3#include "common/io.h"
 4#include "common/strutil.h"
 5
 6const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */) {
 7    FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/input/");
 8    if (dirp == nullptr) {
 9        FF_DEBUG("opendir(\"/sys/class/input/\") failed: %s", strerror(errno));
10        return "opendir(\"/sys/class/input/\") == nullptr";
11    }
12
13    FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/sys/class/input/");
14    uint32_t baseLen = path.length;
15
16    struct dirent* entry;
17    while ((entry = readdir(dirp)) != nullptr) {
18        if (!ffStrStartsWith(entry->d_name, "mouse")) {
19            continue;
20        }
21        if (!ffCharIsDigit(entry->d_name[strlen("mouse")])) {
22            continue;
23        }
24
25        ffStrbufAppendS(&path, entry->d_name);
26        ffStrbufAppendS(&path, "/device/name");
27
28        FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
29        if (ffAppendFileBuffer(path.chars, &name)) {
30            ffStrbufTrimRightSpace(&name);
31            ffStrbufSubstrBefore(&path, path.length - 4);
32
33            FFMouseDevice* device = FF_LIST_ADD(FFMouseDevice, *devices);
34            ffStrbufInitMove(&device->name, &name);
35            ffStrbufInit(&device->serial);
36
37            ffStrbufAppendS(&path, "uniq");
38            if (ffAppendFileBuffer(path.chars, &device->serial)) {
39                ffStrbufTrimRightSpace(&device->serial);
40            }
41        }
42
43        ffStrbufSubstrBefore(&path, baseLen);
44    }
45
46    return nullptr;
47}