From 88a0c6923c29b810e906d30e091aaf22bfed02cb Mon Sep 17 00:00:00 2001 From: Tommie Gannert Date: Mon, 1 Aug 2016 00:24:40 +0100 Subject: Remove statusbuf from print_battery_info. --- src/print_battery_info.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 7fb2ab5..802424d 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -38,7 +38,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char time_t empty_time; struct tm *empty_tm; char buf[1024]; - char statusbuf[16]; char percentagebuf[16]; char remainingbuf[256]; char emptytimebuf[256]; @@ -53,7 +52,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char voltage = -1; charging_status_t status = CS_DISCHARGING; - memset(statusbuf, '\0', sizeof(statusbuf)); memset(percentagebuf, '\0', sizeof(percentagebuf)); memset(remainingbuf, '\0', sizeof(remainingbuf)); memset(emptytimebuf, '\0', sizeof(emptytimebuf)); @@ -63,9 +61,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char sprintf(batpath, path, number); INSTANCE(batpath); -#define BATT_STATUS_NAME(status) \ - (status == CS_CHARGING ? status_chr : (status == CS_DISCHARGING ? status_bat : (status == CS_UNKNOWN ? status_unk : status_full))) - #if defined(LINUX) if (!slurp(batpath, buf, sizeof(buf))) { OUTPUT_FULL_TEXT(format_down); @@ -139,8 +134,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char return; } - (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status)); - float percentage_remaining = (((float)remaining / (float)full_design) * 100); /* Some batteries report POWER_SUPPLY_CHARGE_NOW= when fully * charged, even though that’s plainly wrong. For people who chose to see @@ -248,8 +241,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char full_design = sysctl_rslt; - (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status)); - (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%s", present_rate, pct_mark); @@ -307,7 +298,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char break; } - (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status)); /* integer_battery_capacity is implied as battery_life is already in whole numbers. */ (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.00d%s", apm_info.battery_life, pct_mark); @@ -519,9 +509,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } if (is_full) - (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(CS_FULL)); - else - (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status)); + status = CS_FULL; /* * The envsys(4) ACPI routines do not appear to provide a 'time @@ -602,7 +590,22 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } if (BEGINS_WITH(walk + 1, "status")) { - outwalk += sprintf(outwalk, "%s", statusbuf); + const char *statusstr; + switch (status) { + case CS_CHARGING: + statusstr = status_chr; + break; + case CS_DISCHARGING: + statusstr = status_bat; + break; + case CS_FULL: + statusstr = status_full; + break; + default: + statusstr = status_unk; + } + + outwalk += sprintf(outwalk, "%s", statusstr); walk += strlen("status"); } else if (BEGINS_WITH(walk + 1, "percentage")) { outwalk += sprintf(outwalk, "%s", percentagebuf); -- cgit v1.2.3 From 0390dbf2e01470c0ec13f9e4f33cecabd2e3001e Mon Sep 17 00:00:00 2001 From: Tommie Gannert Date: Mon, 1 Aug 2016 00:33:19 +0100 Subject: Remove percentagebuf from print_battery_info. --- src/print_battery_info.c | 45 +++++++++++++++------------------------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 802424d..3e90484 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -38,7 +38,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char time_t empty_time; struct tm *empty_tm; char buf[1024]; - char percentagebuf[16]; char remainingbuf[256]; char emptytimebuf[256]; char consumptionbuf[256]; @@ -50,9 +49,9 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char remaining = -1, present_rate = -1, voltage = -1; + float percentage_remaining = -1; charging_status_t status = CS_DISCHARGING; - memset(percentagebuf, '\0', sizeof(percentagebuf)); memset(remainingbuf, '\0', sizeof(remainingbuf)); memset(emptytimebuf, '\0', sizeof(emptytimebuf)); memset(consumptionbuf, '\0', sizeof(consumptionbuf)); @@ -134,7 +133,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char return; } - float percentage_remaining = (((float)remaining / (float)full_design) * 100); + percentage_remaining = (((float)remaining / (float)full_design) * 100); /* Some batteries report POWER_SUPPLY_CHARGE_NOW= when fully * charged, even though that’s plainly wrong. For people who chose to see * the percentage calculated based on the last full capacity, we clamp the @@ -143,11 +142,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char if (last_full_capacity && percentage_remaining > 100) { percentage_remaining = 100; } - if (integer_battery_capacity) { - (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.00f%s", percentage_remaining, pct_mark); - } else { - (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%s", percentage_remaining, pct_mark); - } if (present_rate > 0 && status != CS_FULL) { float remaining_time; @@ -219,7 +213,8 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char return; } - present_rate = sysctl_rslt; + integer_battery_capacity = true; + percentage_remaining = sysctl_rslt; if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) { OUTPUT_FULL_TEXT(format_down); return; @@ -232,18 +227,15 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } state = sysctl_rslt; - if (state == 0 && present_rate == 100) + if (state == 0 && percentage_remaining == 100) status = CS_FULL; - else if ((state & ACPI_BATT_STAT_CHARGING) && present_rate < 100) + else if ((state & ACPI_BATT_STAT_CHARGING) && percentage_remaining < 100) status = CS_CHARGING; else status = CS_DISCHARGING; full_design = sysctl_rslt; - (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%s", - present_rate, pct_mark); - if (state == ACPI_BATT_STAT_DISCHARG) { int hours, minutes; minutes = remaining; @@ -251,7 +243,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char minutes -= (hours * 60); (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d", max(hours, 0), max(minutes, 0)); - if (strcasecmp(threshold_type, "percentage") == 0 && present_rate < low_threshold) { + if (strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) { START_COLOR("color_bad"); colorful_output = true; } else if (strcasecmp(threshold_type, "time") == 0 && remaining < (u_int)low_threshold) { @@ -298,8 +290,8 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char break; } - /* integer_battery_capacity is implied as battery_life is already in whole numbers. */ - (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.00d%s", apm_info.battery_life, pct_mark); + integer_battery_capacity = true; + percentage_remaining = apm_info.battery_life; if (status == CS_DISCHARGING && low_threshold > 0) { if (strcasecmp(threshold_type, "percentage") == 0 && apm_info.battery_life < low_threshold) { @@ -483,20 +475,9 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0)); } - float percentage_remaining = + percentage_remaining = (((float)remaining / (float)full_design) * 100); - if (integer_battery_capacity) - (void)snprintf(percentagebuf, - sizeof(percentagebuf), - "%d%s", - (int)percentage_remaining, pct_mark); - else - (void)snprintf(percentagebuf, - sizeof(percentagebuf), - "%.02f%s", - percentage_remaining, pct_mark); - /* * Handle percentage low_threshold here, and time low_threshold when * we have it. @@ -608,7 +589,11 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char outwalk += sprintf(outwalk, "%s", statusstr); walk += strlen("status"); } else if (BEGINS_WITH(walk + 1, "percentage")) { - outwalk += sprintf(outwalk, "%s", percentagebuf); + if (integer_battery_capacity) { + outwalk += sprintf(outwalk, "%.00f%s", percentage_remaining, pct_mark); + } else { + outwalk += sprintf(outwalk, "%.02f%s", percentage_remaining, pct_mark); + } walk += strlen("percentage"); } else if (BEGINS_WITH(walk + 1, "remaining")) { outwalk += sprintf(outwalk, "%s", remainingbuf); -- cgit v1.2.3 From 63724e2fa5969fcf22badfe00a56ce0355b7def7 Mon Sep 17 00:00:00 2001 From: Tommie Gannert Date: Mon, 1 Aug 2016 00:51:48 +0100 Subject: Remove remainingbuf from print_battery_info. --- src/print_battery_info.c | 81 +++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 53 deletions(-) diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 3e90484..6b20114 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -38,7 +38,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char time_t empty_time; struct tm *empty_tm; char buf[1024]; - char remainingbuf[256]; char emptytimebuf[256]; char consumptionbuf[256]; const char *walk, *last; @@ -48,11 +47,11 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char int full_design = -1, remaining = -1, present_rate = -1, - voltage = -1; + voltage = -1, + seconds_remaining = -1; float percentage_remaining = -1; charging_status_t status = CS_DISCHARGING; - memset(remainingbuf, '\0', sizeof(remainingbuf)); memset(emptytimebuf, '\0', sizeof(emptytimebuf)); memset(consumptionbuf, '\0', sizeof(consumptionbuf)); @@ -145,7 +144,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char if (present_rate > 0 && status != CS_FULL) { float remaining_time; - int seconds, hours, minutes, seconds_remaining; if (status == CS_CHARGING) remaining_time = ((float)full_design - (float)remaining) / (float)present_rate; else if (status == CS_DISCHARGING) @@ -155,11 +153,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char seconds_remaining = (int)(remaining_time * 3600.0); - hours = seconds_remaining / 3600; - seconds = seconds_remaining - (hours * 3600); - minutes = seconds / 60; - seconds -= (minutes * 60); - if (status == CS_DISCHARGING && low_threshold > 0) { if (strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) { START_COLOR("color_bad"); @@ -172,13 +165,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } } - if (hide_seconds) - (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d", - max(hours, 0), max(minutes, 0)); - else - (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d", - max(hours, 0), max(minutes, 0), max(seconds, 0)); - empty_time = time(NULL); empty_time += seconds_remaining; empty_tm = localtime(&empty_time); @@ -220,7 +206,8 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char return; } - remaining = sysctl_rslt; + hide_seconds = true; + seconds_remaining = sysctl_rslt * 60; if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) { OUTPUT_FULL_TEXT(format_down); return; @@ -237,16 +224,10 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char full_design = sysctl_rslt; if (state == ACPI_BATT_STAT_DISCHARG) { - int hours, minutes; - minutes = remaining; - hours = minutes / 60; - minutes -= (hours * 60); - (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d", - max(hours, 0), max(minutes, 0)); if (strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) { START_COLOR("color_bad"); colorful_output = true; - } else if (strcasecmp(threshold_type, "time") == 0 && remaining < (u_int)low_threshold) { + } else if (strcasecmp(threshold_type, "time") == 0 && seconds_remaining < 60 * low_threshold) { START_COLOR("color_bad"); colorful_output = true; } @@ -305,9 +286,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char /* Can't give a meaningful value for remaining minutes if we're charging. */ if (status != CS_CHARGING) { - (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d", apm_info.minutes_left / 60, apm_info.minutes_left % 60); - } else { - (void)snprintf(remainingbuf, sizeof(remainingbuf), "%s", "(CHR)"); + seconds_remaining = apm_info.minutes_left * 60; } if (colorful_output) @@ -497,7 +476,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char * remaining' figure, so we must deduce it. */ float remaining_time; - int seconds, hours, minutes, seconds_remaining; if (status == CS_CHARGING) remaining_time = ((float)full_design - (float)remaining) / (float)present_rate; @@ -508,32 +486,13 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char seconds_remaining = (int)(remaining_time * 3600.0); - hours = seconds_remaining / 3600; - seconds = seconds_remaining - (hours * 3600); - minutes = seconds / 60; - seconds -= (minutes * 60); - if (status != CS_CHARGING) { - if (hide_seconds) - (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d", - max(hours, 0), max(minutes, 0)); - else - (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d", - max(hours, 0), max(minutes, 0), max(seconds, 0)); - if (low_threshold > 0) { if (strcasecmp(threshold_type, "time") == 0 && ((float)seconds_remaining / 60.0) < (u_int)low_threshold) { START_COLOR("color_bad"); colorful_output = true; } } - } else { - if (hide_seconds) - (void)snprintf(remainingbuf, sizeof(remainingbuf), "(%02d:%02d until full)", - max(hours, 0), max(minutes, 0)); - else - (void)snprintf(remainingbuf, sizeof(remainingbuf), "(%02d:%02d:%02d until full)", - max(hours, 0), max(minutes, 0), max(seconds, 0)); } empty_time = time(NULL); @@ -554,9 +513,9 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char ((float)present_rate / 1000.0 / 1000.0)); #endif -#define EAT_SPACE_FROM_OUTPUT_IF_EMPTY(_buf) \ +#define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT() \ do { \ - if (strlen(_buf) == 0) { \ + if (outwalk == prevoutwalk) { \ if (outwalk > buffer && isspace(outwalk[-1])) \ outwalk--; \ else if (isspace(*(walk + 1))) \ @@ -565,6 +524,8 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } while (0) for (walk = format; *walk != '\0'; walk++) { + char *prevoutwalk = outwalk; + if (*walk != '%') { *(outwalk++) = *walk; continue; @@ -596,17 +557,31 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } walk += strlen("percentage"); } else if (BEGINS_WITH(walk + 1, "remaining")) { - outwalk += sprintf(outwalk, "%s", remainingbuf); + if (seconds_remaining >= 0) { + int seconds, hours, minutes; + + hours = seconds_remaining / 3600; + seconds = seconds_remaining - (hours * 3600); + minutes = seconds / 60; + seconds -= (minutes * 60); + + if (hide_seconds) + outwalk += sprintf(outwalk, "%02d:%02d", + max(hours, 0), max(minutes, 0)); + else + outwalk += sprintf(outwalk, "%02d:%02d:%02d", + max(hours, 0), max(minutes, 0), max(seconds, 0)); + } walk += strlen("remaining"); - EAT_SPACE_FROM_OUTPUT_IF_EMPTY(remainingbuf); + EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); } else if (BEGINS_WITH(walk + 1, "emptytime")) { outwalk += sprintf(outwalk, "%s", emptytimebuf); walk += strlen("emptytime"); - EAT_SPACE_FROM_OUTPUT_IF_EMPTY(emptytimebuf); + EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); } else if (BEGINS_WITH(walk + 1, "consumption")) { outwalk += sprintf(outwalk, "%s", consumptionbuf); walk += strlen("consumption"); - EAT_SPACE_FROM_OUTPUT_IF_EMPTY(consumptionbuf); + EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); } } -- cgit v1.2.3