From b1833b1f36c5661d2cb9f5da9fcf006c1da01c5e Mon Sep 17 00:00:00 2001 From: Cyril Levis Date: Mon, 5 Dec 2022 21:24:52 +0100 Subject: [PATCH] feat: add macsmc-battery time remaining support for asahi use time_to_empty_now and time_to_full_now --- src/modules/battery.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index bb06781..b7a9cd0 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -233,6 +233,10 @@ const std::tuple waybar::modules::Battery::g bool total_energy_full_design_exists = false; uint32_t total_capacity = 0; bool total_capacity_exists = false; + uint32_t time_to_empty_now = 0; + bool time_to_empty_now_exists = false; + uint32_t time_to_full_now = 0; + bool time_to_full_now_exists = false; std::string status = "Unknown"; for (auto const& item : batteries_) { @@ -260,6 +264,16 @@ const std::tuple waybar::modules::Battery::g std::ifstream(bat / "current_avg") >> current_now; } + if (fs::exists(bat / "time_to_empty_now")) { + time_to_empty_now_exists = true; + std::ifstream(bat / "time_to_empty_now") >> time_to_empty_now; + } + + if (fs::exists(bat / "time_to_full_now")) { + time_to_full_now_exists = true; + std::ifstream(bat / "time_to_full_now") >> time_to_full_now; + } + uint32_t voltage_now = 0; bool voltage_now_exists = false; if (fs::exists(bat / "voltage_now")) { @@ -481,8 +495,15 @@ const std::tuple waybar::modules::Battery::g } float time_remaining{0.0f}; - if (status == "Discharging" && total_power_exists && total_energy_exists) { + if (status == "Discharging" && time_to_empty_now_exists) { + if (time_to_empty_now != 0) time_remaining = (float)time_to_empty_now / 1000.0f; + } else if (status == "Discharging" && total_power_exists && total_energy_exists) { if (total_power != 0) time_remaining = (float)total_energy / total_power; + } else if (status == "Charging" && time_to_full_now_exists) { + if (time_to_full_now_exists && (time_to_full_now != 0)) time_remaining = -(float)time_to_full_now / 1000.0f; + // If we've turned positive it means the battery is past 100% and so just report that as no + // time remaining + if (time_remaining > 0.0f) time_remaining = 0.0f; } else if (status == "Charging" && total_energy_exists && total_energy_full_exists && total_power_exists) { if (total_power != 0)