3 * $Id: xwait.c,v 1.8 1999/08/20 07:29:55 mdw Exp $
5 * Wait until prodded by another X client
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Edgeware X tools collection.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.8 1999/08/20 07:29:55 mdw
33 * New command line syntax, and new atom protocol. Wait for multiple
34 * properties and listen for multiple (or all) values.
36 * Revision 1.7 1999/06/19 23:42:37 mdw
37 * Improve signal handling.
39 * Revision 1.6 1998/12/11 09:50:07 mdw
40 * Minor modifications to work with mLib and mgLib.
42 * Revision 1.5 1998/11/30 22:36:53 mdw
43 * Tidy up tabbing in help texts very slightly.
45 * Revision 1.4 1998/11/21 22:41:19 mdw
46 * Reap children which die before I get my signal handler installed.
48 * Revision 1.3 1998/11/21 22:30:27 mdw
49 * Support GNU-style long options throughout, and introduce proper help
50 * text to all programs. Update manual pages to match.
52 * Revision 1.2 1998/11/18 21:25:06 mdw
53 * Reap dead children as they arrive. The previous shell may have
54 * carelessly left them behind.
56 * Revision 1.1 1998/11/16 23:00:49 mdw
61 /*----- Header files ------------------------------------------------------*/
69 #include <sys/types.h>
74 #include <X11/Xutil.h>
76 #include <mLib/mdwopt.h>
77 #include <mLib/quis.h>
78 #include <mLib/report.h>
84 /*----- Data structures ---------------------------------------------------*/
86 typedef struct xwait_msg {
87 struct xwait_msg *next; /* Next message in the list */
88 Atom a; /* The message atom */
91 typedef struct xwait_atom {
92 struct xwait_atom *next; /* Next atom in the list */
93 Atom a; /* The actual atom */
94 xwait_msg *m; /* List of interesting messages */
97 /*----- Static variables --------------------------------------------------*/
99 static xwait_atom *atoms = 0;
102 /*----- Main code ---------------------------------------------------------*/
104 /* --- @sigchld@ --- */
106 static void sigchld(int sig)
109 while (waitpid(-1, 0, WNOHANG) > 0)
114 /* --- @opendisplay@ --- */
116 static void opendisplay(const char *d)
120 if ((dpy = XOpenDisplay(d)) == 0)
121 die(EXIT_FAILURE, "couldn't open display");
124 /* --- @addatom@ --- */
126 static xwait_atom *addatom(const char *a)
128 xwait_atom *xa = CREATE(xwait_atom);
131 xa->a = XInternAtom(dpy, a, False);
137 /* --- @addmsg@ --- */
139 static void addmsg(xwait_atom *xa, const char *a)
141 xwait_msg *xm = CREATE(xwait_msg);
144 xm->a = XInternAtom(dpy, a, False);
150 static void tidy(int sig)
153 int nsc = ScreenCount(dpy);
156 for (i = 0; i < nsc; i++) {
157 for (xa = atoms; xa; xa = xa->next)
158 XDeleteProperty(dpy, RootWindow(dpy, i), xa->a);
166 static void version(FILE *fp)
168 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
171 static void usage(FILE *fp)
173 fprintf(fp, "Usage: %s [-f] [-d DISPLAY] [ATOM:MSG,MSG]...\n", QUIS);
176 int main(int argc, char *argv[])
188 /* --- Initialize mLib --- */
193 /* --- Parse options --- */
196 static struct option opt[] = {
197 { "help", 0, 0, 'h' },
198 { "usage", 0, 0, 'u' },
199 { "version", 0, 0, 'v' },
200 { "display", OPTF_ARGREQ, 0, 'd' },
201 { "atom", OPTF_ARGREQ, 0, 'a' },
202 { "msg", OPTF_ARGREQ, 0, 'm' },
203 { "force", 0, 0, 'f' },
207 int i = mdwopt(argc, argv, "d:a:m:f", opt, 0, 0, 0);
217 "Waits until signalled by `xtell' or `xshutdown'. Specifically, waits\n"
218 "until a property with name ATOM is written to the root window with\n"
223 "-h, --help Display this help text\n"
224 "-u, --usage Display a short usage summary\n"
225 "-v, --version Display the program's version number\n"
227 "-d, --display=DISPLAY Choose X display to connect to\n"
228 "-f, --force Run even if this property is waited for by another\n"
230 "-a, --atom=ATOM\t Choose property name to listen for [deprecated]\n"
231 "-m, --msg=MSG Choose value of property to wait for [deprecated]\n",
248 xa = addatom(optarg);
252 die(EXIT_FAILURE, "no atom currently defined");
265 /* --- Grind through remaining arguments in the new syntax --- */
267 while (optind < argc) {
268 char *p = strtok(argv[optind++], ":");
272 while ((p = strtok(0, ",")) != 0)
276 /* --- If there's nothing set, put in some defaults --- */
279 xa = addatom(XWAIT_DIE);
280 addmsg(xa, XWAIT_DIE_MSG);
283 /* --- Attach the atoms to the screens --- */
287 Atom ready = XInternAtom(dpy, "XWAIT_READY", False);
288 int nsc = ScreenCount(dpy);
290 /* --- First pass: make sure there's not a process already here --- */
292 if ((f & f_force) == 0) {
293 for (i = 0; i < nsc; i++) {
294 for (xa = atoms; xa; xa = xa->next) {
295 if (xatom_get(dpy, RootWindow(dpy, i), xa->a) != None)
296 die(EXIT_FAILURE, "already waiting for `%s'");
301 /* --- Second pass: set up listening to the property --- */
303 for (i = 0; i < nsc; i++) {
304 for (xa = atoms; xa; xa = xa->next)
305 xatom_set(dpy, RootWindow(dpy, i), xa->a, ready);
306 XSelectInput(dpy, RootWindow(dpy, i), PropertyChangeMask);
310 /* --- Set up a handler when children die --- *
312 * I don't fork any children? Why is this useful? Because I've been
313 * execed from a shell which started lots of background processes, and
314 * they'll zombie themselves otherwise.
320 sa.sa_handler = sigchld;
321 sigemptyset(&sa.sa_mask);
322 sigaddset(&sa.sa_mask, SIGCHLD);
323 sa.sa_flags = SA_NOCLDSTOP;
325 sa.sa_flags |= SA_RESTART;
327 sigaction(SIGCHLD, &sa, 0);
330 /* --- Now reap any which have been waiting around so far --- */
336 sigaddset(&ss, SIGCHLD);
337 sigprocmask(SIG_BLOCK, &ss, &oss);
339 sigprocmask(SIG_SETMASK, &oss, 0);
342 signal(SIGINT, tidy);
343 signal(SIGTERM, tidy);
345 /* --- Now wait for an event --- */
348 XNextEvent(dpy, &ev);
351 for (xa = atoms; xa; xa = xa->next) {
352 if (ev.xproperty.atom != xa->a)
354 a = xatom_get(dpy, ev.xproperty.window, xa->a);
359 for (xm = xa->m; xm; xm = xm->next) {
368 /* --- Finished: report the result if necessary --- */
372 fputs(XGetAtomName(dpy, xa->a), stdout);
373 if (!xa->m || xa->m->next)
374 printf(": %s\n", XGetAtomName(dpy, a));
377 } else if (!xa->m || xa->m->next)
378 printf("%s\n", XGetAtomName(dpy, a));
380 /* --- Go away --- */
386 /*----- That's all, folks -------------------------------------------------*/