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