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