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");
154 _atoms
[kde_net_wm_window_type_override
] =
155 create("_KDE_NET_WM_WINDOW_TYPE_OVERRIDE");
160 * clean up the class' members
163 while (!_support_windows
.empty()) {
164 // make sure we aren't fucking with this somewhere
165 assert(_support_windows
.back() != None
);
166 XDestroyWindow(_display
, _support_windows
.back());
167 _support_windows
.pop_back();
173 * Returns an atom from the Xserver, creating it if necessary.
175 Atom
XAtom::create(const char *name
) const {
176 return XInternAtom(_display
, name
, False
);
181 * Sets which atoms are supported for NETWM, by Openbox, on the root window.
183 void XAtom::setSupported(const ScreenInfo
*screen
) {
184 Window root
= screen
->getRootWindow();
186 // create the netwm support window
187 Window w
= XCreateSimpleWindow(_display
, root
, 0, 0, 1, 1, 0, 0, 0);
189 _support_windows
.push_back(w
);
191 // set supporting window
192 setValue(root
, net_supporting_wm_check
, window
, w
);
194 //set properties on the supporting window
195 setValue(w
, net_wm_name
, utf8
, "Openbox");
196 setValue(w
, net_supporting_wm_check
, window
, w
);
198 // we don't support any yet..
202 _atoms
[net_current_desktop
],
203 _atoms
[net_number_of_desktops
],
204 _atoms
[net_desktop_geometry
],
205 _atoms
[net_desktop_viewport
],
206 _atoms
[net_active_window
],
207 _atoms
[net_workarea
],
208 _atoms
[net_client_list
],
209 _atoms
[net_client_list_stacking
],
210 _atoms
[net_desktop_names
],
211 _atoms
[net_close_window
],
213 _atoms
[net_wm_visible_name
],
214 _atoms
[net_wm_icon_name
],
215 _atoms
[net_wm_visible_icon_name
],
216 _atoms
[net_wm_desktop
],
217 _atoms
[net_wm_strut
],
218 _atoms
[net_wm_window_type
],
219 _atoms
[net_wm_window_type_desktop
],
220 _atoms
[net_wm_window_type_dock
],
221 _atoms
[net_wm_window_type_toolbar
],
222 _atoms
[net_wm_window_type_menu
],
223 _atoms
[net_wm_window_type_utility
],
224 _atoms
[net_wm_window_type_splash
],
225 _atoms
[net_wm_window_type_dialog
],
226 _atoms
[net_wm_window_type_normal
],
227 _atoms
[net_wm_moveresize
],
228 _atoms
[net_wm_moveresize_size_topleft
],
229 _atoms
[net_wm_moveresize_size_topright
],
230 _atoms
[net_wm_moveresize_size_bottomleft
],
231 _atoms
[net_wm_moveresize_size_bottomright
],
232 _atoms
[net_wm_moveresize_move
],
233 _atoms
[net_wm_allowed_actions
],
234 _atoms
[net_wm_action_move
],
235 _atoms
[net_wm_action_resize
],
236 _atoms
[net_wm_action_shade
],
237 _atoms
[net_wm_action_maximize_horz
],
238 _atoms
[net_wm_action_maximize_vert
],
239 _atoms
[net_wm_action_change_desktop
],
240 _atoms
[net_wm_action_close
],
241 _atoms
[net_wm_state
],
242 _atoms
[net_wm_state_modal
],
243 _atoms
[net_wm_state_maximized_vert
],
244 _atoms
[net_wm_state_maximized_horz
],
245 _atoms
[net_wm_state_shaded
],
246 _atoms
[net_wm_state_skip_taskbar
],
247 _atoms
[net_wm_state_skip_pager
],
248 _atoms
[net_wm_state_hidden
],
249 _atoms
[net_wm_state_fullscreen
],
251 const int num_supported
= sizeof(supported
)/sizeof(Atom
);
253 setValue(root
, net_supported
, atom
, supported
, num_supported
);
259 * Sets a window property on a window, optionally appending to the existing
262 void XAtom::setValue(Window win
, Atom atom
, Atom type
,
263 unsigned char* data
, int size
, int nelements
,
265 assert(win
!= None
); assert(atom
!= None
); assert(type
!= None
);
266 assert(nelements
== 0 || (nelements
> 0 && data
!= (unsigned char *) 0));
267 assert(size
== 8 || size
== 16 || size
== 32);
268 XChangeProperty(_display
, win
, atom
, type
, size
,
269 (append
? PropModeAppend
: PropModeReplace
),
275 * Set a 32-bit property value on a window.
277 void XAtom::setValue(Window win
, Atoms atom
, Atoms type
,
278 unsigned long value
) const {
279 assert(atom
>= 0 && atom
< NUM_ATOMS
);
280 assert(type
>= 0 && type
< NUM_ATOMS
);
281 setValue(win
, _atoms
[atom
], _atoms
[type
],
282 reinterpret_cast<unsigned char*>(&value
), 32, 1, False
);
287 * Set an array of 32-bit properties value on a window.
289 void XAtom::setValue(Window win
, Atoms atom
, Atoms type
,
290 unsigned long value
[], int elements
) const {
291 assert(atom
>= 0 && atom
< NUM_ATOMS
);
292 assert(type
>= 0 && type
< NUM_ATOMS
);
293 setValue(win
, _atoms
[atom
], _atoms
[type
],
294 reinterpret_cast<unsigned char*>(value
), 32, elements
, False
);
299 * Set an string property value on a window.
301 void XAtom::setValue(Window win
, Atoms atom
, StringType type
,
302 const std::string
&value
) const {
303 assert(atom
>= 0 && atom
< NUM_ATOMS
);
304 assert(type
>= 0 && type
< NUM_STRING_TYPE
);
308 case ansi
: t
= _atoms
[string
]; break;
309 case utf8
: t
= _atoms
[utf8_string
]; break;
310 default: assert(False
); return; // unhandled StringType
312 setValue(win
, _atoms
[atom
], t
,
313 reinterpret_cast<unsigned char *>(const_cast<char *>(value
.c_str())),
314 8, value
.size() + 1, False
); // add 1 to the size to include the null
319 * Set an array of string property values on a window.
321 void XAtom::setValue(Window win
, Atoms atom
, StringType type
,
322 const StringVect
&strings
) const {
323 assert(atom
>= 0 && atom
< NUM_ATOMS
);
324 assert(type
>= 0 && type
< NUM_STRING_TYPE
);
328 case ansi
: t
= _atoms
[string
]; break;
329 case utf8
: t
= _atoms
[utf8_string
]; break;
330 default: assert(False
); return; // unhandled StringType
335 StringVect::const_iterator it
= strings
.begin();
336 const StringVect::const_iterator end
= strings
.end();
337 for (; it
!= end
; ++it
)
340 setValue(win
, _atoms
[atom
], t
,
341 reinterpret_cast<unsigned char *>(const_cast<char *>(value
.c_str())),
342 8, value
.size(), False
);
347 * Internal getValue function used by all of the typed getValue functions.
348 * Gets an property's value from a window.
349 * Returns True if the property was successfully retrieved; False if the
350 * property did not exist on the window, or has a different type/size format
351 * than the user tried to retrieve.
353 bool XAtom::getValue(Window win
, Atom atom
, Atom type
,
354 unsigned long &nelements
, unsigned char **value
,
356 assert(win
!= None
); assert(atom
!= None
); assert(type
!= None
);
357 assert(size
== 8 || size
== 16 || size
== 32);
358 assert(nelements
> 0);
359 unsigned char *c_val
= 0; // value alloc'd with c malloc
362 unsigned long ret_bytes
;
364 const unsigned long maxread
= nelements
;
365 // try get the first element
366 result
= XGetWindowProperty(_display
, win
, atom
, 0l, 1l, False
,
367 AnyPropertyType
, &ret_type
, &ret_size
,
368 &nelements
, &ret_bytes
, &c_val
);
369 if (result
!= Success
|| ret_type
!= type
|| ret_size
!= size
||
371 // an error occured, the property does not exist on the window, or is empty,
372 // or the wrong data is in property for the request
373 if (c_val
) XFree(c_val
);
376 // the data is correct, now, is there more elements left?
377 if (ret_bytes
== 0 || maxread
<= nelements
) {
378 // we got the whole property's value
379 *value
= new unsigned char[nelements
* size
/8 + 1];
380 memcpy(*value
, c_val
, nelements
* size
/8 + 1);
384 // get the entire property since it is larger than one long
386 // the number of longs that need to be retreived to get the property's entire
387 // value. The last + 1 is the first long that we retrieved above.
388 int remain
= (ret_bytes
- 1)/sizeof(long) + 1 + 1;
389 if (remain
> size
/8 * (signed)maxread
) // dont get more than the max
390 remain
= size
/8 * (signed)maxread
;
391 result
= XGetWindowProperty(_display
, win
, atom
, 0l, remain
, False
, type
,
392 &ret_type
, &ret_size
, &nelements
, &ret_bytes
,
394 assert(result
== Success
);
395 assert(ret_bytes
== 0);
396 *value
= new unsigned char[nelements
* size
/8 + 1];
397 memcpy(*value
, c_val
, nelements
* size
/8 + 1);
404 * Gets a 32-bit property's value from a window.
406 bool XAtom::getValue(Window win
, Atoms atom
, Atoms type
,
407 unsigned long &nelements
,
408 unsigned long **value
) const {
409 assert(atom
>= 0 && atom
< NUM_ATOMS
);
410 assert(type
>= 0 && type
< NUM_ATOMS
);
411 return getValue(win
, _atoms
[atom
], _atoms
[type
], nelements
,
412 reinterpret_cast<unsigned char **>(value
), 32);
417 * Gets a single 32-bit property's value from a window.
419 bool XAtom::getValue(Window win
, Atoms atom
, Atoms type
,
420 unsigned long &value
) const {
421 assert(atom
>= 0 && atom
< NUM_ATOMS
);
422 assert(type
>= 0 && type
< NUM_ATOMS
);
424 unsigned long num
= 1;
425 if (! getValue(win
, _atoms
[atom
], _atoms
[type
], num
,
426 reinterpret_cast<unsigned char **>(&temp
), 32))
435 * Gets an string property's value from a window.
437 bool XAtom::getValue(Window win
, Atoms atom
, StringType type
,
438 std::string
&value
) const {
441 if (getValue(win
, atom
, type
, n
, s
)) {
449 bool XAtom::getValue(Window win
, Atoms atom
, StringType type
,
450 unsigned long &nelements
, StringVect
&strings
) const {
451 assert(atom
>= 0 && atom
< NUM_ATOMS
);
452 assert(type
>= 0 && type
< NUM_STRING_TYPE
);
453 assert(win
!= None
); assert(_atoms
[atom
] != None
);
454 assert(nelements
> 0);
458 case ansi
: t
= _atoms
[string
]; break;
459 case utf8
: t
= _atoms
[utf8_string
]; break;
460 default: assert(False
); return False
; // unhandled StringType
463 unsigned char *value
;
464 unsigned long elements
= (unsigned) -1;
465 if (!getValue(win
, _atoms
[atom
], t
, elements
, &value
, 8) || elements
< 1)
468 std::string
s(reinterpret_cast<char *>(value
), elements
);
471 std::string::const_iterator it
= s
.begin(), end
= s
.end();
472 unsigned long num
= 0;
473 while(num
< nelements
) {
474 std::string::const_iterator tmp
= it
; // current string.begin()
475 it
= std::find(tmp
, end
, '\0'); // look for null between tmp and end
476 strings
.push_back(std::string(tmp
, it
)); // s[tmp:it)
478 if (it
== end
) break;
480 if (it
== end
) break;
490 * Removes a property entirely from a window.
492 void XAtom::eraseValue(Window win
, Atoms atom
) const {
493 assert(atom
>= 0 && atom
< NUM_ATOMS
);
494 XDeleteProperty(_display
, win
, _atoms
[atom
]);
498 void XAtom::sendClientMessage(Window target
, Atoms type
, Window about
,
499 long data
, long data1
, long data2
,
501 assert(atom
>= 0 && atom
< NUM_ATOMS
);
502 assert(target
!= None
);
505 e
.xclient
.type
= ClientMessage
;
506 e
.xclient
.format
= 32;
507 e
.xclient
.message_type
= _atoms
[type
];
508 e
.xclient
.window
= about
;
509 e
.xclient
.data
.l
[0] = data
;
510 e
.xclient
.data
.l
[1] = data1
;
511 e
.xclient
.data
.l
[2] = data2
;
512 e
.xclient
.data
.l
[3] = data3
;
514 XSendEvent(_display
, target
, False
,
515 SubstructureRedirectMask
| SubstructureNotifyMask
,