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