Add a wallpaper_changed signal
[awesome.git] / property.c
blob610174628f8feed03a831184975c05c73154c13c
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 "objects/client.h"
27 #include "ewmh.h"
28 #include "objects/drawin.h"
29 #include "xwindow.h"
30 #include "luaa.h"
31 #include "common/atoms.h"
32 #include "common/xutil.h"
34 #define HANDLE_TEXT_PROPERTY(funcname, atom, setfunc) \
35 xcb_get_property_cookie_t \
36 property_get_##funcname(client_t *c) \
37 { \
38 return xcb_get_property(globalconf.connection, \
39 false, \
40 c->window, \
41 atom, \
42 XCB_GET_PROPERTY_TYPE_ANY, \
43 0, \
44 UINT_MAX); \
45 } \
46 void \
47 property_update_##funcname(client_t *c, xcb_get_property_cookie_t cookie) \
48 { \
49 xcb_get_property_reply_t * reply = \
50 xcb_get_property_reply(globalconf.connection, cookie, NULL); \
51 luaA_object_push(globalconf.L, c); \
52 setfunc(globalconf.L, -1, xutil_get_text_property_from_reply(reply)); \
53 lua_pop(globalconf.L, 1); \
54 p_delete(&reply); \
55 } \
56 static int \
57 property_handle_##funcname(uint8_t state, \
58 xcb_window_t window) \
59 { \
60 client_t *c = client_getbywin(window); \
61 if(c) \
62 property_update_##funcname(c, property_get_##funcname(c));\
63 return 0; \
67 HANDLE_TEXT_PROPERTY(wm_name, XCB_ATOM_WM_NAME, client_set_alt_name)
68 HANDLE_TEXT_PROPERTY(net_wm_name, _NET_WM_NAME, client_set_name)
69 HANDLE_TEXT_PROPERTY(wm_icon_name, XCB_ATOM_WM_ICON_NAME, client_set_alt_icon_name)
70 HANDLE_TEXT_PROPERTY(net_wm_icon_name, _NET_WM_ICON_NAME, client_set_icon_name)
71 HANDLE_TEXT_PROPERTY(wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, client_set_machine)
72 HANDLE_TEXT_PROPERTY(wm_window_role, WM_WINDOW_ROLE, client_set_role)
74 #undef HANDLE_TEXT_PROPERTY
76 #define HANDLE_PROPERTY(name) \
77 static int \
78 property_handle_##name(uint8_t state, \
79 xcb_window_t window) \
80 { \
81 client_t *c = client_getbywin(window); \
82 if(c) \
83 property_update_##name(c, property_get_##name(c));\
84 return 0; \
87 HANDLE_PROPERTY(wm_protocols)
88 HANDLE_PROPERTY(wm_transient_for)
89 HANDLE_PROPERTY(wm_client_leader)
90 HANDLE_PROPERTY(wm_normal_hints)
91 HANDLE_PROPERTY(wm_hints)
92 HANDLE_PROPERTY(wm_class)
93 HANDLE_PROPERTY(net_wm_icon)
94 HANDLE_PROPERTY(net_wm_pid)
96 #undef HANDLE_PROPERTY
98 xcb_get_property_cookie_t
99 property_get_wm_transient_for(client_t *c)
101 return xcb_icccm_get_wm_transient_for_unchecked(globalconf.connection, c->window);
104 void
105 property_update_wm_transient_for(client_t *c, xcb_get_property_cookie_t cookie)
107 xcb_window_t trans;
109 if(!xcb_icccm_get_wm_transient_for_reply(globalconf.connection,
110 cookie,
111 &trans, NULL))
112 return;
114 luaA_object_push(globalconf.L, c);
115 client_set_type(globalconf.L, -1, WINDOW_TYPE_DIALOG);
116 client_set_above(globalconf.L, -1, false);
117 client_set_transient_for(globalconf.L, -1, client_getbywin(trans));
118 lua_pop(globalconf.L, 1);
121 xcb_get_property_cookie_t
122 property_get_wm_client_leader(client_t *c)
124 return xcb_get_property_unchecked(globalconf.connection, false, c->window,
125 WM_CLIENT_LEADER, XCB_ATOM_WINDOW, 1, 32);
128 /** Update leader hint of a client.
129 * \param c The client.
130 * \param cookie Cookie returned by property_get_wm_client_leader.
132 void
133 property_update_wm_client_leader(client_t *c, xcb_get_property_cookie_t cookie)
135 xcb_get_property_reply_t *reply;
136 void *data;
138 reply = xcb_get_property_reply(globalconf.connection, cookie, NULL);
140 if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
141 c->leader_window = *(xcb_window_t *) data;
143 p_delete(&reply);
146 xcb_get_property_cookie_t
147 property_get_wm_normal_hints(client_t *c)
149 return xcb_icccm_get_wm_normal_hints_unchecked(globalconf.connection, c->window);
152 /** Update the size hints of a client.
153 * \param c The client.
154 * \param cookie Cookie returned by property_get_wm_normal_hints.
156 void
157 property_update_wm_normal_hints(client_t *c, xcb_get_property_cookie_t cookie)
159 xcb_icccm_get_wm_normal_hints_reply(globalconf.connection,
160 cookie,
161 &c->size_hints, NULL);
164 xcb_get_property_cookie_t
165 property_get_wm_hints(client_t *c)
167 return xcb_icccm_get_wm_hints_unchecked(globalconf.connection, c->window);
170 /** Update the WM hints of a client.
171 * \param c The client.
172 * \param cookie Cookie returned by property_get_wm_hints.
174 void
175 property_update_wm_hints(client_t *c, xcb_get_property_cookie_t cookie)
177 xcb_icccm_wm_hints_t wmh;
179 if(!xcb_icccm_get_wm_hints_reply(globalconf.connection,
180 cookie,
181 &wmh, NULL))
182 return;
184 luaA_object_push(globalconf.L, c);
185 client_set_urgent(globalconf.L, -1, xcb_icccm_wm_hints_get_urgency(&wmh));
187 if(wmh.flags & XCB_ICCCM_WM_HINT_INPUT)
188 c->nofocus = !wmh.input;
190 if(wmh.flags & XCB_ICCCM_WM_HINT_WINDOW_GROUP)
191 client_set_group_window(globalconf.L, -1, wmh.window_group);
193 lua_pop(globalconf.L, 1);
196 xcb_get_property_cookie_t
197 property_get_wm_class(client_t *c)
199 return xcb_icccm_get_wm_class_unchecked(globalconf.connection, c->window);
202 /** Update WM_CLASS of a client.
203 * \param c The client.
204 * \param cookie Cookie returned by property_get_wm_class.
206 void
207 property_update_wm_class(client_t *c, xcb_get_property_cookie_t cookie)
209 xcb_icccm_get_wm_class_reply_t hint;
211 if(!xcb_icccm_get_wm_class_reply(globalconf.connection,
212 cookie,
213 &hint, NULL))
214 return;
216 luaA_object_push(globalconf.L, c);
217 client_set_class_instance(globalconf.L, -1, hint.class_name, hint.instance_name);
218 lua_pop(globalconf.L, 1);
220 xcb_icccm_get_wm_class_reply_wipe(&hint);
223 static int
224 property_handle_net_wm_strut_partial(uint8_t state,
225 xcb_window_t window)
227 client_t *c = client_getbywin(window);
229 if(c)
230 ewmh_process_client_strut(c);
232 return 0;
235 xcb_get_property_cookie_t
236 property_get_net_wm_icon(client_t *c)
238 return ewmh_window_icon_get_unchecked(c->window);
241 void
242 property_update_net_wm_icon(client_t *c, xcb_get_property_cookie_t cookie)
244 luaA_object_push(globalconf.L, c);
246 if(ewmh_window_icon_get_reply(cookie))
248 client_set_icon(globalconf.L, -2, -1);
249 /* remove icon */
250 lua_pop(globalconf.L, 1);
253 /* remove client */
254 lua_pop(globalconf.L, 1);
257 xcb_get_property_cookie_t
258 property_get_net_wm_pid(client_t *c)
260 return xcb_get_property_unchecked(globalconf.connection, false, c->window, _NET_WM_PID, XCB_ATOM_CARDINAL, 0L, 1L);
263 void
264 property_update_net_wm_pid(client_t *c, xcb_get_property_cookie_t cookie)
266 xcb_get_property_reply_t *reply;
268 reply = xcb_get_property_reply(globalconf.connection, cookie, NULL);
270 if(reply && reply->value_len)
272 uint32_t *rdata = xcb_get_property_value(reply);
273 if(rdata)
275 luaA_object_push(globalconf.L, c);
276 client_set_pid(globalconf.L, -1, *rdata);
277 lua_pop(globalconf.L, 1);
281 p_delete(&reply);
284 xcb_get_property_cookie_t
285 property_get_wm_protocols(client_t *c)
287 return xcb_icccm_get_wm_protocols_unchecked(globalconf.connection,
288 c->window, WM_PROTOCOLS);
291 /** Update the list of supported protocols for a client.
292 * \param c The client.
293 * \param cookie Cookie from property_get_wm_protocols.
295 void
296 property_update_wm_protocols(client_t *c, xcb_get_property_cookie_t cookie)
298 xcb_icccm_get_wm_protocols_reply_t protocols;
300 /* If this fails for any reason, we still got the old value */
301 if(!xcb_icccm_get_wm_protocols_reply(globalconf.connection,
302 cookie,
303 &protocols, NULL))
304 return;
306 xcb_icccm_get_wm_protocols_reply_wipe(&c->protocols);
307 memcpy(&c->protocols, &protocols, sizeof(protocols));
310 /** The property notify event handler.
311 * \param state currently unused
312 * \param window The window to obtain update the property with.
313 * \param name The protocol atom, currently unused.
314 * \param reply (Optional) An existing reply.
316 static int
317 property_handle_xembed_info(uint8_t state,
318 xcb_window_t window)
320 xembed_window_t *emwin = xembed_getbywin(&globalconf.embedded, window);
322 if(emwin)
324 xcb_get_property_cookie_t cookie =
325 xcb_get_property(globalconf.connection, 0, window, _XEMBED_INFO,
326 XCB_GET_PROPERTY_TYPE_ANY, 0, 3);
327 xcb_get_property_reply_t *propr =
328 xcb_get_property_reply(globalconf.connection, cookie, 0);
329 xembed_property_update(globalconf.connection, emwin, propr);
330 p_delete(&propr);
333 return 0;
336 static int
337 property_handle_net_wm_opacity(uint8_t state,
338 xcb_window_t window)
340 drawin_t *drawin = drawin_getbywin(window);
342 if(drawin)
344 luaA_object_push(globalconf.L, drawin);
345 window_set_opacity(globalconf.L, -1, xwindow_get_opacity(drawin->window));
346 lua_pop(globalconf.L, -1);
348 else
350 client_t *c = client_getbywin(window);
351 if(c)
353 luaA_object_push(globalconf.L, c);
354 window_set_opacity(globalconf.L, -1, xwindow_get_opacity(c->window));
355 lua_pop(globalconf.L, 1);
359 return 0;
362 static int
363 property_handle_xrootpmap_id(uint8_t state,
364 xcb_window_t window)
366 signal_object_emit(globalconf.L, &global_signals, "wallpaper_changed", 0);
367 return 0;
371 /** The property notify event handler.
372 * \param data Unused data.
373 * \param connection The connection to the X server.
374 * \param ev The event.
375 * \return Status code, 0 if everything's fine.
377 void
378 property_handle_propertynotify(xcb_property_notify_event_t *ev)
380 int (*handler)(uint8_t state,
381 xcb_window_t window) = NULL;
383 globalconf.timestamp = ev->time;
385 /* Find the correct event handler */
386 #define HANDLE(atom_, cb) \
387 if (ev->atom == atom_) \
389 handler = cb; \
390 } else
391 #define END return
393 /* Xembed stuff */
394 HANDLE(_XEMBED_INFO, property_handle_xembed_info)
396 /* ICCCM stuff */
397 HANDLE(XCB_ATOM_WM_TRANSIENT_FOR, property_handle_wm_transient_for)
398 HANDLE(WM_CLIENT_LEADER, property_handle_wm_client_leader)
399 HANDLE(XCB_ATOM_WM_NORMAL_HINTS, property_handle_wm_normal_hints)
400 HANDLE(XCB_ATOM_WM_HINTS, property_handle_wm_hints)
401 HANDLE(XCB_ATOM_WM_NAME, property_handle_wm_name)
402 HANDLE(XCB_ATOM_WM_ICON_NAME, property_handle_wm_icon_name)
403 HANDLE(XCB_ATOM_WM_CLASS, property_handle_wm_class)
404 HANDLE(WM_PROTOCOLS, property_handle_wm_protocols)
405 HANDLE(XCB_ATOM_WM_CLIENT_MACHINE, property_handle_wm_client_machine)
406 HANDLE(WM_WINDOW_ROLE, property_handle_wm_window_role)
408 /* EWMH stuff */
409 HANDLE(_NET_WM_NAME, property_handle_net_wm_name)
410 HANDLE(_NET_WM_ICON_NAME, property_handle_net_wm_icon_name)
411 HANDLE(_NET_WM_STRUT_PARTIAL, property_handle_net_wm_strut_partial)
412 HANDLE(_NET_WM_ICON, property_handle_net_wm_icon)
413 HANDLE(_NET_WM_PID, property_handle_net_wm_pid)
414 HANDLE(_NET_WM_WINDOW_OPACITY, property_handle_net_wm_opacity)
416 /* background change */
417 HANDLE(_XROOTPMAP_ID, property_handle_xrootpmap_id)
419 /* If nothing was found, return */
420 END;
422 #undef HANDLE
423 #undef END
425 (*handler)(ev->state, ev->window);
428 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80