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