all repos — tint2 @ d1c22762c40b601f092925799b191ff54d559962

fork of the tint2 desktop panel for my custom setup - only minimized windows across all desktops for the taskbar

battery: move percentage calculation to linux code

Only Linux provides detailed energy statistics. The BSDs directly
provide percentage values. This change makes it easier to split
out the BSD code and reduces code overhead on BSD systems.
Sebastian Reichel sre@ring0.de
commit

d1c22762c40b601f092925799b191ff54d559962

parent

441c42077323c084542698367e73645fa11d76f4

3 files changed, 11 insertions(+), 13 deletions(-)

jump to
M src/battery/battery.csrc/battery/battery.c

@@ -258,8 +258,6 @@ }

int update_battery() { - int64_t energy_now = 0, - energy_full = 0; int seconds = 0; int8_t new_percentage = 0; int errors = 0;

@@ -337,7 +335,7 @@ new_percentage = -1;

else new_percentage = sysctl_out; #else - update_linux_batteries(&battery_state.state, &energy_now, &energy_full, &seconds); + update_linux_batteries(&battery_state.state, &new_percentage, &seconds); #endif battery_state.time.hours = seconds / 3600;

@@ -345,9 +343,6 @@ seconds -= 3600 * battery_state.time.hours;

battery_state.time.minutes = seconds / 60; seconds -= 60 * battery_state.time.minutes; battery_state.time.seconds = seconds; - - if (energy_full > 0) - new_percentage = 0.5 + ((energy_now <= energy_full ? energy_now : energy_full) * 100.0) / energy_full; battery_state.percentage = new_percentage;
M src/battery/battery.hsrc/battery/battery.h

@@ -96,7 +96,7 @@

#ifdef __linux gboolean init_linux_batteries(); void free_linux_batteries(); -void update_linux_batteries(enum chargestate *state, gint64 *energy_now, gint64 *energy_full, int *seconds); +void update_linux_batteries(enum chargestate *state, int8_t *percentage, int *seconds); char* linux_batteries_get_tooltip(); #endif
M src/battery/linux.csrc/battery/linux.c

@@ -48,6 +48,10 @@ #define RETURN_ON_ERROR(err) if(error) { g_error_free(err); return FALSE; }

static GList *batteries = NULL; +static guint8 energy_to_percent(gint energy_now, gint energy_full) { + return 0.5 + ((energy_now <= energy_full ? energy_now : energy_full) * 100.0) / energy_full; +} + static gboolean power_supply_is_battery(const gchar *entryname) { gchar *path_type = g_build_filename("/sys/class/power_supply", entryname, "type", NULL); GError *error = NULL;

@@ -237,7 +241,7 @@

return TRUE; } -void update_linux_batteries(enum chargestate *state, gint64 *energy_now, gint64 *energy_full, int *seconds) { +void update_linux_batteries(enum chargestate *state, int8_t *percentage, int *seconds) { GList *l; gint64 total_energy_now = 0;

@@ -261,10 +265,6 @@ discharging |= (bat->status == BATTERY_DISCHARGING);

full |= (bat->status == BATTERY_FULL); } - /* global energy stats */ - *energy_now = total_energy_now; - *energy_full = total_energy_full; - /* build global state */ *state = BATTERY_UNKNOWN; if (charging && !discharging)

@@ -282,6 +282,9 @@ *seconds = 3600 * (total_energy_full - total_energy_now) / total_power_now;

else if(*state == BATTERY_DISCHARGING) *seconds = 3600 * total_energy_now / total_power_now; } + + /* calculate percentage */ + *percentage = energy_to_percent(total_energy_now, total_energy_full); } static gchar* energy_human_readable(struct psy_battery *bat) {

@@ -341,7 +344,7 @@ gchar *power = power_human_readable(bat);

gchar *energy = energy_human_readable(bat); gchar *state = (bat->status == BATTERY_UNKNOWN) ? "Level" : chargestate2str(bat->status); - guint percentage = 0.5 + ((bat->energy_now <= bat->energy_full ? bat->energy_now : bat->energy_full) * 100.0) / bat->energy_full; + guint8 percentage = energy_to_percent(bat->energy_now, bat->energy_full); g_string_append_printf(tooltip, "\t%s: %s (%u %%)\n\tPower: %s", state, energy, percentage, power);