summaryrefslogtreecommitdiffstats
path: root/src/detection/gamepad/gamepad_linux.c
blob: 6bb4ee214cf1c241cb17f7e68ab62a9aefa7d3f6 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "gamepad.h"
#include "common/io.h"
#include "common/strutil.h"

static void detectGamepad(FFlist* devices, FFstrbuf* name, FFstrbuf* path) {
    uint32_t baseLen = path->length;
    FFGamepadDevice* device = FF_LIST_ADD(FFGamepadDevice, *devices);
    ffStrbufInit(&device->serial);
    ffStrbufInitMove(&device->name, name);
    device->battery = 0;

    ffStrbufAppendS(path, "uniq");
    if (ffAppendFileBuffer(path->chars, &device->serial)) {
        ffStrbufTrimRightSpace(&device->serial);
    }

    ffStrbufSubstrBefore(path, baseLen);
    ffStrbufAppendS(path, "device/power_supply/"); // /sys/class/input/jsX/device/device/power_supply

    FF_AUTO_CLOSE_DIR DIR* dirp = opendir(path->chars);
    if (dirp) {
        struct dirent* entry;
        while ((entry = readdir(dirp)) != NULL) {
            if (entry->d_name[0] == '.' || (entry->d_type != DT_DIR && entry->d_type != DT_UNKNOWN)) {
                continue;
            }
            ffStrbufAppendS(path, entry->d_name);
            ffStrbufAppendS(path, "/capacity"); // /sys/class/input/jsX/device/device/power_supply/XXX/capacity
            char capacity[32];
            ssize_t nRead = ffReadFileData(path->chars, ARRAY_SIZE(capacity) - 1, capacity);
            if (nRead > 0) // Tested with a PS4 controller
            {
                capacity[nRead] = '\0';
                device->battery = (uint8_t) strtoul(capacity, NULL, 10);
                break;
            }

            ffStrbufAppendS(path, "_level");
            nRead = ffReadFileData(path->chars, ARRAY_SIZE(capacity) - 1, capacity);
            if (nRead > 0) // Tested with a NS Pro controller
            {
                // https://github.com/torvalds/linux/blob/52b1853b080a082ec3749c3a9577f6c71b1d4a90/drivers/power/supply/power_supply_sysfs.c#L124
                switch (capacity[0]) {
                    case 'C':
                        device->battery = 1;
                        break; // Critical
                    case 'L':
                        device->battery = 25;
                        break; // Low
                    case 'N':
                        device->battery = 50;
                        break; // Normal
                    case 'H':
                        device->battery = 75;
                        break; // High
                    case 'F':
                        device->battery = 100;
                        break; // Full
                }
            }
        }
    }
}

const char* ffDetectGamepad(FFlist* devices /* List of FFGamepadDevice */) {
    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, "js")) {
            continue;
        }
        if (!ffCharIsDigit(entry->d_name[strlen("js")])) {
            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);
            detectGamepad(devices, &name, &path);
        }

        ffStrbufSubstrBefore(&path, baseLen);
    }

    return NULL;
}