summaryrefslogtreecommitdiffstats
path: root/src/detection/mouse/mouse_linux.c
blob: 939c332a129d0a22527a09449af204e76441757a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "mouse.h"
#include "common/io.h"
#include "common/strutil.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 = FF_LIST_ADD(FFMouseDevice, *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;
}