1a7ecd77a0eb5ef7e7c9119b2f8f71763d91d784
3 * $Id: xwait.c,v 1.4 1998/11/21 22:41:19 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.4 1998/11/21 22:41:19 mdw
33 * Reap children which die before I get my signal handler installed.
35 * Revision 1.3 1998/11/21 22:30:27 mdw
36 * Support GNU-style long options throughout, and introduce proper help
37 * text to all programs. Update manual pages to match.
39 * Revision 1.2 1998/11/18 21:25:06 mdw
40 * Reap dead children as they arrive. The previous shell may have
41 * carelessly left them behind.
43 * Revision 1.1 1998/11/16 23:00:49 mdw
48 /*----- Header files ------------------------------------------------------*/
55 #include <sys/types.h>
60 #include <X11/Xutil.h>
66 /*----- Main code ---------------------------------------------------------*/
68 /* --- @sigchld@ --- */
70 static void sigchld(int sig
)
72 while (waitpid(-1, 0, WNOHANG
) > 0)
78 static void version(FILE *fp
)
80 fprintf(fp
, "%s (xtoys version " VERSION
")\n", QUIS
);
83 static void usage(FILE *fp
)
85 fprintf(fp
, "Usage: %s [-f] [-d DISPLAY] [-a ATOM] [-m MSG]\n", QUIS
);
88 int main(int argc
, char *argv
[])
94 char *atom
= XWAIT_DIE
;
95 char *msg
= XWAIT_DIE_MSG
;
102 /* --- Parse options --- */
107 static struct option opt
[] = {
108 { "help", 0, 0, 'h' },
109 { "usage", 0, 0, 'u' },
110 { "version", 0, 0, 'v' },
111 { "display", required_argument
, 0, 'd' },
112 { "atom", required_argument
, 0, 'a' },
113 { "msg", required_argument
, 0, 'm' },
114 { "force", 0, 0, 'f' },
118 int i
= getopt_long(argc
, argv
, "d:a:m:f", opt
, 0);
128 "Waits until signalled by `xtell' or `xshutdown'. Specifically, waits\n"
129 "until a property with name ATOM is written to the root window with\n"
134 "-h, --help Display this help text\n"
135 "-u, --usage Display a short usage summary\n"
136 "-v, --version Display the program's version number\n"
138 "-d, --display=DISPLAY Choose X display to connect to\n"
139 "-f, --force Run even if this property is waited for by another\n"
141 "-a, --atom=ATOM Choose property name to listen for\n"
142 "-m, --msg=MSG Choose value of property to wait for\n",
174 /* --- Connect to the X display --- */
176 dpy
= XOpenDisplay(display
);
178 fprintf(stderr
, "xwait: couldn't open display\n");
182 /* --- Fetch the property name atom --- */
184 xwait_die
= XInternAtom(dpy
, atom
, False
);
186 /* --- Mark ourselves as listening to all the screens --- */
190 int nsc
= ScreenCount(dpy
);
192 /* --- First pass: make sure there's not a process already here --- */
194 if ((f
& f_force
) == 0) {
195 for (i
= 0; i
< nsc
; i
++) {
196 Window win
= RootWindow(dpy
, i
);
199 if (XGetTextProperty(dpy
, win
, &prop
, xwait_die
)) {
200 fprintf(stderr
, "xwait: already waiting for `%s'\n", atom
);
206 /* --- Second pass: set up listening to the property --- */
208 for (i
= 0; i
< nsc
; i
++) {
209 Window win
= RootWindow(dpy
, i
);
211 char *imsg
= "XWAIT_READY";
213 XStringListToTextProperty(&imsg
, 1, &prop
);
214 XSetTextProperty(dpy
, win
, &prop
, xwait_die
);
215 XSelectInput(dpy
, win
, PropertyChangeMask
);
219 /* --- Set up a handler when children die --- *
221 * I don't fork any children? Why is this useful? Because I've been
222 * execed from a shell which started lots of background processes, and
223 * they'll zombie themselves otherwise.
230 /* --- Set the handler up --- */
232 sa
.sa_handler
= sigchld
;
233 sigemptyset(&sa
.sa_mask
);
234 sigaddset(&sa
.sa_mask
, SIGCHLD
);
236 sigaction(SIGCHLD
, &sa
, 0);
238 /* --- Now reap any which have been waiting around so far --- */
241 sigaddset(&ss
, SIGCHLD
);
242 sigprocmask(SIG_BLOCK
, &ss
, &oss
);
244 sigprocmask(SIG_SETMASK
, &oss
, 0);
247 /* --- Now wait for an event --- */
250 XNextEvent(dpy
, &ev
);
253 if (ev
.xproperty
.atom
== xwait_die
) {
258 if (XGetTextProperty(dpy
, ev
.xproperty
.window
, &prop
, xwait_die
)) {
259 XTextPropertyToStringList(&prop
, &sl
, &c
);
260 if (strcmp(sl
[0], msg
) == 0)
268 /* --- Finished: remove the property from all the screens --- */
273 int nsc
= ScreenCount(dpy
);
275 for (i
= 0; i
< nsc
; i
++)
276 XDeleteProperty(dpy
, RootWindow(dpy
, i
), xwait_die
);
279 /* --- Go away --- */
285 /*----- That's all, folks -------------------------------------------------*/