1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-17 10:35:10 +00:00

Address PR comments.

In particular:
- Don't call get_battery_perct() recursively.
- Use define for length of string holding battery name.
- We can use strcpy() since bat is zero-terminated.
This commit is contained in:
Vilmos Nebehaj 2014-03-09 21:32:15 +01:00
parent f39de709e3
commit cda5afda69

View File

@ -2092,11 +2092,10 @@ void get_battery_short_status(char *buffer, unsigned int n, const char *bat)
} }
} }
int get_battery_perct(const char *bat) int _get_battery_perct(const char *bat)
{ {
static int rep = 0; static int rep = 0;
int idx, n = 0, total_capacity = 0; int idx;
char battery[8];
char acpi_path[128]; char acpi_path[128];
char sysfs_path[128]; char sysfs_path[128];
int remaining_capacity = -1; int remaining_capacity = -1;
@ -2104,24 +2103,6 @@ int get_battery_perct(const char *bat)
snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat); snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat); snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat);
init_batteries();
if (!strncmp(bat, "all", strlen(bat))) {
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); idx = get_battery_idx(bat);
/* don't update battery too often */ /* don't update battery too often */
@ -2212,6 +2193,35 @@ int get_battery_perct(const char *bat)
return last_battery_perct[idx]; return last_battery_perct[idx];
} }
int get_battery_perct(const char *bat)
{
int idx, n = 0, total_capacity = 0, remaining_capacity;;
#define BATTERY_LEN 8
char battery[BATTERY_LEN];
init_batteries();
/* Check if user asked for the mean percentage of all batteries. */
if (!strcmp(bat, "all")) {
for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
snprintf(battery, BATTERY_LEN - 1, "BAT%d", idx);
#undef BATTERY_LEN
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;
} else {
return _get_battery_perct(bat);
}
}
double get_battery_perct_bar(struct text_object *obj) double get_battery_perct_bar(struct text_object *obj)
{ {
int idx; int idx;