Merge pull request #144 from tommie/incremental_multibatt
Refactor color code and computation of seconds_remaining in print_battery_info
This commit is contained in:
commit
2d16f144f5
@ -28,6 +28,20 @@
|
|||||||
#include <sys/envsys.h>
|
#include <sys/envsys.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Estimate the number of seconds remaining in state 'status'.
|
||||||
|
*
|
||||||
|
* Assumes a constant (dis)charge rate.
|
||||||
|
*/
|
||||||
|
static int seconds_remaining_from_rate(charging_status_t status, float full_design, float remaining, float present_rate) {
|
||||||
|
if (status == CS_CHARGING)
|
||||||
|
return 3600.0 * (full_design - remaining) / present_rate;
|
||||||
|
else if (status == CS_DISCHARGING)
|
||||||
|
return 3600.0 * remaining / present_rate;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get battery information from /sys. Note that it uses the design capacity to
|
* Get battery information from /sys. Note that it uses the design capacity to
|
||||||
* calculate the percentage, not the last full capacity, so you can see how
|
* calculate the percentage, not the last full capacity, so you can see how
|
||||||
@ -39,7 +53,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
|
|||||||
const char *walk, *last;
|
const char *walk, *last;
|
||||||
char *outwalk = buffer;
|
char *outwalk = buffer;
|
||||||
bool watt_as_unit = false;
|
bool watt_as_unit = false;
|
||||||
bool colorful_output = false;
|
|
||||||
int full_design = -1,
|
int full_design = -1,
|
||||||
remaining = -1,
|
remaining = -1,
|
||||||
present_rate = -1,
|
present_rate = -1,
|
||||||
@ -136,37 +149,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (present_rate > 0 && status != CS_FULL) {
|
if (present_rate > 0 && status != CS_FULL) {
|
||||||
float remaining_time;
|
seconds_remaining = seconds_remaining_from_rate(status, full_design, remaining, present_rate);
|
||||||
if (status == CS_CHARGING)
|
|
||||||
remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
|
|
||||||
else if (status == CS_DISCHARGING)
|
|
||||||
remaining_time = ((float)remaining / (float)present_rate);
|
|
||||||
else
|
|
||||||
remaining_time = 0;
|
|
||||||
|
|
||||||
seconds_remaining = (int)(remaining_time * 3600.0);
|
|
||||||
|
|
||||||
if (status == CS_DISCHARGING && low_threshold > 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 && seconds_remaining < 60 * low_threshold) {
|
|
||||||
START_COLOR("color_bad");
|
|
||||||
colorful_output = true;
|
|
||||||
} else {
|
|
||||||
colorful_output = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
/* On some systems, present_rate may not exist. Still, make sure
|
|
||||||
* we colorize the output if threshold_type is set to percentage
|
|
||||||
* (since we don't have any information on remaining time). */
|
|
||||||
if (status == CS_DISCHARGING && low_threshold > 0) {
|
|
||||||
if (strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) {
|
|
||||||
START_COLOR("color_bad");
|
|
||||||
colorful_output = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
||||||
int state;
|
int state;
|
||||||
@ -201,16 +184,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
|
|||||||
status = CS_DISCHARGING;
|
status = CS_DISCHARGING;
|
||||||
|
|
||||||
full_design = sysctl_rslt;
|
full_design = sysctl_rslt;
|
||||||
|
|
||||||
if (state == ACPI_BATT_STAT_DISCHARG) {
|
|
||||||
if (strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) {
|
|
||||||
START_COLOR("color_bad");
|
|
||||||
colorful_output = true;
|
|
||||||
} else if (strcasecmp(threshold_type, "time") == 0 && seconds_remaining < 60 * low_threshold) {
|
|
||||||
START_COLOR("color_bad");
|
|
||||||
colorful_output = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#elif defined(__OpenBSD__)
|
#elif defined(__OpenBSD__)
|
||||||
/*
|
/*
|
||||||
* We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
|
* We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
|
||||||
@ -253,23 +226,10 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
|
|||||||
integer_battery_capacity = true;
|
integer_battery_capacity = true;
|
||||||
percentage_remaining = apm_info.battery_life;
|
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) {
|
|
||||||
START_COLOR("color_bad");
|
|
||||||
colorful_output = true;
|
|
||||||
} else if (strcasecmp(threshold_type, "time") == 0 && apm_info.minutes_left < (u_int)low_threshold) {
|
|
||||||
START_COLOR("color_bad");
|
|
||||||
colorful_output = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Can't give a meaningful value for remaining minutes if we're charging. */
|
/* Can't give a meaningful value for remaining minutes if we're charging. */
|
||||||
if (status != CS_CHARGING) {
|
if (status != CS_CHARGING) {
|
||||||
seconds_remaining = apm_info.minutes_left * 60;
|
seconds_remaining = apm_info.minutes_left * 60;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (colorful_output)
|
|
||||||
END_COLOR;
|
|
||||||
#elif defined(__NetBSD__)
|
#elif defined(__NetBSD__)
|
||||||
/*
|
/*
|
||||||
* Using envsys(4) via sysmon(4).
|
* Using envsys(4) via sysmon(4).
|
||||||
@ -436,17 +396,6 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
|
|||||||
percentage_remaining =
|
percentage_remaining =
|
||||||
(((float)remaining / (float)full_design) * 100);
|
(((float)remaining / (float)full_design) * 100);
|
||||||
|
|
||||||
/*
|
|
||||||
* Handle percentage low_threshold here, and time low_threshold when
|
|
||||||
* we have it.
|
|
||||||
*/
|
|
||||||
if (status == CS_DISCHARGING && low_threshold > 0) {
|
|
||||||
if (strcasecmp(threshold_type, "percentage") == 0 && (((float)remaining / (float)full_design) * 100) < low_threshold) {
|
|
||||||
START_COLOR("color_bad");
|
|
||||||
colorful_output = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_full)
|
if (is_full)
|
||||||
status = CS_FULL;
|
status = CS_FULL;
|
||||||
|
|
||||||
@ -454,26 +403,20 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
|
|||||||
* The envsys(4) ACPI routines do not appear to provide a 'time
|
* The envsys(4) ACPI routines do not appear to provide a 'time
|
||||||
* remaining' figure, so we must deduce it.
|
* remaining' figure, so we must deduce it.
|
||||||
*/
|
*/
|
||||||
float remaining_time;
|
seconds_remaining = seconds_remaining_from_rate(status, full_design, remaining, present_rate);
|
||||||
|
#endif
|
||||||
|
|
||||||
if (status == CS_CHARGING)
|
bool colorful_output = false;
|
||||||
remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
|
|
||||||
else if (status == CS_DISCHARGING)
|
|
||||||
remaining_time = ((float)remaining / (float)present_rate);
|
|
||||||
else
|
|
||||||
remaining_time = 0;
|
|
||||||
|
|
||||||
seconds_remaining = (int)(remaining_time * 3600.0);
|
if (status == CS_DISCHARGING && low_threshold > 0) {
|
||||||
|
if (percentage_remaining >= 0 && strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) {
|
||||||
if (status != CS_CHARGING) {
|
START_COLOR("color_bad");
|
||||||
if (low_threshold > 0) {
|
colorful_output = true;
|
||||||
if (strcasecmp(threshold_type, "time") == 0 && ((float)seconds_remaining / 60.0) < (u_int)low_threshold) {
|
} else if (seconds_remaining >= 0 && strcasecmp(threshold_type, "time") == 0 && seconds_remaining < 60 * low_threshold) {
|
||||||
START_COLOR("color_bad");
|
START_COLOR("color_bad");
|
||||||
colorful_output = true;
|
colorful_output = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT() \
|
#define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT() \
|
||||||
do { \
|
do { \
|
||||||
|
Loading…
Reference in New Issue
Block a user