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