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;
15 * Updates _NET_CURRENT_DESKTOP with the current desktop number.
17 * EWMH: The index of the current desktop. This is always an integer between 0
18 * and _NET_NUMBER_OF_DESKTOPS - 1.
21 void ewmh_update_current_desktop(void) {
22 const uint32_t idx = ewmh_get_workspace_index(focused);
23 if (idx != NET_WM_DESKTOP_NONE) {
24 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_CURRENT_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &idx);
29 * Updates _NET_NUMBER_OF_DESKTOPS which we interpret as the number of
30 * noninternal workspaces.
32 void ewmh_update_number_of_desktops(void) {
36 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
38 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
39 if (STARTS_WITH(ws->name, "__"))
45 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
46 A__NET_NUMBER_OF_DESKTOPS, XCB_ATOM_CARDINAL, 32, 1, &idx);
50 * Updates _NET_DESKTOP_NAMES: "The names of all virtual desktops. This is a
51 * list of NULL-terminated strings in UTF-8 encoding"
53 void ewmh_update_desktop_names(void) {
57 /* count the size of the property message to set */
58 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
60 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
61 if (STARTS_WITH(ws->name, "__"))
63 msg_length += strlen(ws->name) + 1;
67 char desktop_names[msg_length];
68 int current_position = 0;
70 /* fill the buffer with the names of the i3 workspaces */
71 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
73 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
74 if (STARTS_WITH(ws->name, "__"))
77 for (size_t i = 0; i < strlen(ws->name) + 1; i++) {
78 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 */
95 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
97 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
98 if (STARTS_WITH(ws->name, "__"))
105 uint32_t viewports[num_desktops * 2];
107 int current_position = 0;
108 /* fill the viewport buffer */
109 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
111 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
112 if (STARTS_WITH(ws->name, "__"))
115 viewports[current_position++] = output->rect.x;
116 viewports[current_position++] = output->rect.y;
120 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
121 A__NET_DESKTOP_VIEWPORT, XCB_ATOM_CARDINAL, 32, current_position, &viewports);
124 static void ewmh_update_wm_desktop_recursively(Con *con, const uint32_t desktop) {
127 /* Recursively call this to descend through the entire subtree. */
128 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
129 ewmh_update_wm_desktop_recursively(child, desktop);
132 /* If con is a workspace, we also need to go through the floating windows on it. */
133 if (con->type == CT_WORKSPACE) {
134 TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
135 ewmh_update_wm_desktop_recursively(child, desktop);
139 if (!con_has_managed_window(con))
142 uint32_t wm_desktop = desktop;
143 /* Sticky windows are only actually sticky when they are floating or inside
144 * a floating container. This is technically still slightly wrong, since
145 * sticky windows will only be on all workspaces on this output, but we
146 * ignore multi-monitor situations for this since the spec isn't too
147 * precise on this anyway. */
148 if (con_is_sticky(con) && con_is_floating(con)) {
149 wm_desktop = NET_WM_DESKTOP_ALL;
152 /* If the window is on the scratchpad we assign the sticky value to it
153 * since showing it works on any workspace. We cannot remove the property
154 * as per specification. */
155 Con *ws = con_get_workspace(con);
156 if (ws != NULL && con_is_internal(ws)) {
157 wm_desktop = NET_WM_DESKTOP_ALL;
160 /* If this is the cached value, we don't need to do anything. */
161 if (con->window->wm_desktop == wm_desktop)
163 con->window->wm_desktop = wm_desktop;
165 const xcb_window_t window = con->window->id;
166 if (wm_desktop != NET_WM_DESKTOP_NONE) {
167 DLOG("Setting _NET_WM_DESKTOP = %d for window 0x%08x.\n", wm_desktop, window);
168 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, window, A__NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &wm_desktop);
170 /* If we can't determine the workspace index, delete the property. We'd
171 * rather not set it than lie. */
172 ELOG("Failed to determine the proper EWMH desktop index for window 0x%08x, deleting _NET_WM_DESKTOP.\n", window);
173 xcb_delete_property(conn, window, A__NET_WM_DESKTOP);
178 * Updates _NET_WM_DESKTOP for all windows.
179 * A request will only be made if the cached value differs from the calculated value.
182 void ewmh_update_wm_desktop(void) {
183 uint32_t desktop = 0;
186 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
188 TAILQ_FOREACH(workspace, &(output_get_content(output)->nodes_head), nodes) {
189 ewmh_update_wm_desktop_recursively(workspace, desktop);
191 if (!con_is_internal(workspace)) {
199 * Updates _NET_ACTIVE_WINDOW with the currently focused window.
201 * EWMH: The window ID of the currently active window or None if no window has
205 void ewmh_update_active_window(xcb_window_t window) {
206 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
207 A__NET_ACTIVE_WINDOW, XCB_ATOM_WINDOW, 32, 1, &window);
211 * Updates _NET_WM_VISIBLE_NAME.
214 void ewmh_update_visible_name(xcb_window_t window, const char *name) {
216 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, window, A__NET_WM_VISIBLE_NAME, A_UTF8_STRING, 8, strlen(name), name);
218 xcb_delete_property(conn, window, A__NET_WM_VISIBLE_NAME);
223 * i3 currently does not support _NET_WORKAREA, because it does not correspond
224 * to i3’s concept of workspaces. See also:
225 * https://bugs.i3wm.org/539
226 * https://bugs.i3wm.org/301
227 * https://bugs.i3wm.org/1038
229 * We need to actively delete this property because some display managers (e.g.
232 * EWMH: Contains a geometry for each desktop. These geometries specify an area
233 * that is completely contained within the viewport. Work area SHOULD be used by
234 * desktop applications to place desktop icons appropriately.
237 void ewmh_update_workarea(void) {
238 xcb_delete_property(conn, root, A__NET_WORKAREA);
242 * Updates the _NET_CLIENT_LIST hint.
245 void ewmh_update_client_list(xcb_window_t *list, int num_windows) {
248 XCB_PROP_MODE_REPLACE,
258 * Updates the _NET_CLIENT_LIST_STACKING hint.
261 void ewmh_update_client_list_stacking(xcb_window_t *stack, int num_windows) {
264 XCB_PROP_MODE_REPLACE,
266 A__NET_CLIENT_LIST_STACKING,
274 * Set or remove _NET_WM_STATE_STICKY on the window.
277 void ewmh_update_sticky(xcb_window_t window, bool sticky) {
279 DLOG("Setting _NET_WM_STATE_STICKY for window = %d.\n", window);
280 xcb_add_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_STICKY);
282 DLOG("Removing _NET_WM_STATE_STICKY for window = %d.\n", window);
283 xcb_remove_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_STICKY);
288 * Set or remove _NEW_WM_STATE_FOCUSED on the window.
291 void ewmh_update_focused(xcb_window_t window, bool is_focused) {
293 DLOG("Setting _NET_WM_STATE_FOCUSED for window = %d.\n", window);
294 xcb_add_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_FOCUSED);
296 DLOG("Removing _NET_WM_STATE_FOCUSED for window = %d.\n", window);
297 xcb_remove_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_FOCUSED);
302 * Set up the EWMH hints on the root window.
305 void ewmh_setup_hints(void) {
306 xcb_atom_t supported_atoms[] = {
307 #define xmacro(atom) A_##atom,
308 #include "atoms_NET_SUPPORTED.xmacro"
312 /* Set up the window manager’s name. According to EWMH, section "Root Window
313 * Properties", to indicate that an EWMH-compliant window manager is
314 * present, a child window has to be created (and kept alive as long as the
315 * window manager is running) which has the _NET_SUPPORTING_WM_CHECK and
317 ewmh_window = xcb_generate_id(conn);
318 /* We create the window and put it at (-1, -1) so that it is off-screen. */
321 XCB_COPY_FROM_PARENT, /* depth */
322 ewmh_window, /* window id */
324 -1, -1, 1, 1, /* dimensions (x, y, w, h) */
326 XCB_WINDOW_CLASS_INPUT_ONLY, /* window class */
327 XCB_COPY_FROM_PARENT, /* visual */
328 XCB_CW_OVERRIDE_REDIRECT,
330 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, ewmh_window, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &ewmh_window);
331 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, ewmh_window, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
332 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &ewmh_window);
334 /* I’m not entirely sure if we need to keep _NET_WM_NAME on root. */
335 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
337 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);
339 /* 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. */
340 xcb_map_window(conn, ewmh_window);
341 xcb_configure_window(conn, ewmh_window, XCB_CONFIG_WINDOW_STACK_MODE, (uint32_t[]){XCB_STACK_MODE_BELOW});
345 * Returns the workspace container as enumerated by the EWMH desktop model.
346 * Returns NULL if no workspace could be found for the index.
348 * This is the reverse of ewmh_get_workspace_index.
351 Con *ewmh_get_workspace_by_index(uint32_t idx) {
352 if (idx == NET_WM_DESKTOP_NONE)
355 uint32_t current_index = 0;
358 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
360 TAILQ_FOREACH(workspace, &(output_get_content(output)->nodes_head), nodes) {
361 if (con_is_internal(workspace))
364 if (current_index == idx)
375 * Returns the EWMH desktop index for the workspace the given container is on.
376 * Returns NET_WM_DESKTOP_NONE if the desktop index cannot be determined.
378 * This is the reverse of ewmh_get_workspace_by_index.
381 uint32_t ewmh_get_workspace_index(Con *con) {
384 Con *workspace = con_get_workspace(con);
386 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
388 TAILQ_FOREACH(current, &(output_get_content(output)->nodes_head), nodes) {
389 if (con_is_internal(current))
392 if (current == workspace)
399 return NET_WM_DESKTOP_NONE;