i3
ewmh.c
Go to the documentation of this file.
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  */
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  */
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  */
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  */
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  /* Recursively call this to descend through the entire subtree. */
128  Con *child;
129  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
130  ewmh_update_wm_desktop_recursively(child, desktop);
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  const xcb_window_t window = con->window->id;
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 this is the cached value, we don't need to do anything. */
155  if (con->window->wm_desktop == wm_desktop)
156  return;
157  con->window->wm_desktop = wm_desktop;
158 
159  if (wm_desktop != NET_WM_DESKTOP_NONE) {
160  DLOG("Setting _NET_WM_DESKTOP = %d for window 0x%08x.\n", wm_desktop, window);
161  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, window, A__NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &wm_desktop);
162  } else {
163  /* If we can't determine the workspace index, delete the property. We'd
164  * rather not set it than lie. */
165  ELOG("Failed to determine the proper EWMH desktop index for window 0x%08x, deleting _NET_WM_DESKTOP.\n", window);
166  xcb_delete_property(conn, window, A__NET_WM_DESKTOP);
167  }
168 }
169 
170 /*
171  * Updates _NET_WM_DESKTOP for all windows.
172  * A request will only be made if the cached value differs from the calculated value.
173  *
174  */
176  uint32_t desktop = 0;
177 
178  Con *output;
179  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
180  Con *workspace;
181  TAILQ_FOREACH(workspace, &(output_get_content(output)->nodes_head), nodes) {
182  if (con_is_internal(workspace))
183  continue;
184 
185  ewmh_update_wm_desktop_recursively(workspace, desktop);
186  ++desktop;
187  }
188  }
189 }
190 
191 /*
192  * Updates _NET_ACTIVE_WINDOW with the currently focused window.
193  *
194  * EWMH: The window ID of the currently active window or None if no window has
195  * the focus.
196  *
197  */
198 void ewmh_update_active_window(xcb_window_t window) {
199  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
200  A__NET_ACTIVE_WINDOW, XCB_ATOM_WINDOW, 32, 1, &window);
201 }
202 
203 /*
204  * Updates _NET_WM_VISIBLE_NAME.
205  *
206  */
207 void ewmh_update_visible_name(xcb_window_t window, const char *name) {
208  if (name != NULL) {
209  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, window, A__NET_WM_VISIBLE_NAME, A_UTF8_STRING, 8, strlen(name), name);
210  } else {
211  xcb_delete_property(conn, window, A__NET_WM_VISIBLE_NAME);
212  }
213 }
214 
215 /*
216  * i3 currently does not support _NET_WORKAREA, because it does not correspond
217  * to i3’s concept of workspaces. See also:
218  * http://bugs.i3wm.org/539
219  * http://bugs.i3wm.org/301
220  * http://bugs.i3wm.org/1038
221  *
222  * We need to actively delete this property because some display managers (e.g.
223  * LightDM) set it.
224  *
225  * EWMH: Contains a geometry for each desktop. These geometries specify an area
226  * that is completely contained within the viewport. Work area SHOULD be used by
227  * desktop applications to place desktop icons appropriately.
228  *
229  */
231  xcb_delete_property(conn, root, A__NET_WORKAREA);
232 }
233 
234 /*
235  * Updates the _NET_CLIENT_LIST hint.
236  *
237  */
238 void ewmh_update_client_list(xcb_window_t *list, int num_windows) {
239  xcb_change_property(
240  conn,
241  XCB_PROP_MODE_REPLACE,
242  root,
243  A__NET_CLIENT_LIST,
245  32,
246  num_windows,
247  list);
248 }
249 
250 /*
251  * Updates the _NET_CLIENT_LIST_STACKING hint.
252  *
253  */
254 void ewmh_update_client_list_stacking(xcb_window_t *stack, int num_windows) {
255  xcb_change_property(
256  conn,
257  XCB_PROP_MODE_REPLACE,
258  root,
259  A__NET_CLIENT_LIST_STACKING,
261  32,
262  num_windows,
263  stack);
264 }
265 
266 /*
267  * Set or remove _NET_WM_STATE_STICKY on the window.
268  *
269  */
270 void ewmh_update_sticky(xcb_window_t window, bool sticky) {
271  if (sticky) {
272  DLOG("Setting _NET_WM_STATE_STICKY for window = %d.\n", window);
273  xcb_add_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_STICKY);
274  } else {
275  DLOG("Removing _NET_WM_STATE_STICKY for window = %d.\n", window);
276  xcb_remove_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_STICKY);
277  }
278 }
279 
280 /*
281  * Set up the EWMH hints on the root window.
282  *
283  */
284 void ewmh_setup_hints(void) {
285  xcb_atom_t supported_atoms[] = {
286 #define xmacro(atom) A_##atom,
287 #include "atoms_NET_SUPPORTED.xmacro"
288 #undef xmacro
289  };
290 
291  /* Set up the window manager’s name. According to EWMH, section "Root Window
292  * Properties", to indicate that an EWMH-compliant window manager is
293  * present, a child window has to be created (and kept alive as long as the
294  * window manager is running) which has the _NET_SUPPORTING_WM_CHECK and
295  * _NET_WM_ATOMS. */
296  ewmh_window = xcb_generate_id(conn);
297  /* We create the window and put it at (-1, -1) so that it is off-screen. */
298  xcb_create_window(
299  conn,
300  XCB_COPY_FROM_PARENT, /* depth */
301  ewmh_window, /* window id */
302  root, /* parent */
303  -1, -1, 1, 1, /* dimensions (x, y, w, h) */
304  0, /* border */
305  XCB_WINDOW_CLASS_INPUT_ONLY, /* window class */
306  XCB_COPY_FROM_PARENT, /* visual */
307  XCB_CW_OVERRIDE_REDIRECT,
308  (uint32_t[]){1});
309  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, ewmh_window, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &ewmh_window);
310  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, ewmh_window, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
311  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &ewmh_window);
312 
313  /* I’m not entirely sure if we need to keep _NET_WM_NAME on root. */
314  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
315 
316  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);
317 
318  /* 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. */
319  xcb_map_window(conn, ewmh_window);
320  xcb_configure_window(conn, ewmh_window, XCB_CONFIG_WINDOW_STACK_MODE, (uint32_t[]){XCB_STACK_MODE_BELOW});
321 }
322 
323 /*
324  * Returns the workspace container as enumerated by the EWMH desktop model.
325  * Returns NULL if no workspace could be found for the index.
326  *
327  * This is the reverse of ewmh_get_workspace_index.
328  *
329  */
331  if (idx == NET_WM_DESKTOP_NONE)
332  return NULL;
333 
334  uint32_t current_index = 0;
335 
336  Con *output;
337  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
338  Con *workspace;
339  TAILQ_FOREACH(workspace, &(output_get_content(output)->nodes_head), nodes) {
340  if (con_is_internal(workspace))
341  continue;
342 
343  if (current_index == idx)
344  return workspace;
345 
346  ++current_index;
347  }
348  }
349 
350  return NULL;
351 }
352 
353 /*
354  * Returns the EWMH desktop index for the workspace the given container is on.
355  * Returns NET_WM_DESKTOP_NONE if the desktop index cannot be determined.
356  *
357  * This is the reverse of ewmh_get_workspace_by_index.
358  *
359  */
361  uint32_t index = 0;
362 
363  Con *workspace = con_get_workspace(con);
364  Con *output;
365  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
366  Con *current;
367  TAILQ_FOREACH(current, &(output_get_content(output)->nodes_head), nodes) {
368  if (con_is_internal(current))
369  continue;
370 
371  if (current == workspace)
372  return index;
373 
374  ++index;
375  }
376  }
377 
378  return NET_WM_DESKTOP_NONE;
379 }
uint32_t x
Definition: data.h:142
static void ewmh_update_wm_desktop_recursively(Con *con, const uint32_t desktop)
Definition: ewmh.c:126
struct Window * window
Definition: data.h:611
char * name
Definition: data.h:590
#define XCB_ATOM_CARDINAL
Definition: xcb_compat.h:39
xcb_window_t root
Definition: main.c:56
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition: output.c:18
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition: con.c:467
void ewmh_update_active_window(xcb_window_t window)
Updates _NET_ACTIVE_WINDOW with the currently focused window.
Definition: ewmh.c:198
static struct stack_entry stack[10]
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:14
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:43
void ewmh_update_current_desktop(void)
Updates _NET_CURRENT_DESKTOP with the current desktop number.
Definition: ewmh.c:23
Con * ewmh_get_workspace_by_index(uint32_t idx)
Returns the workspace container as enumerated by the EWMH desktop model.
Definition: ewmh.c:330
#define NET_WM_DESKTOP_ALL
Definition: workspace.h:23
uint32_t y
Definition: data.h:143
void ewmh_update_number_of_desktops(void)
Updates _NET_NUMBER_OF_DESKTOPS which we interpret as the number of noninternal workspaces.
Definition: ewmh.c:34
void ewmh_setup_hints(void)
Set up the EWMH hints on the root window.
Definition: ewmh.c:284
struct Con * croot
Definition: tree.c:14
#define ELOG(fmt,...)
Definition: libi3.h:93
uint32_t ewmh_get_workspace_index(Con *con)
Returns the EWMH desktop index for the workspace the given container is on.
Definition: ewmh.c:360
void ewmh_update_visible_name(xcb_window_t window, const char *name)
Updates _NET_WM_VISIBLE_NAME.
Definition: ewmh.c:207
struct Rect rect
Definition: data.h:580
bool con_is_sticky(Con *con)
Returns whether the container or any of its children is sticky.
Definition: con.c:322
xcb_window_t id
Definition: data.h:362
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:373
void ewmh_update_sticky(xcb_window_t window, bool sticky)
Set or remove _NET_WM_STATE_STICKY on the window.
Definition: ewmh.c:270
void ewmh_update_client_list(xcb_window_t *list, int num_windows)
Updates the _NET_CLIENT_LIST hint.
Definition: ewmh.c:238
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:305
void ewmh_update_wm_desktop(void)
Updates _NET_WM_DESKTOP for all windows.
Definition: ewmh.c:175
#define XCB_ATOM_ATOM
Definition: xcb_compat.h:45
#define DLOG(fmt,...)
Definition: libi3.h:98
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:544
uint32_t wm_desktop
The _NET_WM_DESKTOP for this window.
Definition: data.h:402
#define STARTS_WITH(string, needle)
Definition: util.h:23
enum Con::@20 type
void ewmh_update_client_list_stacking(xcb_window_t *stack, int num_windows)
Updates the _NET_CLIENT_LIST_STACKING hint.
Definition: ewmh.c:254
#define XCB_ATOM_WINDOW
Definition: xcb_compat.h:40
void ewmh_update_workarea(void)
i3 currently does not support _NET_WORKAREA, because it does not correspond to i3’s concept of worksp...
Definition: ewmh.c:230
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:315
bool con_is_floating(Con *con)
Returns true if the node is floating.
Definition: con.c:475
Con * focused
Definition: tree.c:15
void ewmh_update_desktop_names(void)
Updates _NET_DESKTOP_NAMES: "The names of all virtual desktops.
Definition: ewmh.c:55
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:93
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:265
#define NET_WM_DESKTOP_NONE
Definition: workspace.h:22