slstatus

status monitor
git clone git://git.suckless.org/slstatus
Log | Files | Refs | README | LICENSE

slstatus.c (2591B)


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