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 "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
45 ewmh_init(int phys_screen
)
48 xcb_screen_t
*xscreen
= xutil_screen_get(globalconf
.connection
, phys_screen
);
52 _NET_SUPPORTING_WM_CHECK
,
54 _NET_CLIENT_LIST_STACKING
,
55 _NET_NUMBER_OF_DESKTOPS
,
62 _NET_WM_STRUT_PARTIAL
,
64 _NET_WM_VISIBLE_ICON_NAME
,
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
,
76 _NET_WM_STATE_SKIP_TASKBAR
,
77 _NET_WM_STATE_FULLSCREEN
,
82 _NET_WM_STATE_DEMANDS_ATTENTION
86 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
87 xscreen
->root
, _NET_SUPPORTED
, ATOM
, 32,
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,
100 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
101 father
, _NET_SUPPORTING_WM_CHECK
, WINDOW
, 32,
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 */
110 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
,
111 father
, _NET_WM_PID
, CARDINAL
, 32, 1, &i
);
115 ewmh_update_net_client_list(int phys_screen
)
121 for(c
= globalconf
.clients
; c
; c
= c
->next
)
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
)
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.
139 ewmh_update_net_client_list_stacking(int phys_screen
)
145 for(c
= globalconf
.stack
; c
; c
= c
->next
)
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
);
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
);
170 ewmh_update_net_current_desktop(int phys_screen
)
172 tag_array_t
*tags
= &globalconf
.screens
[phys_screen
].tags
;
174 tag_t
**curtags
= tags_get_current(phys_screen
);
176 while(count
< (uint32_t) tags
->len
&& tags
->tab
[count
] != curtags
[0])
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
);
187 ewmh_update_net_desktop_names(int phys_screen
)
189 tag_array_t
*tags
= &globalconf
.screens
[phys_screen
].tags
;
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
);
206 /** Update the work area space for each physical screen and each desktop.
207 * \param phys_screen The physical screen id.
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
,
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
);
234 ewmh_update_net_active_window(int phys_screen
)
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
;
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
);
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
)
266 ewmh_client_update_hints(c
);
268 else if(set
== _NET_WM_STATE_ADD
)
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
)
328 else if(set
== _NET_WM_STATE_ADD
)
330 else if(set
== _NET_WM_STATE_TOGGLE
)
331 c
->isurgent
= !c
->isurgent
;
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
)
346 if(ev
->type
== _NET_CURRENT_DESKTOP
)
348 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
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
)))
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)
368 for(int i
= 0; i
< tags
->len
; i
++)
369 if((int)ev
->data
.data32
[0] == i
)
370 tag_client(c
, tags
->tab
[i
]);
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],
389 /** Update client EWMH hints.
390 * \param c The client.
393 ewmh_client_update_hints(client_t
*c
)
395 xcb_atom_t state
[8]; /* number of defined state atoms */
399 state
[i
++] = _NET_WM_STATE_MODAL
;
401 state
[i
++] = _NET_WM_STATE_FULLSCREEN
;
403 state
[i
++] = _NET_WM_STATE_STICKY
;
405 state
[i
++] = _NET_WM_STATE_SKIP_TASKBAR
;
407 state
[i
++] = _NET_WM_STATE_ABOVE
;
409 state
[i
++] = _NET_WM_STATE_BELOW
;
411 state
[i
++] = _NET_WM_STATE_HIDDEN
;
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
);
420 ewmh_client_check_hints(client_t
*c
)
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
;
447 for(int i
= 0; i
< tags
->len
; i
++)
449 tag_client(c
, tags
->tab
[i
]);
451 untag_client(c
, tags
->tab
[i
]);
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
);
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
;
480 c
->type
= WINDOW_TYPE_NORMAL
;
486 /** Update the WM strut of a client.
487 * \param c The client.
490 ewmh_client_strut_update(client_t
*c
, xcb_get_property_reply_t
*strut_r
)
493 xcb_get_property_reply_t
*mstrut_r
= NULL
;
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
);
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
);
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
);
557 ewmh_window_icon_from_reply(xcb_get_property_reply_t
*r
)
561 if(!r
|| r
->type
!= CARDINAL
|| r
->format
!= 32 || r
->length
< 2 ||
562 !(data
= (uint32_t *) xcb_get_property_value(r
)))
565 if(data
[0] && data
[1])
566 return image_new_from_argb32(data
[0], data
[1], data
+ 2);
572 * \param cookie The cookie.
573 * \return A draw_image_t structure which must be deleted after usage.
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
);
584 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80