3 * $Id: xwait.c,v 1.3 1998/11/21 22:30:27 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.3 1998/11/21 22:30:27 mdw
33 * Support GNU-style long options throughout, and introduce proper help
34 * text to all programs. Update manual pages to match.
36 * Revision 1.2 1998/11/18 21:25:06 mdw
37 * Reap dead children as they arrive. The previous shell may have
38 * carelessly left them behind.
40 * Revision 1.1 1998/11/16 23:00:49 mdw
45 /*----- Header files ------------------------------------------------------*/
52 #include <sys/types.h>
57 #include <X11/Xutil.h>
63 /*----- Main code ---------------------------------------------------------*/
65 /* --- @sigchld@ --- */
67 static void sigchld(int sig
)
69 while (waitpid(-1, 0, WNOHANG
) > 0)
75 static void version(FILE *fp
)
77 fprintf(fp
, "%s (xtoys version " VERSION
")\n", QUIS
);
80 static void usage(FILE *fp
)
82 fprintf(fp
, "Usage: %s [-f] [-d DISPLAY] [-a ATOM] [-m MSG]\n", QUIS
);
85 int main(int argc
, char *argv
[])
91 char *atom
= XWAIT_DIE
;
92 char *msg
= XWAIT_DIE_MSG
;
99 /* --- Parse options --- */
104 static struct option opt
[] = {
105 { "help", 0, 0, 'h' },
106 { "usage", 0, 0, 'u' },
107 { "version", 0, 0, 'v' },
108 { "display", required_argument
, 0, 'd' },
109 { "atom", required_argument
, 0, 'a' },
110 { "msg", required_argument
, 0, 'm' },
111 { "force", 0, 0, 'f' },
115 int i
= getopt_long(argc
, argv
, "d:a:m:f", opt
, 0);
125 "Waits until signalled by `xtell' or `xshutdown'. Specifically, waits\n"
126 "until a property with name ATOM is written to the root window with\n"
131 "-h, --help Display this help text\n"
132 "-u, --usage Display a short usage summary\n"
133 "-v, --version Display the program's version number\n"
135 "-d, --display=DISPLAY Choose X display to connect to\n"
136 "-f, --force Run even if this property is waited for by another\n"
138 "-a, --atom=ATOM Choose property name to listen for\n"
139 "-m, --msg=MSG Choose value of property to wait for\n",
171 /* --- Connect to the X display --- */
173 dpy
= XOpenDisplay(display
);
175 fprintf(stderr
, "xwait: couldn't open display\n");
179 /* --- Fetch the property name atom --- */
181 xwait_die
= XInternAtom(dpy
, atom
, False
);
183 /* --- Mark ourselves as listening to all the screens --- */
187 int nsc
= ScreenCount(dpy
);
189 /* --- First pass: make sure there's not a process already here --- */
191 if ((f
& f_force
) == 0) {
192 for (i
= 0; i
< nsc
; i
++) {
193 Window win
= RootWindow(dpy
, i
);
196 if (XGetTextProperty(dpy
, win
, &prop
, xwait_die
)) {
197 fprintf(stderr
, "xwait: already waiting for `%s'\n", atom
);
203 /* --- Second pass: set up listening to the property --- */
205 for (i
= 0; i
< nsc
; i
++) {
206 Window win
= RootWindow(dpy
, i
);
208 char *imsg
= "XWAIT_READY";
210 XStringListToTextProperty(&imsg
, 1, &prop
);
211 XSetTextProperty(dpy
, win
, &prop
, xwait_die
);
212 XSelectInput(dpy
, win
, PropertyChangeMask
);
216 /* --- Set up a handler when children die --- *
218 * I don't fork any children? Why is this useful? Because I've been
219 * execed from a shell which started lots of background processes, and
220 * they'll zombie themselves otherwise.
226 sa
.sa_handler
= sigchld
;
227 sigemptyset(&sa
.sa_mask
);
228 sigaddset(&sa
.sa_mask
, SIGCHLD
);
230 sigaction(SIGCHLD
, &sa
, 0);
233 /* --- Now wait for an event --- */
236 XNextEvent(dpy
, &ev
);
239 if (ev
.xproperty
.atom
== xwait_die
) {
244 if (XGetTextProperty(dpy
, ev
.xproperty
.window
, &prop
, xwait_die
)) {
245 XTextPropertyToStringList(&prop
, &sl
, &c
);
246 if (strcmp(sl
[0], msg
) == 0)
254 /* --- Finished: remove the property from all the screens --- */
259 int nsc
= ScreenCount(dpy
);
261 for (i
= 0; i
< nsc
; i
++)
262 XDeleteProperty(dpy
, RootWindow(dpy
, i
), xwait_die
);
265 /* --- Go away --- */
271 /*----- That's all, folks -------------------------------------------------*/