status

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

Commit: ddd430a2a4882d29bc97c767adbf286e341fa2f0
Parent: 7bd55aad1bed56487ed5780617a9adfaa7762c1a
Author: Randy Palamar
Date:   Fri, 17 Feb 2023 18:01:43 -0700

change block arg union member to void *

this allows more robust arguments as demonstrated by new vol_arg and
mpd_arg structs

Diffstat:
MLICENSE | 2+-
Mblocks/date.c | 2+-
Mblocks/linux/battery.c | 10+++++-----
Mblocks/linux/volume.c | 7++++---
Mblocks/linux/volume.h | 8++++++--
Mblocks/mpd.c | 25+++++++++++++++++--------
Mblocks/mpd.h | 7++++++-
Mblocks/script.c | 2+-
Mconfig.def.h | 30+++++++++++++++---------------
Mstatus.h | 5+++--
10 files changed, 59 insertions(+), 39 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -1,6 +1,6 @@ ISC License (ISC) -© 2019-2022 Randy Palamar <palamar@ualberta.ca> +© 2019-2023 Randy Palamar <palamar@ualberta.ca> © 2021 Ashish Kumar Yadav Permission to use, copy, modify, and distribute this software for any diff --git a/blocks/date.c b/blocks/date.c @@ -10,7 +10,7 @@ size_t date(struct Block *b) { time_t t = time(NULL); - strftime(buf, sizeof(buf), b->u.s, localtime(&t)); + strftime(buf, sizeof(buf), b->arg, localtime(&t)); return snprintf(b->curstr, LEN(b->curstr), b->fmt, buf); } diff --git a/blocks/linux/battery.c b/blocks/linux/battery.c @@ -10,27 +10,27 @@ size_t batinfo(struct Block *b) { - char path[PATH_MAX], state[12]; + char path[PATH_MAX], state[12], *bat = (char *)b->arg; 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); + snprintf(path, sizeof(path), "/sys/class/power_supply/%s/capacity", bat); if (pscanf(path, "%d", &perc) != 1) perc = 0; - snprintf(path, sizeof(path), "/sys/class/power_supply/%s/status", b->u.s); + snprintf(path, sizeof(path), "/sys/class/power_supply/%s/status", 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", b->u.s); + "/sys/class/power_supply/%s/power_now", bat); if (pscanf(path, "%lu", &power_now) != 1) power_now = 1; snprintf(path, sizeof(path), - "/sys/class/power_supply/%s/energy_now", b->u.s); + "/sys/class/power_supply/%s/energy_now", bat); if (pscanf(path, "%lu", &energy_now) != 1) energy_now = 0; diff --git a/blocks/linux/volume.c b/blocks/linux/volume.c @@ -10,24 +10,25 @@ #include "volume.h" size_t -getvol(struct Block *b) +volume(struct Block *b) { snd_mixer_t *handle; snd_mixer_selem_id_t *sid; snd_mixer_elem_t *elem; + const struct vol_arg *va = b->arg; const char *str = "muted"; int notmuted; long vol = 0, min, max; snd_mixer_open(&handle, 0); - snd_mixer_attach(handle, alsacard); + snd_mixer_attach(handle, va->card); 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); + snd_mixer_selem_id_set_name(sid, va->sink); elem = snd_mixer_find_selem(handle, sid); snd_mixer_selem_get_playback_switch(elem, 0, &notmuted); diff --git a/blocks/linux/volume.h b/blocks/linux/volume.h @@ -1,2 +1,6 @@ -extern const char *alsacard; -size_t getvol(struct Block *b); +struct vol_arg { + const char *card; + const char *sink; +}; + +size_t volume(struct Block *b); diff --git a/blocks/mpd.c b/blocks/mpd.c @@ -9,11 +9,11 @@ static struct mpd_connection *conn; static int -open_conn(void) +open_conn(const char *host) { - conn = mpd_connection_new(mpdhost, 0, 0); + conn = mpd_connection_new(host, 0, 0); if (!mpd_connection_get_error(conn) - && mpd_connection_set_keepalive(conn, true)) { + && mpd_connection_set_keepalive(conn, true)) { mpd_send_idle(conn); return 0; } @@ -23,13 +23,13 @@ open_conn(void) } static int -check_conn(void) +check_conn(const char *host) { if (!mpd_connection_get_error(conn)) return 0; mpd_connection_free(conn); - return open_conn(); + return open_conn(host); } size_t @@ -37,9 +37,11 @@ mpd_tag(struct Block *b) { struct mpd_song *song = NULL; struct mpd_status *status = NULL; + const struct mpd_arg *ma = b->arg; + size_t i, len = 0; const char *str = ""; - if ((!conn && open_conn() != 0) || check_conn() != 0) + if ((!conn && open_conn(ma->host) != 0) || check_conn(ma->host) != 0) return snprintf(b->curstr, LEN(b->curstr), b->fmt, str); mpd_run_noidle(conn); @@ -49,8 +51,15 @@ mpd_tag(struct Block *b) case MPD_STATE_PAUSE: case MPD_STATE_PLAY: song = mpd_run_current_song(conn); - snprintf(buf, sizeof(buf), "%s", - mpd_song_get_tag(song, b->u.i, 0)); + for (i = 0; i < ma->ntags; i++) { + if (len != 0) + len += snprintf(buf + len, + sizeof(buf) - len, "%s", + ma->sep ? ma->sep : ""); + len += snprintf( + buf + len, sizeof(buf) - len, "%s", + mpd_song_get_tag(song, ma->tags[i], 0)); + } str = buf; mpd_song_free(song); case MPD_STATE_STOP: diff --git a/blocks/mpd.h b/blocks/mpd.h @@ -1,6 +1,11 @@ /* to have access to tag types */ #include <mpd/tag.h> -extern const char *mpdhost; +struct mpd_arg { + const char *host; + const char *sep; + const enum mpd_tag_type *tags; + size_t ntags; +}; size_t mpd_tag(struct Block *b); diff --git a/blocks/script.c b/blocks/script.c @@ -11,7 +11,7 @@ script(struct Block *b) { FILE *fp; - if ((fp = popen(b->u.s, "r")) == NULL) + if ((fp = popen(b->arg, "r")) == NULL) die("popen()\n"); if (fgets(buf, sizeof(buf), fp) != NULL) diff --git a/config.def.h b/config.def.h @@ -1,4 +1,4 @@ -#include "blocks/gettime.h" +#include "blocks/date.h" #include "blocks/linux/battery.h" #include "blocks/linux/blight.h" #include "blocks/linux/volume.h" @@ -10,25 +10,25 @@ #define INTERVAL_SEC 1 #define INTERVAL_NANO 0 -/* host for connecting to MPD, set to NULL for the MPD_HOST env variable */ -const char *mpdhost = "localhost"; +/* mpd_arg.host can be NULL to use the MPD_HOST env variable */ +const enum mpd_tag_type tags[] = { MPD_TAG_TITLE, MPD_TAG_ARTIST }; +const struct mpd_arg ma = { "localhost", "|", tags, 2 }; -/* alsa card and output */ -/* card is found with 'aplay -L', default is probably correct - * output is specified as an arg */ -const char *alsacard = "default"; +/* alsa card and sink */ +/* card is found with 'aplay -L', default is probably correct */ +const struct vol_arg va = { "default", "Speaker" }; /* status block definitions * * function description arg (ex) * - * batinfo battery percentage and status (.s) battery name (BAT0) + * batinfo battery percentage and status (char *) battery name (BAT0) * 0 on OpenBSD - * blight backlight percentage (.s) backlight name (intel_backlight) - * date date and time (.s) time fmt string (%R) - * getvol ALSA volume percentage (.s) sink name (Speaker) - * mpd_tag reads tag from current song (.i) enum mpd_tag_type (MPD_TAG_TITLE) - * script run specified script (.s) full script (echo foo | bar) + * blight backlight percentage (char *) backlight name (intel_backlight) + * date date and time (char *) time fmt string (%R) + * volume ALSA volume percentage (struct vol_arg *) + * mpd_tag reads tag from current song (struct mpd_arg *) + * script run specified script (char *) full script (echo foo | bar) * * * interval * INTERVAL above gives actual update interval, 0 only updates @@ -36,7 +36,7 @@ const char *alsacard = "default"; */ struct Block blks[] = { /* fn fmt interval signal arg */ - { batinfo, "[ %s ]", 30, 0, { .s = "BAT0" } }, - { date, "[ %s ]", 20, 0, { .s = "%R" } }, + { batinfo, "[ %s ]", 30, 0, "BAT0" }, + { date, "[ %s ]", 20, 0, "%R" }, { NULL }, }; diff --git a/status.h b/status.h @@ -1,11 +1,12 @@ -#define BLOCKLEN 32 +#define BLOCKLEN 128 #define BLOCKPAD 4 + struct Block { size_t (*const fn)(struct Block *b); const char *fmt; const int interval; const int signal; - union { const char *s; const int i; } u; + const void *arg; char curstr[BLOCKLEN]; char prevstr[BLOCKLEN]; size_t len;