Gamepad (Linux): support battery level detection of NS Pro controller

This commit is contained in:
李通洲
2024-01-07 17:59:50 +08:00
parent cf096eb101
commit 2aa899d83c
+17 -1
View File
@@ -32,10 +32,26 @@ static void detectGamepad(FFlist* devices, FFstrbuf* name, FFstrbuf* path)
ffStrbufAppendS(path, "/capacity"); // /sys/class/input/jsX/device/device/power_supply/XXX/capacity
char capacity[32];
ssize_t nRead = ffReadFileData(path->chars, sizeof(capacity) - 1, capacity);
if (nRead > 0)
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, sizeof(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
}
}
}
}