3 * $Id: xwait.c,v 1.9 2002/01/13 14:44:59 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.9 2002/01/13 14:44:59 mdw
33 * Make flags be unsigned.
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.
39 * Revision 1.7 1999/06/19 23:42:37 mdw
40 * Improve signal handling.
42 * Revision 1.6 1998/12/11 09:50:07 mdw
43 * Minor modifications to work with mLib and mgLib.
45 * Revision 1.5 1998/11/30 22:36:53 mdw
46 * Tidy up tabbing in help texts very slightly.
48 * Revision 1.4 1998/11/21 22:41:19 mdw
49 * Reap children which die before I get my signal handler installed.
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.
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.
59 * Revision 1.1 1998/11/16 23:00:49 mdw
64 /*----- Header files ------------------------------------------------------*/
72 #include <sys/types.h>
77 #include <X11/Xutil.h>
79 #include <mLib/mdwopt.h>
80 #include <mLib/quis.h>
81 #include <mLib/report.h>
87 /*----- Data structures ---------------------------------------------------*/
89 typedef struct xwait_msg
{
90 struct xwait_msg
*next
; /* Next message in the list */
91 Atom a
; /* The message atom */
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 */
100 /*----- Static variables --------------------------------------------------*/
102 static xwait_atom
*atoms
= 0;
105 /*----- Main code ---------------------------------------------------------*/
107 /* --- @sigchld@ --- */
109 static void sigchld(int sig
)
112 while (waitpid(-1, 0, WNOHANG
) > 0)
117 /* --- @opendisplay@ --- */
119 static void opendisplay(const char *d
)
123 if ((dpy
= XOpenDisplay(d
)) == 0)
124 die(EXIT_FAILURE
, "couldn't open display");
127 /* --- @addatom@ --- */
129 static xwait_atom
*addatom(const char *a
)
131 xwait_atom
*xa
= CREATE(xwait_atom
);
134 xa
->a
= XInternAtom(dpy
, a
, False
);
140 /* --- @addmsg@ --- */
142 static void addmsg(xwait_atom
*xa
, const char *a
)
144 xwait_msg
*xm
= CREATE(xwait_msg
);
147 xm
->a
= XInternAtom(dpy
, a
, False
);
153 static void tidy(int sig
)
156 int nsc
= ScreenCount(dpy
);
159 for (i
= 0; i
< nsc
; i
++) {
160 for (xa
= atoms
; xa
; xa
= xa
->next
)
161 XDeleteProperty(dpy
, RootWindow(dpy
, i
), xa
->a
);
169 static void version(FILE *fp
)
171 fprintf(fp
, "%s (xtoys version " VERSION
")\n", QUIS
);
174 static void usage(FILE *fp
)
176 fprintf(fp
, "Usage: %s [-f] [-d DISPLAY] [ATOM:MSG,MSG]...\n", QUIS
);
179 int main(int argc
, char *argv
[])
189 /* --- Initialize mLib --- */
194 /* --- Parse options --- */
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' },
208 int i
= mdwopt(argc
, argv
, "d:a:m:f", opt
, 0, 0, 0);
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"
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"
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"
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",
249 xa
= addatom(optarg
);
253 die(EXIT_FAILURE
, "no atom currently defined");
266 /* --- Grind through remaining arguments in the new syntax --- */
268 while (optind
< argc
) {
269 char *p
= strtok(argv
[optind
++], ":");
273 while ((p
= strtok(0, ",")) != 0)
277 /* --- If there's nothing set, put in some defaults --- */
280 xa
= addatom(XWAIT_DIE
);
281 addmsg(xa
, XWAIT_DIE_MSG
);
284 /* --- Attach the atoms to the screens --- */
288 Atom ready
= XInternAtom(dpy
, "XWAIT_READY", False
);
289 int nsc
= ScreenCount(dpy
);
291 /* --- First pass: make sure there's not a process already here --- */
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'");
302 /* --- Second pass: set up listening to the property --- */
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
);
311 /* --- Set up a handler when children die --- *
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.
321 sa
.sa_handler
= sigchld
;
322 sigemptyset(&sa
.sa_mask
);
323 sigaddset(&sa
.sa_mask
, SIGCHLD
);
324 sa
.sa_flags
= SA_NOCLDSTOP
;
326 sa
.sa_flags
|= SA_RESTART
;
328 sigaction(SIGCHLD
, &sa
, 0);
331 /* --- Now reap any which have been waiting around so far --- */
337 sigaddset(&ss
, SIGCHLD
);
338 sigprocmask(SIG_BLOCK
, &ss
, &oss
);
340 sigprocmask(SIG_SETMASK
, &oss
, 0);
343 signal(SIGINT
, tidy
);
344 signal(SIGTERM
, tidy
);
346 /* --- Now wait for an event --- */
349 XNextEvent(dpy
, &ev
);
352 for (xa
= atoms
; xa
; xa
= xa
->next
) {
353 if (ev
.xproperty
.atom
!= xa
->a
)
355 a
= xatom_get(dpy
, ev
.xproperty
.window
, xa
->a
);
360 for (xm
= xa
->m
; xm
; xm
= xm
->next
) {
369 /* --- Finished: report the result if necessary --- */
373 fputs(XGetAtomName(dpy
, xa
->a
), stdout
);
374 if (!xa
->m
|| xa
->m
->next
)
375 printf(": %s\n", XGetAtomName(dpy
, a
));
378 } else if (!xa
->m
|| xa
->m
->next
)
379 printf("%s\n", XGetAtomName(dpy
, a
));
381 /* --- Go away --- */
387 /*----- That's all, folks -------------------------------------------------*/