]> icculus.org git repositories - mikachu/openbox.git/blob - src/XAtom.cc
new versions of the X classes
[mikachu/openbox.git] / src / XAtom.cc
1 // XAtom.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Jansens (xor at orodu.net)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22 #include "XAtom.h"
23 #include "XDisplay.h"
24 #include "XScreen.h"
25 #include "Util.h"
26
27 XAtom::XAtom(const XDisplay *display) {
28   _display = display->_display;
29
30 #ifdef    HAVE_GETPID
31   openbox_pid = getAtom("_BLACKBOX_PID");
32 #endif // HAVE_GETPID
33
34   wm_colormap_windows = getAtom("WM_COLORMAP_WINDOWS");
35   wm_protocols = getAtom("WM_PROTOCOLS");
36   wm_state = getAtom("WM_STATE");
37   wm_change_state = getAtom("WM_CHANGE_STATE");
38   wm_delete_window = getAtom("WM_DELETE_WINDOW");
39   wm_take_focus = getAtom("WM_TAKE_FOCUS");
40   motif_wm_hints = getAtom("_MOTIF_WM_HINTS");
41   openbox_hints = getAtom("_BLACKBOX_HINTS");
42   openbox_attributes = getAtom("_BLACKBOX_ATTRIBUTES");
43   openbox_change_attributes = getAtom("_BLACKBOX_CHANGE_ATTRIBUTES");
44
45   openbox_structure_messages = getAtom("_BLACKBOX_STRUCTURE_MESSAGES");
46   openbox_notify_startup = getAtom("_BLACKBOX_NOTIFY_STARTUP");
47   openbox_notify_window_add = getAtom("_BLACKBOX_NOTIFY_WINDOW_ADD");
48   openbox_notify_window_del = getAtom("_BLACKBOX_NOTIFY_WINDOW_DEL");
49   openbox_notify_current_workspace =
50     getAtom("_BLACKBOX_NOTIFY_CURRENT_WORKSPACE");
51   openbox_notify_workspace_count = getAtom("_BLACKBOX_NOTIFY_WORKSPACE_COUNT");
52   openbox_notify_window_focus = getAtom("_BLACKBOX_NOTIFY_WINDOW_FOCUS");
53   openbox_notify_window_raise = getAtom("_BLACKBOX_NOTIFY_WINDOW_RAISE");
54   openbox_notify_window_lower = getAtom("_BLACKBOX_NOTIFY_WINDOW_LOWER");
55   
56   openbox_change_workspace = getAtom("_BLACKBOX_CHANGE_WORKSPACE");
57   openbox_change_window_focus = getAtom("_BLACKBOX_CHANGE_WINDOW_FOCUS");
58   openbox_cycle_window_focus = getAtom("_BLACKBOX_CYCLE_WINDOW_FOCUS");
59
60   net_supported = getAtom("_NET_SUPPORTED");
61   net_client_list = getAtom("_NET_CLIENT_LIST");
62   net_client_list_stacking = getAtom("_NET_CLIENT_LIST_STACKING");
63   net_number_of_desktops = getAtom("_NET_NUMBER_OF_DESKTOPS");
64   net_desktop_geometry = getAtom("_NET_DESKTOP_GEOMETRY");
65   net_desktop_viewport = getAtom("_NET_DESKTOP_VIEWPORT");
66   net_current_desktop = getAtom("_NET_CURRENT_DESKTOP");
67   net_desktop_names = getAtom("_NET_DESKTOP_NAMES");
68   net_active_window = getAtom("_NET_ACTIVE_WINDOW");
69   net_workarea = getAtom("_NET_WORKAREA");
70   net_supporting_wm_check = getAtom("_NET_SUPPORTING_WM_CHECK");
71   net_virtual_roots = getAtom("_NET_VIRTUAL_ROOTS");
72
73   net_close_window = getAtom("_NET_CLOSE_WINDOW");
74   net_wm_moveresize = getAtom("_NET_WM_MOVERESIZE");
75
76   net_properties = getAtom("_NET_PROPERTIES");
77   net_wm_name = getAtom("_NET_WM_NAME");
78   net_wm_desktop = getAtom("_NET_WM_DESKTOP");
79   net_wm_window_type = getAtom("_NET_WM_WINDOW_TYPE");
80   net_wm_state = getAtom("_NET_WM_STATE");
81   net_wm_strut = getAtom("_NET_WM_STRUT");
82   net_wm_icon_geometry = getAtom("_NET_WM_ICON_GEOMETRY");
83   net_wm_icon = getAtom("_NET_WM_ICON");
84   net_wm_pid = getAtom("_NET_WM_PID");
85   net_wm_handled_icons = getAtom("_NET_WM_HANDLED_ICONS");
86
87   net_wm_ping = getAtom("_NET_WM_PING");
88
89   for (int s = 0, c = display->screenCount(); s < c; ++s)
90     setSupported(display->screen(s));
91 }
92
93
94 /*
95  * clean up the class' members
96  */
97 XAtom::~XAtom() {
98   while (!_support_windows.empty()) {
99     // make sure we aren't fucking with this somewhere
100     ASSERT(_support_windows.back() != None);
101     XDestroyWindow(_display, _support_windows.back());
102     _support_windows.pop_back();
103   }
104 }
105
106
107 /*
108  * Returns an atom from the Xserver, creating it if necessary.
109  */
110 Atom XAtom::getAtom(const char *name) const {
111   return XInternAtom(_display, name, False);
112 }
113
114
115 /*
116  * Sets which atoms are supported for NETWM, by Openbox, on the root window.
117  */
118 void XAtom::setSupported(const XScreen *screen) {
119   // create the netwm support window
120   Window w = XCreateSimpleWindow(_display, screen->rootWindow(),
121                                  0, 0, 1, 1, 0, 0, 0);
122   ASSERT(w != None);
123   _support_windows.push_back(w);
124   
125   // we don't support any yet..
126 }
127   
128
129 /*
130  * Internal setValue used by all typed setValue functions.
131  * Sets a window property on a window, optionally appending to the existing
132  * value.
133  */
134 void XAtom::setValue(Window win, Atom atom, Atom type, unsigned char* data,
135                      int size, int nelements, bool append) const {
136   ASSERT(win != None); ASSERT(atom != None); ASSERT(type != None);
137   ASSERT(data != (unsigned char *) 0);
138   ASSERT(size == 8 || size == 16 || size == 32);
139   ASSERT(nelements > 0);
140   XChangeProperty(_display, win, atom, type, size,
141                   (append ? PropModeAppend : PropModeReplace),
142                   data, nelements);                  
143 }
144
145
146 /*
147  * Set a 32-bit CARDINAL property value on a window.
148  */
149 void XAtom::setCardValue(Window win, Atom atom, long value) const {
150   setValue(win, atom, XA_CARDINAL, reinterpret_cast<unsigned char*>(&value),
151            32, 1, false);
152 }
153
154
155 /*
156  * Set an Atom property value on a window.
157  */
158 void XAtom::setAtomValue(Window win, Atom atom, Atom value) const {
159   setValue(win, atom, XA_ATOM, reinterpret_cast<unsigned char*>(&value),
160            32, 1, false);
161 }
162
163
164 /*
165  * Set a Window property value on a window.
166  */
167 void XAtom::setWindowValue(Window win, Atom atom, Window value) const {
168   setValue(win, atom, XA_WINDOW, reinterpret_cast<unsigned char*>(&value),
169            32, 1, false);
170 }
171
172
173 /*
174  * Set a Pixmap property value on a window.
175  */
176 void XAtom::setPixmapValue(Window win, Atom atom, Pixmap value) const {
177   setValue(win, atom, XA_PIXMAP, reinterpret_cast<unsigned char*>(&value),
178            32, 1, false);
179 }
180
181
182 /*
183  * Set a string property value on a window.
184  */
185 void XAtom::setStringValue(Window win, Atom atom, std::string &value) const {
186   setValue(win, atom, XA_STRING,
187            const_cast<unsigned char*>
188            (reinterpret_cast<const unsigned char*>(value.c_str())),
189            8, value.size(), false);
190 }
191
192
193 /*
194  * Add elements to a 32-bit CARDINAL property value on a window.
195  */
196 void XAtom::addCardValue(Window win, Atom atom, long value) const {
197   setValue(win, atom, XA_CARDINAL, reinterpret_cast<unsigned char*>(&value),
198            32, 1, true);
199 }
200
201
202 /*
203  * Add elements to an Atom property value on a window.
204  */
205 void XAtom::addAtomValue(Window win, Atom atom, Atom value) const {
206   setValue(win, atom, XA_ATOM, reinterpret_cast<unsigned char*>(&value),
207            32, 1, true);
208 }
209
210
211 /*
212  * Add elements to a Window property value on a window.
213  */
214 void XAtom::addWindowValue(Window win, Atom atom, Window value) const {
215   setValue(win, atom, XA_WINDOW, reinterpret_cast<unsigned char*>(&value),
216            32, 1, true);
217 }
218
219
220 /*
221  * Add elements to a Pixmap property value on a window.
222  */
223 void XAtom::addPixmapValue(Window win, Atom atom, Pixmap value) const {
224   setValue(win, atom, XA_PIXMAP, reinterpret_cast<unsigned char*>(&value),
225            32, 1, true);
226 }
227
228
229 /*
230  * Add characters to a string property value on a window.
231  */
232 void XAtom::addStringValue(Window win, Atom atom, std::string &value) const {
233   setValue(win, atom, XA_STRING,
234            const_cast<unsigned char*>
235            (reinterpret_cast<const unsigned char *>
236             (value.c_str())),
237            8, value.size(), true);
238
239
240
241 /*
242  * Internal getValue function used by all of the typed getValue functions.
243  * Gets an property's value from a window.
244  * Returns true if the property was successfully retrieved; false if the
245  * property did not exist on the window, or has a different type/size format
246  * than the user tried to retrieve.
247  */
248 bool XAtom::getValue(Window win, Atom atom, Atom type, unsigned long *nelements,
249                      unsigned char **value, int size) const {
250   unsigned char *c_val;        // value alloc'd with c malloc
251   Atom ret_type;
252   int ret_size;
253   unsigned long ret_bytes;
254   XGetWindowProperty(_display, win, atom, 0l, 1l, False, AnyPropertyType,
255                      &ret_type, &ret_size, nelements, &ret_bytes,
256                      &c_val); // try get the first element
257   if (ret_type == None)
258     // the property does not exist on the window
259     return false;
260   if (ret_type != type || ret_size != size) {
261     // wrong data in property
262     XFree(c_val);
263     return false;
264   }
265   // the data is correct, now, is there more than 1 element?
266   if (ret_bytes == 0) {
267     // we got the whole property's value
268     *value = new unsigned char[*nelements * size/8 + 1];
269     memcpy(*value, c_val, *nelements * size/8 + 1);
270     XFree(c_val);
271     return true;    
272   }
273   // get the entire property since it is larger than one long
274   free(c_val);
275   // the number of longs that need to be retreived to get the property's entire
276   // value. The last + 1 is the first long that we retrieved above.
277   const int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
278   XGetWindowProperty(_display, win, atom, 0l, remain, False, type, &ret_type,
279                      &ret_size, nelements, &ret_bytes, &c_val);
280   ASSERT(ret_bytes == 0);
281   *value = new unsigned char[*nelements * size/8 + 1];
282   memcpy(*value, c_val, *nelements * size/8 + 1);
283   XFree(c_val);
284   return true;    
285 }
286
287
288 /*
289  * Gets a 32-bit Cardinal property's value from a window.
290  */
291 bool XAtom::getCardValue(Window win, Atom atom, unsigned long *nelements,
292                      long **value) const {
293   return XAtom::getValue(win, atom, XA_CARDINAL, nelements,
294                   reinterpret_cast<unsigned char **>(value), 32);
295 }
296
297
298 /*
299  * Gets an Atom property's value from a window.
300  */
301 bool XAtom::getAtomValue(Window win, Atom atom, unsigned long *nelements,
302                      Atom **value) const {
303   return XAtom::getValue(win, atom, XA_ATOM, nelements,
304                   reinterpret_cast<unsigned char **>(value), 32);
305 }
306
307
308 /*
309  * Gets an Window property's value from a window.
310  */
311 bool XAtom::getWindowValue(Window win, Atom atom, unsigned long *nelements,
312                      Window **value) const {
313   return XAtom::getValue(win, atom, XA_WINDOW, nelements,
314                   reinterpret_cast<unsigned char **>(value), 32);
315 }
316
317
318 /*
319  * Gets an Pixmap property's value from a window.
320  */
321 bool XAtom::getPixmapValue(Window win, Atom atom, unsigned long *nelements,
322                      Pixmap **value) const {
323   return XAtom::getValue(win, atom, XA_PIXMAP, nelements,
324                   reinterpret_cast<unsigned char **>(value), 32);
325 }
326
327
328 /*
329  * Gets an string property's value from a window.
330  */
331 bool XAtom::getStringValue(Window win, Atom atom, unsigned long *nelements,
332                      std::string &value) const {
333   unsigned char *data;
334   bool ret = XAtom::getValue(win, atom, XA_STRING, nelements, &data, 8);
335   if (ret)
336     value = reinterpret_cast<char*>(data);
337   return ret;
338 }
339
340
341 /*
342  * Removes a property entirely from a window.
343  */
344 void XAtom::eraseValue(Window win, Atom atom) const {
345   XDeleteProperty(_display, win, atom);
346 }