From 4c74a7c51dba31f43742cb3c1dd9b139ffbbbed6 Mon Sep 17 00:00:00 2001 From: jkre Date: Wed, 13 Dec 2023 22:51:40 +0100 Subject: [PATCH 01/18] Add current_now and voltage_now to battery struct and fix naming of current_now --- src/linux/btop_collect.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 33a0be8..68b8c58 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -661,9 +661,10 @@ namespace Cpu { } struct battery { - fs::path base_dir, energy_now, energy_full, power_now, status, online; + fs::path base_dir, energy_now, energy_full, power_now, current_now, voltage_now, status, online; string device_type; bool use_energy = true; + bool use_power = true; }; auto get_battery() -> tuple { @@ -710,8 +711,16 @@ namespace Cpu { 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 / "power_now")) { + new_bat.power_now = bat_dir / "power_now"; + } + else if ((fs::exists(bat_dir / "current_now")) and (fs::exists(bat_dir / "current_now"))) { + new_bat.current_now = bat_dir / "current_now"; + new_bat.voltage_now = bat_dir / "voltage_now"; + } + else { + new_bat.use_power = false; + } 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"; From b99008f6267ebbcd700052d5349f4c4bac8b08c3 Mon Sep 17 00:00:00 2001 From: jkre Date: Wed, 13 Dec 2023 23:17:07 +0100 Subject: [PATCH 02/18] Introduce charge in addition to energy for laptops that use charge instead of energy, this is done so that the units make more sense in this case --- src/linux/btop_collect.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 68b8c58..1da9c4a 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -661,9 +661,9 @@ namespace Cpu { } struct battery { - fs::path base_dir, energy_now, energy_full, power_now, current_now, voltage_now, status, online; + fs::path base_dir, energy_now, charge_now, energy_full, charge_full, power_now, current_now, voltage_now, status, online; string device_type; - bool use_energy = true; + bool use_energy_or_charge = true; bool use_power = true; }; @@ -700,14 +700,14 @@ namespace Cpu { } 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; + else if (fs::exists(bat_dir / "charge_now")) new_bat.charge_now = bat_dir / "charge_now"; + else new_bat.use_energy_or_charge = 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; + else if (fs::exists(bat_dir / "charge_full")) new_bat.charge_full = bat_dir / "charge_full"; + else new_bat.use_energy_or_charge = false; - if (not new_bat.use_energy and not fs::exists(bat_dir / "capacity")) { + if (not new_bat.use_energy_or_charge and not fs::exists(bat_dir / "capacity")) { continue; } @@ -757,7 +757,7 @@ namespace Cpu { long seconds = -1; //? Try to get battery percentage - if (b.use_energy) { + if (b.use_energy_or_charge) { try { percent = round(100.0 * stoll(readfile(b.energy_now, "-1")) / stoll(readfile(b.energy_full, "1"))); } @@ -787,7 +787,7 @@ namespace Cpu { //? Get seconds to empty if (not is_in(status, "charging", "full")) { - if (b.use_energy and not b.power_now.empty()) { + if (b.use_energy_or_charge and not b.power_now.empty()) { try { seconds = round((double)stoll(readfile(b.energy_now, "0")) / stoll(readfile(b.power_now, "1")) * 3600); } From ee61700a4437afffd019264c50ad2a18cab75062 Mon Sep 17 00:00:00 2001 From: jkre Date: Wed, 13 Dec 2023 23:39:45 +0100 Subject: [PATCH 03/18] add case of calculating the remaining battery time for current/charge --- src/linux/btop_collect.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 1da9c4a..ae11f61 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -787,13 +787,23 @@ namespace Cpu { //? Get seconds to empty if (not is_in(status, "charging", "full")) { - if (b.use_energy_or_charge and not b.power_now.empty()) { - try { - seconds = round((double)stoll(readfile(b.energy_now, "0")) / stoll(readfile(b.power_now, "1")) * 3600); + if (b.use_energy_or_charge ) { + if (not b.power_now.empty()) { + try { + seconds = round((double)stoll(readfile(b.energy_now, "0")) / stoll(readfile(b.power_now, "1")) * 3600); + } + catch (const std::invalid_argument&) { } + catch (const std::out_of_range&) { } + } + else if (not b.current_now.empty()) { + try { + seconds = round((double)stoll(readfile(b.charge_now, "0")) / (double)stoll(readfile(b.current_now, "1")) * 3600); + } + catch (const std::invalid_argument&) { } + catch (const std::out_of_range&) { } } - catch (const std::invalid_argument&) { } - catch (const std::out_of_range&) { } } + if (seconds < 0 and fs::exists(b.base_dir / "time_to_empty")) { try { seconds = stoll(readfile(b.base_dir / "time_to_empty", "0")) * 60; From ab294bfc104d20e1178cb4c6bc4de2fd879cb892 Mon Sep 17 00:00:00 2001 From: jkre Date: Wed, 13 Dec 2023 23:51:18 +0100 Subject: [PATCH 04/18] add battery percentage calculation in charge case --- src/linux/btop_collect.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index ae11f61..0d58596 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -764,6 +764,13 @@ namespace Cpu { catch (const std::invalid_argument&) { } catch (const std::out_of_range&) { } } + if (b.use_energy_or_charge == true and percent < 0) { + try { + percent = round(100.0 * stoll(readfile(b.charge_now, "-1")) / stoll(readfile(b.charge_full, "1"))); + } + catch (const std::invalid_argument&) { } + catch (const std::out_of_range&) { } + } if (percent < 0) { try { percent = stoll(readfile(b.base_dir / "capacity", "-1")); From f6d8c4a0447a7bb56f8717a6f21920bc6d7ba310 Mon Sep 17 00:00:00 2001 From: jkre Date: Wed, 13 Dec 2023 23:58:40 +0100 Subject: [PATCH 05/18] use capacity as default for battery percentage, less complicated and matches desktop percent exactly --- src/linux/btop_collect.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 0d58596..3961d91 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -757,7 +757,14 @@ namespace Cpu { long seconds = -1; //? Try to get battery percentage - if (b.use_energy_or_charge) { + if (percent < 0) { + try { + percent = stoll(readfile(b.base_dir / "capacity", "-1")); + } + catch (const std::invalid_argument&) { } + catch (const std::out_of_range&) { } + } + if (b.use_energy_or_charge and percent < 0) { try { percent = round(100.0 * stoll(readfile(b.energy_now, "-1")) / stoll(readfile(b.energy_full, "1"))); } @@ -771,13 +778,6 @@ namespace Cpu { catch (const std::invalid_argument&) { } catch (const std::out_of_range&) { } } - if (percent < 0) { - try { - percent = stoll(readfile(b.base_dir / "capacity", "-1")); - } - catch (const std::invalid_argument&) { } - catch (const std::out_of_range&) { } - } if (percent < 0) { has_battery = false; return {0, 0, ""}; From b09f352c09c14788985e23cd01f2975b2443b810 Mon Sep 17 00:00:00 2001 From: jkre Date: Thu, 14 Dec 2023 00:16:15 +0100 Subject: [PATCH 06/18] clean up if statement for battery percent calculation --- src/linux/btop_collect.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 3961d91..212a165 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -771,7 +771,7 @@ namespace Cpu { catch (const std::invalid_argument&) { } catch (const std::out_of_range&) { } } - if (b.use_energy_or_charge == true and percent < 0) { + if (b.use_energy_or_charge and percent < 0) { try { percent = round(100.0 * stoll(readfile(b.charge_now, "-1")) / stoll(readfile(b.charge_full, "1"))); } From 419a7d4ca3889d97afc006f7301017a224e16396 Mon Sep 17 00:00:00 2001 From: jkre Date: Thu, 14 Dec 2023 00:32:06 +0100 Subject: [PATCH 07/18] add power draw calculation for battery --- src/linux/btop_collect.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 212a165..c00c563 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -755,6 +755,7 @@ namespace Cpu { int percent = -1; long seconds = -1; + float watts = -1; //? Try to get battery percentage if (percent < 0) { @@ -820,6 +821,25 @@ namespace Cpu { } } + //? Get power draw + if (b.use_power) { + if (not b.power_now.empty()) { + try { + watts = (float)stoll(readfile(b.energy_now, "-1")) / 1000000.0; + } + catch (const std::invalid_argument&) { } + catch (const std::out_of_range&) { } + } + else if (not b.voltage_now.empty() and not b.current_now.empty()) { + try { + watts = (float)stoll(readfile(b.current_now, "-1")) / 1000000.0 * stoll(readfile(b.voltage_now, "1")) / 1000000.0; + } + catch (const std::invalid_argument&) { } + catch (const std::out_of_range&) { } + } + + } + return {percent, seconds, status}; } From 6e575116fe45b0a48f873820bc9fe4486b827eed Mon Sep 17 00:00:00 2001 From: jkre Date: Thu, 14 Dec 2023 00:53:01 +0100 Subject: [PATCH 08/18] add power to get_battery function output --- src/btop_shared.hpp | 4 ++-- src/linux/btop_collect.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/btop_shared.hpp b/src/btop_shared.hpp index 017c7f8..eb5514c 100644 --- a/src/btop_shared.hpp +++ b/src/btop_shared.hpp @@ -178,7 +178,7 @@ namespace Cpu { extern string cpuName, cpuHz; extern vector available_fields; extern vector available_sensors; - extern tuple current_bat; + extern tuple current_bat; struct cpu_info { unordered_flat_map> cpu_percent = { @@ -213,7 +213,7 @@ namespace Cpu { auto get_cpuHz() -> string; //* Get battery info from /sys - auto get_battery() -> tuple; + auto get_battery() -> tuple; } namespace Mem { diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index c00c563..d88454a 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -291,7 +291,7 @@ namespace Cpu { string cpuName; string cpuHz; bool has_battery = true; - tuple current_bat; + tuple current_bat; const array time_names { "user"s, "nice"s, "system"s, "idle"s, "iowait"s, @@ -667,8 +667,8 @@ namespace Cpu { bool use_power = true; }; - auto get_battery() -> tuple { - if (not has_battery) return {0, 0, ""}; + auto get_battery() -> tuple { + if (not has_battery) return {0, 0, 0, ""}; static string auto_sel; static unordered_flat_map batteries; @@ -735,7 +735,7 @@ namespace Cpu { } if (batteries.empty()) { has_battery = false; - return {0, 0, ""}; + return {0, 0, 0, ""}; } } @@ -781,7 +781,7 @@ namespace Cpu { } if (percent < 0) { has_battery = false; - return {0, 0, ""}; + return {0, 0, 0, ""}; } //? Get charging/discharging status @@ -840,7 +840,7 @@ namespace Cpu { } - return {percent, seconds, status}; + return {percent, watts, seconds, status}; } auto collect(bool no_update) -> cpu_info& { From ddd4bec1c3eed40241fe22254770bc52e931bb88 Mon Sep 17 00:00:00 2001 From: jkre Date: Thu, 14 Dec 2023 00:54:29 +0100 Subject: [PATCH 09/18] Show wattage next to battery remaining time when wattage could be calculated --- src/btop_draw.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index 5ae357e..dde135d 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -705,6 +705,7 @@ namespace Cpu { if (Config::getB("show_battery") and has_battery) { static int old_percent{}; // defaults to = 0 static long old_seconds{}; // defaults to = 0 + static float old_watts{}; // defaults to = 0 static string old_status; static Draw::Meter bat_meter {10, "cpu", true}; static const unordered_flat_map bat_symbols = { @@ -714,16 +715,18 @@ namespace Cpu { {"unknown", "○"} }; - const auto& [percent, seconds, status] = current_bat; + const auto& [percent, watts, seconds, status] = current_bat; - if (redraw or percent != old_percent or seconds != old_seconds or status != old_status) { + if (redraw or percent != old_percent or watts != old_watts or seconds != old_seconds or status != old_status) { old_percent = percent; + old_watts = watts; old_seconds = seconds; old_status = status; const string str_time = (seconds > 0 ? sec_to_dhms(seconds, true, true) : ""); const string str_percent = to_string(percent) + '%'; + const string str_watts = (watts != -1 ? to_string(watts) + 'W' : ""); const auto& bat_symbol = bat_symbols.at((bat_symbols.contains(status) ? status : "unknown")); - const int current_len = (Term::width >= 100 ? 11 : 0) + str_time.size() + str_percent.size() + to_string(Config::getI("update_ms")).size(); + const int current_len = (Term::width >= 100 ? 11 : 0) + str_time.size() + str_percent.size() + str_watts.size() + to_string(Config::getI("update_ms")).size(); const int current_pos = Term::width - current_len - 17; if ((bat_pos != current_pos or bat_len != current_len) and bat_pos > 0 and not redraw) @@ -733,7 +736,7 @@ namespace Cpu { out += Mv::to(y, bat_pos) + title_left + Theme::c("title") + Fx::b + "BAT" + bat_symbol + ' ' + str_percent + (Term::width >= 100 ? Fx::ub + ' ' + bat_meter(percent) + Fx::b : "") - + (not str_time.empty() ? ' ' + Theme::c("title") + str_time : " ") + Fx::ub + title_right; + + (not str_time.empty() ? ' ' + Theme::c("title") + str_time : " ") + (not str_watts.empty() ? ' ' + Theme::c("title") + Fx::b + str_watts : " ") + Fx::ub + title_right; } } else if (bat_pos > 0) { From 1ad1418771bd69c70674388d64a4965f9f7d0582 Mon Sep 17 00:00:00 2001 From: jkre Date: Thu, 14 Dec 2023 00:58:17 +0100 Subject: [PATCH 10/18] remove redundant space --- src/btop_draw.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index dde135d..b5e75ec 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -736,7 +736,7 @@ namespace Cpu { out += Mv::to(y, bat_pos) + title_left + Theme::c("title") + Fx::b + "BAT" + bat_symbol + ' ' + str_percent + (Term::width >= 100 ? Fx::ub + ' ' + bat_meter(percent) + Fx::b : "") - + (not str_time.empty() ? ' ' + Theme::c("title") + str_time : " ") + (not str_watts.empty() ? ' ' + Theme::c("title") + Fx::b + str_watts : " ") + Fx::ub + title_right; + + (not str_time.empty() ? ' ' + Theme::c("title") + str_time : "") + (not str_watts.empty() ? ' ' + Theme::c("title") + Fx::b + str_watts : " ") + Fx::ub + title_right; } } else if (bat_pos > 0) { @@ -2239,3 +2239,4 @@ namespace Draw { } } } + From 7a188bfaaf19266b2d74336ca85ccb46fa0f111a Mon Sep 17 00:00:00 2001 From: jkre Date: Thu, 14 Dec 2023 01:39:00 +0100 Subject: [PATCH 11/18] round wattage to second decimal --- src/btop_draw.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index b5e75ec..9e49d1f 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -724,7 +724,7 @@ namespace Cpu { old_status = status; const string str_time = (seconds > 0 ? sec_to_dhms(seconds, true, true) : ""); const string str_percent = to_string(percent) + '%'; - const string str_watts = (watts != -1 ? to_string(watts) + 'W' : ""); + const string str_watts = (watts != -1 ? fmt::format("{:.2f}", watts) + 'W' : ""); const auto& bat_symbol = bat_symbols.at((bat_symbols.contains(status) ? status : "unknown")); const int current_len = (Term::width >= 100 ? 11 : 0) + str_time.size() + str_percent.size() + str_watts.size() + to_string(Config::getI("update_ms")).size(); const int current_pos = Term::width - current_len - 17; From 578b01e06b6e9bc94d6b20ec361a518ab466ec96 Mon Sep 17 00:00:00 2001 From: jkre Date: Thu, 14 Dec 2023 22:19:22 +0100 Subject: [PATCH 12/18] add show_battery_power option to menu --- src/btop_config.cpp | 3 +++ src/btop_menu.cpp | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/btop_config.cpp b/src/btop_config.cpp index 0f723b7..454429b 100644 --- a/src/btop_config.cpp +++ b/src/btop_config.cpp @@ -197,6 +197,8 @@ namespace Config { {"selected_battery", "#* Which battery to use if multiple are present. \"Auto\" for auto detection."}, + {"show_battery_watt" "#* Show power stats of battery next to charge indicator"}, + {"log_level", "#* Set loglevel for \"~/.config/btop/btop.log\" levels are: \"ERROR\" \"WARNING\" \"INFO\" \"DEBUG\".\n" "#* The level set includes all lower levels, i.e. \"DEBUG\" will show all logging info."}, #ifdef GPU_SUPPORT @@ -291,6 +293,7 @@ namespace Config { {"net_auto", true}, {"net_sync", true}, {"show_battery", true}, + {"show_battery_watts", true}, {"vim_keys", false}, {"tty_mode", false}, {"disk_free_priv", false}, diff --git a/src/btop_menu.cpp b/src/btop_menu.cpp index 206052f..5bbd524 100644 --- a/src/btop_menu.cpp +++ b/src/btop_menu.cpp @@ -354,6 +354,13 @@ namespace Menu { "Can be both batteries and UPS.", "", "\"Auto\" for auto detection."}, + {"show_battery_watts", + "Show battery power.", + "" + "Shows power consumed by device when not connected to power", + "Shows power charging power otherwise" + "", + "True or False."}, {"log_level", "Set loglevel for error.log", "", From 2934138a660ad0b80bde31e5c81f495d7b76dc13 Mon Sep 17 00:00:00 2001 From: jkre Date: Thu, 14 Dec 2023 22:56:31 +0100 Subject: [PATCH 13/18] Only redraw battery indicator on power change if power change option is set to true --- src/btop_draw.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index 9e49d1f..ae9ba69 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -717,14 +717,14 @@ namespace Cpu { const auto& [percent, watts, seconds, status] = current_bat; - if (redraw or percent != old_percent or watts != old_watts or seconds != old_seconds or status != old_status) { + if (redraw or percent != old_percent or (watts != old_watts and Config::getB("show_battery_watts")) or seconds != old_seconds or status != old_status) { old_percent = percent; old_watts = watts; old_seconds = seconds; old_status = status; const string str_time = (seconds > 0 ? sec_to_dhms(seconds, true, true) : ""); const string str_percent = to_string(percent) + '%'; - const string str_watts = (watts != -1 ? fmt::format("{:.2f}", watts) + 'W' : ""); + const string str_watts = (watts != -1 and Config::getB("show_battery_watts") ? fmt::format("{:.2f}", watts) + 'W' : ""); const auto& bat_symbol = bat_symbols.at((bat_symbols.contains(status) ? status : "unknown")); const int current_len = (Term::width >= 100 ? 11 : 0) + str_time.size() + str_percent.size() + str_watts.size() + to_string(Config::getI("update_ms")).size(); const int current_pos = Term::width - current_len - 17; @@ -736,7 +736,7 @@ namespace Cpu { out += Mv::to(y, bat_pos) + title_left + Theme::c("title") + Fx::b + "BAT" + bat_symbol + ' ' + str_percent + (Term::width >= 100 ? Fx::ub + ' ' + bat_meter(percent) + Fx::b : "") - + (not str_time.empty() ? ' ' + Theme::c("title") + str_time : "") + (not str_watts.empty() ? ' ' + Theme::c("title") + Fx::b + str_watts : " ") + Fx::ub + title_right; + + (not str_time.empty() ? ' ' + Theme::c("title") + str_time : "") + (not str_watts.empty() ? " " + Theme::c("title") + Fx::b + str_watts : "") + Fx::ub + title_right; } } else if (bat_pos > 0) { From cd6c1b7294cfdabe234da13c45ba6613ed552eb5 Mon Sep 17 00:00:00 2001 From: jkre Date: Thu, 14 Dec 2023 23:27:19 +0100 Subject: [PATCH 14/18] make discribtion in menu and settings clearer --- src/btop_config.cpp | 2 +- src/btop_menu.cpp | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/btop_config.cpp b/src/btop_config.cpp index 454429b..5fed33f 100644 --- a/src/btop_config.cpp +++ b/src/btop_config.cpp @@ -197,7 +197,7 @@ namespace Config { {"selected_battery", "#* Which battery to use if multiple are present. \"Auto\" for auto detection."}, - {"show_battery_watt" "#* Show power stats of battery next to charge indicator"}, + {"show_battery_watts", "#* Show power stats of battery next to charge indicator."}, {"log_level", "#* Set loglevel for \"~/.config/btop/btop.log\" levels are: \"ERROR\" \"WARNING\" \"INFO\" \"DEBUG\".\n" "#* The level set includes all lower levels, i.e. \"DEBUG\" will show all logging info."}, diff --git a/src/btop_menu.cpp b/src/btop_menu.cpp index 5bbd524..5d1421f 100644 --- a/src/btop_menu.cpp +++ b/src/btop_menu.cpp @@ -356,11 +356,9 @@ namespace Menu { "\"Auto\" for auto detection."}, {"show_battery_watts", "Show battery power.", - "" - "Shows power consumed by device when not connected to power", - "Shows power charging power otherwise" "", - "True or False."}, + "Show discharge power when discharging.", + "Show charging power when charging."}, {"log_level", "Set loglevel for error.log", "", From a81b514d6df6eaff0293aa4ce869a429a1cde4b2 Mon Sep 17 00:00:00 2001 From: jkre Date: Fri, 15 Dec 2023 01:14:47 +0100 Subject: [PATCH 15/18] add freebsd support for battery power --- src/freebsd/btop_collect.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp index 7f93322..d678038 100644 --- a/src/freebsd/btop_collect.cpp +++ b/src/freebsd/btop_collect.cpp @@ -200,7 +200,7 @@ namespace Cpu { string cpuName; string cpuHz; bool has_battery = true; - tuple current_bat; + tuple current_bat; const array time_names = {"user", "nice", "system", "idle"}; @@ -366,10 +366,11 @@ namespace Cpu { return core_map; } - auto get_battery() -> tuple { - if (not has_battery) return {0, 0, ""}; + auto get_battery() -> tuple { + if (not has_battery) return {0, 0, 0, ""}; long seconds = -1; + float watts = -1; uint32_t percent = -1; size_t size = sizeof(percent); string status = "discharging"; @@ -381,6 +382,10 @@ namespace Cpu { if (sysctlbyname("hw.acpi.battery.time", &seconds, &size, nullptr, 0) < 0) { seconds = 0; } + size = sizeof(watts); + if (sysctlbyname("hw.acpi.battery.rate", &watts, &size, nullptr, 0) < 0) { + watts = -1; + } int state; size = sizeof(state); if (sysctlbyname("hw.acpi.battery.state", &state, &size, nullptr, 0) < 0) { @@ -395,7 +400,7 @@ namespace Cpu { } } - return {percent, seconds, status}; + return {percent, watts, seconds, status}; } auto collect(bool no_update) -> cpu_info & { From 0c706cd20a262531788469ca432ab0a09d75c852 Mon Sep 17 00:00:00 2001 From: jkre Date: Fri, 15 Dec 2023 01:28:57 +0100 Subject: [PATCH 16/18] make os compatible --- src/osx/btop_collect.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index e410c74..118af83 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -187,7 +187,7 @@ namespace Cpu { string cpuHz; bool has_battery = true; bool macM1 = false; - tuple current_bat; + tuple current_bat; const array time_names = {"user", "nice", "system", "idle"}; @@ -398,8 +398,8 @@ namespace Cpu { ~IOPSList_Wrap() { CFRelease(data); } }; - auto get_battery() -> tuple { - if (not has_battery) return {0, 0, ""}; + auto get_battery() -> tuple { + if (not has_battery) return {0, 0, 0, ""}; uint32_t percent = -1; long seconds = -1; @@ -438,7 +438,7 @@ namespace Cpu { has_battery = false; } } - return {percent, seconds, status}; + return {percent, -1, seconds, status}; } auto collect(bool no_update) -> cpu_info & { From 61105e46b77f81dfcfbc968f8f6eb9b89d4a541e Mon Sep 17 00:00:00 2001 From: jkre Date: Mon, 22 Jan 2024 22:20:33 +0100 Subject: [PATCH 17/18] Add battery power draw to battery inforamtion tuple for openbsd and set it to a constant --- src/openbsd/btop_collect.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/openbsd/btop_collect.cpp b/src/openbsd/btop_collect.cpp index df35662..c0f7b9f 100644 --- a/src/openbsd/btop_collect.cpp +++ b/src/openbsd/btop_collect.cpp @@ -385,8 +385,8 @@ namespace Cpu { return core_map; } - auto get_battery() -> tuple { - if (not has_battery) return {0, 0, ""}; + auto get_battery() -> tuple { + if (not has_battery) return {0, 0, 0, ""}; long seconds = -1; uint32_t percent = -1; @@ -417,7 +417,7 @@ namespace Cpu { } } - return {percent, seconds, status}; + return {percent, -1, seconds, status}; } auto collect(bool no_update) -> cpu_info & { From c750543950304d8828e21ab5c6e2976c0c54235d Mon Sep 17 00:00:00 2001 From: jkre Date: Mon, 22 Jan 2024 22:32:16 +0100 Subject: [PATCH 18/18] Fix missing value in battery status tuple for openbsd --- src/openbsd/btop_collect.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openbsd/btop_collect.cpp b/src/openbsd/btop_collect.cpp index c0f7b9f..3784ea9 100644 --- a/src/openbsd/btop_collect.cpp +++ b/src/openbsd/btop_collect.cpp @@ -202,7 +202,7 @@ namespace Cpu { string cpuName; string cpuHz; bool has_battery = true; - tuple current_bat; + tuple current_bat; const array time_names = {"user", "nice", "system", "idle"};