]> git.sur5r.net Git - i3/i3/blob - src/window.c
Merge pull request #1895 from Airblader/bug-1883
[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 = xcb_get_property_value(prop);
31     const size_t class_class_index = strnlen(new_class, prop_length) + 1;
32
33     FREE(win->class_instance);
34     FREE(win->class_class);
35
36     win->class_instance = sstrndup(new_class, prop_length);
37     if (class_class_index < prop_length)
38         win->class_class = sstrndup(new_class + class_class_index, prop_length - class_class_index);
39     else
40         win->class_class = NULL;
41     LOG("WM_CLASS changed to %s (instance), %s (class)\n",
42         win->class_instance, win->class_class);
43
44     if (before_mgmt) {
45         free(prop);
46         return;
47     }
48
49     run_assignments(win);
50
51     free(prop);
52 }
53
54 /*
55  * Updates the name by using _NET_WM_NAME (encoded in UTF-8) for the given
56  * window. Further updates using window_update_name_legacy will be ignored.
57  *
58  */
59 void window_update_name(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
60     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
61         DLOG("_NET_WM_NAME not specified, not changing\n");
62         FREE(prop);
63         return;
64     }
65
66     i3string_free(win->name);
67     win->name = i3string_from_utf8_with_length(xcb_get_property_value(prop),
68                                                xcb_get_property_value_length(prop));
69     if (win->title_format != NULL)
70         ewmh_update_visible_name(win->id, i3string_as_utf8(window_parse_title_format(win)));
71     win->name_x_changed = true;
72     LOG("_NET_WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
73
74     win->uses_net_wm_name = true;
75
76     if (before_mgmt) {
77         free(prop);
78         return;
79     }
80
81     run_assignments(win);
82
83     free(prop);
84 }
85
86 /*
87  * Updates the name by using WM_NAME (encoded in COMPOUND_TEXT). We do not
88  * touch what the client sends us but pass it to xcb_image_text_8. To get
89  * proper unicode rendering, the application has to use _NET_WM_NAME (see
90  * window_update_name()).
91  *
92  */
93 void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
94     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
95         DLOG("WM_NAME not set (_NET_WM_NAME is what you want anyways).\n");
96         FREE(prop);
97         return;
98     }
99
100     /* ignore update when the window is known to already have a UTF-8 name */
101     if (win->uses_net_wm_name) {
102         free(prop);
103         return;
104     }
105
106     i3string_free(win->name);
107     win->name = i3string_from_utf8_with_length(xcb_get_property_value(prop),
108                                                xcb_get_property_value_length(prop));
109     if (win->title_format != NULL)
110         ewmh_update_visible_name(win->id, i3string_as_utf8(window_parse_title_format(win)));
111
112     LOG("WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
113     LOG("Using legacy window title. Note that in order to get Unicode window "
114         "titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
115
116     win->name_x_changed = true;
117
118     if (before_mgmt) {
119         free(prop);
120         return;
121     }
122
123     run_assignments(win);
124
125     free(prop);
126 }
127
128 /*
129  * Updates the CLIENT_LEADER (logical parent window).
130  *
131  */
132 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
133     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
134         DLOG("CLIENT_LEADER not set on window 0x%08x.\n", win->id);
135         win->leader = XCB_NONE;
136         FREE(prop);
137         return;
138     }
139
140     xcb_window_t *leader = xcb_get_property_value(prop);
141     if (leader == NULL) {
142         free(prop);
143         return;
144     }
145
146     DLOG("Client leader changed to %08x\n", *leader);
147
148     win->leader = *leader;
149
150     free(prop);
151 }
152
153 /*
154  * Updates the TRANSIENT_FOR (logical parent window).
155  *
156  */
157 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
158     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
159         DLOG("TRANSIENT_FOR not set on window 0x%08x.\n", win->id);
160         win->transient_for = XCB_NONE;
161         FREE(prop);
162         return;
163     }
164
165     xcb_window_t transient_for;
166     if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
167         free(prop);
168         return;
169     }
170
171     DLOG("Transient for changed to 0x%08x (window 0x%08x)\n", transient_for, win->id);
172
173     win->transient_for = transient_for;
174
175     free(prop);
176 }
177
178 /*
179  * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
180  *
181  */
182 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
183     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
184         DLOG("_NET_WM_STRUT_PARTIAL not set.\n");
185         FREE(prop);
186         return;
187     }
188
189     uint32_t *strut;
190     if (!(strut = xcb_get_property_value(prop))) {
191         free(prop);
192         return;
193     }
194
195     DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
196          strut[0], strut[1], strut[2], strut[3]);
197
198     win->reserved = (struct reservedpx){strut[0], strut[1], strut[2], strut[3]};
199
200     free(prop);
201 }
202
203 /*
204  * Updates the WM_WINDOW_ROLE
205  *
206  */
207 void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
208     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
209         DLOG("WM_WINDOW_ROLE not set.\n");
210         FREE(prop);
211         return;
212     }
213
214     char *new_role;
215     sasprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
216               (char *)xcb_get_property_value(prop));
217     FREE(win->role);
218     win->role = new_role;
219     LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
220
221     if (before_mgmt) {
222         free(prop);
223         return;
224     }
225
226     run_assignments(win);
227
228     free(prop);
229 }
230
231 /*
232  * Updates the _NET_WM_WINDOW_TYPE property.
233  *
234  */
235 void window_update_type(i3Window *window, xcb_get_property_reply_t *reply) {
236     xcb_atom_t new_type = xcb_get_preferred_window_type(reply);
237     if (new_type == XCB_NONE) {
238         DLOG("cannot read _NET_WM_WINDOW_TYPE from window.\n");
239         return;
240     }
241
242     window->window_type = new_type;
243     LOG("_NET_WM_WINDOW_TYPE changed to %i.\n", window->window_type);
244
245     run_assignments(window);
246 }
247
248 /*
249  * Updates the WM_HINTS (we only care about the input focus handling part).
250  *
251  */
252 void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop, bool *urgency_hint) {
253     if (urgency_hint != NULL)
254         *urgency_hint = false;
255
256     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
257         DLOG("WM_HINTS not set.\n");
258         FREE(prop);
259         return;
260     }
261
262     xcb_icccm_wm_hints_t hints;
263
264     if (!xcb_icccm_get_wm_hints_from_reply(&hints, prop)) {
265         DLOG("Could not get WM_HINTS\n");
266         free(prop);
267         return;
268     }
269
270     if (hints.flags & XCB_ICCCM_WM_HINT_INPUT) {
271         win->doesnt_accept_focus = !hints.input;
272         LOG("WM_HINTS.input changed to \"%d\"\n", hints.input);
273     }
274
275     if (urgency_hint != NULL)
276         *urgency_hint = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
277
278     free(prop);
279 }
280
281 /*
282  * Updates the MOTIF_WM_HINTS. The container's border style should be set to
283  * `motif_border_style' if border style is not BS_NORMAL.
284  *
285  * i3 only uses this hint when it specifies a window should have no
286  * title bar, or no decorations at all, which is how most window managers
287  * handle it.
288  *
289  * The EWMH spec intended to replace Motif hints with _NET_WM_WINDOW_TYPE, but
290  * it is still in use by popular widget toolkits such as GTK+ and Java AWT.
291  *
292  */
293 void window_update_motif_hints(i3Window *win, xcb_get_property_reply_t *prop, border_style_t *motif_border_style) {
294 /* This implementation simply mirrors Gnome's Metacity. Official
295      * documentation of this hint is nowhere to be found.
296      * For more information see:
297      * https://people.gnome.org/~tthurman/docs/metacity/xprops_8h-source.html
298      * http://stackoverflow.com/questions/13787553/detect-if-a-x11-window-has-decorations
299      */
300 #define MWM_HINTS_DECORATIONS (1 << 1)
301 #define MWM_DECOR_ALL (1 << 0)
302 #define MWM_DECOR_BORDER (1 << 1)
303 #define MWM_DECOR_TITLE (1 << 3)
304
305     if (motif_border_style != NULL)
306         *motif_border_style = BS_NORMAL;
307
308     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
309         FREE(prop);
310         return;
311     }
312
313     /* The property consists of an array of 5 uint64_t's. The first value is a bit
314      * mask of what properties the hint will specify. We are only interested in
315      * MWM_HINTS_DECORATIONS because it indicates that the second value of the
316      * array tells us which decorations the window should have, each flag being
317      * a particular decoration. */
318     uint64_t *motif_hints = (uint64_t *)xcb_get_property_value(prop);
319
320     if (motif_border_style != NULL && motif_hints[0] & MWM_HINTS_DECORATIONS) {
321         if (motif_hints[1] & MWM_DECOR_ALL || motif_hints[1] & MWM_DECOR_TITLE)
322             *motif_border_style = BS_NORMAL;
323         else if (motif_hints[1] & MWM_DECOR_BORDER)
324             *motif_border_style = BS_PIXEL;
325         else
326             *motif_border_style = BS_NONE;
327     }
328
329     FREE(prop);
330
331 #undef MWM_HINTS_DECORATIONS
332 #undef MWM_DECOR_ALL
333 #undef MWM_DECOR_BORDER
334 #undef MWM_DECOR_TITLE
335 }
336
337 /*
338  * Returns the window title considering the current title format.
339  * If no format is set, this will simply return the window's name.
340  *
341  */
342 i3String *window_parse_title_format(i3Window *win) {
343     /* We need to ensure that we only escape the window title if pango
344      * is used by the current font. */
345     const bool is_markup = font_is_pango();
346
347     char *format = win->title_format;
348     if (format == NULL)
349         return i3string_copy(win->name);
350
351     /* We initialize these lazily so we only escape them if really necessary. */
352     const char *escaped_title = NULL;
353     const char *escaped_class = NULL;
354     const char *escaped_instance = NULL;
355
356     /* We have to first iterate over the string to see how much buffer space
357      * we need to allocate. */
358     int buffer_len = strlen(format) + 1;
359     for (char *walk = format; *walk != '\0'; walk++) {
360         if (STARTS_WITH(walk, "%title")) {
361             if (escaped_title == NULL)
362                 escaped_title = win->name == NULL ? "" : i3string_as_utf8(is_markup ? i3string_escape_markup(win->name) : win->name);
363
364             buffer_len = buffer_len - strlen("%title") + strlen(escaped_title);
365             walk += strlen("%title") - 1;
366         } else if (STARTS_WITH(walk, "%class")) {
367             if (escaped_class == NULL)
368                 escaped_class = is_markup ? g_markup_escape_text(win->class_class, -1) : win->class_class;
369
370             buffer_len = buffer_len - strlen("%class") + strlen(escaped_class);
371             walk += strlen("%class") - 1;
372         } else if (STARTS_WITH(walk, "%instance")) {
373             if (escaped_instance == NULL)
374                 escaped_instance = is_markup ? g_markup_escape_text(win->class_instance, -1) : win->class_instance;
375
376             buffer_len = buffer_len - strlen("%instance") + strlen(escaped_instance);
377             walk += strlen("%instance") - 1;
378         }
379     }
380
381     /* Now we can parse the format string. */
382     char buffer[buffer_len];
383     char *outwalk = buffer;
384     for (char *walk = format; *walk != '\0'; walk++) {
385         if (*walk != '%') {
386             *(outwalk++) = *walk;
387             continue;
388         }
389
390         if (STARTS_WITH(walk + 1, "title")) {
391             outwalk += sprintf(outwalk, "%s", escaped_title);
392             walk += strlen("title");
393         } else if (STARTS_WITH(walk + 1, "class")) {
394             outwalk += sprintf(outwalk, "%s", escaped_class);
395             walk += strlen("class");
396         } else if (STARTS_WITH(walk + 1, "instance")) {
397             outwalk += sprintf(outwalk, "%s", escaped_instance);
398             walk += strlen("instance");
399         } else {
400             *(outwalk++) = *walk;
401         }
402     }
403     *outwalk = '\0';
404
405     i3String *formatted = i3string_from_utf8(buffer);
406     i3string_set_markup(formatted, is_markup);
407     return formatted;
408 }