xssstate

a simple utility to get the X screensaver state
git clone git://git.suckless.org/xssstate
Log | Files | Refs | README | LICENSE

xssstate.c (1786B)


      1 /*
      2  * See LICENSE file for copyright and license details.
      3  */
      4 
      5 #include <unistd.h>
      6 #include <stdlib.h>
      7 #include <stdio.h>
      8 #include <stdarg.h>
      9 #include <stdbool.h>
     10 #include <libgen.h>
     11 #include <X11/extensions/scrnsaver.h>
     12 
     13 #include "arg.h"
     14 
     15 char *argv0;
     16 
     17 void
     18 die(const char *errstr, ...) {
     19 	va_list ap;
     20 
     21 	va_start(ap, errstr);
     22 	vfprintf(stderr, errstr, ap);
     23 	va_end(ap);
     24 	exit(EXIT_FAILURE);
     25 }
     26 
     27 void
     28 usage(void)
     29 {
     30 	die("usage: %s [-istv]\n", basename(argv0));
     31 }
     32 
     33 int
     34 main(int argc, char *argv[]) {
     35 	XScreenSaverInfo *info;
     36 	Display *dpy;
     37 	int base, errbase;
     38 	Bool showstate, showtill, showidle;
     39 
     40 	showstate = false;
     41 	showtill = false;
     42 	showidle = false;
     43 
     44 	ARGBEGIN {
     45 	case 'i':
     46 		showidle = true;
     47 		break;
     48 	case 's':
     49 		showstate = true;
     50 		break;
     51 	case 't':
     52 		showtill = true;
     53 		break;
     54 	case 'v':
     55 		die("xssstate-"VERSION", © 2008-2016 xssstate engineers"
     56 				", see LICENSE for details.\n");
     57 	default:
     58 		usage();
     59 	} ARGEND;
     60 
     61 	if (!showstate && !showtill && !showidle)
     62 		usage();
     63 
     64 	if (!(dpy = XOpenDisplay(0)))
     65 		die("Cannot open display.\n");
     66 
     67 	if (!XScreenSaverQueryExtension(dpy, &base, &errbase))
     68 		die("Screensaver extension not activated.\n");
     69 
     70 	info = XScreenSaverAllocInfo();
     71 	XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);
     72 
     73 	if (showstate) {
     74 		switch(info->state) {
     75 		case ScreenSaverOn:
     76 			printf("on\n");
     77 			break;
     78 		case ScreenSaverOff:
     79 			printf("off\n");
     80 			break;
     81 		case ScreenSaverDisabled:
     82 			printf("disabled\n");
     83 			break;
     84 		}
     85 	} else if (showtill) {
     86 		switch(info->state) {
     87 		case ScreenSaverOn:
     88 			printf("0\n");
     89 			break;
     90 		case ScreenSaverOff:
     91 			printf("%lu\n", info->til_or_since);
     92 			break;
     93 		case ScreenSaverDisabled:
     94 			printf("-1\n");
     95 			break;
     96 		}
     97 	} else if (showidle) {
     98 		printf("%lu\n", info->idle);
     99 	}
    100 
    101 
    102 	XCloseDisplay(dpy);
    103 
    104 	return 0;
    105 }
    106