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