From 8aee7492d494ce6ff6082791d64fa147751fdbf2 Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Mon, 14 Mar 2022 17:12:05 +0300 Subject: [PATCH 1/2] Plug/Unplug batteries on hot(useful for gamepads) --- src/modules/battery.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index c0075a4..7ea9b55 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -118,9 +118,10 @@ void waybar::modules::Battery::refreshBatteries() { } if (batteries_.empty()) { if (config_["bat"].isString()) { - throw std::runtime_error("No battery named " + config_["bat"].asString()); + spdlog::warn("No battery named {}", config_["bat"].asString()); + } else { + spdlog::warn("No batteries."); } - throw std::runtime_error("No batteries."); } // Remove any batteries that are no longer present and unwatch them @@ -283,6 +284,10 @@ const std::string waybar::modules::Battery::formatTimeRemaining(float hoursRemai } auto waybar::modules::Battery::update() -> void { + if (batteries_.empty()) { + event_box_.hide(); + return; + } auto [capacity, time_remaining, status, power] = getInfos(); if (status == "Unknown") { status = getAdapterStatus(capacity); From 37d87be3c115ab9556be9ab51f57b96c1ce2bc5e Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Mon, 14 Mar 2022 17:53:19 +0300 Subject: [PATCH 2/2] Add supporting of the gamepads batteries --- src/modules/battery.cpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 7ea9b55..c2dc389 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -154,6 +154,7 @@ const std::tuple waybar::modules::Battery::g uint32_t total_energy = 0; // μWh uint32_t total_energy_full = 0; uint32_t total_energy_full_design = 0; + uint32_t total_capacity{0}; std::string status = "Unknown"; for (auto const& item : batteries_) { auto bat = item.first; @@ -161,6 +162,7 @@ const std::tuple waybar::modules::Battery::g uint32_t energy_full; uint32_t energy_now; uint32_t energy_full_design; + uint32_t capacity{0}; std::string _status; std::getline(std::ifstream(bat / "status"), _status); @@ -188,11 +190,18 @@ const std::tuple waybar::modules::Battery::g energy_now = ((uint64_t)charge_now * (uint64_t)voltage_now) / 1000000; energy_full = ((uint64_t)charge_full * (uint64_t)voltage_now) / 1000000; energy_full_design = ((uint64_t)charge_full_design * (uint64_t)voltage_now) / 1000000; + } // Gamepads such as PS Dualshock provide the only capacity + else if (fs::exists(bat / "energy_now") && fs::exists(bat / "energy_full")) { + std::ifstream(bat / "power_now") >> power_now; + std::ifstream(bat / "energy_now") >> energy_now; + std::ifstream(bat / "energy_full") >> energy_full; + std::ifstream(bat / "energy_full_design") >> energy_full_design; } else { - std::ifstream(bat / "power_now") >> power_now; - std::ifstream(bat / "energy_now") >> energy_now; - std::ifstream(bat / "energy_full") >> energy_full; - std::ifstream(bat / "energy_full_design") >> energy_full_design; + std::ifstream(bat / "capacity") >> capacity; + power_now = 0; + energy_now = 0; + energy_full = 0; + energy_full_design = 0; } // Show the "smallest" status among all batteries @@ -203,6 +212,7 @@ const std::tuple waybar::modules::Battery::g total_energy += energy_now; total_energy_full += energy_full; total_energy_full_design += energy_full_design; + total_capacity += capacity; } if (!adapter_.empty() && status == "Discharging") { bool online; @@ -222,7 +232,12 @@ const std::tuple waybar::modules::Battery::g time_remaining = 0.0f; } } - float capacity = ((float)total_energy * 100.0f / (float) total_energy_full); + float capacity{0.0f}; + if(total_energy_full > 0.0f) { + capacity = ((float)total_energy * 100.0f / (float) total_energy_full); + } else { + capacity = (float)total_capacity; + } // Handle design-capacity if (config_["design-capacity"].isBool() ? config_["design-capacity"].asBool() : false) { capacity = ((float)total_energy * 100.0f / (float) total_energy_full_design);