awful.rules: set focus as last property
[awesome.git] / property.c
blob591cbd20f6df12a57569d277220604293a179e63
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_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 #define HANDLE_PROPERTY(name) \
76 static int \
77 property_handle_##name(void *data, \
78 xcb_connection_t *connection, \
79 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, WINDOW, 0, 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);
211 /** Update WM_CLASS of a client.
212 * \param c The client.
213 * \param reply The reply to get property request, or NULL if none.
215 void
216 property_update_wm_class(client_t *c, xcb_get_property_reply_t *reply)
218 xcb_get_wm_class_reply_t hint;
220 if(reply)
222 if(!xcb_get_wm_class_from_reply(&hint, reply))
223 return;
225 else
227 if(!xcb_get_wm_class_reply(globalconf.connection,
228 xcb_get_wm_class_unchecked(globalconf.connection, c->window),
229 &hint, NULL))
230 return;
233 luaA_object_push(globalconf.L, c);
234 client_set_class_instance(globalconf.L, -1, hint.class_name, hint.instance_name);
235 lua_pop(globalconf.L, 1);
237 /* only delete reply if we get it ourselves */
238 if(!reply)
239 xcb_get_wm_class_reply_wipe(&hint);
242 static int
243 property_handle_net_wm_strut_partial(void *data,
244 xcb_connection_t *connection,
245 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, 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.
307 void
308 property_update_wm_protocols(client_t *c, xcb_get_property_reply_t *reply)
310 xcb_get_wm_protocols_reply_t protocols;
312 if(reply)
314 if(!xcb_get_wm_protocols_from_reply(reply, &protocols))
315 return;
317 else
319 /* If this fails for any reason, we still got the old value */
320 if(!xcb_get_wm_protocols_reply(globalconf.connection,
321 xcb_get_wm_protocols_unchecked(globalconf.connection,
322 c->window, WM_PROTOCOLS),
323 &protocols, NULL))
324 return;
327 xcb_get_wm_protocols_reply_wipe(&c->protocols);
328 memcpy(&c->protocols, &protocols, sizeof(protocols));
331 /** The property notify event handler.
332 * \param data currently unused.
333 * \param connection The connection to the X server.
334 * \param state currently unused
335 * \param window The window to obtain update the property with.
336 * \param name The protocol atom, currently unused.
337 * \param reply (Optional) An existing reply.
339 static int
340 property_handle_xembed_info(void *data __attribute__ ((unused)),
341 xcb_connection_t *connection,
342 uint8_t state,
343 xcb_window_t window,
344 xcb_atom_t name,
345 xcb_get_property_reply_t *reply)
347 xembed_window_t *emwin = xembed_getbywin(&globalconf.embedded, window);
349 if(emwin)
350 xembed_property_update(connection, emwin, reply);
352 return 0;
355 static int
356 property_handle_xrootpmap_id(void *data __attribute__ ((unused)),
357 xcb_connection_t *connection,
358 uint8_t state,
359 xcb_window_t window,
360 xcb_atom_t name,
361 xcb_get_property_reply_t *reply)
363 if(globalconf.xinerama_is_active)
364 foreach(w, globalconf.wiboxes)
365 (*w)->need_update = true;
366 else
368 int screen = xutil_root2screen(connection, window);
369 foreach(w, globalconf.wiboxes)
370 if(screen == screen_array_indexof(&globalconf.screens, (*w)->screen))
371 (*w)->need_update = true;
374 return 0;
377 static int
378 property_handle_net_wm_opacity(void *data __attribute__ ((unused)),
379 xcb_connection_t *connection,
380 uint8_t state,
381 xcb_window_t window,
382 xcb_atom_t name,
383 xcb_get_property_reply_t *reply)
385 wibox_t *wibox = wibox_getbywin(window);
387 if(wibox)
389 luaA_object_push(globalconf.L, wibox);
390 wibox_set_opacity(globalconf.L, -1, window_opacity_get_from_reply(reply));
391 lua_pop(globalconf.L, -1);
393 else
395 client_t *c = client_getbywin(window);
396 if(c)
398 luaA_object_push(globalconf.L, c);
399 client_set_opacity(globalconf.L, -1, window_opacity_get_from_reply(reply));
400 lua_pop(globalconf.L, 1);
404 return 0;
407 void a_xcb_set_property_handlers(void)
409 /* init */
410 xcb_property_handlers_init(&globalconf.prophs, &globalconf.evenths);
412 /* Xembed stuff */
413 xcb_property_set_handler(&globalconf.prophs, _XEMBED_INFO, UINT_MAX,
414 property_handle_xembed_info, NULL);
416 /* ICCCM stuff */
417 xcb_property_set_handler(&globalconf.prophs, WM_TRANSIENT_FOR, UINT_MAX,
418 property_handle_wm_transient_for, NULL);
419 xcb_property_set_handler(&globalconf.prophs, WM_CLIENT_LEADER, UINT_MAX,
420 property_handle_wm_client_leader, NULL);
421 xcb_property_set_handler(&globalconf.prophs, WM_NORMAL_HINTS, UINT_MAX,
422 property_handle_wm_normal_hints, NULL);
423 xcb_property_set_handler(&globalconf.prophs, WM_HINTS, UINT_MAX,
424 property_handle_wm_hints, NULL);
425 xcb_property_set_handler(&globalconf.prophs, WM_NAME, UINT_MAX,
426 property_handle_wm_name, NULL);
427 xcb_property_set_handler(&globalconf.prophs, WM_ICON_NAME, UINT_MAX,
428 property_handle_wm_icon_name, NULL);
429 xcb_property_set_handler(&globalconf.prophs, WM_CLASS, UINT_MAX,
430 property_handle_wm_class, NULL);
431 xcb_property_set_handler(&globalconf.prophs, WM_PROTOCOLS, UINT_MAX,
432 property_handle_wm_protocols, NULL);
433 xcb_property_set_handler(&globalconf.prophs, WM_CLIENT_MACHINE, UINT_MAX,
434 property_handle_wm_client_machine, NULL);
435 xcb_property_set_handler(&globalconf.prophs, WM_WINDOW_ROLE, UINT_MAX,
436 property_handle_wm_window_role, NULL);
438 /* EWMH stuff */
439 xcb_property_set_handler(&globalconf.prophs, _NET_WM_NAME, UINT_MAX,
440 property_handle_wm_name, NULL);
441 xcb_property_set_handler(&globalconf.prophs, _NET_WM_ICON_NAME, UINT_MAX,
442 property_handle_wm_icon_name, NULL);
443 xcb_property_set_handler(&globalconf.prophs, _NET_WM_STRUT_PARTIAL, UINT_MAX,
444 property_handle_net_wm_strut_partial, NULL);
445 xcb_property_set_handler(&globalconf.prophs, _NET_WM_ICON, UINT_MAX,
446 property_handle_net_wm_icon, NULL);
447 xcb_property_set_handler(&globalconf.prophs, _NET_WM_PID, UINT_MAX,
448 property_handle_net_wm_pid, NULL);
449 xcb_property_set_handler(&globalconf.prophs, _NET_WM_WINDOW_OPACITY, 1,
450 property_handle_net_wm_opacity, NULL);
452 /* background change */
453 xcb_property_set_handler(&globalconf.prophs, _XROOTPMAP_ID, 1,
454 property_handle_xrootpmap_id, NULL);
457 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80