Improved out of range handling of interval specification

This commit is contained in:
Ashish Kumar Yadav 2021-04-04 19:37:22 +05:30
parent c265691e73
commit 839261d66b
2 changed files with 10 additions and 16 deletions

View file

@ -37,8 +37,9 @@ static const char delimiter[] = { ' ', ' ', ' ', DELIMITERENDCHAR };
one newline character at the end) one newline character at the end)
* pathc - path of the program used for handling clicks on the block */ * pathc - path of the program used for handling clicks on the block */
/* 1 interval = <first entry> seconds, <second entry> nanoseconds */ /* 1 interval = INTERVALs seconds, INTERVALn nanoseconds */
static const struct timespec interval = { 1, 0 }; #define INTERVALs 1
#define INTERVALn 0
static Block blocks[] = { static Block blocks[] = {
/* pathu pathc interval signal */ /* pathu pathc interval signal */

View file

@ -19,6 +19,9 @@
#include "config.h" #include "config.h"
_Static_assert(INTERVALs >= 0, "INTERVALs must be greater than or equal to 0");
_Static_assert(INTERVALn >= 0 && INTERVALn <= 999999999, "INTERVALn must be between 0 and 999999999");
static void buttonhandler(int sig, siginfo_t *info, void *ucontext); static void buttonhandler(int sig, siginfo_t *info, void *ucontext);
static void cleanup(); static void cleanup();
static void setupsignals(); static void setupsignals();
@ -132,29 +135,19 @@ statusloop()
int i; int i;
struct timespec t; struct timespec t;
/* first run */
sigprocmask(SIG_BLOCK, &blocksigmask, NULL); sigprocmask(SIG_BLOCK, &blocksigmask, NULL);
for (Block *block = blocks; block->pathu; block++) for (Block *block = blocks; block->pathu; block++)
if (block->interval >= 0) if (block->interval >= 0)
updateblock(block, NILL); updateblock(block, NILL);
updatestatus();
sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL);
t = interval;
while (nanosleep(&t, &t) == -1)
if (errno != EINTR) {
perror("statusloop - nanosleep");
exit(1);
}
/* main loop */
for (i = 1; ; i++) { for (i = 1; ; i++) {
updatestatus();
sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL);
t.tv_sec = INTERVALs, t.tv_nsec = INTERVALn;
while (nanosleep(&t, &t) == -1);
sigprocmask(SIG_BLOCK, &blocksigmask, NULL); sigprocmask(SIG_BLOCK, &blocksigmask, NULL);
for (Block *block = blocks; block->pathu; block++) for (Block *block = blocks; block->pathu; block++)
if (block->interval > 0 && i % block->interval == 0) if (block->interval > 0 && i % block->interval == 0)
updateblock(block, NILL); updateblock(block, NILL);
updatestatus();
sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL);
t = interval;
while (nanosleep(&t, &t) == -1);
} }
} }