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