improved battery detection

- new considering all power supplies (instead of path name filtering)
- only consider power supplies that are currently present
- only consider power supplies of type Batter or UPS
This commit is contained in:
Jan Günter 2021-10-17 16:31:25 +02:00
parent 2ee1333226
commit 966c9f5e5e
1 changed files with 10 additions and 1 deletions

View File

@ -477,7 +477,16 @@ namespace Cpu {
if (bat_dir.empty() and has_battery) {
if (fs::exists("/sys/class/power_supply")) {
for (const auto& d : fs::directory_iterator("/sys/class/power_supply")) {
if (const string dir_name = d.path().filename(); d.is_directory() and (dir_name.starts_with("BAT") or s_contains(str_to_lower(dir_name), "battery"))) {
//? Only consider online power supplies of type Battery or UPS
//? see kernel docs for details on the file structure and contents
//? https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-power
if (not d.is_directory()
or not fs::exists(d.path() / "type")
or not fs::exists(d.path() / "present")
or stoi(readfile(d.path() / "present")) != 1)
continue;
string type = readfile(d.path() / "type");
if (type == "Battery" or type == "UPS") {
bat_dir = d.path();
break;
}