3 * $Id: xwait.c,v 1.10 2004/04/08 01:36:29 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 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
42 #include <X11/Xutil.h>
44 #include <mLib/mdwopt.h>
45 #include <mLib/quis.h>
46 #include <mLib/report.h>
52 /*----- Data structures ---------------------------------------------------*/
54 typedef struct xwait_msg {
55 struct xwait_msg *next; /* Next message in the list */
56 Atom a; /* The message atom */
59 typedef 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 */
65 /*----- Static variables --------------------------------------------------*/
67 static xwait_atom *atoms = 0;
70 /*----- Main code ---------------------------------------------------------*/
72 /* --- @sigchld@ --- */
74 static void sigchld(int sig)
77 while (waitpid(-1, 0, WNOHANG) > 0)
82 /* --- @opendisplay@ --- */
84 static void opendisplay(const char *d)
88 if ((dpy = XOpenDisplay(d)) == 0)
89 die(EXIT_FAILURE, "couldn't open display");
92 /* --- @addatom@ --- */
94 static xwait_atom *addatom(const char *a)
96 xwait_atom *xa = CREATE(xwait_atom);
99 xa->a = XInternAtom(dpy, a, False);
105 /* --- @addmsg@ --- */
107 static void addmsg(xwait_atom *xa, const char *a)
109 xwait_msg *xm = CREATE(xwait_msg);
112 xm->a = XInternAtom(dpy, a, False);
118 static void tidy(int sig)
121 int nsc = ScreenCount(dpy);
124 for (i = 0; i < nsc; i++) {
125 for (xa = atoms; xa; xa = xa->next)
126 XDeleteProperty(dpy, RootWindow(dpy, i), xa->a);
134 static void version(FILE *fp)
136 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
139 static void usage(FILE *fp)
141 fprintf(fp, "Usage: %s [-f] [-d DISPLAY] [ATOM:MSG,MSG]...\n", QUIS);
144 int main(int argc, char *argv[])
154 /* --- Initialize mLib --- */
159 /* --- Parse options --- */
162 static struct option opt[] = {
163 { "help", 0, 0, 'h' },
164 { "usage", 0, 0, 'u' },
165 { "version", 0, 0, 'v' },
166 { "display", OPTF_ARGREQ, 0, 'd' },
167 { "atom", OPTF_ARGREQ, 0, 'a' },
168 { "msg", OPTF_ARGREQ, 0, 'm' },
169 { "force", 0, 0, 'f' },
173 int i = mdwopt(argc, argv, "d:a:m:f", opt, 0, 0, 0);
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"
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"
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"
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",
214 xa = addatom(optarg);
218 die(EXIT_FAILURE, "no atom currently defined");
231 /* --- Grind through remaining arguments in the new syntax --- */
233 while (optind < argc) {
234 char *p = strtok(argv[optind++], ":");
238 while ((p = strtok(0, ",")) != 0)
242 /* --- If there's nothing set, put in some defaults --- */
245 xa = addatom(XWAIT_DIE);
246 addmsg(xa, XWAIT_DIE_MSG);
249 /* --- Attach the atoms to the screens --- */
253 Atom ready = XInternAtom(dpy, "XWAIT_READY", False);
254 int nsc = ScreenCount(dpy);
256 /* --- First pass: make sure there's not a process already here --- */
258 if ((f & f_force) == 0) {
259 for (i = 0; i < nsc; i++) {
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'");
267 /* --- Second pass: set up listening to the property --- */
269 for (i = 0; i < nsc; i++) {
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);
276 /* --- Set up a handler when children die --- *
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.
286 sa.sa_handler = sigchld;
287 sigemptyset(&sa.sa_mask);
288 sigaddset(&sa.sa_mask, SIGCHLD);
289 sa.sa_flags = SA_NOCLDSTOP;
291 sa.sa_flags |= SA_RESTART;
293 sigaction(SIGCHLD, &sa, 0);
296 /* --- Now reap any which have been waiting around so far --- */
302 sigaddset(&ss, SIGCHLD);
303 sigprocmask(SIG_BLOCK, &ss, &oss);
305 sigprocmask(SIG_SETMASK, &oss, 0);
308 signal(SIGINT, tidy);
309 signal(SIGTERM, tidy);
311 /* --- Now wait for an event --- */
314 XNextEvent(dpy, &ev);
317 for (xa = atoms; xa; xa = xa->next) {
318 if (ev.xproperty.atom != xa->a)
320 a = xatom_get(dpy, ev.xproperty.window, xa->a);
325 for (xm = xa->m; xm; xm = xm->next) {
334 /* --- Finished: report the result if necessary --- */
338 fputs(XGetAtomName(dpy, xa->a), stdout);
339 if (!xa->m || xa->m->next)
340 printf(": %s\n", XGetAtomName(dpy, a));
343 } else if (!xa->m || xa->m->next)
344 printf("%s\n", XGetAtomName(dpy, a));
346 /* --- Go away --- */
352 /*----- That's all, folks -------------------------------------------------*/