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