proper probing of battery path

If battery driver in kernel behaves sane, it can be autodetected.
Every linux desktop manages to do this, so no excuse for
Inkbox. This simplifies addition of new devices.
This commit is contained in:
Andreas Kemnade 2023-10-20 21:19:38 +02:00 committed by Nicolas Mailloux
parent 0ffd09b261
commit 2853fca7cd

View file

@ -420,6 +420,32 @@ namespace {
}
return 0;
}
QString getPowerSupplyOfType(QString type) {
QDirIterator supplies("/sys/class/power_supply", QDirIterator::NoIteratorFlags);
while(supplies.hasNext()) {
supplies.next();
QString supply = supplies.filePath();
// badly classified device by some ntx kernels, ignore
if ((type == "Battery") &&
(supplies.fileName() == "mc13892_charger")) {
continue;
}
if (readFile(supplies.filePath() + "/type").trimmed() == type) {
return supply;
}
}
return NULL;
}
QString getBatteryFile() {
static QString capacity;
if (capacity.isNull()) {
QString battery = getPowerSupplyOfType("Battery");
if (!battery.isNull()) {
capacity = battery + "/capacity";
}
}
return capacity;
}
void getBatteryLevel() {
batteryLevelInt = 100;
batteryLevel = "100%";
@ -430,22 +456,15 @@ namespace {
batteryLevel.append("%");
}
}
else if(global::deviceID == "n249\n") {
if(QFile::exists("/sys/class/power_supply/rn5t618-battery/capacity")) {
batteryLevel = readFile("/sys/class/power_supply/rn5t618-battery/capacity").trimmed();
batteryLevelInt = batteryLevel.toInt();
batteryLevel.append("%");
}
}
else {
// probably superfluous, catched by default
if(QFile::exists("/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/capacity")) {
batteryLevel = readFile("/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/capacity").trimmed();
batteryLevelInt = batteryLevel.toInt();
batteryLevel.append("%");
}
else {
// It's for the Nia model C - but it's also a more regular/default path, so make it the fallback
QString path = "/sys/class/power_supply/battery/capacity";
QString path = getBatteryFile();
if(QFile::exists(path)) {
batteryLevel = readFile(path).trimmed();
batteryLevelInt = batteryLevel.toInt();