]> icculus.org git repositories - mikachu/openbox.git/blob - src/XAtom.cc
better string get/set
[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 "openbox.h"
24 #include "Screen.h"
25 #include "Util.h"
26
27 XAtom::XAtom(Openbox &ob) {
28   _display = ob.getXDisplay();
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 (unsigned int s = 0, c = ob.managedScreenCount(); s < c; ++s)
90     setSupported( static_cast<ScreenInfo*>(ob.getScreen(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 ScreenInfo *screen) {
119   // create the netwm support window
120   Window w = XCreateSimpleWindow(_display, screen->getRootWindow(),
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,
186                            const std::string &value) const {
187   setValue(win, atom, XA_STRING,
188            const_cast<unsigned char*>
189            (reinterpret_cast<const unsigned char*>(value.c_str())),
190            8, value.size(), false);
191 }
192
193
194 /*
195  * Add elements to a 32-bit CARDINAL property value on a window.
196  */
197 void XAtom::addCardValue(Window win, Atom atom, long value) const {
198   setValue(win, atom, XA_CARDINAL, reinterpret_cast<unsigned char*>(&value),
199            32, 1, true);
200 }
201
202
203 /*
204  * Add elements to an Atom property value on a window.
205  */
206 void XAtom::addAtomValue(Window win, Atom atom, Atom value) const {
207   setValue(win, atom, XA_ATOM, reinterpret_cast<unsigned char*>(&value),
208            32, 1, true);
209 }
210
211
212 /*
213  * Add elements to a Window property value on a window.
214  */
215 void XAtom::addWindowValue(Window win, Atom atom, Window value) const {
216   setValue(win, atom, XA_WINDOW, reinterpret_cast<unsigned char*>(&value),
217            32, 1, true);
218 }
219
220
221 /*
222  * Add elements to a Pixmap property value on a window.
223  */
224 void XAtom::addPixmapValue(Window win, Atom atom, Pixmap value) const {
225   setValue(win, atom, XA_PIXMAP, reinterpret_cast<unsigned char*>(&value),
226            32, 1, true);
227 }
228
229
230 /*
231  * Add characters to a string property value on a window.
232  */
233 void XAtom::addStringValue(Window win, Atom atom,
234                            const std::string &value) const {
235   setValue(win, atom, XA_STRING,
236            const_cast<unsigned char*>
237            (reinterpret_cast<const unsigned char *>
238             (value.c_str())),
239            8, value.size(), true);
240
241
242
243 /*
244  * Internal getValue function used by all of the typed getValue functions.
245  * Gets an property's value from a window.
246  * Returns true if the property was successfully retrieved; false if the
247  * property did not exist on the window, or has a different type/size format
248  * than the user tried to retrieve.
249  */
250 bool XAtom::getValue(Window win, Atom atom, Atom type, unsigned long *nelements,
251                      unsigned char **value, int size) const {
252   unsigned char *c_val;        // value alloc'd with c malloc
253   Atom ret_type;
254   int ret_size;
255   unsigned long ret_bytes;
256   XGetWindowProperty(_display, win, atom, 0l, 1l, False, AnyPropertyType,
257                      &ret_type, &ret_size, nelements, &ret_bytes,
258                      &c_val); // try get the first element
259   if (ret_type == None)
260     // the property does not exist on the window
261     return false;
262   if (ret_type != type || ret_size != size) {
263     // wrong data in property
264     XFree(c_val);
265     return false;
266   }
267   // the data is correct, now, is there more than 1 element?
268   if (ret_bytes == 0) {
269     // we got the whole property's value
270     *value = new unsigned char[*nelements * size/8 + 1];
271     memcpy(*value, c_val, *nelements * size/8 + 1);
272     XFree(c_val);
273     return true;    
274   }
275   // get the entire property since it is larger than one long
276   free(c_val);
277   // the number of longs that need to be retreived to get the property's entire
278   // value. The last + 1 is the first long that we retrieved above.
279   const int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
280   XGetWindowProperty(_display, win, atom, 0l, remain, False, type, &ret_type,
281                      &ret_size, nelements, &ret_bytes, &c_val);
282   ASSERT(ret_bytes == 0);
283   *value = new unsigned char[*nelements * size/8 + 1];
284   memcpy(*value, c_val, *nelements * size/8 + 1);
285   XFree(c_val);
286   return true;    
287 }
288
289
290 /*
291  * Gets a 32-bit Cardinal property's value from a window.
292  */
293 bool XAtom::getCardValue(Window win, Atom atom, unsigned long *nelements,
294                      long **value) const {
295   return XAtom::getValue(win, atom, XA_CARDINAL, nelements,
296                   reinterpret_cast<unsigned char **>(value), 32);
297 }
298
299
300 /*
301  * Gets an Atom property's value from a window.
302  */
303 bool XAtom::getAtomValue(Window win, Atom atom, unsigned long *nelements,
304                      Atom **value) const {
305   return XAtom::getValue(win, atom, XA_ATOM, nelements,
306                   reinterpret_cast<unsigned char **>(value), 32);
307 }
308
309
310 /*
311  * Gets an Window property's value from a window.
312  */
313 bool XAtom::getWindowValue(Window win, Atom atom, unsigned long *nelements,
314                      Window **value) const {
315   return XAtom::getValue(win, atom, XA_WINDOW, nelements,
316                   reinterpret_cast<unsigned char **>(value), 32);
317 }
318
319
320 /*
321  * Gets an Pixmap property's value from a window.
322  */
323 bool XAtom::getPixmapValue(Window win, Atom atom, unsigned long *nelements,
324                      Pixmap **value) const {
325   return XAtom::getValue(win, atom, XA_PIXMAP, nelements,
326                   reinterpret_cast<unsigned char **>(value), 32);
327 }
328
329
330 /*
331  * Gets an string property's value from a window.
332  */
333 bool XAtom::getStringValue(Window win, Atom atom, std::string &value) const {
334   unsigned char *data;
335   unsigned long nelements;
336   bool ret = XAtom::getValue(win, atom, XA_STRING, &nelements, &data, 8);
337   if (ret)
338     value = reinterpret_cast<char*>(data);
339   return ret;
340 }
341
342
343 /*
344  * Removes a property entirely from a window.
345  */
346 void XAtom::eraseValue(Window win, Atom atom) const {
347   XDeleteProperty(_display, win, atom);
348 }