slstatus

Unnamed repository; edit this file 'description' to name the repository.
git clone https://git.beauhilton.com/slstatus.git
Log | Files | Refs | README | LICENSE

slstatus.c (2677B)


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