status

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

Commit: fb8fc3cc317385d4717823a856dccecc9fdaec18
Parent: 97a4ef4e19b351601dfbfcfe9548e1d80a126530
Author: Randy Palamar
Date:   Fri, 23 Apr 2021 00:02:18 -0600

actually implement interval updating

this is based on what dsblocks does

exiting was moved to terminate function so that we exit immediately upon
receiving a terminating signal instead of waiting until latest sleep
interval is finished

Diffstat:
Mconfig.def.h | 7+++++++
Mstatus.c | 63++++++++++++++++++++++++++++++++++++++-------------------------
2 files changed, 45 insertions(+), 25 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -3,6 +3,11 @@ #include "blocks/mpd.h" #include "blocks/volume.h" +/* update intervals: SEC+NANO gives sleep interval */ +/* SEC must be >= 0 and 0 <= NANO <= 999999999 */ +#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"; @@ -12,6 +17,8 @@ const char *mpdhost = "localhost"; const char *alsacard = "default"; /* status block definitions */ +/* interval * INTERVAL above gives actual update interval, 0 only updates + * at the start and when signaled, -1 only updates when signaled */ struct Block blks[] = { /* fn fmt interval signal arg */ { mpd_tag, "[ %s ", 0, 1, { .i = MPD_TAG_ARTIST } }, diff --git a/status.c b/status.c @@ -1,6 +1,7 @@ /* See LICENSE for license details. */ #include <signal.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> @@ -12,7 +13,6 @@ #define STATUSLEN ((LEN(blks) - 1) * BLOCKLEN + 1) -static int done = 0; static int dflag = 0; char buf[BLOCKLEN - BLOCKPAD]; @@ -23,7 +23,12 @@ static sigset_t blocksigmask; static void terminate(int signo) { - done = 1; + if (!dflag) { + XStoreName(dpy, DefaultRootWindow(dpy), NULL); + XCloseDisplay(dpy); + } + + exit(0); } static void @@ -85,9 +90,6 @@ setupsigs(void) /* add signals to blocksigmask */ sigemptyset(&blocksigmask); - sigaddset(&blocksigmask, SIGHUP); - sigaddset(&blocksigmask, SIGINT); - sigaddset(&blocksigmask, SIGTERM); for (b = blks; b->fn; b++) { if (b->signal <= 0) continue; @@ -120,11 +122,39 @@ setupsigs(void) sigaction(SIGRTMIN + b->signal, &sa, NULL); } -int -main(int argc, char *argv[]) +static void +statusloop(void) { + unsigned int i; struct Block *b; + struct timespec t; + + sigprocmask(SIG_BLOCK, &blocksigmask, NULL); + + /* initialize blocks before first print */ + for (b = blks; b->fn; b++) + if (b->interval != -1) + updateblock(b); + updatestatus(); + + for (i = 1; ; (++i == 0? i++ : i)) { + sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL); + t.tv_sec = INTERVAL_SEC; + t.tv_nsec = INTERVAL_NANO; + while (nanosleep(&t, &t) == -1); + sigprocmask(SIG_BLOCK, &blocksigmask, NULL); + + for (b = blks; b->fn; b++) + if (b->interval > 0 && i % b->interval == 0) + updateblock(b); + if (dirty) + updatestatus(); + } +} +int +main(int argc, char *argv[]) +{ if (argc > 2) die("usage: %s [-d]\n", argv[0]); @@ -140,24 +170,7 @@ main(int argc, char *argv[]) if (!dflag && !(dpy = XOpenDisplay(NULL))) die("XOpenDisplay: can't open display\n"); - /* initialize blocks before first print */ - for (b = blks; b->fn; b++) - if (b->interval != -1) - updateblock(b); - updatestatus(); - - for (; !done; sleep(15)) { - for (b = blks; b->fn; b++) - if (b->interval > 0) - updateblock(b); - if (dirty) - updatestatus(); - } - - if (!dflag) { - XStoreName(dpy, DefaultRootWindow(dpy), NULL); - XCloseDisplay(dpy); - } + statusloop(); return 0; }