summaryrefslogtreecommitdiffstats
path: root/xssstate.c
blob: f6e63a81d904ce2869038b17c5a778f0be3533af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * See LICENSE file for copyright and license details.
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <libgen.h>
#include <X11/extensions/scrnsaver.h>

#include "arg.h"

char *argv0;

void
die(const char *errstr, ...) {
	va_list ap;

	va_start(ap, errstr);
	vfprintf(stderr, errstr, ap);
	va_end(ap);
	exit(EXIT_FAILURE);
}

void
usage(void)
{
	die("usage: %s [-istv]\n", basename(argv0));
}

int
main(int argc, char *argv[]) {
	XScreenSaverInfo *info;
	Display *dpy;
	int base, errbase;
	Bool showstate, showtill, showidle;

	showstate = false;
	showtill = false;
	showidle = false;

	ARGBEGIN {
	case 'i':
		showidle = true;
		break;
	case 's':
		showstate = true;
		break;
	case 't':
		showtill = true;
		break;
	case 'v':
		die("xssstate-"VERSION", © 2008-2016 xssstate engineers"
				", see LICENSE for details.\n");
	default:
		usage();
	} ARGEND;

	if (!showstate && !showtill && !showidle)
		usage();

	if (!(dpy = XOpenDisplay(0)))
		die("Cannot open display.\n");

	if (!XScreenSaverQueryExtension(dpy, &base, &errbase))
		die("Screensaver extension not activated.\n");

	info = XScreenSaverAllocInfo();
	XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);

	if (showstate) {
		switch(info->state) {
		case ScreenSaverOn:
			printf("on\n");
			break;
		case ScreenSaverOff:
			printf("off\n");
			break;
		case ScreenSaverDisabled:
			printf("disabled\n");
			break;
		}
	} else if (showtill) {
		switch(info->state) {
		case ScreenSaverOn:
			printf("0\n");
			break;
		case ScreenSaverOff:
			printf("%lu\n", info->til_or_since);
			break;
		case ScreenSaverDisabled:
			printf("-1\n");
			break;
		}
	} else if (showidle) {
		printf("%lu\n", info->idle);
	}


	XCloseDisplay(dpy);

	return 0;
}