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.
23 #include "common/atoms.h"
24 #include "common/xutil.h"
26 #include "objects/client.h"
27 #include "objects/drawin.h"
30 #include <xcb/xcb_atom.h>
32 #define HANDLE_TEXT_PROPERTY(funcname, atom, setfunc) \
33 xcb_get_property_cookie_t \
34 property_get_##funcname(client_t *c) \
36 return xcb_get_property(globalconf.connection, \
40 XCB_GET_PROPERTY_TYPE_ANY, \
45 property_update_##funcname(client_t *c, xcb_get_property_cookie_t cookie) \
47 xcb_get_property_reply_t * reply = \
48 xcb_get_property_reply(globalconf.connection, cookie, NULL); \
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); \
55 property_handle_##funcname(uint8_t state, \
56 xcb_window_t window) \
58 client_t *c = client_getbywin(window); \
60 property_update_##funcname(c, property_get_##funcname(c));\
65 HANDLE_TEXT_PROPERTY(wm_name
, XCB_ATOM_WM_NAME
, client_set_alt_name
)
66 HANDLE_TEXT_PROPERTY(net_wm_name
, _NET_WM_NAME
, client_set_name
)
67 HANDLE_TEXT_PROPERTY(wm_icon_name
, XCB_ATOM_WM_ICON_NAME
, client_set_alt_icon_name
)
68 HANDLE_TEXT_PROPERTY(net_wm_icon_name
, _NET_WM_ICON_NAME
, client_set_icon_name
)
69 HANDLE_TEXT_PROPERTY(wm_client_machine
, XCB_ATOM_WM_CLIENT_MACHINE
, client_set_machine
)
70 HANDLE_TEXT_PROPERTY(wm_window_role
, WM_WINDOW_ROLE
, client_set_role
)
72 #undef HANDLE_TEXT_PROPERTY
74 #define HANDLE_PROPERTY(name) \
76 property_handle_##name(uint8_t state, \
77 xcb_window_t window) \
79 client_t *c = client_getbywin(window); \
81 property_update_##name(c, property_get_##name(c));\
85 HANDLE_PROPERTY(wm_protocols
)
86 HANDLE_PROPERTY(wm_transient_for
)
87 HANDLE_PROPERTY(wm_client_leader
)
88 HANDLE_PROPERTY(wm_normal_hints
)
89 HANDLE_PROPERTY(wm_hints
)
90 HANDLE_PROPERTY(wm_class
)
91 HANDLE_PROPERTY(net_wm_icon
)
92 HANDLE_PROPERTY(net_wm_pid
)
94 #undef HANDLE_PROPERTY
96 xcb_get_property_cookie_t
97 property_get_wm_transient_for(client_t
*c
)
99 return xcb_icccm_get_wm_transient_for_unchecked(globalconf
.connection
, c
->window
);
103 property_update_wm_transient_for(client_t
*c
, xcb_get_property_cookie_t cookie
)
109 if(!xcb_icccm_get_wm_transient_for_reply(globalconf
.connection
,
114 tmp
= tc
= client_getbywin(trans
);
116 luaA_object_push(globalconf
.L
, c
);
117 client_set_type(globalconf
.L
, -1, WINDOW_TYPE_DIALOG
);
118 client_set_above(globalconf
.L
, -1, false);
120 /* Verify that there are no loops in the transient_for relation after we are done */
121 for(counter
= 0; tmp
!= NULL
&& counter
<= globalconf
.stack
.len
; counter
++)
124 /* We arrived back at the client we started from, so there is a loop */
125 counter
= globalconf
.stack
.len
+1;
126 tmp
= tmp
->transient_for
;
128 if (counter
<= globalconf
.stack
.len
)
129 client_set_transient_for(globalconf
.L
, -1, tc
);
131 lua_pop(globalconf
.L
, 1);
134 xcb_get_property_cookie_t
135 property_get_wm_client_leader(client_t
*c
)
137 return xcb_get_property_unchecked(globalconf
.connection
, false, c
->window
,
138 WM_CLIENT_LEADER
, XCB_ATOM_WINDOW
, 0, 32);
141 /** Update leader hint of a client.
142 * \param c The client.
143 * \param cookie Cookie returned by property_get_wm_client_leader.
146 property_update_wm_client_leader(client_t
*c
, xcb_get_property_cookie_t cookie
)
148 xcb_get_property_reply_t
*reply
;
151 reply
= xcb_get_property_reply(globalconf
.connection
, cookie
, NULL
);
153 if(reply
&& reply
->value_len
&& (data
= xcb_get_property_value(reply
)))
154 c
->leader_window
= *(xcb_window_t
*) data
;
159 xcb_get_property_cookie_t
160 property_get_wm_normal_hints(client_t
*c
)
162 return xcb_icccm_get_wm_normal_hints_unchecked(globalconf
.connection
, c
->window
);
165 /** Update the size hints of a client.
166 * \param c The client.
167 * \param cookie Cookie returned by property_get_wm_normal_hints.
170 property_update_wm_normal_hints(client_t
*c
, xcb_get_property_cookie_t cookie
)
172 xcb_icccm_get_wm_normal_hints_reply(globalconf
.connection
,
174 &c
->size_hints
, NULL
);
177 xcb_get_property_cookie_t
178 property_get_wm_hints(client_t
*c
)
180 return xcb_icccm_get_wm_hints_unchecked(globalconf
.connection
, c
->window
);
183 /** Update the WM hints of a client.
184 * \param c The client.
185 * \param cookie Cookie returned by property_get_wm_hints.
188 property_update_wm_hints(client_t
*c
, xcb_get_property_cookie_t cookie
)
190 xcb_icccm_wm_hints_t wmh
;
192 if(!xcb_icccm_get_wm_hints_reply(globalconf
.connection
,
197 luaA_object_push(globalconf
.L
, c
);
198 client_set_urgent(globalconf
.L
, -1, xcb_icccm_wm_hints_get_urgency(&wmh
));
200 if(wmh
.flags
& XCB_ICCCM_WM_HINT_INPUT
)
201 c
->nofocus
= !wmh
.input
;
203 if(wmh
.flags
& XCB_ICCCM_WM_HINT_WINDOW_GROUP
)
204 client_set_group_window(globalconf
.L
, -1, wmh
.window_group
);
206 lua_pop(globalconf
.L
, 1);
209 xcb_get_property_cookie_t
210 property_get_wm_class(client_t
*c
)
212 return xcb_icccm_get_wm_class_unchecked(globalconf
.connection
, c
->window
);
215 /** Update WM_CLASS of a client.
216 * \param c The client.
217 * \param cookie Cookie returned by property_get_wm_class.
220 property_update_wm_class(client_t
*c
, xcb_get_property_cookie_t cookie
)
222 xcb_icccm_get_wm_class_reply_t hint
;
224 if(!xcb_icccm_get_wm_class_reply(globalconf
.connection
,
229 luaA_object_push(globalconf
.L
, c
);
230 client_set_class_instance(globalconf
.L
, -1, hint
.class_name
, hint
.instance_name
);
231 lua_pop(globalconf
.L
, 1);
233 xcb_icccm_get_wm_class_reply_wipe(&hint
);
237 property_handle_net_wm_strut_partial(uint8_t state
,
240 client_t
*c
= client_getbywin(window
);
243 ewmh_process_client_strut(c
);
248 xcb_get_property_cookie_t
249 property_get_net_wm_icon(client_t
*c
)
251 return ewmh_window_icon_get_unchecked(c
->window
);
255 property_update_net_wm_icon(client_t
*c
, xcb_get_property_cookie_t cookie
)
257 cairo_surface_t
*surface
= ewmh_window_icon_get_reply(cookie
);
262 client_set_icon(c
, surface
);
263 cairo_surface_destroy(surface
);
266 xcb_get_property_cookie_t
267 property_get_net_wm_pid(client_t
*c
)
269 return xcb_get_property_unchecked(globalconf
.connection
, false, c
->window
, _NET_WM_PID
, XCB_ATOM_CARDINAL
, 0L, 1L);
273 property_update_net_wm_pid(client_t
*c
, xcb_get_property_cookie_t cookie
)
275 xcb_get_property_reply_t
*reply
;
277 reply
= xcb_get_property_reply(globalconf
.connection
, cookie
, NULL
);
279 if(reply
&& reply
->value_len
)
281 uint32_t *rdata
= xcb_get_property_value(reply
);
284 luaA_object_push(globalconf
.L
, c
);
285 client_set_pid(globalconf
.L
, -1, *rdata
);
286 lua_pop(globalconf
.L
, 1);
293 xcb_get_property_cookie_t
294 property_get_wm_protocols(client_t
*c
)
296 return xcb_icccm_get_wm_protocols_unchecked(globalconf
.connection
,
297 c
->window
, WM_PROTOCOLS
);
300 /** Update the list of supported protocols for a client.
301 * \param c The client.
302 * \param cookie Cookie from property_get_wm_protocols.
305 property_update_wm_protocols(client_t
*c
, xcb_get_property_cookie_t cookie
)
307 xcb_icccm_get_wm_protocols_reply_t protocols
;
309 /* If this fails for any reason, we still got the old value */
310 if(!xcb_icccm_get_wm_protocols_reply(globalconf
.connection
,
315 xcb_icccm_get_wm_protocols_reply_wipe(&c
->protocols
);
316 memcpy(&c
->protocols
, &protocols
, sizeof(protocols
));
319 /** The property notify event handler.
320 * \param state currently unused
321 * \param window The window to obtain update the property with.
322 * \param name The protocol atom, currently unused.
323 * \param reply (Optional) An existing reply.
326 property_handle_xembed_info(uint8_t state
,
329 xembed_window_t
*emwin
= xembed_getbywin(&globalconf
.embedded
, window
);
333 xcb_get_property_cookie_t cookie
=
334 xcb_get_property(globalconf
.connection
, 0, window
, _XEMBED_INFO
,
335 XCB_GET_PROPERTY_TYPE_ANY
, 0, 3);
336 xcb_get_property_reply_t
*propr
=
337 xcb_get_property_reply(globalconf
.connection
, cookie
, 0);
338 xembed_property_update(globalconf
.connection
, emwin
, propr
);
346 property_handle_net_wm_opacity(uint8_t state
,
349 drawin_t
*drawin
= drawin_getbywin(window
);
353 luaA_object_push(globalconf
.L
, drawin
);
354 window_set_opacity(globalconf
.L
, -1, xwindow_get_opacity(drawin
->window
));
355 lua_pop(globalconf
.L
, -1);
359 client_t
*c
= client_getbywin(window
);
362 luaA_object_push(globalconf
.L
, c
);
363 window_set_opacity(globalconf
.L
, -1, xwindow_get_opacity(c
->window
));
364 lua_pop(globalconf
.L
, 1);
372 property_handle_xrootpmap_id(uint8_t state
,
375 signal_object_emit(globalconf
.L
, &global_signals
, "wallpaper_changed", 0);
379 /** The property notify event handler handling xproperties.
380 * \param ev The event.
383 property_handle_propertynotify_xproperty(xcb_property_notify_event_t
*ev
)
386 xproperty_t lookup
= { .atom
= ev
->atom
};
390 prop
= xproperty_array_lookup(&globalconf
.xproperties
, &lookup
);
392 /* Property is not registered */
395 if (ev
->window
!= globalconf
.screen
->root
)
397 obj
= client_getbywin(ev
->window
);
399 obj
= drawin_getbywin(ev
->window
);
405 /* Get us the name of the property */
406 buffer_inita(&buf
, a_strlen(prop
->name
) + a_strlen("xproperty::") + 1);
407 buffer_addf(&buf
, "xproperty::%s", prop
->name
);
409 /* And emit the right signal */
412 luaA_object_push(globalconf
.L
, obj
);
413 luaA_object_emit_signal(globalconf
.L
, -1, buf
.s
, 0);
414 lua_pop(globalconf
.L
, 1);
416 signal_object_emit(globalconf
.L
, &global_signals
, buf
.s
, 0);
420 /** The property notify event handler.
421 * \param ev The event.
424 property_handle_propertynotify(xcb_property_notify_event_t
*ev
)
426 int (*handler
)(uint8_t state
,
427 xcb_window_t window
) = NULL
;
429 globalconf
.timestamp
= ev
->time
;
431 property_handle_propertynotify_xproperty(ev
);
433 /* Find the correct event handler */
434 #define HANDLE(atom_, cb) \
435 if (ev->atom == atom_) \
442 HANDLE(_XEMBED_INFO
, property_handle_xembed_info
)
445 HANDLE(XCB_ATOM_WM_TRANSIENT_FOR
, property_handle_wm_transient_for
)
446 HANDLE(WM_CLIENT_LEADER
, property_handle_wm_client_leader
)
447 HANDLE(XCB_ATOM_WM_NORMAL_HINTS
, property_handle_wm_normal_hints
)
448 HANDLE(XCB_ATOM_WM_HINTS
, property_handle_wm_hints
)
449 HANDLE(XCB_ATOM_WM_NAME
, property_handle_wm_name
)
450 HANDLE(XCB_ATOM_WM_ICON_NAME
, property_handle_wm_icon_name
)
451 HANDLE(XCB_ATOM_WM_CLASS
, property_handle_wm_class
)
452 HANDLE(WM_PROTOCOLS
, property_handle_wm_protocols
)
453 HANDLE(XCB_ATOM_WM_CLIENT_MACHINE
, property_handle_wm_client_machine
)
454 HANDLE(WM_WINDOW_ROLE
, property_handle_wm_window_role
)
457 HANDLE(_NET_WM_NAME
, property_handle_net_wm_name
)
458 HANDLE(_NET_WM_ICON_NAME
, property_handle_net_wm_icon_name
)
459 HANDLE(_NET_WM_STRUT_PARTIAL
, property_handle_net_wm_strut_partial
)
460 HANDLE(_NET_WM_ICON
, property_handle_net_wm_icon
)
461 HANDLE(_NET_WM_PID
, property_handle_net_wm_pid
)
462 HANDLE(_NET_WM_WINDOW_OPACITY
, property_handle_net_wm_opacity
)
464 /* background change */
465 HANDLE(_XROOTPMAP_ID
, property_handle_xrootpmap_id
)
467 /* If nothing was found, return */
473 (*handler
)(ev
->state
, ev
->window
);
476 /** Register a new xproperty.
477 * \param L The Lua VM state.
478 * \return The number of elements pushed on stack.
480 * \lparam The name of the X11 property
481 * \lparam One of "string", "number" or "boolean"
484 luaA_register_xproperty(lua_State
*L
)
487 struct xproperty property
;
488 struct xproperty
*found
;
489 const char *const args
[] = { "string", "number", "boolean" };
490 xcb_intern_atom_reply_t
*atom_r
;
493 name
= luaL_checkstring(L
, 1);
494 type
= luaL_checkoption(L
, 2, NULL
, args
);
496 property
.type
= PROP_STRING
;
498 property
.type
= PROP_NUMBER
;
500 property
.type
= PROP_BOOLEAN
;
502 atom_r
= xcb_intern_atom_reply(globalconf
.connection
,
503 xcb_intern_atom_unchecked(globalconf
.connection
, false,
504 a_strlen(name
), name
),
509 property
.atom
= atom_r
->atom
;
512 found
= xproperty_array_lookup(&globalconf
.xproperties
, &property
);
515 /* Property already registered */
516 if(found
->type
!= property
.type
)
517 return luaL_error(L
, "xproperty '%s' already registered with different type", name
);
522 buffer_inita(&buf
, a_strlen(name
) + a_strlen("xproperty::") + 1);
523 buffer_addf(&buf
, "xproperty::%s", name
);
525 property
.name
= a_strdup(name
);
526 xproperty_array_insert(&globalconf
.xproperties
, property
);
527 signal_add(&window_class
.signals
, buf
.s
);
528 signal_add(&global_signals
, buf
.s
);
535 /** Set an xproperty.
536 * \param L The Lua VM state.
537 * \return The number of elements pushed on stack.
540 luaA_set_xproperty(lua_State
*L
)
542 return window_set_xproperty(L
, globalconf
.screen
->root
, 1, 2);
545 /** Get an xproperty.
546 * \param L The Lua VM state.
547 * \return The number of elements pushed on stack.
550 luaA_get_xproperty(lua_State
*L
)
552 return window_get_xproperty(L
, globalconf
.screen
->root
, 1);
555 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80