2 * property.c - property handlers
4 * Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <xcb/xcb_atom.h>
31 #include "common/atoms.h"
32 #include "common/xutil.h"
35 #define HANDLE_TEXT_PROPERTY(funcname, atom, setfunc) \
37 property_update_##funcname(client_t *c, xcb_get_property_reply_t *reply) \
39 bool no_reply = !reply; \
41 reply = xcb_get_property_reply(globalconf.connection, \
42 xcb_get_property(globalconf.connection, \
46 XCB_GET_PROPERTY_TYPE_ANY, \
49 luaA_object_push(globalconf.L, c); \
50 setfunc(globalconf.L, -1, xutil_get_text_property_from_reply(reply)); \
51 lua_pop(globalconf.L, 1); \
56 property_handle_##funcname(uint8_t state, \
57 xcb_window_t window, \
59 xcb_get_property_reply_t *reply) \
61 client_t *c = client_getbywin(window); \
63 property_update_##funcname(c, reply); \
68 HANDLE_TEXT_PROPERTY(wm_name
, XCB_ATOM_WM_NAME
, client_set_alt_name
)
69 HANDLE_TEXT_PROPERTY(net_wm_name
, _NET_WM_NAME
, client_set_name
)
70 HANDLE_TEXT_PROPERTY(wm_icon_name
, XCB_ATOM_WM_ICON_NAME
, client_set_alt_icon_name
)
71 HANDLE_TEXT_PROPERTY(net_wm_icon_name
, _NET_WM_ICON_NAME
, client_set_icon_name
)
72 HANDLE_TEXT_PROPERTY(wm_client_machine
, XCB_ATOM_WM_CLIENT_MACHINE
, client_set_machine
)
73 HANDLE_TEXT_PROPERTY(wm_window_role
, WM_WINDOW_ROLE
, client_set_role
)
75 #undef HANDLE_TEXT_PROPERTY
77 #define HANDLE_PROPERTY(name) \
79 property_handle_##name(uint8_t state, \
80 xcb_window_t window, \
82 xcb_get_property_reply_t *reply) \
84 client_t *c = client_getbywin(window); \
86 property_update_##name(c, reply); \
90 HANDLE_PROPERTY(wm_protocols
)
91 HANDLE_PROPERTY(wm_transient_for
)
92 HANDLE_PROPERTY(wm_client_leader
)
93 HANDLE_PROPERTY(wm_normal_hints
)
94 HANDLE_PROPERTY(wm_hints
)
95 HANDLE_PROPERTY(wm_class
)
96 HANDLE_PROPERTY(net_wm_icon
)
97 HANDLE_PROPERTY(net_wm_pid
)
99 #undef HANDLE_PROPERTY
102 property_update_wm_transient_for(client_t
*c
, xcb_get_property_reply_t
*reply
)
108 if(!xcb_get_wm_transient_for_from_reply(&trans
, reply
))
113 if(!xcb_get_wm_transient_for_reply(globalconf
.connection
,
114 xcb_get_wm_transient_for_unchecked(globalconf
.connection
,
120 luaA_object_push(globalconf
.L
, c
);
121 client_set_type(globalconf
.L
, -1, WINDOW_TYPE_DIALOG
);
122 client_set_above(globalconf
.L
, -1, false);
123 client_set_transient_for(globalconf
.L
, -1, client_getbywin(trans
));
124 lua_pop(globalconf
.L
, 1);
127 /** Update leader hint of a client.
128 * \param c The client.
129 * \param reply (Optional) An existing reply.
132 property_update_wm_client_leader(client_t
*c
, xcb_get_property_reply_t
*reply
)
134 xcb_get_property_cookie_t client_leader_q
;
136 bool no_reply
= !reply
;
140 client_leader_q
= xcb_get_property_unchecked(globalconf
.connection
, false, c
->window
,
141 WM_CLIENT_LEADER
, XCB_ATOM_WINDOW
, 1, 32);
143 reply
= xcb_get_property_reply(globalconf
.connection
, client_leader_q
, NULL
);
146 if(reply
&& reply
->value_len
&& (data
= xcb_get_property_value(reply
)))
147 c
->leader_window
= *(xcb_window_t
*) data
;
149 /* Only free when we created a reply ourselves. */
154 /** Update the size hints of a client.
155 * \param c The client.
156 * \param reply (Optional) An existing reply.
159 property_update_wm_normal_hints(client_t
*c
, xcb_get_property_reply_t
*reply
)
163 if(!xcb_get_wm_size_hints_from_reply(&c
->size_hints
, reply
))
168 if(!xcb_get_wm_normal_hints_reply(globalconf
.connection
,
169 xcb_get_wm_normal_hints_unchecked(globalconf
.connection
,
171 &c
->size_hints
, NULL
))
176 /** Update the WM hints of a client.
177 * \param c The client.
178 * \param reply (Optional) An existing reply.
181 property_update_wm_hints(client_t
*c
, xcb_get_property_reply_t
*reply
)
187 if(!xcb_get_wm_hints_from_reply(&wmh
, reply
))
192 if(!xcb_get_wm_hints_reply(globalconf
.connection
,
193 xcb_get_wm_hints_unchecked(globalconf
.connection
, c
->window
),
198 luaA_object_push(globalconf
.L
, c
);
199 client_set_urgent(globalconf
.L
, -1, xcb_wm_hints_get_urgency(&wmh
));
200 if(wmh
.flags
& XCB_WM_HINT_STATE
&&
201 wmh
.initial_state
== XCB_WM_STATE_WITHDRAWN
)
202 client_set_border_width(globalconf
.L
, -1, 0);
204 if(wmh
.flags
& XCB_WM_HINT_INPUT
)
205 c
->nofocus
= !wmh
.input
;
207 if(wmh
.flags
& XCB_WM_HINT_WINDOW_GROUP
)
208 client_set_group_window(globalconf
.L
, -1, wmh
.window_group
);
210 lua_pop(globalconf
.L
, 1);
213 /** Update WM_CLASS of a client.
214 * \param c The client.
215 * \param reply The reply to get property request, or NULL if none.
218 property_update_wm_class(client_t
*c
, xcb_get_property_reply_t
*reply
)
220 xcb_get_wm_class_reply_t hint
;
224 if(!xcb_get_wm_class_from_reply(&hint
, reply
))
229 if(!xcb_get_wm_class_reply(globalconf
.connection
,
230 xcb_get_wm_class_unchecked(globalconf
.connection
, c
->window
),
235 luaA_object_push(globalconf
.L
, c
);
236 client_set_class_instance(globalconf
.L
, -1, hint
.class_name
, hint
.instance_name
);
237 lua_pop(globalconf
.L
, 1);
239 /* only delete reply if we get it ourselves */
241 xcb_get_wm_class_reply_wipe(&hint
);
245 property_handle_net_wm_strut_partial(uint8_t state
,
248 xcb_get_property_reply_t
*reply
)
250 client_t
*c
= client_getbywin(window
);
253 ewmh_process_client_strut(c
, reply
);
259 property_update_net_wm_icon(client_t
*c
,
260 xcb_get_property_reply_t
*reply
)
262 luaA_object_push(globalconf
.L
, c
);
266 if(ewmh_window_icon_from_reply(reply
))
267 client_set_icon(globalconf
.L
, -2, -1);
269 else if(ewmh_window_icon_get_reply(ewmh_window_icon_get_unchecked(c
->window
)))
270 client_set_icon(globalconf
.L
, -2, -1);
273 lua_pop(globalconf
.L
, 1);
277 property_update_net_wm_pid(client_t
*c
,
278 xcb_get_property_reply_t
*reply
)
280 bool no_reply
= !reply
;
284 xcb_get_property_cookie_t prop_c
=
285 xcb_get_property_unchecked(globalconf
.connection
, false, c
->window
, _NET_WM_PID
, XCB_ATOM_CARDINAL
, 0L, 1L);
286 reply
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
);
289 if(reply
&& reply
->value_len
)
291 uint32_t *rdata
= xcb_get_property_value(reply
);
294 luaA_object_push(globalconf
.L
, c
);
295 client_set_pid(globalconf
.L
, -1, *rdata
);
296 lua_pop(globalconf
.L
, 1);
304 /** Update the list of supported protocols for a client.
305 * \param c The client.
306 * \param reply The xcb property reply.
309 property_update_wm_protocols(client_t
*c
, xcb_get_property_reply_t
*reply
)
311 xcb_get_wm_protocols_reply_t protocols
;
312 xcb_get_property_reply_t
*reply_copy
;
316 reply_copy
= p_dup(reply
, 1);
318 if(!xcb_get_wm_protocols_from_reply(reply_copy
, &protocols
))
320 p_delete(&reply_copy
);
326 /* If this fails for any reason, we still got the old value */
327 if(!xcb_get_wm_protocols_reply(globalconf
.connection
,
328 xcb_get_wm_protocols_unchecked(globalconf
.connection
,
329 c
->window
, WM_PROTOCOLS
),
334 xcb_get_wm_protocols_reply_wipe(&c
->protocols
);
335 memcpy(&c
->protocols
, &protocols
, sizeof(protocols
));
338 /** The property notify event handler.
339 * \param state currently unused
340 * \param window The window to obtain update the property with.
341 * \param name The protocol atom, currently unused.
342 * \param reply (Optional) An existing reply.
345 property_handle_xembed_info(uint8_t state
,
348 xcb_get_property_reply_t
*reply
)
350 xembed_window_t
*emwin
= xembed_getbywin(&globalconf
.embedded
, window
);
353 xembed_property_update(globalconf
.connection
, emwin
, reply
);
359 property_handle_xrootpmap_id(uint8_t state
,
362 xcb_get_property_reply_t
*reply
)
364 if(globalconf
.xinerama_is_active
)
365 foreach(w
, globalconf
.wiboxes
)
366 (*w
)->need_update
= true;
369 int screen
= xutil_root2screen(globalconf
.connection
, window
);
370 foreach(w
, globalconf
.wiboxes
)
371 if(screen
== screen_array_indexof(&globalconf
.screens
, (*w
)->screen
))
372 (*w
)->need_update
= true;
379 property_handle_net_wm_opacity(uint8_t state
,
382 xcb_get_property_reply_t
*reply
)
384 wibox_t
*wibox
= wibox_getbywin(window
);
388 luaA_object_push(globalconf
.L
, wibox
);
389 wibox_set_opacity(globalconf
.L
, -1, window_opacity_get_from_reply(reply
));
390 lua_pop(globalconf
.L
, -1);
394 client_t
*c
= client_getbywin(window
);
397 luaA_object_push(globalconf
.L
, c
);
398 client_set_opacity(globalconf
.L
, -1, window_opacity_get_from_reply(reply
));
399 lua_pop(globalconf
.L
, 1);
406 /** The property notify event handler.
407 * \param data Unused data.
408 * \param connection The connection to the X server.
409 * \param ev The event.
410 * \return Status code, 0 if everything's fine.
413 property_handle_propertynotify(xcb_property_notify_event_t
*ev
)
416 int (*handler
)(uint8_t state
,
419 xcb_get_property_reply_t
*reply
) = NULL
;
421 /* Find the correct event handler */
422 #define HANDLE(atom_, cb, len) \
423 if (ev->atom == atom_) \
428 #define HANDLE_L(atom, cb) HANDLE(atom, cb, UINT_MAX)
429 #define HANDLE_S(atom, cb) HANDLE(atom, cb, 1)
433 HANDLE_L(_XEMBED_INFO
, property_handle_xembed_info
)
436 HANDLE_L(XCB_ATOM_WM_TRANSIENT_FOR
, property_handle_wm_transient_for
)
437 HANDLE_L(WM_CLIENT_LEADER
, property_handle_wm_client_leader
)
438 HANDLE_L(XCB_ATOM_WM_NORMAL_HINTS
, property_handle_wm_normal_hints
)
439 HANDLE_L(XCB_ATOM_WM_HINTS
, property_handle_wm_hints
)
440 HANDLE_L(XCB_ATOM_WM_NAME
, property_handle_wm_name
)
441 HANDLE_L(XCB_ATOM_WM_ICON_NAME
, property_handle_wm_icon_name
)
442 HANDLE_L(XCB_ATOM_WM_CLASS
, property_handle_wm_class
)
443 HANDLE_L(WM_PROTOCOLS
, property_handle_wm_protocols
)
444 HANDLE_L(XCB_ATOM_WM_CLIENT_MACHINE
, property_handle_wm_client_machine
)
445 HANDLE_L(WM_WINDOW_ROLE
, property_handle_wm_window_role
)
448 HANDLE_L(_NET_WM_NAME
, property_handle_net_wm_name
)
449 HANDLE_L(_NET_WM_ICON_NAME
, property_handle_net_wm_icon_name
)
450 HANDLE_L(_NET_WM_STRUT_PARTIAL
, property_handle_net_wm_strut_partial
)
451 HANDLE_L(_NET_WM_ICON
, property_handle_net_wm_icon
)
452 HANDLE_L(_NET_WM_PID
, property_handle_net_wm_pid
)
453 HANDLE_S(_NET_WM_WINDOW_OPACITY
, property_handle_net_wm_opacity
)
455 /* background change */
456 HANDLE_S(_XROOTPMAP_ID
, property_handle_xrootpmap_id
)
458 /* If nothing was found, return */
465 /* Get the property, if needed. */
466 xcb_get_property_reply_t
*propr
= NULL
;
467 if(ev
->state
!= XCB_PROPERTY_DELETE
)
469 xcb_get_property_cookie_t cookie
=
470 xcb_get_property(globalconf
.connection
, 0, ev
->window
, ev
->atom
,
471 XCB_GET_PROPERTY_TYPE_ANY
, 0, length
);
472 propr
= xcb_get_property_reply(globalconf
.connection
, cookie
, 0);
475 (*handler
)(ev
->state
, ev
->window
, ev
->atom
, propr
);
479 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80