]> icculus.org git repositories - mikachu/openbox.git/blob - src/XAtom.cc
merge in netwm branch at tag netwm-merge2
[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 "../config.h"
23
24 #include "XAtom.hh"
25 #include "blackbox.hh"
26 #include "Screen.hh"
27 #include "Util.hh"
28
29 XAtom::XAtom(Blackbox *bb) {
30   _display = bb->getXDisplay();
31
32   // make sure asserts fire if there is a problem
33   memset(_atoms, sizeof(_atoms), 0);
34
35   _atoms[cardinal] = XA_CARDINAL;
36   _atoms[window] = XA_WINDOW;
37   _atoms[pixmap] = XA_PIXMAP;
38   _atoms[atom] = XA_ATOM;
39   _atoms[string] = XA_STRING;
40   _atoms[utf8_string] = create("UTF8_STRING");
41   
42 #ifdef    HAVE_GETPID
43   _atoms[blackbox_pid] = create("_BLACKBOX_PID");
44 #endif // HAVE_GETPID
45
46   _atoms[wm_colormap_windows] = create("WM_COLORMAP_WINDOWS");
47   _atoms[wm_protocols] = create("WM_PROTOCOLS");
48   _atoms[wm_state] = create("WM_STATE");
49   _atoms[wm_change_state] = create("WM_CHANGE_STATE");
50   _atoms[wm_delete_window] = create("WM_DELETE_WINDOW");
51   _atoms[wm_take_focus] = create("WM_TAKE_FOCUS");
52   _atoms[motif_wm_hints] = create("_MOTIF_WM_HINTS");
53   _atoms[blackbox_hints] = create("_BLACKBOX_HINTS");
54   _atoms[blackbox_attributes] = create("_BLACKBOX_ATTRIBUTES");
55   _atoms[blackbox_change_attributes] = create("_BLACKBOX_CHANGE_ATTRIBUTES");
56   _atoms[blackbox_structure_messages] = create("_BLACKBOX_STRUCTURE_MESSAGES");
57   _atoms[blackbox_notify_startup] = create("_BLACKBOX_NOTIFY_STARTUP");
58   _atoms[blackbox_notify_window_add] = create("_BLACKBOX_NOTIFY_WINDOW_ADD");
59   _atoms[blackbox_notify_window_del] = create("_BLACKBOX_NOTIFY_WINDOW_DEL");
60   _atoms[blackbox_notify_current_workspace] = 
61     create("_BLACKBOX_NOTIFY_CURRENT_WORKSPACE");
62   _atoms[blackbox_notify_workspace_count] =
63     create("_BLACKBOX_NOTIFY_WORKSPACE_COUNT");
64   _atoms[blackbox_notify_window_focus] =
65     create("_BLACKBOX_NOTIFY_WINDOW_FOCUS");
66   _atoms[blackbox_notify_window_raise] =
67     create("_BLACKBOX_NOTIFY_WINDOW_RAISE");
68   _atoms[blackbox_notify_window_lower] =
69     create("_BLACKBOX_NOTIFY_WINDOW_LOWER");
70   
71   _atoms[blackbox_change_workspace] = create("_BLACKBOX_CHANGE_WORKSPACE");
72   _atoms[blackbox_change_window_focus] =
73     create("_BLACKBOX_CHANGE_WINDOW_FOCUS");
74   _atoms[blackbox_cycle_window_focus] = create("_BLACKBOX_CYCLE_WINDOW_FOCUS");
75
76   _atoms[net_supported] = create("_NET_SUPPORTED");
77   _atoms[net_client_list] = create("_NET_CLIENT_LIST");
78   _atoms[net_client_list_stacking] = create("_NET_CLIENT_LIST_STACKING");
79   _atoms[net_number_of_desktops] = create("_NET_NUMBER_OF_DESKTOPS");
80   _atoms[net_desktop_geometry] = create("_NET_DESKTOP_GEOMETRY");
81   _atoms[net_desktop_viewport] = create("_NET_DESKTOP_VIEWPORT");
82   _atoms[net_current_desktop] = create("_NET_CURRENT_DESKTOP");
83   _atoms[net_desktop_names] = create("_NET_DESKTOP_NAMES");
84   _atoms[net_active_window] = create("_NET_ACTIVE_WINDOW");
85   _atoms[net_workarea] = create("_NET_WORKAREA");
86   _atoms[net_supporting_wm_check] = create("_NET_SUPPORTING_WM_CHECK");
87   _atoms[net_virtual_roots] = create("_NET_VIRTUAL_ROOTS");
88
89   _atoms[net_close_window] = create("_NET_CLOSE_WINDOW");
90   _atoms[net_wm_moveresize] = create("_NET_WM_MOVERESIZE");
91
92   _atoms[net_properties] = create("_NET_PROPERTIES");
93   _atoms[net_wm_name] = create("_NET_WM_NAME");
94   _atoms[net_wm_desktop] = create("_NET_WM_DESKTOP");
95   _atoms[net_wm_window_type] = create("_NET_WM_WINDOW_TYPE");
96   _atoms[net_wm_state] = create("_NET_WM_STATE");
97   _atoms[net_wm_strut] = create("_NET_WM_STRUT");
98   _atoms[net_wm_icon_geometry] = create("_NET_WM_ICON_GEOMETRY");
99   _atoms[net_wm_icon] = create("_NET_WM_ICON");
100   _atoms[net_wm_pid] = create("_NET_WM_PID");
101   _atoms[net_wm_handled_icons] = create("_NET_WM_HANDLED_ICONS");
102
103   _atoms[net_wm_ping] = create("_NET_WM_PING");
104 }
105
106
107 /*
108  * clean up the class' members
109  */
110 XAtom::~XAtom() {
111   while (!_support_windows.empty()) {
112     // make sure we aren't fucking with this somewhere
113     assert(_support_windows.back() != None);
114     XDestroyWindow(_display, _support_windows.back());
115     _support_windows.pop_back();
116   }
117 }
118
119
120 /*
121  * Returns an atom from the Xserver, creating it if necessary.
122  */
123 Atom XAtom::create(const char *name) const {
124   return XInternAtom(_display, name, False);
125 }
126
127
128 /*
129  * Sets which atoms are supported for NETWM, by Openbox, on the root window.
130  */
131 void XAtom::setSupported(const ScreenInfo *screen) {
132   Window root = screen->getRootWindow();
133
134   // create the netwm support window
135   Window w = XCreateSimpleWindow(_display, root, 0, 0, 1, 1, 0, 0, 0);
136   assert(w != None);
137   _support_windows.push_back(w);
138   
139   // set supporting window
140   setValue(root, net_supporting_wm_check, window, w);
141  
142   //set properties on the supporting window
143   setValue(w, net_wm_name, utf8, "Openbox");
144   setValue(w, net_supporting_wm_check, window, w);
145   
146   // we don't support any yet..
147   // yes we do!
148
149   Atom supported[] = {
150     _atoms[net_current_desktop],
151     _atoms[net_number_of_desktops]
152   };
153   const int num_supported = sizeof(supported)/sizeof(Atom);
154
155   setValue(root, net_supported, atom, supported, num_supported);
156 }
157   
158
159 /*
160  * Internal setValue.
161  * Sets a window property on a window, optionally appending to the existing
162  * value.
163  */
164 void XAtom::setValue(Window win, Atom atom, Atom type,
165                      unsigned char* data, int size, int nelements,
166                      bool append) const {
167   assert(win != None); assert(atom != None); assert(type != None);
168   assert(data != (unsigned char *) 0);
169   assert(size == 8 || size == 16 || size == 32);
170   assert(nelements > 0);
171   XChangeProperty(_display, win, atom, type, size,
172                   (append ? PropModeAppend : PropModeReplace),
173                   data, nelements);                  
174 }
175
176
177 /*
178  * Set a 32-bit property value on a window.
179  */
180 void XAtom::setValue(Window win, Atoms atom, Atoms type,
181                      unsigned long value) const {
182   assert(atom >= 0 && atom < NUM_ATOMS);
183   assert(type >= 0 && type < NUM_ATOMS);
184   setValue(win, _atoms[atom], _atoms[type],
185            reinterpret_cast<unsigned char*>(&value), 32, 1, false);
186 }
187
188
189 /*
190  * Set an array of 32-bit properties value on a window.
191  */
192 void XAtom::setValue(Window win, Atoms atom, Atoms type,
193                      unsigned long value[], int elements) const {
194   assert(atom >= 0 && atom < NUM_ATOMS);
195   assert(type >= 0 && type < NUM_ATOMS);
196   setValue(win, _atoms[atom], _atoms[type],
197            reinterpret_cast<unsigned char*>(value), 32, elements, false);
198 }
199
200
201 /*
202  * Set an string property value on a window.
203  */
204 void XAtom::setValue(Window win, Atoms atom, StringType type,
205                      const std::string &value) const {
206   assert(atom >= 0 && atom < NUM_ATOMS);
207   assert(type >= 0 && type < NUM_STRING_TYPE);
208   assert(win != None); assert(_atoms[atom] != None);
209
210   const char *c = value.c_str();
211   XTextProperty textprop;
212   if (Success != XmbTextListToTextProperty(_display, const_cast<char**>(&c), 1,
213                                            type == ansi ? XStringStyle :
214 #ifdef X_HAVE_UTF8_STRING
215                                            XUTF8StringStyle,
216 #else
217                                            XCompoundTextStyle,
218 #endif
219                                            &textprop)) {
220     return;
221   }
222   
223   XSetTextProperty(_display, win, &textprop, _atoms[atom]);
224
225   XFree(textprop.value);
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,
237                      unsigned long &nelements, unsigned char **value,
238                      int size) const {
239   assert(win != None); assert(atom != None); assert(type != None);
240   assert(size == 8 || size == 16 || size == 32);
241   unsigned char *c_val;        // value alloc'd with c malloc
242   Atom ret_type;
243   int ret_size;
244   unsigned long ret_bytes;
245   // try get the first element
246   XGetWindowProperty(_display, win, atom, 0l, 1l, False, AnyPropertyType,
247                      &ret_type, &ret_size, &nelements, &ret_bytes, &c_val);
248   if (ret_type == None)
249     // the property does not exist on the window
250     return false;
251   if (ret_type != type || ret_size != size) {
252     // wrong data in property
253     XFree(c_val);
254     return false;
255   }
256   // the data is correct, now, is there more than 1 element?
257   if (ret_bytes == 0) {
258     // we got the whole property's value
259     *value = new unsigned char[nelements * size/8 + 1];
260     memcpy(*value, c_val, nelements * size/8 + 1);
261     XFree(c_val);
262     return true;    
263   }
264   // get the entire property since it is larger than one long
265   free(c_val);
266   // the number of longs that need to be retreived to get the property's entire
267   // value. The last + 1 is the first long that we retrieved above.
268   const int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
269   XGetWindowProperty(_display, win, atom, 0l, remain, False, type, &ret_type,
270                      &ret_size, &nelements, &ret_bytes, &c_val);
271   assert(ret_bytes == 0);
272   *value = new unsigned char[nelements * size/8 + 1];
273   memcpy(*value, c_val, nelements * size/8 + 1);
274   XFree(c_val);
275   return true;    
276 }
277
278
279 /*
280  * Gets a 32-bit property's value from a window.
281  */
282 bool XAtom::getValue(Window win, Atoms atom, Atoms type,
283                          unsigned long &nelements,
284                          unsigned long **value) const {
285   assert(atom >= 0 && atom < NUM_ATOMS);
286   assert(type >= 0 && type < NUM_ATOMS);
287   return getValue(win, _atoms[atom], _atoms[type], nelements,
288                   reinterpret_cast<unsigned char **>(value), 32);
289 }
290
291
292 /*
293  * Gets an string property's value from a window.
294  */
295 bool XAtom::getValue(Window win, Atoms atom, StringType type,
296                      std::string &value) const {
297   assert(atom >= 0 && atom < NUM_ATOMS);
298   assert(type >= 0 && type < NUM_STRING_TYPE);
299   assert(win != None); assert(_atoms[atom] != None);
300
301   XTextProperty textprop;
302   if (0 == XGetTextProperty(_display, win, &textprop, _atoms[atom]))
303     return false;
304
305   int ret;
306   int count;
307   char **list;
308   if (type == ansi) {
309       ret = XmbTextPropertyToTextList(_display, &textprop, &list, &count);
310   } else {
311 #ifdef X_HAVE_UTF8_STRING
312     ret = Xutf8TextPropertyToTextList(_display, &textprop, &list, &count);
313 #else
314     ret = XmbTextPropertyToTextList(_display, &textprop, &list, &count);
315 #endif
316   }
317  
318   if (ret != Success || count < 1) {
319     XFree(textprop.value);
320     return false;
321   }
322
323   value = list[0];
324
325   XFreeStringList(list);
326   XFree(textprop.value);
327   return true;
328 }
329
330
331
332
333 /*
334  * Removes a property entirely from a window.
335  */
336 void XAtom::eraseValue(Window win, Atoms atom) const {
337   assert(atom >= 0 && atom < NUM_ATOMS);
338   XDeleteProperty(_display, win, _atoms[atom]);
339 }