slstatus

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

slstatus.c (2533B)


      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]", 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 's':
     61 			sflag = 1;
     62 			break;
     63 		default:
     64 			usage();
     65 	} ARGEND
     66 
     67 	if (argc) {
     68 		usage();
     69 	}
     70 
     71 	memset(&act, 0, sizeof(act));
     72 	act.sa_handler = terminate;
     73 	sigaction(SIGINT,  &act, NULL);
     74 	sigaction(SIGTERM, &act, NULL);
     75 
     76 	if (!sflag && !(dpy = XOpenDisplay(NULL))) {
     77 		die("XOpenDisplay: Failed to open display");
     78 	}
     79 
     80 	while (!done) {
     81 		if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) {
     82 			die("clock_gettime:");
     83 		}
     84 
     85 		status[0] = '\0';
     86 		for (i = len = 0; i < LEN(args); i++) {
     87 			if (!(res = args[i].func(args[i].args))) {
     88 				res = unknown_str;
     89 			}
     90 			if ((ret = esnprintf(status + len, sizeof(status) - len,
     91 			                    args[i].fmt, res)) < 0) {
     92 				break;
     93 			}
     94 			len += ret;
     95 		}
     96 
     97 		if (sflag) {
     98 			puts(status);
     99 			fflush(stdout);
    100 			if (ferror(stdout))
    101 				die("puts:");
    102 		} else {
    103 			if (XStoreName(dpy, DefaultRootWindow(dpy), status)
    104                             < 0) {
    105 				die("XStoreName: Allocation failed");
    106 			}
    107 			XFlush(dpy);
    108 		}
    109 
    110 		if (!done) {
    111 			if (clock_gettime(CLOCK_MONOTONIC, &current) < 0) {
    112 				die("clock_gettime:");
    113 			}
    114 			difftimespec(&diff, &current, &start);
    115 
    116 			intspec.tv_sec = interval / 1000;
    117 			intspec.tv_nsec = (interval % 1000) * 1E6;
    118 			difftimespec(&wait, &intspec, &diff);
    119 
    120 			if (wait.tv_sec >= 0) {
    121 				if (nanosleep(&wait, NULL) < 0 &&
    122 				    errno != EINTR) {
    123 					die("nanosleep:");
    124 				}
    125 			}
    126 		}
    127 	}
    128 
    129 	if (!sflag) {
    130 		XStoreName(dpy, DefaultRootWindow(dpy), NULL);
    131 		if (XCloseDisplay(dpy) < 0) {
    132 			die("XCloseDisplay: Failed to close display");
    133 		}
    134 	}
    135 
    136 	return 0;
    137 }