2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * ewmh.c: Get/set certain EWMH properties easily.
12 xcb_window_t ewmh_window;
14 #define FOREACH_NONINTERNAL \
15 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) \
16 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) \
17 if (!con_is_internal(ws))
20 * Updates _NET_CURRENT_DESKTOP with the current desktop number.
22 * EWMH: The index of the current desktop. This is always an integer between 0
23 * and _NET_NUMBER_OF_DESKTOPS - 1.
26 void ewmh_update_current_desktop(void) {
27 static uint32_t old_idx = NET_WM_DESKTOP_NONE;
28 const uint32_t idx = ewmh_get_workspace_index(focused);
30 if (idx == old_idx || idx == NET_WM_DESKTOP_NONE) {
35 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_CURRENT_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &idx);
39 * Updates _NET_NUMBER_OF_DESKTOPS which we interpret as the number of
40 * noninternal workspaces.
42 void ewmh_update_number_of_desktops(void) {
44 static uint32_t old_idx = 0;
56 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
57 A__NET_NUMBER_OF_DESKTOPS, XCB_ATOM_CARDINAL, 32, 1, &idx);
61 * Updates _NET_DESKTOP_NAMES: "The names of all virtual desktops. This is a
62 * list of NULL-terminated strings in UTF-8 encoding"
64 void ewmh_update_desktop_names(void) {
68 /* count the size of the property message to set */
70 msg_length += strlen(ws->name) + 1;
73 char desktop_names[msg_length];
74 int current_position = 0;
76 /* fill the buffer with the names of the i3 workspaces */
78 for (size_t i = 0; i < strlen(ws->name) + 1; i++) {
79 desktop_names[current_position++] = ws->name[i];
83 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
84 A__NET_DESKTOP_NAMES, A_UTF8_STRING, 8, msg_length, desktop_names);
88 * Updates _NET_DESKTOP_VIEWPORT, which is an array of pairs of cardinals that
89 * define the top left corner of each desktop's viewport.
91 void ewmh_update_desktop_viewport(void) {
94 /* count number of desktops */
99 uint32_t viewports[num_desktops * 2];
101 int current_position = 0;
102 /* fill the viewport buffer */
103 FOREACH_NONINTERNAL {
104 viewports[current_position++] = output->rect.x;
105 viewports[current_position++] = output->rect.y;
108 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
109 A__NET_DESKTOP_VIEWPORT, XCB_ATOM_CARDINAL, 32, current_position, &viewports);
112 static void ewmh_update_wm_desktop_recursively(Con *con, const uint32_t desktop) {
115 /* Recursively call this to descend through the entire subtree. */
116 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
117 ewmh_update_wm_desktop_recursively(child, desktop);
120 /* If con is a workspace, we also need to go through the floating windows on it. */
121 if (con->type == CT_WORKSPACE) {
122 TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
123 ewmh_update_wm_desktop_recursively(child, desktop);
127 if (!con_has_managed_window(con))
130 uint32_t wm_desktop = desktop;
131 /* Sticky windows are only actually sticky when they are floating or inside
132 * a floating container. This is technically still slightly wrong, since
133 * sticky windows will only be on all workspaces on this output, but we
134 * ignore multi-monitor situations for this since the spec isn't too
135 * precise on this anyway. */
136 if (con_is_sticky(con) && con_is_floating(con)) {
137 wm_desktop = NET_WM_DESKTOP_ALL;
140 /* If the window is on the scratchpad we assign the sticky value to it
141 * since showing it works on any workspace. We cannot remove the property
142 * as per specification. */
143 Con *ws = con_get_workspace(con);
144 if (ws != NULL && con_is_internal(ws)) {
145 wm_desktop = NET_WM_DESKTOP_ALL;
148 /* If this is the cached value, we don't need to do anything. */
149 if (con->window->wm_desktop == wm_desktop)
151 con->window->wm_desktop = wm_desktop;
153 const xcb_window_t window = con->window->id;
154 if (wm_desktop != NET_WM_DESKTOP_NONE) {
155 DLOG("Setting _NET_WM_DESKTOP = %d for window 0x%08x.\n", wm_desktop, window);
156 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, window, A__NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &wm_desktop);
158 /* If we can't determine the workspace index, delete the property. We'd
159 * rather not set it than lie. */
160 ELOG("Failed to determine the proper EWMH desktop index for window 0x%08x, deleting _NET_WM_DESKTOP.\n", window);
161 xcb_delete_property(conn, window, A__NET_WM_DESKTOP);
166 * Updates _NET_WM_DESKTOP for all windows.
167 * A request will only be made if the cached value differs from the calculated value.
170 void ewmh_update_wm_desktop(void) {
171 uint32_t desktop = 0;
174 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
176 TAILQ_FOREACH(workspace, &(output_get_content(output)->nodes_head), nodes) {
177 ewmh_update_wm_desktop_recursively(workspace, desktop);
179 if (!con_is_internal(workspace)) {
187 * Updates _NET_ACTIVE_WINDOW with the currently focused window.
189 * EWMH: The window ID of the currently active window or None if no window has
193 void ewmh_update_active_window(xcb_window_t window) {
194 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
195 A__NET_ACTIVE_WINDOW, XCB_ATOM_WINDOW, 32, 1, &window);
199 * Updates _NET_WM_VISIBLE_NAME.
202 void ewmh_update_visible_name(xcb_window_t window, const char *name) {
204 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, window, A__NET_WM_VISIBLE_NAME, A_UTF8_STRING, 8, strlen(name), name);
206 xcb_delete_property(conn, window, A__NET_WM_VISIBLE_NAME);
211 * i3 currently does not support _NET_WORKAREA, because it does not correspond
212 * to i3’s concept of workspaces. See also:
213 * https://bugs.i3wm.org/539
214 * https://bugs.i3wm.org/301
215 * https://bugs.i3wm.org/1038
217 * We need to actively delete this property because some display managers (e.g.
220 * EWMH: Contains a geometry for each desktop. These geometries specify an area
221 * that is completely contained within the viewport. Work area SHOULD be used by
222 * desktop applications to place desktop icons appropriately.
225 void ewmh_update_workarea(void) {
226 xcb_delete_property(conn, root, A__NET_WORKAREA);
230 * Updates the _NET_CLIENT_LIST hint.
233 void ewmh_update_client_list(xcb_window_t *list, int num_windows) {
236 XCB_PROP_MODE_REPLACE,
246 * Updates the _NET_CLIENT_LIST_STACKING hint.
249 void ewmh_update_client_list_stacking(xcb_window_t *stack, int num_windows) {
252 XCB_PROP_MODE_REPLACE,
254 A__NET_CLIENT_LIST_STACKING,
262 * Set or remove _NET_WM_STATE_STICKY on the window.
265 void ewmh_update_sticky(xcb_window_t window, bool sticky) {
267 DLOG("Setting _NET_WM_STATE_STICKY for window = %d.\n", window);
268 xcb_add_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_STICKY);
270 DLOG("Removing _NET_WM_STATE_STICKY for window = %d.\n", window);
271 xcb_remove_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_STICKY);
276 * Set or remove _NEW_WM_STATE_FOCUSED on the window.
279 void ewmh_update_focused(xcb_window_t window, bool is_focused) {
281 DLOG("Setting _NET_WM_STATE_FOCUSED for window = %d.\n", window);
282 xcb_add_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_FOCUSED);
284 DLOG("Removing _NET_WM_STATE_FOCUSED for window = %d.\n", window);
285 xcb_remove_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_FOCUSED);
290 * Set up the EWMH hints on the root window.
293 void ewmh_setup_hints(void) {
294 xcb_atom_t supported_atoms[] = {
295 #define xmacro(atom) A_##atom,
296 #include "atoms_NET_SUPPORTED.xmacro"
300 /* Set up the window manager’s name. According to EWMH, section "Root Window
301 * Properties", to indicate that an EWMH-compliant window manager is
302 * present, a child window has to be created (and kept alive as long as the
303 * window manager is running) which has the _NET_SUPPORTING_WM_CHECK and
305 ewmh_window = xcb_generate_id(conn);
306 /* We create the window and put it at (-1, -1) so that it is off-screen. */
309 XCB_COPY_FROM_PARENT, /* depth */
310 ewmh_window, /* window id */
312 -1, -1, 1, 1, /* dimensions (x, y, w, h) */
314 XCB_WINDOW_CLASS_INPUT_ONLY, /* window class */
315 XCB_COPY_FROM_PARENT, /* visual */
316 XCB_CW_OVERRIDE_REDIRECT,
318 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, ewmh_window, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &ewmh_window);
319 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, ewmh_window, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
320 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &ewmh_window);
322 /* I’m not entirely sure if we need to keep _NET_WM_NAME on root. */
323 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
325 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTED, XCB_ATOM_ATOM, 32, /* number of atoms */ sizeof(supported_atoms) / sizeof(xcb_atom_t), supported_atoms);
327 /* We need to map this window to be able to set the input focus to it if no other window is available to be focused. */
328 xcb_map_window(conn, ewmh_window);
329 xcb_configure_window(conn, ewmh_window, XCB_CONFIG_WINDOW_STACK_MODE, (uint32_t[]){XCB_STACK_MODE_BELOW});
333 * Returns the workspace container as enumerated by the EWMH desktop model.
334 * Returns NULL if no workspace could be found for the index.
336 * This is the reverse of ewmh_get_workspace_index.
339 Con *ewmh_get_workspace_by_index(uint32_t idx) {
340 if (idx == NET_WM_DESKTOP_NONE)
343 uint32_t current_index = 0;
346 FOREACH_NONINTERNAL {
347 if (current_index == idx) {
357 * Returns the EWMH desktop index for the workspace the given container is on.
358 * Returns NET_WM_DESKTOP_NONE if the desktop index cannot be determined.
360 * This is the reverse of ewmh_get_workspace_by_index.
363 uint32_t ewmh_get_workspace_index(Con *con) {
366 Con *target_workspace = con_get_workspace(con);
368 FOREACH_NONINTERNAL {
369 if (ws == target_workspace) {
376 return NET_WM_DESKTOP_NONE;