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.
This commit is contained in:
Simon Elsbrock 2012-08-23 16:42:38 +02:00 committed by Michael Stapelberg
parent 3baf27bf1d
commit 68f438ec9e
4 changed files with 23 additions and 12 deletions

View 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;
}

View 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();

View 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

View 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));