]> git.sur5r.net Git - i3/i3/blob - src/ewmh.c
Added new criteria 'tiling' / 'floating'. (#2481)
[i3/i3] / src / ewmh.c
1 #undef I3__FILE__
2 #define I3__FILE__ "ewmh.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * ewmh.c: Get/set certain EWMH properties easily.
10  *
11  */
12 #include "all.h"
13
14 xcb_window_t ewmh_window;
15
16 /*
17  * Updates _NET_CURRENT_DESKTOP with the current desktop number.
18  *
19  * EWMH: The index of the current desktop. This is always an integer between 0
20  * and _NET_NUMBER_OF_DESKTOPS - 1.
21  *
22  */
23 void ewmh_update_current_desktop(void) {
24     const uint32_t idx = ewmh_get_workspace_index(focused);
25     if (idx != NET_WM_DESKTOP_NONE) {
26         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_CURRENT_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &idx);
27     }
28 }
29
30 /*
31  * Updates _NET_NUMBER_OF_DESKTOPS which we interpret as the number of
32  * noninternal workspaces.
33  */
34 void ewmh_update_number_of_desktops(void) {
35     Con *output;
36     uint32_t idx = 0;
37
38     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
39         Con *ws;
40         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
41             if (STARTS_WITH(ws->name, "__"))
42                 continue;
43             ++idx;
44         }
45     }
46
47     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
48                         A__NET_NUMBER_OF_DESKTOPS, XCB_ATOM_CARDINAL, 32, 1, &idx);
49 }
50
51 /*
52  * Updates _NET_DESKTOP_NAMES: "The names of all virtual desktops. This is a
53  * list of NULL-terminated strings in UTF-8 encoding"
54  */
55 void ewmh_update_desktop_names(void) {
56     Con *output;
57     int msg_length = 0;
58
59     /* count the size of the property message to set */
60     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
61         Con *ws;
62         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
63             if (STARTS_WITH(ws->name, "__"))
64                 continue;
65             msg_length += strlen(ws->name) + 1;
66         }
67     }
68
69     char desktop_names[msg_length];
70     int current_position = 0;
71
72     /* fill the buffer with the names of the i3 workspaces */
73     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
74         Con *ws;
75         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
76             if (STARTS_WITH(ws->name, "__"))
77                 continue;
78
79             for (size_t i = 0; i < strlen(ws->name) + 1; i++) {
80                 desktop_names[current_position++] = ws->name[i];
81             }
82         }
83     }
84
85     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
86                         A__NET_DESKTOP_NAMES, A_UTF8_STRING, 8, msg_length, desktop_names);
87 }
88
89 /*
90  * Updates _NET_DESKTOP_VIEWPORT, which is an array of pairs of cardinals that
91  * define the top left corner of each desktop's viewport.
92  */
93 void ewmh_update_desktop_viewport(void) {
94     Con *output;
95     int num_desktops = 0;
96     /* count number of desktops */
97     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
98         Con *ws;
99         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
100             if (STARTS_WITH(ws->name, "__"))
101                 continue;
102
103             num_desktops++;
104         }
105     }
106
107     uint32_t viewports[num_desktops * 2];
108
109     int current_position = 0;
110     /* fill the viewport buffer */
111     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
112         Con *ws;
113         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
114             if (STARTS_WITH(ws->name, "__"))
115                 continue;
116
117             viewports[current_position++] = output->rect.x;
118             viewports[current_position++] = output->rect.y;
119         }
120     }
121
122     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
123                         A__NET_DESKTOP_VIEWPORT, XCB_ATOM_CARDINAL, 32, current_position, &viewports);
124 }
125
126 static void ewmh_update_wm_desktop_recursively(Con *con, const uint32_t desktop) {
127     Con *child;
128
129     /* Recursively call this to descend through the entire subtree. */
130     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
131         ewmh_update_wm_desktop_recursively(child, desktop);
132     }
133
134     /* If con is a workspace, we also need to go through the floating windows on it. */
135     if (con->type == CT_WORKSPACE) {
136         TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
137             ewmh_update_wm_desktop_recursively(child, desktop);
138         }
139     }
140
141     if (!con_has_managed_window(con))
142         return;
143
144     uint32_t wm_desktop = desktop;
145     /* Sticky windows are only actually sticky when they are floating or inside
146      * a floating container. This is technically still slightly wrong, since
147      * sticky windows will only be on all workspaces on this output, but we
148      * ignore multi-monitor situations for this since the spec isn't too
149      * precise on this anyway. */
150     if (con_is_sticky(con) && con_is_floating(con)) {
151         wm_desktop = NET_WM_DESKTOP_ALL;
152     }
153
154     /* If the window is on the scratchpad we assign the sticky value to it
155      * since showing it works on any workspace. We cannot remove the property
156      * as per specification. */
157     Con *ws = con_get_workspace(con);
158     if (ws != NULL && con_is_internal(ws)) {
159         wm_desktop = NET_WM_DESKTOP_ALL;
160     }
161
162     /* If this is the cached value, we don't need to do anything. */
163     if (con->window->wm_desktop == wm_desktop)
164         return;
165     con->window->wm_desktop = wm_desktop;
166
167     const xcb_window_t window = con->window->id;
168     if (wm_desktop != NET_WM_DESKTOP_NONE) {
169         DLOG("Setting _NET_WM_DESKTOP = %d for window 0x%08x.\n", wm_desktop, window);
170         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, window, A__NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &wm_desktop);
171     } else {
172         /* If we can't determine the workspace index, delete the property. We'd
173          * rather not set it than lie. */
174         ELOG("Failed to determine the proper EWMH desktop index for window 0x%08x, deleting _NET_WM_DESKTOP.\n", window);
175         xcb_delete_property(conn, window, A__NET_WM_DESKTOP);
176     }
177 }
178
179 /*
180  * Updates _NET_WM_DESKTOP for all windows.
181  * A request will only be made if the cached value differs from the calculated value.
182  *
183  */
184 void ewmh_update_wm_desktop(void) {
185     uint32_t desktop = 0;
186
187     Con *output;
188     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
189         Con *workspace;
190         TAILQ_FOREACH(workspace, &(output_get_content(output)->nodes_head), nodes) {
191             ewmh_update_wm_desktop_recursively(workspace, desktop);
192
193             if (!con_is_internal(workspace)) {
194                 ++desktop;
195             }
196         }
197     }
198 }
199
200 /*
201  * Updates _NET_ACTIVE_WINDOW with the currently focused window.
202  *
203  * EWMH: The window ID of the currently active window or None if no window has
204  * the focus.
205  *
206  */
207 void ewmh_update_active_window(xcb_window_t window) {
208     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
209                         A__NET_ACTIVE_WINDOW, XCB_ATOM_WINDOW, 32, 1, &window);
210 }
211
212 /*
213  * Updates _NET_WM_VISIBLE_NAME.
214  *
215  */
216 void ewmh_update_visible_name(xcb_window_t window, const char *name) {
217     if (name != NULL) {
218         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, window, A__NET_WM_VISIBLE_NAME, A_UTF8_STRING, 8, strlen(name), name);
219     } else {
220         xcb_delete_property(conn, window, A__NET_WM_VISIBLE_NAME);
221     }
222 }
223
224 /*
225  * i3 currently does not support _NET_WORKAREA, because it does not correspond
226  * to i3’s concept of workspaces. See also:
227  * http://bugs.i3wm.org/539
228  * http://bugs.i3wm.org/301
229  * http://bugs.i3wm.org/1038
230  *
231  * We need to actively delete this property because some display managers (e.g.
232  * LightDM) set it.
233  *
234  * EWMH: Contains a geometry for each desktop. These geometries specify an area
235  * that is completely contained within the viewport. Work area SHOULD be used by
236  * desktop applications to place desktop icons appropriately.
237  *
238  */
239 void ewmh_update_workarea(void) {
240     xcb_delete_property(conn, root, A__NET_WORKAREA);
241 }
242
243 /*
244  * Updates the _NET_CLIENT_LIST hint.
245  *
246  */
247 void ewmh_update_client_list(xcb_window_t *list, int num_windows) {
248     xcb_change_property(
249         conn,
250         XCB_PROP_MODE_REPLACE,
251         root,
252         A__NET_CLIENT_LIST,
253         XCB_ATOM_WINDOW,
254         32,
255         num_windows,
256         list);
257 }
258
259 /*
260  * Updates the _NET_CLIENT_LIST_STACKING hint.
261  *
262  */
263 void ewmh_update_client_list_stacking(xcb_window_t *stack, int num_windows) {
264     xcb_change_property(
265         conn,
266         XCB_PROP_MODE_REPLACE,
267         root,
268         A__NET_CLIENT_LIST_STACKING,
269         XCB_ATOM_WINDOW,
270         32,
271         num_windows,
272         stack);
273 }
274
275 /*
276  * Set or remove _NET_WM_STATE_STICKY on the window.
277  *
278  */
279 void ewmh_update_sticky(xcb_window_t window, bool sticky) {
280     if (sticky) {
281         DLOG("Setting _NET_WM_STATE_STICKY for window = %d.\n", window);
282         xcb_add_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_STICKY);
283     } else {
284         DLOG("Removing _NET_WM_STATE_STICKY for window = %d.\n", window);
285         xcb_remove_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_STICKY);
286     }
287 }
288
289 /*
290  * Set up the EWMH hints on the root window.
291  *
292  */
293 void ewmh_setup_hints(void) {
294     xcb_atom_t supported_atoms[] = {
295 #define xmacro(atom) A_##atom,
296 #include "atoms_NET_SUPPORTED.xmacro"
297 #undef xmacro
298     };
299
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
304      * _NET_WM_ATOMS. */
305     ewmh_window = xcb_generate_id(conn);
306     /* We create the window and put it at (-1, -1) so that it is off-screen. */
307     xcb_create_window(
308         conn,
309         XCB_COPY_FROM_PARENT,        /* depth */
310         ewmh_window,                 /* window id */
311         root,                        /* parent */
312         -1, -1, 1, 1,                /* dimensions (x, y, w, h) */
313         0,                           /* border */
314         XCB_WINDOW_CLASS_INPUT_ONLY, /* window class */
315         XCB_COPY_FROM_PARENT,        /* visual */
316         XCB_CW_OVERRIDE_REDIRECT,
317         (uint32_t[]){1});
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);
321
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");
324
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);
326
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});
330 }
331
332 /*
333  * Returns the workspace container as enumerated by the EWMH desktop model.
334  * Returns NULL if no workspace could be found for the index.
335  *
336  * This is the reverse of ewmh_get_workspace_index.
337  *
338  */
339 Con *ewmh_get_workspace_by_index(uint32_t idx) {
340     if (idx == NET_WM_DESKTOP_NONE)
341         return NULL;
342
343     uint32_t current_index = 0;
344
345     Con *output;
346     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
347         Con *workspace;
348         TAILQ_FOREACH(workspace, &(output_get_content(output)->nodes_head), nodes) {
349             if (con_is_internal(workspace))
350                 continue;
351
352             if (current_index == idx)
353                 return workspace;
354
355             ++current_index;
356         }
357     }
358
359     return NULL;
360 }
361
362 /*
363  * Returns the EWMH desktop index for the workspace the given container is on.
364  * Returns NET_WM_DESKTOP_NONE if the desktop index cannot be determined.
365  *
366  * This is the reverse of ewmh_get_workspace_by_index.
367  *
368  */
369 uint32_t ewmh_get_workspace_index(Con *con) {
370     uint32_t index = 0;
371
372     Con *workspace = con_get_workspace(con);
373     Con *output;
374     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
375         Con *current;
376         TAILQ_FOREACH(current, &(output_get_content(output)->nodes_head), nodes) {
377             if (con_is_internal(current))
378                 continue;
379
380             if (current == workspace)
381                 return index;
382
383             ++index;
384         }
385     }
386
387     return NET_WM_DESKTOP_NONE;
388 }