Skip to content

Commit d5c69a9

Browse files
joeseymourstapelberg
authored andcommitted
Treat zero battery capacity as "not available" (i3#259)
`print_battery_info` computes `batt_info.percentage_remaining` by dividing batt_info.remaining by `full`. If `full` is `0` then the battery remaining will be reported as "inf". Before this, it tries to set `full` to either the design capacity or to the last known good charge. It determines if these values are available by checking whether their fields in `batt_info` are non-negative. As it initialized `batt_info` with values of `-1`, a non-negative value implies that something has provided a value. `slurp_all_batteries` and `add_battery_info` however initialize these fields to zero, so if these functions are called then `batt_info.full_design` will always be used. This means that on systems that don't provide a value for design capacity the percentage remaining will be reported as "inf", unless the user has set `last_full_capacity` to `true` in their `i3status.conf`. This patch changes `print_battery_info` to expect values for the battery capacity to be strictly greater than zero. This seems reasonable as a battery with a capacity of zero isn't useful. An alternative solution would be to change `slurp_all_batteries` and `add_battery_info` to initialize `batt_info` with `-1`, as `print_battery_info` does. This is less appealing as `add_battery_info` is accumulating the values, so using `-1` would introduce off-by-one errors without additional code to avoid them.
1 parent ef8f9dd commit d5c69a9

File tree

7 files changed

+31
-4
lines changed

7 files changed

+31
-4
lines changed

src/print_battery_info.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,13 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
513513
// We prefer the design capacity, but use the last capacity if we don't have it,
514514
// or if we are asked to (last_full_capacity == true); but similarly we use
515515
// the design capacity if we don't have the last capacity.
516-
// If we don't have either then both full_design and full_last < 0,
517-
// which implies full < 0, which bails out on the following line.
516+
// If we don't have either then both full_design and full_last <= 0,
517+
// which implies full <= 0, which bails out on the following line.
518518
int full = batt_info.full_design;
519-
if (full < 0 || (last_full_capacity && batt_info.full_last >= 0)) {
519+
if (full <= 0 || (last_full_capacity && batt_info.full_last > 0)) {
520520
full = batt_info.full_last;
521521
}
522-
if (full < 0 && batt_info.remaining < 0 && batt_info.percentage_remaining < 0) {
522+
if (full <= 0 && batt_info.remaining < 0 && batt_info.percentage_remaining < 0) {
523523
/* We have no physical measurements and no estimates. Nothing
524524
* much we can report, then. */
525525
OUTPUT_FULL_TEXT(format_down);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
POWER_SUPPLY_CHARGE_FULL=100
2+
POWER_SUPPLY_CHARGE_NOW=50
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
50.00%
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
general {
2+
output_format = "none"
3+
}
4+
5+
order += "battery all"
6+
7+
battery all {
8+
format = "%percentage"
9+
path = "testcases/018-battery-capacity/%d/uevent"
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
POWER_SUPPLY_CHARGE_FULL_DESIGN=100
2+
POWER_SUPPLY_CHARGE_NOW=50
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
50.00%
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
general {
2+
output_format = "none"
3+
}
4+
5+
order += "battery all"
6+
7+
battery all {
8+
format = "%percentage"
9+
path = "testcases/019-battery-capacity/%d/uevent"
10+
last_full_capacity = true
11+
}

0 commit comments

Comments
 (0)