mirror of
https://github.com/aristocratos/btop.git
synced 2024-09-29 16:51:40 +13:00
Changed: Disable battery monitoring if it fails instead of exiting
This commit is contained in:
parent
83e9942d67
commit
7d86602ede
1 changed files with 46 additions and 41 deletions
|
@ -513,52 +513,57 @@ namespace Cpu {
|
||||||
|
|
||||||
//? Get paths to needed files and check for valid values on first run
|
//? Get paths to needed files and check for valid values on first run
|
||||||
if (batteries.empty() and has_battery) {
|
if (batteries.empty() and has_battery) {
|
||||||
if (fs::exists("/sys/class/power_supply")) {
|
try {
|
||||||
for (const auto& d : fs::directory_iterator("/sys/class/power_supply")) {
|
if (fs::exists("/sys/class/power_supply")) {
|
||||||
//? Only consider online power supplies of type Battery or UPS
|
for (const auto& d : fs::directory_iterator("/sys/class/power_supply")) {
|
||||||
//? see kernel docs for details on the file structure and contents
|
//? Only consider online power supplies of type Battery or UPS
|
||||||
//? https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-power
|
//? see kernel docs for details on the file structure and contents
|
||||||
battery new_bat;
|
//? https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-power
|
||||||
fs::path bat_dir;
|
battery new_bat;
|
||||||
try {
|
fs::path bat_dir;
|
||||||
if (not d.is_directory()
|
try {
|
||||||
or not fs::exists(d.path() / "type")
|
if (not d.is_directory()
|
||||||
or not fs::exists(d.path() / "present")
|
or not fs::exists(d.path() / "type")
|
||||||
or stoi(readfile(d.path() / "present")) != 1)
|
or not fs::exists(d.path() / "present")
|
||||||
|
or stoi(readfile(d.path() / "present")) != 1)
|
||||||
|
continue;
|
||||||
|
string dev_type = readfile(d.path() / "type");
|
||||||
|
if (is_in(dev_type, "Battery", "UPS")) {
|
||||||
|
bat_dir = d.path();
|
||||||
|
new_bat.base_dir = d.path();
|
||||||
|
new_bat.device_type = dev_type;
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
//? skip power supplies not conforming to the kernel standard
|
||||||
continue;
|
continue;
|
||||||
string dev_type = readfile(d.path() / "type");
|
|
||||||
if (is_in(dev_type, "Battery", "UPS")) {
|
|
||||||
bat_dir = d.path();
|
|
||||||
new_bat.base_dir = d.path();
|
|
||||||
new_bat.device_type = dev_type;
|
|
||||||
}
|
}
|
||||||
} catch (...) {
|
|
||||||
//? skip power supplies not conforming to the kernel standard
|
if (fs::exists(bat_dir / "energy_now")) new_bat.energy_now = bat_dir / "energy_now";
|
||||||
continue;
|
else if (fs::exists(bat_dir / "charge_now")) new_bat.energy_now = bat_dir / "charge_now";
|
||||||
|
else new_bat.use_energy = false;
|
||||||
|
|
||||||
|
if (fs::exists(bat_dir / "energy_full")) new_bat.energy_full = bat_dir / "energy_full";
|
||||||
|
else if (fs::exists(bat_dir / "charge_full")) new_bat.energy_full = bat_dir / "charge_full";
|
||||||
|
else new_bat.use_energy = false;
|
||||||
|
|
||||||
|
if (not new_bat.use_energy and not fs::exists(bat_dir / "capacity")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fs::exists(bat_dir / "power_now")) new_bat.power_now = bat_dir / "power_now";
|
||||||
|
else if (fs::exists(bat_dir / "current_now")) new_bat.power_now = bat_dir / "current_now";
|
||||||
|
|
||||||
|
if (fs::exists(bat_dir / "AC0/online")) new_bat.online = bat_dir / "AC0/online";
|
||||||
|
else if (fs::exists(bat_dir / "AC/online")) new_bat.online = bat_dir / "AC/online";
|
||||||
|
|
||||||
|
batteries[bat_dir.filename()] = new_bat;
|
||||||
|
Config::available_batteries.push_back(bat_dir.filename());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fs::exists(bat_dir / "energy_now")) new_bat.energy_now = bat_dir / "energy_now";
|
|
||||||
else if (fs::exists(bat_dir / "charge_now")) new_bat.energy_now = bat_dir / "charge_now";
|
|
||||||
else new_bat.use_energy = false;
|
|
||||||
|
|
||||||
if (fs::exists(bat_dir / "energy_full")) new_bat.energy_full = bat_dir / "energy_full";
|
|
||||||
else if (fs::exists(bat_dir / "charge_full")) new_bat.energy_full = bat_dir / "charge_full";
|
|
||||||
else new_bat.use_energy = false;
|
|
||||||
|
|
||||||
if (not new_bat.use_energy and not fs::exists(bat_dir / "capacity")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fs::exists(bat_dir / "power_now")) new_bat.power_now = bat_dir / "power_now";
|
|
||||||
else if (fs::exists(bat_dir / "current_now")) new_bat.power_now = bat_dir / "current_now";
|
|
||||||
|
|
||||||
if (fs::exists(bat_dir / "AC0/online")) new_bat.online = bat_dir / "AC0/online";
|
|
||||||
else if (fs::exists(bat_dir / "AC/online")) new_bat.online = bat_dir / "AC/online";
|
|
||||||
|
|
||||||
batteries[bat_dir.filename()] = new_bat;
|
|
||||||
Config::available_batteries.push_back(bat_dir.filename());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (...) {
|
||||||
|
batteries.clear();
|
||||||
|
}
|
||||||
if (batteries.empty()) {
|
if (batteries.empty()) {
|
||||||
has_battery = false;
|
has_battery = false;
|
||||||
return {0, 0, ""};
|
return {0, 0, ""};
|
||||||
|
|
Loading…
Reference in a new issue