summaryrefslogtreecommitdiff
path: root/app/xidle/xidle.c
blob: 3c70692579e2672655c13db727fffdb53db8208e (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*	$OpenBSD: xidle.c,v 1.1 2006/11/26 10:57:44 matthieu Exp $	*/
/*
 * Copyright (c) 2005 Federico G. Schwindt
 * Copyright (c) 2005 Claudio Castiglia
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <X11/Xlib.h>
#include <X11/Xresource.h>
#include <X11/extensions/scrnsaver.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <err.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifndef CLASS_NAME
#define CLASS_NAME	"XIdle"
#endif

#ifndef PATH_PROG
#define PATH_PROG	"/usr/X11R6/bin/xlock"
#endif


enum {
	north = 0x01,
	south = 0x02,
	east  = 0x04,
	west  = 0x08
};

struct xinfo {
	Display		*dpy;
	Window		 win;
	int		 coord_x;
	int		 coord_y;

	int		 saver_event;	/* Only if Xss ext is available */

	int		 saved_timeout;
	int		 saved_interval;
	int		 saved_pref_blank;
	int		 saved_allow_exp;
};

struct	xinfo x;

static XrmOptionDescRec fopts[] = {
	{ "-display",	".display",	XrmoptionSepArg,	(caddr_t)NULL },
};

static XrmOptionDescRec opts[] = {
	{ "-area",	".area",	XrmoptionSepArg,	(caddr_t)NULL },
	{ "-delay",	".delay",	XrmoptionSepArg,	(caddr_t)NULL },
	{ "-program",	".program",	XrmoptionSepArg,	(caddr_t)NULL },
	{ "-timeout",	".timeout",	XrmoptionSepArg,	(caddr_t)NULL },

	{ "-ne",	".position",	XrmoptionNoArg,		(caddr_t)"ne" },
	{ "-nw",	".position", 	XrmoptionNoArg,		(caddr_t)"nw" },
	{ "-se",	".position",	XrmoptionNoArg,		(caddr_t)"se" },
	{ "-sw",	".position",	XrmoptionNoArg,		(caddr_t)"sw" }
};

extern char *__progname;

void	action(struct xinfo *, char **);
void	close_x(struct xinfo *);
Bool	getres(XrmValue *, const XrmDatabase, const char *, const char *);
void    init_x(struct xinfo *, int, int, int);
void	handler(int);
void	parse_opts(int, char **, Display **, int *, int *, int *, int *,
	    char **);
int	str2pos(const char *);
__dead void	usage(void);


__dead void
usage()
{
	fprintf(stderr, "Usage:\n%s %s\n", __progname,
	    "[-area pixels] [-delay secs] [-display host:dpy] "
	    "[-ne | -nw | -se | -sw]\n      [-program path] [-timeout secs]");
	exit(1);
}


void
init_x(struct xinfo *xi, int position, int area, int timeout)
{
	XSetWindowAttributes attr;
	Display *dpy = xi->dpy;
	int error, event;
	int screen;

	screen = DefaultScreen(dpy);

	if (position & south)
		xi->coord_y = DisplayHeight(dpy, screen) - area;
	if (position & east)
		xi->coord_x = DisplayWidth(dpy, screen) - area;

	attr.override_redirect = True;
	xi->win = XCreateWindow(dpy, DefaultRootWindow(dpy),
	    xi->coord_x, xi->coord_y, area, area, 0, 0, InputOnly,
	    CopyFromParent, CWOverrideRedirect, &attr);

	XSelectInput(dpy, xi->win, EnterWindowMask|StructureNotifyMask
#if 0
			       |VisibilityChangeMask
#endif
	);
	XMapWindow(dpy, xi->win);

	/*
	 * AFAICT, we need the event number for ScreenSaverNotify
	 * _always_ since it's the only way to distinguish whether
	 * we've been obscured by an external locking program or
	 * by another window and react according.
	 */
	if (XScreenSaverQueryExtension(dpy, &event, &error) == True) {
		xi->saver_event = event;

		XScreenSaverSelectInput(dpy, DefaultRootWindow(dpy),
		    ScreenSaverNotifyMask);
	} else
		warnx("XScreenSaver extension not available.%s",
		    timeout > 0 ? " Timeout disabled." : "");

	if (timeout > 0 && xi->saver_event) {
		XGetScreenSaver(dpy, &xi->saved_timeout, &xi->saved_interval,
		    &xi->saved_pref_blank, &xi->saved_allow_exp);

		XSetScreenSaver(dpy, timeout, 0, DontPreferBlanking,
		    DontAllowExposures);
	}
}


