]> git.sur5r.net Git - i3/i3status/commitdiff
add additional battery threshold type "percentage"
authorSimon Elsbrock <simon@iodev.org>
Thu, 23 Aug 2012 14:42:38 +0000 (16:42 +0200)
committerMichael Stapelberg <michael@stapelberg.de>
Tue, 28 Aug 2012 16:05:11 +0000 (18:05 +0200)
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
include/i3status.h
man/i3status.man
src/print_battery_info.c

index f0767a0555ac959a0f8257b713f0c9dc92584d42..915cbccdb33be9f72fd73754c4fae51033bb42ae 100644 (file)
@@ -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;
                         }
 
index dc40c8f8d6fd5d539a2df3bf2badec067556e55c..1f9da4d60476d91d84e08e17397cf35c1753117a 100644 (file)
@@ -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();
index 9ec6acb616ae64735bbe5bf5875b81efcd126547..5938acbd0b42e06bdd9d830716cad2bdba288419 100644 (file)
@@ -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
 
index 3e130c7946898d199ede5d56fe96692e50f968aa..202d9bb43c20bf3b5ee47d29e9a635d9a4b6db61 100644 (file)
@@ -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));