update Arnaud commit years
[awesome.git] / property.c
blob55f54091008173aaa7d5d414e64df3ff11a812c9
1 /*
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>
24 #include "screen.h"
25 #include "property.h"
26 #include "client.h"
27 #include "ewmh.h"
28 #include "wibox.h"
29 #include "window.h"
30 #include "luaa.h"
31 #include "common/atoms.h"
32 #include "common/xutil.h"
35 #define HANDLE_TEXT_PROPERTY(funcname, atom, setfunc) \
36 void \
37 property_update_##funcname(client_t *c, xcb_get_property_reply_t *reply) \
38 { \
39 bool no_reply = !reply; \
40 if(no_reply) \
41 reply = xcb_get_property_reply(globalconf.connection, \
42 xcb_get_any_property(globalconf.connection, \
43 false, \
44 c->window, \
45 WM_NAME, \
46 UINT_MAX), NULL); \
47 luaA_object_push(globalconf.L, c); \
48 setfunc(globalconf.L, -1, xutil_get_text_property_from_reply(reply)); \
49 lua_pop(globalconf.L, 1); \
50 if(no_reply) \
51 p_delete(&reply); \
52 } \
53 static int \
54 property_handle_##funcname(void *data, \
55 xcb_connection_t *connection, \
56 uint8_t state, \
57 xcb_window_t window, \
58 xcb_atom_t name, \
59 xcb_get_property_reply_t *reply) \
60 { \
61 client_t *c = client_getbywin(window); \
62 if(c) \
63 property_update_##funcname(c, reply); \
64 return 0; \
68 HANDLE_TEXT_PROPERTY(wm_name, WM_NAME, client_set_name)
69 HANDLE_TEXT_PROPERTY(wm_icon_name, WM_ICON_NAME, client_set_icon_name)
70 HANDLE_TEXT_PROPERTY(wm_client_machine, WM_CLIENT_MACHINE, client_set_machine)
71 HANDLE_TEXT_PROPERTY(wm_window_role, WM_WINDOW_ROLE, client_set_role)
73 #undef HANDLE_TEXT_PROPERTY
75 void
76 property_update_wm_transient_for(client_t *c, xcb_get_property_reply_t *reply)
78 xcb_window_t trans;
80 if(reply)
82 if(!xcb_get_wm_transient_for_from_reply(&trans, reply))
83 return;
85 else
87 if(!xcb_get_wm_transient_for_reply(globalconf.connection,
88 xcb_get_wm_transient_for_unchecked(globalconf.connection,
89 c->window),
90 &trans, NULL))
91 return;
94 luaA_object_push(globalconf.L, c);
95 client_set_type(globalconf.L, -1, WINDOW_TYPE_DIALOG);
96 client_set_above(globalconf.L, -1, false);
97 client_set_transient_for(globalconf.L, -1, client_getbywin(trans));
98 lua_pop(globalconf.L, 1);
101 static int
102 property_handle_wm_transient_for(void *data,
103 xcb_connection_t *connection,
104 uint8_t state,
105 xcb_window_t window,
106 xcb_atom_t name,
107 xcb_get_property_reply_t *reply)
109 client_t *c = client_getbywin(window);
111 if(c)
112 property_update_wm_transient_for(c, reply);
114 return 0;
117 /** Update leader hint of a client.
118 * \param c The client.
119 * \param reply (Optional) An existing reply.
121 void
122 property_update_wm_client_leader(client_t *c, xcb_get_property_reply_t *reply)
124 xcb_get_property_cookie_t client_leader_q;
125 void *data;
126 bool no_reply = !reply;
128 if(no_reply)
130 client_leader_q = xcb_get_property_unchecked(globalconf.connection, false, c->window,
131 WM_CLIENT_LEADER, WINDOW, 0, 32);
133 reply = xcb_get_property_reply(globalconf.connection, client_leader_q, NULL);
136 if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
137 c->leader_window = *(xcb_window_t *) data;
139 /* Only free when we created a reply ourselves. */
140 if(no_reply)
141 p_delete(&reply);
144 static int
145 property_handle_wm_client_leader(void *data,
146 xcb_connection_t *connection,
147 uint8_t state,
148 xcb_window_t window,
149 xcb_atom_t name,
150 xcb_get_property_reply_t *reply)
152 client_t *c = client_getbywin(window);
154 if(c)
155 property_update_wm_client_leader(c, reply);
157 return 0;
160 /** Update the size hints of a client.
161 * \param c The client.
162 * \param reply (Optional) An existing reply.
164 void
165 property_update_wm_normal_hints(client_t *c, xcb_get_property_reply_t *reply)
167 if(reply)
169 if(!xcb_get_wm_size_hints_from_reply(&c->size_hints, reply))
170 return;
172 else
174 if(!xcb_get_wm_normal_hints_reply(globalconf.connection,
175 xcb_get_wm_normal_hints_unchecked(globalconf.connection,
176 c->window),
177 &c->size_hints, NULL))
178 return;
182 static int
183 property_handle_wm_normal_hints(void *data,
184 xcb_connection_t *connection,
185 uint8_t state,
186 xcb_window_t window,
187 xcb_atom_t name,
188 xcb_get_property_reply_t *reply)
190 client_t *c = client_getbywin(window);
192 if(c)
193 property_update_wm_normal_hints(c, reply);
195 return 0;
198 /** Update the WM hints of a client.
199 * \param c The client.
200 * \param reply (Optional) An existing reply.
202 void
203 property_update_wm_hints(client_t *c, xcb_get_property_reply_t *reply)
205 xcb_wm_hints_t wmh;
207 if(reply)
209 if(!xcb_get_wm_hints_from_reply(&wmh, reply))
210 return;
212 else
214 if(!xcb_get_wm_hints_reply(globalconf.connection,
215 xcb_get_wm_hints_unchecked(globalconf.connection, c->window),
216 &wmh, NULL))
217 return;
220 luaA_object_push(globalconf.L, c);
221 client_set_urgent(globalconf.L, -1, xcb_wm_hints_get_urgency(&wmh));
222 if(wmh.flags & XCB_WM_HINT_STATE &&
223 wmh.initial_state == XCB_WM_STATE_WITHDRAWN)
224 client_set_border_width(globalconf.L, -1, 0);
226 if(wmh.flags & XCB_WM_HINT_INPUT)
227 c->nofocus = !wmh.input;
229 if(wmh.flags & XCB_WM_HINT_WINDOW_GROUP)
230 client_set_group_window(globalconf.L, -1, wmh.window_group);
233 static int
234 property_handle_wm_hints(void *data,
235 xcb_connection_t *connection,
236 uint8_t state,
237 xcb_window_t window,
238 xcb_atom_t name,
239 xcb_get_property_reply_t *reply)
241 client_t *c = client_getbywin(window);
243 if(c)
244 property_update_wm_hints(c, reply);
246 return 0;
249 /** Update WM_CLASS of a client.
250 * \param c The client.
251 * \param reply The reply to get property request, or NULL if none.
253 void
254 property_update_wm_class(client_t *c, xcb_get_property_reply_t *reply)
256 xcb_get_wm_class_reply_t hint;
258 if(reply)
260 if(!xcb_get_wm_class_from_reply(&hint, reply))
261 return;
263 else
265 if(!xcb_get_wm_class_reply(globalconf.connection,
266 xcb_get_wm_class_unchecked(globalconf.connection, c->window),
267 &hint, NULL))
268 return;
271 luaA_object_push(globalconf.L, c);
272 client_set_class_instance(globalconf.L, -1, hint.instance_name, hint.class_name);
273 lua_pop(globalconf.L, 1);
275 /* only delete reply if we get it ourselves */
276 if(!reply)
277 xcb_get_wm_class_reply_wipe(&hint);
280 static int
281 property_handle_wm_class(void *data,
282 xcb_connection_t *connection,
283 uint8_t state,
284 xcb_window_t window,
285 xcb_atom_t name,
286 xcb_get_property_reply_t *reply)
288 client_t *c = client_getbywin(window);
290 if(c)
291 property_update_wm_class(c, reply);
293 return 0;
296 static int
297 property_handle_net_wm_strut_partial(void *data,
298 xcb_connection_t *connection,
299 uint8_t state,
300 xcb_window_t window,
301 xcb_atom_t name,
302 xcb_get_property_reply_t *reply)
304 client_t *c = client_getbywin(window);
306 if(c)
307 ewmh_process_client_strut(c, reply);
309 return 0;
312 void
313 property_update_net_wm_icon(client_t *c,
314 xcb_get_property_reply_t *reply)
316 luaA_object_push(globalconf.L, c);
318 if(reply)
320 if(ewmh_window_icon_from_reply(reply))
321 client_set_icon(globalconf.L, -2, -1);
323 else if(ewmh_window_icon_get_reply(ewmh_window_icon_get_unchecked(c->window)))
324 client_set_icon(globalconf.L, -2, -1);
326 /* remove client */
327 lua_pop(globalconf.L, 1);
330 static int
331 property_handle_net_wm_icon(void *data,
332 xcb_connection_t *connection,
333 uint8_t state,
334 xcb_window_t window,
335 xcb_atom_t name,
336 xcb_get_property_reply_t *reply)
338 client_t *c = client_getbywin(window);
340 if(c)
341 property_update_net_wm_icon(c, reply);
343 return 0;
346 void
347 property_update_net_wm_pid(client_t *c,
348 xcb_get_property_reply_t *reply)
350 bool no_reply = !reply;
352 if(no_reply)
354 xcb_get_property_cookie_t prop_c =
355 xcb_get_property_unchecked(globalconf.connection, false, c->window, _NET_WM_PID, CARDINAL, 0L, 1L);
356 reply = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
359 if(reply && reply->value_len)
361 uint32_t *rdata = xcb_get_property_value(reply);
362 if(rdata)
364 luaA_object_push(globalconf.L, c);
365 client_set_pid(globalconf.L, -1, *rdata);
366 lua_pop(globalconf.L, 1);
370 if(no_reply)
371 p_delete(&reply);
374 static int
375 property_handle_net_wm_pid(void *data,
376 xcb_connection_t *connection,
377 uint8_t state,
378 xcb_window_t window,
379 xcb_atom_t name,
380 xcb_get_property_reply_t *reply)
382 client_t *c = client_getbywin(window);
384 if(c)
385 property_update_net_wm_pid(c, reply);
387 return 0;
390 /** Update the list of supported protocols for a client.
391 * \param c The client.
393 void
394 property_update_wm_protocols(client_t *c, xcb_get_property_reply_t *reply)
396 xcb_get_wm_protocols_reply_t protocols;
398 if(reply)
400 if(!xcb_get_wm_protocols_from_reply(reply, &protocols))
401 return;
403 else
405 /* If this fails for any reason, we still got the old value */
406 if(!xcb_get_wm_protocols_reply(globalconf.connection,
407 xcb_get_wm_protocols_unchecked(globalconf.connection,
408 c->window, WM_PROTOCOLS),
409 &protocols, NULL))
410 return;
413 xcb_get_wm_protocols_reply_wipe(&c->protocols);
414 memcpy(&c->protocols, &protocols, sizeof(protocols));
417 /** The property notify event handler.
418 * \param data currently unused.
419 * \param connection currently unusued.
420 * \param state currently unused.
421 * \param window The window to obtain update protocols from.
422 * \param name currently unused.
423 * \param reply currently unused.
425 static int
426 property_handle_wm_protocols(void *data,
427 xcb_connection_t *connection,
428 uint8_t state,
429 xcb_window_t window,
430 xcb_atom_t name,
431 xcb_get_property_reply_t *reply)
433 client_t *c = client_getbywin(window);
435 if(c)
436 property_update_wm_protocols(c, reply);
438 return 0;
441 /** The property notify event handler.
442 * \param data currently unused.
443 * \param connection The connection to the X server.
444 * \param state currently unused
445 * \param window The window to obtain update the property with.
446 * \param name The protocol atom, currently unused.
447 * \param reply (Optional) An existing reply.
449 static int
450 property_handle_xembed_info(void *data __attribute__ ((unused)),
451 xcb_connection_t *connection,
452 uint8_t state,
453 xcb_window_t window,
454 xcb_atom_t name,
455 xcb_get_property_reply_t *reply)
457 xembed_window_t *emwin = xembed_getbywin(&globalconf.embedded, window);
459 if(emwin)
460 xembed_property_update(connection, emwin, reply);
462 return 0;
465 static int
466 property_handle_xrootpmap_id(void *data __attribute__ ((unused)),
467 xcb_connection_t *connection,
468 uint8_t state,
469 xcb_window_t window,
470 xcb_atom_t name,
471 xcb_get_property_reply_t *reply)
473 if(globalconf.xinerama_is_active)
474 foreach(w, globalconf.wiboxes)
475 (*w)->need_update = true;
476 else
478 int screen = xutil_root2screen(connection, window);
479 foreach(w, globalconf.wiboxes)
480 if(screen == screen_array_indexof(&globalconf.screens, (*w)->screen))
481 (*w)->need_update = true;
484 return 0;
487 static int
488 property_handle_net_wm_opacity(void *data __attribute__ ((unused)),
489 xcb_connection_t *connection,
490 uint8_t state,
491 xcb_window_t window,
492 xcb_atom_t name,
493 xcb_get_property_reply_t *reply)
495 wibox_t *wibox = wibox_getbywin(window);
497 if(wibox)
499 luaA_object_push(globalconf.L, wibox);
500 wibox_set_opacity(globalconf.L, -1, window_opacity_get_from_reply(reply));
501 lua_pop(globalconf.L, -1);
503 else
505 client_t *c = client_getbywin(window);
506 if(c)
508 luaA_object_push(globalconf.L, c);
509 client_set_opacity(globalconf.L, -1, window_opacity_get_from_reply(reply));
510 lua_pop(globalconf.L, 1);
514 return 0;
517 void a_xcb_set_property_handlers(void)
519 /* init */
520 xcb_property_handlers_init(&globalconf.prophs, &globalconf.evenths);
522 /* Xembed stuff */
523 xcb_property_set_handler(&globalconf.prophs, _XEMBED_INFO, UINT_MAX,
524 property_handle_xembed_info, NULL);
526 /* ICCCM stuff */
527 xcb_property_set_handler(&globalconf.prophs, WM_TRANSIENT_FOR, UINT_MAX,
528 property_handle_wm_transient_for, NULL);
529 xcb_property_set_handler(&globalconf.prophs, WM_CLIENT_LEADER, UINT_MAX,
530 property_handle_wm_client_leader, NULL);
531 xcb_property_set_handler(&globalconf.prophs, WM_NORMAL_HINTS, UINT_MAX,
532 property_handle_wm_normal_hints, NULL);
533 xcb_property_set_handler(&globalconf.prophs, WM_HINTS, UINT_MAX,
534 property_handle_wm_hints, NULL);
535 xcb_property_set_handler(&globalconf.prophs, WM_NAME, UINT_MAX,
536 property_handle_wm_name, NULL);
537 xcb_property_set_handler(&globalconf.prophs, WM_ICON_NAME, UINT_MAX,
538 property_handle_wm_icon_name, NULL);
539 xcb_property_set_handler(&globalconf.prophs, WM_CLASS, UINT_MAX,
540 property_handle_wm_class, NULL);
541 xcb_property_set_handler(&globalconf.prophs, WM_PROTOCOLS, UINT_MAX,
542 property_handle_wm_protocols, NULL);
543 xcb_property_set_handler(&globalconf.prophs, WM_CLIENT_MACHINE, UINT_MAX,
544 property_handle_wm_client_machine, NULL);
545 xcb_property_set_handler(&globalconf.prophs, WM_WINDOW_ROLE, UINT_MAX,
546 property_handle_wm_window_role, NULL);
548 /* EWMH stuff */
549 xcb_property_set_handler(&globalconf.prophs, _NET_WM_NAME, UINT_MAX,
550 property_handle_wm_name, NULL);
551 xcb_property_set_handler(&globalconf.prophs, _NET_WM_ICON_NAME, UINT_MAX,
552 property_handle_wm_icon_name, NULL);
553 xcb_property_set_handler(&globalconf.prophs, _NET_WM_STRUT_PARTIAL, UINT_MAX,
554 property_handle_net_wm_strut_partial, NULL);
555 xcb_property_set_handler(&globalconf.prophs, _NET_WM_ICON, UINT_MAX,
556 property_handle_net_wm_icon, NULL);
557 xcb_property_set_handler(&globalconf.prophs, _NET_WM_PID, UINT_MAX,
558 property_handle_net_wm_pid, NULL);
559 xcb_property_set_handler(&globalconf.prophs, _NET_WM_WINDOW_OPACITY, 1,
560 property_handle_net_wm_opacity, NULL);
562 /* background change */
563 xcb_property_set_handler(&globalconf.prophs, _XROOTPMAP_ID, 1,
564 property_handle_xrootpmap_id, NULL);
567 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80