Merge branch 'fix-ldoc-set-spacing' of git://github.com/actionless/awesome
[awesome.git] / property.c
blob8b77ded68cd4d03f39b7cd14fe4b0d3b59af8925
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 "property.h"
23 #include "common/atoms.h"
24 #include "common/xutil.h"
25 #include "ewmh.h"
26 #include "objects/client.h"
27 #include "objects/drawin.h"
28 #include "xwindow.h"
30 #include <xcb/xcb_atom.h>
32 #define HANDLE_TEXT_PROPERTY(funcname, atom, setfunc) \
33 xcb_get_property_cookie_t \
34 property_get_##funcname(client_t *c) \
35 { \
36 return xcb_get_property(globalconf.connection, \
37 false, \
38 c->window, \
39 atom, \
40 XCB_GET_PROPERTY_TYPE_ANY, \
41 0, \
42 UINT_MAX); \
43 } \
44 void \
45 property_update_##funcname(client_t *c, xcb_get_property_cookie_t cookie) \
46 { \
47 xcb_get_property_reply_t * reply = \
48 xcb_get_property_reply(globalconf.connection, cookie, 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 p_delete(&reply); \
53 } \
54 static int \
55 property_handle_##funcname(uint8_t state, \
56 xcb_window_t window) \
57 { \
58 client_t *c = client_getbywin(window); \
59 if(c) \
60 property_update_##funcname(c, property_get_##funcname(c));\
61 return 0; \
65 HANDLE_TEXT_PROPERTY(wm_name, XCB_ATOM_WM_NAME, client_set_alt_name)
66 HANDLE_TEXT_PROPERTY(net_wm_name, _NET_WM_NAME, client_set_name)
67 HANDLE_TEXT_PROPERTY(wm_icon_name, XCB_ATOM_WM_ICON_NAME, client_set_alt_icon_name)
68 HANDLE_TEXT_PROPERTY(net_wm_icon_name, _NET_WM_ICON_NAME, client_set_icon_name)
69 HANDLE_TEXT_PROPERTY(wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, client_set_machine)
70 HANDLE_TEXT_PROPERTY(wm_window_role, WM_WINDOW_ROLE, client_set_role)
72 #undef HANDLE_TEXT_PROPERTY
74 #define HANDLE_PROPERTY(name) \
75 static int \
76 property_handle_##name(uint8_t state, \
77 xcb_window_t window) \
78 { \
79 client_t *c = client_getbywin(window); \
80 if(c) \
81 property_update_##name(c, property_get_##name(c));\
82 return 0; \
85 HANDLE_PROPERTY(wm_protocols)
86 HANDLE_PROPERTY(wm_transient_for)
87 HANDLE_PROPERTY(wm_client_leader)
88 HANDLE_PROPERTY(wm_normal_hints)
89 HANDLE_PROPERTY(wm_hints)
90 HANDLE_PROPERTY(wm_class)
91 HANDLE_PROPERTY(net_wm_icon)
92 HANDLE_PROPERTY(net_wm_pid)
94 #undef HANDLE_PROPERTY
96 xcb_get_property_cookie_t
97 property_get_wm_transient_for(client_t *c)
99 return xcb_icccm_get_wm_transient_for_unchecked(globalconf.connection, c->window);
102 void
103 property_update_wm_transient_for(client_t *c, xcb_get_property_cookie_t cookie)
105 xcb_window_t trans;
106 int counter;
107 client_t *tc, *tmp;
109 if(!xcb_icccm_get_wm_transient_for_reply(globalconf.connection,
110 cookie,
111 &trans, NULL))
112 return;
114 tmp = tc = client_getbywin(trans);
116 luaA_object_push(globalconf.L, c);
117 client_set_type(globalconf.L, -1, WINDOW_TYPE_DIALOG);
118 client_set_above(globalconf.L, -1, false);
120 /* Verify that there are no loops in the transient_for relation after we are done */
121 for(counter = 0; tmp != NULL && counter <= globalconf.stack.len; counter++)
123 if (tmp == c)
124 /* We arrived back at the client we started from, so there is a loop */
125 counter = globalconf.stack.len+1;
126 tmp = tmp->transient_for;
128 if (counter <= globalconf.stack.len)
129 client_set_transient_for(globalconf.L, -1, tc);
131 lua_pop(globalconf.L, 1);
134 xcb_get_property_cookie_t
135 property_get_wm_client_leader(client_t *c)
137 return xcb_get_property_unchecked(globalconf.connection, false, c->window,
138 WM_CLIENT_LEADER, XCB_ATOM_WINDOW, 0, 32);
141 /** Update leader hint of a client.
142 * \param c The client.
143 * \param cookie Cookie returned by property_get_wm_client_leader.
145 void
146 property_update_wm_client_leader(client_t *c, xcb_get_property_cookie_t cookie)
148 xcb_get_property_reply_t *reply;
149 void *data;
151 reply = xcb_get_property_reply(globalconf.connection, cookie, NULL);
153 if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
154 c->leader_window = *(xcb_window_t *) data;
156 p_delete(&reply);
159 xcb_get_property_cookie_t
160 property_get_wm_normal_hints(client_t *c)
162 return xcb_icccm_get_wm_normal_hints_unchecked(globalconf.connection, c->window);
165 /** Update the size hints of a client.
166 * \param c The client.
167 * \param cookie Cookie returned by property_get_wm_normal_hints.
169 void
170 property_update_wm_normal_hints(client_t *c, xcb_get_property_cookie_t cookie)
172 xcb_icccm_get_wm_normal_hints_reply(globalconf.connection,
173 cookie,
174 &c->size_hints, NULL);
177 xcb_get_property_cookie_t
178 property_get_wm_hints(client_t *c)
180 return xcb_icccm_get_wm_hints_unchecked(globalconf.connection, c->window);
183 /** Update the WM hints of a client.
184 * \param c The client.
185 * \param cookie Cookie returned by property_get_wm_hints.
187 void
188 property_update_wm_hints(client_t *c, xcb_get_property_cookie_t cookie)
190 xcb_icccm_wm_hints_t wmh;
192 if(!xcb_icccm_get_wm_hints_reply(globalconf.connection,
193 cookie,
194 &wmh, NULL))
195 return;
197 luaA_object_push(globalconf.L, c);
198 client_set_urgent(globalconf.L, -1, xcb_icccm_wm_hints_get_urgency(&wmh));
200 if(wmh.flags & XCB_ICCCM_WM_HINT_INPUT)
201 c->nofocus = !wmh.input;
203 if(wmh.flags & XCB_ICCCM_WM_HINT_WINDOW_GROUP)
204 client_set_group_window(globalconf.L, -1, wmh.window_group);
206 lua_pop(globalconf.L, 1);
209 xcb_get_property_cookie_t
210 property_get_wm_class(client_t *c)
212 return xcb_icccm_get_wm_class_unchecked(globalconf.connection, c->window);
215 /** Update WM_CLASS of a client.
216 * \param c The client.
217 * \param cookie Cookie returned by property_get_wm_class.
219 void
220 property_update_wm_class(client_t *c, xcb_get_property_cookie_t cookie)
222 xcb_icccm_get_wm_class_reply_t hint;
224 if(!xcb_icccm_get_wm_class_reply(globalconf.connection,
225 cookie,
226 &hint, NULL))
227 return;
229 luaA_object_push(globalconf.L, c);
230 client_set_class_instance(globalconf.L, -1, hint.class_name, hint.instance_name);
231 lua_pop(globalconf.L, 1);
233 xcb_icccm_get_wm_class_reply_wipe(&hint);
236 static int
237 property_handle_net_wm_strut_partial(uint8_t state,
238 xcb_window_t window)
240 client_t *c = client_getbywin(window);
242 if(c)
243 ewmh_process_client_strut(c);
245 return 0;
248 xcb_get_property_cookie_t
249 property_get_net_wm_icon(client_t *c)
251 return ewmh_window_icon_get_unchecked(c->window);
254 void
255 property_update_net_wm_icon(client_t *c, xcb_get_property_cookie_t cookie)
257 cairo_surface_t *surface = ewmh_window_icon_get_reply(cookie);
259 if(!surface)
260 return;
262 client_set_icon(c, surface);
263 cairo_surface_destroy(surface);
266 xcb_get_property_cookie_t
267 property_get_net_wm_pid(client_t *c)
269 return xcb_get_property_unchecked(globalconf.connection, false, c->window, _NET_WM_PID, XCB_ATOM_CARDINAL, 0L, 1L);
272 void
273 property_update_net_wm_pid(client_t *c, xcb_get_property_cookie_t cookie)
275 xcb_get_property_reply_t *reply;
277 reply = xcb_get_property_reply(globalconf.connection, cookie, NULL);
279 if(reply && reply->value_len)
281 uint32_t *rdata = xcb_get_property_value(reply);
282 if(rdata)
284 luaA_object_push(globalconf.L, c);
285 client_set_pid(globalconf.L, -1, *rdata);
286 lua_pop(globalconf.L, 1);
290 p_delete(&reply);
293 xcb_get_property_cookie_t
294 property_get_wm_protocols(client_t *c)
296 return xcb_icccm_get_wm_protocols_unchecked(globalconf.connection,
297 c->window, WM_PROTOCOLS);
300 /** Update the list of supported protocols for a client.
301 * \param c The client.
302 * \param cookie Cookie from property_get_wm_protocols.
304 void
305 property_update_wm_protocols(client_t *c, xcb_get_property_cookie_t cookie)
307 xcb_icccm_get_wm_protocols_reply_t protocols;
309 /* If this fails for any reason, we still got the old value */
310 if(!xcb_icccm_get_wm_protocols_reply(globalconf.connection,
311 cookie,
312 &protocols, NULL))
313 return;
315 xcb_icccm_get_wm_protocols_reply_wipe(&c->protocols);
316 memcpy(&c->protocols, &protocols, sizeof(protocols));
319 /** The property notify event handler.
320 * \param state currently unused
321 * \param window The window to obtain update the property with.
322 * \param name The protocol atom, currently unused.
323 * \param reply (Optional) An existing reply.
325 static int
326 property_handle_xembed_info(uint8_t state,
327 xcb_window_t window)
329 xembed_window_t *emwin = xembed_getbywin(&globalconf.embedded, window);
331 if(emwin)
333 xcb_get_property_cookie_t cookie =
334 xcb_get_property(globalconf.connection, 0, window, _XEMBED_INFO,
335 XCB_GET_PROPERTY_TYPE_ANY, 0, 3);
336 xcb_get_property_reply_t *propr =
337 xcb_get_property_reply(globalconf.connection, cookie, 0);
338 xembed_property_update(globalconf.connection, emwin, propr);
339 p_delete(&propr);
342 return 0;
345 static int
346 property_handle_net_wm_opacity(uint8_t state,
347 xcb_window_t window)
349 drawin_t *drawin = drawin_getbywin(window);
351 if(drawin)
353 luaA_object_push(globalconf.L, drawin);
354 window_set_opacity(globalconf.L, -1, xwindow_get_opacity(drawin->window));
355 lua_pop(globalconf.L, -1);
357 else
359 client_t *c = client_getbywin(window);
360 if(c)
362 luaA_object_push(globalconf.L, c);
363 window_set_opacity(globalconf.L, -1, xwindow_get_opacity(c->window));
364 lua_pop(globalconf.L, 1);
368 return 0;
371 static int
372 property_handle_xrootpmap_id(uint8_t state,
373 xcb_window_t window)
375 signal_object_emit(globalconf.L, &global_signals, "wallpaper_changed", 0);
376 return 0;
379 /** The property notify event handler handling xproperties.
380 * \param ev The event.
382 static void
383 property_handle_propertynotify_xproperty(xcb_property_notify_event_t *ev)
385 xproperty_t *prop;
386 xproperty_t lookup = { .atom = ev->atom };
387 buffer_t buf;
388 void *obj;
390 prop = xproperty_array_lookup(&globalconf.xproperties, &lookup);
391 if(!prop)
392 /* Property is not registered */
393 return;
395 if (ev->window != globalconf.screen->root)
397 obj = client_getbywin(ev->window);
398 if(!obj)
399 obj = drawin_getbywin(ev->window);
400 if(!obj)
401 return;
402 } else
403 obj = NULL;
405 /* Get us the name of the property */
406 buffer_inita(&buf, a_strlen(prop->name) + a_strlen("xproperty::") + 1);
407 buffer_addf(&buf, "xproperty::%s", prop->name);
409 /* And emit the right signal */
410 if (obj)
412 luaA_object_push(globalconf.L, obj);
413 luaA_object_emit_signal(globalconf.L, -1, buf.s, 0);
414 lua_pop(globalconf.L, 1);
415 } else
416 signal_object_emit(globalconf.L, &global_signals, buf.s, 0);
417 buffer_wipe(&buf);
420 /** The property notify event handler.
421 * \param ev The event.
423 void
424 property_handle_propertynotify(xcb_property_notify_event_t *ev)
426 int (*handler)(uint8_t state,
427 xcb_window_t window) = NULL;
429 globalconf.timestamp = ev->time;
431 property_handle_propertynotify_xproperty(ev);
433 /* Find the correct event handler */
434 #define HANDLE(atom_, cb) \
435 if (ev->atom == atom_) \
437 handler = cb; \
438 } else
439 #define END return
441 /* Xembed stuff */
442 HANDLE(_XEMBED_INFO, property_handle_xembed_info)
444 /* ICCCM stuff */
445 HANDLE(XCB_ATOM_WM_TRANSIENT_FOR, property_handle_wm_transient_for)
446 HANDLE(WM_CLIENT_LEADER, property_handle_wm_client_leader)
447 HANDLE(XCB_ATOM_WM_NORMAL_HINTS, property_handle_wm_normal_hints)
448 HANDLE(XCB_ATOM_WM_HINTS, property_handle_wm_hints)
449 HANDLE(XCB_ATOM_WM_NAME, property_handle_wm_name)
450 HANDLE(XCB_ATOM_WM_ICON_NAME, property_handle_wm_icon_name)
451 HANDLE(XCB_ATOM_WM_CLASS, property_handle_wm_class)
452 HANDLE(WM_PROTOCOLS, property_handle_wm_protocols)
453 HANDLE(XCB_ATOM_WM_CLIENT_MACHINE, property_handle_wm_client_machine)
454 HANDLE(WM_WINDOW_ROLE, property_handle_wm_window_role)
456 /* EWMH stuff */
457 HANDLE(_NET_WM_NAME, property_handle_net_wm_name)
458 HANDLE(_NET_WM_ICON_NAME, property_handle_net_wm_icon_name)
459 HANDLE(_NET_WM_STRUT_PARTIAL, property_handle_net_wm_strut_partial)
460 HANDLE(_NET_WM_ICON, property_handle_net_wm_icon)
461 HANDLE(_NET_WM_PID, property_handle_net_wm_pid)
462 HANDLE(_NET_WM_WINDOW_OPACITY, property_handle_net_wm_opacity)
464 /* background change */
465 HANDLE(_XROOTPMAP_ID, property_handle_xrootpmap_id)
467 /* If nothing was found, return */
468 END;
470 #undef HANDLE
471 #undef END
473 (*handler)(ev->state, ev->window);
476 /** Register a new xproperty.
477 * \param L The Lua VM state.
478 * \return The number of elements pushed on stack.
479 * \luastack
480 * \lparam The name of the X11 property
481 * \lparam One of "string", "number" or "boolean"
484 luaA_register_xproperty(lua_State *L)
486 const char *name;
487 struct xproperty property;
488 struct xproperty *found;
489 const char *const args[] = { "string", "number", "boolean" };
490 xcb_intern_atom_reply_t *atom_r;
491 int type;
493 name = luaL_checkstring(L, 1);
494 type = luaL_checkoption(L, 2, NULL, args);
495 if (type == 0)
496 property.type = PROP_STRING;
497 else if (type == 1)
498 property.type = PROP_NUMBER;
499 else
500 property.type = PROP_BOOLEAN;
502 atom_r = xcb_intern_atom_reply(globalconf.connection,
503 xcb_intern_atom_unchecked(globalconf.connection, false,
504 a_strlen(name), name),
505 NULL);
506 if(!atom_r)
507 return 0;
509 property.atom = atom_r->atom;
510 p_delete(&atom_r);
512 found = xproperty_array_lookup(&globalconf.xproperties, &property);
513 if(found)
515 /* Property already registered */
516 if(found->type != property.type)
517 return luaL_error(L, "xproperty '%s' already registered with different type", name);
519 else
521 buffer_t buf;
522 buffer_inita(&buf, a_strlen(name) + a_strlen("xproperty::") + 1);
523 buffer_addf(&buf, "xproperty::%s", name);
525 property.name = a_strdup(name);
526 xproperty_array_insert(&globalconf.xproperties, property);
527 signal_add(&window_class.signals, buf.s);
528 signal_add(&global_signals, buf.s);
529 buffer_wipe(&buf);
532 return 0;
535 /** Set an xproperty.
536 * \param L The Lua VM state.
537 * \return The number of elements pushed on stack.
540 luaA_set_xproperty(lua_State *L)
542 return window_set_xproperty(L, globalconf.screen->root, 1, 2);
545 /** Get an xproperty.
546 * \param L The Lua VM state.
547 * \return The number of elements pushed on stack.
550 luaA_get_xproperty(lua_State *L)
552 return window_get_xproperty(L, globalconf.screen->root, 1);
555 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80