]> git.sur5r.net Git - i3/i3/blob - src/ewmh.c
Handle _NET_WM_STATE_STICKY, but only for floating containers. If this atom is set...
[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     Con *focused_ws = con_get_workspace(focused);
25     Con *output;
26     uint32_t idx = 0;
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) {
30         Con *ws;
31         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
32             if (STARTS_WITH(ws->name, "__"))
33                 continue;
34
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);
38                 return;
39             }
40             ++idx;
41         }
42     }
43 }
44
45 /*
46  * Updates _NET_NUMBER_OF_DESKTOPS which we interpret as the number of
47  * noninternal workspaces.
48  */
49 void ewmh_update_number_of_desktops(void) {
50     Con *output;
51     uint32_t idx = 0;
52
53     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
54         Con *ws;
55         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
56             if (STARTS_WITH(ws->name, "__"))
57                 continue;
58             ++idx;
59         }
60     }
61
62     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
63                         A__NET_NUMBER_OF_DESKTOPS, XCB_ATOM_CARDINAL, 32, 1, &idx);
64 }
65
66 /*
67  * Updates _NET_DESKTOP_NAMES: "The names of all virtual desktops. This is a
68  * list of NULL-terminated strings in UTF-8 encoding"
69  */
70 void ewmh_update_desktop_names(void) {
71     Con *output;
72     int msg_length = 0;
73
74     /* count the size of the property message to set */
75     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
76         Con *ws;
77         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
78             if (STARTS_WITH(ws->name, "__"))
79                 continue;
80             msg_length += strlen(ws->name) + 1;
81         }
82     }
83
84     char desktop_names[msg_length];
85     int current_position = 0;
86
87     /* fill the buffer with the names of the i3 workspaces */
88     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
89         Con *ws;
90         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
91             if (STARTS_WITH(ws->name, "__"))
92                 continue;
93
94             for (size_t i = 0; i < strlen(ws->name) + 1; i++) {
95                 desktop_names[current_position++] = ws->name[i];
96             }
97         }
98     }
99
100     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
101                         A__NET_DESKTOP_NAMES, A_UTF8_STRING, 8, msg_length, desktop_names);
102 }
103
104 /*
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.
107  */
108 void ewmh_update_desktop_viewport(void) {
109     Con *output;
110     int num_desktops = 0;
111     /* count number of desktops */
112     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
113         Con *ws;
114         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
115             if (STARTS_WITH(ws->name, "__"))
116                 continue;
117
118             num_desktops++;
119         }
120     }
121
122     uint32_t viewports[num_desktops * 2];
123
124     int current_position = 0;
125     /* fill the viewport buffer */
126     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
127         Con *ws;
128         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
129             if (STARTS_WITH(ws->name, "__"))
130                 continue;
131
132             viewports[current_position++] = output->rect.x;
133             viewports[current_position++] = output->rect.y;
134         }
135     }
136
137     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
138                         A__NET_DESKTOP_VIEWPORT, XCB_ATOM_CARDINAL, 32, current_position, &viewports);
139 }
140
141 /*
142  * Updates _NET_ACTIVE_WINDOW with the currently focused window.
143  *
144  * EWMH: The window ID of the currently active window or None if no window has
145  * the focus.
146  *
147  */
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);
151 }
152
153 /*
154  * Updates _NET_WM_VISIBLE_NAME.
155  *
156  */
157 void ewmh_update_visible_name(xcb_window_t window, const char *name) {
158     if (name != NULL) {
159         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, window, A__NET_WM_VISIBLE_NAME, A_UTF8_STRING, 8, strlen(name), name);
160     } else {
161         xcb_delete_property(conn, window, A__NET_WM_VISIBLE_NAME);
162     }
163 }
164
165 /*
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
171  *
172  * We need to actively delete this property because some display managers (e.g.
173  * LightDM) set it.
174  *
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.
178  *
179  */
180 void ewmh_update_workarea(void) {
181     xcb_delete_property(conn, root, A__NET_WORKAREA);
182 }
183
184 /*
185  * Updates the _NET_CLIENT_LIST hint.
186  *
187  */
188 void ewmh_update_client_list(xcb_window_t *list, int num_windows) {
189     xcb_change_property(
190         conn,
191         XCB_PROP_MODE_REPLACE,
192         root,
193         A__NET_CLIENT_LIST,
194         XCB_ATOM_WINDOW,
195         32,
196         num_windows,
197         list);
198 }
199
200 /*
201  * Updates the _NET_CLIENT_LIST_STACKING hint.
202  *
203  */
204 void ewmh_update_client_list_stacking(xcb_window_t *stack, int num_windows) {
205     xcb_change_property(
206         conn,
207         XCB_PROP_MODE_REPLACE,
208         root,
209         A__NET_CLIENT_LIST_STACKING,
210         XCB_ATOM_WINDOW,
211         32,
212         num_windows,
213         stack);
214 }
215
216 /*
217  * Set up the EWMH hints on the root window.
218  *
219  */
220 void ewmh_setup_hints(void) {
221     xcb_atom_t supported_atoms[] = {
222 #define xmacro(atom) A_##atom,
223 #include "atoms.xmacro"
224 #undef xmacro
225     };
226
227     /* Set up the window manager’s name. According to EWMH, section "Root Window
228      * Properties", to indicate that an EWMH-compliant window manager is
229      * present, a child window has to be created (and kept alive as long as the
230      * window manager is running) which has the _NET_SUPPORTING_WM_CHECK and
231      * _NET_WM_ATOMS. */
232     ewmh_window = xcb_generate_id(conn);
233     /* We create the window and put it at (-1, -1) so that it is off-screen. */
234     xcb_create_window(
235         conn,
236         XCB_COPY_FROM_PARENT,        /* depth */
237         ewmh_window,                 /* window id */
238         root,                        /* parent */
239         -1, -1, 1, 1,                /* dimensions (x, y, w, h) */
240         0,                           /* border */
241         XCB_WINDOW_CLASS_INPUT_ONLY, /* window class */
242         XCB_COPY_FROM_PARENT,        /* visual */
243         XCB_CW_OVERRIDE_REDIRECT,
244         (uint32_t[]){1});
245     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, ewmh_window, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &ewmh_window);
246     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, ewmh_window, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
247     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &ewmh_window);
248
249     /* I’m not entirely sure if we need to keep _NET_WM_NAME on root. */
250     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
251
252     /* only send the first 31 atoms (last one is _NET_CLOSE_WINDOW) increment that number when adding supported atoms */
253     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTED, XCB_ATOM_ATOM, 32, /* number of atoms */ 32, supported_atoms);
254
255     /* 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. */
256     xcb_map_window(conn, ewmh_window);
257     xcb_configure_window(conn, ewmh_window, XCB_CONFIG_WINDOW_STACK_MODE, (uint32_t[]){XCB_STACK_MODE_BELOW});
258 }