]> Dogcows Code - chaz/openbox/blob - otk/property.cc
add OBProperty to otk, which was formerly known as ob::XAtom
[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 extern "C" {
8 #include <assert.h>
9 }
10
11 #include "property.hh"
12 #include "display.hh"
13
14 namespace otk {
15
16 OBProperty::OBProperty()
17 {
18 assert(OBDisplay::display);
19
20 // make sure asserts fire if there is a problem
21 memset(_atoms, 0, sizeof(_atoms));
22
23 _atoms[Atom_Cardinal] = XA_CARDINAL;
24 _atoms[Atom_Window] = XA_WINDOW;
25 _atoms[Atom_Pixmap] = XA_PIXMAP;
26 _atoms[Atom_Atom] = XA_ATOM;
27 _atoms[Atom_String] = XA_STRING;
28 _atoms[Atom_Utf8] = create("UTF8_STRING");
29
30 #ifdef HAVE_GETPID
31 _atoms[blackbox_pid] = create("_BLACKBOX_PID");
32 #endif // HAVE_GETPID
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_maximized_vert] = create("_NET_WM_STATE_MAXIMIZED_VERT");
134 _atoms[net_wm_state_maximized_horz] = create("_NET_WM_STATE_MAXIMIZED_HORZ");
135 _atoms[net_wm_state_shaded] = create("_NET_WM_STATE_SHADED");
136 _atoms[net_wm_state_skip_taskbar] = create("_NET_WM_STATE_SKIP_TASKBAR");
137 _atoms[net_wm_state_skip_pager] = create("_NET_WM_STATE_SKIP_PAGER");
138 _atoms[net_wm_state_hidden] = create("_NET_WM_STATE_HIDDEN");
139 _atoms[net_wm_state_fullscreen] = create("_NET_WM_STATE_FULLSCREEN");
140
141 _atoms[kde_net_system_tray_windows] = create("_KDE_NET_SYSTEM_TRAY_WINDOWS");
142 _atoms[kde_net_wm_system_tray_window_for] =
143 create("_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR");
144 _atoms[kde_net_wm_window_type_override] =
145 create("_KDE_NET_WM_WINDOW_TYPE_OVERRIDE");
146 }
147
148
149 /*
150 * clean up the class' members
151 */
152 OBProperty::~OBProperty()
153 {
154 }
155
156
157 /*
158 * Returns an atom from the Xserver, creating it if necessary.
159 */
160 Atom OBProperty::create(const char *name) const
161 {
162 return XInternAtom(OBDisplay::display, name, False);
163 }
164
165
166 /*
167 * Internal set.
168 * Sets a window property on a window, optionally appending to the existing
169 * value.
170 */
171 void OBProperty::set(Window win, Atom atom, Atom type,
172 unsigned char* data, int size, int nelements,
173 bool append) const
174 {
175 assert(win != None); assert(atom != None); assert(type != None);
176 assert(nelements == 0 || (nelements > 0 && data != (unsigned char *) 0));
177 assert(size == 8 || size == 16 || size == 32);
178 XChangeProperty(OBDisplay::display, win, atom, type, size,
179 (append ? PropModeAppend : PropModeReplace),
180 data, nelements);
181 }
182
183
184 /*
185 * Set a 32-bit property value on a window.
186 */
187 void OBProperty::set(Window win, Atoms atom, Atoms type,
188 unsigned long value) const
189 {
190 assert(atom >= 0 && atom < NUM_ATOMS);
191 assert(type >= 0 && type < NUM_ATOMS);
192 set(win, _atoms[atom], _atoms[type],
193 reinterpret_cast<unsigned char*>(&value), 32, 1, False);
194 }
195
196
197 /*
198 * Set an array of 32-bit properties value on a window.
199 */
200 void OBProperty::set(Window win, Atoms atom, Atoms type,
201 unsigned long value[], int elements) const
202 {
203 assert(atom >= 0 && atom < NUM_ATOMS);
204 assert(type >= 0 && type < NUM_ATOMS);
205 set(win, _atoms[atom], _atoms[type],
206 reinterpret_cast<unsigned char*>(value), 32, elements, False);
207 }
208
209
210 /*
211 * Set an string property value on a window.
212 */
213 void OBProperty::set(Window win, Atoms atom, StringType type,
214 const std::string &value) const
215 {
216 assert(atom >= 0 && atom < NUM_ATOMS);
217 assert(type >= 0 && type < NUM_STRING_TYPE);
218
219 Atom t;
220 switch (type) {
221 case ascii: t = _atoms[Atom_String]; break;
222 case utf8: t = _atoms[Atom_Utf8]; break;
223 default: assert(False); return; // unhandled StringType
224 }
225 set(win, _atoms[atom], t,
226 reinterpret_cast<unsigned char *>(const_cast<char *>(value.c_str())),
227 8, value.size() + 1, False); // add 1 to the size to include the null
228 }
229
230
231 /*
232 * Set an array of string property values on a window.
233 */
234 void OBProperty::set(Window win, Atoms atom, StringType type,
235 const StringVect &strings) const
236 {
237 assert(atom >= 0 && atom < NUM_ATOMS);
238 assert(type >= 0 && type < NUM_STRING_TYPE);
239
240 Atom t;
241 switch (type) {
242 case ascii: t = _atoms[Atom_String]; break;
243 case utf8: t = _atoms[Atom_Utf8]; break;
244 default: assert(False); return; // unhandled StringType
245 }
246
247 std::string value;
248
249 StringVect::const_iterator it = strings.begin();
250 const StringVect::const_iterator end = strings.end();
251 for (; it != end; ++it)
252 value += *it + '\0';
253
254 set(win, _atoms[atom], t,
255 reinterpret_cast<unsigned char *>(const_cast<char *>(value.c_str())),
256 8, value.size(), False);
257 }
258
259
260 /*
261 * Internal get function used by all of the typed get functions.
262 * Gets an property's value from a window.
263 * Returns True if the property was successfully retrieved; False if the
264 * property did not exist on the window, or has a different type/size format
265 * than the user tried to retrieve.
266 */
267 bool OBProperty::get(Window win, Atom atom, Atom type,
268 unsigned long *nelements, unsigned char **value,
269 int size) const
270 {
271 assert(win != None); assert(atom != None); assert(type != None);
272 assert(size == 8 || size == 16 || size == 32);
273 assert(*nelements > 0);
274 unsigned char *c_val = 0; // value alloc'd in Xlib, must be XFree()d
275 Atom ret_type;
276 int ret_size;
277 unsigned long ret_bytes;
278 int result;
279 unsigned long maxread = *nelements;
280 bool ret = False;
281
282 // try get the first element
283 result = XGetWindowProperty(OBDisplay::display, win, atom, 0l, 1l,
284 False, AnyPropertyType, &ret_type, &ret_size,
285 nelements, &ret_bytes, &c_val);
286 ret = (result == Success && ret_type == type && ret_size == size &&
287 *nelements > 0);
288 if (ret) {
289 if (ret_bytes == 0 || maxread <= *nelements) {
290 // we got the whole property's value
291 *value = new unsigned char[*nelements * size/8 + 1];
292 memcpy(*value, c_val, *nelements * size/8 + 1);
293 } else {
294 // get the entire property since it is larger than one long
295 XFree(c_val);
296 // the number of longs that need to be retreived to get the property's
297 // entire value. The last + 1 is the first long that we retrieved above.
298 int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
299 if (remain > size/8 * (signed)maxread) // dont get more than the max
300 remain = size/8 * (signed)maxread;
301 result = XGetWindowProperty(OBDisplay::display, win, atom, 0l,
302 remain, False, type, &ret_type, &ret_size,
303 nelements, &ret_bytes, &c_val);
304 ret = (result == Success && ret_type == type && ret_size == size &&
305 ret_bytes == 0);
306 /*
307 If the property has changed type/size, or has grown since our first
308 read of it, then stop here and try again. If it shrank, then this will
309 still work.
310 */
311 if (! ret)
312 return get(win, atom, type, &maxread, value, size);
313
314 *value = new unsigned char[*nelements * size/8 + 1];
315 memcpy(*value, c_val, *nelements * size/8 + 1);
316 }
317 }
318 if (c_val) XFree(c_val);
319 return ret;
320 }
321
322
323 /*
324 * Gets a 32-bit property's value from a window.
325 */
326 bool OBProperty::get(Window win, Atoms atom, Atoms type,
327 unsigned long *nelements,
328 unsigned long **value) const
329 {
330 assert(atom >= 0 && atom < NUM_ATOMS);
331 assert(type >= 0 && type < NUM_ATOMS);
332 return get(win, _atoms[atom], _atoms[type], nelements,
333 reinterpret_cast<unsigned char **>(value), 32);
334 }
335
336
337 /*
338 * Gets a single 32-bit property's value from a window.
339 */
340 bool OBProperty::get(Window win, Atoms atom, Atoms type,
341 unsigned long *value) const
342 {
343 assert(atom >= 0 && atom < NUM_ATOMS);
344 assert(type >= 0 && type < NUM_ATOMS);
345 unsigned long *temp;
346 unsigned long num = 1;
347 if (! get(win, _atoms[atom], _atoms[type], &num,
348 reinterpret_cast<unsigned char **>(&temp), 32))
349 return False;
350 *value = temp[0];
351 delete [] temp;
352 return True;
353 }
354
355
356 /*
357 * Gets an string property's value from a window.
358 */
359 bool OBProperty::get(Window win, Atoms atom, StringType type,
360 std::string *value) const
361 {
362 unsigned long n = 1;
363 StringVect s;
364 if (get(win, atom, type, &n, &s)) {
365 *value = s[0];
366 return True;
367 }
368 return False;
369 }
370
371
372 bool OBProperty::get(Window win, Atoms atom, StringType type,
373 unsigned long *nelements, StringVect *strings) const
374 {
375 assert(atom >= 0 && atom < NUM_ATOMS);
376 assert(type >= 0 && type < NUM_STRING_TYPE);
377 assert(win != None); assert(_atoms[atom] != None);
378 assert(*nelements > 0);
379
380 Atom t;
381 switch (type) {
382 case ascii: t = _atoms[Atom_String]; break;
383 case utf8: t = _atoms[Atom_Utf8]; break;
384 default: assert(False); return False; // unhandled StringType
385 }
386
387 unsigned char *value;
388 unsigned long elements = (unsigned) -1;
389 if (!get(win, _atoms[atom], t, &elements, &value, 8) || elements < 1)
390 return False;
391
392 std::string s(reinterpret_cast<char *>(value), elements);
393 delete [] value;
394
395 std::string::const_iterator it = s.begin(), end = s.end();
396 unsigned long num = 0;
397 while(num < *nelements) {
398 std::string::const_iterator tmp = it; // current string.begin()
399 it = std::find(tmp, end, '\0'); // look for null between tmp and end
400 strings->push_back(std::string(tmp, it)); // s[tmp:it)
401 ++num;
402 if (it == end) break;
403 ++it;
404 if (it == end) break;
405 }
406
407 *nelements = num;
408
409 return True;
410 }
411
412
413 /*
414 * Removes a property entirely from a window.
415 */
416 void OBProperty::erase(Window win, Atoms atom) const
417 {
418 assert(atom >= 0 && atom < NUM_ATOMS);
419 XDeleteProperty(OBDisplay::display, win, _atoms[atom]);
420 }
421
422 }
This page took 0.059397 seconds and 5 git commands to generate.