/* -*-c-*-
*
- * $Id: xwait.c,v 1.1 1998/11/16 23:00:49 mdw Exp $
+ * $Id: xwait.c,v 1.10 2004/04/08 01:36:29 mdw Exp $
*
* Wait until prodded by another X client
*
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: xwait.c,v $
- * Revision 1.1 1998/11/16 23:00:49 mdw
- * Initial versions.
- *
- */
-
/*----- Header files ------------------------------------------------------*/
+#include <errno.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/wait.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
+#include <mLib/mdwopt.h>
+#include <mLib/quis.h>
+#include <mLib/report.h>
+#include <mLib/sub.h>
+
+#include "xatom.h"
#include "xwait.h"
+/*----- Data structures ---------------------------------------------------*/
+
+typedef struct xwait_msg {
+ struct xwait_msg *next; /* Next message in the list */
+ Atom a; /* The message atom */
+} xwait_msg;
+
+typedef struct xwait_atom {
+ struct xwait_atom *next; /* Next atom in the list */
+ Atom a; /* The actual atom */
+ xwait_msg *m; /* List of interesting messages */
+} xwait_atom;
+
+/*----- Static variables --------------------------------------------------*/
+
+static xwait_atom *atoms = 0;
+static Display *dpy;
+
/*----- Main code ---------------------------------------------------------*/
+/* --- @sigchld@ --- */
+
+static void sigchld(int sig)
+{
+ int e = errno;
+ while (waitpid(-1, 0, WNOHANG) > 0)
+ ;
+ errno = e;
+}
+
+/* --- @opendisplay@ --- */
+
+static void opendisplay(const char *d)
+{
+ if (dpy)
+ return;
+ if ((dpy = XOpenDisplay(d)) == 0)
+ die(EXIT_FAILURE, "couldn't open display");
+}
+
+/* --- @addatom@ --- */
+
+static xwait_atom *addatom(const char *a)
+{
+ xwait_atom *xa = CREATE(xwait_atom);
+ opendisplay(0);
+ xa->next = atoms;
+ xa->a = XInternAtom(dpy, a, False);
+ xa->m = 0;
+ atoms = xa;
+ return (xa);
+}
+
+/* --- @addmsg@ --- */
+
+static void addmsg(xwait_atom *xa, const char *a)
+{
+ xwait_msg *xm = CREATE(xwait_msg);
+ opendisplay(0);
+ xm->next = xa->m;
+ xm->a = XInternAtom(dpy, a, False);
+ xa->m = xm;
+}
+
+/* --- @tidy@ --- */
+
+static void tidy(int sig)
+{
+ int i;
+ int nsc = ScreenCount(dpy);
+ xwait_atom *xa;
+
+ for (i = 0; i < nsc; i++) {
+ for (xa = atoms; xa; xa = xa->next)
+ XDeleteProperty(dpy, RootWindow(dpy, i), xa->a);
+ }
+ XCloseDisplay(dpy);
+ exit(0);
+}
+
+/* --- @main@ --- */
+
+static void version(FILE *fp)
+{
+ fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
+}
+
+static void usage(FILE *fp)
+{
+ fprintf(fp, "Usage: %s [-f] [-d DISPLAY] [ATOM:MSG,MSG]...\n", QUIS);
+}
+
int main(int argc, char *argv[])
{
- char *display = 0;
- Display *dpy;
- Atom xwait_die;
+ Atom a;
XEvent ev;
- char *atom = XWAIT_DIE;
- char *msg = XWAIT_DIE_MSG;
unsigned f = 0;
+ xwait_atom *xa = 0;
+ xwait_msg *xm = 0;
+
+#define f_force 1u
- enum {
- f_force = 1
- };
+ /* --- Initialize mLib --- */
+
+ ego(argv[0]);
+ sub_init();
/* --- Parse options --- */
for (;;) {
- int i = getopt(argc, argv, "d:a:m:f");
+ static struct option opt[] = {
+ { "help", 0, 0, 'h' },
+ { "usage", 0, 0, 'u' },
+ { "version", 0, 0, 'v' },
+ { "display", OPTF_ARGREQ, 0, 'd' },
+ { "atom", OPTF_ARGREQ, 0, 'a' },
+ { "msg", OPTF_ARGREQ, 0, 'm' },
+ { "force", 0, 0, 'f' },
+ { 0, 0, 0, 0 }
+ };
+
+ int i = mdwopt(argc, argv, "huvd:a:m:f", opt, 0, 0, 0);
if (i < 0)
break;
switch (i) {
+ case 'h':
+ version(stdout);
+ fputs("\n", stdout);
+ usage(stdout);
+ fputs(
+"\n"
+"Waits until signalled by `xtell' or `xshutdown'. Specifically, waits\n"
+"until a property with name ATOM is written to the root window with\n"
+"contents MSG.\n"
+"\n"
+"Options:\n"
+"\n"
+"-h, --help Display this help text\n"
+"-u, --usage Display a short usage summary\n"
+"-v, --version Display the program's version number\n"
+"\n"
+"-d, --display=DISPLAY Choose X display to connect to\n"
+"-f, --force Run even if this property is waited for by another\n"
+" process\n"
+"-a, --atom=ATOM\t Choose property name to listen for [deprecated]\n"
+"-m, --msg=MSG Choose value of property to wait for [deprecated]\n",
+ stdout);
+ exit(0);
+ break;
+ case 'u':
+ usage(stdout);
+ exit(0);
+ break;
+ case 'v':
+ version(stdout);
+ exit(0);
+ break;
+
case 'd':
- display = optarg;
+ opendisplay(optarg);
break;
case 'a':
- atom = optarg;
+ xa = addatom(optarg);
break;
case 'm':
- msg = optarg;
+ if (!xa)
+ die(EXIT_FAILURE, "no atom currently defined");
+ addmsg(xa, optarg);
break;
case 'f':
f |= f_force;
break;
default:
- fprintf(stderr,
- "Usage: xwait [-f] [-d DISPLAY] [-a ATOM] [-m MSG]\n");
+ usage(stderr);
exit(EXIT_FAILURE);
break;
}
}
- /* --- Connect to the X display --- */
+ /* --- Grind through remaining arguments in the new syntax --- */
- dpy = XOpenDisplay(display);
- if (!dpy) {
- fprintf(stderr, "xwait: couldn't open display\n");
- exit(EXIT_FAILURE);
+ while (optind < argc) {
+ char *p = strtok(argv[optind++], ":");
+ if (!p)
+ continue;
+ xa = addatom(p);
+ while ((p = strtok(0, ",")) != 0)
+ addmsg(xa, p);
}
- /* --- Fetch the property name atom --- */
+ /* --- If there's nothing set, put in some defaults --- */
- xwait_die = XInternAtom(dpy, atom, False);
+ if (!atoms) {
+ xa = addatom(XWAIT_DIE);
+ addmsg(xa, XWAIT_DIE_MSG);
+ }
- /* --- Mark ourselves as listening to all the screens --- */
+ /* --- Attach the atoms to the screens --- */
{
int i;
+ Atom ready = XInternAtom(dpy, "XWAIT_READY", False);
int nsc = ScreenCount(dpy);
/* --- First pass: make sure there's not a process already here --- */
if ((f & f_force) == 0) {
for (i = 0; i < nsc; i++) {
- Window win = RootWindow(dpy, i);
- XTextProperty prop;
-
- if (XGetTextProperty(dpy, win, &prop, xwait_die)) {
- fprintf(stderr, "xwait: already waiting for `%s'\n", atom);
- exit(EXIT_FAILURE);
+ for (xa = atoms; xa; xa = xa->next) {
+ if (xatom_get(dpy, RootWindow(dpy, i), xa->a) != None)
+ die(EXIT_FAILURE, "already waiting for `%s'");
}
}
}
/* --- Second pass: set up listening to the property --- */
for (i = 0; i < nsc; i++) {
- Window win = RootWindow(dpy, i);
- XTextProperty prop;
- char *imsg = "XWAIT_READY";
-
- XStringListToTextProperty(&imsg, 1, &prop);
- XSetTextProperty(dpy, win, &prop, xwait_die);
- XSelectInput(dpy, win, PropertyChangeMask);
+ for (xa = atoms; xa; xa = xa->next)
+ xatom_set(dpy, RootWindow(dpy, i), xa->a, ready);
+ XSelectInput(dpy, RootWindow(dpy, i), PropertyChangeMask);
}
}
+ /* --- Set up a handler when children die --- *
+ *
+ * I don't fork any children? Why is this useful? Because I've been
+ * execed from a shell which started lots of background processes, and
+ * they'll zombie themselves otherwise.
+ */
+
+ {
+ struct sigaction sa;
+
+ sa.sa_handler = sigchld;
+ sigemptyset(&sa.sa_mask);
+ sigaddset(&sa.sa_mask, SIGCHLD);
+ sa.sa_flags = SA_NOCLDSTOP;
+#ifdef SA_RESTART
+ sa.sa_flags |= SA_RESTART;
+#endif
+ sigaction(SIGCHLD, &sa, 0);
+ }
+
+ /* --- Now reap any which have been waiting around so far --- */
+
+ {
+ sigset_t ss, oss;
+
+ sigemptyset(&ss);
+ sigaddset(&ss, SIGCHLD);
+ sigprocmask(SIG_BLOCK, &ss, &oss);
+ sigchld(SIGCHLD);
+ sigprocmask(SIG_SETMASK, &oss, 0);
+ }
+
+ signal(SIGINT, tidy);
+ signal(SIGTERM, tidy);
+
/* --- Now wait for an event --- */
for (;;) {
XNextEvent(dpy, &ev);
switch (ev.type) {
case PropertyNotify:
- if (ev.xproperty.atom == xwait_die) {
- XTextProperty prop;
- char **sl;
- int c;
-
- if (XGetTextProperty(dpy, ev.xproperty.window, &prop, xwait_die)) {
- XTextPropertyToStringList(&prop, &sl, &c);
- if (strcmp(sl[0], msg) == 0)
+ for (xa = atoms; xa; xa = xa->next) {
+ if (ev.xproperty.atom != xa->a)
+ continue;
+ a = xatom_get(dpy, ev.xproperty.window, xa->a);
+ if (a == None)
+ continue;
+ if (xa->m == 0)
+ goto exit;
+ for (xm = xa->m; xm; xm = xm->next) {
+ if (a == xm->a)
goto exit;
- XFreeStringList(sl);
}
}
+ break;
}
}
- /* --- Finished: remove the property from all the screens --- */
+ /* --- Finished: report the result if necessary --- */
exit:
- {
- int i;
- int nsc = ScreenCount(dpy);
-
- for (i = 0; i < nsc; i++)
- XDeleteProperty(dpy, RootWindow(dpy, i), xwait_die);
- }
+ if (atoms->next) {
+ fputs(XGetAtomName(dpy, xa->a), stdout);
+ if (!xa->m || xa->m->next)
+ printf(": %s\n", XGetAtomName(dpy, a));
+ else
+ fputc('\n', stdout);
+ } else if (!xa->m || xa->m->next)
+ printf("%s\n", XGetAtomName(dpy, a));
/* --- Go away --- */
- XCloseDisplay(dpy);
+ tidy(0);
return (0);
}