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