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>
26 #include <xcb/xcb_atom.h>
34 #include "common/atoms.h"
35 #include "common/buffer.h"
36 #include "common/xutil.h"
38 #define _NET_WM_STATE_REMOVE 0
39 #define _NET_WM_STATE_ADD 1
40 #define _NET_WM_STATE_TOGGLE 2
42 /** Update the desktop geometry.
43 * \param phys_screen The physical screen id.
46 ewmh_update_desktop_geometry(int phys_screen
)
48 area_t geom
= screen_area_get(&globalconf
.screens
.tab
[phys_screen
], false);
49 uint32_t sizes
[] = { geom
.width
, geom
.height
};
51 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
52 xutil_screen_get(globalconf
.connection
, phys_screen
)->root
,
53 _NET_DESKTOP_GEOMETRY
, CARDINAL
, 32, countof(sizes
), sizes
);
57 ewmh_init(int phys_screen
)
60 xcb_screen_t
*xscreen
= xutil_screen_get(globalconf
.connection
, phys_screen
);
64 _NET_SUPPORTING_WM_CHECK
,
67 _NET_CLIENT_LIST_STACKING
,
68 _NET_NUMBER_OF_DESKTOPS
,
73 _NET_DESKTOP_GEOMETRY
,
76 _NET_WM_STRUT_PARTIAL
,
78 _NET_WM_VISIBLE_ICON_NAME
,
81 _NET_WM_WINDOW_TYPE_DESKTOP
,
82 _NET_WM_WINDOW_TYPE_DOCK
,
83 _NET_WM_WINDOW_TYPE_TOOLBAR
,
84 _NET_WM_WINDOW_TYPE_MENU
,
85 _NET_WM_WINDOW_TYPE_UTILITY
,
86 _NET_WM_WINDOW_TYPE_SPLASH
,
87 _NET_WM_WINDOW_TYPE_DIALOG
,
88 _NET_WM_WINDOW_TYPE_DROPDOWN_MENU
,
89 _NET_WM_WINDOW_TYPE_POPUP_MENU
,
90 _NET_WM_WINDOW_TYPE_TOOLTIP
,
91 _NET_WM_WINDOW_TYPE_NOTIFICATION
,
92 _NET_WM_WINDOW_TYPE_COMBO
,
93 _NET_WM_WINDOW_TYPE_DND
,
94 _NET_WM_WINDOW_TYPE_NORMAL
,
99 _NET_WM_STATE_SKIP_TASKBAR
,
100 _NET_WM_STATE_FULLSCREEN
,
101 _NET_WM_STATE_MAXIMIZED_HORZ
,
102 _NET_WM_STATE_MAXIMIZED_VERT
,
106 _NET_WM_STATE_HIDDEN
,
107 _NET_WM_STATE_DEMANDS_ATTENTION
111 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
112 xscreen
->root
, _NET_SUPPORTED
, ATOM
, 32,
113 countof(atom
), atom
);
115 /* create our own window */
116 father
= xcb_generate_id(globalconf
.connection
);
117 xcb_create_window(globalconf
.connection
, xscreen
->root_depth
,
118 father
, xscreen
->root
, -1, -1, 1, 1, 0,
119 XCB_COPY_FROM_PARENT
, xscreen
->root_visual
, 0, NULL
);
121 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
122 xscreen
->root
, _NET_SUPPORTING_WM_CHECK
, WINDOW
, 32,
125 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
126 father
, _NET_SUPPORTING_WM_CHECK
, WINDOW
, 32,
129 /* set the window manager name */
130 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
131 father
, _NET_WM_NAME
, UTF8_STRING
, 8, 7, "awesome");
133 /* set the window manager PID */
135 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
136 father
, _NET_WM_PID
, CARDINAL
, 32, 1, &i
);
138 ewmh_update_desktop_geometry(phys_screen
);
142 ewmh_update_net_client_list(int phys_screen
)
144 xcb_window_t
*wins
= p_alloca(xcb_window_t
, globalconf
.clients
.len
);
147 foreach(_c
, globalconf
.clients
)
150 if(c
->phys_screen
== phys_screen
)
154 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
155 xutil_screen_get(globalconf
.connection
, phys_screen
)->root
,
156 _NET_CLIENT_LIST
, WINDOW
, 32, n
, wins
);
159 /** Set the client list in stacking order, bottom to top.
160 * \param phys_screen The physical screen id.
163 ewmh_update_net_client_list_stacking(int phys_screen
)
166 xcb_window_t
*wins
= p_alloca(xcb_window_t
, globalconf
.stack
.len
);
168 foreach(client
, globalconf
.stack
)
169 if((*client
)->phys_screen
== phys_screen
)
170 wins
[n
++] = (*client
)->win
;
172 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
173 xutil_screen_get(globalconf
.connection
, phys_screen
)->root
,
174 _NET_CLIENT_LIST_STACKING
, WINDOW
, 32, n
, wins
);
178 ewmh_update_net_numbers_of_desktop(int phys_screen
)
180 uint32_t count
= globalconf
.screens
.tab
[phys_screen
].tags
.len
;
182 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
183 xutil_screen_get(globalconf
.connection
, phys_screen
)->root
,
184 _NET_NUMBER_OF_DESKTOPS
, CARDINAL
, 32, 1, &count
);
188 ewmh_update_net_current_desktop(int phys_screen
)
190 uint32_t idx
= tags_get_first_selected_index(&globalconf
.screens
.tab
[phys_screen
]);
192 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
193 xutil_screen_get(globalconf
.connection
, phys_screen
)->root
,
194 _NET_CURRENT_DESKTOP
, CARDINAL
, 32, 1, &idx
);
198 ewmh_update_net_desktop_names(int phys_screen
)
200 tag_array_t
*tags
= &globalconf
.screens
.tab
[phys_screen
].tags
;
203 buffer_inita(&buf
, BUFSIZ
);
205 for(int i
= 0; i
< tags
->len
; i
++)
207 buffer_adds(&buf
, tags
->tab
[i
]->name
);
208 buffer_addc(&buf
, '\0');
211 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
212 xutil_screen_get(globalconf
.connection
, phys_screen
)->root
,
213 _NET_DESKTOP_NAMES
, UTF8_STRING
, 8, buf
.len
, buf
.s
);
217 /** Update the work area space for each physical screen and each desktop.
218 * \param phys_screen The physical screen id.
221 ewmh_update_workarea(int phys_screen
)
223 tag_array_t
*tags
= &globalconf
.screens
.tab
[phys_screen
].tags
;
224 uint32_t *area
= p_alloca(uint32_t, tags
->len
* 4);
225 area_t geom
= screen_area_get(&globalconf
.screens
.tab
[phys_screen
], true);
227 for(int i
= 0; i
< tags
->len
; i
++)
229 area
[4 * i
+ 0] = geom
.x
;
230 area
[4 * i
+ 1] = geom
.y
;
231 area
[4 * i
+ 2] = geom
.width
;
232 area
[4 * i
+ 3] = geom
.height
;
235 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
236 xutil_screen_get(globalconf
.connection
, phys_screen
)->root
,
237 _NET_WORKAREA
, CARDINAL
, 32, tags
->len
* 4, area
);
241 ewmh_update_net_active_window(int phys_screen
)
245 if(globalconf
.screen_focus
->client_focus
246 && globalconf
.screen_focus
->client_focus
->phys_screen
== phys_screen
)
247 win
= globalconf
.screen_focus
->client_focus
->win
;
251 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
252 xutil_screen_get(globalconf
.connection
, phys_screen
)->root
,
253 _NET_ACTIVE_WINDOW
, WINDOW
, 32, 1, &win
);
257 ewmh_process_state_atom(client_t
*c
, xcb_atom_t state
, int set
)
259 if(state
== _NET_WM_STATE_STICKY
)
261 if(set
== _NET_WM_STATE_REMOVE
)
262 client_setsticky(c
, false);
263 else if(set
== _NET_WM_STATE_ADD
)
264 client_setsticky(c
, true);
265 else if(set
== _NET_WM_STATE_TOGGLE
)
266 client_setsticky(c
, !c
->issticky
);
268 else if(state
== _NET_WM_STATE_SKIP_TASKBAR
)
270 if(set
== _NET_WM_STATE_REMOVE
)
273 ewmh_client_update_hints(c
);
275 else if(set
== _NET_WM_STATE_ADD
)
278 ewmh_client_update_hints(c
);
280 else if(set
== _NET_WM_STATE_TOGGLE
)
282 c
->skiptb
= !c
->skiptb
;
283 ewmh_client_update_hints(c
);
286 else if(state
== _NET_WM_STATE_FULLSCREEN
)
288 if(set
== _NET_WM_STATE_REMOVE
)
289 client_setfullscreen(c
, false);
290 else if(set
== _NET_WM_STATE_ADD
)
291 client_setfullscreen(c
, true);
292 else if(set
== _NET_WM_STATE_TOGGLE
)
293 client_setfullscreen(c
, !c
->isfullscreen
);
295 else if(state
== _NET_WM_STATE_MAXIMIZED_HORZ
)
297 if(set
== _NET_WM_STATE_REMOVE
)
298 client_setmaxhoriz(c
, false);
299 else if(set
== _NET_WM_STATE_ADD
)
300 client_setmaxhoriz(c
, true);
301 else if(set
== _NET_WM_STATE_TOGGLE
)
302 client_setmaxhoriz(c
, !c
->ismaxhoriz
);
304 else if(state
== _NET_WM_STATE_MAXIMIZED_VERT
)
306 if(set
== _NET_WM_STATE_REMOVE
)
307 client_setmaxvert(c
, false);
308 else if(set
== _NET_WM_STATE_ADD
)
309 client_setmaxvert(c
, true);
310 else if(set
== _NET_WM_STATE_TOGGLE
)
311 client_setmaxvert(c
, !c
->ismaxvert
);
313 else if(state
== _NET_WM_STATE_ABOVE
)
315 if(set
== _NET_WM_STATE_REMOVE
)
316 client_setabove(c
, false);
317 else if(set
== _NET_WM_STATE_ADD
)
318 client_setabove(c
, true);
319 else if(set
== _NET_WM_STATE_TOGGLE
)
320 client_setabove(c
, !c
->isabove
);
322 else if(state
== _NET_WM_STATE_BELOW
)
324 if(set
== _NET_WM_STATE_REMOVE
)
325 client_setbelow(c
, false);
326 else if(set
== _NET_WM_STATE_ADD
)
327 client_setbelow(c
, true);
328 else if(set
== _NET_WM_STATE_TOGGLE
)
329 client_setbelow(c
, !c
->isbelow
);
331 else if(state
== _NET_WM_STATE_MODAL
)
333 if(set
== _NET_WM_STATE_REMOVE
)
334 client_setmodal(c
, false);
335 else if(set
== _NET_WM_STATE_ADD
)
336 client_setmodal(c
, true);
337 else if(set
== _NET_WM_STATE_TOGGLE
)
338 client_setmodal(c
, !c
->ismodal
);
340 else if(state
== _NET_WM_STATE_HIDDEN
)
342 if(set
== _NET_WM_STATE_REMOVE
)
343 client_setminimized(c
, false);
344 else if(set
== _NET_WM_STATE_ADD
)
345 client_setminimized(c
, true);
346 else if(set
== _NET_WM_STATE_TOGGLE
)
347 client_setminimized(c
, !c
->isminimized
);
349 else if(state
== _NET_WM_STATE_DEMANDS_ATTENTION
)
351 if(set
== _NET_WM_STATE_REMOVE
)
352 client_seturgent(c
, false);
353 else if(set
== _NET_WM_STATE_ADD
)
354 client_seturgent(c
, true);
355 else if(set
== _NET_WM_STATE_TOGGLE
)
356 client_seturgent(c
, !c
->isurgent
);
361 ewmh_process_client_message(xcb_client_message_event_t
*ev
)
366 if(ev
->type
== _NET_CURRENT_DESKTOP
)
368 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
371 if(ev
->window
== xutil_screen_get(globalconf
.connection
, screen
)->root
)
372 tag_view_only_byindex(&globalconf
.screens
.tab
[screen
], ev
->data
.data32
[0]);
374 else if(ev
->type
== _NET_CLOSE_WINDOW
)
376 if((c
= client_getbywin(ev
->window
)))
379 else if(ev
->type
== _NET_WM_DESKTOP
)
381 if((c
= client_getbywin(ev
->window
)))
383 tag_array_t
*tags
= &c
->screen
->tags
;
385 if(ev
->data
.data32
[0] == 0xffffffff)
388 for(int i
= 0; i
< tags
->len
; i
++)
389 if((int)ev
->data
.data32
[0] == i
)
391 tag_push(globalconf
.L
, tags
->tab
[i
]);
395 untag_client(c
, tags
->tab
[i
]);
398 else if(ev
->type
== _NET_WM_STATE
)
400 if((c
= client_getbywin(ev
->window
)))
402 ewmh_process_state_atom(c
, (xcb_atom_t
) ev
->data
.data32
[1], ev
->data
.data32
[0]);
403 if(ev
->data
.data32
[2])
404 ewmh_process_state_atom(c
, (xcb_atom_t
) ev
->data
.data32
[2],
408 else if(ev
->type
== _NET_ACTIVE_WINDOW
)
410 if((c
= client_getbywin(ev
->window
)))
417 /** Update client EWMH hints.
418 * \param c The client.
421 ewmh_client_update_hints(client_t
*c
)
423 xcb_atom_t state
[10]; /* number of defined state atoms */
427 state
[i
++] = _NET_WM_STATE_MODAL
;
429 state
[i
++] = _NET_WM_STATE_FULLSCREEN
;
431 state
[i
++] = _NET_WM_STATE_MAXIMIZED_VERT
;
433 state
[i
++] = _NET_WM_STATE_MAXIMIZED_HORZ
;
435 state
[i
++] = _NET_WM_STATE_STICKY
;
437 state
[i
++] = _NET_WM_STATE_SKIP_TASKBAR
;
439 state
[i
++] = _NET_WM_STATE_ABOVE
;
441 state
[i
++] = _NET_WM_STATE_BELOW
;
443 state
[i
++] = _NET_WM_STATE_HIDDEN
;
445 state
[i
++] = _NET_WM_STATE_DEMANDS_ATTENTION
;
447 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
448 c
->win
, _NET_WM_STATE
, ATOM
, 32, i
, state
);
451 /** Update the client active desktop.
452 * This is "wrong" since it can be on several tags, but EWMH has a strict view
453 * of desktop system so just take the first tag.
454 * \param c The client.
457 ewmh_client_update_desktop(client_t
*c
)
460 tag_array_t
*tags
= &c
->screen
->tags
;
462 for(i
= 0; i
< tags
->len
; i
++)
463 if(is_client_tagged(c
, tags
->tab
[i
]))
465 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
466 c
->win
, _NET_WM_DESKTOP
, CARDINAL
, 32, 1, &i
);
471 /** Update the client struts.
472 * \param c The client.
475 ewmh_update_client_strut(client_t
*c
)
483 c
->strut
.left_start_y
,
485 c
->strut
.right_start_y
,
486 c
->strut
.right_end_y
,
487 c
->strut
.top_start_x
,
489 c
->strut
.bottom_start_x
,
490 c
->strut
.bottom_end_x
493 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
494 c
->win
, _NET_WM_STRUT_PARTIAL
, CARDINAL
, 32, countof(state
), state
);
498 ewmh_client_check_hints(client_t
*c
)
503 xcb_get_property_cookie_t c0
, c1
, c2
;
504 xcb_get_property_reply_t
*reply
;
506 /* Send the GetProperty requests which will be processed later */
507 c0
= xcb_get_property_unchecked(globalconf
.connection
, false, c
->win
,
508 _NET_WM_DESKTOP
, XCB_GET_PROPERTY_TYPE_ANY
, 0, 1);
510 c1
= xcb_get_property_unchecked(globalconf
.connection
, false, c
->win
,
511 _NET_WM_STATE
, ATOM
, 0, UINT32_MAX
);
513 c2
= xcb_get_property_unchecked(globalconf
.connection
, false, c
->win
,
514 _NET_WM_WINDOW_TYPE
, ATOM
, 0, UINT32_MAX
);
516 reply
= xcb_get_property_reply(globalconf
.connection
, c0
, NULL
);
517 if(reply
&& reply
->value_len
&& (data
= xcb_get_property_value(reply
)))
519 tag_array_t
*tags
= &c
->screen
->tags
;
521 desktop
= *(uint32_t *) data
;
525 for(int i
= 0; i
< tags
->len
; i
++)
528 tag_push(globalconf
.L
, tags
->tab
[i
]);
532 untag_client(c
, tags
->tab
[i
]);
537 reply
= xcb_get_property_reply(globalconf
.connection
, c1
, NULL
);
538 if(reply
&& (data
= xcb_get_property_value(reply
)))
540 state
= (xcb_atom_t
*) data
;
541 for(int i
= 0; i
< xcb_get_property_value_length(reply
); i
++)
542 ewmh_process_state_atom(c
, state
[i
], _NET_WM_STATE_ADD
);
547 reply
= xcb_get_property_reply(globalconf
.connection
, c2
, NULL
);
548 if(reply
&& (data
= xcb_get_property_value(reply
)))
550 state
= (xcb_atom_t
*) data
;
551 for(int i
= 0; i
< xcb_get_property_value_length(reply
); i
++)
552 if(state
[i
] == _NET_WM_WINDOW_TYPE_DESKTOP
)
553 c
->type
= MAX(c
->type
, WINDOW_TYPE_DESKTOP
);
554 else if(state
[i
] == _NET_WM_WINDOW_TYPE_DIALOG
)
555 c
->type
= MAX(c
->type
, WINDOW_TYPE_DIALOG
);
556 else if(state
[i
] == _NET_WM_WINDOW_TYPE_SPLASH
)
557 c
->type
= MAX(c
->type
, WINDOW_TYPE_SPLASH
);
558 else if(state
[i
] == _NET_WM_WINDOW_TYPE_DOCK
)
559 c
->type
= MAX(c
->type
, WINDOW_TYPE_DOCK
);
560 else if(state
[i
] == _NET_WM_WINDOW_TYPE_MENU
)
561 c
->type
= MAX(c
->type
, WINDOW_TYPE_MENU
);
562 else if(state
[i
] == _NET_WM_WINDOW_TYPE_TOOLBAR
)
563 c
->type
= MAX(c
->type
, WINDOW_TYPE_TOOLBAR
);
564 else if(state
[i
] == _NET_WM_WINDOW_TYPE_UTILITY
)
565 c
->type
= MAX(c
->type
, WINDOW_TYPE_UTILITY
);
571 /** Process the WM strut of a client.
572 * \param c The client.
573 * \param strut_r (Optional) An existing reply.
576 ewmh_process_client_strut(client_t
*c
, xcb_get_property_reply_t
*strut_r
)
579 xcb_get_property_reply_t
*mstrut_r
= NULL
;
583 xcb_get_property_cookie_t strut_q
= xcb_get_property_unchecked(globalconf
.connection
, false, c
->win
,
584 _NET_WM_STRUT_PARTIAL
, CARDINAL
, 0, 12);
585 strut_r
= mstrut_r
= xcb_get_property_reply(globalconf
.connection
, strut_q
, NULL
);
589 && strut_r
->value_len
590 && (data
= xcb_get_property_value(strut_r
)))
592 uint32_t *strut
= data
;
594 if(c
->strut
.left
!= strut
[0]
595 || c
->strut
.right
!= strut
[1]
596 || c
->strut
.top
!= strut
[2]
597 || c
->strut
.bottom
!= strut
[3]
598 || c
->strut
.left_start_y
!= strut
[4]
599 || c
->strut
.left_end_y
!= strut
[5]
600 || c
->strut
.right_start_y
!= strut
[6]
601 || c
->strut
.right_end_y
!= strut
[7]
602 || c
->strut
.top_start_x
!= strut
[8]
603 || c
->strut
.top_end_x
!= strut
[9]
604 || c
->strut
.bottom_start_x
!= strut
[10]
605 || c
->strut
.bottom_end_x
!= strut
[11])
607 c
->strut
.left
= strut
[0];
608 c
->strut
.right
= strut
[1];
609 c
->strut
.top
= strut
[2];
610 c
->strut
.bottom
= strut
[3];
611 c
->strut
.left_start_y
= strut
[4];
612 c
->strut
.left_end_y
= strut
[5];
613 c
->strut
.right_start_y
= strut
[6];
614 c
->strut
.right_end_y
= strut
[7];
615 c
->strut
.top_start_x
= strut
[8];
616 c
->strut
.top_end_x
= strut
[9];
617 c
->strut
.bottom_start_x
= strut
[10];
618 c
->strut
.bottom_end_x
= strut
[11];
620 hook_property(client
, c
, "struts");
627 /** Send request to get NET_WM_ICON (EWMH)
628 * \param w The window.
629 * \return The cookie associated with the request.
631 xcb_get_property_cookie_t
632 ewmh_window_icon_get_unchecked(xcb_window_t w
)
634 return xcb_get_property_unchecked(globalconf
.connection
, false, w
,
635 _NET_WM_ICON
, CARDINAL
, 0, UINT32_MAX
);
639 ewmh_window_icon_from_reply(xcb_get_property_reply_t
*r
)
644 if(!r
|| r
->type
!= CARDINAL
|| r
->format
!= 32 || r
->length
< 2)
647 data
= (uint32_t *) xcb_get_property_value(r
);
651 /* Check that the property is as long as it should be, handling integer
652 * overflow. <uint32_t> times <another uint32_t casted to uint64_t> always
653 * fits into an uint64_t and thus this multiplication cannot overflow.
655 len
= data
[0] * (uint64_t) data
[1];
656 if (!data
[0] || !data
[1] || len
> r
->length
- 2)
659 return image_new_from_argb32(data
[0], data
[1], data
+ 2);
663 * \param cookie The cookie.
664 * \return The number of elements on stack.
667 ewmh_window_icon_get_reply(xcb_get_property_cookie_t cookie
)
669 xcb_get_property_reply_t
*r
= xcb_get_property_reply(globalconf
.connection
, cookie
, NULL
);
670 int ret
= ewmh_window_icon_from_reply(r
);
675 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80