From b890d2e416b23fd53b11bd6df240df5e0faae5f1 Mon Sep 17 00:00:00 2001 From: Tommie Gannert Date: Mon, 1 Aug 2016 01:00:31 +0100 Subject: Remove emptytimebuf from print_battery_info. This changes the behavior for NetBSD: previously this time was not shown while charging. On Linux it was treated as "full time". This change makes all OSes behave the same. OpenBSD and FreeBSD did not support emptytime previously. --- src/print_battery_info.c | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 6b20114..979d0c1 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -35,10 +35,7 @@ * */ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, const char *status_chr, const char *status_bat, const char *status_unk, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds) { - time_t empty_time; - struct tm *empty_tm; char buf[1024]; - char emptytimebuf[256]; char consumptionbuf[256]; const char *walk, *last; char *outwalk = buffer; @@ -52,7 +49,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char float percentage_remaining = -1; charging_status_t status = CS_DISCHARGING; - memset(emptytimebuf, '\0', sizeof(emptytimebuf)); memset(consumptionbuf, '\0', sizeof(consumptionbuf)); static char batpath[512]; @@ -165,17 +161,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } } - empty_time = time(NULL); - empty_time += seconds_remaining; - empty_tm = localtime(&empty_time); - - if (hide_seconds) - (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d", - max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0)); - else - (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d", - max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0)); - (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW", ((float)present_rate / 1000.0 / 1000.0)); } else { @@ -495,20 +480,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } } - empty_time = time(NULL); - empty_time += seconds_remaining; - empty_tm = localtime(&empty_time); - - /* No need to show empty time if battery is charging */ - if (status != CS_CHARGING) { - if (hide_seconds) - (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d", - max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0)); - else - (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d", - max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0)); - } - (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW", ((float)present_rate / 1000.0 / 1000.0)); #endif @@ -575,7 +546,17 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char walk += strlen("remaining"); EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); } else if (BEGINS_WITH(walk + 1, "emptytime")) { - outwalk += sprintf(outwalk, "%s", emptytimebuf); + if (seconds_remaining >= 0) { + time_t empty_time = time(NULL) + seconds_remaining; + struct tm *empty_tm = localtime(&empty_time); + + if (hide_seconds) + outwalk += sprintf(outwalk, "%02d:%02d", + max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0)); + else + outwalk += sprintf(outwalk, "%02d:%02d:%02d", + max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0)); + } walk += strlen("emptytime"); EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); } else if (BEGINS_WITH(walk + 1, "consumption")) { -- cgit v1.2.3 From 47ec48b85d7896b19a4432a01b535ad904254ff2 Mon Sep 17 00:00:00 2001 From: Tommie Gannert Date: Mon, 1 Aug 2016 01:07:23 +0100 Subject: Remove consumptionbuf from print_battery_info. --- src/print_battery_info.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 979d0c1..09db0a2 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -36,7 +36,6 @@ */ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, const char *status_chr, const char *status_bat, const char *status_unk, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds) { char buf[1024]; - char consumptionbuf[256]; const char *walk, *last; char *outwalk = buffer; bool watt_as_unit = false; @@ -49,8 +48,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char float percentage_remaining = -1; charging_status_t status = CS_DISCHARGING; - memset(consumptionbuf, '\0', sizeof(consumptionbuf)); - static char batpath[512]; sprintf(batpath, path, number); INSTANCE(batpath); @@ -160,9 +157,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char colorful_output = false; } } - - (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW", - ((float)present_rate / 1000.0 / 1000.0)); } else { /* On some systems, present_rate may not exist. Still, make sure * we colorize the output if threshold_type is set to percentage @@ -479,9 +473,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } } } - - (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW", - ((float)present_rate / 1000.0 / 1000.0)); #endif #define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT() \ @@ -560,7 +551,9 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char walk += strlen("emptytime"); EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); } else if (BEGINS_WITH(walk + 1, "consumption")) { - outwalk += sprintf(outwalk, "%s", consumptionbuf); + if (present_rate >= 0) + outwalk += sprintf(outwalk, "%1.2fW", present_rate / 1e6); + walk += strlen("consumption"); EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); } -- cgit v1.2.3