From a52626ae332c1b60be64b036d460a713ec44000e Mon Sep 17 00:00:00 2001 From: Ashish Kumar Yadav Date: Wed, 25 Nov 2020 01:41:43 +0530 Subject: [PATCH] Term handling improvements and refactoring --- dwmblocks.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/dwmblocks.c b/dwmblocks.c index 6224fbd..f4a6981 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -25,6 +25,7 @@ typedef struct { #include "blocks.h" static void buttonhandler(int signal, siginfo_t *si, void *ucontext); +static void cleanup(); static void getcmd(Block *block, int sigval); static void setroot(); static void setupsignals(); @@ -34,7 +35,6 @@ static void termhandler(int signum); static int updatestatus(); static void writepid(); -static int statuscontinue = 1; static char statustext[STTLENGTH]; static size_t delimlength; static Display *dpy; @@ -64,6 +64,14 @@ buttonhandler(int signal, siginfo_t *si, void *ucontext) } } +void +cleanup() +{ + unlink(LOCKFILE); + XStoreName(dpy, DefaultRootWindow(dpy), ""); + XCloseDisplay(dpy); +} + void getcmd(Block *block, int sigval) { @@ -124,6 +132,16 @@ setupsignals() { struct sigaction sa; + /* populate blocksigmask */ + sigemptyset(&blocksigmask); + sigaddset(&blocksigmask, SIGHUP); + sigaddset(&blocksigmask, SIGINT); + sigaddset(&blocksigmask, SIGTERM); + for (Block *block = blocks; block->pathu; block++) + if (block->signal > 0) + sigaddset(&blocksigmask, SIGRTMIN + block->signal); + + /* setup signal handlers */ /* to handle HUP, INT and TERM */ sa.sa_flags = SA_RESTART; sigemptyset(&sa.sa_mask); @@ -185,7 +203,7 @@ statusloop() sleep(SLEEPINTERVAL); i = SLEEPINTERVAL; /* main loop */ - while (statuscontinue) { + for (;; i += SLEEPINTERVAL) { sigprocmask(SIG_BLOCK, &blocksigmask, NULL); for (Block *block = blocks; block->pathu; block++) if (block->interval > 0 && i % block->interval == 0) @@ -193,14 +211,14 @@ statusloop() setroot(); sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL); sleep(SLEEPINTERVAL); - i += SLEEPINTERVAL; } } void termhandler(int signum) { - statuscontinue = 0; + cleanup(); + exit(0); } /* returns whether block outputs have changed and updates statustext if they have */ @@ -338,17 +356,8 @@ main(int argc, char *argv[]) fputs("Error: could not open display.\n", stderr); return 1; } - sigemptyset(&blocksigmask); - sigaddset(&blocksigmask, SIGHUP); - sigaddset(&blocksigmask, SIGINT); - sigaddset(&blocksigmask, SIGTERM); - for (Block *block = blocks; block->pathu; block++) - if (block->signal > 0) - sigaddset(&blocksigmask, SIGRTMIN + block->signal); setupsignals(); statusloop(); - unlink(LOCKFILE); - XStoreName(dpy, DefaultRootWindow(dpy), ""); - XCloseDisplay(dpy); + cleanup(); return 0; }