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