xscreen: remove useless screensinfo_delete
[awesome.git] / ewmh.c
blob51fb6d1670a98c8336e931e1263be39efacf8453
1 /*
2 * ewmh.c - EWMH support functions
4 * Copyright © 2007-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 <sys/types.h>
23 #include <unistd.h>
25 #include <xcb/xcb.h>
26 #include <xcb/xcb_atom.h>
28 #include "ewmh.h"
29 #include "tag.h"
30 #include "screen.h"
31 #include "client.h"
32 #include "widget.h"
33 #include "cnode.h"
34 #include "titlebar.h"
35 #include "statusbar.h"
36 #include "common/atoms.h"
38 extern awesome_t globalconf;
40 #define _NET_WM_STATE_REMOVE 0
41 #define _NET_WM_STATE_ADD 1
42 #define _NET_WM_STATE_TOGGLE 2
44 void
45 ewmh_init(int phys_screen)
47 xcb_window_t father;
48 xcb_screen_t *xscreen = xutil_screen_get(globalconf.connection, phys_screen);
49 xcb_atom_t atom[] =
51 _NET_SUPPORTED,
52 _NET_SUPPORTING_WM_CHECK,
53 _NET_CLIENT_LIST,
54 _NET_CLIENT_LIST_STACKING,
55 _NET_NUMBER_OF_DESKTOPS,
56 _NET_CURRENT_DESKTOP,
57 _NET_DESKTOP_NAMES,
58 _NET_ACTIVE_WINDOW,
59 _NET_WORKAREA,
60 _NET_CLOSE_WINDOW,
61 _NET_WM_NAME,
62 _NET_WM_STRUT_PARTIAL,
63 _NET_WM_ICON_NAME,
64 _NET_WM_VISIBLE_ICON_NAME,
65 _NET_WM_DESKTOP,
66 _NET_WM_WINDOW_TYPE,
67 _NET_WM_WINDOW_TYPE_NORMAL,
68 _NET_WM_WINDOW_TYPE_DESKTOP,
69 _NET_WM_WINDOW_TYPE_DOCK,
70 _NET_WM_WINDOW_TYPE_SPLASH,
71 _NET_WM_WINDOW_TYPE_DIALOG,
72 _NET_WM_ICON,
73 _NET_WM_PID,
74 _NET_WM_STATE,
75 _NET_WM_STATE_STICKY,
76 _NET_WM_STATE_SKIP_TASKBAR,
77 _NET_WM_STATE_FULLSCREEN,
78 _NET_WM_STATE_ABOVE,
79 _NET_WM_STATE_BELOW,
80 _NET_WM_STATE_MODAL,
81 _NET_WM_STATE_HIDDEN,
82 _NET_WM_STATE_DEMANDS_ATTENTION
84 int i;
86 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
87 xscreen->root, _NET_SUPPORTED, ATOM, 32,
88 countof(atom), atom);
90 /* create our own window */
91 father = xcb_generate_id(globalconf.connection);
92 xcb_create_window(globalconf.connection, xscreen->root_depth,
93 father, xscreen->root, -1, -1, 1, 1, 0,
94 XCB_COPY_FROM_PARENT, xscreen->root_visual, 0, NULL);
96 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
97 xscreen->root, _NET_SUPPORTING_WM_CHECK, WINDOW, 32,
98 1, &father);
100 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
101 father, _NET_SUPPORTING_WM_CHECK, WINDOW, 32,
102 1, &father);
104 /* set the window manager name */
105 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
106 father, _NET_WM_NAME, UTF8_STRING, 8, 7, "awesome");
108 /* set the window manager PID */
109 i = getpid();
110 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
111 father, _NET_WM_PID, CARDINAL, 32, 1, &i);
114 void
115 ewmh_update_net_client_list(int phys_screen)
117 xcb_window_t *wins;
118 client_t *c;
119 int n = 0;
121 for(c = globalconf.clients; c; c = c->next)
122 n++;
124 wins = p_alloca(xcb_window_t, n);
126 for(n = 0, c = globalconf.clients; c; c = c->next, n++)
127 if(c->phys_screen == phys_screen)
128 wins[n] = c->win;
130 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
131 xutil_screen_get(globalconf.connection, phys_screen)->root,
132 _NET_CLIENT_LIST, WINDOW, 32, n, wins);
135 /** Set the client list in stacking order, bottom to top.
136 * \param phys_screen The physical screen id.
138 void
139 ewmh_update_net_client_list_stacking(int phys_screen)
141 xcb_window_t *wins;
142 client_node_t *c;
143 int n = 0;
145 for(c = globalconf.stack; c; c = c->next)
146 n++;
148 wins = p_alloca(xcb_window_t, n);
150 for(n = 0, c = *client_node_list_last(&globalconf.stack); c; c = c->prev, n++)
151 if(c->client->phys_screen == phys_screen)
152 wins[n] = c->client->win;
154 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
155 xutil_screen_get(globalconf.connection, phys_screen)->root,
156 _NET_CLIENT_LIST_STACKING, WINDOW, 32, n, wins);
159 void
160 ewmh_update_net_numbers_of_desktop(int phys_screen)
162 uint32_t count = globalconf.screens[phys_screen].tags.len;
164 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
165 xutil_screen_get(globalconf.connection, phys_screen)->root,
166 _NET_NUMBER_OF_DESKTOPS, CARDINAL, 32, 1, &count);
169 void
170 ewmh_update_net_current_desktop(int phys_screen)
172 tag_array_t *tags = &globalconf.screens[phys_screen].tags;
173 uint32_t count = 0;
174 tag_t **curtags = tags_get_current(phys_screen);
176 while(count < (uint32_t) tags->len && tags->tab[count] != curtags[0])
177 count++;
179 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
180 xutil_screen_get(globalconf.connection, phys_screen)->root,
181 _NET_CURRENT_DESKTOP, CARDINAL, 32, 1, &count);
183 p_delete(&curtags);
186 void
187 ewmh_update_net_desktop_names(int phys_screen)
189 tag_array_t *tags = &globalconf.screens[phys_screen].tags;
190 buffer_t buf;
192 buffer_inita(&buf, BUFSIZ);
194 for(int i = 0; i < tags->len; i++)
196 buffer_adds(&buf, tags->tab[i]->name);
197 buffer_addc(&buf, '\0');
200 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
201 xutil_screen_get(globalconf.connection, phys_screen)->root,
202 _NET_DESKTOP_NAMES, UTF8_STRING, 8, buf.len, buf.s);
203 buffer_wipe(&buf);
206 /** Update the work area space for each physical screen and each desktop.
207 * \param phys_screen The physical screen id.
209 void
210 ewmh_update_workarea(int phys_screen)
212 tag_array_t *tags = &globalconf.screens[phys_screen].tags;
213 uint32_t *area = p_alloca(uint32_t, tags->len * 4);
214 area_t geom = screen_area_get(phys_screen,
215 globalconf.screens[phys_screen].statusbar,
216 &globalconf.screens[phys_screen].padding,
217 true);
220 for(int i = 0; i < tags->len; i++)
222 area[4 * i + 0] = geom.x;
223 area[4 * i + 1] = geom.y;
224 area[4 * i + 2] = geom.width;
225 area[4 * i + 3] = geom.height;
228 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
229 xutil_screen_get(globalconf.connection, phys_screen)->root,
230 _NET_WORKAREA, CARDINAL, 32, tags->len * 4, area);
233 void
234 ewmh_update_net_active_window(int phys_screen)
236 xcb_window_t win;
238 if(globalconf.screen_focus->client_focus
239 && globalconf.screen_focus->client_focus->phys_screen == phys_screen)
240 win = globalconf.screen_focus->client_focus->win;
241 else
242 win = XCB_NONE;
244 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
245 xutil_screen_get(globalconf.connection, phys_screen)->root,
246 _NET_ACTIVE_WINDOW, WINDOW, 32, 1, &win);
249 static void
250 ewmh_process_state_atom(client_t *c, xcb_atom_t state, int set)
252 if(state == _NET_WM_STATE_STICKY)
254 if(set == _NET_WM_STATE_REMOVE)
255 client_setsticky(c, false);
256 else if(set == _NET_WM_STATE_ADD)
257 client_setsticky(c, true);
258 else if(set == _NET_WM_STATE_TOGGLE)
259 client_setsticky(c, !c->issticky);
261 else if(state == _NET_WM_STATE_SKIP_TASKBAR)
263 if(set == _NET_WM_STATE_REMOVE)
265 c->skiptb = false;
266 ewmh_client_update_hints(c);
268 else if(set == _NET_WM_STATE_ADD)
270 c->skiptb = true;
271 ewmh_client_update_hints(c);
273 else if(set == _NET_WM_STATE_TOGGLE)
275 c->skiptb = !c->skiptb;
276 ewmh_client_update_hints(c);
279 else if(state == _NET_WM_STATE_FULLSCREEN)
281 if(set == _NET_WM_STATE_REMOVE)
282 client_setfullscreen(c, false);
283 else if(set == _NET_WM_STATE_ADD)
284 client_setfullscreen(c, true);
285 else if(set == _NET_WM_STATE_TOGGLE)
286 client_setfullscreen(c, !c->isfullscreen);
288 else if(state == _NET_WM_STATE_ABOVE)
290 if(set == _NET_WM_STATE_REMOVE)
291 client_setabove(c, false);
292 else if(set == _NET_WM_STATE_ADD)
293 client_setabove(c, true);
294 else if(set == _NET_WM_STATE_TOGGLE)
295 client_setabove(c, !c->isabove);
297 else if(state == _NET_WM_STATE_BELOW)
299 if(set == _NET_WM_STATE_REMOVE)
300 client_setbelow(c, false);
301 else if(set == _NET_WM_STATE_ADD)
302 client_setbelow(c, true);
303 else if(set == _NET_WM_STATE_TOGGLE)
304 client_setbelow(c, !c->isbelow);
306 else if(state == _NET_WM_STATE_MODAL)
308 if(set == _NET_WM_STATE_REMOVE)
309 client_setmodal(c, false);
310 else if(set == _NET_WM_STATE_ADD)
311 client_setmodal(c, true);
312 else if(set == _NET_WM_STATE_TOGGLE)
313 client_setmodal(c, !c->ismodal);
315 else if(state == _NET_WM_STATE_HIDDEN)
317 if(set == _NET_WM_STATE_REMOVE)
318 client_setminimized(c, false);
319 else if(set == _NET_WM_STATE_ADD)
320 client_setminimized(c, true);
321 else if(set == _NET_WM_STATE_TOGGLE)
322 client_setminimized(c, !c->isminimized);
324 else if(state == _NET_WM_STATE_DEMANDS_ATTENTION)
326 if(set == _NET_WM_STATE_REMOVE)
327 c->isurgent = false;
328 else if(set == _NET_WM_STATE_ADD)
329 c->isurgent = true;
330 else if(set == _NET_WM_STATE_TOGGLE)
331 c->isurgent = !c->isurgent;
333 /* execute hook */
334 luaA_client_userdata_new(globalconf.L, c);
335 luaA_dofunction(globalconf.L, globalconf.hooks.urgent, 1, 0);
336 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
341 ewmh_process_client_message(xcb_client_message_event_t *ev)
343 client_t *c;
344 int screen;
346 if(ev->type == _NET_CURRENT_DESKTOP)
347 for(screen = 0;
348 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
349 screen++)
351 if(ev->window == xutil_screen_get(globalconf.connection, screen)->root)
352 tag_view_only_byindex(screen, ev->data.data32[0]);
354 else if(ev->type == _NET_CLOSE_WINDOW)
356 if((c = client_getbywin(ev->window)))
357 client_kill(c);
359 else if(ev->type == _NET_WM_DESKTOP)
361 if((c = client_getbywin(ev->window)))
363 tag_array_t *tags = &globalconf.screens[c->screen].tags;
365 if(ev->data.data32[0] == 0xffffffff)
366 c->issticky = true;
367 else
368 for(int i = 0; i < tags->len; i++)
369 if((int)ev->data.data32[0] == i)
370 tag_client(c, tags->tab[i]);
371 else
372 untag_client(c, tags->tab[i]);
375 else if(ev->type == _NET_WM_STATE)
377 if((c = client_getbywin(ev->window)))
379 ewmh_process_state_atom(c, (xcb_atom_t) ev->data.data32[1], ev->data.data32[0]);
380 if(ev->data.data32[2])
381 ewmh_process_state_atom(c, (xcb_atom_t) ev->data.data32[2],
382 ev->data.data32[0]);
386 return 0;
389 /** Update client EWMH hints.
390 * \param c The client.
392 void
393 ewmh_client_update_hints(client_t *c)
395 xcb_atom_t state[8]; /* number of defined state atoms */
396 int i = 0;
398 if(c->ismodal)
399 state[i++] = _NET_WM_STATE_MODAL;
400 if(c->isfullscreen)
401 state[i++] = _NET_WM_STATE_FULLSCREEN;
402 if(c->issticky)
403 state[i++] = _NET_WM_STATE_STICKY;
404 if(c->skiptb)
405 state[i++] = _NET_WM_STATE_SKIP_TASKBAR;
406 if(c->isabove)
407 state[i++] = _NET_WM_STATE_ABOVE;
408 if(c->isbelow)
409 state[i++] = _NET_WM_STATE_BELOW;
410 if(c->isminimized)
411 state[i++] = _NET_WM_STATE_HIDDEN;
412 if(c->isurgent)
413 state[i++] = _NET_WM_STATE_DEMANDS_ATTENTION;
415 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
416 c->win, _NET_WM_STATE, ATOM, 32, i, state);
419 void
420 ewmh_client_check_hints(client_t *c)
422 xcb_atom_t *state;
423 void *data = NULL;
424 int desktop;
425 xcb_get_property_cookie_t c0, c1, c2;
426 xcb_get_property_reply_t *reply;
428 /* Send the GetProperty requests which will be processed later */
429 c0 = xcb_get_property_unchecked(globalconf.connection, false, c->win,
430 _NET_WM_DESKTOP, XCB_GET_PROPERTY_TYPE_ANY, 0, 1);
432 c1 = xcb_get_property_unchecked(globalconf.connection, false, c->win,
433 _NET_WM_STATE, ATOM, 0, UINT32_MAX);
435 c2 = xcb_get_property_unchecked(globalconf.connection, false, c->win,
436 _NET_WM_WINDOW_TYPE, ATOM, 0, UINT32_MAX);
438 reply = xcb_get_property_reply(globalconf.connection, c0, NULL);
439 if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
441 tag_array_t *tags = &globalconf.screens[c->screen].tags;
443 desktop = *(uint32_t *) data;
444 if(desktop == -1)
445 c->issticky = true;
446 else
447 for(int i = 0; i < tags->len; i++)
448 if(desktop == i)
449 tag_client(c, tags->tab[i]);
450 else
451 untag_client(c, tags->tab[i]);
454 p_delete(&reply);
456 reply = xcb_get_property_reply(globalconf.connection, c1, NULL);
457 if(reply && (data = xcb_get_property_value(reply)))
459 state = (xcb_atom_t *) data;
460 for(int i = 0; i < xcb_get_property_value_length(reply); i++)
461 ewmh_process_state_atom(c, state[i], _NET_WM_STATE_ADD);
464 p_delete(&reply);
466 reply = xcb_get_property_reply(globalconf.connection, c2, NULL);
467 if(reply && (data = xcb_get_property_value(reply)))
469 state = (xcb_atom_t *) data;
470 for(int i = 0; i < xcb_get_property_value_length(reply); i++)
471 if(state[i] == _NET_WM_WINDOW_TYPE_DESKTOP)
472 c->type = WINDOW_TYPE_DESKTOP;
473 else if(state[i] == _NET_WM_WINDOW_TYPE_DIALOG)
474 c->type = WINDOW_TYPE_DIALOG;
475 else if(state[i] == _NET_WM_WINDOW_TYPE_SPLASH)
476 c->type = WINDOW_TYPE_SPLASH;
477 else if(state[i] == _NET_WM_WINDOW_TYPE_DOCK)
478 c->type = WINDOW_TYPE_DOCK;
479 else
480 c->type = WINDOW_TYPE_NORMAL;
483 p_delete(&reply);
486 /** Update the WM strut of a client.
487 * \param c The client.
489 void
490 ewmh_client_strut_update(client_t *c, xcb_get_property_reply_t *strut_r)
492 void *data;
493 xcb_get_property_reply_t *mstrut_r = NULL;
495 if(!strut_r)
497 xcb_get_property_cookie_t strut_q = xcb_get_property_unchecked(globalconf.connection, false, c->win,
498 _NET_WM_STRUT_PARTIAL, CARDINAL, 0, 12);
499 strut_r = mstrut_r = xcb_get_property_reply(globalconf.connection, strut_q, NULL);
502 if(strut_r
503 && strut_r->value_len
504 && (data = xcb_get_property_value(strut_r)))
506 uint32_t *strut = data;
508 if(c->strut.left != strut[0]
509 || c->strut.right != strut[1]
510 || c->strut.top != strut[2]
511 || c->strut.bottom != strut[3]
512 || c->strut.left_start_y != strut[4]
513 || c->strut.left_end_y != strut[5]
514 || c->strut.right_start_y != strut[6]
515 || c->strut.right_end_y != strut[7]
516 || c->strut.top_start_x != strut[8]
517 || c->strut.top_end_x != strut[9]
518 || c->strut.bottom_start_x != strut[10]
519 || c->strut.bottom_end_x != strut[11])
521 c->strut.left = strut[0];
522 c->strut.right = strut[1];
523 c->strut.top = strut[2];
524 c->strut.bottom = strut[3];
525 c->strut.left_start_y = strut[4];
526 c->strut.left_end_y = strut[5];
527 c->strut.right_start_y = strut[6];
528 c->strut.right_end_y = strut[7];
529 c->strut.top_start_x = strut[8];
530 c->strut.top_end_x = strut[9];
531 c->strut.bottom_start_x = strut[10];
532 c->strut.bottom_end_x = strut[11];
534 client_need_arrange(c);
535 /* All the statusbars (may) need to be repositioned */
536 for(int screen = 0; screen < globalconf.screens_info->nscreen; screen++)
537 for(statusbar_t *s = globalconf.screens[screen].statusbar; s; s = s->next)
538 statusbar_position_update(s);
542 p_delete(&mstrut_r);
545 /** Send request to get NET_WM_ICON (EWMH)
546 * \param w The window.
547 * \return The cookie associated with the request.
549 xcb_get_property_cookie_t
550 ewmh_window_icon_get_unchecked(xcb_window_t w)
552 return xcb_get_property_unchecked(globalconf.connection, false, w,
553 _NET_WM_ICON, CARDINAL, 0, UINT32_MAX);
556 image_t *
557 ewmh_window_icon_from_reply(xcb_get_property_reply_t *r)
559 uint32_t *data;
561 if(!r || r->type != CARDINAL || r->format != 32 || r->length < 2 ||
562 !(data = (uint32_t *) xcb_get_property_value(r)))
563 return NULL;
565 if(data[0] && data[1])
566 return image_new_from_argb32(data[0], data[1], data + 2);
568 return NULL;
571 /** Get NET_WM_ICON.
572 * \param cookie The cookie.
573 * \return A draw_image_t structure which must be deleted after usage.
575 image_t *
576 ewmh_window_icon_get_reply(xcb_get_property_cookie_t cookie)
578 xcb_get_property_reply_t *r = xcb_get_property_reply(globalconf.connection, cookie, NULL);
579 image_t *icon = ewmh_window_icon_from_reply(r);
580 p_delete(&r);
581 return icon;
584 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80