diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | config.def.mk | 12 | ||||
-rw-r--r-- | dwmclock.c | 84 |
3 files changed, 71 insertions, 26 deletions
@@ -2,5 +2,6 @@ *.swp *~ /dwmclock +/dwmclock-print /config.mk /compile_commands.json diff --git a/config.def.mk b/config.def.mk index a9dfe19..cfd6766 100644 --- a/config.def.mk +++ b/config.def.mk @@ -3,19 +3,25 @@ # enable battery charge display CFLAGS += -DBATTERY +# the string to match when the battery is charging; comment out to disable charging detection +CFLAGS += -DCHARGING=\"Charging\\n\" + +# enable battery power usage display; needs POWER_NOW or CURRENT_NOW+VOLTAGE_NOW +#CFLAGS += -DPOWER + # 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\" +CFLAGS += -DPOWER_NOW=\"/sys/class/power_supply/BAT0/power_now\" # possible alternative on some systems #CFLAGS += -DENERGY_FULL=\"/sys/class/power_supply/BAT0/charge_full\" #CFLAGS += -DENERGY_NOW=\"/sys/class/power_supply/BAT0/charge_now\" #CFLAGS += -DSTATUS=\"/sys/class/power_supply/ADP1/online\" #CFLAGS += -DCHARGING=\"1\\n\" +#CFLAGS += -DCURRENT_NOW=\"/sys/class/power_supply/BAT0/current_now\" +#CFLAGS += -DVOLTAGE_NOW=\"/sys/class/power_supply/BAT0/voltage_now\" # every how many seconds to refresh the battery charge display CFLAGS += -DBAT_REFRESH_SECONDS=10 @@ -70,12 +70,20 @@ int main(void) struct tm *tm; time_t now; #ifdef BATTERY - long long curr, max, a=-1, b=-1, i; - char name[8 + 9]; /* (p)pp.pp% hh:mm:ss\0 */ - char *perc = name; + long long ccur, cmax, ca=-1, cb=-1, i; +#ifdef POWER + long long pcur, pa=-1, pb=-1; + char name[8 + 6 + 9]; /* bbb.bb% pp.pW hh:mm:ss\0 */ + char *bat = name; + char *power = name + 8; + char *clock = name + 8 + 6; +#else /* POWER */ + char name[8 + 9]; /* bbb.bb% hh:mm:ss\0 */ + char *bat = name; char *clock = name + 8; +#endif /* POWER */ char sign = '%'; -#else +#else /* BATTERY */ char name[9]; /* hh:mm:ss\0 */ char *clock = name; #endif /* BATTERY */ @@ -112,41 +120,71 @@ int main(void) #ifdef BATTERY for (i=0; !stop; ++i) { -#else +#else /* BATTERY */ while (!stop) { #endif /* BATTERY */ #ifdef BATTERY if (i % (BAT_REFRESH_SECONDS) == 0) { - curr = getnum(ENERGY_NOW) * MUL; - max = getnum(ENERGY_FULL); + ccur = getnum(ENERGY_NOW) * MUL; + cmax = getnum(ENERGY_FULL); #ifdef CHARGING sign = charging() ? '+' : '%'; #endif /* CHARGING */ - if (curr < 0 || max < 1) { - a = b = -1; + if (ccur < 0 || cmax < 1) { + ca = cb = -1; } else { - a = curr * 10000 / max; - b = a % 100; - a /= 100; + ca = ccur * 10000 / cmax; + cb = ca % 100; + ca /= 100; } + +#ifdef POWER +#ifdef POWER_NOW + pcur = getnum(POWER_NOW); +#elif defined(CURRENT_NOW) && defined(VOLTAGE_NOW) /* POWER_NOW */ + pcur = getnum(CURRENT_NOW) * getnum(VOLTAGE_NOW) / 1000000; +#else /* POWER_NOW */ +#error "Either POWER_NOW or CURRENT_NOW and VOLTAGE_NOW must be defined" +#endif /* POWER_NOW*/ + if (pcur < 0) { + pa = pb = -1; + } else { + pa = pcur / 100000; + pb = pa % 10; + pa /= 10; + } +#endif /* POWER */ } - if (a < 0 || b < 0) { - snprintf(perc, 8, "NO BAT "); - } else if (a < 0 || a > 999999 || b < 0 || b > 99) { - snprintf(perc, 8, "INVALID"); - } else if (a > 999) { - snprintf(perc, 8, "%6lld%c", a, sign); + if (ca < 0 || cb < 0) { + snprintf(bat, 8, "NO BAT "); + } else if (ca < 0 || ca > 999999 || cb < 0 || cb > 99) { + snprintf(bat, 8, "INVALID"); + } else if (ca > 999) { + snprintf(bat, 8, "%6lld%c", ca, sign); + } else { + snprintf(bat, 8, "%3lld.%02lld%c", ca, cb, sign); + } + bat[7] = ' '; + +#ifdef POWER + if (pa < 0 || pb < 0) { + snprintf(power, 6, "NOPOW"); + } else if (pa < 0 || pa > 9999 || pb < 0 || pb > 9) { + snprintf(power, 6, "?????W"); + } else if (pa > 99) { + snprintf(power, 6, "%4lldW", pa); } else { - snprintf(perc, 8, "%3lld.%02lld%c", a, b, sign); + snprintf(power, 6, "%2lld.%01lldW", pa, (unsigned long long)pb); } - perc[7] = ' '; + power[5] = ' '; +#endif /* POWER */ #endif /* BATTERY */ #ifdef EXACT_SLEEP clock_gettime(CLOCK_REALTIME, &ts); now = ts.tv_sec; -#else +#else /* EXACT_SLEEP */ now = time(NULL); #endif /* EXACT_SLEEP */ tm = localtime(&now); @@ -155,7 +193,7 @@ int main(void) #ifdef PRINT printf("%s\n", name); fflush(stdout); -#else +#else /* PRINT */ XStoreName(disp, root, name); XFlush(disp); #endif /* PRINT */ @@ -164,7 +202,7 @@ int main(void) ts.tv_sec = 0; ts.tv_nsec = 1000000000 - ts.tv_nsec; nanosleep(&ts, NULL); -#else +#else /* EXACT_SLEEP */ sleep(1); #endif /* EXACT_SLEEP */ } |