Expunge revision histories in files.
[xtoys] / xwait.c
CommitLineData
90b2c5d4 1/* -*-c-*-
2 *
47747dbe 3 * $Id: xwait.c,v 1.10 2004/04/08 01:36:29 mdw Exp $
90b2c5d4 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
90b2c5d4 29/*----- Header files ------------------------------------------------------*/
30
7b50dab0 31#include <errno.h>
203d0643 32#include <signal.h>
90b2c5d4 33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
203d0643 37#include <sys/types.h>
38#include <sys/wait.h>
90b2c5d4 39#include <unistd.h>
40
41#include <X11/Xlib.h>
42#include <X11/Xutil.h>
43
c4efa11c 44#include <mLib/mdwopt.h>
45#include <mLib/quis.h>
25c93171 46#include <mLib/report.h>
47#include <mLib/sub.h>
c4efa11c 48
25c93171 49#include "xatom.h"
90b2c5d4 50#include "xwait.h"
51
25c93171 52/*----- Data structures ---------------------------------------------------*/
53
54typedef struct xwait_msg {
55 struct xwait_msg *next; /* Next message in the list */
56 Atom a; /* The message atom */
57} xwait_msg;
58
59typedef struct xwait_atom {
60 struct xwait_atom *next; /* Next atom in the list */
61 Atom a; /* The actual atom */
62 xwait_msg *m; /* List of interesting messages */
63} xwait_atom;
64
65/*----- Static variables --------------------------------------------------*/
66
67static xwait_atom *atoms = 0;
68static Display *dpy;
69
90b2c5d4 70/*----- Main code ---------------------------------------------------------*/
71
203d0643 72/* --- @sigchld@ --- */
73
74static void sigchld(int sig)
75{
7b50dab0 76 int e = errno;
203d0643 77 while (waitpid(-1, 0, WNOHANG) > 0)
78 ;
7b50dab0 79 errno = e;
203d0643 80}
81
25c93171 82/* --- @opendisplay@ --- */
83
84static void opendisplay(const char *d)
85{
86 if (dpy)
87 return;
88 if ((dpy = XOpenDisplay(d)) == 0)
89 die(EXIT_FAILURE, "couldn't open display");
90}
91
92/* --- @addatom@ --- */
93
94static xwait_atom *addatom(const char *a)
95{
96 xwait_atom *xa = CREATE(xwait_atom);
97 opendisplay(0);
98 xa->next = atoms;
99 xa->a = XInternAtom(dpy, a, False);
100 xa->m = 0;
101 atoms = xa;
102 return (xa);
103}
104
105/* --- @addmsg@ --- */
106
107static void addmsg(xwait_atom *xa, const char *a)
108{
109 xwait_msg *xm = CREATE(xwait_msg);
110 opendisplay(0);
111 xm->next = xa->m;
112 xm->a = XInternAtom(dpy, a, False);
113 xa->m = xm;
114}
115
116/* --- @tidy@ --- */
117
118static void tidy(int sig)
119{
120 int i;
121 int nsc = ScreenCount(dpy);
122 xwait_atom *xa;
123
124 for (i = 0; i < nsc; i++) {
125 for (xa = atoms; xa; xa = xa->next)
126 XDeleteProperty(dpy, RootWindow(dpy, i), xa->a);
127 }
128 XCloseDisplay(dpy);
129 exit(0);
130}
131
203d0643 132/* --- @main@ --- */
133
f3b35b6b 134static void version(FILE *fp)
135{
136 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
137}
138
139static void usage(FILE *fp)
140{
25c93171 141 fprintf(fp, "Usage: %s [-f] [-d DISPLAY] [ATOM:MSG,MSG]...\n", QUIS);
f3b35b6b 142}
143
90b2c5d4 144int main(int argc, char *argv[])
145{
25c93171 146 Atom a;
90b2c5d4 147 XEvent ev;
90b2c5d4 148 unsigned f = 0;
25c93171 149 xwait_atom *xa = 0;
150 xwait_msg *xm = 0;
90b2c5d4 151
6672918d 152#define f_force 1u
90b2c5d4 153
25c93171 154 /* --- Initialize mLib --- */
90b2c5d4 155
f3b35b6b 156 ego(argv[0]);
25c93171 157 sub_init();
158
159 /* --- Parse options --- */
f3b35b6b 160
90b2c5d4 161 for (;;) {
f3b35b6b 162 static struct option opt[] = {
163 { "help", 0, 0, 'h' },
164 { "usage", 0, 0, 'u' },
165 { "version", 0, 0, 'v' },
25c93171 166 { "display", OPTF_ARGREQ, 0, 'd' },
167 { "atom", OPTF_ARGREQ, 0, 'a' },
168 { "msg", OPTF_ARGREQ, 0, 'm' },
f3b35b6b 169 { "force", 0, 0, 'f' },
170 { 0, 0, 0, 0 }
171 };
172
25c93171 173 int i = mdwopt(argc, argv, "d:a:m:f", opt, 0, 0, 0);
90b2c5d4 174 if (i < 0)
175 break;
176 switch (i) {
f3b35b6b 177 case 'h':
178 version(stdout);
179 fputs("\n", stdout);
180 usage(stdout);
181 fputs(
182"\n"
183"Waits until signalled by `xtell' or `xshutdown'. Specifically, waits\n"
184"until a property with name ATOM is written to the root window with\n"
185"contents MSG.\n"
186"\n"
187"Options:\n"
188"\n"
189"-h, --help Display this help text\n"
190"-u, --usage Display a short usage summary\n"
191"-v, --version Display the program's version number\n"
192"\n"
193"-d, --display=DISPLAY Choose X display to connect to\n"
194"-f, --force Run even if this property is waited for by another\n"
195" process\n"
25c93171 196"-a, --atom=ATOM\t Choose property name to listen for [deprecated]\n"
197"-m, --msg=MSG Choose value of property to wait for [deprecated]\n",
f3b35b6b 198 stdout);
199 exit(0);
200 break;
201 case 'u':
202 usage(stdout);
203 exit(0);
204 break;
205 case 'v':
206 version(stdout);
207 exit(0);
208 break;
209
90b2c5d4 210 case 'd':
25c93171 211 opendisplay(optarg);
90b2c5d4 212 break;
213 case 'a':
25c93171 214 xa = addatom(optarg);
90b2c5d4 215 break;
216 case 'm':
25c93171 217 if (!xa)
218 die(EXIT_FAILURE, "no atom currently defined");
219 addmsg(xa, optarg);
90b2c5d4 220 break;
221 case 'f':
222 f |= f_force;
223 break;
224 default:
f3b35b6b 225 usage(stderr);
90b2c5d4 226 exit(EXIT_FAILURE);
227 break;
228 }
229 }
230
25c93171 231 /* --- Grind through remaining arguments in the new syntax --- */
90b2c5d4 232
25c93171 233 while (optind < argc) {
234 char *p = strtok(argv[optind++], ":");
235 if (!p)
236 continue;
237 xa = addatom(p);
238 while ((p = strtok(0, ",")) != 0)
239 addmsg(xa, p);
90b2c5d4 240 }
241
25c93171 242 /* --- If there's nothing set, put in some defaults --- */
90b2c5d4 243
25c93171 244 if (!atoms) {
245 xa = addatom(XWAIT_DIE);
246 addmsg(xa, XWAIT_DIE_MSG);
247 }
90b2c5d4 248
25c93171 249 /* --- Attach the atoms to the screens --- */
90b2c5d4 250
251 {
252 int i;
25c93171 253 Atom ready = XInternAtom(dpy, "XWAIT_READY", False);
90b2c5d4 254 int nsc = ScreenCount(dpy);
255
256 /* --- First pass: make sure there's not a process already here --- */
257
258 if ((f & f_force) == 0) {
259 for (i = 0; i < nsc; i++) {
25c93171 260 for (xa = atoms; xa; xa = xa->next) {
261 if (xatom_get(dpy, RootWindow(dpy, i), xa->a) != None)
262 die(EXIT_FAILURE, "already waiting for `%s'");
90b2c5d4 263 }
264 }
265 }
266
267 /* --- Second pass: set up listening to the property --- */
268
269 for (i = 0; i < nsc; i++) {
25c93171 270 for (xa = atoms; xa; xa = xa->next)
271 xatom_set(dpy, RootWindow(dpy, i), xa->a, ready);
272 XSelectInput(dpy, RootWindow(dpy, i), PropertyChangeMask);
90b2c5d4 273 }
274 }
275
203d0643 276 /* --- Set up a handler when children die --- *
277 *
278 * I don't fork any children? Why is this useful? Because I've been
279 * execed from a shell which started lots of background processes, and
280 * they'll zombie themselves otherwise.
281 */
282
283 {
284 struct sigaction sa;
285
286 sa.sa_handler = sigchld;
287 sigemptyset(&sa.sa_mask);
288 sigaddset(&sa.sa_mask, SIGCHLD);
7b50dab0 289 sa.sa_flags = SA_NOCLDSTOP;
290#ifdef SA_RESTART
291 sa.sa_flags |= SA_RESTART;
292#endif
203d0643 293 sigaction(SIGCHLD, &sa, 0);
25c93171 294 }
5a314f82 295
25c93171 296 /* --- Now reap any which have been waiting around so far --- */
297
298 {
299 sigset_t ss, oss;
5a314f82 300
301 sigemptyset(&ss);
302 sigaddset(&ss, SIGCHLD);
303 sigprocmask(SIG_BLOCK, &ss, &oss);
304 sigchld(SIGCHLD);
305 sigprocmask(SIG_SETMASK, &oss, 0);
203d0643 306 }
307
25c93171 308 signal(SIGINT, tidy);
309 signal(SIGTERM, tidy);
310
90b2c5d4 311 /* --- Now wait for an event --- */
312
313 for (;;) {
314 XNextEvent(dpy, &ev);
315 switch (ev.type) {
316 case PropertyNotify:
25c93171 317 for (xa = atoms; xa; xa = xa->next) {
318 if (ev.xproperty.atom != xa->a)
319 continue;
320 a = xatom_get(dpy, ev.xproperty.window, xa->a);
321 if (a == None)
322 continue;
323 if (xa->m == 0)
324 goto exit;
325 for (xm = xa->m; xm; xm = xm->next) {
326 if (a == xm->a)
90b2c5d4 327 goto exit;
90b2c5d4 328 }
329 }
25c93171 330 break;
90b2c5d4 331 }
332 }
333
25c93171 334 /* --- Finished: report the result if necessary --- */
90b2c5d4 335
336exit:
25c93171 337 if (atoms->next) {
338 fputs(XGetAtomName(dpy, xa->a), stdout);
339 if (!xa->m || xa->m->next)
340 printf(": %s\n", XGetAtomName(dpy, a));
341 else
342 fputc('\n', stdout);
343 } else if (!xa->m || xa->m->next)
344 printf("%s\n", XGetAtomName(dpy, a));
90b2c5d4 345
346 /* --- Go away --- */
347
25c93171 348 tidy(0);
90b2c5d4 349 return (0);
350}
351
352/*----- That's all, folks -------------------------------------------------*/