luaa: merge tostring() with DO_LUA_NEW
[awesome.git] / ewmh.c
blob3974ea78dcefad8f10b46ac129405a111fba5ddf
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 "wibox.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_STRUT_PARTIAL,
62 _NET_WM_ICON_NAME,
63 _NET_WM_VISIBLE_ICON_NAME,
64 _NET_WM_DESKTOP,
65 _NET_WM_WINDOW_TYPE,
66 _NET_WM_WINDOW_TYPE_NORMAL,
67 _NET_WM_WINDOW_TYPE_DESKTOP,
68 _NET_WM_WINDOW_TYPE_DOCK,
69 _NET_WM_WINDOW_TYPE_SPLASH,
70 _NET_WM_WINDOW_TYPE_DIALOG,
71 _NET_WM_ICON,
72 _NET_WM_PID,
73 _NET_WM_STATE,
74 _NET_WM_STATE_STICKY,
75 _NET_WM_STATE_SKIP_TASKBAR,
76 _NET_WM_STATE_FULLSCREEN,
77 _NET_WM_STATE_ABOVE,
78 _NET_WM_STATE_BELOW,
79 _NET_WM_STATE_MODAL,
80 _NET_WM_STATE_HIDDEN,
81 _NET_WM_STATE_DEMANDS_ATTENTION
83 int i;
85 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
86 xscreen->root, _NET_SUPPORTED, ATOM, 32,
87 countof(atom), atom);
89 /* create our own window */
90 father = xcb_generate_id(globalconf.connection);
91 xcb_create_window(globalconf.connection, xscreen->root_depth,
92 father, xscreen->root, -1, -1, 1, 1, 0,
93 XCB_COPY_FROM_PARENT, xscreen->root_visual, 0, NULL);
95 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
96 xscreen->root, _NET_SUPPORTING_WM_CHECK, WINDOW, 32,
97 1, &father);
99 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
100 father, _NET_SUPPORTING_WM_CHECK, WINDOW, 32,
101 1, &father);
103 /* set the window manager name */
104 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
105 father, _NET_WM_NAME, UTF8_STRING, 8, 7, "awesome");
107 /* set the window manager PID */
108 i = getpid();
109 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
110 father, _NET_WM_PID, CARDINAL, 32, 1, &i);
113 void
114 ewmh_update_net_client_list(int phys_screen)
116 xcb_window_t *wins;
117 client_t *c;
118 int n = 0;
120 for(c = globalconf.clients; c; c = c->next)
121 n++;
123 wins = p_alloca(xcb_window_t, n);
125 for(n = 0, c = globalconf.clients; c; c = c->next, n++)
126 if(c->phys_screen == phys_screen)
127 wins[n] = c->win;
129 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
130 xutil_screen_get(globalconf.connection, phys_screen)->root,
131 _NET_CLIENT_LIST, WINDOW, 32, n, wins);
134 /** Set the client list in stacking order, bottom to top.
135 * \param phys_screen The physical screen id.
137 void
138 ewmh_update_net_client_list_stacking(int phys_screen)
140 xcb_window_t *wins;
141 client_node_t *c;
142 int n = 0;
144 for(c = globalconf.stack; c; c = c->next)
145 n++;
147 wins = p_alloca(xcb_window_t, n);
149 for(n = 0, c = *client_node_list_last(&globalconf.stack); c; c = c->prev, n++)
150 if(c->client->phys_screen == phys_screen)
151 wins[n] = c->client->win;
153 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
154 xutil_screen_get(globalconf.connection, phys_screen)->root,
155 _NET_CLIENT_LIST_STACKING, WINDOW, 32, n, wins);
158 void
159 ewmh_update_net_numbers_of_desktop(int phys_screen)
161 uint32_t count = globalconf.screens[phys_screen].tags.len;
163 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
164 xutil_screen_get(globalconf.connection, phys_screen)->root,
165 _NET_NUMBER_OF_DESKTOPS, CARDINAL, 32, 1, &count);
168 void
169 ewmh_update_net_current_desktop(int phys_screen)
171 tag_array_t *tags = &globalconf.screens[phys_screen].tags;
172 uint32_t count = 0;
173 tag_t **curtags = tags_get_current(phys_screen);
175 while(count < (uint32_t) tags->len && tags->tab[count] != curtags[0])
176 count++;
178 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
179 xutil_screen_get(globalconf.connection, phys_screen)->root,
180 _NET_CURRENT_DESKTOP, CARDINAL, 32, 1, &count);
182 p_delete(&curtags);
185 void
186 ewmh_update_net_desktop_names(int phys_screen)
188 tag_array_t *tags = &globalconf.screens[phys_screen].tags;
189 buffer_t buf;
191 buffer_inita(&buf, BUFSIZ);
193 for(int i = 0; i < tags->len; i++)
195 buffer_adds(&buf, tags->tab[i]->name);
196 buffer_addc(&buf, '\0');
199 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
200 xutil_screen_get(globalconf.connection, phys_screen)->root,
201 _NET_DESKTOP_NAMES, UTF8_STRING, 8, buf.len, buf.s);
202 buffer_wipe(&buf);
205 /** Update the work area space for each physical screen and each desktop.
206 * \param phys_screen The physical screen id.
208 void
209 ewmh_update_workarea(int phys_screen)
211 tag_array_t *tags = &globalconf.screens[phys_screen].tags;
212 uint32_t *area = p_alloca(uint32_t, tags->len * 4);
213 area_t geom = screen_area_get(phys_screen,
214 &globalconf.screens[phys_screen].wiboxes,
215 &globalconf.screens[phys_screen].padding,
216 true);
219 for(int i = 0; i < tags->len; i++)
221 area[4 * i + 0] = geom.x;
222 area[4 * i + 1] = geom.y;
223 area[4 * i + 2] = geom.width;
224 area[4 * i + 3] = geom.height;
227 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
228 xutil_screen_get(globalconf.connection, phys_screen)->root,
229 _NET_WORKAREA, CARDINAL, 32, tags->len * 4, area);
232 void
233 ewmh_update_net_active_window(int phys_screen)
235 xcb_window_t win;
237 if(globalconf.screen_focus->client_focus
238 && globalconf.screen_focus->client_focus->phys_screen == phys_screen)
239 win = globalconf.screen_focus->client_focus->win;
240 else
241 win = XCB_NONE;
243 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
244 xutil_screen_get(globalconf.connection, phys_screen)->root,
245 _NET_ACTIVE_WINDOW, WINDOW, 32, 1, &win);
248 static void
249 ewmh_process_state_atom(client_t *c, xcb_atom_t state, int set)
251 if(state == _NET_WM_STATE_STICKY)
253 if(set == _NET_WM_STATE_REMOVE)
254 client_setsticky(c, false);
255 else if(set == _NET_WM_STATE_ADD)
256 client_setsticky(c, true);
257 else if(set == _NET_WM_STATE_TOGGLE)
258 client_setsticky(c, !c->issticky);
260 else if(state == _NET_WM_STATE_SKIP_TASKBAR)
262 if(set == _NET_WM_STATE_REMOVE)
264 c->skiptb = false;
265 ewmh_client_update_hints(c);
267 else if(set == _NET_WM_STATE_ADD)
269 c->skiptb = true;
270 ewmh_client_update_hints(c);
272 else if(set == _NET_WM_STATE_TOGGLE)
274 c->skiptb = !c->skiptb;
275 ewmh_client_update_hints(c);
278 else if(state == _NET_WM_STATE_FULLSCREEN)
280 if(set == _NET_WM_STATE_REMOVE)
281 client_setfullscreen(c, false);
282 else if(set == _NET_WM_STATE_ADD)
283 client_setfullscreen(c, true);
284 else if(set == _NET_WM_STATE_TOGGLE)
285 client_setfullscreen(c, !c->isfullscreen);
287 else if(state == _NET_WM_STATE_ABOVE)
289 if(set == _NET_WM_STATE_REMOVE)
290 client_setabove(c, false);
291 else if(set == _NET_WM_STATE_ADD)
292 client_setabove(c, true);
293 else if(set == _NET_WM_STATE_TOGGLE)
294 client_setabove(c, !c->isabove);
296 else if(state == _NET_WM_STATE_BELOW)
298 if(set == _NET_WM_STATE_REMOVE)
299 client_setbelow(c, false);
300 else if(set == _NET_WM_STATE_ADD)
301 client_setbelow(c, true);
302 else if(set == _NET_WM_STATE_TOGGLE)
303 client_setbelow(c, !c->isbelow);
305 else if(state == _NET_WM_STATE_MODAL)
307 if(set == _NET_WM_STATE_REMOVE)
308 client_setmodal(c, false);
309 else if(set == _NET_WM_STATE_ADD)
310 client_setmodal(c, true);
311 else if(set == _NET_WM_STATE_TOGGLE)
312 client_setmodal(c, !c->ismodal);
314 else if(state == _NET_WM_STATE_HIDDEN)
316 if(set == _NET_WM_STATE_REMOVE)
317 client_setminimized(c, false);
318 else if(set == _NET_WM_STATE_ADD)
319 client_setminimized(c, true);
320 else if(set == _NET_WM_STATE_TOGGLE)
321 client_setminimized(c, !c->isminimized);
323 else if(state == _NET_WM_STATE_DEMANDS_ATTENTION)
325 if(set == _NET_WM_STATE_REMOVE)
326 c->isurgent = false;
327 else if(set == _NET_WM_STATE_ADD)
328 c->isurgent = true;
329 else if(set == _NET_WM_STATE_TOGGLE)
330 c->isurgent = !c->isurgent;
332 /* execute hook */
333 luaA_client_userdata_new(globalconf.L, c);
334 lua_pushliteral(globalconf.L, "urgent");
335 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0);
340 ewmh_process_client_message(xcb_client_message_event_t *ev)
342 client_t *c;
343 int screen;
345 if(ev->type == _NET_CURRENT_DESKTOP)
346 for(screen = 0;
347 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
348 screen++)
350 if(ev->window == xutil_screen_get(globalconf.connection, screen)->root)
351 tag_view_only_byindex(screen, ev->data.data32[0]);
353 else if(ev->type == _NET_CLOSE_WINDOW)
355 if((c = client_getbywin(ev->window)))
356 client_kill(c);
358 else if(ev->type == _NET_WM_DESKTOP)
360 if((c = client_getbywin(ev->window)))
362 tag_array_t *tags = &globalconf.screens[c->screen].tags;
364 if(ev->data.data32[0] == 0xffffffff)
365 c->issticky = true;
366 else
367 for(int i = 0; i < tags->len; i++)
368 if((int)ev->data.data32[0] == i)
369 tag_client(c, tags->tab[i]);
370 else
371 untag_client(c, tags->tab[i]);
374 else if(ev->type == _NET_WM_STATE)
376 if((c = client_getbywin(ev->window)))
378 ewmh_process_state_atom(c, (xcb_atom_t) ev->data.data32[1], ev->data.data32[0]);
379 if(ev->data.data32[2])
380 ewmh_process_state_atom(c, (xcb_atom_t) ev->data.data32[2],
381 ev->data.data32[0]);
385 return 0;
388 /** Update client EWMH hints.
389 * \param c The client.
391 void
392 ewmh_client_update_hints(client_t *c)
394 xcb_atom_t state[8]; /* number of defined state atoms */
395 int i = 0;
397 if(c->ismodal)
398 state[i++] = _NET_WM_STATE_MODAL;
399 if(c->isfullscreen)
400 state[i++] = _NET_WM_STATE_FULLSCREEN;
401 if(c->issticky)
402 state[i++] = _NET_WM_STATE_STICKY;
403 if(c->skiptb)
404 state[i++] = _NET_WM_STATE_SKIP_TASKBAR;
405 if(c->isabove)
406 state[i++] = _NET_WM_STATE_ABOVE;
407 if(c->isbelow)
408 state[i++] = _NET_WM_STATE_BELOW;
409 if(c->isminimized)
410 state[i++] = _NET_WM_STATE_HIDDEN;
411 if(c->isurgent)
412 state[i++] = _NET_WM_STATE_DEMANDS_ATTENTION;
414 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
415 c->win, _NET_WM_STATE, ATOM, 32, i, state);
418 void
419 ewmh_client_check_hints(client_t *c)
421 xcb_atom_t *state;
422 void *data = NULL;
423 int desktop;
424 xcb_get_property_cookie_t c0, c1, c2;
425 xcb_get_property_reply_t *reply;
427 /* Send the GetProperty requests which will be processed later */
428 c0 = xcb_get_property_unchecked(globalconf.connection, false, c->win,
429 _NET_WM_DESKTOP, XCB_GET_PROPERTY_TYPE_ANY, 0, 1);
431 c1 = xcb_get_property_unchecked(globalconf.connection, false, c->win,
432 _NET_WM_STATE, ATOM, 0, UINT32_MAX);
434 c2 = xcb_get_property_unchecked(globalconf.connection, false, c->win,
435 _NET_WM_WINDOW_TYPE, ATOM, 0, UINT32_MAX);
437 reply = xcb_get_property_reply(globalconf.connection, c0, NULL);
438 if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
440 tag_array_t *tags = &globalconf.screens[c->screen].tags;
442 desktop = *(uint32_t *) data;
443 if(desktop == -1)
444 c->issticky = true;
445 else
446 for(int i = 0; i < tags->len; i++)
447 if(desktop == i)
448 tag_client(c, tags->tab[i]);
449 else
450 untag_client(c, tags->tab[i]);
453 p_delete(&reply);
455 reply = xcb_get_property_reply(globalconf.connection, c1, NULL);
456 if(reply && (data = xcb_get_property_value(reply)))
458 state = (xcb_atom_t *) data;
459 for(int i = 0; i < xcb_get_property_value_length(reply); i++)
460 ewmh_process_state_atom(c, state[i], _NET_WM_STATE_ADD);
463 p_delete(&reply);
465 reply = xcb_get_property_reply(globalconf.connection, c2, NULL);
466 if(reply && (data = xcb_get_property_value(reply)))
468 state = (xcb_atom_t *) data;
469 for(int i = 0; i < xcb_get_property_value_length(reply); i++)
470 if(state[i] == _NET_WM_WINDOW_TYPE_DESKTOP)
471 c->type = MAX(c->type, WINDOW_TYPE_DESKTOP);
472 else if(state[i] == _NET_WM_WINDOW_TYPE_DIALOG)
473 c->type = MAX(c->type, WINDOW_TYPE_DIALOG);
474 else if(state[i] == _NET_WM_WINDOW_TYPE_SPLASH)
475 c->type = MAX(c->type, WINDOW_TYPE_SPLASH);
476 else if(state[i] == _NET_WM_WINDOW_TYPE_DOCK)
477 c->type = MAX(c->type, WINDOW_TYPE_DOCK);
480 p_delete(&reply);
483 /** Update the WM strut of a client.
484 * \param c The client.
486 void
487 ewmh_client_strut_update(client_t *c, xcb_get_property_reply_t *strut_r)
489 void *data;
490 xcb_get_property_reply_t *mstrut_r = NULL;
492 if(!strut_r)
494 xcb_get_property_cookie_t strut_q = xcb_get_property_unchecked(globalconf.connection, false, c->win,
495 _NET_WM_STRUT_PARTIAL, CARDINAL, 0, 12);
496 strut_r = mstrut_r = xcb_get_property_reply(globalconf.connection, strut_q, NULL);
499 if(strut_r
500 && strut_r->value_len
501 && (data = xcb_get_property_value(strut_r)))
503 uint32_t *strut = data;
505 if(c->strut.left != strut[0]
506 || c->strut.right != strut[1]
507 || c->strut.top != strut[2]
508 || c->strut.bottom != strut[3]
509 || c->strut.left_start_y != strut[4]
510 || c->strut.left_end_y != strut[5]
511 || c->strut.right_start_y != strut[6]
512 || c->strut.right_end_y != strut[7]
513 || c->strut.top_start_x != strut[8]
514 || c->strut.top_end_x != strut[9]
515 || c->strut.bottom_start_x != strut[10]
516 || c->strut.bottom_end_x != strut[11])
518 c->strut.left = strut[0];
519 c->strut.right = strut[1];
520 c->strut.top = strut[2];
521 c->strut.bottom = strut[3];
522 c->strut.left_start_y = strut[4];
523 c->strut.left_end_y = strut[5];
524 c->strut.right_start_y = strut[6];
525 c->strut.right_end_y = strut[7];
526 c->strut.top_start_x = strut[8];
527 c->strut.top_end_x = strut[9];
528 c->strut.bottom_start_x = strut[10];
529 c->strut.bottom_end_x = strut[11];
531 client_need_arrange(c);
532 /* All the wiboxes (may) need to be repositioned */
533 for(int screen = 0; screen < globalconf.nscreen; screen++)
534 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
536 wibox_t *s = globalconf.screens[screen].wiboxes.tab[i];
537 wibox_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