awesomerc: make titlebar optional and disabled by default
[awesome.git] / ewmh.c
blob17bb886b676f085d3a009ead8d8f1fd6ca634a37
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 "common/atoms.h"
37 extern awesome_t globalconf;
39 #define _NET_WM_STATE_REMOVE 0
40 #define _NET_WM_STATE_ADD 1
41 #define _NET_WM_STATE_TOGGLE 2
43 void
44 ewmh_init(int phys_screen)
46 xcb_window_t father;
47 xcb_screen_t *xscreen = xutil_screen_get(globalconf.connection, phys_screen);
48 xcb_atom_t atom[] =
50 _NET_SUPPORTED,
51 _NET_SUPPORTING_WM_CHECK,
52 _NET_CLIENT_LIST,
53 _NET_CLIENT_LIST_STACKING,
54 _NET_NUMBER_OF_DESKTOPS,
55 _NET_CURRENT_DESKTOP,
56 _NET_DESKTOP_NAMES,
57 _NET_ACTIVE_WINDOW,
58 _NET_WORKAREA,
59 _NET_CLOSE_WINDOW,
60 _NET_WM_NAME,
61 _NET_WM_ICON_NAME,
62 _NET_WM_VISIBLE_ICON_NAME,
63 _NET_WM_DESKTOP,
64 _NET_WM_WINDOW_TYPE,
65 _NET_WM_WINDOW_TYPE_NORMAL,
66 _NET_WM_WINDOW_TYPE_DESKTOP,
67 _NET_WM_WINDOW_TYPE_DOCK,
68 _NET_WM_WINDOW_TYPE_SPLASH,
69 _NET_WM_WINDOW_TYPE_DIALOG,
70 _NET_WM_ICON,
71 _NET_WM_PID,
72 _NET_WM_STATE,
73 _NET_WM_STATE_STICKY,
74 _NET_WM_STATE_SKIP_TASKBAR,
75 _NET_WM_STATE_FULLSCREEN,
76 _NET_WM_STATE_ABOVE,
77 _NET_WM_STATE_BELOW,
78 _NET_WM_STATE_MODAL,
79 _NET_WM_STATE_HIDDEN,
80 _NET_WM_STATE_DEMANDS_ATTENTION
82 int i;
84 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
85 xscreen->root, _NET_SUPPORTED, ATOM, 32,
86 countof(atom), atom);
88 /* create our own window */
89 father = xcb_generate_id(globalconf.connection);
90 xcb_create_window(globalconf.connection, xscreen->root_depth,
91 father, xscreen->root, -1, -1, 1, 1, 0,
92 XCB_COPY_FROM_PARENT, xscreen->root_visual, 0, NULL);
94 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
95 xscreen->root, _NET_SUPPORTING_WM_CHECK, WINDOW, 32,
96 1, &father);
98 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
99 father, _NET_SUPPORTING_WM_CHECK, WINDOW, 32,
100 1, &father);
102 /* set the window manager name */
103 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
104 father, _NET_WM_NAME, UTF8_STRING, 8, 7, "awesome");
106 /* set the window manager PID */
107 i = getpid();
108 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
109 father, _NET_WM_PID, CARDINAL, 32, 1, &i);
112 void
113 ewmh_update_net_client_list(int phys_screen)
115 xcb_window_t *wins;
116 client_t *c;
117 int n = 0;
119 for(c = globalconf.clients; c; c = c->next)
120 n++;
122 wins = p_new(xcb_window_t, n);
124 for(n = 0, c = globalconf.clients; c; c = c->next, n++)
125 if(c->phys_screen == phys_screen)
126 wins[n] = c->win;
128 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
129 xutil_screen_get(globalconf.connection, phys_screen)->root,
130 _NET_CLIENT_LIST, WINDOW, 32, n, wins);
132 p_delete(&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_new(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);
158 p_delete(&wins);
161 void
162 ewmh_update_net_numbers_of_desktop(int phys_screen)
164 uint32_t count = globalconf.screens[phys_screen].tags.len;
166 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
167 xutil_screen_get(globalconf.connection, phys_screen)->root,
168 _NET_NUMBER_OF_DESKTOPS, CARDINAL, 32, 1, &count);
171 void
172 ewmh_update_net_current_desktop(int phys_screen)
174 tag_array_t *tags = &globalconf.screens[phys_screen].tags;
175 uint32_t count = 0;
176 tag_t **curtags = tags_get_current(phys_screen);
178 for(count = 0; tags->tab[count] != curtags[0]; count++);
180 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
181 xutil_screen_get(globalconf.connection, phys_screen)->root,
182 _NET_CURRENT_DESKTOP, CARDINAL, 32, 1, &count);
184 p_delete(&curtags);
187 void
188 ewmh_update_net_desktop_names(int phys_screen)
190 tag_array_t *tags = &globalconf.screens[phys_screen].tags;
191 buffer_t buf;
193 buffer_inita(&buf, BUFSIZ);
195 for(int i = 0; i < tags->len; i++)
197 buffer_adds(&buf, tags->tab[i]->name);
198 buffer_addc(&buf, '\0');
201 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
202 xutil_screen_get(globalconf.connection, phys_screen)->root,
203 _NET_DESKTOP_NAMES, UTF8_STRING, 8, buf.len, buf.s);
204 buffer_wipe(&buf);
207 /** Update the work area space for each physical screen and each desktop.
208 * \param phys_screen The physical screen id.
210 void
211 ewmh_update_workarea(int phys_screen)
213 tag_array_t *tags = &globalconf.screens[phys_screen].tags;
214 uint32_t *area = p_alloca(uint32_t, tags->len * 4);
215 area_t geom = screen_area_get(phys_screen,
216 globalconf.screens[phys_screen].statusbar,
217 &globalconf.screens[phys_screen].padding);
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 tag_array_t *tags = &globalconf.screens[c->screen].tags;
255 for(int i = 0; i < tags->len; i++)
256 tag_client(c, tags->tab[i]);
258 else if(state == _NET_WM_STATE_SKIP_TASKBAR)
260 if(set == _NET_WM_STATE_REMOVE)
262 c->skiptb = false;
263 c->skip = false;
265 else if(set == _NET_WM_STATE_ADD)
267 c->skiptb = true;
268 c->skip = true;
271 else if(state == _NET_WM_STATE_FULLSCREEN)
273 area_t geometry = c->geometry;
274 if(set == _NET_WM_STATE_REMOVE)
276 /* restore geometry */
277 geometry = c->m_geometry;
278 /* restore borders and titlebar */
279 if(c->titlebar && c->titlebar->sw && (c->titlebar->position = c->titlebar->oldposition))
280 xcb_map_window(globalconf.connection, c->titlebar->sw->window);
281 c->noborder = false;
282 c->ismax = false;
283 client_setlayer(c, c->oldlayer);
284 client_setborder(c, c->oldborder);
285 client_setfloating(c, c->wasfloating);
287 else if(set == _NET_WM_STATE_ADD)
289 geometry = screen_area_get(c->screen, NULL, &globalconf.screens[c->screen].padding);
290 /* save geometry */
291 c->m_geometry = c->geometry;
292 c->wasfloating = c->isfloating;
293 /* disable titlebar and borders */
294 if(c->titlebar && c->titlebar->sw && (c->titlebar->oldposition = c->titlebar->position))
296 xcb_unmap_window(globalconf.connection, c->titlebar->sw->window);
297 c->titlebar->position = Off;
299 c->ismax = true;
300 c->oldborder = c->border;
301 client_setborder(c, 0);
302 c->noborder = true;
303 c->oldlayer = c->layer;
304 client_setlayer(c, LAYER_FULLSCREEN);
305 client_setfloating(c, true);
307 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
308 client_resize(c, geometry, false);
310 else if(state == _NET_WM_STATE_ABOVE)
312 if(set == _NET_WM_STATE_REMOVE)
313 client_setlayer(c, c->oldlayer);
314 else if(set == _NET_WM_STATE_ADD)
316 c->oldlayer = c->layer;
317 client_setlayer(c, LAYER_ABOVE);
320 else if(state == _NET_WM_STATE_BELOW)
322 if(set == _NET_WM_STATE_REMOVE)
323 client_setlayer(c, c->oldlayer);
324 else if(set == _NET_WM_STATE_ADD)
326 c->oldlayer = c->layer;
327 client_setlayer(c, LAYER_BELOW);
330 else if(state == _NET_WM_STATE_MODAL)
332 if(set == _NET_WM_STATE_REMOVE)
333 client_setlayer(c, c->oldlayer);
334 else if(set == _NET_WM_STATE_ADD)
336 c->oldlayer = c->layer;
337 client_setlayer(c, LAYER_MODAL);
340 else if(state == _NET_WM_STATE_HIDDEN)
342 if(set == _NET_WM_STATE_REMOVE)
343 c->ishidden = false;
344 else if(set == _NET_WM_STATE_ADD)
345 c->ishidden = true;
346 globalconf.screens[c->screen].need_arrange = true;
348 else if(state == _NET_WM_STATE_DEMANDS_ATTENTION)
350 if(set == _NET_WM_STATE_REMOVE)
351 c->isurgent = false;
352 else if(set == _NET_WM_STATE_ADD)
354 c->isurgent = true;
355 /* execute hook */
356 luaA_client_userdata_new(globalconf.L, c);
357 luaA_dofunction(globalconf.L, globalconf.hooks.urgent, 1, 0);
358 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
363 static void
364 ewmh_process_window_type_atom(client_t *c, xcb_atom_t state)
366 if(state == _NET_WM_WINDOW_TYPE_NORMAL)
368 /* do nothing. this is REALLY IMPORTANT */
370 else if(state == _NET_WM_WINDOW_TYPE_DOCK
371 || state == _NET_WM_WINDOW_TYPE_SPLASH)
373 c->skip = true;
374 c->isfixed = true;
375 if(c->titlebar && c->titlebar->position && c->titlebar->sw)
377 xcb_unmap_window(globalconf.connection, c->titlebar->sw->window);
378 c->titlebar->position = Off;
380 client_setborder(c, 0);
381 c->noborder = true;
382 client_setlayer(c, LAYER_ABOVE);
383 client_setfloating(c, true);
385 else if(state == _NET_WM_WINDOW_TYPE_DIALOG)
387 client_setlayer(c, LAYER_MODAL);
388 client_setfloating(c, true);
390 else if(state == _NET_WM_WINDOW_TYPE_DESKTOP)
392 tag_array_t *tags = &globalconf.screens[c->screen].tags;
393 c->noborder = true;
394 c->isfixed = true;
395 c->skip = true;
396 client_setlayer(c, LAYER_DESKTOP);
397 for(int i = 0; i < tags->len; i++)
398 tag_client(c, tags->tab[i]);
403 ewmh_process_client_message(xcb_client_message_event_t *ev)
405 client_t *c;
406 int screen;
408 if(ev->type == _NET_CURRENT_DESKTOP)
409 for(screen = 0;
410 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
411 screen++)
413 if(ev->window == xutil_screen_get(globalconf.connection, screen)->root)
414 tag_view_only_byindex(screen, ev->data.data32[0]);
416 else if(ev->type == _NET_CLOSE_WINDOW)
418 if((c = client_getbywin(ev->window)))
419 client_kill(c);
421 else if(ev->type == _NET_WM_DESKTOP)
423 if((c = client_getbywin(ev->window)))
425 tag_array_t *tags = &globalconf.screens[c->screen].tags;
427 if(ev->data.data32[0] == 0xffffffff)
428 for(int i = 0; i < tags->len; i++)
429 tag_client(c, tags->tab[i]);
430 else
431 for(int i = 0; i < tags->len; i++)
432 if((int)ev->data.data32[0] == i)
433 tag_client(c, tags->tab[i]);
434 else
435 untag_client(c, tags->tab[i]);
438 else if(ev->type == _NET_WM_STATE)
440 if((c = client_getbywin(ev->window)))
442 ewmh_process_state_atom(c, (xcb_atom_t) ev->data.data32[1], ev->data.data32[0]);
443 if(ev->data.data32[2])
444 ewmh_process_state_atom(c, (xcb_atom_t) ev->data.data32[2],
445 ev->data.data32[0]);
449 return 0;
452 void
453 ewmh_check_client_hints(client_t *c)
455 xcb_atom_t *state;
456 void *data = NULL;
457 int desktop;
458 xcb_get_property_cookie_t c0, c1, c2;
459 xcb_get_property_reply_t *reply;
461 /* Send the GetProperty requests which will be processed later */
462 c0 = xcb_get_property_unchecked(globalconf.connection, false, c->win,
463 _NET_WM_DESKTOP, XCB_GET_PROPERTY_TYPE_ANY, 0, 1);
465 c1 = xcb_get_property_unchecked(globalconf.connection, false, c->win,
466 _NET_WM_STATE, ATOM, 0, UINT32_MAX);
468 c2 = xcb_get_property_unchecked(globalconf.connection, false, c->win,
469 _NET_WM_WINDOW_TYPE, ATOM, 0, UINT32_MAX);
471 reply = xcb_get_property_reply(globalconf.connection, c0, NULL);
472 if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
474 tag_array_t *tags = &globalconf.screens[c->screen].tags;
476 desktop = *(uint32_t *) data;
477 if(desktop == -1)
478 for(int i = 0; i < tags->len; i++)
479 tag_client(c, tags->tab[i]);
480 else
481 for(int i = 0; i < tags->len; i++)
482 if(desktop == i)
483 tag_client(c, tags->tab[i]);
484 else
485 untag_client(c, tags->tab[i]);
488 p_delete(&reply);
490 reply = xcb_get_property_reply(globalconf.connection, c1, NULL);
491 if(reply && (data = xcb_get_property_value(reply)))
493 state = (xcb_atom_t *) data;
494 for(int i = 0; i < xcb_get_property_value_length(reply); i++)
495 ewmh_process_state_atom(c, state[i], _NET_WM_STATE_ADD);
498 p_delete(&reply);
500 reply = xcb_get_property_reply(globalconf.connection, c2, NULL);
501 if(reply && (data = xcb_get_property_value(reply)))
503 state = (xcb_atom_t *) data;
504 for(int i = 0; i < xcb_get_property_value_length(reply); i++)
505 ewmh_process_window_type_atom(c, state[i]);
508 p_delete(&reply);
511 /** Get NET_WM_ICON.
512 * \param w The window.
513 * \return A netwm_icon_t structure which must be deleted after usage.
515 netwm_icon_t *
516 ewmh_get_window_icon(xcb_window_t w)
518 double alpha;
519 netwm_icon_t *icon;
520 int size, i;
521 uint32_t *data;
522 unsigned char *imgdata;
523 xcb_get_property_reply_t *r;
525 r = xcb_get_property_reply(globalconf.connection,
526 xcb_get_property_unchecked(globalconf.connection, false, w,
527 _NET_WM_ICON, CARDINAL, 0, UINT32_MAX),
528 NULL);
529 if(!r || r->type != CARDINAL || r->format != 32 || r->length < 2 ||
530 !(data = (uint32_t *) xcb_get_property_value(r)))
532 p_delete(&r);
533 return NULL;
536 icon = p_new(netwm_icon_t, 1);
538 icon->width = data[0];
539 icon->height = data[1];
540 size = icon->width * icon->height;
542 if(!size)
544 p_delete(&icon);
545 p_delete(&r);
546 return NULL;
549 icon->image = p_new(unsigned char, size * 4);
550 for(imgdata = icon->image, i = 2; i < size + 2; i++, imgdata += 4)
552 imgdata[3] = (data[i] >> 24) & 0xff; /* A */
553 alpha = imgdata[3] / 255.0;
554 imgdata[2] = ((data[i] >> 16) & 0xff) * alpha; /* R */
555 imgdata[1] = ((data[i] >> 8) & 0xff) * alpha; /* G */
556 imgdata[0] = (data[i] & 0xff) * alpha; /* B */
559 p_delete(&r);
561 return icon;
565 * Restart awesome.
567 void
568 ewmh_restart(void)
570 client_t *c;
572 for(c = globalconf.clients; c; c = c->next)
573 client_unban(c);
575 xcb_aux_sync(globalconf.connection);
576 xcb_disconnect(globalconf.connection);
578 a_exec(globalconf.argv);
581 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80