From 22084691ff290ad2b8690e91e6ef3a595f42c137 Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Sun, 4 Dec 2022 00:14:42 -0800 Subject: [PATCH] fix(battery): ignore non-system power supplies Linux power_supply sysfs interface allows checking if the battery powers the whole system or a specific device/tree of devices with `scope` attribute[1]. We can use it to skip the non-system power supplies in the battery module and avoid adding HIDs or other peripheral devices to the total. The logic is based on UPower, where it is assumed that "Unknown" devices or devices without a `scope` are system power supplies. [1]: https://lore.kernel.org/lkml/alpine.LNX.2.00.1201031556460.24984@pobox.suse.cz/T/ --- src/modules/battery.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index bb06781e..54ed8ca5 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -107,6 +107,15 @@ void waybar::modules::Battery::refreshBatteries() { std::ifstream(node.path() / "type") >> type; if (!type.compare("Battery")) { + // Ignore non-system power supplies unless explicitly requested + if (!bat_defined && fs::exists(node.path() / "scope")) { + std::string scope; + std::ifstream(node.path() / "scope") >> scope; + if (g_ascii_strcasecmp(scope.data(), "device") == 0) { + continue; + } + } + check_map[node.path()] = true; auto search = batteries_.find(node.path()); if (search == batteries_.end()) {