]> Dogcows Code - chaz/openbox/blob - src/xatom.cc
3ed6cdf4957bdf875265eff36202179a66044b3f
[chaz/openbox] / src / xatom.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 "xatom.hh"
12 #include "screen.hh"
13 #include "util.hh"
14
15 namespace ob {
16
17 XAtom::XAtom(Display *d) {
18 _display = d;
19
20 // make sure asserts fire if there is a problem
21 memset(_atoms, 0, sizeof(_atoms));
22
23 _atoms[cardinal] = XA_CARDINAL;
24 _atoms[window] = XA_WINDOW;
25 _atoms[pixmap] = XA_PIXMAP;
26 _atoms[atom] = XA_ATOM;
27 _atoms[string] = XA_STRING;
28 _atoms[utf8_string] = 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 XAtom::~XAtom() {
153 while (!_support_windows.empty()) {
154 // make sure we aren't fucking with this somewhere
155 assert(_support_windows.back() != None);
156 XDestroyWindow(_display, _support_windows.back());
157 _support_windows.pop_back();
158 }
159 }
160
161
162 /*
163 * Returns an atom from the Xserver, creating it if necessary.
164 */
165 Atom XAtom::create(const char *name) const {
166 return XInternAtom(_display, name, False);
167 }
168
169
170 /*
171 * Sets which atoms are supported for NETWM, by Openbox, on the root window.
172 */
173 void XAtom::setSupported(const otk::ScreenInfo *screen) {
174 Window root = screen->getRootWindow();
175
176 // create the netwm support window
177 Window w = XCreateSimpleWindow(_display, root, 0, 0, 1, 1, 0, 0, 0);
178 assert(w != None);
179 _support_windows.push_back(w);
180
181 // set supporting window
182 setValue(root, net_supporting_wm_check, window, w);
183
184 //set properties on the supporting window
185 setValue(w, net_wm_name, utf8, "Openbox");
186 setValue(w, net_supporting_wm_check, window, w);
187
188 // we don't support any yet..
189 // yes we do!
190
191 Atom supported[] = {
192 _atoms[net_current_desktop],
193 _atoms[net_number_of_desktops],
194 _atoms[net_desktop_geometry],
195 _atoms[net_desktop_viewport],
196 _atoms[net_active_window],
197 _atoms[net_workarea],
198 _atoms[net_client_list],
199 _atoms[net_client_list_stacking],
200 _atoms[net_desktop_names],
201 _atoms[net_close_window],
202 _atoms[net_wm_name],
203 _atoms[net_wm_visible_name],
204 _atoms[net_wm_icon_name],
205 _atoms[net_wm_visible_icon_name],
206 _atoms[net_wm_desktop],
207 _atoms[net_wm_strut],
208 _atoms[net_wm_window_type],
209 _atoms[net_wm_window_type_desktop],
210 _atoms[net_wm_window_type_dock],
211 _atoms[net_wm_window_type_toolbar],
212 _atoms[net_wm_window_type_menu],
213 _atoms[net_wm_window_type_utility],
214 _atoms[net_wm_window_type_splash],
215 _atoms[net_wm_window_type_dialog],
216 _atoms[net_wm_window_type_normal],
217 _atoms[net_wm_moveresize],
218 _atoms[net_wm_moveresize_size_topleft],
219 _atoms[net_wm_moveresize_size_topright],
220 _atoms[net_wm_moveresize_size_bottomleft],
221 _atoms[net_wm_moveresize_size_bottomright],
222 _atoms[net_wm_moveresize_move],
223 _atoms[net_wm_allowed_actions],
224 _atoms[net_wm_action_move],
225 _atoms[net_wm_action_resize],
226 _atoms[net_wm_action_shade],
227 _atoms[net_wm_action_maximize_horz],
228 _atoms[net_wm_action_maximize_vert],
229 _atoms[net_wm_action_change_desktop],
230 _atoms[net_wm_action_close],
231 _atoms[net_wm_state],
232 _atoms[net_wm_state_modal],
233 _atoms[net_wm_state_maximized_vert],
234 _atoms[net_wm_state_maximized_horz],
235 _atoms[net_wm_state_shaded],
236 _atoms[net_wm_state_skip_taskbar],
237 _atoms[net_wm_state_skip_pager],
238 _atoms[net_wm_state_hidden],
239 _atoms[net_wm_state_fullscreen],
240 };
241 const int num_supported = sizeof(supported)/sizeof(Atom);
242
243 setValue(root, net_supported, atom, supported, num_supported);
244 }
245
246
247 /*
248 * Internal setValue.
249 * Sets a window property on a window, optionally appending to the existing
250 * value.
251 */
252 void XAtom::setValue(Window win, Atom atom, Atom type,
253 unsigned char* data, int size, int nelements,
254 bool append) const {
255 assert(win != None); assert(atom != None); assert(type != None);
256 assert(nelements == 0 || (nelements > 0 && data != (unsigned char *) 0));
257 assert(size == 8 || size == 16 || size == 32);
258 XChangeProperty(_display, win, atom, type, size,
259 (append ? PropModeAppend : PropModeReplace),
260 data, nelements);
261 }
262
263
264 /*
265 * Set a 32-bit property value on a window.
266 */
267 void XAtom::setValue(Window win, Atoms atom, Atoms type,
268 unsigned long value) const {
269 assert(atom >= 0 && atom < NUM_ATOMS);
270 assert(type >= 0 && type < NUM_ATOMS);
271 setValue(win, _atoms[atom], _atoms[type],
272 reinterpret_cast<unsigned char*>(&value), 32, 1, False);
273 }
274
275
276 /*
277 * Set an array of 32-bit properties value on a window.
278 */
279 void XAtom::setValue(Window win, Atoms atom, Atoms type,
280 unsigned long value[], int elements) const {
281 assert(atom >= 0 && atom < NUM_ATOMS);
282 assert(type >= 0 && type < NUM_ATOMS);
283 setValue(win, _atoms[atom], _atoms[type],
284 reinterpret_cast<unsigned char*>(value), 32, elements, False);
285 }
286
287
288 /*
289 * Set an string property value on a window.
290 */
291 void XAtom::setValue(Window win, Atoms atom, StringType type,
292 const std::string &value) const {
293 assert(atom >= 0 && atom < NUM_ATOMS);
294 assert(type >= 0 && type < NUM_STRING_TYPE);
295
296 Atom t;
297 switch (type) {
298 case ansi: t = _atoms[string]; break;
299 case utf8: t = _atoms[utf8_string]; break;
300 default: assert(False); return; // unhandled StringType
301 }
302 setValue(win, _atoms[atom], t,
303 reinterpret_cast<unsigned char *>(const_cast<char *>(value.c_str())),
304 8, value.size() + 1, False); // add 1 to the size to include the null
305 }
306
307
308 /*
309 * Set an array of string property values on a window.
310 */
311 void XAtom::setValue(Window win, Atoms atom, StringType type,
312 const StringVect &strings) const {
313 assert(atom >= 0 && atom < NUM_ATOMS);
314 assert(type >= 0 && type < NUM_STRING_TYPE);
315
316 Atom t;
317 switch (type) {
318 case ansi: t = _atoms[string]; break;
319 case utf8: t = _atoms[utf8_string]; break;
320 default: assert(False); return; // unhandled StringType
321 }
322
323 std::string value;
324
325 StringVect::const_iterator it = strings.begin();
326 const StringVect::const_iterator end = strings.end();
327 for (; it != end; ++it)
328 value += *it + '\0';
329
330 setValue(win, _atoms[atom], t,
331 reinterpret_cast<unsigned char *>(const_cast<char *>(value.c_str())),
332 8, value.size(), False);
333 }
334
335
336 /*
337 * Internal getValue function used by all of the typed getValue functions.
338 * Gets an property's value from a window.
339 * Returns True if the property was successfully retrieved; False if the
340 * property did not exist on the window, or has a different type/size format
341 * than the user tried to retrieve.
342 */
343 bool XAtom::getValue(Window win, Atom atom, Atom type,
344 unsigned long &nelements, unsigned char **value,
345 int size) const {
346 assert(win != None); assert(atom != None); assert(type != None);
347 assert(size == 8 || size == 16 || size == 32);
348 assert(nelements > 0);
349 unsigned char *c_val = 0; // value alloc'd in Xlib, must be XFree()d
350 Atom ret_type;
351 int ret_size;
352 unsigned long ret_bytes;
353 int result;
354 unsigned long maxread = nelements;
355 bool ret = False;
356
357 // try get the first element
358 result = XGetWindowProperty(_display, win, atom, 0l, 1l, False,
359 AnyPropertyType, &ret_type, &ret_size,
360 &nelements, &ret_bytes, &c_val);
361 ret = (result == Success && ret_type == type && ret_size == size &&
362 nelements > 0);
363 if (ret) {
364 if (ret_bytes == 0 || maxread <= nelements) {
365 // we got the whole property's value
366 *value = new unsigned char[nelements * size/8 + 1];
367 memcpy(*value, c_val, nelements * size/8 + 1);
368 } else {
369 // get the entire property since it is larger than one long
370 XFree(c_val);
371 // the number of longs that need to be retreived to get the property's
372 // entire value. The last + 1 is the first long that we retrieved above.
373 int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
374 if (remain > size/8 * (signed)maxread) // dont get more than the max
375 remain = size/8 * (signed)maxread;
376 result = XGetWindowProperty(_display, win, atom, 0l, remain, False, type,
377 &ret_type, &ret_size, &nelements, &ret_bytes,
378 &c_val);
379 ret = (result == Success && ret_type == type && ret_size == size &&
380 ret_bytes == 0);
381 /*
382 If the property has changed type/size, or has grown since our first
383 read of it, then stop here and try again. If it shrank, then this will
384 still work.
385 */
386 if (! ret)
387 return getValue(win, atom, type, maxread, value, size);
388
389 *value = new unsigned char[nelements * size/8 + 1];
390 memcpy(*value, c_val, nelements * size/8 + 1);
391 }
392 }
393 if (c_val) XFree(c_val);
394 return ret;
395 }
396
397
398 /*
399 * Gets a 32-bit property's value from a window.
400 */
401 bool XAtom::getValue(Window win, Atoms atom, Atoms type,
402 unsigned long &nelements,
403 unsigned long **value) const {
404 assert(atom >= 0 && atom < NUM_ATOMS);
405 assert(type >= 0 && type < NUM_ATOMS);
406 return getValue(win, _atoms[atom], _atoms[type], nelements,
407 reinterpret_cast<unsigned char **>(value), 32);
408 }
409
410
411 /*
412 * Gets a single 32-bit property's value from a window.
413 */
414 bool XAtom::getValue(Window win, Atoms atom, Atoms type,
415 unsigned long &value) const {
416 assert(atom >= 0 && atom < NUM_ATOMS);
417 assert(type >= 0 && type < NUM_ATOMS);
418 unsigned long *temp;
419 unsigned long num = 1;
420 if (! getValue(win, _atoms[atom], _atoms[type], num,
421 reinterpret_cast<unsigned char **>(&temp), 32))
422 return False;
423 value = temp[0];
424 delete [] temp;
425 return True;
426 }
427
428
429 /*
430 * Gets an string property's value from a window.
431 */
432 bool XAtom::getValue(Window win, Atoms atom, StringType type,
433 std::string &value) const {
434 unsigned long n = 1;
435 StringVect s;
436 if (getValue(win, atom, type, n, s)) {
437 value = s[0];
438 return True;
439 }
440 return False;
441 }
442
443
444 bool XAtom::getValue(Window win, Atoms atom, StringType type,
445 unsigned long &nelements, StringVect &strings) const {
446 assert(atom >= 0 && atom < NUM_ATOMS);
447 assert(type >= 0 && type < NUM_STRING_TYPE);
448 assert(win != None); assert(_atoms[atom] != None);
449 assert(nelements > 0);
450
451 Atom t;
452 switch (type) {
453 case ansi: t = _atoms[string]; break;
454 case utf8: t = _atoms[utf8_string]; break;
455 default: assert(False); return False; // unhandled StringType
456 }
457
458 unsigned char *value;
459 unsigned long elements = (unsigned) -1;
460 if (!getValue(win, _atoms[atom], t, elements, &value, 8) || elements < 1)
461 return False;
462
463 std::string s(reinterpret_cast<char *>(value), elements);
464 delete [] value;
465
466 std::string::const_iterator it = s.begin(), end = s.end();
467 unsigned long num = 0;
468 while(num < nelements) {
469 std::string::const_iterator tmp = it; // current string.begin()
470 it = std::find(tmp, end, '\0'); // look for null between tmp and end
471 strings.push_back(std::string(tmp, it)); // s[tmp:it)
472 ++num;
473 if (it == end) break;
474 ++it;
475 if (it == end) break;
476 }
477
478 nelements = num;
479
480 return True;
481 }
482
483
484 /*
485 * Removes a property entirely from a window.
486 */
487 void XAtom::eraseValue(Window win, Atoms atom) const {
488 assert(atom >= 0 && atom < NUM_ATOMS);
489 XDeleteProperty(_display, win, _atoms[atom]);
490 }
491
492
493 void XAtom::sendClientMessage(Window target, Atoms type, Window about,
494 long data, long data1, long data2,
495 long data3, long data4) const {
496 assert(atom >= 0 && atom < NUM_ATOMS);
497 assert(target != None);
498
499 XEvent e;
500 e.xclient.type = ClientMessage;
501 e.xclient.format = 32;
502 e.xclient.message_type = _atoms[type];
503 e.xclient.window = about;
504 e.xclient.data.l[0] = data;
505 e.xclient.data.l[1] = data1;
506 e.xclient.data.l[2] = data2;
507 e.xclient.data.l[3] = data3;
508 e.xclient.data.l[4] = data4;
509
510 XSendEvent(_display, target, False,
511 SubstructureRedirectMask | SubstructureNotifyMask,
512 &e);
513 }
514
515 }
This page took 0.055248 seconds and 4 git commands to generate.