status

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

Commit: 4de785f8112a0e51c473d11acebe42df1ab43be9
Parent: 854529d83778872b9e4e74226cb41f4946c21e58
Author: Randy Palamar
Date:   Sat,  4 Jun 2022 13:53:29 -0600

move os specific functions to subdirectories

Diffstat:
MMakefile | 2+-
Dblocks/battery.c | 82-------------------------------------------------------------------------------
Dblocks/blight_linux.c | 30------------------------------
Ablocks/linux/battery.c | 46++++++++++++++++++++++++++++++++++++++++++++++
Rblocks/battery.h -> blocks/linux/battery.h | 0
Ablocks/linux/blight.c | 30++++++++++++++++++++++++++++++
Rblocks/blight_linux.h -> blocks/linux/blight.h | 0
Ablocks/linux/volume.c | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rblocks/volume.h -> blocks/linux/volume.h | 0
Ablocks/openbsd/battery.c | 38++++++++++++++++++++++++++++++++++++++
Rblocks/battery.h -> blocks/openbsd/battery.h | 0
Dblocks/volume.c | 59-----------------------------------------------------------
Mconfig.def.h | 7++++---
Mconfig.mk | 8++++----
14 files changed, 179 insertions(+), 179 deletions(-)

diff --git a/Makefile b/Makefile @@ -18,7 +18,7 @@ status: $(OBJ) $(CC) -o $@ $(OBJ) $(LDFLAGS) clean: - rm -f blocks/*.o *.o status + rm -f $(OBJ) status install: all mkdir -p $(DESTDIR)$(PREFIX)/bin diff --git a/blocks/battery.c b/blocks/battery.c @@ -1,82 +0,0 @@ -/* See LICENSE for license details. */ -#include <limits.h> -#include <stdio.h> -#include <string.h> - -#include "../status.h" -#include "../util.h" -#include "battery.h" - -#if defined(__linux__) -size_t -batinfo(struct Block *b) -{ - static char path[PATH_MAX], state[12]; - int perc; - unsigned long power_now, energy_now, h, m; - double timeleft; - - snprintf(path, sizeof(path), "/sys/class/power_supply/%s/capacity", b->u.s); - if (pscanf(path, "%d", &perc) != 1) - perc = 0; - - snprintf(path, sizeof(path), "/sys/class/power_supply/%s/status", b->u.s); - 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", b->u.s); - if (pscanf(path, "%lu", &power_now) != 1) - power_now = 1; - - snprintf(path, sizeof(path), - "/sys/class/power_supply/%s/energy_now", b->u.s); - if (pscanf(path, "%lu", &energy_now) != 1) - energy_now = 0; - - timeleft = (double)energy_now / (double)power_now; - h = timeleft; - m = (timeleft - (double)h) * 60; - - snprintf(buf, sizeof(buf), "%d%% (%d:%02d)", perc, h, m); - } else - snprintf(buf, sizeof(buf), "%d%% (%s)", perc, state); - - return snprintf(b->curstr, LEN(b->curstr), b->fmt, buf); -} - -#elif defined(__OpenBSD__) -#include <fcntl.h> -#include <machine/apmvar.h> -#include <sys/ioctl.h> -#include <unistd.h> - -size_t -batinfo(struct Block *b) -{ - struct apm_power_info pi; - int fd; - - if ((fd = open("/dev/apm", O_RDONLY)) < 0) - die("open\n"); - - if ((ioctl(fd, APM_IOC_GETPOWER, &pi)) < 0) { - close(fd); - die("ioctl\n"); - } - close(fd); - - 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); - 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); - } - return snprintf(b->curstr, LEN(b->curstr), b->fmt, buf); -} -#endif diff --git a/blocks/blight_linux.c b/blocks/blight_linux.c @@ -1,30 +0,0 @@ -/* See LICENSE for license details. */ -#include <limits.h> -#include <stdio.h> -#include <string.h> - -#include "../status.h" -#include "../util.h" -#include "blight_linux.h" - -size_t -blight(struct Block *b) -{ - char path[PATH_MAX]; - int perc; - unsigned long max, now; - - snprintf(path, sizeof(path), "/sys/class/backlight/%s/brightness", b->u.s); - if (pscanf(path, "%lu", &now) != 1) - now = 0; - - snprintf(path, sizeof(path), "/sys/class/backlight/%s/max_brightness", b->u.s); - if (pscanf(path, "%lu", &max) != 1) - /* avoid divison by 0 */ - max = 1; - - perc = 100 * now / max; - snprintf(buf, sizeof(buf), "%d%%", perc); - - return snprintf(b->curstr, LEN(b->curstr), b->fmt, buf); -} diff --git a/blocks/linux/battery.c b/blocks/linux/battery.c @@ -0,0 +1,46 @@ +/* See LICENSE for license details. */ +#include <limits.h> +#include <stdio.h> +#include <string.h> + +#include "../../status.h" +#include "../../util.h" +#include "battery.h" + +size_t +batinfo(struct Block *b) +{ + char path[PATH_MAX], state[12]; + int perc; + unsigned long power_now, energy_now, h, m; + double timeleft; + + snprintf(path, sizeof(path), "/sys/class/power_supply/%s/capacity", b->u.s); + if (pscanf(path, "%d", &perc) != 1) + perc = 0; + + snprintf(path, sizeof(path), "/sys/class/power_supply/%s/status", b->u.s); + 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", b->u.s); + if (pscanf(path, "%lu", &power_now) != 1) + power_now = 1; + + snprintf(path, sizeof(path), + "/sys/class/power_supply/%s/energy_now", b->u.s); + if (pscanf(path, "%lu", &energy_now) != 1) + energy_now = 0; + + timeleft = (double)energy_now / (double)power_now; + h = timeleft; + m = (timeleft - (double)h) * 60; + + snprintf(buf, sizeof(buf), "%d%% (%d:%02d)", perc, h, m); + } else + snprintf(buf, sizeof(buf), "%d%% (%s)", perc, state); + + return snprintf(b->curstr, LEN(b->curstr), b->fmt, buf); +} diff --git a/blocks/battery.h b/blocks/linux/battery.h diff --git a/blocks/linux/blight.c b/blocks/linux/blight.c @@ -0,0 +1,30 @@ +/* See LICENSE for license details. */ +#include <limits.h> +#include <stdio.h> +#include <string.h> + +#include "../../status.h" +#include "../../util.h" +#include "blight.h" + +size_t +blight(struct Block *b) +{ + char path[PATH_MAX]; + int perc; + unsigned long max, now; + + snprintf(path, sizeof(path), "/sys/class/backlight/%s/brightness", b->u.s); + if (pscanf(path, "%lu", &now) != 1) + now = 0; + + snprintf(path, sizeof(path), "/sys/class/backlight/%s/max_brightness", b->u.s); + if (pscanf(path, "%lu", &max) != 1) + /* avoid divison by 0 */ + max = 1; + + perc = 100 * now / max; + snprintf(buf, sizeof(buf), "%d%%", perc); + + return snprintf(b->curstr, LEN(b->curstr), b->fmt, buf); +} diff --git a/blocks/blight_linux.h b/blocks/linux/blight.h diff --git a/blocks/linux/volume.c b/blocks/linux/volume.c @@ -0,0 +1,56 @@ +/* See LICENSE for license details. */ +#include <alsa/asoundlib.h> +#include <alsa/mixer.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> + +#include "../../status.h" +#include "../../util.h" +#include "volume.h" + +size_t +getvol(struct Block *b) +{ + snd_mixer_t *handle; + snd_mixer_selem_id_t *sid; + snd_mixer_elem_t *elem; + const char *str = "muted"; + + int notmuted; + long vol = 0, min, max; + + snd_mixer_open(&handle, 0); + snd_mixer_attach(handle, alsacard); + snd_mixer_selem_register(handle, NULL, NULL); + snd_mixer_load(handle); + + snd_mixer_selem_id_malloc(&sid); + snd_mixer_selem_id_set_index(sid, 0); + snd_mixer_selem_id_set_name(sid, b->u.s); + elem = snd_mixer_find_selem(handle, sid); + + snd_mixer_selem_get_playback_switch(elem, 0, &notmuted); + + if (snd_mixer_selem_has_playback_volume(elem)) { + snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &vol); + snd_mixer_selem_get_playback_volume_range(elem, &min, &max); + + /* covert from raw value to percent */ + vol = (double)(vol - min) / (double)(max - min) * 100; + } + + /* don't change to snd_mixer_elem_free() it leaks memory */ + snd_mixer_close(handle); + snd_mixer_selem_id_free(sid); + + if (notmuted) { + if (vol) { + snprintf(buf, sizeof(buf), "%ld%%", vol); + str = buf; + } else + str = "on"; + } + + return snprintf(b->curstr, LEN(b->curstr), b->fmt, str); +} diff --git a/blocks/volume.h b/blocks/linux/volume.h diff --git a/blocks/openbsd/battery.c b/blocks/openbsd/battery.c @@ -0,0 +1,38 @@ +/* See LICENSE for license details. */ +#include <fcntl.h> +#include <machine/apmvar.h> +#include <stdio.h> +#include <sys/ioctl.h> +#include <unistd.h> + +#include "../../status.h" +#include "../../util.h" +#include "battery.h" + +size_t +batinfo(struct Block *b) +{ + struct apm_power_info pi; + int fd; + + if ((fd = open("/dev/apm", O_RDONLY)) < 0) + die("open\n"); + + if ((ioctl(fd, APM_IOC_GETPOWER, &pi)) < 0) { + close(fd); + die("ioctl\n"); + } + close(fd); + + 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); + 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); + } + return snprintf(b->curstr, LEN(b->curstr), b->fmt, buf); +} diff --git a/blocks/battery.h b/blocks/openbsd/battery.h diff --git a/blocks/volume.c b/blocks/volume.c @@ -1,59 +0,0 @@ -/* See LICENSE for license details. */ -#include <limits.h> -#include <stdio.h> -#include <stdlib.h> - -#include "../status.h" -#include "../util.h" -#include "volume.h" - -#if defined(__linux__) -#include <alsa/asoundlib.h> -#include <alsa/mixer.h> - -size_t -getvol(struct Block *b) -{ - snd_mixer_t *handle; - snd_mixer_selem_id_t *sid; - snd_mixer_elem_t *elem; - const char *str = "muted"; - - int notmuted; - long vol = 0, min, max; - - snd_mixer_open(&handle, 0); - snd_mixer_attach(handle, alsacard); - snd_mixer_selem_register(handle, NULL, NULL); - snd_mixer_load(handle); - - snd_mixer_selem_id_malloc(&sid); - snd_mixer_selem_id_set_index(sid, 0); - snd_mixer_selem_id_set_name(sid, b->u.s); - elem = snd_mixer_find_selem(handle, sid); - - snd_mixer_selem_get_playback_switch(elem, 0, &notmuted); - - if (snd_mixer_selem_has_playback_volume(elem)) { - snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &vol); - snd_mixer_selem_get_playback_volume_range(elem, &min, &max); - - /* covert from raw value to percent */ - vol = (double)(vol - min) / (double)(max - min) * 100; - } - - /* don't change to snd_mixer_elem_free() it leaks memory */ - snd_mixer_close(handle); - snd_mixer_selem_id_free(sid); - - if (notmuted) { - if (vol) { - snprintf(buf, sizeof(buf), "%ld%%", vol); - str = buf; - } else - str = "on"; - } - - return snprintf(b->curstr, LEN(b->curstr), b->fmt, str); -} -#endif diff --git a/config.def.h b/config.def.h @@ -1,8 +1,9 @@ -#include "blocks/battery.h" -#include "blocks/blight_linux.h" #include "blocks/gettime.h" +#include "blocks/linux/battery.h" +#include "blocks/linux/blight.h" +#include "blocks/linux/volume.h" #include "blocks/mpd.h" -#include "blocks/volume.h" +#include "blocks/script.h" /* update intervals: SEC+NANO gives sleep interval */ /* SEC must be >= 0 and 0 <= NANO <= 999999999 */ diff --git a/config.mk b/config.mk @@ -1,9 +1,9 @@ SRC =\ - blocks/battery.c\ - blocks/blight_linux.c\ blocks/gettime.c\ - blocks/mpd.c\ - blocks/volume.c + blocks/linux/battery.c\ + blocks/linux/blight.c\ + blocks/linux/volume.c\ + blocks/mpd.c PREFIX = /