]> git.sur5r.net Git - i3/i3/blob - src/window.c
Merge pull request #1665 from Airblader/feature-1658
[i3/i3] / src / window.c
1 #undef I3__FILE__
2 #define I3__FILE__ "window.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  * window.c: Updates window attributes (X11 hints/properties).
10  *
11  */
12 #include "all.h"
13
14 /*
15  * Updates the WM_CLASS (consisting of the class and instance) for the
16  * given window.
17  *
18  */
19 void window_update_class(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
20     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
21         DLOG("WM_CLASS not set.\n");
22         FREE(prop);
23         return;
24     }
25
26     /* We cannot use asprintf here since this property contains two
27      * null-terminated strings (for compatibility reasons). Instead, we
28      * use strdup() on both strings */
29     const size_t prop_length = xcb_get_property_value_length(prop);
30     char *new_class = smalloc(prop_length + 1);
31     memcpy(new_class, xcb_get_property_value(prop), prop_length);
32     new_class[prop_length] = '\0';
33
34     FREE(win->class_instance);
35     FREE(win->class_class);
36
37     win->class_instance = sstrdup(new_class);
38     if ((strlen(new_class) + 1) < prop_length)
39         win->class_class = sstrdup(new_class + strlen(new_class) + 1);
40     else
41         win->class_class = NULL;
42     LOG("WM_CLASS changed to %s (instance), %s (class)\n",
43         win->class_instance, win->class_class);
44
45     if (before_mgmt) {
46         free(new_class);
47         free(prop);
48         return;
49     }
50
51     run_assignments(win);
52
53     free(new_class);
54     free(prop);
55 }
56
57 /*
58  * Updates the name by using _NET_WM_NAME (encoded in UTF-8) for the given
59  * window. Further updates using window_update_name_legacy will be ignored.
60  *
61  */
62 void window_update_name(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
63     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
64         DLOG("_NET_WM_NAME not specified, not changing\n");
65         FREE(prop);
66         return;
67     }
68
69     i3string_free(win->name);
70     win->name = i3string_from_utf8_with_length(xcb_get_property_value(prop),
71                                                xcb_get_property_value_length(prop));
72     win->name_x_changed = true;
73     LOG("_NET_WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
74
75     win->uses_net_wm_name = true;
76
77     if (before_mgmt) {
78         free(prop);
79         return;
80     }
81
82     run_assignments(win);
83
84     free(prop);
85 }
86
87 /*
88  * Updates the name by using WM_NAME (encoded in COMPOUND_TEXT). We do not
89  * touch what the client sends us but pass it to xcb_image_text_8. To get
90  * proper unicode rendering, the application has to use _NET_WM_NAME (see
91  * window_update_name()).
92  *
93  */
94 void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
95     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
96         DLOG("WM_NAME not set (_NET_WM_NAME is what you want anyways).\n");
97         FREE(prop);
98         return;
99     }
100
101     /* ignore update when the window is known to already have a UTF-8 name */
102     if (win->uses_net_wm_name) {
103         free(prop);
104         return;
105     }
106
107     i3string_free(win->name);
108     win->name = i3string_from_utf8_with_length(xcb_get_property_value(prop),
109                                                xcb_get_property_value_length(prop));
110
111     LOG("WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
112     LOG("Using legacy window title. Note that in order to get Unicode window "
113         "titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
114
115     win->name_x_changed = true;
116
117     if (before_mgmt) {
118         free(prop);
119         return;
120     }
121
122     run_assignments(win);
123
124     free(prop);
125 }
126
127 /*
128  * Updates the CLIENT_LEADER (logical parent window).
129  *
130  */
131 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
132     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
133         DLOG("CLIENT_LEADER not set on window 0x%08x.\n", win->id);
134         win->leader = XCB_NONE;
135         FREE(prop);
136         return;
137     }
138
139     xcb_window_t *leader = xcb_get_property_value(prop);
140     if (leader == NULL) {
141         free(prop);
142         return;
143     }
144
145     DLOG("Client leader changed to %08x\n", *leader);
146
147     win->leader = *leader;
148
149     free(prop);
150 }
151
152 /*
153  * Updates the TRANSIENT_FOR (logical parent window).
154  *
155  */
156 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
157     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
158         DLOG("TRANSIENT_FOR not set on window 0x%08x.\n", win->id);
159         win->transient_for = XCB_NONE;
160         FREE(prop);
161         return;
162     }
163
164     xcb_window_t transient_for;
165     if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
166         free(prop);
167         return;
168     }
169
170     DLOG("Transient for changed to 0x%08x (window 0x%08x)\n", transient_for, win->id);
171
172     win->transient_for = transient_for;
173
174     free(prop);
175 }
176
177 /*
178  * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
179  *
180  */
181 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
182     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
183         DLOG("_NET_WM_STRUT_PARTIAL not set.\n");
184         FREE(prop);
185         return;
186     }
187
188     uint32_t *strut;
189     if (!(strut = xcb_get_property_value(prop))) {
190         free(prop);
191         return;
192     }
193
194     DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
195          strut[0], strut[1], strut[2], strut[3]);
196
197     win->reserved = (struct reservedpx){strut[0], strut[1], strut[2], strut[3]};
198
199     free(prop);
200 }
201
202 /*
203  * Updates the WM_WINDOW_ROLE
204  *
205  */
206 void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
207     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
208         DLOG("WM_WINDOW_ROLE not set.\n");
209         FREE(prop);
210         return;
211     }
212
213     char *new_role;
214     if (asprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
215                  (char *)xcb_get_property_value(prop)) == -1) {
216         perror("asprintf()");
217         DLOG("Could not get WM_WINDOW_ROLE\n");
218         free(prop);
219         return;
220     }
221     FREE(win->role);
222     win->role = new_role;
223     LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
224
225     if (before_mgmt) {
226         free(prop);
227         return;
228     }
229
230     run_assignments(win);
231
232     free(prop);
233 }
234
235 /*
236  * Updates the _NET_WM_WINDOW_TYPE property.
237  *
238  */
239 void window_update_type(i3Window *window, xcb_get_property_reply_t *reply) {
240     xcb_atom_t new_type = xcb_get_preferred_window_type(reply);
241     if (new_type == XCB_NONE) {
242         DLOG("cannot read _NET_WM_WINDOW_TYPE from window.\n");
243         return;
244     }
245
246     window->window_type = new_type;
247     LOG("_NET_WM_WINDOW_TYPE changed to %i", window->window_type);
248
249     run_assignments(window);
250 }
251
252 /*
253  * Updates the WM_HINTS (we only care about the input focus handling part).
254  *
255  */
256 void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop, bool *urgency_hint) {
257     if (urgency_hint != NULL)
258         *urgency_hint = false;
259
260     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
261         DLOG("WM_HINTS not set.\n");
262         FREE(prop);
263         return;
264     }
265
266     xcb_icccm_wm_hints_t hints;
267
268     if (!xcb_icccm_get_wm_hints_from_reply(&hints, prop)) {
269         DLOG("Could not get WM_HINTS\n");
270         free(prop);
271         return;
272     }
273
274     win->doesnt_accept_focus = !hints.input;
275     LOG("WM_HINTS.input changed to \"%d\"\n", hints.input);
276
277     if (urgency_hint != NULL)
278         *urgency_hint = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
279
280     free(prop);
281 }
282
283 /*
284  * Updates the MOTIF_WM_HINTS. The container's border style should be set to
285  * `motif_border_style' if border style is not BS_NORMAL.
286  *
287  * i3 only uses this hint when it specifies a window should have no
288  * title bar, or no decorations at all, which is how most window managers
289  * handle it.
290  *
291  * The EWMH spec intended to replace Motif hints with _NET_WM_WINDOW_TYPE, but
292  * it is still in use by popular widget toolkits such as GTK+ and Java AWT.
293  *
294  */
295 void window_update_motif_hints(i3Window *win, xcb_get_property_reply_t *prop, border_style_t *motif_border_style) {
296 /* This implementation simply mirrors Gnome's Metacity. Official
297      * documentation of this hint is nowhere to be found.
298      * For more information see:
299      * https://people.gnome.org/~tthurman/docs/metacity/xprops_8h-source.html
300      * http://stackoverflow.com/questions/13787553/detect-if-a-x11-window-has-decorations
301      */
302 #define MWM_HINTS_DECORATIONS (1 << 1)
303 #define MWM_DECOR_ALL (1 << 0)
304 #define MWM_DECOR_BORDER (1 << 1)
305 #define MWM_DECOR_TITLE (1 << 3)
306
307     if (motif_border_style != NULL)
308         *motif_border_style = BS_NORMAL;
309
310     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
311         FREE(prop);
312         return;
313     }
314
315     /* The property consists of an array of 5 uint64_t's. The first value is a bit
316      * mask of what properties the hint will specify. We are only interested in
317      * MWM_HINTS_DECORATIONS because it indicates that the second value of the
318      * array tells us which decorations the window should have, each flag being
319      * a particular decoration. */
320     uint64_t *motif_hints = (uint64_t *)xcb_get_property_value(prop);
321
322     if (motif_border_style != NULL && motif_hints[0] & MWM_HINTS_DECORATIONS) {
323         if (motif_hints[1] & MWM_DECOR_ALL || motif_hints[1] & MWM_DECOR_TITLE)
324             *motif_border_style = BS_NORMAL;
325         else if (motif_hints[1] & MWM_DECOR_BORDER)
326             *motif_border_style = BS_PIXEL;
327         else
328             *motif_border_style = BS_NONE;
329     }
330
331     FREE(prop);
332
333 #undef MWM_HINTS_DECORATIONS
334 #undef MWM_DECOR_ALL
335 #undef MWM_DECOR_BORDER
336 #undef MWM_DECOR_TITLE
337 }