]> git.sur5r.net Git - i3/i3/blob - src/ewmh.c
433a202b59c4397a1ca5bc3b9aaf8f95c3b647a4
[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_VIEWPORT, which is an array of pairs of cardinals that
66  * define the top left corner of each desktop's viewport.
67  */
68 void ewmh_update_desktop_viewport(void) {
69     Con *output;
70     int num_desktops = 0;
71     /* count number of desktops */
72     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
73         Con *ws;
74         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
75             if (STARTS_WITH(ws->name, "__"))
76                 continue;
77
78             num_desktops++;
79         }
80     }
81
82     uint32_t viewports[num_desktops * 2];
83
84     int current_position = 0;
85     /* fill the viewport buffer */
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             viewports[current_position++] = output->rect.x;
93             viewports[current_position++] = output->rect.y;
94         }
95     }
96
97     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
98                         A__NET_DESKTOP_VIEWPORT, XCB_ATOM_CARDINAL, 32, current_position, &viewports);
99 }
100
101 /*
102  * Updates _NET_ACTIVE_WINDOW with the currently focused window.
103  *
104  * EWMH: The window ID of the currently active window or None if no window has
105  * the focus.
106  *
107  */
108 void ewmh_update_active_window(xcb_window_t window) {
109     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
110                         A__NET_ACTIVE_WINDOW, XCB_ATOM_WINDOW, 32, 1, &window);
111 }
112
113 /*
114  * i3 currently does not support _NET_WORKAREA, because it does not correspond
115  * to i3’s concept of workspaces. See also:
116  * http://bugs.i3wm.org/539
117  * http://bugs.i3wm.org/301
118  * http://bugs.i3wm.org/1038
119  *
120  * We need to actively delete this property because some display managers (e.g.
121  * LightDM) set it.
122  *
123  * EWMH: Contains a geometry for each desktop. These geometries specify an area
124  * that is completely contained within the viewport. Work area SHOULD be used by
125  * desktop applications to place desktop icons appropriately.
126  *
127  */
128 void ewmh_update_workarea(void) {
129     xcb_delete_property(conn, root, A__NET_WORKAREA);
130 }
131
132 /*
133  * Updates the _NET_CLIENT_LIST hint.
134  *
135  */
136 void ewmh_update_client_list(xcb_window_t *list, int num_windows) {
137     xcb_change_property(
138         conn,
139         XCB_PROP_MODE_REPLACE,
140         root,
141         A__NET_CLIENT_LIST,
142         XCB_ATOM_WINDOW,
143         32,
144         num_windows,
145         list);
146 }
147
148 /*
149  * Updates the _NET_CLIENT_LIST_STACKING hint.
150  *
151  */
152 void ewmh_update_client_list_stacking(xcb_window_t *stack, int num_windows) {
153     xcb_change_property(
154         conn,
155         XCB_PROP_MODE_REPLACE,
156         root,
157         A__NET_CLIENT_LIST_STACKING,
158         XCB_ATOM_WINDOW,
159         32,
160         num_windows,
161         stack);
162 }
163
164 /*
165  * Set up the EWMH hints on the root window.
166  *
167  */
168 void ewmh_setup_hints(void) {
169     xcb_atom_t supported_atoms[] = {
170 #define xmacro(atom) A_##atom,
171 #include "atoms.xmacro"
172 #undef xmacro
173     };
174
175     /* Set up the window manager’s name. According to EWMH, section "Root Window
176      * Properties", to indicate that an EWMH-compliant window manager is
177      * present, a child window has to be created (and kept alive as long as the
178      * window manager is running) which has the _NET_SUPPORTING_WM_CHECK and
179      * _NET_WM_ATOMS. */
180     xcb_window_t child_window = xcb_generate_id(conn);
181     xcb_create_window(
182         conn,
183         XCB_COPY_FROM_PARENT,        /* depth */
184         child_window,                /* window id */
185         root,                        /* parent */
186         0, 0, 1, 1,                  /* dimensions (x, y, w, h) */
187         0,                           /* border */
188         XCB_WINDOW_CLASS_INPUT_ONLY, /* window class */
189         XCB_COPY_FROM_PARENT,        /* visual */
190         0,
191         NULL);
192     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, child_window, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &child_window);
193     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, child_window, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
194     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 33, 1, &child_window);
195
196     /* I’m not entirely sure if we need to keep _NET_WM_NAME on root. */
197     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
198
199     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTED, XCB_ATOM_ATOM, 32, 20, supported_atoms);
200 }