diff --git a/blocks.h b/blocks.h index 6a000b0..dc9ec26 100644 --- a/blocks.h +++ b/blocks.h @@ -1,6 +1,3 @@ -/* time interval in seconds to sleep before looking for updates in the main loop */ -#define SLEEPINTERVAL 1 - #define PATH(name) "/home/ashish/.local/projects/dwmblocks/blocks/"name /* If interval of a block is set to 0, the block will only be updated once at startup. @@ -13,6 +10,11 @@ /* pathu - path of the program whose output is to be used for status text * output of the program should have a null or newline character at the end * pathc - path of the program to be executed on clicks */ + +/* 1 interval = INTERVALs seconds, INTERVALn nanoseconds */ +#define INTERVALs 1 +#define INTERVALn 0 + static Block blocks[] = { /* pathu pathc interval signal */ { PATH("time.sh"), NULL, 30, 10}, diff --git a/dwmblocks.c b/dwmblocks.c index f4a6981..5b7fd20 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -192,6 +193,7 @@ void statusloop() { int i; + struct timespec t; /* first run */ sigprocmask(SIG_BLOCK, &blocksigmask, NULL); @@ -200,17 +202,19 @@ statusloop() getcmd(block, NILL); setroot(); sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL); - sleep(SLEEPINTERVAL); - i = SLEEPINTERVAL; /* main loop */ - for (;; i += SLEEPINTERVAL) { + for (i = 1;; t.tv_sec = INTERVALs, t.tv_nsec = INTERVALn, i += 1) { + while (nanosleep(&t, &t) == -1) + if (errno != EINTR) { + perror("statusloop - nanosleep"); + exit(1); + } sigprocmask(SIG_BLOCK, &blocksigmask, NULL); for (Block *block = blocks; block->pathu; block++) if (block->interval > 0 && i % block->interval == 0) getcmd(block, NILL); setroot(); sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL); - sleep(SLEEPINTERVAL); } }