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 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
)
345 if(ev
->type
== _NET_CURRENT_DESKTOP
)
347 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
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
)))
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)
367 for(int i
= 0; i
< tags
->len
; i
++)
368 if((int)ev
->data
.data32
[0] == i
)
369 tag_client(c
, tags
->tab
[i
]);
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],
388 /** Update client EWMH hints.
389 * \param c The client.
392 ewmh_client_update_hints(client_t
*c
)
394 xcb_atom_t state
[8]; /* number of defined state atoms */
398 state
[i
++] = _NET_WM_STATE_MODAL
;
400 state
[i
++] = _NET_WM_STATE_FULLSCREEN
;
402 state
[i
++] = _NET_WM_STATE_STICKY
;
404 state
[i
++] = _NET_WM_STATE_SKIP_TASKBAR
;
406 state
[i
++] = _NET_WM_STATE_ABOVE
;
408 state
[i
++] = _NET_WM_STATE_BELOW
;
410 state
[i
++] = _NET_WM_STATE_HIDDEN
;
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
);
419 ewmh_client_check_hints(client_t
*c
)
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
;
446 for(int i
= 0; i
< tags
->len
; i
++)
448 tag_client(c
, tags
->tab
[i
]);
450 untag_client(c
, tags
->tab
[i
]);
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
);
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
);
483 /** Update the WM strut of a client.
484 * \param c The client.
487 ewmh_client_strut_update(client_t
*c
, xcb_get_property_reply_t
*strut_r
)
490 xcb_get_property_reply_t
*mstrut_r
= NULL
;
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
);
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
);
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