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.
27 XAtom::XAtom(const XDisplay *display) {
28 _display = display->_display;
30 wm_colormap_windows = getAtom("WM_COLORMAP_WINDOWS");
31 wm_protocols = getAtom("WM_PROTOCOLS");
32 wm_state = getAtom("WM_STATE");
33 wm_change_state = getAtom("WM_CHANGE_STATE");
34 wm_delete_window = getAtom("WM_DELETE_WINDOW");
35 wm_take_focus = getAtom("WM_TAKE_FOCUS");
36 motif_wm_hints = getAtom("_MOTIF_WM_HINTS");
37 openbox_hints = getAtom("_BLACKBOX_HINTS");
38 openbox_attributes = getAtom("_BLACKBOX_ATTRIBUTES");
39 openbox_change_attributes = getAtom("_BLACKBOX_CHANGE_ATTRIBUTES");
41 openbox_structure_messages = getAtom("_BLACKBOX_STRUCTURE_MESSAGES");
42 openbox_notify_startup = getAtom("_BLACKBOX_NOTIFY_STARTUP");
43 openbox_notify_window_add = getAtom("_BLACKBOX_NOTIFY_WINDOW_ADD");
44 openbox_notify_window_del = getAtom("_BLACKBOX_NOTIFY_WINDOW_DEL");
45 openbox_notify_current_workspace =
46 getAtom("_BLACKBOX_NOTIFY_CURRENT_WORKSPACE");
47 openbox_notify_workspace_count = getAtom("_BLACKBOX_NOTIFY_WORKSPACE_COUNT");
48 openbox_notify_window_focus = getAtom("_BLACKBOX_NOTIFY_WINDOW_FOCUS");
49 openbox_notify_window_raise = getAtom("_BLACKBOX_NOTIFY_WINDOW_RAISE");
50 openbox_notify_window_lower = getAtom("_BLACKBOX_NOTIFY_WINDOW_LOWER");
52 openbox_change_workspace = getAtom("_BLACKBOX_CHANGE_WORKSPACE");
53 openbox_change_window_focus = getAtom("_BLACKBOX_CHANGE_WINDOW_FOCUS");
54 openbox_cycle_window_focus = getAtom("_BLACKBOX_CYCLE_WINDOW_FOCUS");
56 net_supported = getAtom("_NET_SUPPORTED");
57 net_client_list = getAtom("_NET_CLIENT_LIST");
58 net_client_list_stacking = getAtom("_NET_CLIENT_LIST_STACKING");
59 net_number_of_desktops = getAtom("_NET_NUMBER_OF_DESKTOPS");
60 net_desktop_geometry = getAtom("_NET_DESKTOP_GEOMETRY");
61 net_desktop_viewport = getAtom("_NET_DESKTOP_VIEWPORT");
62 net_current_desktop = getAtom("_NET_CURRENT_DESKTOP");
63 net_desktop_names = getAtom("_NET_DESKTOP_NAMES");
64 net_active_window = getAtom("_NET_ACTIVE_WINDOW");
65 net_workarea = getAtom("_NET_WORKAREA");
66 net_supporting_wm_check = getAtom("_NET_SUPPORTING_WM_CHECK");
67 net_virtual_roots = getAtom("_NET_VIRTUAL_ROOTS");
69 net_close_window = getAtom("_NET_CLOSE_WINDOW");
70 net_wm_moveresize = getAtom("_NET_WM_MOVERESIZE");
72 net_properties = getAtom("_NET_PROPERTIES");
73 net_wm_name = getAtom("_NET_WM_NAME");
74 net_wm_desktop = getAtom("_NET_WM_DESKTOP");
75 net_wm_window_type = getAtom("_NET_WM_WINDOW_TYPE");
76 net_wm_state = getAtom("_NET_WM_STATE");
77 net_wm_strut = getAtom("_NET_WM_STRUT");
78 net_wm_icon_geometry = getAtom("_NET_WM_ICON_GEOMETRY");
79 net_wm_icon = getAtom("_NET_WM_ICON");
80 net_wm_pid = getAtom("_NET_WM_PID");
81 net_wm_handled_icons = getAtom("_NET_WM_HANDLED_ICONS");
83 net_wm_ping = getAtom("_NET_WM_PING");
85 for (int s = 0, c = display->screenCount(); s < c; ++s)
86 setSupported(display->screen(s));
91 * clean up the class' members
94 while (!_support_windows.empty()) {
95 // make sure we aren't fucking with this somewhere
96 ASSERT(_support_windows.back() != None);
97 XDestroyWindow(_display, _support_windows.back());
98 _support_windows.pop_back();
104 * Returns an atom from the Xserver, creating it if necessary.
106 Atom XAtom::getAtom(const char *name) const {
107 return XInternAtom(_display, name, False);
112 * Sets which atoms are supported for NETWM, by Openbox, on the root window.
114 void XAtom::setSupported(const XScreen *screen) {
115 // create the netwm support window
116 Window w = XCreateSimpleWindow(_display, screen->rootWindow(),
117 0, 0, 1, 1, 0, 0, 0);
119 _support_windows.push_back(w);
121 // we don't support any yet..
126 * Internal setValue used by all typed setValue functions.
127 * Sets a window property on a window, optionally appending to the existing
130 void XAtom::setValue(Window win, Atom atom, Atom type, unsigned char* data,
131 int size, int nelements, bool append) const {
132 ASSERT(win != None); ASSERT(atom != None); ASSERT(type != None);
133 ASSERT(data != (unsigned char *) 0);
134 ASSERT(size == 8 || size == 16 || size == 32);
135 ASSERT(nelements > 0);
136 XChangeProperty(_display, win, atom, type, size,
137 (append ? PropModeAppend : PropModeReplace),
143 * Set a 32-bit CARDINAL property value on a window.
145 void XAtom::setCardValue(Window win, Atom atom, long value) const {
146 setValue(win, atom, XA_CARDINAL, reinterpret_cast<unsigned char*>(&value),
152 * Set an Atom property value on a window.
154 void XAtom::setAtomValue(Window win, Atom atom, Atom value) const {
155 setValue(win, atom, XA_ATOM, reinterpret_cast<unsigned char*>(&value),
161 * Set a Window property value on a window.
163 void XAtom::setWindowValue(Window win, Atom atom, Window value) const {
164 setValue(win, atom, XA_WINDOW, reinterpret_cast<unsigned char*>(&value),
170 * Set a Pixmap property value on a window.
172 void XAtom::setPixmapValue(Window win, Atom atom, Pixmap value) const {
173 setValue(win, atom, XA_PIXMAP, reinterpret_cast<unsigned char*>(&value),
179 * Set a string property value on a window.
181 void XAtom::setStringValue(Window win, Atom atom, std::string &value) const {
182 setValue(win, atom, XA_STRING,
183 const_cast<unsigned char*>
184 (reinterpret_cast<const unsigned char*>(value.c_str())),
185 8, value.size(), false);
190 * Add elements to a 32-bit CARDINAL property value on a window.
192 void XAtom::addCardValue(Window win, Atom atom, long value) const {
193 setValue(win, atom, XA_CARDINAL, reinterpret_cast<unsigned char*>(&value),
199 * Add elements to an Atom property value on a window.
201 void XAtom::addAtomValue(Window win, Atom atom, Atom value) const {
202 setValue(win, atom, XA_ATOM, reinterpret_cast<unsigned char*>(&value),
208 * Add elements to a Window property value on a window.
210 void XAtom::addWindowValue(Window win, Atom atom, Window value) const {
211 setValue(win, atom, XA_WINDOW, reinterpret_cast<unsigned char*>(&value),
217 * Add elements to a Pixmap property value on a window.
219 void XAtom::addPixmapValue(Window win, Atom atom, Pixmap value) const {
220 setValue(win, atom, XA_PIXMAP, reinterpret_cast<unsigned char*>(&value),
226 * Add characters to a string property value on a window.
228 void XAtom::addStringValue(Window win, Atom atom, std::string &value) const {
229 setValue(win, atom, XA_STRING,
230 const_cast<unsigned char*>
231 (reinterpret_cast<const unsigned char *>
233 8, value.size(), true);
238 * Internal getValue function used by all of the typed getValue functions.
239 * Gets an property's value from a window.
240 * Returns true if the property was successfully retrieved; false if the
241 * property did not exist on the window, or has a different type/size format
242 * than the user tried to retrieve.
244 bool XAtom::getValue(Window win, Atom atom, Atom type, unsigned long *nelements,
245 unsigned char **value, int size) const {
246 unsigned char *c_val; // value alloc'd with c malloc
249 unsigned long ret_bytes;
250 XGetWindowProperty(_display, win, atom, 0l, 1l, False, AnyPropertyType,
251 &ret_type, &ret_size, nelements, &ret_bytes,
252 &c_val); // try get the first element
253 if (ret_type == None)
254 // the property does not exist on the window
256 if (ret_type != type || ret_size != size) {
257 // wrong data in property
261 // the data is correct, now, is there more than 1 element?
262 if (ret_bytes == 0) {
263 // we got the whole property's value
264 *value = new unsigned char[*nelements * size/8 + 1];
265 memcpy(*value, c_val, *nelements * size/8 + 1);
269 // get the entire property since it is larger than one long
271 // the number of longs that need to be retreived to get the property's entire
272 // value. The last + 1 is the first long that we retrieved above.
273 const int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
274 XGetWindowProperty(_display, win, atom, 0l, remain, False, type, &ret_type,
275 &ret_size, nelements, &ret_bytes, &c_val);
276 ASSERT(ret_bytes == 0);
277 *value = new unsigned char[*nelements * size/8 + 1];
278 memcpy(*value, c_val, *nelements * size/8 + 1);
285 * Gets a 32-bit Cardinal property's value from a window.
287 bool XAtom::getCardValue(Window win, Atom atom, unsigned long *nelements,
288 long **value) const {
289 return XAtom::getValue(win, atom, XA_CARDINAL, nelements,
290 reinterpret_cast<unsigned char **>(value), 32);
295 * Gets an Atom property's value from a window.
297 bool XAtom::getAtomValue(Window win, Atom atom, unsigned long *nelements,
298 Atom **value) const {
299 return XAtom::getValue(win, atom, XA_ATOM, nelements,
300 reinterpret_cast<unsigned char **>(value), 32);
305 * Gets an Window property's value from a window.
307 bool XAtom::getWindowValue(Window win, Atom atom, unsigned long *nelements,
308 Window **value) const {
309 return XAtom::getValue(win, atom, XA_WINDOW, nelements,
310 reinterpret_cast<unsigned char **>(value), 32);
315 * Gets an Pixmap property's value from a window.
317 bool XAtom::getPixmapValue(Window win, Atom atom, unsigned long *nelements,
318 Pixmap **value) const {
319 return XAtom::getValue(win, atom, XA_PIXMAP, nelements,
320 reinterpret_cast<unsigned char **>(value), 32);
325 * Gets an string property's value from a window.
327 bool XAtom::getStringValue(Window win, Atom atom, unsigned long *nelements,
328 std::string &value) const {
330 bool ret = XAtom::getValue(win, atom, XA_STRING, nelements, &data, 8);
332 value = reinterpret_cast<char*>(data);
338 * Removes a property entirely from a window.
340 void XAtom::eraseValue(Window win, Atom atom) const {
341 XDeleteProperty(_display, win, atom);