client: reimplement client_{ban,unban} for more performance
[awesome.git] / property.c
blobbe41b9c26d5d91a08b50d624b648217985131295
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 "property.h"
25 #include "client.h"
26 #include "widget.h"
27 #include "ewmh.h"
28 #include "common/atoms.h"
30 extern awesome_t globalconf;
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);
56 static int
57 property_handle_wm_transient_for(void *data,
58 xcb_connection_t *connection,
59 uint8_t state,
60 xcb_window_t window,
61 xcb_atom_t name,
62 xcb_get_property_reply_t *reply)
64 client_t *c = client_getbywin(window);
66 if(c && !client_isfloating(c))
67 property_update_wm_transient_for(c, reply);
69 return 0;
72 /** Update the size hints of a client.
73 * \param c The client.
75 void
76 property_update_wm_normal_hints(client_t *c, xcb_get_property_reply_t *reply)
78 if(reply)
80 if(!xcb_get_wm_size_hints_from_reply(&c->size_hints, reply))
81 return;
83 else
85 if(!xcb_get_wm_normal_hints_reply(globalconf.connection,
86 xcb_get_wm_normal_hints_unchecked(globalconf.connection,
87 c->win),
88 &c->size_hints, NULL))
89 return;
92 if((c->size_hints.flags & XCB_SIZE_HINT_P_SIZE))
94 c->basew = c->size_hints.base_width;
95 c->baseh = c->size_hints.base_height;
97 else if((c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE))
99 c->basew = c->size_hints.min_width;
100 c->baseh = c->size_hints.min_height;
102 else
103 c->basew = c->baseh = 0;
105 if((c->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC))
107 c->incw = c->size_hints.width_inc;
108 c->inch = c->size_hints.height_inc;
110 else
111 c->incw = c->inch = 0;
113 if((c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE))
115 c->maxw = c->size_hints.max_width;
116 c->maxh = c->size_hints.max_height;
118 else
119 c->maxw = c->maxh = 0;
121 if((c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE))
123 c->minw = c->size_hints.min_width;
124 c->minh = c->size_hints.min_height;
126 else if((c->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE))
128 c->minw = c->size_hints.base_width;
129 c->minh = c->size_hints.base_height;
131 else
132 c->minw = c->minh = 0;
134 if((c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT))
136 c->minax = c->size_hints.min_aspect_num;
137 c->minay = c->size_hints.min_aspect_den;
138 c->maxax = c->size_hints.max_aspect_num;
139 c->maxay = c->size_hints.max_aspect_den;
141 else
142 c->minax = c->maxax = c->minay = c->maxay = 0;
145 static int
146 property_handle_wm_normal_hints(void *data,
147 xcb_connection_t *connection,
148 uint8_t state,
149 xcb_window_t window,
150 xcb_atom_t name,
151 xcb_get_property_reply_t *reply)
153 client_t *c = client_getbywin(window);
155 if(c)
156 property_update_wm_normal_hints(c, reply);
158 return 0;
161 /** Update the WM hints of a client.
162 * \param c The client.
164 void
165 property_update_wm_hints(client_t *c, xcb_get_property_reply_t *reply)
167 xcb_wm_hints_t wmh;
169 if(reply)
171 if(!xcb_get_wm_hints_from_reply(&wmh, reply))
172 return;
174 else
176 if(!xcb_get_wm_hints_reply(globalconf.connection,
177 xcb_get_wm_hints_unchecked(globalconf.connection, c->win),
178 &wmh, NULL))
179 return;
182 bool isurgent = xcb_wm_hints_get_urgency(&wmh);
183 if(isurgent != c->isurgent)
185 c->isurgent = isurgent;
186 /* execute hook */
187 hooks_property(c, "urgent");
189 if(wmh.flags & XCB_WM_HINT_STATE &&
190 wmh.initial_state == XCB_WM_STATE_WITHDRAWN)
191 client_setborder(c, 0);
193 if(wmh.flags & XCB_WM_HINT_INPUT)
194 c->nofocus = !wmh.input;
197 static int
198 property_handle_wm_hints(void *data,
199 xcb_connection_t *connection,
200 uint8_t state,
201 xcb_window_t window,
202 xcb_atom_t name,
203 xcb_get_property_reply_t *reply)
205 client_t *c = client_getbywin(window);
207 if(c)
208 property_update_wm_hints(c, reply);
210 return 0;
213 /** Update client name attribute with its new title.
214 * \param c The client.
215 * \param Return true if it has been updated.
217 void
218 property_update_wm_name(client_t *c)
220 char *name, *utf8;
221 ssize_t len;
223 if(!xutil_text_prop_get(globalconf.connection, c->win, _NET_WM_NAME, &name, &len))
224 if(!xutil_text_prop_get(globalconf.connection, c->win, WM_NAME, &name, &len))
225 return;
227 p_delete(&c->name);
229 if((utf8 = draw_iso2utf8(name, len)))
230 c->name = utf8;
231 else
232 c->name = name;
234 /* call hook */
235 hooks_property(c, "name");
238 /** Update client icon name attribute with its new title.
239 * \param c The client.
240 * \param Return true if it has been updated.
242 void
243 property_update_wm_icon_name(client_t *c)
245 char *name, *utf8;
246 ssize_t len;
248 if(!xutil_text_prop_get(globalconf.connection, c->win, _NET_WM_ICON_NAME, &name, &len))
249 if(!xutil_text_prop_get(globalconf.connection, c->win, WM_ICON_NAME, &name, &len))
250 return;
252 p_delete(&c->icon_name);
254 if((utf8 = draw_iso2utf8(name, len)))
255 c->icon_name = utf8;
256 else
257 c->icon_name = name;
259 /* call hook */
260 hooks_property(c, "icon_name");
263 static int
264 property_handle_wm_name(void *data,
265 xcb_connection_t *connection,
266 uint8_t state,
267 xcb_window_t window,
268 xcb_atom_t name,
269 xcb_get_property_reply_t *reply)
271 client_t *c = client_getbywin(window);
273 if(c)
274 property_update_wm_name(c);
276 return 0;
279 static int
280 property_handle_wm_icon_name(void *data,
281 xcb_connection_t *connection,
282 uint8_t state,
283 xcb_window_t window,
284 xcb_atom_t name,
285 xcb_get_property_reply_t *reply)
287 client_t *c = client_getbywin(window);
289 if(c)
290 property_update_wm_icon_name(c);
292 return 0;
295 static int
296 property_handle_net_wm_strut_partial(void *data,
297 xcb_connection_t *connection,
298 uint8_t state,
299 xcb_window_t window,
300 xcb_atom_t name,
301 xcb_get_property_reply_t *reply)
303 client_t *c = client_getbywin(window);
305 if(c)
306 ewmh_client_strut_update(c, reply);
308 return 0;
311 static int
312 property_handle_net_wm_icon(void *data,
313 xcb_connection_t *connection,
314 uint8_t state,
315 xcb_window_t window,
316 xcb_atom_t name,
317 xcb_get_property_reply_t *reply)
319 client_t *c = client_getbywin(window);
321 if(c)
323 image_t *icon;
324 image_unref(&c->icon);
325 icon = ewmh_window_icon_from_reply(reply);
326 c->icon = icon ? image_ref(&icon) : NULL;
328 /* execute hook */
329 hooks_property(c, "icon");
332 return 0;
335 /** The property notify event handler.
336 * \param data currently unused.
337 * \param connection The connection to the X server.
338 * \param ev The event.
340 static int
341 property_handle_xembed_info(void *data __attribute__ ((unused)),
342 xcb_connection_t *connection,
343 uint8_t state,
344 xcb_window_t window,
345 xcb_atom_t name,
346 xcb_get_property_reply_t *reply)
348 xembed_window_t *emwin = xembed_getbywin(globalconf.embedded, window);
350 if(emwin)
351 xembed_property_update(connection, emwin, reply);
353 return 0;
356 static int
357 property_handle_xrootpmap_id(void *data __attribute__ ((unused)),
358 xcb_connection_t *connection,
359 uint8_t state,
360 xcb_window_t window,
361 xcb_atom_t name,
362 xcb_get_property_reply_t *reply)
364 if(globalconf.xinerama_is_active)
365 for(int screen = 0; screen < globalconf.nscreen; screen++)
367 wibox_array_t *w = &globalconf.screens[screen].wiboxes;
368 for(int i = 0; i < w->len; i++)
369 w->tab[i]->need_update = true;
371 else
373 int screen = xutil_root2screen(connection, window);
374 wibox_array_t *w = &globalconf.screens[screen].wiboxes;
375 for(int i = 0; i < w->len; i++)
376 w->tab[i]->need_update = true;
379 return 0;
382 void a_xcb_set_property_handlers(void)
384 /* init */
385 xcb_property_handlers_init(&globalconf.prophs, &globalconf.evenths);
387 /* Xembed stuff */
388 xcb_property_set_handler(&globalconf.prophs, _XEMBED_INFO, UINT_MAX,
389 property_handle_xembed_info, NULL);
391 /* ICCCM stuff */
392 xcb_property_set_handler(&globalconf.prophs, WM_TRANSIENT_FOR, UINT_MAX,
393 property_handle_wm_transient_for, NULL);
394 xcb_property_set_handler(&globalconf.prophs, WM_NORMAL_HINTS, UINT_MAX,
395 property_handle_wm_normal_hints, NULL);
396 xcb_property_set_handler(&globalconf.prophs, WM_HINTS, UINT_MAX,
397 property_handle_wm_hints, NULL);
398 xcb_property_set_handler(&globalconf.prophs, WM_NAME, UINT_MAX,
399 property_handle_wm_name, NULL);
400 xcb_property_set_handler(&globalconf.prophs, WM_ICON_NAME, UINT_MAX,
401 property_handle_wm_icon_name, NULL);
403 /* EWMH stuff */
404 xcb_property_set_handler(&globalconf.prophs, _NET_WM_NAME, UINT_MAX,
405 property_handle_wm_name, NULL);
406 xcb_property_set_handler(&globalconf.prophs, _NET_WM_ICON_NAME, UINT_MAX,
407 property_handle_wm_icon_name, NULL);
408 xcb_property_set_handler(&globalconf.prophs, _NET_WM_STRUT_PARTIAL, UINT_MAX,
409 property_handle_net_wm_strut_partial, NULL);
410 xcb_property_set_handler(&globalconf.prophs, _NET_WM_ICON, UINT_MAX,
411 property_handle_net_wm_icon, NULL);
413 /* background change */
414 xcb_property_set_handler(&globalconf.prophs, _XROOTPMAP_ID, 1,
415 property_handle_xrootpmap_id, NULL);
418 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80