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