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>
26 #include <xcb/xcb_atom.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
44 ewmh_init(int phys_screen
)
47 xcb_screen_t
*xscreen
= xutil_screen_get(globalconf
.connection
, phys_screen
);
51 _NET_SUPPORTING_WM_CHECK
,
53 _NET_CLIENT_LIST_STACKING
,
54 _NET_NUMBER_OF_DESKTOPS
,
61 _NET_WM_STRUT_PARTIAL
,
63 _NET_WM_VISIBLE_ICON_NAME
,
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
,
75 _NET_WM_STATE_SKIP_TASKBAR
,
76 _NET_WM_STATE_FULLSCREEN
,
81 _NET_WM_STATE_DEMANDS_ATTENTION
85 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
86 xscreen
->root
, _NET_SUPPORTED
, ATOM
, 32,
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,
99 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
100 father
, _NET_SUPPORTING_WM_CHECK
, WINDOW
, 32,
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 */
109 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
110 father
, _NET_WM_PID
, CARDINAL
, 32, 1, &i
);
114 ewmh_update_net_client_list(int phys_screen
)
120 for(c
= globalconf
.clients
; c
; c
= c
->next
)
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
)
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.
138 ewmh_update_net_client_list_stacking(int phys_screen
)
144 for(c
= globalconf
.stack
; c
; c
= c
->next
)
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
);
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
);
169 ewmh_update_net_current_desktop(int phys_screen
)
171 tag_array_t
*tags
= &globalconf
.screens
[phys_screen
].tags
;
173 tag_t
**curtags
= tags_get_current(phys_screen
);
175 while(count
< (uint32_t) tags
->len
&& tags
->tab
[count
] != curtags
[0])
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
);
186 ewmh_update_net_desktop_names(int phys_screen
)
188 tag_array_t
*tags
= &globalconf
.screens
[phys_screen
].tags
;
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
);
205 /** Update the work area space for each physical screen and each desktop.
206 * \param phys_screen The physical screen id.
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
,
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
);
233 ewmh_update_net_active_window(int phys_screen
)
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
;
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
);
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
)
265 ewmh_client_update_hints(c
);
267 else if(set
== _NET_WM_STATE_ADD
)
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
)
327 else if(set
== _NET_WM_STATE_ADD
)
329 else if(set
== _NET_WM_STATE_TOGGLE
)
330 c
->isurgent
= !c
->isurgent
;
333 hooks_property(c
, "urgent");
338 ewmh_process_client_message(xcb_client_message_event_t
*ev
)
343 if(ev
->type
== _NET_CURRENT_DESKTOP
)
345 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
348 if(ev
->window
== xutil_screen_get(globalconf
.connection
, screen
)->root
)
349 tag_view_only_byindex(screen
, ev
->data
.data32
[0]);
351 else if(ev
->type
== _NET_CLOSE_WINDOW
)
353 if((c
= client_getbywin(ev
->window
)))
356 else if(ev
->type
== _NET_WM_DESKTOP
)
358 if((c
= client_getbywin(ev
->window
)))
360 tag_array_t
*tags
= &globalconf
.screens
[c
->screen
].tags
;
362 if(ev
->data
.data32
[0] == 0xffffffff)
365 for(int i
= 0; i
< tags
->len
; i
++)
366 if((int)ev
->data
.data32
[0] == i
)
367 tag_client(c
, tags
->tab
[i
]);
369 untag_client(c
, tags
->tab
[i
]);
372 else if(ev
->type
== _NET_WM_STATE
)
374 if((c
= client_getbywin(ev
->window
)))
376 ewmh_process_state_atom(c
, (xcb_atom_t
) ev
->data
.data32
[1], ev
->data
.data32
[0]);
377 if(ev
->data
.data32
[2])
378 ewmh_process_state_atom(c
, (xcb_atom_t
) ev
->data
.data32
[2],
386 /** Update client EWMH hints.
387 * \param c The client.
390 ewmh_client_update_hints(client_t
*c
)
392 xcb_atom_t state
[8]; /* number of defined state atoms */
396 state
[i
++] = _NET_WM_STATE_MODAL
;
398 state
[i
++] = _NET_WM_STATE_FULLSCREEN
;
400 state
[i
++] = _NET_WM_STATE_STICKY
;
402 state
[i
++] = _NET_WM_STATE_SKIP_TASKBAR
;
404 state
[i
++] = _NET_WM_STATE_ABOVE
;
406 state
[i
++] = _NET_WM_STATE_BELOW
;
408 state
[i
++] = _NET_WM_STATE_HIDDEN
;
410 state
[i
++] = _NET_WM_STATE_DEMANDS_ATTENTION
;
412 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
413 c
->win
, _NET_WM_STATE
, ATOM
, 32, i
, state
);
417 ewmh_client_check_hints(client_t
*c
)
422 xcb_get_property_cookie_t c0
, c1
, c2
;
423 xcb_get_property_reply_t
*reply
;
425 /* Send the GetProperty requests which will be processed later */
426 c0
= xcb_get_property_unchecked(globalconf
.connection
, false, c
->win
,
427 _NET_WM_DESKTOP
, XCB_GET_PROPERTY_TYPE_ANY
, 0, 1);
429 c1
= xcb_get_property_unchecked(globalconf
.connection
, false, c
->win
,
430 _NET_WM_STATE
, ATOM
, 0, UINT32_MAX
);
432 c2
= xcb_get_property_unchecked(globalconf
.connection
, false, c
->win
,
433 _NET_WM_WINDOW_TYPE
, ATOM
, 0, UINT32_MAX
);
435 reply
= xcb_get_property_reply(globalconf
.connection
, c0
, NULL
);
436 if(reply
&& reply
->value_len
&& (data
= xcb_get_property_value(reply
)))
438 tag_array_t
*tags
= &globalconf
.screens
[c
->screen
].tags
;
440 desktop
= *(uint32_t *) data
;
444 for(int i
= 0; i
< tags
->len
; i
++)
446 tag_client(c
, tags
->tab
[i
]);
448 untag_client(c
, tags
->tab
[i
]);
453 reply
= xcb_get_property_reply(globalconf
.connection
, c1
, NULL
);
454 if(reply
&& (data
= xcb_get_property_value(reply
)))
456 state
= (xcb_atom_t
*) data
;
457 for(int i
= 0; i
< xcb_get_property_value_length(reply
); i
++)
458 ewmh_process_state_atom(c
, state
[i
], _NET_WM_STATE_ADD
);
463 reply
= xcb_get_property_reply(globalconf
.connection
, c2
, NULL
);
464 if(reply
&& (data
= xcb_get_property_value(reply
)))
466 state
= (xcb_atom_t
*) data
;
467 for(int i
= 0; i
< xcb_get_property_value_length(reply
); i
++)
468 if(state
[i
] == _NET_WM_WINDOW_TYPE_DESKTOP
)
469 c
->type
= MAX(c
->type
, WINDOW_TYPE_DESKTOP
);
470 else if(state
[i
] == _NET_WM_WINDOW_TYPE_DIALOG
)
471 c
->type
= MAX(c
->type
, WINDOW_TYPE_DIALOG
);
472 else if(state
[i
] == _NET_WM_WINDOW_TYPE_SPLASH
)
473 c
->type
= MAX(c
->type
, WINDOW_TYPE_SPLASH
);
474 else if(state
[i
] == _NET_WM_WINDOW_TYPE_DOCK
)
475 c
->type
= MAX(c
->type
, WINDOW_TYPE_DOCK
);
481 /** Update the WM strut of a client.
482 * \param c The client.
485 ewmh_client_strut_update(client_t
*c
, xcb_get_property_reply_t
*strut_r
)
488 xcb_get_property_reply_t
*mstrut_r
= NULL
;
492 xcb_get_property_cookie_t strut_q
= xcb_get_property_unchecked(globalconf
.connection
, false, c
->win
,
493 _NET_WM_STRUT_PARTIAL
, CARDINAL
, 0, 12);
494 strut_r
= mstrut_r
= xcb_get_property_reply(globalconf
.connection
, strut_q
, NULL
);
498 && strut_r
->value_len
499 && (data
= xcb_get_property_value(strut_r
)))
501 uint32_t *strut
= data
;
503 if(c
->strut
.left
!= strut
[0]
504 || c
->strut
.right
!= strut
[1]
505 || c
->strut
.top
!= strut
[2]
506 || c
->strut
.bottom
!= strut
[3]
507 || c
->strut
.left_start_y
!= strut
[4]
508 || c
->strut
.left_end_y
!= strut
[5]
509 || c
->strut
.right_start_y
!= strut
[6]
510 || c
->strut
.right_end_y
!= strut
[7]
511 || c
->strut
.top_start_x
!= strut
[8]
512 || c
->strut
.top_end_x
!= strut
[9]
513 || c
->strut
.bottom_start_x
!= strut
[10]
514 || c
->strut
.bottom_end_x
!= strut
[11])
516 c
->strut
.left
= strut
[0];
517 c
->strut
.right
= strut
[1];
518 c
->strut
.top
= strut
[2];
519 c
->strut
.bottom
= strut
[3];
520 c
->strut
.left_start_y
= strut
[4];
521 c
->strut
.left_end_y
= strut
[5];
522 c
->strut
.right_start_y
= strut
[6];
523 c
->strut
.right_end_y
= strut
[7];
524 c
->strut
.top_start_x
= strut
[8];
525 c
->strut
.top_end_x
= strut
[9];
526 c
->strut
.bottom_start_x
= strut
[10];
527 c
->strut
.bottom_end_x
= strut
[11];
529 client_need_arrange(c
);
530 /* All the wiboxes (may) need to be repositioned */
531 for(int screen
= 0; screen
< globalconf
.nscreen
; screen
++)
532 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
534 wibox_t
*s
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
535 wibox_position_update(s
);
543 /** Send request to get NET_WM_ICON (EWMH)
544 * \param w The window.
545 * \return The cookie associated with the request.
547 xcb_get_property_cookie_t
548 ewmh_window_icon_get_unchecked(xcb_window_t w
)
550 return xcb_get_property_unchecked(globalconf
.connection
, false, w
,
551 _NET_WM_ICON
, CARDINAL
, 0, UINT32_MAX
);
555 ewmh_window_icon_from_reply(xcb_get_property_reply_t
*r
)
559 if(!r
|| r
->type
!= CARDINAL
|| r
->format
!= 32 || r
->length
< 2 ||
560 !(data
= (uint32_t *) xcb_get_property_value(r
)))
563 if(data
[0] && data
[1])
564 return image_new_from_argb32(data
[0], data
[1], data
+ 2);
570 * \param cookie The cookie.
571 * \return A draw_image_t structure which must be deleted after usage.
574 ewmh_window_icon_get_reply(xcb_get_property_cookie_t cookie
)
576 xcb_get_property_reply_t
*r
= xcb_get_property_reply(globalconf
.connection
, cookie
, NULL
);
577 image_t
*icon
= ewmh_window_icon_from_reply(r
);
582 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80