90b2c5d4 |
1 | /* -*-c-*- |
2 | * |
5a314f82 |
3 | * $Id: xwait.c,v 1.4 1998/11/21 22:41:19 mdw Exp $ |
90b2c5d4 |
4 | * |
5 | * Wait until prodded by another X client |
6 | * |
7 | * (c) 1998 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of the Edgeware X tools collection. |
13 | * |
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. |
18 | * |
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. |
23 | * |
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. |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: xwait.c,v $ |
5a314f82 |
32 | * Revision 1.4 1998/11/21 22:41:19 mdw |
33 | * Reap children which die before I get my signal handler installed. |
34 | * |
f3b35b6b |
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. |
38 | * |
203d0643 |
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. |
42 | * |
90b2c5d4 |
43 | * Revision 1.1 1998/11/16 23:00:49 mdw |
44 | * Initial versions. |
45 | * |
46 | */ |
47 | |
48 | /*----- Header files ------------------------------------------------------*/ |
49 | |
203d0643 |
50 | #include <signal.h> |
90b2c5d4 |
51 | #include <stdio.h> |
52 | #include <stdlib.h> |
53 | #include <string.h> |
54 | |
203d0643 |
55 | #include <sys/types.h> |
56 | #include <sys/wait.h> |
90b2c5d4 |
57 | #include <unistd.h> |
58 | |
59 | #include <X11/Xlib.h> |
60 | #include <X11/Xutil.h> |
61 | |
f3b35b6b |
62 | #include "mdwopt.h" |
63 | #include "quis.h" |
90b2c5d4 |
64 | #include "xwait.h" |
65 | |
66 | /*----- Main code ---------------------------------------------------------*/ |
67 | |
203d0643 |
68 | /* --- @sigchld@ --- */ |
69 | |
70 | static void sigchld(int sig) |
71 | { |
72 | while (waitpid(-1, 0, WNOHANG) > 0) |
73 | ; |
74 | } |
75 | |
76 | /* --- @main@ --- */ |
77 | |
f3b35b6b |
78 | static void version(FILE *fp) |
79 | { |
80 | fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS); |
81 | } |
82 | |
83 | static void usage(FILE *fp) |
84 | { |
85 | fprintf(fp, "Usage: %s [-f] [-d DISPLAY] [-a ATOM] [-m MSG]\n", QUIS); |
86 | } |
87 | |
90b2c5d4 |
88 | int main(int argc, char *argv[]) |
89 | { |
90 | char *display = 0; |
91 | Display *dpy; |
92 | Atom xwait_die; |
93 | XEvent ev; |
94 | char *atom = XWAIT_DIE; |
95 | char *msg = XWAIT_DIE_MSG; |
96 | unsigned f = 0; |
97 | |
98 | enum { |
99 | f_force = 1 |
100 | }; |
101 | |
102 | /* --- Parse options --- */ |
103 | |
f3b35b6b |
104 | ego(argv[0]); |
105 | |
90b2c5d4 |
106 | for (;;) { |
f3b35b6b |
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' }, |
115 | { 0, 0, 0, 0 } |
116 | }; |
117 | |
118 | int i = getopt_long(argc, argv, "d:a:m:f", opt, 0); |
90b2c5d4 |
119 | if (i < 0) |
120 | break; |
121 | switch (i) { |
f3b35b6b |
122 | case 'h': |
123 | version(stdout); |
124 | fputs("\n", stdout); |
125 | usage(stdout); |
126 | fputs( |
127 | "\n" |
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" |
130 | "contents MSG.\n" |
131 | "\n" |
132 | "Options:\n" |
133 | "\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" |
137 | "\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" |
140 | " process\n" |
141 | "-a, --atom=ATOM Choose property name to listen for\n" |
142 | "-m, --msg=MSG Choose value of property to wait for\n", |
143 | stdout); |
144 | exit(0); |
145 | break; |
146 | case 'u': |
147 | usage(stdout); |
148 | exit(0); |
149 | break; |
150 | case 'v': |
151 | version(stdout); |
152 | exit(0); |
153 | break; |
154 | |
90b2c5d4 |
155 | case 'd': |
156 | display = optarg; |
157 | break; |
158 | case 'a': |
159 | atom = optarg; |
160 | break; |
161 | case 'm': |
162 | msg = optarg; |
163 | break; |
164 | case 'f': |
165 | f |= f_force; |
166 | break; |
167 | default: |
f3b35b6b |
168 | usage(stderr); |
90b2c5d4 |
169 | exit(EXIT_FAILURE); |
170 | break; |
171 | } |
172 | } |
173 | |
174 | /* --- Connect to the X display --- */ |
175 | |
176 | dpy = XOpenDisplay(display); |
177 | if (!dpy) { |
178 | fprintf(stderr, "xwait: couldn't open display\n"); |
179 | exit(EXIT_FAILURE); |
180 | } |
181 | |
182 | /* --- Fetch the property name atom --- */ |
183 | |
184 | xwait_die = XInternAtom(dpy, atom, False); |
185 | |
186 | /* --- Mark ourselves as listening to all the screens --- */ |
187 | |
188 | { |
189 | int i; |
190 | int nsc = ScreenCount(dpy); |
191 | |
192 | /* --- First pass: make sure there's not a process already here --- */ |
193 | |
194 | if ((f & f_force) == 0) { |
195 | for (i = 0; i < nsc; i++) { |
196 | Window win = RootWindow(dpy, i); |
197 | XTextProperty prop; |
198 | |
199 | if (XGetTextProperty(dpy, win, &prop, xwait_die)) { |
200 | fprintf(stderr, "xwait: already waiting for `%s'\n", atom); |
201 | exit(EXIT_FAILURE); |
202 | } |
203 | } |
204 | } |
205 | |
206 | /* --- Second pass: set up listening to the property --- */ |
207 | |
208 | for (i = 0; i < nsc; i++) { |
209 | Window win = RootWindow(dpy, i); |
210 | XTextProperty prop; |
211 | char *imsg = "XWAIT_READY"; |
212 | |
213 | XStringListToTextProperty(&imsg, 1, &prop); |
214 | XSetTextProperty(dpy, win, &prop, xwait_die); |
215 | XSelectInput(dpy, win, PropertyChangeMask); |
216 | } |
217 | } |
218 | |
203d0643 |
219 | /* --- Set up a handler when children die --- * |
220 | * |
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. |
224 | */ |
225 | |
226 | { |
227 | struct sigaction sa; |
5a314f82 |
228 | sigset_t ss, oss; |
229 | |
230 | /* --- Set the handler up --- */ |
203d0643 |
231 | |
232 | sa.sa_handler = sigchld; |
233 | sigemptyset(&sa.sa_mask); |
234 | sigaddset(&sa.sa_mask, SIGCHLD); |
235 | sa.sa_flags = 0; |
236 | sigaction(SIGCHLD, &sa, 0); |
5a314f82 |
237 | |
238 | /* --- Now reap any which have been waiting around so far --- */ |
239 | |
240 | sigemptyset(&ss); |
241 | sigaddset(&ss, SIGCHLD); |
242 | sigprocmask(SIG_BLOCK, &ss, &oss); |
243 | sigchld(SIGCHLD); |
244 | sigprocmask(SIG_SETMASK, &oss, 0); |
203d0643 |
245 | } |
246 | |
90b2c5d4 |
247 | /* --- Now wait for an event --- */ |
248 | |
249 | for (;;) { |
250 | XNextEvent(dpy, &ev); |
251 | switch (ev.type) { |
252 | case PropertyNotify: |
253 | if (ev.xproperty.atom == xwait_die) { |
254 | XTextProperty prop; |
255 | char **sl; |
256 | int c; |
257 | |
258 | if (XGetTextProperty(dpy, ev.xproperty.window, &prop, xwait_die)) { |
259 | XTextPropertyToStringList(&prop, &sl, &c); |
260 | if (strcmp(sl[0], msg) == 0) |
261 | goto exit; |
262 | XFreeStringList(sl); |
263 | } |
264 | } |
265 | } |
266 | } |
267 | |
268 | /* --- Finished: remove the property from all the screens --- */ |
269 | |
270 | exit: |
271 | { |
272 | int i; |
273 | int nsc = ScreenCount(dpy); |
274 | |
275 | for (i = 0; i < nsc; i++) |
276 | XDeleteProperty(dpy, RootWindow(dpy, i), xwait_die); |
277 | } |
278 | |
279 | /* --- Go away --- */ |
280 | |
281 | XCloseDisplay(dpy); |
282 | return (0); |
283 | } |
284 | |
285 | /*----- That's all, folks -------------------------------------------------*/ |