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.
25 XAtom::XAtom(XDisplay
*display
) {
26 _display
= display
->_display
;
28 wm_colormap_windows
= getAtom("WM_COLORMAP_WINDOWS");
29 wm_protocols
= getAtom("WM_PROTOCOLS");
30 wm_state
= getAtom("WM_STATE");
31 wm_change_state
= getAtom("WM_CHANGE_STATE");
32 wm_delete_window
= getAtom("WM_DELETE_WINDOW");
33 wm_take_focus
= getAtom("WM_TAKE_FOCUS")
34 motif_wm_hints
= getAtom("_MOTIF_WM_HINTS");
35 openbox_hints
= getAtom("_BLACKBOX_HINTS");
36 openbox_attributes
= getAtom("_BLACKBOX_ATTRIBUTES");
37 openbox_change_attributes
= getAtom("_BLACKBOX_CHANGE_ATTRIBUTES");
39 openbox_structure_messages
= getAtom("_BLACKBOX_STRUCTURE_MESSAGES");
40 openbox_notify_startup
= getAtom("_BLACKBOX_NOTIFY_STARTUP");
41 openbox_notify_window_add
= getAtom("_BLACKBOX_NOTIFY_WINDOW_ADD");
42 openbox_notify_window_del
= getAtom("_BLACKBOX_NOTIFY_WINDOW_DEL");
43 openbox_notify_current_workspace
=
44 getAtom("_BLACKBOX_NOTIFY_CURRENT_WORKSPACE");
45 openbox_notify_workspace_count
= getAtom("_BLACKBOX_NOTIFY_WORKSPACE_COUNT");
46 openbox_notify_window_focus
= getAtom("_BLACKBOX_NOTIFY_WINDOW_FOCUS");
47 openbox_notify_window_raise
= getAtom("_BLACKBOX_NOTIFY_WINDOW_RAISE");
48 openbox_notify_window_lower
= getAtom("_BLACKBOX_NOTIFY_WINDOW_LOWER");
50 openbox_change_workspace
= getAtom("_BLACKBOX_CHANGE_WORKSPACE");
51 openbox_change_window_focus
= getAtom("_BLACKBOX_CHANGE_WINDOW_FOCUS");
52 openbox_cycle_window_focus
= getAtom("_BLACKBOX_CYCLE_WINDOW_FOCUS");
54 net_supported
= getAtom("_NET_SUPPORTED");
55 net_client_list
= getAtom("_NET_CLIENT_LIST");
56 net_client_list_stacking
= getAtom("_NET_CLIENT_LIST_STACKING");
57 net_number_of_desktops
= getAtom("_NET_NUMBER_OF_DESKTOPS");
58 net_desktop_geometry
= getAtom("_NET_DESKTOP_GEOMETRY");
59 net_desktop_viewport
= getAtom("_NET_DESKTOP_VIEWPORT");
60 net_current_desktop
= getAtom("_NET_CURRENT_DESKTOP");
61 net_desktop_names
= getAtom("_NET_DESKTOP_NAMES");
62 net_active_window
= getAtom("_NET_ACTIVE_WINDOW");
63 net_workarea
= getAtom("_NET_WORKAREA");
64 net_supporting_wm_check
= getAtom("_NET_SUPPORTING_WM_CHECK");
65 net_virtual_roots
= getAtom("_NET_VIRTUAL_ROOTS");
67 net_close_window
= getAtom("_NET_CLOSE_WINDOW");
68 net_wm_moveresize
= getAtom("_NET_WM_MOVERESIZE");
70 net_properties
= getAtom("_NET_PROPERTIES");
71 net_wm_name
= getAtom("_NET_WM_NAME");
72 net_wm_desktop
= getAtom("_NET_WM_DESKTOP");
73 net_wm_window_type
= getAtom("_NET_WM_WINDOW_TYPE");
74 net_wm_state
= getAtom("_NET_WM_STATE");
75 net_wm_strut
= getAtom("_NET_WM_STRUT");
76 net_wm_icon_geometry
= getAtom("_NET_WM_ICON_GEOMETRY");
77 net_wm_icon
= getAtom("_NET_WM_ICON");
78 net_wm_pid
= getAtom("_NET_WM_PID");
79 net_wm_handled_icons
= getAtom("_NET_WM_HANDLED_ICONS");
81 net_wm_ping
= getAtom("_NET_WM_PING");
83 for (int s
= 0, c
= display
->screenCount(); s
< c
; ++s
)
84 setSupported(display
->screen(s
));
89 * clean up the class' members
92 while (!_support_windows
.empty()) {
93 Window w
= _support_windows
.pop_back();
94 ASSERT(w
!= None
); // make sure we aren't fucking with this somewhere
95 XDestroyWindow(_display
, w
);
101 * Returns an atom from the Xserver, creating it if necessary.
103 Atom
XAtom::getAtom(const char *name
) const {
104 return XInternAtom(_display
, name
, False
);
109 * Sets which atoms are supported for NETWM, by Openbox, on the root window.
111 void XAtom::setSupported(const XScreen
*screen
) {
112 // create the netwm support window
113 Window w
= XCreateSimpleWindow(_display
, screen
->rootWindow(),
116 _support_windows
.push_back(w
);
118 // we don't support any yet..
123 * Internal setValue used by all typed setValue functions.
124 * Sets a window property on a window, optionally appending to the existing
127 void XAtom::setValue(Window win
, Atom atom
, Atom type
, unsigned char* data
,
128 int size
, int nelements
, bool append
) {
129 ASSERT(win
!= None
); ASSERT(atom
!= None
); ASSERT(type
!= None
);
130 ASSERT(data
!= (unsigned char *) 0);
131 ASSERT(size
== 8 || size
== 16 || size
== 32);
132 ASSERT(nelements
> 0);
133 XChangeProperty(_display
, win
, atom
, type
, size
,
134 (append
? PropModeAppend
: PropModeReplace
),
140 * Set a 32-bit CARDINAL property value on a window.
142 void XAtom::setValue(Window win
, Atom atom
, long value
) const {
143 setValue(win
, atom
, XA_CARDINAL
, static_cast<unsigned char*>(&value
),
144 sizeof(long), 1, false);
149 * Set an Atom property value on a window.
151 void XAtom::setValue(Window win
, Atom atom
, Atom value
) {
152 setValue(win
, atom
, XA_ATOM
, static_cast<unsigned char*>(&value
),
153 sizeof(Atom
), 1, false);
158 * Set a Window property value on a window.
160 void XAtom::setValue(Window win
, Atom atom
, Window value
) {
161 setValue(win
, atom
, XA_WINDOW
, static_cast<unsigned char*>(&value
),
162 sizeof(Window
), 1, false);
167 * Set a Pixmap property value on a window.
169 void XAtom::setValue(Window win
, Atom atom
, Pixmap value
) {
170 setValue(win
, atom
, XA_PIXMAP
, static_cast<unsigned char*>(&value
),
171 sizeof(Pixmap
), 1, false);
176 * Set a string property value on a window.
178 void XAtom::setValue(Window win
, Atom atom
, std::string
&value
) {
179 setValue(win
, atom
, XA_STRING
, static_cast<unsigned char*>(value
.c_str()),
180 8, value
.size(), false);
185 * Add elements to a 32-bit CARDINAL property value on a window.
187 void XAtom::addValue(Window win
, Atom atom
, long value
) const {
188 setValue(win
, atom
, XA_CARDINAL
, static_cast<unsigned char*>(&value
),
189 sizeof(long), 1, true);
194 * Add elements to an Atom property value on a window.
196 void XAtom::addValue(Window win
, Atom atom
, Atom value
) const {
197 setValue(win
, atom
, XA_ATOM
, static_cast<unsigned char*>(&value
),
198 sizeof(Atom
), 1, true);
203 * Add elements to a Window property value on a window.
205 void XAtom::addValue(Window win
, Atom atom
, Window value
) const {
206 setValue(win
, atom
, XA_WINDOW
, static_cast<unsigned char*>(&value
),
207 sizeof(Window
), 1, true);
212 * Add elements to a Pixmap property value on a window.
214 void XAtom::addValue(Window win
, Atom atom
, Pixmap value
) const {
215 setValue(win
, atom
, XA_PIXMAP
, static_cast<unsigned char*>(&value
),
216 sizeof(Pixmap
), 1, true);
221 * Add characters to a string property value on a window.
223 void XAtom::addValue(Window win
, Atom atom
, std::string
&value
) const {
224 setValue(win
, atom
, XA_STRING
, static_cast<unsigned char*>(value
.c_str()),
225 8, value
.size(), true);
230 * Internal getValue function used by all of the typed getValue functions.
231 * Gets an property's value from a window.
232 * Returns true if the property was successfully retrieved; false if the
233 * property did not exist on the window, or has a different type/size format
234 * than the user tried to retrieve.
236 bool XAtom::getValue(Window win
, Atom atom
, Atom type
, unsigned long *nelements
,
237 unsigned char **value
, int size
) const {
238 unsigned char *c_val
; // value alloc'd with c malloc
241 unsigned long ret_bytes
;
242 XGetWindowProperty(_display
, win
, atom
, 0l, 1l, False
, AnyPropertyType
,
243 &ret_type
, &ret_size
, nelements
, &ret_bytes
,
244 &c_val
); // try get the first element
245 if (ret_type
== None
)
246 // the property does not exist on the window
248 if (ret_type
!= type
|| ret_size
!= size
) {
249 // wrong data in property
253 // the data is correct, now, is there more than 1 element?
254 if (ret_bytes
== 0) {
255 // we got the whole property's value
256 *value
= new unsigned char[*nelements
* size
/8 + 1];
257 memcpy(*value
, c_val
, *nelements
* size
/8 + 1);
261 // get the entire property since it is larger than one long
263 // the number of longs that need to be retreived to get the property's entire
264 // value. The last + 1 is the first long that we retrieved above.
265 const int remain
= (ret_bytes
- 1)/sizeof(long) + 1 + 1;
266 XGetWindowProperty(_display
, win
, atom
, 0l, remain
, False
, type
, &ret_type
,
267 &ret_size
, nelements
, &ret_bytes
, &c_val
);
268 ASSERT(ret_bytes
== 0);
269 *value
= new unsigned char[*nelements
* size
/8 + 1];
270 memcpy(*value
, c_val
, *nelements
* size
/8 + 1);
277 * Gets a 32-bit Cardinal property's value from a window.
279 bool XAtom::getValue(Window win
, Atom atom
, unsigned long *nelements
,
280 long **value
) const {
281 return XAtom::getValue(win
, atom
, XA_CARDINAL
, nelements
,
282 static_cast<unsigned char **>(value
), sizeof(long));
287 * Gets an Atom property's value from a window.
289 bool XAtom::getValue(Window win
, Atom atom
, unsigned long *nelements
,
290 Atom
**value
) const {
291 return XAtom::getValue(win
, atom
, XA_ATOM
, nelements
,
292 static_cast<unsigned char **>(value
), sizeof(Atom
));
297 * Gets an Window property's value from a window.
299 bool XAtom::getValue(Window win
, Atom atom
, unsigned long *nelements
,
300 Window
**value
) const {
301 return XAtom::getValue(win
, atom
, XA_WINDOW
, nelements
,
302 static_cast<unsigned char **>(value
), sizeof(Window
));
307 * Gets an Pixmap property's value from a window.
309 bool XAtom::getValue(Window win
, Atom atom
, unsigned long *nelements
,
310 Pixmap
**value
) const {
311 return XAtom::getValue(win
, atom
, XA_PIXMAP
, nelements
,
312 static_cast<unsigned char **>(value
), sizeof(Pixmap
));
317 * Gets an string property's value from a window.
319 bool XAtom::getValue(Window win
, Atom atom
, unsigned long *nelements
,
320 std::string
&value
) const {
322 bool ret
= XAtom::getValue(win
, atom
, XA_STRING
, nelements
, &data
, 8);
330 * Removes a property entirely from a window.
332 void XAtom::eraseValue(Window win
, Atom atom
) const {
333 XDeleteProperty(_display
, win
, atom
);