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. --- src/print_battery_info.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src') 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