| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Messing with X atom properties |
| 4 | * |
| 5 | * (c) 2007 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the Edgeware X tools collection. |
| 11 | * |
| 12 | * X tools is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * X tools is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with X tools; if not, write to the Free Software Foundation, |
| 24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | */ |
| 26 | |
| 27 | /*----- Header files ------------------------------------------------------*/ |
| 28 | |
| 29 | #include <stddef.h> |
| 30 | |
| 31 | #include <X11/Xlib.h> |
| 32 | #include <X11/Xatom.h> |
| 33 | |
| 34 | #include "libxatom.h" |
| 35 | |
| 36 | /*----- Main code ---------------------------------------------------------*/ |
| 37 | |
| 38 | /* --- @xatom_set@ --- * |
| 39 | * |
| 40 | * Arguments: @Display *d@ = pointer to display |
| 41 | * @Window w@ = window to set |
| 42 | * @Atom p@ = property to set |
| 43 | * @Atom a@ = atom property value |
| 44 | * |
| 45 | * Returns: --- |
| 46 | * |
| 47 | * Use: Sets an atom property on a particular window. |
| 48 | */ |
| 49 | |
| 50 | void xatom_set(Display *d, Window w, Atom p, Atom a) |
| 51 | { |
| 52 | XChangeProperty(d, w, p, XA_ATOM, 32, PropModeReplace, |
| 53 | (unsigned char *)&a, 1); |
| 54 | XSync(d, False); |
| 55 | } |
| 56 | |
| 57 | /* --- @xatom_get@ --- * |
| 58 | * |
| 59 | * Arguments: @Display *d@ = pointer to display |
| 60 | * @Window w@ = window to set |
| 61 | * @Atom p@ = property to read |
| 62 | * |
| 63 | * Returns: Atom which is the value of the property. |
| 64 | * |
| 65 | * Use: Reads an atom property from a particular window. The value |
| 66 | * @None@ is returned if there is no atom value. |
| 67 | */ |
| 68 | |
| 69 | Atom xatom_get(Display *d, Window w, Atom p) |
| 70 | { |
| 71 | Atom type, v; |
| 72 | unsigned long n, left; |
| 73 | int fmt; |
| 74 | unsigned char *buf; |
| 75 | |
| 76 | /* --- Fetch the property value --- */ |
| 77 | |
| 78 | if (XGetWindowProperty(d, w, p, /* Display, window, property */ |
| 79 | 0, 64, /* Offset, length (both in words) */ |
| 80 | False, /* Delete after return? */ |
| 81 | XA_ATOM, /* Data format type */ |
| 82 | &type, &fmt, /* Actual type and format */ |
| 83 | &n, &left, /* Amount read, and bytes left */ |
| 84 | &buf) /* Where to put the buffer */ |
| 85 | != Success || |
| 86 | type != XA_ATOM || |
| 87 | n < 1 || fmt < 32) |
| 88 | return (None); |
| 89 | |
| 90 | /* --- OK, get the atom and return --- * |
| 91 | * |
| 92 | * This assumes that atoms are 32-bit things. This may not be the case. |
| 93 | * That's a right pain, actually. It looks as if Xlib is trying to do the |
| 94 | * right thing, so I'll go with that rather than trying to do anything |
| 95 | * clever. This is actually a bit of a poor interface. |
| 96 | */ |
| 97 | |
| 98 | v = *(Atom *)buf; |
| 99 | XFree(buf); |
| 100 | return (v); |
| 101 | } |
| 102 | |
| 103 | /* --- @xatom_delete@ --- * |
| 104 | * |
| 105 | * Arguments: @Display *d@ = pointer to display |
| 106 | * @Window w@ = window containing atom |
| 107 | * @Atom p@ = property to delete |
| 108 | * |
| 109 | * Returns: --- |
| 110 | * |
| 111 | * Use: Removes a property from a window. |
| 112 | */ |
| 113 | |
| 114 | void xatom_delete(Display *d, Window w, Atom p) |
| 115 | { |
| 116 | XDeleteProperty(d, w, p); |
| 117 | XSync(d, False); |
| 118 | } |
| 119 | |
| 120 | /* --- @xatom_wait@ --- * |
| 121 | * |
| 122 | * Arguments: @Display *d@ = pointer to display |
| 123 | * @Window w@ = window to watch |
| 124 | * @Atom p@ = property to fetch |
| 125 | * @const Atom *aa@ = pointer to vector of atoms |
| 126 | * @size_t n@ = numer of atoms in vector |
| 127 | * |
| 128 | * Returns: The matching atom. |
| 129 | * |
| 130 | * Use: Waits for the given property on the window to match one of |
| 131 | * the @aa[i]@. |
| 132 | */ |
| 133 | |
| 134 | Atom xatom_wait(Display *d, Window w, Atom p, const Atom *aa, size_t n) |
| 135 | { |
| 136 | Atom a; |
| 137 | size_t i; |
| 138 | XEvent event; |
| 139 | |
| 140 | XSelectInput(d, w, PropertyChangeMask); |
| 141 | for (;;) { |
| 142 | a = xatom_get(d, w, p); |
| 143 | if (a != None) { |
| 144 | if (!n) return (a); |
| 145 | for (i = 0; i < n; i++) |
| 146 | if (a == aa[i]) return (a); |
| 147 | } |
| 148 | do XNextEvent(d, &event); while (event.type != PropertyNotify); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /*----- That's all, folks -------------------------------------------------*/ |