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(Openbox &ob) {
28 _display = ob.getXDisplay();
31 openbox_pid = getAtom("_BLACKBOX_PID");
34 wm_colormap_windows = getAtom("WM_COLORMAP_WINDOWS");
35 wm_protocols = getAtom("WM_PROTOCOLS");
36 wm_state = getAtom("WM_STATE");
37 wm_change_state = getAtom("WM_CHANGE_STATE");
38 wm_delete_window = getAtom("WM_DELETE_WINDOW");
39 wm_take_focus = getAtom("WM_TAKE_FOCUS");
40 motif_wm_hints = getAtom("_MOTIF_WM_HINTS");
41 openbox_hints = getAtom("_BLACKBOX_HINTS");
42 openbox_attributes = getAtom("_BLACKBOX_ATTRIBUTES");
43 openbox_change_attributes = getAtom("_BLACKBOX_CHANGE_ATTRIBUTES");
45 openbox_structure_messages = getAtom("_BLACKBOX_STRUCTURE_MESSAGES");
46 openbox_notify_startup = getAtom("_BLACKBOX_NOTIFY_STARTUP");
47 openbox_notify_window_add = getAtom("_BLACKBOX_NOTIFY_WINDOW_ADD");
48 openbox_notify_window_del = getAtom("_BLACKBOX_NOTIFY_WINDOW_DEL");
49 openbox_notify_current_workspace =
50 getAtom("_BLACKBOX_NOTIFY_CURRENT_WORKSPACE");
51 openbox_notify_workspace_count = getAtom("_BLACKBOX_NOTIFY_WORKSPACE_COUNT");
52 openbox_notify_window_focus = getAtom("_BLACKBOX_NOTIFY_WINDOW_FOCUS");
53 openbox_notify_window_raise = getAtom("_BLACKBOX_NOTIFY_WINDOW_RAISE");
54 openbox_notify_window_lower = getAtom("_BLACKBOX_NOTIFY_WINDOW_LOWER");
56 openbox_change_workspace = getAtom("_BLACKBOX_CHANGE_WORKSPACE");
57 openbox_change_window_focus = getAtom("_BLACKBOX_CHANGE_WINDOW_FOCUS");
58 openbox_cycle_window_focus = getAtom("_BLACKBOX_CYCLE_WINDOW_FOCUS");
60 net_supported = getAtom("_NET_SUPPORTED");
61 net_client_list = getAtom("_NET_CLIENT_LIST");
62 net_client_list_stacking = getAtom("_NET_CLIENT_LIST_STACKING");
63 net_number_of_desktops = getAtom("_NET_NUMBER_OF_DESKTOPS");
64 net_desktop_geometry = getAtom("_NET_DESKTOP_GEOMETRY");
65 net_desktop_viewport = getAtom("_NET_DESKTOP_VIEWPORT");
66 net_current_desktop = getAtom("_NET_CURRENT_DESKTOP");
67 net_desktop_names = getAtom("_NET_DESKTOP_NAMES");
68 net_active_window = getAtom("_NET_ACTIVE_WINDOW");
69 net_workarea = getAtom("_NET_WORKAREA");
70 net_supporting_wm_check = getAtom("_NET_SUPPORTING_WM_CHECK");
71 net_virtual_roots = getAtom("_NET_VIRTUAL_ROOTS");
73 net_close_window = getAtom("_NET_CLOSE_WINDOW");
74 net_wm_moveresize = getAtom("_NET_WM_MOVERESIZE");
76 net_properties = getAtom("_NET_PROPERTIES");
77 net_wm_name = getAtom("_NET_WM_NAME");
78 net_wm_desktop = getAtom("_NET_WM_DESKTOP");
79 net_wm_window_type = getAtom("_NET_WM_WINDOW_TYPE");
80 net_wm_state = getAtom("_NET_WM_STATE");
81 net_wm_strut = getAtom("_NET_WM_STRUT");
82 net_wm_icon_geometry = getAtom("_NET_WM_ICON_GEOMETRY");
83 net_wm_icon = getAtom("_NET_WM_ICON");
84 net_wm_pid = getAtom("_NET_WM_PID");
85 net_wm_handled_icons = getAtom("_NET_WM_HANDLED_ICONS");
87 net_wm_ping = getAtom("_NET_WM_PING");
89 for (unsigned int s = 0, c = ob.managedScreenCount(); s < c; ++s)
90 setSupported( static_cast<ScreenInfo*>(ob.getScreen(s)) );
95 * clean up the class' members
98 while (!_support_windows.empty()) {
99 // make sure we aren't fucking with this somewhere
100 ASSERT(_support_windows.back() != None);
101 XDestroyWindow(_display, _support_windows.back());
102 _support_windows.pop_back();
108 * Returns an atom from the Xserver, creating it if necessary.
110 Atom XAtom::getAtom(const char *name) const {
111 return XInternAtom(_display, name, False);
116 * Sets which atoms are supported for NETWM, by Openbox, on the root window.
118 void XAtom::setSupported(const ScreenInfo *screen) {
119 // create the netwm support window
120 Window w = XCreateSimpleWindow(_display, screen->getRootWindow(),
121 0, 0, 1, 1, 0, 0, 0);
123 _support_windows.push_back(w);
125 // we don't support any yet..
130 * Internal setValue used by all typed setValue functions.
131 * Sets a window property on a window, optionally appending to the existing
134 void XAtom::setValue(Window win, Atom atom, Atom type, unsigned char* data,
135 int size, int nelements, bool append) const {
136 ASSERT(win != None); ASSERT(atom != None); ASSERT(type != None);
137 ASSERT(data != (unsigned char *) 0);
138 ASSERT(size == 8 || size == 16 || size == 32);
139 ASSERT(nelements > 0);
140 XChangeProperty(_display, win, atom, type, size,
141 (append ? PropModeAppend : PropModeReplace),
147 * Set a 32-bit CARDINAL property value on a window.
149 void XAtom::setCardValue(Window win, Atom atom, long value) const {
150 setValue(win, atom, XA_CARDINAL, reinterpret_cast<unsigned char*>(&value),
156 * Set an Atom property value on a window.
158 void XAtom::setAtomValue(Window win, Atom atom, Atom value) const {
159 setValue(win, atom, XA_ATOM, reinterpret_cast<unsigned char*>(&value),
165 * Set a Window property value on a window.
167 void XAtom::setWindowValue(Window win, Atom atom, Window value) const {
168 setValue(win, atom, XA_WINDOW, reinterpret_cast<unsigned char*>(&value),
174 * Set a Pixmap property value on a window.
176 void XAtom::setPixmapValue(Window win, Atom atom, Pixmap value) const {
177 setValue(win, atom, XA_PIXMAP, reinterpret_cast<unsigned char*>(&value),
183 * Set a string property value on a window.
185 void XAtom::setStringValue(Window win, Atom atom,
186 const std::string &value) const {
187 setValue(win, atom, XA_STRING,
188 const_cast<unsigned char*>
189 (reinterpret_cast<const unsigned char*>(value.c_str())),
190 8, value.size(), false);
195 * Add elements to a 32-bit CARDINAL property value on a window.
197 void XAtom::addCardValue(Window win, Atom atom, long value) const {
198 setValue(win, atom, XA_CARDINAL, reinterpret_cast<unsigned char*>(&value),
204 * Add elements to an Atom property value on a window.
206 void XAtom::addAtomValue(Window win, Atom atom, Atom value) const {
207 setValue(win, atom, XA_ATOM, reinterpret_cast<unsigned char*>(&value),
213 * Add elements to a Window property value on a window.
215 void XAtom::addWindowValue(Window win, Atom atom, Window value) const {
216 setValue(win, atom, XA_WINDOW, reinterpret_cast<unsigned char*>(&value),
222 * Add elements to a Pixmap property value on a window.
224 void XAtom::addPixmapValue(Window win, Atom atom, Pixmap value) const {
225 setValue(win, atom, XA_PIXMAP, reinterpret_cast<unsigned char*>(&value),
231 * Add characters to a string property value on a window.
233 void XAtom::addStringValue(Window win, Atom atom,
234 const std::string &value) const {
235 setValue(win, atom, XA_STRING,
236 const_cast<unsigned char*>
237 (reinterpret_cast<const unsigned char *>
239 8, value.size(), true);
244 * Internal getValue function used by all of the typed getValue functions.
245 * Gets an property's value from a window.
246 * Returns true if the property was successfully retrieved; false if the
247 * property did not exist on the window, or has a different type/size format
248 * than the user tried to retrieve.
250 bool XAtom::getValue(Window win, Atom atom, Atom type, unsigned long *nelements,
251 unsigned char **value, int size) const {
252 unsigned char *c_val; // value alloc'd with c malloc
255 unsigned long ret_bytes;
256 XGetWindowProperty(_display, win, atom, 0l, 1l, False, AnyPropertyType,
257 &ret_type, &ret_size, nelements, &ret_bytes,
258 &c_val); // try get the first element
259 if (ret_type == None)
260 // the property does not exist on the window
262 if (ret_type != type || ret_size != size) {
263 // wrong data in property
267 // the data is correct, now, is there more than 1 element?
268 if (ret_bytes == 0) {
269 // we got the whole property's value
270 *value = new unsigned char[*nelements * size/8 + 1];
271 memcpy(*value, c_val, *nelements * size/8 + 1);
275 // get the entire property since it is larger than one long
277 // the number of longs that need to be retreived to get the property's entire
278 // value. The last + 1 is the first long that we retrieved above.
279 const int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
280 XGetWindowProperty(_display, win, atom, 0l, remain, False, type, &ret_type,
281 &ret_size, nelements, &ret_bytes, &c_val);
282 ASSERT(ret_bytes == 0);
283 *value = new unsigned char[*nelements * size/8 + 1];
284 memcpy(*value, c_val, *nelements * size/8 + 1);
291 * Gets a 32-bit Cardinal property's value from a window.
293 bool XAtom::getCardValue(Window win, Atom atom, unsigned long *nelements,
294 long **value) const {
295 return XAtom::getValue(win, atom, XA_CARDINAL, nelements,
296 reinterpret_cast<unsigned char **>(value), 32);
301 * Gets an Atom property's value from a window.
303 bool XAtom::getAtomValue(Window win, Atom atom, unsigned long *nelements,
304 Atom **value) const {
305 return XAtom::getValue(win, atom, XA_ATOM, nelements,
306 reinterpret_cast<unsigned char **>(value), 32);
311 * Gets an Window property's value from a window.
313 bool XAtom::getWindowValue(Window win, Atom atom, unsigned long *nelements,
314 Window **value) const {
315 return XAtom::getValue(win, atom, XA_WINDOW, nelements,
316 reinterpret_cast<unsigned char **>(value), 32);
321 * Gets an Pixmap property's value from a window.
323 bool XAtom::getPixmapValue(Window win, Atom atom, unsigned long *nelements,
324 Pixmap **value) const {
325 return XAtom::getValue(win, atom, XA_PIXMAP, nelements,
326 reinterpret_cast<unsigned char **>(value), 32);
331 * Gets an string property's value from a window.
333 bool XAtom::getStringValue(Window win, Atom atom, std::string &value) const {
335 unsigned long nelements;
336 bool ret = XAtom::getValue(win, atom, XA_STRING, &nelements, &data, 8);
338 value = reinterpret_cast<char*>(data);
344 * Removes a property entirely from a window.
346 void XAtom::eraseValue(Window win, Atom atom) const {
347 XDeleteProperty(_display, win, atom);