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