chiark / gitweb /
xatom: Overhaul xwait/xtell into a single program with subcommands.
[xtoys] / libxatom.c
diff --git a/libxatom.c b/libxatom.c
new file mode 100644 (file)
index 0000000..3bca18f
--- /dev/null
@@ -0,0 +1,152 @@
+/* -*-c-*-
+ *
+ * Messing with X atom properties
+ *
+ * (c) 2007 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the Edgeware X tools collection.
+ *
+ * X tools is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * X tools is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with X tools; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stddef.h>
+
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+
+#include "libxatom.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @xatom_set@ --- *
+ *
+ * Arguments:  @Display *d@ = pointer to display
+ *             @Window w@ = window to set
+ *             @Atom p@ = property to set
+ *             @Atom a@ = atom property value
+ *
+ * Returns:    ---
+ *
+ * Use:                Sets an atom property on a particular window.
+ */
+
+void xatom_set(Display *d, Window w, Atom p, Atom a)
+{
+  XChangeProperty(d, w, p, XA_ATOM, 32, PropModeReplace,
+                 (unsigned char *)&a, 1);
+  XSync(d, False);
+}
+
+/* --- @xatom_get@ --- *
+ *
+ * Arguments:  @Display *d@ = pointer to display
+ *             @Window w@ = window to set
+ *             @Atom p@ = property to read
+ *
+ * Returns:    Atom which is the value of the property.
+ *
+ * Use:                Reads an atom property from a particular window.  The value
+ *             @None@ is returned if there is no atom value.
+ */
+
+Atom xatom_get(Display *d, Window w, Atom p)
+{
+  Atom type, v;
+  unsigned long n, left;
+  int fmt;
+  unsigned char *buf;
+
+  /* --- Fetch the property value --- */
+
+  if (XGetWindowProperty(d, w, p,      /* Display, window, property */
+                        0, 64,         /* Offset, length (both in words) */
+                        False,         /* Delete after return? */
+                        XA_ATOM,       /* Data format type */
+                        &type, &fmt,   /* Actual type and format */
+                        &n, &left,     /* Amount read, and bytes left */
+                        &buf)          /* Where to put the buffer */
+        != Success ||
+      type != XA_ATOM ||
+      n < 1 || fmt < 32)
+    return (None);
+
+  /* --- OK, get the atom and return --- *
+   *
+   * This assumes that atoms are 32-bit things.  This may not be the case.
+   * That's a right pain, actually.  It looks as if Xlib is trying to do the
+   * right thing, so I'll go with that rather than trying to do anything
+   * clever.  This is actually a bit of a poor interface.
+   */
+
+  v = *(Atom *)buf;
+  XFree(buf);
+  return (v);
+}
+
+/* --- @xatom_delete@ --- *
+ *
+ * Arguments:  @Display *d@ = pointer to display
+ *             @Window w@ = window containing atom
+ *             @Atom p@ = property to delete
+ *
+ * Returns:    ---
+ *
+ * Use:                Removes a property from a window.
+ */
+
+void xatom_delete(Display *d, Window w, Atom p)
+{
+  XDeleteProperty(d, w, p);
+  XSync(d, False);
+}
+
+/* --- @xatom_wait@ --- *
+ *
+ * Arguments:  @Display *d@ = pointer to display
+ *             @Window w@ = window to watch
+ *             @Atom p@ = property to fetch
+ *             @const Atom *aa@ = pointer to vector of atoms
+ *             @size_t n@ = numer of atoms in vector
+ *
+ * Returns:    The matching atom.
+ *
+ * Use:                Waits for the given property on the window to match one of
+ *             the @aa[i]@.
+ */
+
+Atom xatom_wait(Display *d, Window w, Atom p, const Atom *aa, size_t n)
+{
+  Atom a;
+  size_t i;
+  XEvent event;
+
+  XSelectInput(d, w, PropertyChangeMask);
+  for (;;) {
+    a = xatom_get(d, w, p);
+    if (a != None) {
+      if (!n) return (a);
+      for (i = 0; i < n; i++)
+       if (a == aa[i]) return (a);
+    }
+    do XNextEvent(d, &event); while (event.type != PropertyNotify);
+  }
+}
+
+/*----- That's all, folks -------------------------------------------------*/