void
close_x(struct xinfo *xi)
{
	XSetScreenSaver(xi->dpy, xi->saved_timeout, xi->saved_interval,
	    xi->saved_pref_blank, xi->saved_allow_exp);
	XDestroyWindow(xi->dpy, xi->win);
	XCloseDisplay(xi->dpy);
}


void
action(struct xinfo *xi, char **args)
{
	int dumb;

	switch (fork()) {
	case -1:
		err(1, "fork");
		/* NOTREACHED */

	case 0:
		execv(*args, args);
		exit(1);
		/* NOTREACHED */

	default:
		wait(&dumb);
		XSync(xi->dpy, True);
		break;
	}
}


void
handler(int sig)
{
	XClientMessageEvent ev;

	ev.type = ClientMessage;
	ev.display = x.dpy;
	ev.window = x.win;
	ev.message_type = 0xdead;
	ev.format = 8;
	XSendEvent(x.dpy, x.win, False, 0L, (XEvent *)&ev);
	XFlush(x.dpy);
}


int
str2pos(const char *src)
{
	static struct {
		char	*str;
		int	 pos;
	} s2p[] = {
		{ "ne", north|east },
		{ "nw", north|west },
		{ "se", south|east },
		{ "sw", south|west },
		{ NULL, 0	   }
	}, *s;

	for (s = s2p; s->str != NULL; s++)
		if (!strcmp(src, s->str))
			break;
	return (s->pos);
}


Bool
getres(XrmValue *value, const XrmDatabase rdb, const char *rname,
    const char *cname)
{
	char fullres[PATH_MAX], fullclass[PATH_MAX], *type;

	snprintf(fullres, sizeof(fullres), "%s.%s", __progname, rname);
	snprintf(fullclass, sizeof(fullclass), "%s.%s", CLASS_NAME, cname);
	return (XrmGetResource(rdb, fullres, fullclass, &type, value));
}


void
parse_opts(int argc, char **argv, Display **dpy, int *area, int *delay,
    int *timeout, int *position, char **args)
{
	char **ap, *program = PATH_PROG;
	char *display, *p;
	XrmDatabase tdb, rdb = NULL;
	XrmValue value;

	XrmInitialize();

	/* Get display to open. */
	XrmParseCommand(&rdb, fopts, sizeof(fopts) / sizeof(fopts[0]),
	    __progname, &argc, argv);

	display = (getres(&value, rdb, "display", "Display") == True) ?
	    (char *)value.addr : NULL;

	*dpy = XOpenDisplay(display);
	if (!*dpy) {
		errx(1, "Unable to open display %s", XDisplayName(display));
		/* NOTREACHED */
	}

	/* Get server resources database. */
	p = XResourceManagerString(*dpy);
	if (!p) {
		/* Get screen resources database. */
		p = XScreenResourceString(ScreenOfDisplay(*dpy,
		    DefaultScreen(*dpy)));
	}

	if (p) {
		tdb = XrmGetStringDatabase(p);
		XrmMergeDatabases(tdb, &rdb);
	}

	/* Get remaining command line values. */
	XrmParseCommand(&rdb, opts, sizeof(opts) / sizeof(opts[0]),
	    __progname, &argc, argv);
	if (argc > 1) {
		usage();
		/* NOTREACHED */
	}
	if (getres(&value, rdb, "area", "Area")) {
		*area = strtol((char *)value.addr, &p, 10);
		if (*p || *area < 1) {
fail:			errx(1, "illegal value -- %s", (char *)value.addr);
			/* NOTREACHED */
		}
	}
	if (getres(&value, rdb, "delay", "Delay")) {
		*delay = strtol((char *)value.addr, &p, 10);
		if (*p || *delay < 0)
			goto fail;
	}
	if (getres(&value, rdb, "position", "Position")) {
		*position = str2pos((char *)value.addr);
		if (!*position)
			goto fail;
	}
	if (getres(&value, rdb, "timeout", "Timeout")) {
		*timeout = strtol((char *)value.addr, &p, 10);
		if (*p || *timeout < 0)
			goto fail;
	}
	if (getres(&value, rdb, "program", "Program")) {
		/* Should be the last :) */
		program = (char *)value.addr;
	}

	for (ap = args; ap < &args[9] &&
	    (*ap = strsep(&program, " ")) != NULL;) {
		if (**ap != '\0')
			ap++;
	}
	*ap = NULL;
}


