From 68f438ec9eefbca5d735a1a7897d52e0e8e6f1c7 Mon Sep 17 00:00:00 2001 From: Simon Elsbrock Date: Thu, 23 Aug 2012 16:42:38 +0200 Subject: add additional battery threshold type "percentage" The battery threshold can now be configured as type "time" or "percentage", but defaults to "time" to prevent unexpected behavior. Also, low_threshold was set to a more reasonable default of 30. --- i3status.c | 5 +++-- include/i3status.h | 2 +- man/i3status.man | 11 +++++++---- src/print_battery_info.c | 17 ++++++++++++----- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/i3status.c b/i3status.c index f0767a0..915cbcc 100644 --- a/i3status.c +++ b/i3status.c @@ -214,7 +214,8 @@ int main(int argc, char *argv[]) { cfg_opt_t battery_opts[] = { CFG_STR("format", "%status %percentage %remaining", CFGF_NONE), CFG_STR("path", "/sys/class/power_supply/BAT%d/uevent", CFGF_NONE), - CFG_INT("low_threshold", 10, CFGF_NONE), + CFG_INT("low_threshold", 30, CFGF_NONE), + CFG_STR("threshold_type", "time", CFGF_NONE), CFG_BOOL("last_full_capacity", false, CFGF_NONE), CFG_END() }; @@ -414,7 +415,7 @@ int main(int argc, char *argv[]) { CASE_SEC_TITLE("battery") { SEC_OPEN_MAP("battery"); - print_battery_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getint(sec, "low_threshold"), cfg_getbool(sec, "last_full_capacity")); + print_battery_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getint(sec, "low_threshold"), cfg_getstr(sec, "threshold_type"), cfg_getbool(sec, "last_full_capacity")); SEC_CLOSE_MAP; } diff --git a/include/i3status.h b/include/i3status.h index dc40c8f..1f9da4d 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -137,7 +137,7 @@ char *auto_detect_format(); void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down); void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format); -void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, int threshold, bool last_full_capacity); +void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, int low_threshold, char *threshold_type, bool last_full_capacity); void print_time(yajl_gen json_gen, char *buffer, const char *format, struct tm *current_tm); void print_ddate(yajl_gen json_gen, char *buffer, const char *format, struct tm *current_tm); const char *get_ip_addr(); diff --git a/man/i3status.man b/man/i3status.man index 9ec6acb..5938acb 100644 --- a/man/i3status.man +++ b/man/i3status.man @@ -204,15 +204,18 @@ If your battery is represented in a non-standard path in /sys, be sure to modify the "path" property accordingly. The first occurence of %d gets replaced with the battery number, but you can just hard-code a path as well. -If the remaining time sinks below low_threshold minutes, the battery text will -be colored red. So, if you configure low_threshold to 10, and your battery -lasts another 9 minutes, it will be colored red. +It is possible to define a low_threshold that causes the battery text to be +colored red. The low_threshold type can be of threshold_type "time" or +"percentage". So, if you configure low_threshold to 10 and threshold_type to +"time", and your battery lasts another 9 minutes, it will be colored red. *Example order*: +battery 0+ *Example format*: +%status %remaining (%emptytime %consumption)+ -*Example low_threshold*: +low_threshold 10+ +*Example low_threshold*: +30+ + +*Example threshold_type*: +time+ === CPU-Temperature diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 3e130c7..202d9bb 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -29,7 +29,7 @@ * worn off your battery is. * */ -void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, int threshold, bool last_full_capacity) { +void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, int low_threshold, char *threshold_type, bool last_full_capacity) { time_t empty_time; struct tm *empty_tm; char buf[1024]; @@ -127,8 +127,8 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status)); - (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%", - (((float)remaining / (float)full_design) * 100)); + float percentage_remaining = (((float)remaining / (float)full_design) * 100); + (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%", percentage_remaining); if (present_rate > 0) { float remaining_time; @@ -146,8 +146,15 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char minutes = seconds / 60; seconds -= (minutes * 60); - if (status == CS_DISCHARGING && threshold > 0 && seconds_remaining < 60 * threshold) - START_COLOR("color_bad"); + if (status == CS_DISCHARGING && low_threshold > 0) { + if (strncmp(threshold_type, "percentage", strlen(threshold_type)) == 0 + && percentage_remaining < low_threshold) { + START_COLOR("color_bad"); + } else if (strncmp(threshold_type, "time", strlen(threshold_type)) == 0 + && seconds_remaining < 60 * low_threshold) { + START_COLOR("color_bad"); + } + } (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d", max(hours, 0), max(minutes, 0), max(seconds, 0)); -- cgit v1.2.3