From ad92c60d6b3263888652762e4d90e67ed45a1563 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 9 Mar 2014 14:03:36 +0100 Subject: [PATCH] Compute mean percentage for multiple batteries. If a laptop has multiple batteries, compute the mean percentage if the user asks for 'all' instead of a specific battery. --- src/linux.cc | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/linux.cc b/src/linux.cc index 30536778..684bfed3 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -2095,7 +2095,8 @@ void get_battery_short_status(char *buffer, unsigned int n, const char *bat) int get_battery_perct(const char *bat) { static int rep = 0; - int idx; + int idx, n = 0, total_capacity = 0; + char battery[8]; char acpi_path[128]; char sysfs_path[128]; int remaining_capacity = -1; @@ -2105,6 +2106,22 @@ int get_battery_perct(const char *bat) init_batteries(); + if (!strcmp(bat, "all")) { + for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) { + snprintf(battery, sizeof(battery) - 1, "BAT%d", idx); + remaining_capacity = get_battery_perct(battery); + if (remaining_capacity > 0) { + total_capacity += remaining_capacity; + n++; + } + } + + if (n == 0) + return 0; + else + return total_capacity / n; + } + idx = get_battery_idx(bat); /* don't update battery too often */