2 #define I3__FILE__ "ewmh.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
9 * ewmh.c: Get/set certain EWMH properties easily.
14 xcb_window_t ewmh_window;
17 * Updates _NET_CURRENT_DESKTOP with the current desktop number.
19 * EWMH: The index of the current desktop. This is always an integer between 0
20 * and _NET_NUMBER_OF_DESKTOPS - 1.
23 void ewmh_update_current_desktop(void) {
24 Con *focused_ws = con_get_workspace(focused);
27 /* We count to get the index of this workspace because named workspaces
28 * don’t have the ->num property */
29 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
31 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
32 if (STARTS_WITH(ws->name, "__"))
35 if (ws == focused_ws) {
36 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
37 A__NET_CURRENT_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &idx);
46 * Updates _NET_NUMBER_OF_DESKTOPS which we interpret as the number of
47 * noninternal workspaces.
49 void ewmh_update_number_of_desktops(void) {
53 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
55 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
56 if (STARTS_WITH(ws->name, "__"))
62 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
63 A__NET_NUMBER_OF_DESKTOPS, XCB_ATOM_CARDINAL, 32, 1, &idx);
67 * Updates _NET_DESKTOP_NAMES: "The names of all virtual desktops. This is a
68 * list of NULL-terminated strings in UTF-8 encoding"
70 void ewmh_update_desktop_names(void) {
74 /* count the size of the property message to set */
75 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
77 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
78 if (STARTS_WITH(ws->name, "__"))
80 msg_length += strlen(ws->name) + 1;
84 char desktop_names[msg_length];
85 int current_position = 0;
87 /* fill the buffer with the names of the i3 workspaces */
88 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
90 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
91 if (STARTS_WITH(ws->name, "__"))
94 for (size_t i = 0; i < strlen(ws->name) + 1; i++) {
95 desktop_names[current_position++] = ws->name[i];
100 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
101 A__NET_DESKTOP_NAMES, A_UTF8_STRING, 8, msg_length, desktop_names);
105 * Updates _NET_DESKTOP_VIEWPORT, which is an array of pairs of cardinals that
106 * define the top left corner of each desktop's viewport.
108 void ewmh_update_desktop_viewport(void) {
110 int num_desktops = 0;
111 /* count number of desktops */
112 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
114 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
115 if (STARTS_WITH(ws->name, "__"))
122 uint32_t viewports[num_desktops * 2];
124 int current_position = 0;
125 /* fill the viewport buffer */
126 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
128 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
129 if (STARTS_WITH(ws->name, "__"))
132 viewports[current_position++] = output->rect.x;
133 viewports[current_position++] = output->rect.y;
137 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
138 A__NET_DESKTOP_VIEWPORT, XCB_ATOM_CARDINAL, 32, current_position, &viewports);
142 * Updates _NET_ACTIVE_WINDOW with the currently focused window.
144 * EWMH: The window ID of the currently active window or None if no window has
148 void ewmh_update_active_window(xcb_window_t window) {
149 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
150 A__NET_ACTIVE_WINDOW, XCB_ATOM_WINDOW, 32, 1, &window);
154 * Updates _NET_WM_VISIBLE_NAME.
157 void ewmh_update_visible_name(xcb_window_t window, const char *name) {
159 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, window, A__NET_WM_VISIBLE_NAME, A_UTF8_STRING, 8, strlen(name), name);
161 xcb_delete_property(conn, window, A__NET_WM_VISIBLE_NAME);
166 * i3 currently does not support _NET_WORKAREA, because it does not correspond
167 * to i3’s concept of workspaces. See also:
168 * http://bugs.i3wm.org/539
169 * http://bugs.i3wm.org/301
170 * http://bugs.i3wm.org/1038
172 * We need to actively delete this property because some display managers (e.g.
175 * EWMH: Contains a geometry for each desktop. These geometries specify an area
176 * that is completely contained within the viewport. Work area SHOULD be used by
177 * desktop applications to place desktop icons appropriately.
180 void ewmh_update_workarea(void) {
181 xcb_delete_property(conn, root, A__NET_WORKAREA);
185 * Updates the _NET_CLIENT_LIST hint.
188 void ewmh_update_client_list(xcb_window_t *list, int num_windows) {
191 XCB_PROP_MODE_REPLACE,
201 * Updates the _NET_CLIENT_LIST_STACKING hint.
204 void ewmh_update_client_list_stacking(xcb_window_t *stack, int num_windows) {
207 XCB_PROP_MODE_REPLACE,
209 A__NET_CLIENT_LIST_STACKING,
217 * Set or remove _NET_WM_STATE_STICKY on the window.
220 void ewmh_update_sticky(xcb_window_t window, bool sticky) {
222 DLOG("Setting _NET_WM_STATE_STICKY for window = %d.\n", window);
223 xcb_add_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_STICKY);
225 DLOG("Removing _NET_WM_STATE_STICKY for window = %d.\n", window);
226 xcb_remove_property_atom(conn, window, A__NET_WM_STATE, A__NET_WM_STATE_STICKY);
231 * Set up the EWMH hints on the root window.
234 void ewmh_setup_hints(void) {
235 xcb_atom_t supported_atoms[] = {
236 #define xmacro(atom) A_##atom,
237 #include "atoms.xmacro"
241 /* Set up the window manager’s name. According to EWMH, section "Root Window
242 * Properties", to indicate that an EWMH-compliant window manager is
243 * present, a child window has to be created (and kept alive as long as the
244 * window manager is running) which has the _NET_SUPPORTING_WM_CHECK and
246 ewmh_window = xcb_generate_id(conn);
247 /* We create the window and put it at (-1, -1) so that it is off-screen. */
250 XCB_COPY_FROM_PARENT, /* depth */
251 ewmh_window, /* window id */
253 -1, -1, 1, 1, /* dimensions (x, y, w, h) */
255 XCB_WINDOW_CLASS_INPUT_ONLY, /* window class */
256 XCB_COPY_FROM_PARENT, /* visual */
257 XCB_CW_OVERRIDE_REDIRECT,
259 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, ewmh_window, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &ewmh_window);
260 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, ewmh_window, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
261 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &ewmh_window);
263 /* I’m not entirely sure if we need to keep _NET_WM_NAME on root. */
264 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
266 /* only send the first 32 atoms (last one is _NET_CLOSE_WINDOW) increment that number when adding supported atoms */
267 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTED, XCB_ATOM_ATOM, 32, /* number of atoms */ 32, supported_atoms);
269 /* 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. */
270 xcb_map_window(conn, ewmh_window);
271 xcb_configure_window(conn, ewmh_window, XCB_CONFIG_WINDOW_STACK_MODE, (uint32_t[]){XCB_STACK_MODE_BELOW});