slstatus

Custom version of the Suckless status monitor
git clone git://git.danielmoch.com/slstatus.git
Log | Files | Refs | README | LICENSE

slstatus.c (2588B)


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