90b2c5d4 |
1 | /* -*-c-*- |
2 | * |
c4efa11c |
3 | * $Id: xwait.c,v 1.6 1998/12/11 09:50:07 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 $ |
c4efa11c |
32 | * Revision 1.6 1998/12/11 09:50:07 mdw |
33 | * Minor modifications to work with mLib and mgLib. |
34 | * |
d6130abd |
35 | * Revision 1.5 1998/11/30 22:36:53 mdw |
36 | * Tidy up tabbing in help texts very slightly. |
37 | * |
5a314f82 |
38 | * Revision 1.4 1998/11/21 22:41:19 mdw |
39 | * Reap children which die before I get my signal handler installed. |
40 | * |
f3b35b6b |
41 | * Revision 1.3 1998/11/21 22:30:27 mdw |
42 | * Support GNU-style long options throughout, and introduce proper help |
43 | * text to all programs. Update manual pages to match. |
44 | * |
203d0643 |
45 | * Revision 1.2 1998/11/18 21:25:06 mdw |
46 | * Reap dead children as they arrive. The previous shell may have |
47 | * carelessly left them behind. |
48 | * |
90b2c5d4 |
49 | * Revision 1.1 1998/11/16 23:00:49 mdw |
50 | * Initial versions. |
51 | * |
52 | */ |
53 | |
54 | /*----- Header files ------------------------------------------------------*/ |
55 | |
203d0643 |
56 | #include <signal.h> |
90b2c5d4 |
57 | #include <stdio.h> |
58 | #include <stdlib.h> |
59 | #include <string.h> |
60 | |
203d0643 |
61 | #include <sys/types.h> |
62 | #include <sys/wait.h> |
90b2c5d4 |
63 | #include <unistd.h> |
64 | |
65 | #include <X11/Xlib.h> |
66 | #include <X11/Xutil.h> |
67 | |
c4efa11c |
68 | #include <mLib/mdwopt.h> |
69 | #include <mLib/quis.h> |
70 | |
90b2c5d4 |
71 | #include "xwait.h" |
72 | |
73 | /*----- Main code ---------------------------------------------------------*/ |
74 | |
203d0643 |
75 | /* --- @sigchld@ --- */ |
76 | |
77 | static void sigchld(int sig) |
78 | { |
79 | while (waitpid(-1, 0, WNOHANG) > 0) |
80 | ; |
81 | } |
82 | |
83 | /* --- @main@ --- */ |
84 | |
f3b35b6b |
85 | static void version(FILE *fp) |
86 | { |
87 | fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS); |
88 | } |
89 | |
90 | static void usage(FILE *fp) |
91 | { |
92 | fprintf(fp, "Usage: %s [-f] [-d DISPLAY] [-a ATOM] [-m MSG]\n", QUIS); |
93 | } |
94 | |
90b2c5d4 |
95 | int main(int argc, char *argv[]) |
96 | { |
97 | char *display = 0; |
98 | Display *dpy; |
99 | Atom xwait_die; |
100 | XEvent ev; |
101 | char *atom = XWAIT_DIE; |
102 | char *msg = XWAIT_DIE_MSG; |
103 | unsigned f = 0; |
104 | |
105 | enum { |
106 | f_force = 1 |
107 | }; |
108 | |
109 | /* --- Parse options --- */ |
110 | |
f3b35b6b |
111 | ego(argv[0]); |
112 | |
90b2c5d4 |
113 | for (;;) { |
f3b35b6b |
114 | static struct option opt[] = { |
115 | { "help", 0, 0, 'h' }, |
116 | { "usage", 0, 0, 'u' }, |
117 | { "version", 0, 0, 'v' }, |
118 | { "display", required_argument, 0, 'd' }, |
119 | { "atom", required_argument, 0, 'a' }, |
120 | { "msg", required_argument, 0, 'm' }, |
121 | { "force", 0, 0, 'f' }, |
122 | { 0, 0, 0, 0 } |
123 | }; |
124 | |
125 | int i = getopt_long(argc, argv, "d:a:m:f", opt, 0); |
90b2c5d4 |
126 | if (i < 0) |
127 | break; |
128 | switch (i) { |
f3b35b6b |
129 | case 'h': |
130 | version(stdout); |
131 | fputs("\n", stdout); |
132 | usage(stdout); |
133 | fputs( |
134 | "\n" |
135 | "Waits until signalled by `xtell' or `xshutdown'. Specifically, waits\n" |
136 | "until a property with name ATOM is written to the root window with\n" |
137 | "contents MSG.\n" |
138 | "\n" |
139 | "Options:\n" |
140 | "\n" |
141 | "-h, --help Display this help text\n" |
142 | "-u, --usage Display a short usage summary\n" |
143 | "-v, --version Display the program's version number\n" |
144 | "\n" |
145 | "-d, --display=DISPLAY Choose X display to connect to\n" |
146 | "-f, --force Run even if this property is waited for by another\n" |
147 | " process\n" |
d6130abd |
148 | "-a, --atom=ATOM\t Choose property name to listen for\n" |
f3b35b6b |
149 | "-m, --msg=MSG Choose value of property to wait for\n", |
150 | stdout); |
151 | exit(0); |
152 | break; |
153 | case 'u': |
154 | usage(stdout); |
155 | exit(0); |
156 | break; |
157 | case 'v': |
158 | version(stdout); |
159 | exit(0); |
160 | break; |
161 | |
90b2c5d4 |
162 | case 'd': |
163 | display = optarg; |
164 | break; |
165 | case 'a': |
166 | atom = optarg; |
167 | break; |
168 | case 'm': |
169 | msg = optarg; |
170 | break; |
171 | case 'f': |
172 | f |= f_force; |
173 | break; |
174 | default: |
f3b35b6b |
175 | usage(stderr); |
90b2c5d4 |
176 | exit(EXIT_FAILURE); |
177 | break; |
178 | } |
179 | } |
180 | |
181 | /* --- Connect to the X display --- */ |
182 | |
183 | dpy = XOpenDisplay(display); |
184 | if (!dpy) { |
185 | fprintf(stderr, "xwait: couldn't open display\n"); |
186 | exit(EXIT_FAILURE); |
187 | } |
188 | |
189 | /* --- Fetch the property name atom --- */ |
190 | |
191 | xwait_die = XInternAtom(dpy, atom, False); |
192 | |
193 | /* --- Mark ourselves as listening to all the screens --- */ |
194 | |
195 | { |
196 | int i; |
197 | int nsc = ScreenCount(dpy); |
198 | |
199 | /* --- First pass: make sure there's not a process already here --- */ |
200 | |
201 | if ((f & f_force) == 0) { |
202 | for (i = 0; i < nsc; i++) { |
203 | Window win = RootWindow(dpy, i); |
204 | XTextProperty prop; |
205 | |
206 | if (XGetTextProperty(dpy, win, &prop, xwait_die)) { |
207 | fprintf(stderr, "xwait: already waiting for `%s'\n", atom); |
208 | exit(EXIT_FAILURE); |
209 | } |
210 | } |
211 | } |
212 | |
213 | /* --- Second pass: set up listening to the property --- */ |
214 | |
215 | for (i = 0; i < nsc; i++) { |
216 | Window win = RootWindow(dpy, i); |
217 | XTextProperty prop; |
218 | char *imsg = "XWAIT_READY"; |
219 | |
220 | XStringListToTextProperty(&imsg, 1, &prop); |
221 | XSetTextProperty(dpy, win, &prop, xwait_die); |
222 | XSelectInput(dpy, win, PropertyChangeMask); |
223 | } |
224 | } |
225 | |
203d0643 |
226 | /* --- Set up a handler when children die --- * |
227 | * |
228 | * I don't fork any children? Why is this useful? Because I've been |
229 | * execed from a shell which started lots of background processes, and |
230 | * they'll zombie themselves otherwise. |
231 | */ |
232 | |
233 | { |
234 | struct sigaction sa; |
5a314f82 |
235 | sigset_t ss, oss; |
236 | |
237 | /* --- Set the handler up --- */ |
203d0643 |
238 | |
239 | sa.sa_handler = sigchld; |
240 | sigemptyset(&sa.sa_mask); |
241 | sigaddset(&sa.sa_mask, SIGCHLD); |
242 | sa.sa_flags = 0; |
243 | sigaction(SIGCHLD, &sa, 0); |
5a314f82 |
244 | |
245 | /* --- Now reap any which have been waiting around so far --- */ |
246 | |
247 | sigemptyset(&ss); |
248 | sigaddset(&ss, SIGCHLD); |
249 | sigprocmask(SIG_BLOCK, &ss, &oss); |
250 | sigchld(SIGCHLD); |
251 | sigprocmask(SIG_SETMASK, &oss, 0); |
203d0643 |
252 | } |
253 | |
90b2c5d4 |
254 | /* --- Now wait for an event --- */ |
255 | |
256 | for (;;) { |
257 | XNextEvent(dpy, &ev); |
258 | switch (ev.type) { |
259 | case PropertyNotify: |
260 | if (ev.xproperty.atom == xwait_die) { |
261 | XTextProperty prop; |
262 | char **sl; |
263 | int c; |
264 | |
265 | if (XGetTextProperty(dpy, ev.xproperty.window, &prop, xwait_die)) { |
266 | XTextPropertyToStringList(&prop, &sl, &c); |
267 | if (strcmp(sl[0], msg) == 0) |
268 | goto exit; |
269 | XFreeStringList(sl); |
270 | } |
271 | } |
272 | } |
273 | } |
274 | |
275 | /* --- Finished: remove the property from all the screens --- */ |
276 | |
277 | exit: |
278 | { |
279 | int i; |
280 | int nsc = ScreenCount(dpy); |
281 | |
282 | for (i = 0; i < nsc; i++) |
283 | XDeleteProperty(dpy, RootWindow(dpy, i), xwait_die); |
284 | } |
285 | |
286 | /* --- Go away --- */ |
287 | |
288 | XCloseDisplay(dpy); |
289 | return (0); |
290 | } |
291 | |
292 | /*----- That's all, folks -------------------------------------------------*/ |