Debianization\!
[xtoys] / xwait.c
1 /* -*-c-*-
2 *
3 * $Id: xwait.c,v 1.9 2002/01/13 14:44:59 mdw Exp $
4 *
5 * Wait until prodded by another X client
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the Edgeware X tools collection.
13 *
14 * X tools is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * X tools is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with X tools; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: xwait.c,v $
32 * Revision 1.9 2002/01/13 14:44:59 mdw
33 * Make flags be unsigned.
34 *
35 * Revision 1.8 1999/08/20 07:29:55 mdw
36 * New command line syntax, and new atom protocol. Wait for multiple
37 * properties and listen for multiple (or all) values.
38 *
39 * Revision 1.7 1999/06/19 23:42:37 mdw
40 * Improve signal handling.
41 *
42 * Revision 1.6 1998/12/11 09:50:07 mdw
43 * Minor modifications to work with mLib and mgLib.
44 *
45 * Revision 1.5 1998/11/30 22:36:53 mdw
46 * Tidy up tabbing in help texts very slightly.
47 *
48 * Revision 1.4 1998/11/21 22:41:19 mdw
49 * Reap children which die before I get my signal handler installed.
50 *
51 * Revision 1.3 1998/11/21 22:30:27 mdw
52 * Support GNU-style long options throughout, and introduce proper help
53 * text to all programs. Update manual pages to match.
54 *
55 * Revision 1.2 1998/11/18 21:25:06 mdw
56 * Reap dead children as they arrive. The previous shell may have
57 * carelessly left them behind.
58 *
59 * Revision 1.1 1998/11/16 23:00:49 mdw
60 * Initial versions.
61 *
62 */
63
64 /*----- Header files ------------------------------------------------------*/
65
66 #include <errno.h>
67 #include <signal.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71
72 #include <sys/types.h>
73 #include <sys/wait.h>
74 #include <unistd.h>
75
76 #include <X11/Xlib.h>
77 #include <X11/Xutil.h>
78
79 #include <mLib/mdwopt.h>
80 #include <mLib/quis.h>
81 #include <mLib/report.h>
82 #include <mLib/sub.h>
83
84 #include "xatom.h"
85 #include "xwait.h"
86
87 /*----- Data structures ---------------------------------------------------*/
88
89 typedef struct xwait_msg {
90 struct xwait_msg *next; /* Next message in the list */
91 Atom a; /* The message atom */
92 } xwait_msg;
93
94 typedef struct xwait_atom {
95 struct xwait_atom *next; /* Next atom in the list */
96 Atom a; /* The actual atom */
97 xwait_msg *m; /* List of interesting messages */
98 } xwait_atom;
99
100 /*----- Static variables --------------------------------------------------*/
101
102 static xwait_atom *atoms = 0;
103 static Display *dpy;
104
105 /*----- Main code ---------------------------------------------------------*/
106
107 /* --- @sigchld@ --- */
108
109 static void sigchld(int sig)
110 {
111 int e = errno;
112 while (waitpid(-1, 0, WNOHANG) > 0)
113 ;
114 errno = e;
115 }
116
117 /* --- @opendisplay@ --- */
118
119 static void opendisplay(const char *d)
120 {
121 if (dpy)
122 return;
123 if ((dpy = XOpenDisplay(d)) == 0)
124 die(EXIT_FAILURE, "couldn't open display");
125 }
126
127 /* --- @addatom@ --- */
128
129 static xwait_atom *addatom(const char *a)
130 {
131 xwait_atom *xa = CREATE(xwait_atom);
132 opendisplay(0);
133 xa->next = atoms;
134 xa->a = XInternAtom(dpy, a, False);
135 xa->m = 0;
136 atoms = xa;
137 return (xa);
138 }
139
140 /* --- @addmsg@ --- */
141
142 static void addmsg(xwait_atom *xa, const char *a)
143 {
144 xwait_msg *xm = CREATE(xwait_msg);
145 opendisplay(0);
146 xm->next = xa->m;
147 xm->a = XInternAtom(dpy, a, False);
148 xa->m = xm;
149 }
150
151 /* --- @tidy@ --- */
152
153 static void tidy(int sig)
154 {
155 int i;
156 int nsc = ScreenCount(dpy);
157 xwait_atom *xa;
158
159 for (i = 0; i < nsc; i++) {
160 for (xa = atoms; xa; xa = xa->next)
161 XDeleteProperty(dpy, RootWindow(dpy, i), xa->a);
162 }
163 XCloseDisplay(dpy);
164 exit(0);
165 }
166
167 /* --- @main@ --- */
168
169 static void version(FILE *fp)
170 {
171 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
172 }
173
174 static void usage(FILE *fp)
175 {
176 fprintf(fp, "Usage: %s [-f] [-d DISPLAY] [ATOM:MSG,MSG]...\n", QUIS);
177 }
178
179 int main(int argc, char *argv[])
180 {
181 Atom a;
182 XEvent ev;
183 unsigned f = 0;
184 xwait_atom *xa = 0;
185 xwait_msg *xm = 0;
186
187 #define f_force 1u
188
189 /* --- Initialize mLib --- */
190
191 ego(argv[0]);
192 sub_init();
193
194 /* --- Parse options --- */
195
196 for (;;) {
197 static struct option opt[] = {
198 { "help", 0, 0, 'h' },
199 { "usage", 0, 0, 'u' },
200 { "version", 0, 0, 'v' },
201 { "display", OPTF_ARGREQ, 0, 'd' },
202 { "atom", OPTF_ARGREQ, 0, 'a' },
203 { "msg", OPTF_ARGREQ, 0, 'm' },
204 { "force", 0, 0, 'f' },
205 { 0, 0, 0, 0 }
206 };
207
208 int i = mdwopt(argc, argv, "d:a:m:f", opt, 0, 0, 0);
209 if (i < 0)
210 break;
211 switch (i) {
212 case 'h':
213 version(stdout);
214 fputs("\n", stdout);
215 usage(stdout);
216 fputs(
217 "\n"
218 "Waits until signalled by `xtell' or `xshutdown'. Specifically, waits\n"
219 "until a property with name ATOM is written to the root window with\n"
220 "contents MSG.\n"
221 "\n"
222 "Options:\n"
223 "\n"
224 "-h, --help Display this help text\n"
225 "-u, --usage Display a short usage summary\n"
226 "-v, --version Display the program's version number\n"
227 "\n"
228 "-d, --display=DISPLAY Choose X display to connect to\n"
229 "-f, --force Run even if this property is waited for by another\n"
230 " process\n"
231 "-a, --atom=ATOM\t Choose property name to listen for [deprecated]\n"
232 "-m, --msg=MSG Choose value of property to wait for [deprecated]\n",
233 stdout);
234 exit(0);
235 break;
236 case 'u':
237 usage(stdout);
238 exit(0);
239 break;
240 case 'v':
241 version(stdout);
242 exit(0);
243 break;
244
245 case 'd':
246 opendisplay(optarg);
247 break;
248 case 'a':
249 xa = addatom(optarg);
250 break;
251 case 'm':
252 if (!xa)
253 die(EXIT_FAILURE, "no atom currently defined");
254 addmsg(xa, optarg);
255 break;
256 case 'f':
257 f |= f_force;
258 break;
259 default:
260 usage(stderr);
261 exit(EXIT_FAILURE);
262 break;
263 }
264 }
265
266 /* --- Grind through remaining arguments in the new syntax --- */
267
268 while (optind < argc) {
269 char *p = strtok(argv[optind++], ":");
270 if (!p)
271 continue;
272 xa = addatom(p);
273 while ((p = strtok(0, ",")) != 0)
274 addmsg(xa, p);
275 }
276
277 /* --- If there's nothing set, put in some defaults --- */
278
279 if (!atoms) {
280 xa = addatom(XWAIT_DIE);
281 addmsg(xa, XWAIT_DIE_MSG);
282 }
283
284 /* --- Attach the atoms to the screens --- */
285
286 {
287 int i;
288 Atom ready = XInternAtom(dpy, "XWAIT_READY", False);
289 int nsc = ScreenCount(dpy);
290
291 /* --- First pass: make sure there's not a process already here --- */
292
293 if ((f & f_force) == 0) {
294 for (i = 0; i < nsc; i++) {
295 for (xa = atoms; xa; xa = xa->next) {
296 if (xatom_get(dpy, RootWindow(dpy, i), xa->a) != None)
297 die(EXIT_FAILURE, "already waiting for `%s'");
298 }
299 }
300 }
301
302 /* --- Second pass: set up listening to the property --- */
303
304 for (i = 0; i < nsc; i++) {
305 for (xa = atoms; xa; xa = xa->next)
306 xatom_set(dpy, RootWindow(dpy, i), xa->a, ready);
307 XSelectInput(dpy, RootWindow(dpy, i), PropertyChangeMask);
308 }
309 }
310
311 /* --- Set up a handler when children die --- *
312 *
313 * I don't fork any children? Why is this useful? Because I've been
314 * execed from a shell which started lots of background processes, and
315 * they'll zombie themselves otherwise.
316 */
317
318 {
319 struct sigaction sa;
320
321 sa.sa_handler = sigchld;
322 sigemptyset(&sa.sa_mask);
323 sigaddset(&sa.sa_mask, SIGCHLD);
324 sa.sa_flags = SA_NOCLDSTOP;
325 #ifdef SA_RESTART
326 sa.sa_flags |= SA_RESTART;
327 #endif
328 sigaction(SIGCHLD, &sa, 0);
329 }
330
331 /* --- Now reap any which have been waiting around so far --- */
332
333 {
334 sigset_t ss, oss;
335
336 sigemptyset(&ss);
337 sigaddset(&ss, SIGCHLD);
338 sigprocmask(SIG_BLOCK, &ss, &oss);
339 sigchld(SIGCHLD);
340 sigprocmask(SIG_SETMASK, &oss, 0);
341 }
342
343 signal(SIGINT, tidy);
344 signal(SIGTERM, tidy);
345
346 /* --- Now wait for an event --- */
347
348 for (;;) {
349 XNextEvent(dpy, &ev);
350 switch (ev.type) {
351 case PropertyNotify:
352 for (xa = atoms; xa; xa = xa->next) {
353 if (ev.xproperty.atom != xa->a)
354 continue;
355 a = xatom_get(dpy, ev.xproperty.window, xa->a);
356 if (a == None)
357 continue;
358 if (xa->m == 0)
359 goto exit;
360 for (xm = xa->m; xm; xm = xm->next) {
361 if (a == xm->a)
362 goto exit;
363 }
364 }
365 break;
366 }
367 }
368
369 /* --- Finished: report the result if necessary --- */
370
371 exit:
372 if (atoms->next) {
373 fputs(XGetAtomName(dpy, xa->a), stdout);
374 if (!xa->m || xa->m->next)
375 printf(": %s\n", XGetAtomName(dpy, a));
376 else
377 fputc('\n', stdout);
378 } else if (!xa->m || xa->m->next)
379 printf("%s\n", XGetAtomName(dpy, a));
380
381 /* --- Go away --- */
382
383 tidy(0);
384 return (0);
385 }
386
387 /*----- That's all, folks -------------------------------------------------*/