xbattmon

simple battery monitor for X
git clone git://git.2f30.org/xbattmon
Log | Files | Refs | README | LICENSE

commit fe4305206c16d37d7454f29aa9ad2bac9a4ebe85
parent cccdefc87720f4413b48e9e351ced3ed752c5207
Author: sin <sin@2f30.org>
Date:   Wed,  2 Aug 2017 18:19:57 +0100

Rework multiple battery support

Some systems do not have energy_{full,now} files but instead only have
charge_{full,now} files.  This can now be configured through the
makefile instead of having to edit the .c file.

Diffstat:
Mconfig.linux.mk | 3++-
Mxbattmon.c | 46+++++++++++++++++++++-------------------------
2 files changed, 23 insertions(+), 26 deletions(-)

diff --git a/config.linux.mk b/config.linux.mk @@ -3,6 +3,7 @@ MANPREFIX = $(PREFIX)/share/man CPPFLAGS = \ -DVERSION=\"${VERSION}\" \ - -DPATH_LIST_BAT='{ "/sys/class/power_supply/BAT0", "/sys/class/power_supply/BAT1" }' \ + -DPATH_FMT_BAT_FULL=\"/sys/class/power_supply/BAT%d/energy_full\" \ + -DPATH_FMT_BAT_NOW=\"/sys/class/power_supply/BAT%d/energy_now\" \ -DPATH_AC_ONLINE=\"/sys/class/power_supply/AC/online\" LDLIBS = -lX11 diff --git a/xbattmon.c b/xbattmon.c @@ -270,43 +270,39 @@ void pollbat(void) { FILE *fp; - char *path_list_bat[] = PATH_LIST_BAT; - char path_energy_full[PATH_MAX]; - char path_energy_now[PATH_MAX]; - int total_energy_full = 0; - int total_energy_now = 0; - int energy_full; - int energy_now; + char tmp[PATH_MAX]; + int total_full = 0, total_now = 0; + int full, now; int acon; - int i; + int i = 0; - for (i = 0; i < LEN(path_list_bat); i++) { - snprintf(path_energy_full, sizeof(path_energy_full), - "%s/energy_full", path_list_bat[i]); - fp = fopen(path_energy_full, "r"); + for (;;) { + snprintf(tmp, sizeof(tmp), PATH_FMT_BAT_FULL, i); + fp = fopen(tmp, "r"); if (!fp) { - warn("fopen %s", path_energy_full); - continue; + warn("fopen %s", tmp); + break; } - fscanf(fp, "%d", &energy_full); + fscanf(fp, "%d", &full); fclose(fp); - snprintf(path_energy_now, sizeof(path_energy_now), - "%s/energy_now", path_list_bat[i]); - fp = fopen(path_energy_now, "r"); + snprintf(tmp, sizeof(tmp), PATH_FMT_BAT_NOW, i); + fp = fopen(tmp, "r"); if (!fp) { - warn("fopen %s", path_energy_now); - continue; + warn("fopen %s", tmp); + break; } - fscanf(fp, "%d", &energy_now); + fscanf(fp, "%d", &now); fclose(fp); - total_energy_full += energy_full / 1000; - total_energy_now += energy_now / 1000; + total_full += full / 1000; + total_now += now / 1000; + + i++; } - if (total_energy_full > 0) - batcap = 100 * total_energy_now / total_energy_full; + if (total_full > 0) + batcap = 100 * total_now / total_full; else batcap = 0;