slstatus

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

slstatus.c (2778B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <errno.h>
      3 #include <signal.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <string.h>
      7 #include <time.h>
      8 #include <fcntl.h>
      9 
     10 #include <sys/poll.h>
     11 
     12 #include <X11/Xlib.h>
     13 
     14 #include "arg.h"
     15 #include "slstatus.h"
     16 #include "util.h"
     17 
     18 struct arg {
     19 	const char *(*func)();
     20 	const char *fmt;
     21 	const char *args;
     22 };
     23 
     24 char buf[1024];
     25 static volatile sig_atomic_t done;
     26 static Display *dpy;
     27 
     28 #include "config.h"
     29 
     30 static struct pollfd *pfds = NULL;
     31 static int npfds = 0;
     32 
     33 static void
     34 terminate(const int signo)
     35 {
     36 	if (signo != SIGUSR1)
     37 		done = 1;
     38 }
     39 
     40 void
     41 watch(int fd)
     42 {
     43 	pfds = realloc(pfds, ++npfds * sizeof(*pfds));
     44 	pfds[npfds - 1].fd = fd;
     45 	pfds[npfds - 1].events = POLLIN | POLLHUP;
     46 }
     47 
     48 static void
     49 difftimespec(struct timespec *res, struct timespec *a, struct timespec *b)
     50 {
     51 	res->tv_sec = a->tv_sec - b->tv_sec - (a->tv_nsec < b->tv_nsec);
     52 	res->tv_nsec = a->tv_nsec - b->tv_nsec +
     53 	               (a->tv_nsec < b->tv_nsec) * 1E9;
     54 }
     55 
     56 static void
     57 usage(void)
     58 {
     59 	die("usage: %s [-s] [-1]", argv0);
     60 }
     61 
     62 int
     63 main(int argc, char *argv[])
     64 {
     65 	struct sigaction act;
     66 	struct timespec start, current, diff, intspec, wait;
     67 	size_t i, len;
     68 	int sflag, ret;
     69 	char status[MAXLEN];
     70 	const char *res;
     71 
     72 	sflag = 0;
     73 	ARGBEGIN {
     74 		case '1':
     75 			done = 1;
     76 			/* fallthrough */
     77 		case 's':
     78 			sflag = 1;
     79 			break;
     80 		default:
     81 			usage();
     82 	} ARGEND
     83 
     84 	if (argc) {
     85 		usage();
     86 	}
     87 
     88 	memset(&act, 0, sizeof(act));
     89 	act.sa_handler = terminate;
     90 	sigaction(SIGINT,  &act, NULL);
     91 	sigaction(SIGTERM, &act, NULL);
     92 	act.sa_flags |= SA_RESTART;
     93 	sigaction(SIGUSR1, &act, NULL);
     94 
     95 	if (!sflag && !(dpy = getdisplay())) {
     96 		die("XOpenDisplay: Failed to open display");
     97 	}
     98 
     99 
    100 	do {
    101 		if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) {
    102 			die("clock_gettime:");
    103 		}
    104 
    105 		status[0] = '\0';
    106 		for (i = len = 0; i < LEN(args); i++) {
    107 			if (!(res = args[i].func(args[i].args))) {
    108 				continue;
    109 			}
    110 			if ((ret = esnprintf(status + len, sizeof(status) - len,
    111 			                    args[i].fmt, res)) < 0) {
    112 				break;
    113 			}
    114 			len += ret;
    115 		}
    116 
    117 		if (sflag) {
    118 			puts(status);
    119 			fflush(stdout);
    120 			if (ferror(stdout))
    121 				die("puts:");
    122 		} else {
    123 			if (XStoreName(dpy, DefaultRootWindow(dpy), status)
    124                             < 0) {
    125 				die("XStoreName: Allocation failed");
    126 			}
    127 			XFlush(dpy);
    128 		}
    129 
    130 		if (!done) {
    131 			if (clock_gettime(CLOCK_MONOTONIC, &current) < 0) {
    132 				die("clock_gettime:");
    133 			}
    134 			difftimespec(&diff, &current, &start);
    135 
    136 			intspec.tv_sec = interval / 1000;
    137 			intspec.tv_nsec = (interval % 1000) * 1E6;
    138 			difftimespec(&wait, &intspec, &diff);
    139 
    140 			poll(pfds, npfds, wait.tv_sec * 1000 + wait.tv_nsec / 1000000);
    141 		}
    142 	} while (!done);
    143 
    144 	if (!sflag) {
    145 		XStoreName(dpy, DefaultRootWindow(dpy), NULL);
    146 	}
    147 	closedisplay();
    148 
    149 	return 0;
    150 }