3 * $Id: xwait.c,v 1.2 1998/11/18 21:25:06 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.2 1998/11/18 21:25:06 mdw
33 * Reap dead children as they arrive. The previous shell may have
34 * carelessly left them behind.
36 * Revision 1.1 1998/11/16 23:00:49 mdw
41 /*----- Header files ------------------------------------------------------*/
48 #include <sys/types.h>
53 #include <X11/Xutil.h>
57 /*----- Main code ---------------------------------------------------------*/
59 /* --- @sigchld@ --- */
61 static void sigchld(int sig)
63 while (waitpid(-1, 0, WNOHANG) > 0)
69 int main(int argc, char *argv[])
75 char *atom = XWAIT_DIE;
76 char *msg = XWAIT_DIE_MSG;
83 /* --- Parse options --- */
86 int i = getopt(argc, argv, "d:a:m:f");
104 "Usage: xwait [-f] [-d DISPLAY] [-a ATOM] [-m MSG]\n");
110 /* --- Connect to the X display --- */
112 dpy = XOpenDisplay(display);
114 fprintf(stderr, "xwait: couldn't open display\n");
118 /* --- Fetch the property name atom --- */
120 xwait_die = XInternAtom(dpy, atom, False);
122 /* --- Mark ourselves as listening to all the screens --- */
126 int nsc = ScreenCount(dpy);
128 /* --- First pass: make sure there's not a process already here --- */
130 if ((f & f_force) == 0) {
131 for (i = 0; i < nsc; i++) {
132 Window win = RootWindow(dpy, i);
135 if (XGetTextProperty(dpy, win, &prop, xwait_die)) {
136 fprintf(stderr, "xwait: already waiting for `%s'\n", atom);
142 /* --- Second pass: set up listening to the property --- */
144 for (i = 0; i < nsc; i++) {
145 Window win = RootWindow(dpy, i);
147 char *imsg = "XWAIT_READY";
149 XStringListToTextProperty(&imsg, 1, &prop);
150 XSetTextProperty(dpy, win, &prop, xwait_die);
151 XSelectInput(dpy, win, PropertyChangeMask);
155 /* --- Set up a handler when children die --- *
157 * I don't fork any children? Why is this useful? Because I've been
158 * execed from a shell which started lots of background processes, and
159 * they'll zombie themselves otherwise.
165 sa.sa_handler = sigchld;
166 sigemptyset(&sa.sa_mask);
167 sigaddset(&sa.sa_mask, SIGCHLD);
169 sigaction(SIGCHLD, &sa, 0);
172 /* --- Now wait for an event --- */
175 XNextEvent(dpy, &ev);
178 if (ev.xproperty.atom == xwait_die) {
183 if (XGetTextProperty(dpy, ev.xproperty.window, &prop, xwait_die)) {
184 XTextPropertyToStringList(&prop, &sl, &c);
185 if (strcmp(sl[0], msg) == 0)
193 /* --- Finished: remove the property from all the screens --- */
198 int nsc = ScreenCount(dpy);
200 for (i = 0; i < nsc; i++)
201 XDeleteProperty(dpy, RootWindow(dpy, i), xwait_die);
204 /* --- Go away --- */
210 /*----- That's all, folks -------------------------------------------------*/