Track the last timestamp received from the server
[awesome.git] / property.c
blobd5090c633b9d5062275e7ac88baad6b9c89c4e93
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_property(globalconf.connection, \
43 false, \
44 c->window, \
45 atom, \
46 XCB_GET_PROPERTY_TYPE_ANY, \
47 0, \
48 UINT_MAX), 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); \
52 if(no_reply) \
53 p_delete(&reply); \
54 } \
55 static int \
56 property_handle_##funcname(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, 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) \
78 static int \
79 property_handle_##name(uint8_t state, \
80 xcb_window_t window, \
81 xcb_atom_t name, \
82 xcb_get_property_reply_t *reply) \
83 { \
84 client_t *c = client_getbywin(window); \
85 if(c) \
86 property_update_##name(c, reply); \
87 return 0; \
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
101 void
102 property_update_wm_transient_for(client_t *c, xcb_get_property_reply_t *reply)
104 xcb_window_t trans;
106 if(reply)
108 if(!xcb_get_wm_transient_for_from_reply(&trans, reply))
109 return;
111 else
113 if(!xcb_get_wm_transient_for_reply(globalconf.connection,
114 xcb_get_wm_transient_for_unchecked(globalconf.connection,
115 c->window),
116 &trans, NULL))
117 return;
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.
131 void
132 property_update_wm_client_leader(client_t *c, xcb_get_property_reply_t *reply)
134 xcb_get_property_cookie_t client_leader_q;
135 void *data;
136 bool no_reply = !reply;
138 if(no_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. */
150 if(no_reply)
151 p_delete(&reply);
154 /** Update the size hints of a client.
155 * \param c The client.
156 * \param reply (Optional) An existing reply.
158 void
159 property_update_wm_normal_hints(client_t *c, xcb_get_property_reply_t *reply)
161 if(reply)
163 if(!xcb_get_wm_size_hints_from_reply(&c->size_hints, reply))
164 return;
166 else
168 if(!xcb_get_wm_normal_hints_reply(globalconf.connection,
169 xcb_get_wm_normal_hints_unchecked(globalconf.connection,
170 c->window),
171 &c->size_hints, NULL))
172 return;
176 /** Update the WM hints of a client.
177 * \param c The client.
178 * \param reply (Optional) An existing reply.
180 void
181 property_update_wm_hints(client_t *c, xcb_get_property_reply_t *reply)
183 xcb_wm_hints_t wmh;
185 if(reply)
187 if(!xcb_get_wm_hints_from_reply(&wmh, reply))
188 return;
190 else
192 if(!xcb_get_wm_hints_reply(globalconf.connection,
193 xcb_get_wm_hints_unchecked(globalconf.connection, c->window),
194 &wmh, NULL))
195 return;
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.
217 void
218 property_update_wm_class(client_t *c, xcb_get_property_reply_t *reply)
220 xcb_get_wm_class_reply_t hint;
222 if(reply)
224 if(!xcb_get_wm_class_from_reply(&hint, reply))
225 return;
227 else
229 if(!xcb_get_wm_class_reply(globalconf.connection,
230 xcb_get_wm_class_unchecked(globalconf.connection, c->window),
231 &hint, NULL))
232 return;
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 */
240 if(!reply)
241 xcb_get_wm_class_reply_wipe(&hint);
244 static int
245 property_handle_net_wm_strut_partial(uint8_t state,
246 xcb_window_t window,
247 xcb_atom_t name,
248 xcb_get_property_reply_t *reply)
250 client_t *c = client_getbywin(window);
252 if(c)
253 ewmh_process_client_strut(c, reply);
255 return 0;
258 void
259 property_update_net_wm_icon(client_t *c,
260 xcb_get_property_reply_t *reply)
262 luaA_object_push(globalconf.L, c);
264 if(reply)
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);
272 /* remove client */
273 lua_pop(globalconf.L, 1);
276 void
277 property_update_net_wm_pid(client_t *c,
278 xcb_get_property_reply_t *reply)
280 bool no_reply = !reply;
282 if(no_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);
292 if(rdata)
294 luaA_object_push(globalconf.L, c);
295 client_set_pid(globalconf.L, -1, *rdata);
296 lua_pop(globalconf.L, 1);
300 if(no_reply)
301 p_delete(&reply);
304 /** Update the list of supported protocols for a client.
305 * \param c The client.
306 * \param reply The xcb property reply.
308 void
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;
314 if(reply)
316 reply_copy = p_dup(reply, 1);
318 if(!xcb_get_wm_protocols_from_reply(reply_copy, &protocols))
320 p_delete(&reply_copy);
321 return;
324 else
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),
330 &protocols, NULL))
331 return;
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.
344 static int
345 property_handle_xembed_info(uint8_t state,
346 xcb_window_t window,
347 xcb_atom_t name,
348 xcb_get_property_reply_t *reply)
350 xembed_window_t *emwin = xembed_getbywin(&globalconf.embedded, window);
352 if(emwin)
353 xembed_property_update(globalconf.connection, emwin, reply);
355 return 0;
358 static int
359 property_handle_xrootpmap_id(uint8_t state,
360 xcb_window_t window,
361 xcb_atom_t name,
362 xcb_get_property_reply_t *reply)
364 if(globalconf.xinerama_is_active)
365 foreach(w, globalconf.wiboxes)
366 (*w)->need_update = true;
367 else
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;
375 return 0;
378 static int
379 property_handle_net_wm_opacity(uint8_t state,
380 xcb_window_t window,
381 xcb_atom_t name,
382 xcb_get_property_reply_t *reply)
384 wibox_t *wibox = wibox_getbywin(window);
386 if(wibox)
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);
392 else
394 client_t *c = client_getbywin(window);
395 if(c)
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);
403 return 0;
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.
412 void
413 property_handle_propertynotify(xcb_property_notify_event_t *ev)
415 uint32_t length;
416 int (*handler)(uint8_t state,
417 xcb_window_t window,
418 xcb_atom_t name,
419 xcb_get_property_reply_t *reply) = NULL;
421 globalconf.timestamp = ev->time;
423 /* Find the correct event handler */
424 #define HANDLE(atom_, cb, len) \
425 if (ev->atom == atom_) \
427 handler = cb; \
428 length = len; \
429 } else
430 #define HANDLE_L(atom, cb) HANDLE(atom, cb, UINT_MAX)
431 #define HANDLE_S(atom, cb) HANDLE(atom, cb, 1)
432 #define END return
434 /* Xembed stuff */
435 HANDLE_L(_XEMBED_INFO, property_handle_xembed_info)
437 /* ICCCM stuff */
438 HANDLE_L(XCB_ATOM_WM_TRANSIENT_FOR, property_handle_wm_transient_for)
439 HANDLE_L(WM_CLIENT_LEADER, property_handle_wm_client_leader)
440 HANDLE_L(XCB_ATOM_WM_NORMAL_HINTS, property_handle_wm_normal_hints)
441 HANDLE_L(XCB_ATOM_WM_HINTS, property_handle_wm_hints)
442 HANDLE_L(XCB_ATOM_WM_NAME, property_handle_wm_name)
443 HANDLE_L(XCB_ATOM_WM_ICON_NAME, property_handle_wm_icon_name)
444 HANDLE_L(XCB_ATOM_WM_CLASS, property_handle_wm_class)
445 HANDLE_L(WM_PROTOCOLS, property_handle_wm_protocols)
446 HANDLE_L(XCB_ATOM_WM_CLIENT_MACHINE, property_handle_wm_client_machine)
447 HANDLE_L(WM_WINDOW_ROLE, property_handle_wm_window_role)
449 /* EWMH stuff */
450 HANDLE_L(_NET_WM_NAME, property_handle_net_wm_name)
451 HANDLE_L(_NET_WM_ICON_NAME, property_handle_net_wm_icon_name)
452 HANDLE_L(_NET_WM_STRUT_PARTIAL, property_handle_net_wm_strut_partial)
453 HANDLE_L(_NET_WM_ICON, property_handle_net_wm_icon)
454 HANDLE_L(_NET_WM_PID, property_handle_net_wm_pid)
455 HANDLE_S(_NET_WM_WINDOW_OPACITY, property_handle_net_wm_opacity)
457 /* background change */
458 HANDLE_S(_XROOTPMAP_ID, property_handle_xrootpmap_id)
460 /* If nothing was found, return */
461 END;
463 #undef HANDLE_L
464 #undef HANDLE_S
465 #undef END
467 /* Get the property, if needed. */
468 xcb_get_property_reply_t *propr = NULL;
469 if(ev->state != XCB_PROPERTY_DELETE)
471 xcb_get_property_cookie_t cookie =
472 xcb_get_property(globalconf.connection, 0, ev->window, ev->atom,
473 XCB_GET_PROPERTY_TYPE_ANY, 0, length);
474 propr = xcb_get_property_reply(globalconf.connection, cookie, 0);
477 (*handler)(ev->state, ev->window, ev->atom, propr);
478 p_delete(&propr);
481 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80