awful.ewmh: Enforce client geometry (FS#764,FS#1216)
[awesome.git] / ewmh.c
blob3983221057603b9cdf4fb249259c523f14e9efff
1 /*
2 * ewmh.c - EWMH support functions
4 * Copyright © 2007-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 <sys/types.h>
23 #include <unistd.h>
25 #include <xcb/xcb.h>
26 #include <xcb/xcb_atom.h>
28 #include "ewmh.h"
29 #include "objects/tag.h"
30 #include "screen.h"
31 #include "objects/client.h"
32 #include "luaa.h"
33 #include "common/atoms.h"
34 #include "common/buffer.h"
35 #include "common/xutil.h"
37 #define _NET_WM_STATE_REMOVE 0
38 #define _NET_WM_STATE_ADD 1
39 #define _NET_WM_STATE_TOGGLE 2
41 /** Update client EWMH hints.
42 * \param c The client.
44 static int
45 ewmh_client_update_hints(lua_State *L)
47 client_t *c = luaA_checkudata(L, 1, &client_class);
48 xcb_atom_t state[10]; /* number of defined state atoms */
49 int i = 0;
51 if(c->modal)
52 state[i++] = _NET_WM_STATE_MODAL;
53 if(c->fullscreen)
54 state[i++] = _NET_WM_STATE_FULLSCREEN;
55 if(c->maximized_vertical)
56 state[i++] = _NET_WM_STATE_MAXIMIZED_VERT;
57 if(c->maximized_horizontal)
58 state[i++] = _NET_WM_STATE_MAXIMIZED_HORZ;
59 if(c->sticky)
60 state[i++] = _NET_WM_STATE_STICKY;
61 if(c->skip_taskbar)
62 state[i++] = _NET_WM_STATE_SKIP_TASKBAR;
63 if(c->above)
64 state[i++] = _NET_WM_STATE_ABOVE;
65 if(c->below)
66 state[i++] = _NET_WM_STATE_BELOW;
67 if(c->minimized)
68 state[i++] = _NET_WM_STATE_HIDDEN;
69 if(c->urgent)
70 state[i++] = _NET_WM_STATE_DEMANDS_ATTENTION;
72 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
73 c->window, _NET_WM_STATE, XCB_ATOM_ATOM, 32, i, state);
75 return 0;
78 static int
79 ewmh_update_net_active_window(lua_State *L)
81 xcb_window_t win;
83 if(globalconf.focus.client)
84 win = globalconf.focus.client->window;
85 else
86 win = XCB_NONE;
88 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
89 globalconf.screen->root,
90 _NET_ACTIVE_WINDOW, XCB_ATOM_WINDOW, 32, 1, &win);
92 return 0;
95 static int
96 ewmh_update_net_client_list(lua_State *L)
98 xcb_window_t *wins = p_alloca(xcb_window_t, globalconf.clients.len);
100 int n = 0;
101 foreach(client, globalconf.clients)
102 wins[n++] = (*client)->window;
104 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
105 globalconf.screen->root,
106 _NET_CLIENT_LIST, XCB_ATOM_WINDOW, 32, n, wins);
108 return 0;
111 void
112 ewmh_init(void)
114 xcb_window_t father;
115 xcb_screen_t *xscreen = globalconf.screen;
116 xcb_atom_t atom[] =
118 _NET_SUPPORTED,
119 _NET_SUPPORTING_WM_CHECK,
120 _NET_STARTUP_ID,
121 _NET_CLIENT_LIST,
122 _NET_CLIENT_LIST_STACKING,
123 _NET_NUMBER_OF_DESKTOPS,
124 _NET_CURRENT_DESKTOP,
125 _NET_DESKTOP_NAMES,
126 _NET_ACTIVE_WINDOW,
127 _NET_CLOSE_WINDOW,
128 _NET_WM_NAME,
129 _NET_WM_STRUT_PARTIAL,
130 _NET_WM_ICON_NAME,
131 _NET_WM_VISIBLE_ICON_NAME,
132 _NET_WM_DESKTOP,
133 _NET_WM_WINDOW_TYPE,
134 _NET_WM_WINDOW_TYPE_DESKTOP,
135 _NET_WM_WINDOW_TYPE_DOCK,
136 _NET_WM_WINDOW_TYPE_TOOLBAR,
137 _NET_WM_WINDOW_TYPE_MENU,
138 _NET_WM_WINDOW_TYPE_UTILITY,
139 _NET_WM_WINDOW_TYPE_SPLASH,
140 _NET_WM_WINDOW_TYPE_DIALOG,
141 _NET_WM_WINDOW_TYPE_DROPDOWN_MENU,
142 _NET_WM_WINDOW_TYPE_POPUP_MENU,
143 _NET_WM_WINDOW_TYPE_TOOLTIP,
144 _NET_WM_WINDOW_TYPE_NOTIFICATION,
145 _NET_WM_WINDOW_TYPE_COMBO,
146 _NET_WM_WINDOW_TYPE_DND,
147 _NET_WM_WINDOW_TYPE_NORMAL,
148 _NET_WM_ICON,
149 _NET_WM_PID,
150 _NET_WM_STATE,
151 _NET_WM_STATE_STICKY,
152 _NET_WM_STATE_SKIP_TASKBAR,
153 _NET_WM_STATE_FULLSCREEN,
154 _NET_WM_STATE_MAXIMIZED_HORZ,
155 _NET_WM_STATE_MAXIMIZED_VERT,
156 _NET_WM_STATE_ABOVE,
157 _NET_WM_STATE_BELOW,
158 _NET_WM_STATE_MODAL,
159 _NET_WM_STATE_HIDDEN,
160 _NET_WM_STATE_DEMANDS_ATTENTION
162 int i;
164 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
165 xscreen->root, _NET_SUPPORTED, XCB_ATOM_ATOM, 32,
166 countof(atom), atom);
168 /* create our own window */
169 father = xcb_generate_id(globalconf.connection);
170 xcb_create_window(globalconf.connection, xscreen->root_depth,
171 father, xscreen->root, -1, -1, 1, 1, 0,
172 XCB_COPY_FROM_PARENT, xscreen->root_visual, 0, NULL);
174 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
175 xscreen->root, _NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32,
176 1, &father);
178 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
179 father, _NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32,
180 1, &father);
182 /* set the window manager name */
183 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
184 father, _NET_WM_NAME, UTF8_STRING, 8, 7, "awesome");
186 /* set the window manager PID */
187 i = getpid();
188 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
189 father, _NET_WM_PID, XCB_ATOM_CARDINAL, 32, 1, &i);
192 luaA_class_connect_signal(globalconf.L, &client_class, "focus", ewmh_update_net_active_window);
193 luaA_class_connect_signal(globalconf.L, &client_class, "unfocus", ewmh_update_net_active_window);
194 luaA_class_connect_signal(globalconf.L, &client_class, "manage", ewmh_update_net_client_list);
195 luaA_class_connect_signal(globalconf.L, &client_class, "unmanage", ewmh_update_net_client_list);
196 luaA_class_connect_signal(globalconf.L, &client_class, "property::modal" , ewmh_client_update_hints);
197 luaA_class_connect_signal(globalconf.L, &client_class, "property::fullscreen" , ewmh_client_update_hints);
198 luaA_class_connect_signal(globalconf.L, &client_class, "property::maximized_horizontal" , ewmh_client_update_hints);
199 luaA_class_connect_signal(globalconf.L, &client_class, "property::maximized_vertical" , ewmh_client_update_hints);
200 luaA_class_connect_signal(globalconf.L, &client_class, "property::sticky" , ewmh_client_update_hints);
201 luaA_class_connect_signal(globalconf.L, &client_class, "property::skip_taskbar" , ewmh_client_update_hints);
202 luaA_class_connect_signal(globalconf.L, &client_class, "property::above" , ewmh_client_update_hints);
203 luaA_class_connect_signal(globalconf.L, &client_class, "property::below" , ewmh_client_update_hints);
204 luaA_class_connect_signal(globalconf.L, &client_class, "property::minimized" , ewmh_client_update_hints);
205 luaA_class_connect_signal(globalconf.L, &client_class, "property::urgent" , ewmh_client_update_hints);
208 /** Set the client list in stacking order, bottom to top.
210 void
211 ewmh_update_net_client_list_stacking(void)
213 int n = 0;
214 xcb_window_t *wins = p_alloca(xcb_window_t, globalconf.stack.len);
216 foreach(client, globalconf.stack)
217 wins[n++] = (*client)->window;
219 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
220 globalconf.screen->root,
221 _NET_CLIENT_LIST_STACKING, XCB_ATOM_WINDOW, 32, n, wins);
224 void
225 ewmh_update_net_numbers_of_desktop(void)
227 uint32_t count = globalconf.tags.len;
229 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
230 globalconf.screen->root,
231 _NET_NUMBER_OF_DESKTOPS, XCB_ATOM_CARDINAL, 32, 1, &count);
234 void
235 ewmh_update_net_current_desktop(void)
237 uint32_t idx = tags_get_first_selected_index();
239 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
240 globalconf.screen->root,
241 _NET_CURRENT_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &idx);
244 void
245 ewmh_update_net_desktop_names(void)
247 buffer_t buf;
249 buffer_inita(&buf, BUFSIZ);
251 foreach(tag, globalconf.tags)
253 buffer_adds(&buf, tag_get_name(*tag));
254 buffer_addc(&buf, '\0');
257 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
258 globalconf.screen->root,
259 _NET_DESKTOP_NAMES, UTF8_STRING, 8, buf.len, buf.s);
260 buffer_wipe(&buf);
263 static void
264 ewmh_process_state_atom(client_t *c, xcb_atom_t state, int set)
266 luaA_object_push(globalconf.L, c);
268 if(state == _NET_WM_STATE_STICKY)
270 if(set == _NET_WM_STATE_REMOVE)
271 client_set_sticky(globalconf.L, -1, false);
272 else if(set == _NET_WM_STATE_ADD)
273 client_set_sticky(globalconf.L, -1, true);
274 else if(set == _NET_WM_STATE_TOGGLE)
275 client_set_sticky(globalconf.L, -1, !c->sticky);
277 else if(state == _NET_WM_STATE_SKIP_TASKBAR)
279 if(set == _NET_WM_STATE_REMOVE)
280 client_set_skip_taskbar(globalconf.L, -1, false);
281 else if(set == _NET_WM_STATE_ADD)
282 client_set_skip_taskbar(globalconf.L, -1, true);
283 else if(set == _NET_WM_STATE_TOGGLE)
284 client_set_skip_taskbar(globalconf.L, -1, !c->skip_taskbar);
286 else if(state == _NET_WM_STATE_FULLSCREEN)
288 if(set == _NET_WM_STATE_REMOVE)
289 client_set_fullscreen(globalconf.L, -1, false);
290 else if(set == _NET_WM_STATE_ADD)
291 client_set_fullscreen(globalconf.L, -1, true);
292 else if(set == _NET_WM_STATE_TOGGLE)
293 client_set_fullscreen(globalconf.L, -1, !c->fullscreen);
295 else if(state == _NET_WM_STATE_MAXIMIZED_HORZ)
297 if(set == _NET_WM_STATE_REMOVE)
298 client_set_maximized_horizontal(globalconf.L, -1, false);
299 else if(set == _NET_WM_STATE_ADD)
300 client_set_maximized_horizontal(globalconf.L, -1, true);
301 else if(set == _NET_WM_STATE_TOGGLE)
302 client_set_maximized_horizontal(globalconf.L, -1, !c->maximized_horizontal);
304 else if(state == _NET_WM_STATE_MAXIMIZED_VERT)
306 if(set == _NET_WM_STATE_REMOVE)
307 client_set_maximized_vertical(globalconf.L, -1, false);
308 else if(set == _NET_WM_STATE_ADD)
309 client_set_maximized_vertical(globalconf.L, -1, true);
310 else if(set == _NET_WM_STATE_TOGGLE)
311 client_set_maximized_vertical(globalconf.L, -1, !c->maximized_vertical);
313 else if(state == _NET_WM_STATE_ABOVE)
315 if(set == _NET_WM_STATE_REMOVE)
316 client_set_above(globalconf.L, -1, false);
317 else if(set == _NET_WM_STATE_ADD)
318 client_set_above(globalconf.L, -1, true);
319 else if(set == _NET_WM_STATE_TOGGLE)
320 client_set_above(globalconf.L, -1, !c->above);
322 else if(state == _NET_WM_STATE_BELOW)
324 if(set == _NET_WM_STATE_REMOVE)
325 client_set_below(globalconf.L, -1, false);
326 else if(set == _NET_WM_STATE_ADD)
327 client_set_below(globalconf.L, -1, true);
328 else if(set == _NET_WM_STATE_TOGGLE)
329 client_set_below(globalconf.L, -1, !c->below);
331 else if(state == _NET_WM_STATE_MODAL)
333 if(set == _NET_WM_STATE_REMOVE)
334 client_set_modal(globalconf.L, -1, false);
335 else if(set == _NET_WM_STATE_ADD)
336 client_set_modal(globalconf.L, -1, true);
337 else if(set == _NET_WM_STATE_TOGGLE)
338 client_set_modal(globalconf.L, -1, !c->modal);
340 else if(state == _NET_WM_STATE_HIDDEN)
342 if(set == _NET_WM_STATE_REMOVE)
343 client_set_minimized(globalconf.L, -1, false);
344 else if(set == _NET_WM_STATE_ADD)
345 client_set_minimized(globalconf.L, -1, true);
346 else if(set == _NET_WM_STATE_TOGGLE)
347 client_set_minimized(globalconf.L, -1, !c->minimized);
349 else if(state == _NET_WM_STATE_DEMANDS_ATTENTION)
351 if(set == _NET_WM_STATE_REMOVE)
352 client_set_urgent(globalconf.L, -1, false);
353 else if(set == _NET_WM_STATE_ADD)
354 client_set_urgent(globalconf.L, -1, true);
355 else if(set == _NET_WM_STATE_TOGGLE)
356 client_set_urgent(globalconf.L, -1, !c->urgent);
359 lua_pop(globalconf.L, 1);
363 ewmh_process_client_message(xcb_client_message_event_t *ev)
365 client_t *c;
367 if(ev->type == _NET_CURRENT_DESKTOP)
368 tag_view_only_byindex(ev->data.data32[0]);
369 else if(ev->type == _NET_CLOSE_WINDOW)
371 if((c = client_getbywin(ev->window)))
372 client_kill(c);
374 else if(ev->type == _NET_WM_DESKTOP)
376 if((c = client_getbywin(ev->window)))
378 if(ev->data.data32[0] == 0xffffffff)
379 c->sticky = true;
380 else
381 for(int i = 0; i < globalconf.tags.len; i++)
382 if((int)ev->data.data32[0] == i)
384 luaA_object_push(globalconf.L, globalconf.tags.tab[i]);
385 tag_client(c);
387 else
388 untag_client(c, globalconf.tags.tab[i]);
391 else if(ev->type == _NET_WM_STATE)
393 if((c = client_getbywin(ev->window)))
395 ewmh_process_state_atom(c, (xcb_atom_t) ev->data.data32[1], ev->data.data32[0]);
396 if(ev->data.data32[2])
397 ewmh_process_state_atom(c, (xcb_atom_t) ev->data.data32[2],
398 ev->data.data32[0]);
401 else if(ev->type == _NET_ACTIVE_WINDOW)
403 if((c = client_getbywin(ev->window))) {
404 luaA_object_push(globalconf.L, c);
405 luaA_object_emit_signal(globalconf.L, -1, "request::activate", 0);
406 lua_pop(globalconf.L, 1);
410 return 0;
413 /** Update the client active desktop.
414 * This is "wrong" since it can be on several tags, but EWMH has a strict view
415 * of desktop system so just take the first tag.
416 * \param c The client.
418 void
419 ewmh_client_update_desktop(client_t *c)
421 int i;
423 for(i = 0; i < globalconf.tags.len; i++)
424 if(is_client_tagged(c, globalconf.tags.tab[i]))
426 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
427 c->window, _NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &i);
428 return;
430 /* It doesn't have any tags, remove the property */
431 xcb_delete_property(globalconf.connection, c->window, _NET_WM_DESKTOP);
434 /** Update the client struts.
435 * \param window The window to update the struts for.
436 * \param strut The strut type to update the window with.
438 void
439 ewmh_update_strut(xcb_window_t window, strut_t *strut)
441 if(window)
443 const uint32_t state[] =
445 strut->left,
446 strut->right,
447 strut->top,
448 strut->bottom,
449 strut->left_start_y,
450 strut->left_end_y,
451 strut->right_start_y,
452 strut->right_end_y,
453 strut->top_start_x,
454 strut->top_end_x,
455 strut->bottom_start_x,
456 strut->bottom_end_x
459 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
460 window, _NET_WM_STRUT_PARTIAL, XCB_ATOM_CARDINAL, 32, countof(state), state);
464 /** Update the window type.
465 * \param window The window to update.
466 * \param type The new type to set.
468 void
469 ewmh_update_window_type(xcb_window_t window, uint32_t type)
471 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
472 window, _NET_WM_WINDOW_TYPE, XCB_ATOM_ATOM, 32, 1, &type);
475 void
476 ewmh_client_check_hints(client_t *c)
478 xcb_atom_t *state;
479 void *data = NULL;
480 int desktop;
481 xcb_get_property_cookie_t c0, c1, c2;
482 xcb_get_property_reply_t *reply;
484 /* Send the GetProperty requests which will be processed later */
485 c0 = xcb_get_property_unchecked(globalconf.connection, false, c->window,
486 _NET_WM_DESKTOP, XCB_GET_PROPERTY_TYPE_ANY, 0, 1);
488 c1 = xcb_get_property_unchecked(globalconf.connection, false, c->window,
489 _NET_WM_STATE, XCB_ATOM_ATOM, 0, UINT32_MAX);
491 c2 = xcb_get_property_unchecked(globalconf.connection, false, c->window,
492 _NET_WM_WINDOW_TYPE, XCB_ATOM_ATOM, 0, UINT32_MAX);
494 reply = xcb_get_property_reply(globalconf.connection, c0, NULL);
495 if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
497 desktop = *(uint32_t *) data;
498 if(desktop == -1)
499 c->sticky = true;
500 else if (desktop >= 0 && desktop < globalconf.tags.len)
501 for(int i = 0; i < globalconf.tags.len; i++)
502 if(desktop == i)
504 luaA_object_push(globalconf.L, globalconf.tags.tab[i]);
505 tag_client(c);
507 else
508 untag_client(c, globalconf.tags.tab[i]);
509 else
510 /* Value out of bounds, just give it the first tag */
511 if (globalconf.tags.len > 0)
513 luaA_object_push(globalconf.L, globalconf.tags.tab[0]);
514 tag_client(c);
518 p_delete(&reply);
520 reply = xcb_get_property_reply(globalconf.connection, c1, NULL);
521 if(reply && (data = xcb_get_property_value(reply)))
523 state = (xcb_atom_t *) data;
524 for(int i = 0; i < xcb_get_property_value_length(reply) / ssizeof(xcb_atom_t); i++)
525 ewmh_process_state_atom(c, state[i], _NET_WM_STATE_ADD);
528 p_delete(&reply);
530 reply = xcb_get_property_reply(globalconf.connection, c2, NULL);
531 if(reply && (data = xcb_get_property_value(reply)))
533 state = (xcb_atom_t *) data;
534 for(int i = 0; i < xcb_get_property_value_length(reply) / ssizeof(xcb_atom_t); i++)
535 if(state[i] == _NET_WM_WINDOW_TYPE_DESKTOP)
536 c->type = MAX(c->type, WINDOW_TYPE_DESKTOP);
537 else if(state[i] == _NET_WM_WINDOW_TYPE_DIALOG)
538 c->type = MAX(c->type, WINDOW_TYPE_DIALOG);
539 else if(state[i] == _NET_WM_WINDOW_TYPE_SPLASH)
540 c->type = MAX(c->type, WINDOW_TYPE_SPLASH);
541 else if(state[i] == _NET_WM_WINDOW_TYPE_DOCK)
542 c->type = MAX(c->type, WINDOW_TYPE_DOCK);
543 else if(state[i] == _NET_WM_WINDOW_TYPE_MENU)
544 c->type = MAX(c->type, WINDOW_TYPE_MENU);
545 else if(state[i] == _NET_WM_WINDOW_TYPE_TOOLBAR)
546 c->type = MAX(c->type, WINDOW_TYPE_TOOLBAR);
547 else if(state[i] == _NET_WM_WINDOW_TYPE_UTILITY)
548 c->type = MAX(c->type, WINDOW_TYPE_UTILITY);
551 p_delete(&reply);
554 /** Process the WM strut of a client.
555 * \param c The client.
556 * \param strut_r (Optional) An existing reply.
558 void
559 ewmh_process_client_strut(client_t *c)
561 void *data;
562 xcb_get_property_reply_t *strut_r;
564 xcb_get_property_cookie_t strut_q = xcb_get_property_unchecked(globalconf.connection, false, c->window,
565 _NET_WM_STRUT_PARTIAL, XCB_ATOM_CARDINAL, 0, 12);
566 strut_r = xcb_get_property_reply(globalconf.connection, strut_q, NULL);
568 if(strut_r
569 && strut_r->value_len
570 && (data = xcb_get_property_value(strut_r)))
572 uint32_t *strut = data;
574 if(c->strut.left != strut[0]
575 || c->strut.right != strut[1]
576 || c->strut.top != strut[2]
577 || c->strut.bottom != strut[3]
578 || c->strut.left_start_y != strut[4]
579 || c->strut.left_end_y != strut[5]
580 || c->strut.right_start_y != strut[6]
581 || c->strut.right_end_y != strut[7]
582 || c->strut.top_start_x != strut[8]
583 || c->strut.top_end_x != strut[9]
584 || c->strut.bottom_start_x != strut[10]
585 || c->strut.bottom_end_x != strut[11])
587 c->strut.left = strut[0];
588 c->strut.right = strut[1];
589 c->strut.top = strut[2];
590 c->strut.bottom = strut[3];
591 c->strut.left_start_y = strut[4];
592 c->strut.left_end_y = strut[5];
593 c->strut.right_start_y = strut[6];
594 c->strut.right_end_y = strut[7];
595 c->strut.top_start_x = strut[8];
596 c->strut.top_end_x = strut[9];
597 c->strut.bottom_start_x = strut[10];
598 c->strut.bottom_end_x = strut[11];
600 luaA_object_push(globalconf.L, c);
601 luaA_object_emit_signal(globalconf.L, -1, "property::struts", 0);
602 lua_pop(globalconf.L, 1);
606 p_delete(&strut_r);
609 /** Send request to get NET_WM_ICON (EWMH)
610 * \param w The window.
611 * \return The cookie associated with the request.
613 xcb_get_property_cookie_t
614 ewmh_window_icon_get_unchecked(xcb_window_t w)
616 return xcb_get_property_unchecked(globalconf.connection, false, w,
617 _NET_WM_ICON, XCB_ATOM_CARDINAL, 0, UINT32_MAX);
620 static cairo_surface_t *
621 ewmh_window_icon_from_reply(xcb_get_property_reply_t *r)
623 uint32_t *data;
624 uint64_t len;
626 if(!r || r->type != XCB_ATOM_CARDINAL || r->format != 32 || r->length < 2)
627 return 0;
629 data = (uint32_t *) xcb_get_property_value(r);
630 if (!data)
631 return 0;
633 /* Check that the property is as long as it should be, handling integer
634 * overflow. <uint32_t> times <another uint32_t casted to uint64_t> always
635 * fits into an uint64_t and thus this multiplication cannot overflow.
637 len = data[0] * (uint64_t) data[1];
638 if (!data[0] || !data[1] || len > r->length - 2)
639 return 0;
641 return draw_surface_from_data(data[0], data[1], data + 2);
644 /** Get NET_WM_ICON.
645 * \param cookie The cookie.
646 * \return The number of elements on stack.
648 cairo_surface_t *
649 ewmh_window_icon_get_reply(xcb_get_property_cookie_t cookie)
651 xcb_get_property_reply_t *r = xcb_get_property_reply(globalconf.connection, cookie, NULL);
652 cairo_surface_t *surface = ewmh_window_icon_from_reply(r);
653 p_delete(&r);
654 return surface;
657 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80