]> icculus.org git repositories - mikachu/openbox.git/blob - src/XAtom.cc
merge from netwm-merge2 to netwm-merge3. Basically, all of netwm that we intend to...
[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); // 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); // 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;        // value alloc'd with c malloc
357   Atom ret_type;
358   int ret_size;
359   unsigned long ret_bytes;
360   const unsigned long maxread = nelements;
361   // try get the first element
362   XGetWindowProperty(_display, win, atom, 0l, 1l, False, AnyPropertyType,
363                      &ret_type, &ret_size, &nelements, &ret_bytes, &c_val);
364   if (ret_type == None || nelements < 1)
365     // the property does not exist on the window or is empty
366     return false;
367   if (ret_type != type || ret_size != size) {
368     // wrong data in property
369     XFree(c_val);
370     return false;
371   }
372   // the data is correct, now, is there more elements left?
373   if (ret_bytes == 0 || maxread <= nelements) {
374     // we got the whole property's value
375     *value = new unsigned char[nelements * size/8 + 1];
376     memcpy(*value, c_val, nelements * size/8 + 1);
377     XFree(c_val);
378     return true;    
379   }
380   // get the entire property since it is larger than one long
381   XFree(c_val);
382   // the number of longs that need to be retreived to get the property's entire
383   // value. The last + 1 is the first long that we retrieved above.
384   int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
385   if (remain > size/8 * (signed)maxread) // dont get more than the max
386     remain = size/8 * (signed)maxread;
387   XGetWindowProperty(_display, win, atom, 0l, remain, False, type, &ret_type,
388                      &ret_size, &nelements, &ret_bytes, &c_val);
389   assert(ret_bytes == 0);
390   *value = new unsigned char[nelements * size/8 + 1];
391   memcpy(*value, c_val, nelements * size/8 + 1);
392   XFree(c_val);
393   return true;    
394 }
395
396
397 /*
398  * Gets a 32-bit property's value from a window.
399  */
400 bool XAtom::getValue(Window win, Atoms atom, Atoms type,
401                          unsigned long &nelements,
402                          unsigned long **value) const {
403   assert(atom >= 0 && atom < NUM_ATOMS);
404   assert(type >= 0 && type < NUM_ATOMS);
405   return getValue(win, _atoms[atom], _atoms[type], nelements,
406                   reinterpret_cast<unsigned char **>(value), 32);
407 }
408
409
410 /*
411  * Gets a single 32-bit property's value from a window.
412  */
413 bool XAtom::getValue(Window win, Atoms atom, Atoms type,
414                      unsigned long &value) const {
415   assert(atom >= 0 && atom < NUM_ATOMS);
416   assert(type >= 0 && type < NUM_ATOMS);
417   unsigned long *temp;
418   unsigned long num = 1;
419   if (! getValue(win, _atoms[atom], _atoms[type], num,
420                  reinterpret_cast<unsigned char **>(&temp), 32))
421     return false;
422   value = temp[0];
423   delete [] temp;
424   return true;
425 }
426
427
428 /*
429  * Gets an string property's value from a window.
430  */
431 bool XAtom::getValue(Window win, Atoms atom, StringType type,
432                      std::string &value) const {
433   int n = 1;
434   StringVect s;
435   if (getValue(win, atom, type, n, s)) {
436     value = s[0];
437     return true;
438   }
439   return false;
440 }
441
442
443 bool XAtom::getValue(Window win, Atoms atom, StringType type, int &nelements,
444                      StringVect &strings) const {
445   assert(atom >= 0 && atom < NUM_ATOMS);
446   assert(type >= 0 && type < NUM_STRING_TYPE);
447   assert(win != None); assert(_atoms[atom] != None);
448   assert(nelements > 0);
449
450   Atom t;
451   switch (type) {
452   case ansi: t = _atoms[string]; break;
453   case utf8: t = _atoms[utf8_string]; break;
454   default: assert(false); // unhandled StringType
455   }
456   
457   unsigned char *value;
458   unsigned long elements = (unsigned) -1;
459   if (!getValue(win, _atoms[atom], t, elements, &value, 8) || elements < 1)
460     return false;
461
462   std::string s(reinterpret_cast<char *>(value));
463   delete [] value;
464
465   std::string::const_iterator it = s.begin(), end = s.end();
466   int num = 0;
467   while(num < nelements) {
468     std::string::const_iterator tmp = it; // current string.begin()
469     it = std::find(tmp, end, '\0');       // look for null between tmp and end
470     strings.push_back(std::string(tmp, it));   // s[tmp:it)
471     if (it == end)
472       break;
473     ++it;
474     ++num;
475   }
476
477   nelements = elements;
478
479   return true;
480 }
481
482
483 /*
484  * Removes a property entirely from a window.
485  */
486 void XAtom::eraseValue(Window win, Atoms atom) const {
487   assert(atom >= 0 && atom < NUM_ATOMS);
488   XDeleteProperty(_display, win, _atoms[atom]);
489 }