]> icculus.org git repositories - dana/openbox.git/blob - src/xatom.cc
rename, remove bullshit. ya
[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   unsigned long maxread = nelements;
368   bool ret = False;
369
370   // try get the first element
371   result = XGetWindowProperty(_display, win, atom, 0l, 1l, False,
372                               AnyPropertyType, &ret_type, &ret_size,
373                               &nelements, &ret_bytes, &c_val);
374   ret = (result == Success && ret_type == type && ret_size == size &&
375          nelements > 0);
376   if (ret) {
377     if (ret_bytes == 0 || maxread <= nelements) {
378       // we got the whole property's value
379       *value = new unsigned char[nelements * size/8 + 1];
380       memcpy(*value, c_val, nelements * size/8 + 1);
381     } else {
382       // get the entire property since it is larger than one long
383       XFree(c_val);
384       // the number of longs that need to be retreived to get the property's
385       // entire value. The last + 1 is the first long that we retrieved above.
386       int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
387       if (remain > size/8 * (signed)maxread) // dont get more than the max
388         remain = size/8 * (signed)maxread;
389       result = XGetWindowProperty(_display, win, atom, 0l, remain, False, type,
390                                   &ret_type, &ret_size, &nelements, &ret_bytes,
391                                   &c_val);
392       ret = (result == Success && ret_type == type && ret_size == size &&
393              ret_bytes == 0);
394       /*
395         If the property has changed type/size, or has grown since our first
396         read of it, then stop here and try again. If it shrank, then this will
397         still work.
398       */
399       if (! ret)
400         return getValue(win, atom, type, maxread, value, size);
401   
402       *value = new unsigned char[nelements * size/8 + 1];
403       memcpy(*value, c_val, nelements * size/8 + 1);
404     }    
405   }
406   if (c_val) XFree(c_val);
407   return ret;
408 }
409
410
411 /*
412  * Gets a 32-bit property's value from a window.
413  */
414 bool XAtom::getValue(Window win, Atoms atom, Atoms type,
415                          unsigned long &nelements,
416                          unsigned long **value) const {
417   assert(atom >= 0 && atom < NUM_ATOMS);
418   assert(type >= 0 && type < NUM_ATOMS);
419   return getValue(win, _atoms[atom], _atoms[type], nelements,
420                   reinterpret_cast<unsigned char **>(value), 32);
421 }
422
423
424 /*
425  * Gets a single 32-bit property's value from a window.
426  */
427 bool XAtom::getValue(Window win, Atoms atom, Atoms type,
428                      unsigned long &value) const {
429   assert(atom >= 0 && atom < NUM_ATOMS);
430   assert(type >= 0 && type < NUM_ATOMS);
431   unsigned long *temp;
432   unsigned long num = 1;
433   if (! getValue(win, _atoms[atom], _atoms[type], num,
434                  reinterpret_cast<unsigned char **>(&temp), 32))
435     return False;
436   value = temp[0];
437   delete [] temp;
438   return True;
439 }
440
441
442 /*
443  * Gets an string property's value from a window.
444  */
445 bool XAtom::getValue(Window win, Atoms atom, StringType type,
446                      std::string &value) const {
447   unsigned long n = 1;
448   StringVect s;
449   if (getValue(win, atom, type, n, s)) {
450     value = s[0];
451     return True;
452   }
453   return False;
454 }
455
456
457 bool XAtom::getValue(Window win, Atoms atom, StringType type,
458                      unsigned long &nelements, StringVect &strings) const {
459   assert(atom >= 0 && atom < NUM_ATOMS);
460   assert(type >= 0 && type < NUM_STRING_TYPE);
461   assert(win != None); assert(_atoms[atom] != None);
462   assert(nelements > 0);
463
464   Atom t;
465   switch (type) {
466   case ansi: t = _atoms[string]; break;
467   case utf8: t = _atoms[utf8_string]; break;
468   default: assert(False); return False; // unhandled StringType
469   }
470   
471   unsigned char *value;
472   unsigned long elements = (unsigned) -1;
473   if (!getValue(win, _atoms[atom], t, elements, &value, 8) || elements < 1)
474     return False;
475
476   std::string s(reinterpret_cast<char *>(value), elements);
477   delete [] value;
478
479   std::string::const_iterator it = s.begin(), end = s.end();
480   unsigned long num = 0;
481   while(num < nelements) {
482     std::string::const_iterator tmp = it; // current string.begin()
483     it = std::find(tmp, end, '\0');       // look for null between tmp and end
484     strings.push_back(std::string(tmp, it));   // s[tmp:it)
485     ++num;
486     if (it == end) break;
487     ++it;
488     if (it == end) break;
489   }
490
491   nelements = num;
492
493   return True;
494 }
495
496
497 /*
498  * Removes a property entirely from a window.
499  */
500 void XAtom::eraseValue(Window win, Atoms atom) const {
501   assert(atom >= 0 && atom < NUM_ATOMS);
502   XDeleteProperty(_display, win, _atoms[atom]);
503 }
504
505
506 void XAtom::sendClientMessage(Window target, Atoms type, Window about,
507                               long data, long data1, long data2,
508                               long data3, long data4) const {
509   assert(atom >= 0 && atom < NUM_ATOMS);
510   assert(target != None);
511
512   XEvent e;
513   e.xclient.type = ClientMessage;
514   e.xclient.format = 32;
515   e.xclient.message_type = _atoms[type];
516   e.xclient.window = about;
517   e.xclient.data.l[0] = data;
518   e.xclient.data.l[1] = data1;
519   e.xclient.data.l[2] = data2;
520   e.xclient.data.l[3] = data3;
521   e.xclient.data.l[4] = data4;
522
523   XSendEvent(_display, target, False,
524              SubstructureRedirectMask | SubstructureNotifyMask,
525              &e);
526 }