i3
ewmh.c
Go to the documentation of this file.
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * ewmh.c: Get/set certain EWMH properties easily.
8  *
9  */
10 #include "all.h"
11 
12 xcb_window_t ewmh_window;
13 
14 /*
15  * Updates _NET_CURRENT_DESKTOP with the current desktop number.
16  *
17  * EWMH: The index of the current desktop. This is always an integer between 0
18  * and _NET_NUMBER_OF_DESKTOPS - 1.
19  *
20  */
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);
25  }
26 }
27 
28 /*
29  * Updates _NET_NUMBER_OF_DESKTOPS which we interpret as the number of
30  * noninternal workspaces.
31  */
33  Con *output;
34  uint32_t idx = 0;
35 
36  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
37  Con *ws;
38  TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
39  if (STARTS_WITH(ws->name, "__"))
40  continue;
41  ++idx;
42  }
43  }
44 
45  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
46  A__NET_NUMBER_OF_DESKTOPS, XCB_ATOM_CARDINAL, 32, 1, &idx);
47 }
48 
49 /*
50  * Updates _NET_DESKTOP_NAMES: "The names of all virtual desktops. This is a
51  * list of NULL-terminated strings in UTF-8 encoding"
52  */
54  Con *output;
55  int msg_length = 0;
56 
57  /* count the size of the property message to set */
58  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
59  Con *ws;
60  TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
61  if (STARTS_WITH(ws->name, "__"))
62  continue;
63  msg_length += strlen(ws->name) + 1;
64  }
65  }
66 
67  char desktop_names[msg_length];
68  int current_position = 0;
69 
70  /* fill the buffer with the names of the i3 workspaces */
71  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
72  Con *ws;
73  TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
74  if (STARTS_WITH(ws->name, "__"))
75  continue;
76 
77  for (size_t i = 0; i < strlen(ws->name) + 1; i++) {
78  desktop_names[current_position++] = ws->name[i];
79  }
80  }
81  }
82 
83  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
84  A__NET_DESKTOP_NAMES, A_UTF8_STRING, 8, msg_length, desktop_names);
85 }
86 
87 /*
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.
90  */
92  Con *output;
93  int num_desktops = 0;
94  /* count number of desktops */
95  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
96  Con *ws;
97  TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
98  if (STARTS_WITH(ws->name, "__"))
99  continue;
100 
101  num_desktops++;
102  }
103  }
104 
105  uint32_t viewports[num_desktops * 2];
106 
107  int current_position = 0;
108  /* fill the viewport buffer */
109  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
110  Con *ws;
111  TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
112  if (STARTS_WITH(ws->name, "__"))
113  continue;
114 
115  viewports[current_position++] = output->rect.x;
116  viewports[current_position++] = output->rect.y;
117  }
118  }
119 
120  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
121  A__NET_DESKTOP_VIEWPORT, XCB_ATOM_CARDINAL, 32, current_position, &viewports);
122 }
123 
124 static void ewmh_update_wm_desktop_recursively(Con *con, const uint32_t desktop) {
125  Con *child;
126 
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);
130  }
131 
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);
136  }
137  }
138 
139  if (!con_has_managed_window(con))
140  return;
141 
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;
150  }
151 
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;
158  }
159 
160  /* If this is the cached value, we don't need to do anything. */
161  if (con->window->wm_desktop == wm_desktop)
162  return;
163  con->window->wm_desktop = wm_desktop;
164 
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);
169  } else {
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);
174  }
175 }
176 
177 /*
178  * Updates _NET_WM_DESKTOP for all windows.
179  * A request will only be made if the cached value differs from the calculated value.
180  *
181  */
183  uint32_t desktop = 0;
184 
185  Con *output;
186  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
187  Con *workspace;
188  TAILQ_FOREACH(workspace, &(output_get_content(output)->nodes_head), nodes) {
189  ewmh_update_wm_desktop_recursively(workspace, desktop);
190 
191  if (!con_is_internal(workspace)) {
192  ++desktop;
193  }
194  }
195  }
196 }
197 
198 /*
199  * Updates _NET_ACTIVE_WINDOW with the currently focused window.
200  *
201  * EWMH: The window ID of the currently active window or None if no window has
202  * the focus.
203  *
204  */
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);
208 }
209 
210 /*
211  * Updates _NET_WM_VISIBLE_NAME.
212  *
213  */
214 void ewmh_update_visible_name(xcb_window_t window, const char *name) {
215  if (name != NULL) {
216  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, window, A__NET_WM_VISIBLE_NAME, A_UTF8_STRING, 8, strlen(name), name);
217  } else {
218  xcb_delete_property(conn, window, A__NET_WM_VISIBLE_NAME);
219  }
220 }
221 
222 /*
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
228  *
229  * We need to actively delete this property because some display managers (e.g.
230  * LightDM) set it.
231  *
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.
235  *
236  */
238  xcb_delete_property(conn, root, A__NET_WORKAREA);
239 }
240 
241 /*
242  * Updates the _NET_CLIENT_LIST hint.
243  *
244  */
245 void ewmh_update_client_list(xcb_window_t *list, int num_windows) {
246  xcb_change_property(
247  conn,
248  XCB_PROP_MODE_REPLACE,
249  root,
250  A__NET_CLIENT_LIST,
251  XCB_ATOM_WINDOW,
252  32,
253  num_windows,
254  list);
255 }
256 
257 /*
258  * Updates the _NET_CLIENT_LIST_STACKING hint.
259  *
260  */
261 void ewmh_update_client_list_stacking(xcb_window_t *stack, int num_windows) {
262  xcb_change_property(
263  conn,
264  XCB_PROP_MODE_REPLACE,
265  root,
266  A__NET_CLIENT_LIST_STACKING,
267  XCB_ATOM_WINDOW,
268  32,
269  num_windows,
270  stack);
271 }
272 
273 /*
274  * Set or remove _NET_WM_STATE_STICKY on the window.
275  *
276  */
277 void ewmh_update_sticky(xcb_window_t window, bool sticky) {
278  if (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);
281  } else {
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);
284  }
285 }
286 
287 /*
288  * Set up the EWMH hints on the root window.
289  *
290  */
291 void ewmh_setup_hints(void) {
292  xcb_atom_t supported_atoms[] = {
293 #define xmacro(atom) A_##atom,
294 #include "atoms_NET_SUPPORTED.xmacro"
295 #undef xmacro
296  };
297 
298  /* Set up the window manager’s name. According to EWMH, section "Root Window
299  * Properties", to indicate that an EWMH-compliant window manager is
300  * present, a child window has to be created (and kept alive as long as the
301  * window manager is running) which has the _NET_SUPPORTING_WM_CHECK and
302  * _NET_WM_ATOMS. */
303  ewmh_window = xcb_generate_id(conn);
304  /* We create the window and put it at (-1, -1) so that it is off-screen. */
305  xcb_create_window(
306  conn,
307  XCB_COPY_FROM_PARENT, /* depth */
308  ewmh_window, /* window id */
309  root, /* parent */
310  -1, -1, 1, 1, /* dimensions (x, y, w, h) */
311  0, /* border */
312  XCB_WINDOW_CLASS_INPUT_ONLY, /* window class */
313  XCB_COPY_FROM_PARENT, /* visual */
314  XCB_CW_OVERRIDE_REDIRECT,
315  (uint32_t[]){1});
316  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, ewmh_window, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &ewmh_window);
317  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, ewmh_window, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
318  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &ewmh_window);
319 
320  /* I’m not entirely sure if we need to keep _NET_WM_NAME on root. */
321  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
322 
323  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);
324 
325  /* 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. */
326  xcb_map_window(conn, ewmh_window);
327  xcb_configure_window(conn, ewmh_window, XCB_CONFIG_WINDOW_STACK_MODE, (uint32_t[]){XCB_STACK_MODE_BELOW});
328 }
329 
330 /*
331  * Returns the workspace container as enumerated by the EWMH desktop model.
332  * Returns NULL if no workspace could be found for the index.
333  *
334  * This is the reverse of ewmh_get_workspace_index.
335  *
336  */
338  if (idx == NET_WM_DESKTOP_NONE)
339  return NULL;
340 
341  uint32_t current_index = 0;
342 
343  Con *output;
344  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
345  Con *workspace;
346  TAILQ_FOREACH(workspace, &(output_get_content(output)->nodes_head), nodes) {
347  if (con_is_internal(workspace))
348  continue;
349 
350  if (current_index == idx)
351  return workspace;
352 
353  ++current_index;
354  }
355  }
356 
357  return NULL;
358 }
359 
360 /*
361  * Returns the EWMH desktop index for the workspace the given container is on.
362  * Returns NET_WM_DESKTOP_NONE if the desktop index cannot be determined.
363  *
364  * This is the reverse of ewmh_get_workspace_by_index.
365  *
366  */
368  uint32_t index = 0;
369 
370  Con *workspace = con_get_workspace(con);
371  Con *output;
372  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
373  Con *current;
374  TAILQ_FOREACH(current, &(output_get_content(output)->nodes_head), nodes) {
375  if (con_is_internal(current))
376  continue;
377 
378  if (current == workspace)
379  return index;
380 
381  ++index;
382  }
383  }
384 
385  return NET_WM_DESKTOP_NONE;
386 }
xcb_window_t ewmh_window
The EWMH support window that is used to indicate that an EWMH-compliant window manager is present...
Definition: ewmh.c:12
xcb_window_t root
Definition: main.c:59
uint32_t wm_desktop
The _NET_WM_DESKTOP for this window.
Definition: data.h:442
Con * focused
Definition: tree.c:13
void xcb_add_property_atom(xcb_connection_t *conn, xcb_window_t window, xcb_atom_t property, xcb_atom_t atom)
Add an atom to a list of atoms the given property defines.
Definition: xcb.c:265
uint32_t ewmh_get_workspace_index(Con *con)
Returns the EWMH desktop index for the workspace the given container is on.
Definition: ewmh.c:367
void ewmh_update_wm_desktop(void)
Updates _NET_WM_DESKTOP for all windows.
Definition: ewmh.c:182
#define ELOG(fmt,...)
Definition: libi3.h:99
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:591
Con * ewmh_get_workspace_by_index(uint32_t idx)
Returns the workspace container as enumerated by the EWMH desktop model.
Definition: ewmh.c:337
struct Rect rect
Definition: data.h:627
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:46
#define NET_WM_DESKTOP_ALL
Definition: workspace.h:25
struct Window * window
Definition: data.h:659
void ewmh_update_sticky(xcb_window_t window, bool sticky)
Set or remove _NET_WM_STATE_STICKY on the window.
Definition: ewmh.c:277
void ewmh_update_number_of_desktops(void)
Updates _NET_NUMBER_OF_DESKTOPS which we interpret as the number of noninternal workspaces.
Definition: ewmh.c:32
enum Con::@20 type
void xcb_remove_property_atom(xcb_connection_t *conn, xcb_window_t window, xcb_atom_t property, xcb_atom_t atom)
Remove an atom from a list of atoms the given property defines without removing any other potentially...
Definition: xcb.c:275
void ewmh_update_current_desktop(void)
Updates _NET_CURRENT_DESKTOP with the current desktop number.
Definition: ewmh.c:21
void ewmh_update_workarea(void)
i3 currently does not support _NET_WORKAREA, because it does not correspond to i3’s concept of works...
Definition: ewmh.c:237
void ewmh_update_client_list(xcb_window_t *list, int num_windows)
Updates the _NET_CLIENT_LIST hint.
Definition: ewmh.c:245
nodes_head
Definition: data.h:672
void ewmh_setup_hints(void)
Set up the EWMH hints on the root window.
Definition: ewmh.c:291
void ewmh_update_desktop_viewport(void)
Updates _NET_DESKTOP_VIEWPORT, which is an array of pairs of cardinals that define the top left corne...
Definition: ewmh.c:91
uint32_t x
Definition: data.h:149
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:405
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
bool con_is_floating(Con *con)
Returns true if the node is floating.
Definition: con.c:510
floating_head
Definition: data.h:669
void ewmh_update_active_window(xcb_window_t window)
Updates _NET_ACTIVE_WINDOW with the currently focused window.
Definition: ewmh.c:205
char * name
Definition: data.h:637
static void ewmh_update_wm_desktop_recursively(Con *con, const uint32_t desktop)
Definition: ewmh.c:124
xcb_window_t id
Definition: data.h:402
bool con_has_managed_window(Con *con)
Returns true when this con is a leaf node with a managed X11 window (e.g., excluding dock containers)...
Definition: con.c:297
void ewmh_update_desktop_names(void)
Updates _NET_DESKTOP_NAMES: "The names of all virtual desktops.
Definition: ewmh.c:53
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition: con.c:502
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition: output.c:16
#define NET_WM_DESKTOP_NONE
Definition: workspace.h:24
uint32_t y
Definition: data.h:150
bool con_is_sticky(Con *con)
Returns whether the container or any of its children is sticky.
Definition: con.c:354
void ewmh_update_visible_name(xcb_window_t window, const char *name)
Updates _NET_WM_VISIBLE_NAME.
Definition: ewmh.c:214
#define DLOG(fmt,...)
Definition: libi3.h:104
static struct stack_entry stack[10]
void ewmh_update_client_list_stacking(xcb_window_t *stack, int num_windows)
Updates the _NET_CLIENT_LIST_STACKING hint.
Definition: ewmh.c:261
#define STARTS_WITH(string, needle)
Definition: util.h:25
struct Con * croot
Definition: tree.c:12