status

statusbar program for dwm
git clone anongit@rnpnr.xyz:status.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 886cd1344c5f2a0235c161d800fc1a79f7e2ac32
Parent: 92a698f3a164c06c5e54dd04df32d7ea85fd64ad
Author: Randy Palamar
Date:   Mon, 27 Feb 2023 10:21:27 -0700

add bat_arg struct

this allows a prefix and suffix to be added to the output of batinfo()
when the remaining percentage is less than a threshold

useful for making it more clear when you need to plug in the charger

Diffstat:
Mblocks/linux/battery.c | 26+++++++++++++++++---------
Mblocks/linux/battery.h | 7+++++++
Mblocks/openbsd/battery.c | 15+++++++++++----
Mblocks/openbsd/battery.h | 7+++++++
Mconfig.def.h | 8+++++---
5 files changed, 47 insertions(+), 16 deletions(-)

diff --git a/blocks/linux/battery.c b/blocks/linux/battery.c @@ -10,27 +10,32 @@ size_t batinfo(struct Block *b) { - char path[PATH_MAX], state[12], *bat = (char *)b->arg; - int perc; + const struct bat_arg *ba = b->arg; + char *pre = ba->pre ? ba->pre : ""; + char *suf = ba->suf ? ba->suf : ""; + char path[PATH_MAX], state[12]; + int perc, s; unsigned long power_now, energy_now, h, m; double timeleft; - snprintf(path, sizeof(path), "/sys/class/power_supply/%s/capacity", bat); + snprintf(path, sizeof(path), "/sys/class/power_supply/%s/capacity", ba->bat); if (pscanf(path, "%d", &perc) != 1) perc = 0; - snprintf(path, sizeof(path), "/sys/class/power_supply/%s/status", bat); + s = perc < ba->thres; + + snprintf(path, sizeof(path), "/sys/class/power_supply/%s/status", ba->bat); if (pscanf(path, "%12s", &state) != 1) snprintf(state, sizeof(state), "Unknown"); if (!strcmp(state, "Discharging")) { snprintf(path, sizeof(path), - "/sys/class/power_supply/%s/power_now", bat); + "/sys/class/power_supply/%s/power_now", ba->bat); if (pscanf(path, "%lu", &power_now) != 1) power_now = 1; snprintf(path, sizeof(path), - "/sys/class/power_supply/%s/energy_now", bat); + "/sys/class/power_supply/%s/energy_now", ba->bat); if (pscanf(path, "%lu", &energy_now) != 1) energy_now = 0; @@ -38,9 +43,12 @@ batinfo(struct Block *b) h = timeleft; m = (timeleft - (double)h) * 60; - snprintf(buf, sizeof(buf), "%d%% (%lu:%02lu)", perc, h, m); - } else - snprintf(buf, sizeof(buf), "%d%% (%s)", perc, state); + snprintf(buf, sizeof(buf), "%s%d%% (%lu:%02lu)%s", s ? pre : "", + perc, h, m, s ? suf : ""); + } else { + snprintf(buf, sizeof(buf), "%s%d%% (%s)%s", s ? pre : "", perc, + state, s ? suf : ""); + } return snprintf(b->curstr, LEN(b->curstr), b->fmt, buf); } diff --git a/blocks/linux/battery.h b/blocks/linux/battery.h @@ -1 +1,8 @@ +struct bat_arg { + char *bat; /* BAT name (ex. BAT0) */ + char *pre; /* prefix for percentages less than thres */ + char *suf; /* suffix for percentages less than thres */ + int thres; /* % threshold to consider low (-1 to disable) */ +}; + size_t batinfo(struct Block *b); diff --git a/blocks/openbsd/battery.c b/blocks/openbsd/battery.c @@ -12,8 +12,11 @@ size_t batinfo(struct Block *b) { + const struct bat_arg ba = b->arg; + char *pre = ba->pre ? ba->pre : ""; + char *suf = ba->suf ? ba->suf : ""; struct apm_power_info pi; - int fd; + int fd, s; if ((fd = open("/dev/apm", O_RDONLY)) < 0) die("open\n"); @@ -24,15 +27,19 @@ batinfo(struct Block *b) } close(fd); + s = pi.battery_life < ba->thres; + switch (pi.ac_state) { case APM_AC_OFF: - snprintf(buf, sizeof(buf), "%d%% (%d:%02d)", pi.battery_life, - pi.minutes_left / 60, pi.minutes_left % 60); + snprintf(buf, sizeof(buf), "%s%d%% (%d:%02d)%s", s ? pre : "", + pi.battery_life, pi.minutes_left / 60, + pi.minutes_left % 60, s ? suf : ""); case APM_AC_ON: case APM_BATT_CHARGING: snprintf(buf, sizeof(buf), "%d%% (ac)", pi.battery_life); default: - snprintf(buf, sizeof(buf), "%d%% (unknown)", pi.battery_life); + snprintf(buf, sizeof(buf), "%s%d%% (unknown)%s", s ? pre : "", + pi.battery_life, s ? suf : ""); } return snprintf(b->curstr, LEN(b->curstr), b->fmt, buf); } diff --git a/blocks/openbsd/battery.h b/blocks/openbsd/battery.h @@ -1 +1,8 @@ +struct bat_arg { + char *bat; /* BAT name (ignored on OpenBSD) */ + char *pre; /* prefix for percentages less than thres */ + char *suf; /* suffix for percentages less than thres */ + int thres; /* % threshold to consider low (-1 to disable) */ +}; + size_t batinfo(struct Block *b); diff --git a/config.def.h b/config.def.h @@ -18,12 +18,14 @@ const struct mpd_arg ma = { "localhost", "|", tags, 2 }; /* card is found with 'aplay -L', default is probably correct */ const struct vol_arg va = { "default", "Speaker" }; +/* check battery.h for info */ +const struct bat_arg ba = { "BAT0", NULL, NULL, -1 }; + /* status block definitions * * function description arg (ex) * - * batinfo battery percentage and status (char *) battery name (BAT0) - * 0 on OpenBSD + * batinfo battery percentage and status (struct bat_arg *) * blight backlight percentage (char *) backlight name (intel_backlight) * date date and time (char *) time fmt string (%R) * volume ALSA volume percentage (struct vol_arg *) @@ -36,7 +38,7 @@ const struct vol_arg va = { "default", "Speaker" }; */ struct Block blks[] = { /* fn fmt interval signal arg */ - { batinfo, "[ %s ]", 30, 0, "BAT0" }, + { batinfo, "[ %s ]", 30, 0, &ba }, { date, "[ %s ]", 20, 0, "%R" }, { NULL }, };