]> icculus.org git repositories - mikachu/openbox.git/blob - src/XAtom.cc
add another return, and return a value for a non-void funtion.
[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 <assert.h>
25
26 #include "XAtom.hh"
27 #include "Screen.hh"
28 #include "Util.hh"
29
30 XAtom::XAtom(Display *d) {
31   _display = d;
32
33   // make sure asserts fire if there is a problem
34   memset(_atoms, 0, sizeof(_atoms));
35
36   _atoms[cardinal] = XA_CARDINAL;
37   _atoms[window] = XA_WINDOW;
38   _atoms[pixmap] = XA_PIXMAP;
39   _atoms[atom] = XA_ATOM;
40   _atoms[string] = XA_STRING;
41   _atoms[utf8_string] = create("UTF8_STRING");
42   
43 #ifdef    HAVE_GETPID
44   _atoms[blackbox_pid] = create("_BLACKBOX_PID");
45 #endif // HAVE_GETPID
46
47   _atoms[wm_colormap_windows] = create("WM_COLORMAP_WINDOWS");
48   _atoms[wm_protocols] = create("WM_PROTOCOLS");
49   _atoms[wm_state] = create("WM_STATE");
50   _atoms[wm_change_state] = create("WM_CHANGE_STATE");
51   _atoms[wm_delete_window] = create("WM_DELETE_WINDOW");
52   _atoms[wm_take_focus] = create("WM_TAKE_FOCUS");
53   _atoms[wm_name] = create("WM_NAME");
54   _atoms[wm_icon_name] = create("WM_ICON_NAME");
55   _atoms[motif_wm_hints] = create("_MOTIF_WM_HINTS");
56   _atoms[blackbox_hints] = create("_BLACKBOX_HINTS");
57   _atoms[blackbox_attributes] = create("_BLACKBOX_ATTRIBUTES");
58   _atoms[blackbox_change_attributes] = create("_BLACKBOX_CHANGE_ATTRIBUTES");
59   _atoms[blackbox_structure_messages] = create("_BLACKBOX_STRUCTURE_MESSAGES");
60   _atoms[blackbox_notify_startup] = create("_BLACKBOX_NOTIFY_STARTUP");
61   _atoms[blackbox_notify_window_add] = create("_BLACKBOX_NOTIFY_WINDOW_ADD");
62   _atoms[blackbox_notify_window_del] = create("_BLACKBOX_NOTIFY_WINDOW_DEL");
63   _atoms[blackbox_notify_current_workspace] = 
64     create("_BLACKBOX_NOTIFY_CURRENT_WORKSPACE");
65   _atoms[blackbox_notify_workspace_count] =
66     create("_BLACKBOX_NOTIFY_WORKSPACE_COUNT");
67   _atoms[blackbox_notify_window_focus] =
68     create("_BLACKBOX_NOTIFY_WINDOW_FOCUS");
69   _atoms[blackbox_notify_window_raise] =
70     create("_BLACKBOX_NOTIFY_WINDOW_RAISE");
71   _atoms[blackbox_notify_window_lower] =
72     create("_BLACKBOX_NOTIFY_WINDOW_LOWER");
73   
74   _atoms[blackbox_change_workspace] = create("_BLACKBOX_CHANGE_WORKSPACE");
75   _atoms[blackbox_change_window_focus] =
76     create("_BLACKBOX_CHANGE_WINDOW_FOCUS");
77   _atoms[blackbox_cycle_window_focus] = create("_BLACKBOX_CYCLE_WINDOW_FOCUS");
78
79   _atoms[net_supported] = create("_NET_SUPPORTED");
80   _atoms[net_client_list] = create("_NET_CLIENT_LIST");
81   _atoms[net_client_list_stacking] = create("_NET_CLIENT_LIST_STACKING");
82   _atoms[net_number_of_desktops] = create("_NET_NUMBER_OF_DESKTOPS");
83   _atoms[net_desktop_geometry] = create("_NET_DESKTOP_GEOMETRY");
84   _atoms[net_desktop_viewport] = create("_NET_DESKTOP_VIEWPORT");
85   _atoms[net_current_desktop] = create("_NET_CURRENT_DESKTOP");
86   _atoms[net_desktop_names] = create("_NET_DESKTOP_NAMES");
87   _atoms[net_active_window] = create("_NET_ACTIVE_WINDOW");
88   _atoms[net_workarea] = create("_NET_WORKAREA");
89   _atoms[net_supporting_wm_check] = create("_NET_SUPPORTING_WM_CHECK");
90 //  _atoms[net_virtual_roots] = create("_NET_VIRTUAL_ROOTS");
91
92   _atoms[net_close_window] = create("_NET_CLOSE_WINDOW");
93   _atoms[net_wm_moveresize] = create("_NET_WM_MOVERESIZE");
94
95 //  _atoms[net_properties] = create("_NET_PROPERTIES");
96   _atoms[net_wm_name] = create("_NET_WM_NAME");
97   _atoms[net_wm_visible_name] = create("_NET_WM_VISIBLE_NAME");
98   _atoms[net_wm_icon_name] = create("_NET_WM_ICON_NAME");
99   _atoms[net_wm_visible_icon_name] = create("_NET_WM_VISIBLE_ICON_NAME");
100   _atoms[net_wm_desktop] = create("_NET_WM_DESKTOP");
101   _atoms[net_wm_window_type] = create("_NET_WM_WINDOW_TYPE");
102   _atoms[net_wm_state] = create("_NET_WM_STATE");
103   _atoms[net_wm_strut] = create("_NET_WM_STRUT");
104 //  _atoms[net_wm_icon_geometry] = create("_NET_WM_ICON_GEOMETRY");
105 //  _atoms[net_wm_icon] = create("_NET_WM_ICON");
106 //  _atoms[net_wm_pid] = create("_NET_WM_PID");
107 //  _atoms[net_wm_handled_icons] = create("_NET_WM_HANDLED_ICONS");
108   _atoms[net_wm_allowed_actions] = create("_NET_WM_ALLOWED_ACTIONS");
109
110 //  _atoms[net_wm_ping] = create("_NET_WM_PING");
111   
112   _atoms[net_wm_window_type_desktop] = create("_NET_WM_WINDOW_TYPE_DESKTOP");
113   _atoms[net_wm_window_type_dock] = create("_NET_WM_WINDOW_TYPE_DOCK");
114   _atoms[net_wm_window_type_toolbar] = create("_NET_WM_WINDOW_TYPE_TOOLBAR");
115   _atoms[net_wm_window_type_menu] = create("_NET_WM_WINDOW_TYPE_MENU");
116   _atoms[net_wm_window_type_utility] = create("_NET_WM_WINDOW_TYPE_UTILITY");
117   _atoms[net_wm_window_type_splash] = create("_NET_WM_WINDOW_TYPE_SPLASH");
118   _atoms[net_wm_window_type_dialog] = create("_NET_WM_WINDOW_TYPE_DIALOG");
119   _atoms[net_wm_window_type_normal] = create("_NET_WM_WINDOW_TYPE_NORMAL");
120
121   _atoms[net_wm_moveresize_size_topleft] =
122     create("_NET_WM_MOVERESIZE_SIZE_TOPLEFT");
123   _atoms[net_wm_moveresize_size_topright] =
124     create("_NET_WM_MOVERESIZE_SIZE_TOPRIGHT");
125   _atoms[net_wm_moveresize_size_bottomleft] =
126     create("_NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT");
127   _atoms[net_wm_moveresize_size_bottomright] =
128     create("_NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT");
129   _atoms[net_wm_moveresize_move] =
130     create("_NET_WM_MOVERESIZE_MOVE");
131  
132   _atoms[net_wm_action_move] = create("_NET_WM_ACTION_MOVE");
133   _atoms[net_wm_action_resize] = create("_NET_WM_ACTION_RESIZE");
134   _atoms[net_wm_action_shade] = create("_NET_WM_ACTION_SHADE");
135   _atoms[net_wm_action_maximize_horz] = create("_NET_WM_ACTION_MAXIMIZE_HORZ");
136   _atoms[net_wm_action_maximize_vert] = create("_NET_WM_ACTION_MAXIMIZE_VERT");
137   _atoms[net_wm_action_change_desktop] =
138     create("_NET_WM_ACTION_CHANGE_DESKTOP");
139   _atoms[net_wm_action_close] = create("_NET_WM_ACTION_CLOSE");
140     
141   _atoms[net_wm_state_modal] = create("_NET_WM_STATE_MODAL");
142   _atoms[net_wm_state_maximized_vert] = create("_NET_WM_STATE_MAXIMIZED_VERT");
143   _atoms[net_wm_state_maximized_horz] = create("_NET_WM_STATE_MAXIMIZED_HORZ");
144   _atoms[net_wm_state_shaded] = create("_NET_WM_STATE_SHADED");
145   _atoms[net_wm_state_skip_taskbar] = create("_NET_WM_STATE_SKIP_TASKBAR");
146   _atoms[net_wm_state_skip_pager] = create("_NET_WM_STATE_SKIP_PAGER");
147   _atoms[net_wm_state_hidden] = create("_NET_WM_STATE_HIDDEN");
148   _atoms[net_wm_state_fullscreen] = create("_NET_WM_STATE_FULLSCREEN");
149   
150   _atoms[kde_net_system_tray_windows] = create("_KDE_NET_SYSTEM_TRAY_WINDOWS");
151   _atoms[kde_net_wm_system_tray_window_for] =
152     create("_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR");
153 }
154
155
156 /*
157  * clean up the class' members
158  */
159 XAtom::~XAtom() {
160   while (!_support_windows.empty()) {
161     // make sure we aren't fucking with this somewhere
162     assert(_support_windows.back() != None);
163     XDestroyWindow(_display, _support_windows.back());
164     _support_windows.pop_back();
165   }
166 }
167
168
169 /*
170  * Returns an atom from the Xserver, creating it if necessary.
171  */
172 Atom XAtom::create(const char *name) const {
173   return XInternAtom(_display, name, False);
174 }
175
176
177 /*
178  * Sets which atoms are supported for NETWM, by Openbox, on the root window.
179  */
180 void XAtom::setSupported(const ScreenInfo *screen) {
181   Window root = screen->getRootWindow();
182
183   // create the netwm support window
184   Window w = XCreateSimpleWindow(_display, root, 0, 0, 1, 1, 0, 0, 0);
185   assert(w != None);
186   _support_windows.push_back(w);
187   
188   // set supporting window
189   setValue(root, net_supporting_wm_check, window, w);
190  
191   //set properties on the supporting window
192   setValue(w, net_wm_name, utf8, "Openbox");
193   setValue(w, net_supporting_wm_check, window, w);
194   
195   // we don't support any yet..
196   // yes we do!
197
198   Atom supported[] = {
199     _atoms[net_current_desktop],
200     _atoms[net_number_of_desktops],
201     _atoms[net_desktop_geometry],
202     _atoms[net_desktop_viewport],
203     _atoms[net_active_window],
204     _atoms[net_workarea],
205     _atoms[net_client_list],
206     _atoms[net_client_list_stacking],
207     _atoms[net_desktop_names],
208     _atoms[net_close_window],
209     _atoms[net_wm_name],
210     _atoms[net_wm_visible_name],
211     _atoms[net_wm_icon_name],
212     _atoms[net_wm_visible_icon_name],
213     _atoms[net_wm_desktop],
214     _atoms[net_wm_strut],
215     _atoms[net_wm_window_type],
216     _atoms[net_wm_window_type_desktop],
217     _atoms[net_wm_window_type_dock],
218     _atoms[net_wm_window_type_toolbar],
219     _atoms[net_wm_window_type_menu],
220     _atoms[net_wm_window_type_utility],
221     _atoms[net_wm_window_type_splash],
222     _atoms[net_wm_window_type_dialog],
223     _atoms[net_wm_window_type_normal],
224     _atoms[net_wm_moveresize],
225     _atoms[net_wm_moveresize_size_topleft],
226     _atoms[net_wm_moveresize_size_topright],
227     _atoms[net_wm_moveresize_size_bottomleft],
228     _atoms[net_wm_moveresize_size_bottomright],
229     _atoms[net_wm_moveresize_move],
230     _atoms[net_wm_allowed_actions],
231     _atoms[net_wm_action_move],
232     _atoms[net_wm_action_resize],
233     _atoms[net_wm_action_shade],
234     _atoms[net_wm_action_maximize_horz],
235     _atoms[net_wm_action_maximize_vert],
236     _atoms[net_wm_action_change_desktop],
237     _atoms[net_wm_action_close],
238     _atoms[net_wm_state],
239     _atoms[net_wm_state_modal],
240     _atoms[net_wm_state_maximized_vert],
241     _atoms[net_wm_state_maximized_horz],
242     _atoms[net_wm_state_shaded],
243     _atoms[net_wm_state_skip_taskbar],
244     _atoms[net_wm_state_skip_pager],
245     _atoms[net_wm_state_hidden],
246     _atoms[net_wm_state_fullscreen],
247   };
248   const int num_supported = sizeof(supported)/sizeof(Atom);
249
250   setValue(root, net_supported, atom, supported, num_supported);
251 }
252   
253
254 /*
255  * Internal setValue.
256  * Sets a window property on a window, optionally appending to the existing
257  * value.
258  */
259 void XAtom::setValue(Window win, Atom atom, Atom type,
260                      unsigned char* data, int size, int nelements,
261                      bool append) const {
262   assert(win != None); assert(atom != None); assert(type != None);
263   assert(nelements == 0 || (nelements > 0 && data != (unsigned char *) 0));
264   assert(size == 8 || size == 16 || size == 32);
265   XChangeProperty(_display, win, atom, type, size,
266                   (append ? PropModeAppend : PropModeReplace),
267                   data, nelements);
268 }
269
270
271 /*
272  * Set a 32-bit property value on a window.
273  */
274 void XAtom::setValue(Window win, Atoms atom, Atoms type,
275                      unsigned long value) const {
276   assert(atom >= 0 && atom < NUM_ATOMS);
277   assert(type >= 0 && type < NUM_ATOMS);
278   setValue(win, _atoms[atom], _atoms[type],
279            reinterpret_cast<unsigned char*>(&value), 32, 1, False);
280 }
281
282
283 /*
284  * Set an array of 32-bit properties value on a window.
285  */
286 void XAtom::setValue(Window win, Atoms atom, Atoms type,
287                      unsigned long value[], int elements) const {
288   assert(atom >= 0 && atom < NUM_ATOMS);
289   assert(type >= 0 && type < NUM_ATOMS);
290   setValue(win, _atoms[atom], _atoms[type],
291            reinterpret_cast<unsigned char*>(value), 32, elements, False);
292 }
293
294
295 /*
296  * Set an string property value on a window.
297  */
298 void XAtom::setValue(Window win, Atoms atom, StringType type,
299                      const std::string &value) const {
300   assert(atom >= 0 && atom < NUM_ATOMS);
301   assert(type >= 0 && type < NUM_STRING_TYPE);
302   
303   Atom t;
304   switch (type) {
305   case ansi: t = _atoms[string]; break;
306   case utf8: t = _atoms[utf8_string]; break;
307   default: assert(False); return; // unhandled StringType
308   }
309   setValue(win, _atoms[atom], t,
310            reinterpret_cast<unsigned char *>(const_cast<char *>(value.c_str())),
311            8, value.size() + 1, False); // add 1 to the size to include the null
312 }
313
314
315 /*
316  * Set an array of string property values on a window.
317  */
318 void XAtom::setValue(Window win, Atoms atom, StringType type,
319                      const StringVect &strings) const {
320   assert(atom >= 0 && atom < NUM_ATOMS);
321   assert(type >= 0 && type < NUM_STRING_TYPE);
322
323   Atom t;
324   switch (type) {
325   case ansi: t = _atoms[string]; break;
326   case utf8: t = _atoms[utf8_string]; break;
327   default: assert(False); return; // unhandled StringType
328   }
329
330   std::string value;
331
332   StringVect::const_iterator it = strings.begin();
333   const StringVect::const_iterator end = strings.end();
334   for (; it != end; ++it)
335       value += *it + '\0';
336
337   setValue(win, _atoms[atom], t,
338            reinterpret_cast<unsigned char *>(const_cast<char *>(value.c_str())),
339            8, value.size(), False);
340 }
341
342
343 /*
344  * Internal getValue function used by all of the typed getValue functions.
345  * Gets an property's value from a window.
346  * Returns True if the property was successfully retrieved; False if the
347  * property did not exist on the window, or has a different type/size format
348  * than the user tried to retrieve.
349  */
350 bool XAtom::getValue(Window win, Atom atom, Atom type,
351                      unsigned long &nelements, unsigned char **value,
352                      int size) const {
353   assert(win != None); assert(atom != None); assert(type != None);
354   assert(size == 8 || size == 16 || size == 32);
355   assert(nelements > 0);
356   unsigned char *c_val = 0;        // value alloc'd with c malloc
357   Atom ret_type;
358   int ret_size;
359   unsigned long ret_bytes;
360   int result;
361   const unsigned long maxread = nelements;
362   // try get the first element
363   result = XGetWindowProperty(_display, win, atom, 0l, 1l, False,
364                               AnyPropertyType, &ret_type, &ret_size,
365                               &nelements, &ret_bytes, &c_val);
366   if (result != Success || ret_type != type || ret_size != size ||
367       nelements < 1) {
368     // an error occured, the property does not exist on the window, or is empty,
369     // or the wrong data is in property for the request
370     if (c_val) XFree(c_val);
371     return False;
372   }
373   // the data is correct, now, is there more elements left?
374   if (ret_bytes == 0 || maxread <= nelements) {
375     // we got the whole property's value
376     *value = new unsigned char[nelements * size/8 + 1];
377     memcpy(*value, c_val, nelements * size/8 + 1);
378     XFree(c_val);
379     return True;    
380   }
381   // get the entire property since it is larger than one long
382   XFree(c_val);
383   // the number of longs that need to be retreived to get the property's entire
384   // value. The last + 1 is the first long that we retrieved above.
385   int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
386   if (remain > size/8 * (signed)maxread) // dont get more than the max
387     remain = size/8 * (signed)maxread;
388   result = XGetWindowProperty(_display, win, atom, 0l, remain, False, type,
389                               &ret_type, &ret_size, &nelements, &ret_bytes,
390                               &c_val);
391   assert(result == Success);
392   assert(ret_bytes == 0);
393   *value = new unsigned char[nelements * size/8 + 1];
394   memcpy(*value, c_val, nelements * size/8 + 1);
395   XFree(c_val);
396   return True;    
397 }
398
399
400 /*
401  * Gets a 32-bit property's value from a window.
402  */
403 bool XAtom::getValue(Window win, Atoms atom, Atoms type,
404                          unsigned long &nelements,
405                          unsigned long **value) const {
406   assert(atom >= 0 && atom < NUM_ATOMS);
407   assert(type >= 0 && type < NUM_ATOMS);
408   return getValue(win, _atoms[atom], _atoms[type], nelements,
409                   reinterpret_cast<unsigned char **>(value), 32);
410 }
411
412
413 /*
414  * Gets a single 32-bit property's value from a window.
415  */
416 bool XAtom::getValue(Window win, Atoms atom, Atoms type,
417                      unsigned long &value) const {
418   assert(atom >= 0 && atom < NUM_ATOMS);
419   assert(type >= 0 && type < NUM_ATOMS);
420   unsigned long *temp;
421   unsigned long num = 1;
422   if (! getValue(win, _atoms[atom], _atoms[type], num,
423                  reinterpret_cast<unsigned char **>(&temp), 32))
424     return False;
425   value = temp[0];
426   delete [] temp;
427   return True;
428 }
429
430
431 /*
432  * Gets an string property's value from a window.
433  */
434 bool XAtom::getValue(Window win, Atoms atom, StringType type,
435                      std::string &value) const {
436   unsigned long n = 1;
437   StringVect s;
438   if (getValue(win, atom, type, n, s)) {
439     value = s[0];
440     return True;
441   }
442   return False;
443 }
444
445
446 bool XAtom::getValue(Window win, Atoms atom, StringType type,
447                      unsigned long &nelements, StringVect &strings) const {
448   assert(atom >= 0 && atom < NUM_ATOMS);
449   assert(type >= 0 && type < NUM_STRING_TYPE);
450   assert(win != None); assert(_atoms[atom] != None);
451   assert(nelements > 0);
452
453   Atom t;
454   switch (type) {
455   case ansi: t = _atoms[string]; break;
456   case utf8: t = _atoms[utf8_string]; break;
457   default: assert(False); return False; // unhandled StringType
458   }
459   
460   unsigned char *value;
461   unsigned long elements = (unsigned) -1;
462   if (!getValue(win, _atoms[atom], t, elements, &value, 8) || elements < 1)
463     return False;
464
465   std::string s(reinterpret_cast<char *>(value), elements);
466   delete [] value;
467
468   std::string::const_iterator it = s.begin(), end = s.end();
469   unsigned long num = 0;
470   while(num < nelements) {
471     std::string::const_iterator tmp = it; // current string.begin()
472     it = std::find(tmp, end, '\0');       // look for null between tmp and end
473     strings.push_back(std::string(tmp, it));   // s[tmp:it)
474     if (it == end) break;
475     ++it;
476     if (it == end) break;
477     ++num;
478   }
479
480   nelements = num;
481
482   return True;
483 }
484
485
486 /*
487  * Removes a property entirely from a window.
488  */
489 void XAtom::eraseValue(Window win, Atoms atom) const {
490   assert(atom >= 0 && atom < NUM_ATOMS);
491   XDeleteProperty(_display, win, _atoms[atom]);
492 }