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