From 3272abcfbe6cfe2ba616fc3310c9f02eef61b0d7 Mon Sep 17 00:00:00 2001 From: erbth Date: Sun, 20 Mar 2022 17:20:20 +0100 Subject: [PATCH] Add another battery status called 'idle' On some systems, a battery's charging regulator may not charge a battery even though it is not full. This might be the case because it was configured to stop charging at a capacity threshold, or e.g. because environmental conditions do not allow for charging the battery. This commit adds this status (called 'idle') and adds support for detecting the status on Linux. --- i3status.c | 2 ++ include/i3status.h | 1 + man/i3status.man | 1 + src/print_battery_info.c | 15 ++++++++++++++- .../023-battery-not-charging/expected_output.txt | 2 +- 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/i3status.c b/i3status.c index 88fb90e..2f60b4e 100644 --- a/i3status.c +++ b/i3status.c @@ -322,6 +322,7 @@ int main(int argc, char *argv[]) { CFG_STR("status_bat", "BAT", CFGF_NONE), CFG_STR("status_unk", "UNK", CFGF_NONE), CFG_STR("status_full", "FULL", CFGF_NONE), + CFG_STR("status_idle", "IDLE", CFGF_NONE), CFG_STR("path", "/sys/class/power_supply/BAT%d/uevent", CFGF_NONE), CFG_INT("low_threshold", 30, CFGF_NONE), CFG_STR("threshold_type", "time", CFGF_NONE), @@ -742,6 +743,7 @@ int main(int argc, char *argv[]) { .status_bat = cfg_getstr(sec, "status_bat"), .status_unk = cfg_getstr(sec, "status_unk"), .status_full = cfg_getstr(sec, "status_full"), + .status_idle = cfg_getstr(sec, "status_idle"), .low_threshold = cfg_getint(sec, "low_threshold"), .threshold_type = cfg_getstr(sec, "threshold_type"), .last_full_capacity = cfg_getbool(sec, "last_full_capacity"), diff --git a/include/i3status.h b/include/i3status.h index fe44780..1bc2c85 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -271,6 +271,7 @@ typedef struct { const char *status_bat; const char *status_unk; const char *status_full; + const char *status_idle; int low_threshold; char *threshold_type; bool last_full_capacity; diff --git a/man/i3status.man b/man/i3status.man index 36e1a6e..f6267da 100644 --- a/man/i3status.man +++ b/man/i3status.man @@ -87,6 +87,7 @@ battery 0 { status_bat = "🔋 BAT" status_unk = "? UNK" status_full = "☻ FULL" + status_idle = "☻ IDLE" path = "/sys/class/power_supply/BAT%d/uevent" low_threshold = 10 } diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 7465d17..8864978 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -49,6 +49,7 @@ typedef enum { CS_DISCHARGING, CS_CHARGING, CS_FULL, + CS_IDLE, } charging_status_t; /* A description of the state of one or more batteries. */ @@ -129,6 +130,13 @@ static void add_battery_info(struct battery_info *acc, const struct battery_info acc->status = batt_info->status; /* else: retain FULL, since it is more specific than UNKNOWN */ break; + + case CS_IDLE: + if (batt_info->status != CS_UNKNOWN && batt_info->status != CS_FULL) + acc->status = batt_info->status; + /* else: retain IDLE, since it is more specific than UNKNOWN and is + * implied by CS_FULL though correct for all batteries */ + break; } acc->present_rate = abs(present_rate); @@ -187,8 +195,10 @@ static bool slurp_battery_info(battery_info_ctx_t *ctx, struct battery_info *bat batt_info->status = CS_CHARGING; else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full")) batt_info->status = CS_FULL; - else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging") || BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Not charging")) + else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging")) batt_info->status = CS_DISCHARGING; + else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Not charging")) + batt_info->status = CS_IDLE; else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=")) batt_info->status = CS_UNKNOWN; else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN=") || @@ -674,6 +684,9 @@ void print_battery_info(battery_info_ctx_t *ctx) { case CS_FULL: statusstr = ctx->status_full; break; + case CS_IDLE: + statusstr = ctx->status_idle; + break; default: statusstr = ctx->status_unk; } diff --git a/testcases/023-battery-not-charging/expected_output.txt b/testcases/023-battery-not-charging/expected_output.txt index 6fc5dfb..0ec43b3 100644 --- a/testcases/023-battery-not-charging/expected_output.txt +++ b/testcases/023-battery-not-charging/expected_output.txt @@ -1 +1 @@ -BAT +IDLE