From aa3b2f14247feb0c8be9e1204b27aa24df6c1d0b Mon Sep 17 00:00:00 2001 From: Stefan Biereigel Date: Sat, 29 Oct 2022 16:54:20 +0200 Subject: [PATCH] fix handling of POWER_SUPPLY_TIME_TO_* properties The POWER_SUPPLY_TIME_TO_* properties provided via sysfs have to be interpreted as seconds. Further, the presence of both the POWER_SUPPLY_TIME_TO_EMPTY_NOW and POWER_SUPPLY_TIME_TO_FULL_NOW properties confused the logic and ended up always showing the time-to-full as 0 while charging (since TIME_TO_EMPTY reports 0 with a charger attached). --- src/print_battery_info.c | 14 ++++++++++++-- testcases/026-battery-time-to-empty/BAT0_uevent | 3 ++- testcases/026-battery-time-to-empty/BAT1_uevent | 5 +++++ .../026-battery-time-to-empty/expected_output.txt | 2 +- testcases/026-battery-time-to-empty/i3status.conf | 6 ++++++ 5 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 testcases/026-battery-time-to-empty/BAT1_uevent diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 7465d171..9316b68e 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -143,6 +143,7 @@ static bool slurp_battery_info(battery_info_ctx_t *ctx, struct battery_info *bat const char *walk, *last; bool watt_as_unit = false; int voltage = -1; + int seconds_remaining; char batpath[512]; sprintf(batpath, path, number); INSTANCE(batpath); @@ -175,8 +176,17 @@ static bool slurp_battery_info(battery_info_ctx_t *ctx, struct battery_info *bat batt_info->present_rate = abs(atoi(walk + 1)); else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW=")) voltage = abs(atoi(walk + 1)); - else if (BEGINS_WITH(last, "POWER_SUPPLY_TIME_TO_EMPTY_NOW=")) - batt_info->seconds_remaining = abs(atoi(walk + 1)) * 60; + else if (BEGINS_WITH(last, "POWER_SUPPLY_TIME_TO_EMPTY_NOW=")) { + seconds_remaining = abs(atoi(walk + 1)); + if (seconds_remaining) { + batt_info->seconds_remaining = seconds_remaining; + } + } else if (BEGINS_WITH(last, "POWER_SUPPLY_TIME_TO_FULL_NOW=")) { + seconds_remaining = abs(atoi(walk + 1)); + if (seconds_remaining) { + batt_info->seconds_remaining = seconds_remaining; + } + } /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as * unit instead of μAh. We will calculate it as we need it diff --git a/testcases/026-battery-time-to-empty/BAT0_uevent b/testcases/026-battery-time-to-empty/BAT0_uevent index 5657217b..abf51e63 100644 --- a/testcases/026-battery-time-to-empty/BAT0_uevent +++ b/testcases/026-battery-time-to-empty/BAT0_uevent @@ -1,4 +1,5 @@ POWER_SUPPLY_STATUS=Discharging -POWER_SUPPLY_TIME_TO_EMPTY_NOW=655 +POWER_SUPPLY_TIME_TO_EMPTY_NOW=39300 +POWER_SUPPLY_TIME_TO_FULL_NOW=0 POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000 POWER_SUPPLY_CHARGE_NOW=2390000 diff --git a/testcases/026-battery-time-to-empty/BAT1_uevent b/testcases/026-battery-time-to-empty/BAT1_uevent new file mode 100644 index 00000000..93ab2cd4 --- /dev/null +++ b/testcases/026-battery-time-to-empty/BAT1_uevent @@ -0,0 +1,5 @@ +POWER_SUPPLY_STATUS=Charging +POWER_SUPPLY_TIME_TO_EMPTY_NOW=0 +POWER_SUPPLY_TIME_TO_FULL_NOW=15600 +POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000 +POWER_SUPPLY_CHARGE_NOW=2390000 diff --git a/testcases/026-battery-time-to-empty/expected_output.txt b/testcases/026-battery-time-to-empty/expected_output.txt index 92926bb0..307fee37 100644 --- a/testcases/026-battery-time-to-empty/expected_output.txt +++ b/testcases/026-battery-time-to-empty/expected_output.txt @@ -1 +1 @@ -BAT 30.64% 10:55 +BAT 30.64% 10:55 | CHR 30.64% 04:20 diff --git a/testcases/026-battery-time-to-empty/i3status.conf b/testcases/026-battery-time-to-empty/i3status.conf index 038ca5e2..fd7371fe 100644 --- a/testcases/026-battery-time-to-empty/i3status.conf +++ b/testcases/026-battery-time-to-empty/i3status.conf @@ -3,8 +3,14 @@ general { } order += "battery 0" +order += "battery 1" battery 0 { format = "%status %percentage %remaining" path = "testcases/026-battery-time-to-empty/BAT0_uevent" } + +battery 1 { + format = "%status %percentage %remaining" + path = "testcases/026-battery-time-to-empty/BAT1_uevent" +}