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