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