Term handling improvements and refactoring

This commit is contained in:
Ashish Kumar Yadav 2020-11-25 01:41:43 +05:30
parent 01f1481f54
commit a52626ae33

View file

@ -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;
}