From 262fd581a79f61ee99b018938c480cf318ba6bd6 Mon Sep 17 00:00:00 2001 From: clsr Date: Wed, 26 Apr 2017 10:41:48 +0200 Subject: Deduplicate file reading code and add battery charging indicator --- Makefile | 2 +- README.md | 6 +++--- config.def.mk | 3 +++ dwmclock.c | 57 ++++++++++++++++++++++++++++++++++++++++----------------- 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 9aa9a59..753018d 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ $(MAIN): $(OBJ) $(CC) $(LDFLAGS) -o $@ $(OBJ) .c.o: - $(CC) -c $(CFLAGS) $< -o $@ + $(CC) -c $(CFLAGS) $< -o $@ -O2 clean: -rm -f $(MAIN) $(OBJ) diff --git a/README.md b/README.md index 7761fd4..06f333b 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,9 @@ Compile it: Install dwmclock (needs to be run as root if installing to `/usr` or `/usr/local`): - make instal + make install -Install dwmclock to ~/bin: +Install dwmclock to `~/bin`: make DESTDIR=$HOME PREFIX= install @@ -30,6 +30,6 @@ Install dwmclock to ~/bin: Usage ----- -Amp it off in your ~/.xinitrc: +Amp it off in your `~/.xinitrc`: dwmclock & diff --git a/config.def.mk b/config.def.mk index a01d057..1860aaf 100644 --- a/config.def.mk +++ b/config.def.mk @@ -6,6 +6,9 @@ CFLAGS+=-DBATTERY # set this to the correct path for your current system CFLAGS+=-DENERGY_FULL=\"/sys/class/power_supply/BAT0/energy_full\" CFLAGS+=-DENERGY_NOW=\"/sys/class/power_supply/BAT0/energy_now\" +CFLAGS+=-DSTATUS=\"/sys/class/power_supply/BAT0/status\" +# the string to match when the battery is charging; comment out to disable charging detection +CFLAGS+=-DCHARGING=\"Charging\\n\" # possible alternative on some systems #CFLAGS+=-DENERGY_FULL=\"/sys/class/power_supply/BAT0/charge_full\" diff --git a/dwmclock.c b/dwmclock.c index cc4b5b2..e7fd938 100644 --- a/dwmclock.c +++ b/dwmclock.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -12,29 +13,47 @@ int stop = 0; #ifdef BATTERY -int getnum(char *fn) -{ - char buf[1024]; +char *readfile(char *buf, size_t len, const char *fn) { FILE *f; - int len; + size_t n; f = fopen(fn, "r"); if (!f) { - return -1; + return NULL; } - len = fread(buf, 1, 1023, f); + n = fread(buf, 1, len-1, f); if (ferror(f)) { - return -1; + return NULL; } fclose(f); - if (len > 1022 || len < 1) { - return -1; + if (n >= len-1 || n < 1) { + return NULL; } - buf[1023] = '\0'; + buf[n] = '\0'; + + return buf; +} - return atoi(buf); +int getnum(const char *fn) { + char buf[1024]; + + if (readfile(buf, sizeof(buf), fn)) { + return atoi(buf); + } + return -1; +} + +#ifdef CHARGING +int charging(void) { + char buf[1024]; + + if (readfile(buf, sizeof(buf), STATUS)) { + return !strcmp(buf, CHARGING); + } + return 0; } -#endif +#endif /* CHARGING */ +#endif /* BATTERY */ void handler(int sig) { @@ -50,10 +69,11 @@ int main(void) char name[8 + 9]; /* (p)pp.pp% hh:mm:ss\0 */ char *perc = name; char *clock = name + 8; + char sign = '%'; #else char name[9]; /* hh:mm:ss\0 */ char *clock = name; -#endif +#endif /* BATTERY */ struct sigaction act; Display *disp; int screen; @@ -73,7 +93,7 @@ int main(void) for (i=0; !stop; ++i) { #else while (!stop) { -#endif +#endif /* BATTERY */ if (!disp) { puts("Cannot open display!"); return 1; @@ -84,6 +104,9 @@ int main(void) if (i % (BAT_REFRESH_SECONDS) == 0) { curr = getnum(ENERGY_NOW) * MUL; max = getnum(ENERGY_FULL); +#ifdef CHARGING + sign = charging() ? '+' : '%'; +#endif /* CHARGING */ if (curr < 0 || max < 1) { a = b = -1; } else { @@ -98,12 +121,12 @@ int main(void) } else if (a < 0 || a > 999999 || b < 0 || b > 99) { snprintf(perc, 8, "INVALID"); } else if (a > 999) { - snprintf(perc, 8, "%6lld%%", a); + snprintf(perc, 8, "%6lld%c", a, sign); } else { - snprintf(perc, 8, "%3lld.%02lld%%", a, b); + snprintf(perc, 8, "%3lld.%02lld%c", a, b, sign); } perc[7] = ' '; -#endif +#endif /* BATTERY */ now = time(NULL); tm = localtime(&now); -- cgit