Improved sleep handling in statusloop

This commit is contained in:
Ashish Kumar Yadav 2020-12-04 12:19:12 +05:30
parent d9ccaac2d1
commit c8fc778841
2 changed files with 13 additions and 7 deletions

View file

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

View file

@ -5,6 +5,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
@ -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);
}
}