int
main(int argc, char **argv)
{
	char *args[10];
	int area = 2, delay = 2, timeout = 0;
	int position = north|west;
	u_long last_serial = 0;

	bzero(&x, sizeof(struct xinfo));

	parse_opts(argc, argv, &x.dpy, &area, &delay, &timeout,
	    &position, args);

#ifdef DEBUG
	printf("Area: %d\nDelay: %d\nPosition: %d\nTimeout: %d\n"
	    "Program: \"%s\"\n",
	    area, delay, position, timeout, args[0]);
#endif

	init_x(&x, position, area, timeout);

	signal(SIGINT, handler);
	signal(SIGTERM, handler);

	for (;;) {
		XEvent ev;
		u_long mask;

		XNextEvent(x.dpy, &ev);

#ifdef DEBUG
		printf("got event %d\n", ev.type);
#endif

		switch (ev.type) {
		case VisibilityNotify:
			/*
			 * If we got here and the current serial matches
			 * the one from saver_event, we're being obscured
			 * by a locking program. Disable further
			 * screen saver events and raises.
			 */
			if (ev.xvisibility.serial == last_serial)
				mask = 0;
			else
				mask = ScreenSaverNotifyMask;

			XScreenSaverSelectInput(x.dpy,
			    DefaultRootWindow(x.dpy), mask);

			if (mask)
				XMapRaised(x.dpy, x.win);

			XSync(x.dpy, True);
			break;

		case MapNotify:
			XMapRaised(x.dpy, x.win);
			break;

		case ClientMessage:
			if (ev.xclient.message_type != 0xdead)
				break;
			close_x(&x);
			exit(0);
			/* NOTREACHED */

		case EnterNotify:
			sleep(delay);

			XQueryPointer(x.dpy, x.win, &ev.xcrossing.root,
			    &ev.xcrossing.window,
			    &ev.xcrossing.x_root, &ev.xcrossing.y_root,
			    &ev.xcrossing.x, &ev.xcrossing.y,
			    &ev.xcrossing.state);

			/* Check it was for real. */
			if (ev.xcrossing.x_root < x.coord_x ||
			    ev.xcrossing.y_root < x.coord_y ||
			    ev.xcrossing.x_root > x.coord_x + area ||
			    ev.xcrossing.y_root > x.coord_y + area)
				break;
			/* FALLTHROUGH */

		default:
			if (ev.type != EnterNotify &&
			    ev.type != x.saver_event)
				break;

			if (ev.type == x.saver_event) {
				XScreenSaverNotifyEvent *se =
				    (XScreenSaverNotifyEvent *)&ev;

				/* Take note of the serial for this event. */
				last_serial = se->serial;

				/*
				 * Was for real or due to terminal
				 * switching or a locking program?
				 */
				if (se->forced != False)
					break;
			}
			action(&x, args);
			break;
		}
	}

	/* NOTREACHED */
}