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