awful.completion: sort matches
[awesome.git] / property.c
blob0645df9f4d7a37d473a54531b1bebf98780046b6
1 /*
2 * property.c - property handlers
4 * Copyright © 2008 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 "common/atoms.h"
30 #include "common/xutil.h"
31 #include "window.h"
33 void
34 property_update_wm_transient_for(client_t *c, xcb_get_property_reply_t *reply)
36 xcb_window_t trans;
38 if(reply)
40 if(!xcb_get_wm_transient_for_from_reply(&trans, reply))
41 return;
43 else
45 if(!xcb_get_wm_transient_for_reply(globalconf.connection,
46 xcb_get_wm_transient_for_unchecked(globalconf.connection,
47 c->win),
48 &trans, NULL))
49 return;
52 c->type = WINDOW_TYPE_DIALOG;
53 c->transient_for = client_getbywin(trans);
54 client_setabove(c, false);
57 static int
58 property_handle_wm_transient_for(void *data,
59 xcb_connection_t *connection,
60 uint8_t state,
61 xcb_window_t window,
62 xcb_atom_t name,
63 xcb_get_property_reply_t *reply)
65 client_t *c = client_getbywin(window);
67 if(c)
68 property_update_wm_transient_for(c, reply);
70 return 0;
74 /** Update leader hint of a client.
75 * \param c The client.
76 * \param reply (Optional) An existing reply.
78 void
79 property_update_wm_client_leader(client_t *c, xcb_get_property_reply_t *reply)
81 xcb_get_property_cookie_t client_leader_q;
82 void *data;
83 bool no_reply = !reply;
85 if(no_reply)
87 client_leader_q = xcb_get_property_unchecked(globalconf.connection, false, c->win,
88 WM_CLIENT_LEADER, WINDOW, 0, 32);
90 reply = xcb_get_property_reply(globalconf.connection, client_leader_q, NULL);
93 if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
94 c->leader_win = *(xcb_window_t *) data;
96 /* Only free when we created a reply ourselves. */
97 if(no_reply)
98 p_delete(&reply);
101 static int
102 property_handle_wm_client_leader(void *data,
103 xcb_connection_t *connection,
104 uint8_t state,
105 xcb_window_t window,
106 xcb_atom_t name,
107 xcb_get_property_reply_t *reply)
109 client_t *c = client_getbywin(window);
111 if(c)
112 property_update_wm_client_leader(c, reply);
114 return 0;
117 /** Update the size hints of a client.
118 * \param c The client.
119 * \param reply (Optional) An existing reply.
121 void
122 property_update_wm_normal_hints(client_t *c, xcb_get_property_reply_t *reply)
124 if(reply)
126 if(!xcb_get_wm_size_hints_from_reply(&c->size_hints, reply))
127 return;
129 else
131 if(!xcb_get_wm_normal_hints_reply(globalconf.connection,
132 xcb_get_wm_normal_hints_unchecked(globalconf.connection,
133 c->win),
134 &c->size_hints, NULL))
135 return;
139 static int
140 property_handle_wm_normal_hints(void *data,
141 xcb_connection_t *connection,
142 uint8_t state,
143 xcb_window_t window,
144 xcb_atom_t name,
145 xcb_get_property_reply_t *reply)
147 client_t *c = client_getbywin(window);
149 if(c)
150 property_update_wm_normal_hints(c, reply);
152 return 0;
155 /** Update the WM hints of a client.
156 * \param c The client.
157 * \param reply (Optional) An existing reply.
159 void
160 property_update_wm_hints(client_t *c, xcb_get_property_reply_t *reply)
162 xcb_wm_hints_t wmh;
164 if(reply)
166 if(!xcb_get_wm_hints_from_reply(&wmh, reply))
167 return;
169 else
171 if(!xcb_get_wm_hints_reply(globalconf.connection,
172 xcb_get_wm_hints_unchecked(globalconf.connection, c->win),
173 &wmh, NULL))
174 return;
177 bool isurgent = xcb_wm_hints_get_urgency(&wmh);
178 client_seturgent(c, isurgent);
179 if(wmh.flags & XCB_WM_HINT_STATE &&
180 wmh.initial_state == XCB_WM_STATE_WITHDRAWN)
181 client_setborder(c, 0);
183 if(wmh.flags & XCB_WM_HINT_INPUT)
184 c->nofocus = !wmh.input;
186 if(wmh.flags & XCB_WM_HINT_WINDOW_GROUP)
187 c->group_win = wmh.window_group;
190 static int
191 property_handle_wm_hints(void *data,
192 xcb_connection_t *connection,
193 uint8_t state,
194 xcb_window_t window,
195 xcb_atom_t name,
196 xcb_get_property_reply_t *reply)
198 client_t *c = client_getbywin(window);
200 if(c)
201 property_update_wm_hints(c, reply);
203 return 0;
206 /** Update client name attribute with its new title.
207 * \param c The client.
209 void
210 property_update_wm_name(client_t *c)
212 char *name;
213 ssize_t len;
215 if(!xutil_text_prop_get(globalconf.connection, c->win, _NET_WM_NAME, &name, &len))
216 if(!xutil_text_prop_get(globalconf.connection, c->win, WM_NAME, &name, &len))
217 return;
219 p_delete(&c->name);
221 c->name = name;
223 /* call hook */
224 hook_property(client, c, "name");
227 /** Update WM_CLASS of a client.
228 * \param c The client.
229 * \param reply The reply to get property request, or NULL if none.
231 void
232 property_update_wm_class(client_t *c, xcb_get_property_reply_t *reply)
234 xcb_get_wm_class_reply_t hint;
236 if(reply)
238 if(!xcb_get_wm_class_from_reply(&hint, reply))
239 return;
241 else
243 if(!xcb_get_wm_class_reply(globalconf.connection,
244 xcb_get_wm_class_unchecked(globalconf.connection, c->win),
245 &hint, NULL))
246 return;
249 p_delete(&c->instance);
250 p_delete(&c->class);
252 c->instance = a_strdup(hint.instance_name);
253 c->class = a_strdup(hint.class_name);
254 /* only delete reply if we get it ourselves */
255 if(!reply)
256 xcb_get_wm_class_reply_wipe(&hint);
259 /** Update client icon name attribute with its new title.
260 * \param c The client.
262 void
263 property_update_wm_icon_name(client_t *c)
265 char *name;
266 ssize_t len;
268 if(!xutil_text_prop_get(globalconf.connection, c->win, _NET_WM_ICON_NAME, &name, &len))
269 if(!xutil_text_prop_get(globalconf.connection, c->win, WM_ICON_NAME, &name, &len))
270 return;
272 p_delete(&c->icon_name);
274 c->icon_name = name;
276 /* call hook */
277 hook_property(client, c, "icon_name");
280 static int
281 property_handle_wm_name(void *data,
282 xcb_connection_t *connection,
283 uint8_t state,
284 xcb_window_t window,
285 xcb_atom_t name,
286 xcb_get_property_reply_t *reply)
288 client_t *c = client_getbywin(window);
290 if(c)
291 property_update_wm_name(c);
293 return 0;
296 static int
297 property_handle_wm_icon_name(void *data,
298 xcb_connection_t *connection,
299 uint8_t state,
300 xcb_window_t window,
301 xcb_atom_t name,
302 xcb_get_property_reply_t *reply)
304 client_t *c = client_getbywin(window);
306 if(c)
307 property_update_wm_icon_name(c);
309 return 0;
312 static int
313 property_handle_wm_class(void *data,
314 xcb_connection_t *connection,
315 uint8_t state,
316 xcb_window_t window,
317 xcb_atom_t name,
318 xcb_get_property_reply_t *reply)
320 client_t *c = client_getbywin(window);
322 if(c)
323 property_update_wm_class(c, reply);
325 return 0;
328 static int
329 property_handle_net_wm_strut_partial(void *data,
330 xcb_connection_t *connection,
331 uint8_t state,
332 xcb_window_t window,
333 xcb_atom_t name,
334 xcb_get_property_reply_t *reply)
336 client_t *c = client_getbywin(window);
338 if(c)
339 ewmh_process_client_strut(c, reply);
341 return 0;
344 static int
345 property_handle_net_wm_icon(void *data,
346 xcb_connection_t *connection,
347 uint8_t state,
348 xcb_window_t window,
349 xcb_atom_t name,
350 xcb_get_property_reply_t *reply)
352 client_t *c = client_getbywin(window);
354 if(c)
356 client_push(globalconf.L, c);
357 luaA_object_unref_item(globalconf.L, -1, c->icon);
358 if(ewmh_window_icon_from_reply(reply))
359 c->icon = luaA_object_ref_item(globalconf.L, -2, -1);
360 else
361 c->icon = NULL;
362 /* remove client */
363 lua_pop(globalconf.L, 1);
364 /* execute hook */
365 hook_property(client, c, "icon");
368 return 0;
371 /** Update the list of supported protocols for a client.
372 * \param c The client.
374 void
375 property_update_wm_protocols(client_t *c)
377 xcb_get_wm_protocols_reply_t protocols;
379 /* If this fails for any reason, we still got the old value */
380 if(xcb_get_wm_protocols_reply(globalconf.connection,
381 xcb_get_wm_protocols_unchecked(globalconf.connection,
382 c->win, WM_PROTOCOLS),
383 &protocols, NULL))
385 xcb_get_wm_protocols_reply_wipe(&c->protocols);
386 memcpy(&c->protocols, &protocols, sizeof(protocols));
390 /** The property notify event handler.
391 * \param data currently unused.
392 * \param connection currently unusued.
393 * \param state currently unused.
394 * \param window The window to obtain update protocols from.
395 * \param name currently unused.
396 * \param reply currently unused.
398 static int
399 property_handle_wm_protocols(void *data,
400 xcb_connection_t *connection,
401 uint8_t state,
402 xcb_window_t window,
403 xcb_atom_t name,
404 xcb_get_property_reply_t *reply)
406 client_t *c = client_getbywin(window);
408 if(c)
409 property_update_wm_protocols(c);
411 return 0;
414 /** The property notify event handler.
415 * \param data currently unused.
416 * \param connection The connection to the X server.
417 * \param state currently unused
418 * \param window The window to obtain update the property with.
419 * \param name The protocol atom, currently unused.
420 * \param reply (Optional) An existing reply.
422 static int
423 property_handle_xembed_info(void *data __attribute__ ((unused)),
424 xcb_connection_t *connection,
425 uint8_t state,
426 xcb_window_t window,
427 xcb_atom_t name,
428 xcb_get_property_reply_t *reply)
430 xembed_window_t *emwin = xembed_getbywin(&globalconf.embedded, window);
432 if(emwin)
433 xembed_property_update(connection, emwin, reply);
435 return 0;
438 static int
439 property_handle_xrootpmap_id(void *data __attribute__ ((unused)),
440 xcb_connection_t *connection,
441 uint8_t state,
442 xcb_window_t window,
443 xcb_atom_t name,
444 xcb_get_property_reply_t *reply)
446 if(globalconf.xinerama_is_active)
447 foreach(w, globalconf.wiboxes)
448 (*w)->need_update = true;
449 else
451 int screen = xutil_root2screen(connection, window);
452 foreach(w, globalconf.wiboxes)
453 if(screen == screen_array_indexof(&globalconf.screens, (*w)->screen))
454 (*w)->need_update = true;
457 return 0;
460 static int
461 property_handle_opacity(void *data __attribute__ ((unused)),
462 xcb_connection_t *connection,
463 uint8_t state,
464 xcb_window_t window,
465 xcb_atom_t name,
466 xcb_get_property_reply_t *reply)
468 wibox_t *wibox = wibox_getbywin(window);
470 if (wibox)
471 wibox->opacity = window_opacity_get_from_reply(reply);
472 return 0;
475 void a_xcb_set_property_handlers(void)
477 /* init */
478 xcb_property_handlers_init(&globalconf.prophs, &globalconf.evenths);
480 /* Xembed stuff */
481 xcb_property_set_handler(&globalconf.prophs, _XEMBED_INFO, UINT_MAX,
482 property_handle_xembed_info, NULL);
484 /* ICCCM stuff */
485 xcb_property_set_handler(&globalconf.prophs, WM_TRANSIENT_FOR, UINT_MAX,
486 property_handle_wm_transient_for, NULL);
487 xcb_property_set_handler(&globalconf.prophs, WM_CLIENT_LEADER, UINT_MAX,
488 property_handle_wm_client_leader, NULL);
489 xcb_property_set_handler(&globalconf.prophs, WM_NORMAL_HINTS, UINT_MAX,
490 property_handle_wm_normal_hints, NULL);
491 xcb_property_set_handler(&globalconf.prophs, WM_HINTS, UINT_MAX,
492 property_handle_wm_hints, NULL);
493 xcb_property_set_handler(&globalconf.prophs, WM_NAME, UINT_MAX,
494 property_handle_wm_name, NULL);
495 xcb_property_set_handler(&globalconf.prophs, WM_ICON_NAME, UINT_MAX,
496 property_handle_wm_icon_name, NULL);
497 xcb_property_set_handler(&globalconf.prophs, WM_CLASS, UINT_MAX,
498 property_handle_wm_class, NULL);
499 xcb_property_set_handler(&globalconf.prophs, WM_PROTOCOLS, UINT_MAX,
500 property_handle_wm_protocols, NULL);
502 /* EWMH stuff */
503 xcb_property_set_handler(&globalconf.prophs, _NET_WM_NAME, UINT_MAX,
504 property_handle_wm_name, NULL);
505 xcb_property_set_handler(&globalconf.prophs, _NET_WM_ICON_NAME, UINT_MAX,
506 property_handle_wm_icon_name, NULL);
507 xcb_property_set_handler(&globalconf.prophs, _NET_WM_STRUT_PARTIAL, UINT_MAX,
508 property_handle_net_wm_strut_partial, NULL);
509 xcb_property_set_handler(&globalconf.prophs, _NET_WM_ICON, UINT_MAX,
510 property_handle_net_wm_icon, NULL);
512 /* background change */
513 xcb_property_set_handler(&globalconf.prophs, _XROOTPMAP_ID, 1,
514 property_handle_xrootpmap_id, NULL);
516 /* Opacity */
517 xcb_property_set_handler(&globalconf.prophs, _NET_WM_WINDOW_OPACITY, 1,
518 property_handle_opacity, NULL);
521 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80