Set c.screen in ewmh.tag and before tags in rules.execute
[awesome.git] / property.c
blob13c340eef9ff0ecdd206865726b0c78b5750cfc3
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;
108 int counter;
109 client_t *tc, *tmp;
111 if(!xcb_icccm_get_wm_transient_for_reply(globalconf.connection,
112 cookie,
113 &trans, NULL))
114 return;
116 tmp = tc = client_getbywin(trans);
118 luaA_object_push(globalconf.L, c);
119 client_set_type(globalconf.L, -1, WINDOW_TYPE_DIALOG);
120 client_set_above(globalconf.L, -1, false);
122 /* Verify that there are no loops in the transient_for relation after we are done */
123 for(counter = 0; tmp != NULL && counter <= globalconf.stack.len; counter++)
125 if (tmp == c)
126 /* We arrived back at the client we started from, so there is a loop */
127 counter = globalconf.stack.len+1;
128 tmp = tmp->transient_for;
130 if (counter <= globalconf.stack.len)
131 client_set_transient_for(globalconf.L, -1, tc);
133 lua_pop(globalconf.L, 1);
136 xcb_get_property_cookie_t
137 property_get_wm_client_leader(client_t *c)
139 return xcb_get_property_unchecked(globalconf.connection, false, c->window,
140 WM_CLIENT_LEADER, XCB_ATOM_WINDOW, 0, 32);
143 /** Update leader hint of a client.
144 * \param c The client.
145 * \param cookie Cookie returned by property_get_wm_client_leader.
147 void
148 property_update_wm_client_leader(client_t *c, xcb_get_property_cookie_t cookie)
150 xcb_get_property_reply_t *reply;
151 void *data;
153 reply = xcb_get_property_reply(globalconf.connection, cookie, NULL);
155 if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
156 c->leader_window = *(xcb_window_t *) data;
158 p_delete(&reply);
161 xcb_get_property_cookie_t
162 property_get_wm_normal_hints(client_t *c)
164 return xcb_icccm_get_wm_normal_hints_unchecked(globalconf.connection, c->window);
167 /** Update the size hints of a client.
168 * \param c The client.
169 * \param cookie Cookie returned by property_get_wm_normal_hints.
171 void
172 property_update_wm_normal_hints(client_t *c, xcb_get_property_cookie_t cookie)
174 xcb_icccm_get_wm_normal_hints_reply(globalconf.connection,
175 cookie,
176 &c->size_hints, NULL);
179 xcb_get_property_cookie_t
180 property_get_wm_hints(client_t *c)
182 return xcb_icccm_get_wm_hints_unchecked(globalconf.connection, c->window);
185 /** Update the WM hints of a client.
186 * \param c The client.
187 * \param cookie Cookie returned by property_get_wm_hints.
189 void
190 property_update_wm_hints(client_t *c, xcb_get_property_cookie_t cookie)
192 xcb_icccm_wm_hints_t wmh;
194 if(!xcb_icccm_get_wm_hints_reply(globalconf.connection,
195 cookie,
196 &wmh, NULL))
197 return;
199 luaA_object_push(globalconf.L, c);
200 client_set_urgent(globalconf.L, -1, xcb_icccm_wm_hints_get_urgency(&wmh));
202 if(wmh.flags & XCB_ICCCM_WM_HINT_INPUT)
203 c->nofocus = !wmh.input;
205 if(wmh.flags & XCB_ICCCM_WM_HINT_WINDOW_GROUP)
206 client_set_group_window(globalconf.L, -1, wmh.window_group);
208 lua_pop(globalconf.L, 1);
211 xcb_get_property_cookie_t
212 property_get_wm_class(client_t *c)
214 return xcb_icccm_get_wm_class_unchecked(globalconf.connection, c->window);
217 /** Update WM_CLASS of a client.
218 * \param c The client.
219 * \param cookie Cookie returned by property_get_wm_class.
221 void
222 property_update_wm_class(client_t *c, xcb_get_property_cookie_t cookie)
224 xcb_icccm_get_wm_class_reply_t hint;
226 if(!xcb_icccm_get_wm_class_reply(globalconf.connection,
227 cookie,
228 &hint, NULL))
229 return;
231 luaA_object_push(globalconf.L, c);
232 client_set_class_instance(globalconf.L, -1, hint.class_name, hint.instance_name);
233 lua_pop(globalconf.L, 1);
235 xcb_icccm_get_wm_class_reply_wipe(&hint);
238 static int
239 property_handle_net_wm_strut_partial(uint8_t state,
240 xcb_window_t window)
242 client_t *c = client_getbywin(window);
244 if(c)
245 ewmh_process_client_strut(c);
247 return 0;
250 xcb_get_property_cookie_t
251 property_get_net_wm_icon(client_t *c)
253 return ewmh_window_icon_get_unchecked(c->window);
256 void
257 property_update_net_wm_icon(client_t *c, xcb_get_property_cookie_t cookie)
259 cairo_surface_t *surface = ewmh_window_icon_get_reply(cookie);
261 if(!surface)
262 return;
264 client_set_icon(c, surface);
265 cairo_surface_destroy(surface);
268 xcb_get_property_cookie_t
269 property_get_net_wm_pid(client_t *c)
271 return xcb_get_property_unchecked(globalconf.connection, false, c->window, _NET_WM_PID, XCB_ATOM_CARDINAL, 0L, 1L);
274 void
275 property_update_net_wm_pid(client_t *c, xcb_get_property_cookie_t cookie)
277 xcb_get_property_reply_t *reply;
279 reply = xcb_get_property_reply(globalconf.connection, cookie, NULL);
281 if(reply && reply->value_len)
283 uint32_t *rdata = xcb_get_property_value(reply);
284 if(rdata)
286 luaA_object_push(globalconf.L, c);
287 client_set_pid(globalconf.L, -1, *rdata);
288 lua_pop(globalconf.L, 1);
292 p_delete(&reply);
295 xcb_get_property_cookie_t
296 property_get_wm_protocols(client_t *c)
298 return xcb_icccm_get_wm_protocols_unchecked(globalconf.connection,
299 c->window, WM_PROTOCOLS);
302 /** Update the list of supported protocols for a client.
303 * \param c The client.
304 * \param cookie Cookie from property_get_wm_protocols.
306 void
307 property_update_wm_protocols(client_t *c, xcb_get_property_cookie_t cookie)
309 xcb_icccm_get_wm_protocols_reply_t protocols;
311 /* If this fails for any reason, we still got the old value */
312 if(!xcb_icccm_get_wm_protocols_reply(globalconf.connection,
313 cookie,
314 &protocols, NULL))
315 return;
317 xcb_icccm_get_wm_protocols_reply_wipe(&c->protocols);
318 memcpy(&c->protocols, &protocols, sizeof(protocols));
321 /** The property notify event handler.
322 * \param state currently unused
323 * \param window The window to obtain update the property with.
324 * \param name The protocol atom, currently unused.
325 * \param reply (Optional) An existing reply.
327 static int
328 property_handle_xembed_info(uint8_t state,
329 xcb_window_t window)
331 xembed_window_t *emwin = xembed_getbywin(&globalconf.embedded, window);
333 if(emwin)
335 xcb_get_property_cookie_t cookie =
336 xcb_get_property(globalconf.connection, 0, window, _XEMBED_INFO,
337 XCB_GET_PROPERTY_TYPE_ANY, 0, 3);
338 xcb_get_property_reply_t *propr =
339 xcb_get_property_reply(globalconf.connection, cookie, 0);
340 xembed_property_update(globalconf.connection, emwin, propr);
341 p_delete(&propr);
344 return 0;
347 static int
348 property_handle_net_wm_opacity(uint8_t state,
349 xcb_window_t window)
351 drawin_t *drawin = drawin_getbywin(window);
353 if(drawin)
355 luaA_object_push(globalconf.L, drawin);
356 window_set_opacity(globalconf.L, -1, xwindow_get_opacity(drawin->window));
357 lua_pop(globalconf.L, -1);
359 else
361 client_t *c = client_getbywin(window);
362 if(c)
364 luaA_object_push(globalconf.L, c);
365 window_set_opacity(globalconf.L, -1, xwindow_get_opacity(c->window));
366 lua_pop(globalconf.L, 1);
370 return 0;
373 static int
374 property_handle_xrootpmap_id(uint8_t state,
375 xcb_window_t window)
377 signal_object_emit(globalconf.L, &global_signals, "wallpaper_changed", 0);
378 return 0;
381 /** The property notify event handler handling xproperties.
382 * \param ev The event.
384 static void
385 property_handle_propertynotify_xproperty(xcb_property_notify_event_t *ev)
387 xproperty_t *prop;
388 xproperty_t lookup = { .atom = ev->atom };
389 buffer_t buf;
390 void *obj;
392 prop = xproperty_array_lookup(&globalconf.xproperties, &lookup);
393 if(!prop)
394 /* Property is not registered */
395 return;
397 if (ev->window != globalconf.screen->root)
399 obj = client_getbywin(ev->window);
400 if(!obj)
401 obj = drawin_getbywin(ev->window);
402 if(!obj)
403 return;
404 } else
405 obj = NULL;
407 /* Get us the name of the property */
408 buffer_inita(&buf, a_strlen(prop->name) + a_strlen("xproperty::") + 1);
409 buffer_addf(&buf, "xproperty::%s", prop->name);
411 /* And emit the right signal */
412 if (obj)
414 luaA_object_push(globalconf.L, obj);
415 luaA_object_emit_signal(globalconf.L, -1, buf.s, 0);
416 lua_pop(globalconf.L, 1);
417 } else
418 signal_object_emit(globalconf.L, &global_signals, buf.s, 0);
419 buffer_wipe(&buf);
422 /** The property notify event handler.
423 * \param ev The event.
425 void
426 property_handle_propertynotify(xcb_property_notify_event_t *ev)
428 int (*handler)(uint8_t state,
429 xcb_window_t window) = NULL;
431 globalconf.timestamp = ev->time;
433 property_handle_propertynotify_xproperty(ev);
435 /* Find the correct event handler */
436 #define HANDLE(atom_, cb) \
437 if (ev->atom == atom_) \
439 handler = cb; \
440 } else
441 #define END return
443 /* Xembed stuff */
444 HANDLE(_XEMBED_INFO, property_handle_xembed_info)
446 /* ICCCM stuff */
447 HANDLE(XCB_ATOM_WM_TRANSIENT_FOR, property_handle_wm_transient_for)
448 HANDLE(WM_CLIENT_LEADER, property_handle_wm_client_leader)
449 HANDLE(XCB_ATOM_WM_NORMAL_HINTS, property_handle_wm_normal_hints)
450 HANDLE(XCB_ATOM_WM_HINTS, property_handle_wm_hints)
451 HANDLE(XCB_ATOM_WM_NAME, property_handle_wm_name)
452 HANDLE(XCB_ATOM_WM_ICON_NAME, property_handle_wm_icon_name)
453 HANDLE(XCB_ATOM_WM_CLASS, property_handle_wm_class)
454 HANDLE(WM_PROTOCOLS, property_handle_wm_protocols)
455 HANDLE(XCB_ATOM_WM_CLIENT_MACHINE, property_handle_wm_client_machine)
456 HANDLE(WM_WINDOW_ROLE, property_handle_wm_window_role)
458 /* EWMH stuff */
459 HANDLE(_NET_WM_NAME, property_handle_net_wm_name)
460 HANDLE(_NET_WM_ICON_NAME, property_handle_net_wm_icon_name)
461 HANDLE(_NET_WM_STRUT_PARTIAL, property_handle_net_wm_strut_partial)
462 HANDLE(_NET_WM_ICON, property_handle_net_wm_icon)
463 HANDLE(_NET_WM_PID, property_handle_net_wm_pid)
464 HANDLE(_NET_WM_WINDOW_OPACITY, property_handle_net_wm_opacity)
466 /* background change */
467 HANDLE(_XROOTPMAP_ID, property_handle_xrootpmap_id)
469 /* If nothing was found, return */
470 END;
472 #undef HANDLE
473 #undef END
475 (*handler)(ev->state, ev->window);
478 /** Register a new xproperty.
479 * \param L The Lua VM state.
480 * \return The number of elements pushed on stack.
481 * \luastack
482 * \lparam The name of the X11 property
483 * \lparam One of "string", "number" or "boolean"
486 luaA_register_xproperty(lua_State *L)
488 const char *name;
489 struct xproperty property;
490 struct xproperty *found;
491 const char *const args[] = { "string", "number", "boolean" };
492 xcb_intern_atom_reply_t *atom_r;
493 int type;
495 name = luaL_checkstring(L, 1);
496 type = luaL_checkoption(L, 2, NULL, args);
497 if (type == 0)
498 property.type = PROP_STRING;
499 else if (type == 1)
500 property.type = PROP_NUMBER;
501 else
502 property.type = PROP_BOOLEAN;
504 atom_r = xcb_intern_atom_reply(globalconf.connection,
505 xcb_intern_atom_unchecked(globalconf.connection, false,
506 a_strlen(name), name),
507 NULL);
508 if(!atom_r)
509 return 0;
511 property.atom = atom_r->atom;
512 p_delete(&atom_r);
514 found = xproperty_array_lookup(&globalconf.xproperties, &property);
515 if(found)
517 /* Property already registered */
518 if(found->type != property.type)
519 return luaL_error(L, "xproperty '%s' already registered with different type", name);
521 else
523 buffer_t buf;
524 buffer_inita(&buf, a_strlen(name) + a_strlen("xproperty::") + 1);
525 buffer_addf(&buf, "xproperty::%s", name);
527 property.name = a_strdup(name);
528 xproperty_array_insert(&globalconf.xproperties, property);
529 signal_add(&window_class.signals, buf.s);
530 signal_add(&global_signals, buf.s);
531 buffer_wipe(&buf);
534 return 0;
537 /** Set an xproperty.
538 * \param L The Lua VM state.
539 * \return The number of elements pushed on stack.
542 luaA_set_xproperty(lua_State *L)
544 return window_set_xproperty(L, globalconf.screen->root, 1, 2);
547 /** Get an xproperty.
548 * \param L The Lua VM state.
549 * \return The number of elements pushed on stack.
552 luaA_get_xproperty(lua_State *L)
554 return window_get_xproperty(L, globalconf.screen->root, 1);
557 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80