1 // XAtom.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Jansens (xor at orodu.net)
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
22 #include "../config.h"
30 XAtom::XAtom(Display
*d
) {
33 // make sure asserts fire if there is a problem
34 memset(_atoms
, 0, sizeof(_atoms
));
36 _atoms
[cardinal
] = XA_CARDINAL
;
37 _atoms
[window
] = XA_WINDOW
;
38 _atoms
[pixmap
] = XA_PIXMAP
;
39 _atoms
[atom
] = XA_ATOM
;
40 _atoms
[string
] = XA_STRING
;
41 _atoms
[utf8_string
] = create("UTF8_STRING");
44 _atoms
[blackbox_pid
] = create("_BLACKBOX_PID");
47 _atoms
[wm_colormap_windows
] = create("WM_COLORMAP_WINDOWS");
48 _atoms
[wm_protocols
] = create("WM_PROTOCOLS");
49 _atoms
[wm_state
] = create("WM_STATE");
50 _atoms
[wm_change_state
] = create("WM_CHANGE_STATE");
51 _atoms
[wm_delete_window
] = create("WM_DELETE_WINDOW");
52 _atoms
[wm_take_focus
] = create("WM_TAKE_FOCUS");
53 _atoms
[wm_name
] = create("WM_NAME");
54 _atoms
[wm_icon_name
] = create("WM_ICON_NAME");
55 _atoms
[wm_class
] = create("WM_CLASS");
56 _atoms
[motif_wm_hints
] = create("_MOTIF_WM_HINTS");
57 _atoms
[blackbox_hints
] = create("_BLACKBOX_HINTS");
58 _atoms
[blackbox_attributes
] = create("_BLACKBOX_ATTRIBUTES");
59 _atoms
[blackbox_change_attributes
] = create("_BLACKBOX_CHANGE_ATTRIBUTES");
60 _atoms
[blackbox_structure_messages
] = create("_BLACKBOX_STRUCTURE_MESSAGES");
61 _atoms
[blackbox_notify_startup
] = create("_BLACKBOX_NOTIFY_STARTUP");
62 _atoms
[blackbox_notify_window_add
] = create("_BLACKBOX_NOTIFY_WINDOW_ADD");
63 _atoms
[blackbox_notify_window_del
] = create("_BLACKBOX_NOTIFY_WINDOW_DEL");
64 _atoms
[blackbox_notify_current_workspace
] =
65 create("_BLACKBOX_NOTIFY_CURRENT_WORKSPACE");
66 _atoms
[blackbox_notify_workspace_count
] =
67 create("_BLACKBOX_NOTIFY_WORKSPACE_COUNT");
68 _atoms
[blackbox_notify_window_focus
] =
69 create("_BLACKBOX_NOTIFY_WINDOW_FOCUS");
70 _atoms
[blackbox_notify_window_raise
] =
71 create("_BLACKBOX_NOTIFY_WINDOW_RAISE");
72 _atoms
[blackbox_notify_window_lower
] =
73 create("_BLACKBOX_NOTIFY_WINDOW_LOWER");
75 _atoms
[blackbox_change_workspace
] = create("_BLACKBOX_CHANGE_WORKSPACE");
76 _atoms
[blackbox_change_window_focus
] =
77 create("_BLACKBOX_CHANGE_WINDOW_FOCUS");
78 _atoms
[blackbox_cycle_window_focus
] = create("_BLACKBOX_CYCLE_WINDOW_FOCUS");
80 _atoms
[net_supported
] = create("_NET_SUPPORTED");
81 _atoms
[net_client_list
] = create("_NET_CLIENT_LIST");
82 _atoms
[net_client_list_stacking
] = create("_NET_CLIENT_LIST_STACKING");
83 _atoms
[net_number_of_desktops
] = create("_NET_NUMBER_OF_DESKTOPS");
84 _atoms
[net_desktop_geometry
] = create("_NET_DESKTOP_GEOMETRY");
85 _atoms
[net_desktop_viewport
] = create("_NET_DESKTOP_VIEWPORT");
86 _atoms
[net_current_desktop
] = create("_NET_CURRENT_DESKTOP");
87 _atoms
[net_desktop_names
] = create("_NET_DESKTOP_NAMES");
88 _atoms
[net_active_window
] = create("_NET_ACTIVE_WINDOW");
89 _atoms
[net_workarea
] = create("_NET_WORKAREA");
90 _atoms
[net_supporting_wm_check
] = create("_NET_SUPPORTING_WM_CHECK");
91 // _atoms[net_virtual_roots] = create("_NET_VIRTUAL_ROOTS");
93 _atoms
[net_close_window
] = create("_NET_CLOSE_WINDOW");
94 _atoms
[net_wm_moveresize
] = create("_NET_WM_MOVERESIZE");
96 // _atoms[net_properties] = create("_NET_PROPERTIES");
97 _atoms
[net_wm_name
] = create("_NET_WM_NAME");
98 _atoms
[net_wm_visible_name
] = create("_NET_WM_VISIBLE_NAME");
99 _atoms
[net_wm_icon_name
] = create("_NET_WM_ICON_NAME");
100 _atoms
[net_wm_visible_icon_name
] = create("_NET_WM_VISIBLE_ICON_NAME");
101 _atoms
[net_wm_desktop
] = create("_NET_WM_DESKTOP");
102 _atoms
[net_wm_window_type
] = create("_NET_WM_WINDOW_TYPE");
103 _atoms
[net_wm_state
] = create("_NET_WM_STATE");
104 _atoms
[net_wm_strut
] = create("_NET_WM_STRUT");
105 // _atoms[net_wm_icon_geometry] = create("_NET_WM_ICON_GEOMETRY");
106 // _atoms[net_wm_icon] = create("_NET_WM_ICON");
107 // _atoms[net_wm_pid] = create("_NET_WM_PID");
108 // _atoms[net_wm_handled_icons] = create("_NET_WM_HANDLED_ICONS");
109 _atoms
[net_wm_allowed_actions
] = create("_NET_WM_ALLOWED_ACTIONS");
111 // _atoms[net_wm_ping] = create("_NET_WM_PING");
113 _atoms
[net_wm_window_type_desktop
] = create("_NET_WM_WINDOW_TYPE_DESKTOP");
114 _atoms
[net_wm_window_type_dock
] = create("_NET_WM_WINDOW_TYPE_DOCK");
115 _atoms
[net_wm_window_type_toolbar
] = create("_NET_WM_WINDOW_TYPE_TOOLBAR");
116 _atoms
[net_wm_window_type_menu
] = create("_NET_WM_WINDOW_TYPE_MENU");
117 _atoms
[net_wm_window_type_utility
] = create("_NET_WM_WINDOW_TYPE_UTILITY");
118 _atoms
[net_wm_window_type_splash
] = create("_NET_WM_WINDOW_TYPE_SPLASH");
119 _atoms
[net_wm_window_type_dialog
] = create("_NET_WM_WINDOW_TYPE_DIALOG");
120 _atoms
[net_wm_window_type_normal
] = create("_NET_WM_WINDOW_TYPE_NORMAL");
122 _atoms
[net_wm_moveresize_size_topleft
] =
123 create("_NET_WM_MOVERESIZE_SIZE_TOPLEFT");
124 _atoms
[net_wm_moveresize_size_topright
] =
125 create("_NET_WM_MOVERESIZE_SIZE_TOPRIGHT");
126 _atoms
[net_wm_moveresize_size_bottomleft
] =
127 create("_NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT");
128 _atoms
[net_wm_moveresize_size_bottomright
] =
129 create("_NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT");
130 _atoms
[net_wm_moveresize_move
] =
131 create("_NET_WM_MOVERESIZE_MOVE");
133 _atoms
[net_wm_action_move
] = create("_NET_WM_ACTION_MOVE");
134 _atoms
[net_wm_action_resize
] = create("_NET_WM_ACTION_RESIZE");
135 _atoms
[net_wm_action_shade
] = create("_NET_WM_ACTION_SHADE");
136 _atoms
[net_wm_action_maximize_horz
] = create("_NET_WM_ACTION_MAXIMIZE_HORZ");
137 _atoms
[net_wm_action_maximize_vert
] = create("_NET_WM_ACTION_MAXIMIZE_VERT");
138 _atoms
[net_wm_action_change_desktop
] =
139 create("_NET_WM_ACTION_CHANGE_DESKTOP");
140 _atoms
[net_wm_action_close
] = create("_NET_WM_ACTION_CLOSE");
142 _atoms
[net_wm_state_modal
] = create("_NET_WM_STATE_MODAL");
143 _atoms
[net_wm_state_maximized_vert
] = create("_NET_WM_STATE_MAXIMIZED_VERT");
144 _atoms
[net_wm_state_maximized_horz
] = create("_NET_WM_STATE_MAXIMIZED_HORZ");
145 _atoms
[net_wm_state_shaded
] = create("_NET_WM_STATE_SHADED");
146 _atoms
[net_wm_state_skip_taskbar
] = create("_NET_WM_STATE_SKIP_TASKBAR");
147 _atoms
[net_wm_state_skip_pager
] = create("_NET_WM_STATE_SKIP_PAGER");
148 _atoms
[net_wm_state_hidden
] = create("_NET_WM_STATE_HIDDEN");
149 _atoms
[net_wm_state_fullscreen
] = create("_NET_WM_STATE_FULLSCREEN");
151 _atoms
[kde_net_system_tray_windows
] = create("_KDE_NET_SYSTEM_TRAY_WINDOWS");
152 _atoms
[kde_net_wm_system_tray_window_for
] =
153 create("_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR");
158 * clean up the class' members
161 while (!_support_windows
.empty()) {
162 // make sure we aren't fucking with this somewhere
163 assert(_support_windows
.back() != None
);
164 XDestroyWindow(_display
, _support_windows
.back());
165 _support_windows
.pop_back();
171 * Returns an atom from the Xserver, creating it if necessary.
173 Atom
XAtom::create(const char *name
) const {
174 return XInternAtom(_display
, name
, False
);
179 * Sets which atoms are supported for NETWM, by Openbox, on the root window.
181 void XAtom::setSupported(const ScreenInfo
*screen
) {
182 Window root
= screen
->getRootWindow();
184 // create the netwm support window
185 Window w
= XCreateSimpleWindow(_display
, root
, 0, 0, 1, 1, 0, 0, 0);
187 _support_windows
.push_back(w
);
189 // set supporting window
190 setValue(root
, net_supporting_wm_check
, window
, w
);
192 //set properties on the supporting window
193 setValue(w
, net_wm_name
, utf8
, "Openbox");
194 setValue(w
, net_supporting_wm_check
, window
, w
);
196 // we don't support any yet..
200 _atoms
[net_current_desktop
],
201 _atoms
[net_number_of_desktops
],
202 _atoms
[net_desktop_geometry
],
203 _atoms
[net_desktop_viewport
],
204 _atoms
[net_active_window
],
205 _atoms
[net_workarea
],
206 _atoms
[net_client_list
],
207 _atoms
[net_client_list_stacking
],
208 _atoms
[net_desktop_names
],
209 _atoms
[net_close_window
],
211 _atoms
[net_wm_visible_name
],
212 _atoms
[net_wm_icon_name
],
213 _atoms
[net_wm_visible_icon_name
],
214 _atoms
[net_wm_desktop
],
215 _atoms
[net_wm_strut
],
216 _atoms
[net_wm_window_type
],
217 _atoms
[net_wm_window_type_desktop
],
218 _atoms
[net_wm_window_type_dock
],
219 _atoms
[net_wm_window_type_toolbar
],
220 _atoms
[net_wm_window_type_menu
],
221 _atoms
[net_wm_window_type_utility
],
222 _atoms
[net_wm_window_type_splash
],
223 _atoms
[net_wm_window_type_dialog
],
224 _atoms
[net_wm_window_type_normal
],
225 _atoms
[net_wm_moveresize
],
226 _atoms
[net_wm_moveresize_size_topleft
],
227 _atoms
[net_wm_moveresize_size_topright
],
228 _atoms
[net_wm_moveresize_size_bottomleft
],
229 _atoms
[net_wm_moveresize_size_bottomright
],
230 _atoms
[net_wm_moveresize_move
],
231 _atoms
[net_wm_allowed_actions
],
232 _atoms
[net_wm_action_move
],
233 _atoms
[net_wm_action_resize
],
234 _atoms
[net_wm_action_shade
],
235 _atoms
[net_wm_action_maximize_horz
],
236 _atoms
[net_wm_action_maximize_vert
],
237 _atoms
[net_wm_action_change_desktop
],
238 _atoms
[net_wm_action_close
],
239 _atoms
[net_wm_state
],
240 _atoms
[net_wm_state_modal
],
241 _atoms
[net_wm_state_maximized_vert
],
242 _atoms
[net_wm_state_maximized_horz
],
243 _atoms
[net_wm_state_shaded
],
244 _atoms
[net_wm_state_skip_taskbar
],
245 _atoms
[net_wm_state_skip_pager
],
246 _atoms
[net_wm_state_hidden
],
247 _atoms
[net_wm_state_fullscreen
],
249 const int num_supported
= sizeof(supported
)/sizeof(Atom
);
251 setValue(root
, net_supported
, atom
, supported
, num_supported
);
257 * Sets a window property on a window, optionally appending to the existing
260 void XAtom::setValue(Window win
, Atom atom
, Atom type
,
261 unsigned char* data
, int size
, int nelements
,
263 assert(win
!= None
); assert(atom
!= None
); assert(type
!= None
);
264 assert(nelements
== 0 || (nelements
> 0 && data
!= (unsigned char *) 0));
265 assert(size
== 8 || size
== 16 || size
== 32);
266 XChangeProperty(_display
, win
, atom
, type
, size
,
267 (append
? PropModeAppend
: PropModeReplace
),
273 * Set a 32-bit property value on a window.
275 void XAtom::setValue(Window win
, Atoms atom
, Atoms type
,
276 unsigned long value
) const {
277 assert(atom
>= 0 && atom
< NUM_ATOMS
);
278 assert(type
>= 0 && type
< NUM_ATOMS
);
279 setValue(win
, _atoms
[atom
], _atoms
[type
],
280 reinterpret_cast<unsigned char*>(&value
), 32, 1, False
);
285 * Set an array of 32-bit properties value on a window.
287 void XAtom::setValue(Window win
, Atoms atom
, Atoms type
,
288 unsigned long value
[], int elements
) const {
289 assert(atom
>= 0 && atom
< NUM_ATOMS
);
290 assert(type
>= 0 && type
< NUM_ATOMS
);
291 setValue(win
, _atoms
[atom
], _atoms
[type
],
292 reinterpret_cast<unsigned char*>(value
), 32, elements
, False
);
297 * Set an string property value on a window.
299 void XAtom::setValue(Window win
, Atoms atom
, StringType type
,
300 const std::string
&value
) const {
301 assert(atom
>= 0 && atom
< NUM_ATOMS
);
302 assert(type
>= 0 && type
< NUM_STRING_TYPE
);
306 case ansi
: t
= _atoms
[string
]; break;
307 case utf8
: t
= _atoms
[utf8_string
]; break;
308 default: assert(False
); return; // unhandled StringType
310 setValue(win
, _atoms
[atom
], t
,
311 reinterpret_cast<unsigned char *>(const_cast<char *>(value
.c_str())),
312 8, value
.size() + 1, False
); // add 1 to the size to include the null
317 * Set an array of string property values on a window.
319 void XAtom::setValue(Window win
, Atoms atom
, StringType type
,
320 const StringVect
&strings
) const {
321 assert(atom
>= 0 && atom
< NUM_ATOMS
);
322 assert(type
>= 0 && type
< NUM_STRING_TYPE
);
326 case ansi
: t
= _atoms
[string
]; break;
327 case utf8
: t
= _atoms
[utf8_string
]; break;
328 default: assert(False
); return; // unhandled StringType
333 StringVect::const_iterator it
= strings
.begin();
334 const StringVect::const_iterator end
= strings
.end();
335 for (; it
!= end
; ++it
)
338 setValue(win
, _atoms
[atom
], t
,
339 reinterpret_cast<unsigned char *>(const_cast<char *>(value
.c_str())),
340 8, value
.size(), False
);
345 * Internal getValue function used by all of the typed getValue functions.
346 * Gets an property's value from a window.
347 * Returns True if the property was successfully retrieved; False if the
348 * property did not exist on the window, or has a different type/size format
349 * than the user tried to retrieve.
351 bool XAtom::getValue(Window win
, Atom atom
, Atom type
,
352 unsigned long &nelements
, unsigned char **value
,
354 assert(win
!= None
); assert(atom
!= None
); assert(type
!= None
);
355 assert(size
== 8 || size
== 16 || size
== 32);
356 assert(nelements
> 0);
357 unsigned char *c_val
= 0; // value alloc'd with c malloc
360 unsigned long ret_bytes
;
362 const unsigned long maxread
= nelements
;
363 // try get the first element
364 result
= XGetWindowProperty(_display
, win
, atom
, 0l, 1l, False
,
365 AnyPropertyType
, &ret_type
, &ret_size
,
366 &nelements
, &ret_bytes
, &c_val
);
367 if (result
!= Success
|| ret_type
!= type
|| ret_size
!= size
||
369 // an error occured, the property does not exist on the window, or is empty,
370 // or the wrong data is in property for the request
371 if (c_val
) XFree(c_val
);
374 // the data is correct, now, is there more elements left?
375 if (ret_bytes
== 0 || maxread
<= nelements
) {
376 // we got the whole property's value
377 *value
= new unsigned char[nelements
* size
/8 + 1];
378 memcpy(*value
, c_val
, nelements
* size
/8 + 1);
382 // get the entire property since it is larger than one long
384 // the number of longs that need to be retreived to get the property's entire
385 // value. The last + 1 is the first long that we retrieved above.
386 int remain
= (ret_bytes
- 1)/sizeof(long) + 1 + 1;
387 if (remain
> size
/8 * (signed)maxread
) // dont get more than the max
388 remain
= size
/8 * (signed)maxread
;
389 result
= XGetWindowProperty(_display
, win
, atom
, 0l, remain
, False
, type
,
390 &ret_type
, &ret_size
, &nelements
, &ret_bytes
,
392 assert(result
== Success
);
393 assert(ret_bytes
== 0);
394 *value
= new unsigned char[nelements
* size
/8 + 1];
395 memcpy(*value
, c_val
, nelements
* size
/8 + 1);
402 * Gets a 32-bit property's value from a window.
404 bool XAtom::getValue(Window win
, Atoms atom
, Atoms type
,
405 unsigned long &nelements
,
406 unsigned long **value
) const {
407 assert(atom
>= 0 && atom
< NUM_ATOMS
);
408 assert(type
>= 0 && type
< NUM_ATOMS
);
409 return getValue(win
, _atoms
[atom
], _atoms
[type
], nelements
,
410 reinterpret_cast<unsigned char **>(value
), 32);
415 * Gets a single 32-bit property's value from a window.
417 bool XAtom::getValue(Window win
, Atoms atom
, Atoms type
,
418 unsigned long &value
) const {
419 assert(atom
>= 0 && atom
< NUM_ATOMS
);
420 assert(type
>= 0 && type
< NUM_ATOMS
);
422 unsigned long num
= 1;
423 if (! getValue(win
, _atoms
[atom
], _atoms
[type
], num
,
424 reinterpret_cast<unsigned char **>(&temp
), 32))
433 * Gets an string property's value from a window.
435 bool XAtom::getValue(Window win
, Atoms atom
, StringType type
,
436 std::string
&value
) const {
439 if (getValue(win
, atom
, type
, n
, s
)) {
447 bool XAtom::getValue(Window win
, Atoms atom
, StringType type
,
448 unsigned long &nelements
, StringVect
&strings
) const {
449 assert(atom
>= 0 && atom
< NUM_ATOMS
);
450 assert(type
>= 0 && type
< NUM_STRING_TYPE
);
451 assert(win
!= None
); assert(_atoms
[atom
] != None
);
452 assert(nelements
> 0);
456 case ansi
: t
= _atoms
[string
]; break;
457 case utf8
: t
= _atoms
[utf8_string
]; break;
458 default: assert(False
); return False
; // unhandled StringType
461 unsigned char *value
;
462 unsigned long elements
= (unsigned) -1;
463 if (!getValue(win
, _atoms
[atom
], t
, elements
, &value
, 8) || elements
< 1)
466 std::string
s(reinterpret_cast<char *>(value
), elements
);
469 std::string::const_iterator it
= s
.begin(), end
= s
.end();
470 unsigned long num
= 0;
471 while(num
< nelements
) {
472 std::string::const_iterator tmp
= it
; // current string.begin()
473 it
= std::find(tmp
, end
, '\0'); // look for null between tmp and end
474 strings
.push_back(std::string(tmp
, it
)); // s[tmp:it)
476 if (it
== end
) break;
478 if (it
== end
) break;
488 * Removes a property entirely from a window.
490 void XAtom::eraseValue(Window win
, Atoms atom
) const {
491 assert(atom
>= 0 && atom
< NUM_ATOMS
);
492 XDeleteProperty(_display
, win
, _atoms
[atom
]);
496 void XAtom::sendClientMessage(Window target
, Atoms type
, Window about
,
497 long data
, long data1
, long data2
,
499 assert(atom
>= 0 && atom
< NUM_ATOMS
);
500 assert(target
!= None
);
503 e
.xclient
.type
= ClientMessage
;
504 e
.xclient
.format
= 32;
505 e
.xclient
.message_type
= _atoms
[type
];
506 e
.xclient
.window
= about
;
507 e
.xclient
.data
.l
[0] = data
;
508 e
.xclient
.data
.l
[1] = data1
;
509 e
.xclient
.data
.l
[2] = data2
;
510 e
.xclient
.data
.l
[3] = data3
;
512 XSendEvent(_display
, target
, False
,
513 SubstructureRedirectMask
| SubstructureNotifyMask
,