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