awful.widget: add graph stack property
[awesome.git] / property.c
blob8cc68060a7b3ac965119f2094ea586f7264a7434
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 atom, \
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_alt_name)
69 HANDLE_TEXT_PROPERTY(net_wm_name, _NET_WM_NAME, client_set_name)
70 HANDLE_TEXT_PROPERTY(wm_icon_name, 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, 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) \
78 static int \
79 property_handle_##name(void *data, \
80 xcb_connection_t *connection, \
81 uint8_t state, \
82 xcb_window_t window, \
83 xcb_atom_t name, \
84 xcb_get_property_reply_t *reply) \
85 { \
86 client_t *c = client_getbywin(window); \
87 if(c) \
88 property_update_##name(c, reply); \
89 return 0; \
92 HANDLE_PROPERTY(wm_protocols)
93 HANDLE_PROPERTY(wm_transient_for)
94 HANDLE_PROPERTY(wm_client_leader)
95 HANDLE_PROPERTY(wm_normal_hints)
96 HANDLE_PROPERTY(wm_hints)
97 HANDLE_PROPERTY(wm_class)
98 HANDLE_PROPERTY(net_wm_icon)
99 HANDLE_PROPERTY(net_wm_pid)
101 #undef HANDLE_PROPERTY
103 void
104 property_update_wm_transient_for(client_t *c, xcb_get_property_reply_t *reply)
106 xcb_window_t trans;
108 if(reply)
110 if(!xcb_get_wm_transient_for_from_reply(&trans, reply))
111 return;
113 else
115 if(!xcb_get_wm_transient_for_reply(globalconf.connection,
116 xcb_get_wm_transient_for_unchecked(globalconf.connection,
117 c->window),
118 &trans, NULL))
119 return;
122 luaA_object_push(globalconf.L, c);
123 client_set_type(globalconf.L, -1, WINDOW_TYPE_DIALOG);
124 client_set_above(globalconf.L, -1, false);
125 client_set_transient_for(globalconf.L, -1, client_getbywin(trans));
126 lua_pop(globalconf.L, 1);
129 /** Update leader hint of a client.
130 * \param c The client.
131 * \param reply (Optional) An existing reply.
133 void
134 property_update_wm_client_leader(client_t *c, xcb_get_property_reply_t *reply)
136 xcb_get_property_cookie_t client_leader_q;
137 void *data;
138 bool no_reply = !reply;
140 if(no_reply)
142 client_leader_q = xcb_get_property_unchecked(globalconf.connection, false, c->window,
143 WM_CLIENT_LEADER, WINDOW, 0, 32);
145 reply = xcb_get_property_reply(globalconf.connection, client_leader_q, NULL);
148 if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
149 c->leader_window = *(xcb_window_t *) data;
151 /* Only free when we created a reply ourselves. */
152 if(no_reply)
153 p_delete(&reply);
156 /** Update the size hints of a client.
157 * \param c The client.
158 * \param reply (Optional) An existing reply.
160 void
161 property_update_wm_normal_hints(client_t *c, xcb_get_property_reply_t *reply)
163 if(reply)
165 if(!xcb_get_wm_size_hints_from_reply(&c->size_hints, reply))
166 return;
168 else
170 if(!xcb_get_wm_normal_hints_reply(globalconf.connection,
171 xcb_get_wm_normal_hints_unchecked(globalconf.connection,
172 c->window),
173 &c->size_hints, NULL))
174 return;
178 /** Update the WM hints of a client.
179 * \param c The client.
180 * \param reply (Optional) An existing reply.
182 void
183 property_update_wm_hints(client_t *c, xcb_get_property_reply_t *reply)
185 xcb_wm_hints_t wmh;
187 if(reply)
189 if(!xcb_get_wm_hints_from_reply(&wmh, reply))
190 return;
192 else
194 if(!xcb_get_wm_hints_reply(globalconf.connection,
195 xcb_get_wm_hints_unchecked(globalconf.connection, c->window),
196 &wmh, NULL))
197 return;
200 luaA_object_push(globalconf.L, c);
201 client_set_urgent(globalconf.L, -1, xcb_wm_hints_get_urgency(&wmh));
202 if(wmh.flags & XCB_WM_HINT_STATE &&
203 wmh.initial_state == XCB_WM_STATE_WITHDRAWN)
204 client_set_border_width(globalconf.L, -1, 0);
206 if(wmh.flags & XCB_WM_HINT_INPUT)
207 c->nofocus = !wmh.input;
209 if(wmh.flags & XCB_WM_HINT_WINDOW_GROUP)
210 client_set_group_window(globalconf.L, -1, wmh.window_group);
212 lua_pop(globalconf.L, 1);
215 /** Update WM_CLASS of a client.
216 * \param c The client.
217 * \param reply The reply to get property request, or NULL if none.
219 void
220 property_update_wm_class(client_t *c, xcb_get_property_reply_t *reply)
222 xcb_get_wm_class_reply_t hint;
224 if(reply)
226 if(!xcb_get_wm_class_from_reply(&hint, reply))
227 return;
229 else
231 if(!xcb_get_wm_class_reply(globalconf.connection,
232 xcb_get_wm_class_unchecked(globalconf.connection, c->window),
233 &hint, NULL))
234 return;
237 luaA_object_push(globalconf.L, c);
238 client_set_class_instance(globalconf.L, -1, hint.class_name, hint.instance_name);
239 lua_pop(globalconf.L, 1);
241 /* only delete reply if we get it ourselves */
242 if(!reply)
243 xcb_get_wm_class_reply_wipe(&hint);
246 static int
247 property_handle_net_wm_strut_partial(void *data,
248 xcb_connection_t *connection,
249 uint8_t state,
250 xcb_window_t window,
251 xcb_atom_t name,
252 xcb_get_property_reply_t *reply)
254 client_t *c = client_getbywin(window);
256 if(c)
257 ewmh_process_client_strut(c, reply);
259 return 0;
262 void
263 property_update_net_wm_icon(client_t *c,
264 xcb_get_property_reply_t *reply)
266 luaA_object_push(globalconf.L, c);
268 if(reply)
270 if(ewmh_window_icon_from_reply(reply))
271 client_set_icon(globalconf.L, -2, -1);
273 else if(ewmh_window_icon_get_reply(ewmh_window_icon_get_unchecked(c->window)))
274 client_set_icon(globalconf.L, -2, -1);
276 /* remove client */
277 lua_pop(globalconf.L, 1);
280 void
281 property_update_net_wm_pid(client_t *c,
282 xcb_get_property_reply_t *reply)
284 bool no_reply = !reply;
286 if(no_reply)
288 xcb_get_property_cookie_t prop_c =
289 xcb_get_property_unchecked(globalconf.connection, false, c->window, _NET_WM_PID, CARDINAL, 0L, 1L);
290 reply = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
293 if(reply && reply->value_len)
295 uint32_t *rdata = xcb_get_property_value(reply);
296 if(rdata)
298 luaA_object_push(globalconf.L, c);
299 client_set_pid(globalconf.L, -1, *rdata);
300 lua_pop(globalconf.L, 1);
304 if(no_reply)
305 p_delete(&reply);
308 /** Update the list of supported protocols for a client.
309 * \param c The client.
310 * \param reply The xcb property reply.
312 void
313 property_update_wm_protocols(client_t *c, xcb_get_property_reply_t *reply)
315 xcb_get_wm_protocols_reply_t protocols;
316 xcb_get_property_reply_t *reply_copy;
318 if(reply)
320 reply_copy = p_dup(reply, 1);
322 if(!xcb_get_wm_protocols_from_reply(reply_copy, &protocols))
324 p_delete(&reply_copy);
325 return;
328 else
330 /* If this fails for any reason, we still got the old value */
331 if(!xcb_get_wm_protocols_reply(globalconf.connection,
332 xcb_get_wm_protocols_unchecked(globalconf.connection,
333 c->window, WM_PROTOCOLS),
334 &protocols, NULL))
335 return;
338 xcb_get_wm_protocols_reply_wipe(&c->protocols);
339 memcpy(&c->protocols, &protocols, sizeof(protocols));
342 /** The property notify event handler.
343 * \param data currently unused.
344 * \param connection The connection to the X server.
345 * \param state currently unused
346 * \param window The window to obtain update the property with.
347 * \param name The protocol atom, currently unused.
348 * \param reply (Optional) An existing reply.
350 static int
351 property_handle_xembed_info(void *data __attribute__ ((unused)),
352 xcb_connection_t *connection,
353 uint8_t state,
354 xcb_window_t window,
355 xcb_atom_t name,
356 xcb_get_property_reply_t *reply)
358 xembed_window_t *emwin = xembed_getbywin(&globalconf.embedded, window);
360 if(emwin)
361 xembed_property_update(connection, emwin, reply);
363 return 0;
366 static int
367 property_handle_xrootpmap_id(void *data __attribute__ ((unused)),
368 xcb_connection_t *connection,
369 uint8_t state,
370 xcb_window_t window,
371 xcb_atom_t name,
372 xcb_get_property_reply_t *reply)
374 if(globalconf.xinerama_is_active)
375 foreach(w, globalconf.wiboxes)
376 (*w)->need_update = true;
377 else
379 int screen = xutil_root2screen(connection, window);
380 foreach(w, globalconf.wiboxes)
381 if(screen == screen_array_indexof(&globalconf.screens, (*w)->screen))
382 (*w)->need_update = true;
385 return 0;
388 static int
389 property_handle_net_wm_opacity(void *data __attribute__ ((unused)),
390 xcb_connection_t *connection,
391 uint8_t state,
392 xcb_window_t window,
393 xcb_atom_t name,
394 xcb_get_property_reply_t *reply)
396 wibox_t *wibox = wibox_getbywin(window);
398 if(wibox)
400 luaA_object_push(globalconf.L, wibox);
401 wibox_set_opacity(globalconf.L, -1, window_opacity_get_from_reply(reply));
402 lua_pop(globalconf.L, -1);
404 else
406 client_t *c = client_getbywin(window);
407 if(c)
409 luaA_object_push(globalconf.L, c);
410 client_set_opacity(globalconf.L, -1, window_opacity_get_from_reply(reply));
411 lua_pop(globalconf.L, 1);
415 return 0;
418 void a_xcb_set_property_handlers(void)
420 /* init */
421 xcb_property_handlers_init(&globalconf.prophs, &globalconf.evenths);
423 /* Xembed stuff */
424 xcb_property_set_handler(&globalconf.prophs, _XEMBED_INFO, UINT_MAX,
425 property_handle_xembed_info, NULL);
427 /* ICCCM stuff */
428 xcb_property_set_handler(&globalconf.prophs, WM_TRANSIENT_FOR, UINT_MAX,
429 property_handle_wm_transient_for, NULL);
430 xcb_property_set_handler(&globalconf.prophs, WM_CLIENT_LEADER, UINT_MAX,
431 property_handle_wm_client_leader, NULL);
432 xcb_property_set_handler(&globalconf.prophs, WM_NORMAL_HINTS, UINT_MAX,
433 property_handle_wm_normal_hints, NULL);
434 xcb_property_set_handler(&globalconf.prophs, WM_HINTS, UINT_MAX,
435 property_handle_wm_hints, NULL);
436 xcb_property_set_handler(&globalconf.prophs, WM_NAME, UINT_MAX,
437 property_handle_wm_name, NULL);
438 xcb_property_set_handler(&globalconf.prophs, WM_ICON_NAME, UINT_MAX,
439 property_handle_wm_icon_name, NULL);
440 xcb_property_set_handler(&globalconf.prophs, WM_CLASS, UINT_MAX,
441 property_handle_wm_class, NULL);
442 xcb_property_set_handler(&globalconf.prophs, WM_PROTOCOLS, UINT_MAX,
443 property_handle_wm_protocols, NULL);
444 xcb_property_set_handler(&globalconf.prophs, WM_CLIENT_MACHINE, UINT_MAX,
445 property_handle_wm_client_machine, NULL);
446 xcb_property_set_handler(&globalconf.prophs, WM_WINDOW_ROLE, UINT_MAX,
447 property_handle_wm_window_role, NULL);
449 /* EWMH stuff */
450 xcb_property_set_handler(&globalconf.prophs, _NET_WM_NAME, UINT_MAX,
451 property_handle_net_wm_name, NULL);
452 xcb_property_set_handler(&globalconf.prophs, _NET_WM_ICON_NAME, UINT_MAX,
453 property_handle_net_wm_icon_name, NULL);
454 xcb_property_set_handler(&globalconf.prophs, _NET_WM_STRUT_PARTIAL, UINT_MAX,
455 property_handle_net_wm_strut_partial, NULL);
456 xcb_property_set_handler(&globalconf.prophs, _NET_WM_ICON, UINT_MAX,
457 property_handle_net_wm_icon, NULL);
458 xcb_property_set_handler(&globalconf.prophs, _NET_WM_PID, UINT_MAX,
459 property_handle_net_wm_pid, NULL);
460 xcb_property_set_handler(&globalconf.prophs, _NET_WM_WINDOW_OPACITY, 1,
461 property_handle_net_wm_opacity, NULL);
463 /* background change */
464 xcb_property_set_handler(&globalconf.prophs, _XROOTPMAP_ID, 1,
465 property_handle_xrootpmap_id, NULL);
468 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80