]> git.sur5r.net Git - i3/i3/blob - src/ewmh.c
Merge branch 'next'
[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-2011 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 /*
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  */
21 void ewmh_update_current_desktop(void) {
22     Con *focused_ws = con_get_workspace(focused);
23     Con *output;
24     uint32_t idx = 0;
25     /* We count to get the index of this workspace because named workspaces
26      * don’t have the ->num property */
27     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
28         Con *ws;
29         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
30             if (STARTS_WITH(ws->name, "__"))
31                 continue;
32
33             if (ws == focused_ws) {
34                 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
35                                     A__NET_CURRENT_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &idx);
36                 return;
37             }
38             ++idx;
39         }
40     }
41 }
42
43 /*
44  * Updates _NET_NUMBER_OF_DESKTOPS which we interpret as the number of
45  * noninternal workspaces.
46  */
47 void ewmh_update_number_of_desktops(void) {
48     Con *output;
49     uint32_t idx = 0;
50
51     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
52         Con *ws;
53         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
54             if (STARTS_WITH(ws->name, "__"))
55                 continue;
56             ++idx;
57         }
58     }
59
60     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
61                         A__NET_NUMBER_OF_DESKTOPS, XCB_ATOM_CARDINAL, 32, 1, &idx);
62 }
63
64 /*
65  * Updates _NET_DESKTOP_NAMES: "The names of all virtual desktops. This is a
66  * list of NULL-terminated strings in UTF-8 encoding"
67  */
68 void ewmh_update_desktop_names(void) {
69     Con *output;
70     int msg_length = 0;
71
72     /* count the size of the property message to set */
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             msg_length += strlen(ws->name) + 1;
79         }
80     }
81
82     char desktop_names[msg_length];
83     int current_position = 0;
84
85     /* fill the buffer with the names of the i3 workspaces */
86     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
87         Con *ws;
88         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
89             if (STARTS_WITH(ws->name, "__"))
90                 continue;
91
92             for (size_t i = 0; i < strlen(ws->name) + 1; i++) {
93                 desktop_names[current_position++] = ws->name[i];
94             }
95         }
96     }
97
98     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
99                         A__NET_DESKTOP_NAMES, A_UTF8_STRING, 8, msg_length, desktop_names);
100 }
101
102 /*
103  * Updates _NET_DESKTOP_VIEWPORT, which is an array of pairs of cardinals that
104  * define the top left corner of each desktop's viewport.
105  */
106 void ewmh_update_desktop_viewport(void) {
107     Con *output;
108     int num_desktops = 0;
109     /* count number of desktops */
110     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
111         Con *ws;
112         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
113             if (STARTS_WITH(ws->name, "__"))
114                 continue;
115
116             num_desktops++;
117         }
118     }
119
120     uint32_t viewports[num_desktops * 2];
121
122     int current_position = 0;
123     /* fill the viewport buffer */
124     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
125         Con *ws;
126         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
127             if (STARTS_WITH(ws->name, "__"))
128                 continue;
129
130             viewports[current_position++] = output->rect.x;
131             viewports[current_position++] = output->rect.y;
132         }
133     }
134
135     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
136                         A__NET_DESKTOP_VIEWPORT, XCB_ATOM_CARDINAL, 32, current_position, &viewports);
137 }
138
139 /*
140  * Updates _NET_ACTIVE_WINDOW with the currently focused window.
141  *
142  * EWMH: The window ID of the currently active window or None if no window has
143  * the focus.
144  *
145  */
146 void ewmh_update_active_window(xcb_window_t window) {
147     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
148                         A__NET_ACTIVE_WINDOW, XCB_ATOM_WINDOW, 32, 1, &window);
149 }
150
151 /*
152  * i3 currently does not support _NET_WORKAREA, because it does not correspond
153  * to i3’s concept of workspaces. See also:
154  * http://bugs.i3wm.org/539
155  * http://bugs.i3wm.org/301
156  * http://bugs.i3wm.org/1038
157  *
158  * We need to actively delete this property because some display managers (e.g.
159  * LightDM) set it.
160  *
161  * EWMH: Contains a geometry for each desktop. These geometries specify an area
162  * that is completely contained within the viewport. Work area SHOULD be used by
163  * desktop applications to place desktop icons appropriately.
164  *
165  */
166 void ewmh_update_workarea(void) {
167     xcb_delete_property(conn, root, A__NET_WORKAREA);
168 }
169
170 /*
171  * Updates the _NET_CLIENT_LIST hint.
172  *
173  */
174 void ewmh_update_client_list(xcb_window_t *list, int num_windows) {
175     xcb_change_property(
176         conn,
177         XCB_PROP_MODE_REPLACE,
178         root,
179         A__NET_CLIENT_LIST,
180         XCB_ATOM_WINDOW,
181         32,
182         num_windows,
183         list);
184 }
185
186 /*
187  * Updates the _NET_CLIENT_LIST_STACKING hint.
188  *
189  */
190 void ewmh_update_client_list_stacking(xcb_window_t *stack, int num_windows) {
191     xcb_change_property(
192         conn,
193         XCB_PROP_MODE_REPLACE,
194         root,
195         A__NET_CLIENT_LIST_STACKING,
196         XCB_ATOM_WINDOW,
197         32,
198         num_windows,
199         stack);
200 }
201
202 /*
203  * Set up the EWMH hints on the root window.
204  *
205  */
206 void ewmh_setup_hints(void) {
207     xcb_atom_t supported_atoms[] = {
208 #define xmacro(atom) A_##atom,
209 #include "atoms.xmacro"
210 #undef xmacro
211     };
212
213     /* Set up the window manager’s name. According to EWMH, section "Root Window
214      * Properties", to indicate that an EWMH-compliant window manager is
215      * present, a child window has to be created (and kept alive as long as the
216      * window manager is running) which has the _NET_SUPPORTING_WM_CHECK and
217      * _NET_WM_ATOMS. */
218     xcb_window_t child_window = xcb_generate_id(conn);
219     xcb_create_window(
220         conn,
221         XCB_COPY_FROM_PARENT,        /* depth */
222         child_window,                /* window id */
223         root,                        /* parent */
224         0, 0, 1, 1,                  /* dimensions (x, y, w, h) */
225         0,                           /* border */
226         XCB_WINDOW_CLASS_INPUT_ONLY, /* window class */
227         XCB_COPY_FROM_PARENT,        /* visual */
228         0,
229         NULL);
230     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, child_window, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &child_window);
231     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, child_window, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
232     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &child_window);
233
234     /* I’m not entirely sure if we need to keep _NET_WM_NAME on root. */
235     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
236
237     /* only send the first 24 atoms (last one is _NET_CLOSE_WINDOW) increment that number when adding supported atoms */
238     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTED, XCB_ATOM_ATOM, 32, 24, supported_atoms);
239 }