slstatus

My fork of https://tools.suckless.org/slstatus/
git clone https://git.inz.fi/slstatus
Log | Files | Refs | README | LICENSE

commit 9383bca29156ecbdb32e16e9d33e77e82693a8eb
parent 15bce67ce796a326f6640053545d55411f9920f2
Author: Santtu Lakkala <inz@inz.fi>
Date:   Sat, 26 Feb 2022 11:42:54 +0200

Add fd watch

Diffstat:
Mslstatus.c | 22++++++++++++++++------
Mutil.h | 1+
2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/slstatus.c b/slstatus.c @@ -5,6 +5,10 @@ #include <stdlib.h> #include <string.h> #include <time.h> +#include <fcntl.h> + +#include <sys/poll.h> + #include <X11/Xlib.h> #include "arg.h" @@ -23,6 +27,9 @@ static Display *dpy; #include "config.h" +static struct pollfd *pfds = NULL; +static int npfds = 0; + static void terminate(const int signo) { @@ -30,6 +37,14 @@ terminate(const int signo) done = 1; } +void +watch(int fd) +{ + pfds = realloc(pfds, ++npfds * sizeof(*pfds)); + pfds[npfds - 1].fd = fd; + pfds[npfds - 1].events = POLLIN | POLLHUP; +} + static void difftimespec(struct timespec *res, struct timespec *a, struct timespec *b) { @@ -121,12 +136,7 @@ main(int argc, char *argv[]) intspec.tv_nsec = (interval % 1000) * 1E6; difftimespec(&wait, &intspec, &diff); - if (wait.tv_sec >= 0) { - if (nanosleep(&wait, NULL) < 0 && - errno != EINTR) { - die("nanosleep:"); - } - } + poll(pfds, npfds, wait.tv_sec * 1000 + wait.tv_nsec / 1000000); } } while (!done); diff --git a/util.h b/util.h @@ -19,3 +19,4 @@ const char *fmt_human(uintmax_t num, int base); int pscanf(const char *path, const char *fmt, ...); Display *getdisplay(void); void closedisplay(void); +void watch(int);