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.
This commit is contained in:
parent
441c420773
commit
d1c22762c4
3 changed files with 11 additions and 13 deletions
|
@ -258,8 +258,6 @@ void init_battery_panel(void *p)
|
|||
|
||||
|
||||
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 @@ int update_battery() {
|
|||
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;
|
||||
|
@ -346,9 +344,6 @@ int update_battery() {
|
|||
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;
|
||||
|
||||
// clamp percentage to 100 in case battery is misreporting that its current charge is more than its max
|
||||
|
|
|
@ -96,7 +96,7 @@ void battery_action(int button);
|
|||
#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
|
||||
|
||||
|
|
|
@ -48,6 +48,10 @@ struct psy_battery {
|
|||
|
||||
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 @@ static gboolean update_linux_battery(struct psy_battery *bat) {
|
|||
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 @@ void update_linux_batteries(enum chargestate *state, gint64 *energy_now, gint64
|
|||
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 @@ void update_linux_batteries(enum chargestate *state, gint64 *energy_now, gint64
|
|||
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 @@ char* linux_batteries_get_tooltip() {
|
|||
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);
|
||||
|
|
Loading…
Reference in a new issue