master ohmu/fastfetch-openbsd / src / detection / gamepad / gamepad_linux.c
ohmu  ·  2026-10-15
 1#include "gamepad.h"
 2#include "common/debug.h"
 3#include "common/io.h"
 4#include "common/strutil.h"
 5
 6static void detectGamepad(FFlist* devices, FFstrbuf* name, FFstrbuf* path) {
 7    uint32_t baseLen = path->length;
 8    FFGamepadDevice* device = FF_LIST_ADD(FFGamepadDevice, *devices);
 9    ffStrbufInit(&device->serial);
10    ffStrbufInitMove(&device->name, name);
11    device->battery = 0;
12
13    ffStrbufAppendS(path, "uniq");
14    if (ffAppendFileBuffer(path->chars, &device->serial)) {
15        ffStrbufTrimRightSpace(&device->serial);
16    }
17
18    ffStrbufSubstrBefore(path, baseLen);
19    ffStrbufAppendS(path, "device/power_supply/"); // /sys/class/input/jsX/device/device/power_supply
20
21    FF_AUTO_CLOSE_DIR DIR* dirp = opendir(path->chars);
22    if (dirp) {
23        struct dirent* entry;
24        while ((entry = readdir(dirp)) != nullptr) {
25            if (entry->d_name[0] == '.' || (entry->d_type != DT_DIR && entry->d_type != DT_UNKNOWN)) {
26                continue;
27            }
28            ffStrbufAppendS(path, entry->d_name);
29            ffStrbufAppendS(path, "/capacity"); // /sys/class/input/jsX/device/device/power_supply/XXX/capacity
30            char capacity[32];
31            ssize_t nRead = ffReadFileData(path->chars, ARRAY_SIZE(capacity) - 1, capacity);
32            if (nRead > 0) // Tested with a PS4 controller
33            {
34                capacity[nRead] = '\0';
35                device->battery = (uint8_t) strtoul(capacity, nullptr, 10);
36                break;
37            }
38
39            ffStrbufAppendS(path, "_level");
40            nRead = ffReadFileData(path->chars, ARRAY_SIZE(capacity) - 1, capacity);
41            if (nRead > 0) // Tested with a NS Pro controller
42            {
43                // https://github.com/torvalds/linux/blob/52b1853b080a082ec3749c3a9577f6c71b1d4a90/drivers/power/supply/power_supply_sysfs.c#L124
44                switch (capacity[0]) {
45                    case 'C':
46                        device->battery = 1;
47                        break; // Critical
48                    case 'L':
49                        device->battery = 25;
50                        break; // Low
51                    case 'N':
52                        device->battery = 50;
53                        break; // Normal
54                    case 'H':
55                        device->battery = 75;
56                        break; // High
57                    case 'F':
58                        device->battery = 100;
59                        break; // Full
60                }
61            }
62        }
63    }
64}
65
66const char* ffDetectGamepad(FFlist* devices /* List of FFGamepadDevice */) {
67    FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/input/");
68    if (dirp == nullptr) {
69        FF_DEBUG("opendir(\"/sys/class/input/\") failed: %s", strerror(errno));
70        return "opendir(\"/sys/class/input/\") == nullptr";
71    }
72
73    FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/sys/class/input/");
74    uint32_t baseLen = path.length;
75
76    struct dirent* entry;
77    while ((entry = readdir(dirp)) != nullptr) {
78        if (!ffStrStartsWith(entry->d_name, "js")) {
79            continue;
80        }
81        if (!ffCharIsDigit(entry->d_name[strlen("js")])) {
82            continue;
83        }
84
85        ffStrbufAppendS(&path, entry->d_name);
86        ffStrbufAppendS(&path, "/device/name");
87
88        FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
89        if (ffAppendFileBuffer(path.chars, &name)) {
90            ffStrbufTrimRightSpace(&name);
91            ffStrbufSubstrBefore(&path, path.length - 4);
92            detectGamepad(devices, &name, &path);
93        }
94
95        ffStrbufSubstrBefore(&path, baseLen);
96    }
97
98    return nullptr;
99}