90b2c5d4 |
1 | /* -*-c-*- |
2 | * |
25c93171 |
3 | * $Id: xwait.c,v 1.8 1999/08/20 07:29:55 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 $ |
25c93171 |
32 | * Revision 1.8 1999/08/20 07:29:55 mdw |
33 | * New command line syntax, and new atom protocol. Wait for multiple |
34 | * properties and listen for multiple (or all) values. |
35 | * |
7b50dab0 |
36 | * Revision 1.7 1999/06/19 23:42:37 mdw |
37 | * Improve signal handling. |
38 | * |
c4efa11c |
39 | * Revision 1.6 1998/12/11 09:50:07 mdw |
40 | * Minor modifications to work with mLib and mgLib. |
41 | * |
d6130abd |
42 | * Revision 1.5 1998/11/30 22:36:53 mdw |
43 | * Tidy up tabbing in help texts very slightly. |
44 | * |
5a314f82 |
45 | * Revision 1.4 1998/11/21 22:41:19 mdw |
46 | * Reap children which die before I get my signal handler installed. |
47 | * |
f3b35b6b |
48 | * Revision 1.3 1998/11/21 22:30:27 mdw |
49 | * Support GNU-style long options throughout, and introduce proper help |
50 | * text to all programs. Update manual pages to match. |
51 | * |
203d0643 |
52 | * Revision 1.2 1998/11/18 21:25:06 mdw |
53 | * Reap dead children as they arrive. The previous shell may have |
54 | * carelessly left them behind. |
55 | * |
90b2c5d4 |
56 | * Revision 1.1 1998/11/16 23:00:49 mdw |
57 | * Initial versions. |
58 | * |
59 | */ |
60 | |
61 | /*----- Header files ------------------------------------------------------*/ |
62 | |
7b50dab0 |
63 | #include <errno.h> |
203d0643 |
64 | #include <signal.h> |
90b2c5d4 |
65 | #include <stdio.h> |
66 | #include <stdlib.h> |
67 | #include <string.h> |
68 | |
203d0643 |
69 | #include <sys/types.h> |
70 | #include <sys/wait.h> |
90b2c5d4 |
71 | #include <unistd.h> |
72 | |
73 | #include <X11/Xlib.h> |
74 | #include <X11/Xutil.h> |
75 | |
c4efa11c |
76 | #include <mLib/mdwopt.h> |
77 | #include <mLib/quis.h> |
25c93171 |
78 | #include <mLib/report.h> |
79 | #include <mLib/sub.h> |
c4efa11c |
80 | |
25c93171 |
81 | #include "xatom.h" |
90b2c5d4 |
82 | #include "xwait.h" |
83 | |
25c93171 |
84 | /*----- Data structures ---------------------------------------------------*/ |
85 | |
86 | typedef struct xwait_msg { |
87 | struct xwait_msg *next; /* Next message in the list */ |
88 | Atom a; /* The message atom */ |
89 | } xwait_msg; |
90 | |
91 | typedef struct xwait_atom { |
92 | struct xwait_atom *next; /* Next atom in the list */ |
93 | Atom a; /* The actual atom */ |
94 | xwait_msg *m; /* List of interesting messages */ |
95 | } xwait_atom; |
96 | |
97 | /*----- Static variables --------------------------------------------------*/ |
98 | |
99 | static xwait_atom *atoms = 0; |
100 | static Display *dpy; |
101 | |
90b2c5d4 |
102 | /*----- Main code ---------------------------------------------------------*/ |
103 | |
203d0643 |
104 | /* --- @sigchld@ --- */ |
105 | |
106 | static void sigchld(int sig) |
107 | { |
7b50dab0 |
108 | int e = errno; |
203d0643 |
109 | while (waitpid(-1, 0, WNOHANG) > 0) |
110 | ; |
7b50dab0 |
111 | errno = e; |
203d0643 |
112 | } |
113 | |
25c93171 |
114 | /* --- @opendisplay@ --- */ |
115 | |
116 | static void opendisplay(const char *d) |
117 | { |
118 | if (dpy) |
119 | return; |
120 | if ((dpy = XOpenDisplay(d)) == 0) |
121 | die(EXIT_FAILURE, "couldn't open display"); |
122 | } |
123 | |
124 | /* --- @addatom@ --- */ |
125 | |
126 | static xwait_atom *addatom(const char *a) |
127 | { |
128 | xwait_atom *xa = CREATE(xwait_atom); |
129 | opendisplay(0); |
130 | xa->next = atoms; |
131 | xa->a = XInternAtom(dpy, a, False); |
132 | xa->m = 0; |
133 | atoms = xa; |
134 | return (xa); |
135 | } |
136 | |
137 | /* --- @addmsg@ --- */ |
138 | |
139 | static void addmsg(xwait_atom *xa, const char *a) |
140 | { |
141 | xwait_msg *xm = CREATE(xwait_msg); |
142 | opendisplay(0); |
143 | xm->next = xa->m; |
144 | xm->a = XInternAtom(dpy, a, False); |
145 | xa->m = xm; |
146 | } |
147 | |
148 | /* --- @tidy@ --- */ |
149 | |
150 | static void tidy(int sig) |
151 | { |
152 | int i; |
153 | int nsc = ScreenCount(dpy); |
154 | xwait_atom *xa; |
155 | |
156 | for (i = 0; i < nsc; i++) { |
157 | for (xa = atoms; xa; xa = xa->next) |
158 | XDeleteProperty(dpy, RootWindow(dpy, i), xa->a); |
159 | } |
160 | XCloseDisplay(dpy); |
161 | exit(0); |
162 | } |
163 | |
203d0643 |
164 | /* --- @main@ --- */ |
165 | |
f3b35b6b |
166 | static void version(FILE *fp) |
167 | { |
168 | fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS); |
169 | } |
170 | |
171 | static void usage(FILE *fp) |
172 | { |
25c93171 |
173 | fprintf(fp, "Usage: %s [-f] [-d DISPLAY] [ATOM:MSG,MSG]...\n", QUIS); |
f3b35b6b |
174 | } |
175 | |
90b2c5d4 |
176 | int main(int argc, char *argv[]) |
177 | { |
25c93171 |
178 | Atom a; |
90b2c5d4 |
179 | XEvent ev; |
90b2c5d4 |
180 | unsigned f = 0; |
25c93171 |
181 | xwait_atom *xa = 0; |
182 | xwait_msg *xm = 0; |
90b2c5d4 |
183 | |
184 | enum { |
185 | f_force = 1 |
186 | }; |
187 | |
25c93171 |
188 | /* --- Initialize mLib --- */ |
90b2c5d4 |
189 | |
f3b35b6b |
190 | ego(argv[0]); |
25c93171 |
191 | sub_init(); |
192 | |
193 | /* --- Parse options --- */ |
f3b35b6b |
194 | |
90b2c5d4 |
195 | for (;;) { |
f3b35b6b |
196 | static struct option opt[] = { |
197 | { "help", 0, 0, 'h' }, |
198 | { "usage", 0, 0, 'u' }, |
199 | { "version", 0, 0, 'v' }, |
25c93171 |
200 | { "display", OPTF_ARGREQ, 0, 'd' }, |
201 | { "atom", OPTF_ARGREQ, 0, 'a' }, |
202 | { "msg", OPTF_ARGREQ, 0, 'm' }, |
f3b35b6b |
203 | { "force", 0, 0, 'f' }, |
204 | { 0, 0, 0, 0 } |
205 | }; |
206 | |
25c93171 |
207 | int i = mdwopt(argc, argv, "d:a:m:f", opt, 0, 0, 0); |
90b2c5d4 |
208 | if (i < 0) |
209 | break; |
210 | switch (i) { |
f3b35b6b |
211 | case 'h': |
212 | version(stdout); |
213 | fputs("\n", stdout); |
214 | usage(stdout); |
215 | fputs( |
216 | "\n" |
217 | "Waits until signalled by `xtell' or `xshutdown'. Specifically, waits\n" |
218 | "until a property with name ATOM is written to the root window with\n" |
219 | "contents MSG.\n" |
220 | "\n" |
221 | "Options:\n" |
222 | "\n" |
223 | "-h, --help Display this help text\n" |
224 | "-u, --usage Display a short usage summary\n" |
225 | "-v, --version Display the program's version number\n" |
226 | "\n" |
227 | "-d, --display=DISPLAY Choose X display to connect to\n" |
228 | "-f, --force Run even if this property is waited for by another\n" |
229 | " process\n" |
25c93171 |
230 | "-a, --atom=ATOM\t Choose property name to listen for [deprecated]\n" |
231 | "-m, --msg=MSG Choose value of property to wait for [deprecated]\n", |
f3b35b6b |
232 | stdout); |
233 | exit(0); |
234 | break; |
235 | case 'u': |
236 | usage(stdout); |
237 | exit(0); |
238 | break; |
239 | case 'v': |
240 | version(stdout); |
241 | exit(0); |
242 | break; |
243 | |
90b2c5d4 |
244 | case 'd': |
25c93171 |
245 | opendisplay(optarg); |
90b2c5d4 |
246 | break; |
247 | case 'a': |
25c93171 |
248 | xa = addatom(optarg); |
90b2c5d4 |
249 | break; |
250 | case 'm': |
25c93171 |
251 | if (!xa) |
252 | die(EXIT_FAILURE, "no atom currently defined"); |
253 | addmsg(xa, optarg); |
90b2c5d4 |
254 | break; |
255 | case 'f': |
256 | f |= f_force; |
257 | break; |
258 | default: |
f3b35b6b |
259 | usage(stderr); |
90b2c5d4 |
260 | exit(EXIT_FAILURE); |
261 | break; |
262 | } |
263 | } |
264 | |
25c93171 |
265 | /* --- Grind through remaining arguments in the new syntax --- */ |
90b2c5d4 |
266 | |
25c93171 |
267 | while (optind < argc) { |
268 | char *p = strtok(argv[optind++], ":"); |
269 | if (!p) |
270 | continue; |
271 | xa = addatom(p); |
272 | while ((p = strtok(0, ",")) != 0) |
273 | addmsg(xa, p); |
90b2c5d4 |
274 | } |
275 | |
25c93171 |
276 | /* --- If there's nothing set, put in some defaults --- */ |
90b2c5d4 |
277 | |
25c93171 |
278 | if (!atoms) { |
279 | xa = addatom(XWAIT_DIE); |
280 | addmsg(xa, XWAIT_DIE_MSG); |
281 | } |
90b2c5d4 |
282 | |
25c93171 |
283 | /* --- Attach the atoms to the screens --- */ |
90b2c5d4 |
284 | |
285 | { |
286 | int i; |
25c93171 |
287 | Atom ready = XInternAtom(dpy, "XWAIT_READY", False); |
90b2c5d4 |
288 | int nsc = ScreenCount(dpy); |
289 | |
290 | /* --- First pass: make sure there's not a process already here --- */ |
291 | |
292 | if ((f & f_force) == 0) { |
293 | for (i = 0; i < nsc; i++) { |
25c93171 |
294 | for (xa = atoms; xa; xa = xa->next) { |
295 | if (xatom_get(dpy, RootWindow(dpy, i), xa->a) != None) |
296 | die(EXIT_FAILURE, "already waiting for `%s'"); |
90b2c5d4 |
297 | } |
298 | } |
299 | } |
300 | |
301 | /* --- Second pass: set up listening to the property --- */ |
302 | |
303 | for (i = 0; i < nsc; i++) { |
25c93171 |
304 | for (xa = atoms; xa; xa = xa->next) |
305 | xatom_set(dpy, RootWindow(dpy, i), xa->a, ready); |
306 | XSelectInput(dpy, RootWindow(dpy, i), PropertyChangeMask); |
90b2c5d4 |
307 | } |
308 | } |
309 | |
203d0643 |
310 | /* --- Set up a handler when children die --- * |
311 | * |
312 | * I don't fork any children? Why is this useful? Because I've been |
313 | * execed from a shell which started lots of background processes, and |
314 | * they'll zombie themselves otherwise. |
315 | */ |
316 | |
317 | { |
318 | struct sigaction sa; |
319 | |
320 | sa.sa_handler = sigchld; |
321 | sigemptyset(&sa.sa_mask); |
322 | sigaddset(&sa.sa_mask, SIGCHLD); |
7b50dab0 |
323 | sa.sa_flags = SA_NOCLDSTOP; |
324 | #ifdef SA_RESTART |
325 | sa.sa_flags |= SA_RESTART; |
326 | #endif |
203d0643 |
327 | sigaction(SIGCHLD, &sa, 0); |
25c93171 |
328 | } |
5a314f82 |
329 | |
25c93171 |
330 | /* --- Now reap any which have been waiting around so far --- */ |
331 | |
332 | { |
333 | sigset_t ss, oss; |
5a314f82 |
334 | |
335 | sigemptyset(&ss); |
336 | sigaddset(&ss, SIGCHLD); |
337 | sigprocmask(SIG_BLOCK, &ss, &oss); |
338 | sigchld(SIGCHLD); |
339 | sigprocmask(SIG_SETMASK, &oss, 0); |
203d0643 |
340 | } |
341 | |
25c93171 |
342 | signal(SIGINT, tidy); |
343 | signal(SIGTERM, tidy); |
344 | |
90b2c5d4 |
345 | /* --- Now wait for an event --- */ |
346 | |
347 | for (;;) { |
348 | XNextEvent(dpy, &ev); |
349 | switch (ev.type) { |
350 | case PropertyNotify: |
25c93171 |
351 | for (xa = atoms; xa; xa = xa->next) { |
352 | if (ev.xproperty.atom != xa->a) |
353 | continue; |
354 | a = xatom_get(dpy, ev.xproperty.window, xa->a); |
355 | if (a == None) |
356 | continue; |
357 | if (xa->m == 0) |
358 | goto exit; |
359 | for (xm = xa->m; xm; xm = xm->next) { |
360 | if (a == xm->a) |
90b2c5d4 |
361 | goto exit; |
90b2c5d4 |
362 | } |
363 | } |
25c93171 |
364 | break; |
90b2c5d4 |
365 | } |
366 | } |
367 | |
25c93171 |
368 | /* --- Finished: report the result if necessary --- */ |
90b2c5d4 |
369 | |
370 | exit: |
25c93171 |
371 | if (atoms->next) { |
372 | fputs(XGetAtomName(dpy, xa->a), stdout); |
373 | if (!xa->m || xa->m->next) |
374 | printf(": %s\n", XGetAtomName(dpy, a)); |
375 | else |
376 | fputc('\n', stdout); |
377 | } else if (!xa->m || xa->m->next) |
378 | printf("%s\n", XGetAtomName(dpy, a)); |
90b2c5d4 |
379 | |
380 | /* --- Go away --- */ |
381 | |
25c93171 |
382 | tidy(0); |
90b2c5d4 |
383 | return (0); |
384 | } |
385 | |
386 | /*----- That's all, folks -------------------------------------------------*/ |