]> git.sur5r.net Git - i3/i3/blob - src/window.c
Merge pull request #3432 from orestisf1993/aspect-ratio
[i3/i3] / src / window.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * window.c: Updates window attributes (X11 hints/properties).
8  *
9  */
10 #include "all.h"
11
12 /*
13  * Frees an i3Window and all its members.
14  *
15  */
16 void window_free(i3Window *win) {
17     FREE(win->class_class);
18     FREE(win->class_instance);
19     i3string_free(win->name);
20     FREE(win->ran_assignments);
21     FREE(win);
22 }
23
24 /*
25  * Updates the WM_CLASS (consisting of the class and instance) for the
26  * given window.
27  *
28  */
29 void window_update_class(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
30     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
31         DLOG("WM_CLASS not set.\n");
32         FREE(prop);
33         return;
34     }
35
36     /* We cannot use asprintf here since this property contains two
37      * null-terminated strings (for compatibility reasons). Instead, we
38      * use strdup() on both strings */
39     const size_t prop_length = xcb_get_property_value_length(prop);
40     char *new_class = xcb_get_property_value(prop);
41     const size_t class_class_index = strnlen(new_class, prop_length) + 1;
42
43     FREE(win->class_instance);
44     FREE(win->class_class);
45
46     win->class_instance = sstrndup(new_class, prop_length);
47     if (class_class_index < prop_length)
48         win->class_class = sstrndup(new_class + class_class_index, prop_length - class_class_index);
49     else
50         win->class_class = NULL;
51     LOG("WM_CLASS changed to %s (instance), %s (class)\n",
52         win->class_instance, win->class_class);
53
54     if (before_mgmt) {
55         free(prop);
56         return;
57     }
58
59     run_assignments(win);
60
61     free(prop);
62 }
63
64 /*
65  * Updates the name by using _NET_WM_NAME (encoded in UTF-8) for the given
66  * window. Further updates using window_update_name_legacy will be ignored.
67  *
68  */
69 void window_update_name(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
70     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
71         DLOG("_NET_WM_NAME not specified, not changing\n");
72         FREE(prop);
73         return;
74     }
75
76     i3string_free(win->name);
77
78     /* Truncate the name at the first zero byte. See #3515. */
79     const int len = xcb_get_property_value_length(prop);
80     char *name = sstrndup(xcb_get_property_value(prop), len);
81     win->name = i3string_from_utf8(name);
82     free(name);
83
84     Con *con = con_by_window_id(win->id);
85     if (con != NULL && con->title_format != NULL) {
86         i3String *name = con_parse_title_format(con);
87         ewmh_update_visible_name(win->id, i3string_as_utf8(name));
88         I3STRING_FREE(name);
89     }
90     win->name_x_changed = true;
91     LOG("_NET_WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
92
93     win->uses_net_wm_name = true;
94
95     if (before_mgmt) {
96         free(prop);
97         return;
98     }
99
100     run_assignments(win);
101
102     free(prop);
103 }
104
105 /*
106  * Updates the name by using WM_NAME (encoded in COMPOUND_TEXT). We do not
107  * touch what the client sends us but pass it to xcb_image_text_8. To get
108  * proper unicode rendering, the application has to use _NET_WM_NAME (see
109  * window_update_name()).
110  *
111  */
112 void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
113     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
114         DLOG("WM_NAME not set (_NET_WM_NAME is what you want anyways).\n");
115         FREE(prop);
116         return;
117     }
118
119     /* ignore update when the window is known to already have a UTF-8 name */
120     if (win->uses_net_wm_name) {
121         free(prop);
122         return;
123     }
124
125     i3string_free(win->name);
126     const int len = xcb_get_property_value_length(prop);
127     char *name = sstrndup(xcb_get_property_value(prop), len);
128     win->name = i3string_from_utf8(name);
129     free(name);
130
131     Con *con = con_by_window_id(win->id);
132     if (con != NULL && con->title_format != NULL) {
133         i3String *name = con_parse_title_format(con);
134         ewmh_update_visible_name(win->id, i3string_as_utf8(name));
135         I3STRING_FREE(name);
136     }
137
138     LOG("WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
139     LOG("Using legacy window title. Note that in order to get Unicode window "
140         "titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
141
142     win->name_x_changed = true;
143
144     if (before_mgmt) {
145         free(prop);
146         return;
147     }
148
149     run_assignments(win);
150
151     free(prop);
152 }
153
154 /*
155  * Updates the CLIENT_LEADER (logical parent window).
156  *
157  */
158 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
159     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
160         DLOG("CLIENT_LEADER not set on window 0x%08x.\n", win->id);
161         win->leader = XCB_NONE;
162         FREE(prop);
163         return;
164     }
165
166     xcb_window_t *leader = xcb_get_property_value(prop);
167     if (leader == NULL) {
168         free(prop);
169         return;
170     }
171
172     DLOG("Client leader changed to %08x\n", *leader);
173
174     win->leader = *leader;
175
176     free(prop);
177 }
178
179 /*
180  * Updates the TRANSIENT_FOR (logical parent window).
181  *
182  */
183 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
184     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
185         DLOG("TRANSIENT_FOR not set on window 0x%08x.\n", win->id);
186         win->transient_for = XCB_NONE;
187         FREE(prop);
188         return;
189     }
190
191     xcb_window_t transient_for;
192     if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
193         free(prop);
194         return;
195     }
196
197     DLOG("Transient for changed to 0x%08x (window 0x%08x)\n", transient_for, win->id);
198
199     win->transient_for = transient_for;
200
201     free(prop);
202 }
203
204 /*
205  * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
206  *
207  */
208 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
209     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
210         DLOG("_NET_WM_STRUT_PARTIAL not set.\n");
211         FREE(prop);
212         return;
213     }
214
215     uint32_t *strut;
216     if (!(strut = xcb_get_property_value(prop))) {
217         free(prop);
218         return;
219     }
220
221     DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
222          strut[0], strut[1], strut[2], strut[3]);
223
224     win->reserved = (struct reservedpx){strut[0], strut[1], strut[2], strut[3]};
225
226     free(prop);
227 }
228
229 /*
230  * Updates the WM_WINDOW_ROLE
231  *
232  */
233 void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
234     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
235         DLOG("WM_WINDOW_ROLE not set.\n");
236         FREE(prop);
237         return;
238     }
239
240     char *new_role;
241     sasprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
242               (char *)xcb_get_property_value(prop));
243     FREE(win->role);
244     win->role = new_role;
245     LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
246
247     if (before_mgmt) {
248         free(prop);
249         return;
250     }
251
252     run_assignments(win);
253
254     free(prop);
255 }
256
257 /*
258  * Updates the _NET_WM_WINDOW_TYPE property.
259  *
260  */
261 void window_update_type(i3Window *window, xcb_get_property_reply_t *reply) {
262     xcb_atom_t new_type = xcb_get_preferred_window_type(reply);
263     free(reply);
264     if (new_type == XCB_NONE) {
265         DLOG("cannot read _NET_WM_WINDOW_TYPE from window.\n");
266         return;
267     }
268
269     window->window_type = new_type;
270     LOG("_NET_WM_WINDOW_TYPE changed to %i.\n", window->window_type);
271
272     run_assignments(window);
273 }
274
275 /*
276  * Updates the WM_NORMAL_HINTS
277  *
278  */
279 bool window_update_normal_hints(i3Window *win, xcb_get_property_reply_t *reply, xcb_get_geometry_reply_t *geom) {
280     bool changed = false;
281     xcb_size_hints_t size_hints;
282
283     /* If the hints were already in this event, use them, if not, request them */
284     bool success;
285     if (reply != NULL) {
286         success = xcb_icccm_get_wm_size_hints_from_reply(&size_hints, reply);
287     } else {
288         success = xcb_icccm_get_wm_normal_hints_reply(conn, xcb_icccm_get_wm_normal_hints_unchecked(conn, win->id), &size_hints, NULL);
289     }
290     if (!success) {
291         DLOG("Could not get WM_NORMAL_HINTS\n");
292         return false;
293     }
294
295 #define ASSIGN_IF_CHANGED(original, new) \
296     do {                                 \
297         if (original != new) {           \
298             original = new;              \
299             changed = true;              \
300         }                                \
301     } while (0)
302
303     if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)) {
304         DLOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
305
306         ASSIGN_IF_CHANGED(win->min_width, size_hints.min_width);
307         ASSIGN_IF_CHANGED(win->min_height, size_hints.min_height);
308     }
309
310     if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)) {
311         DLOG("Maximum size: %d (width) x %d (height)\n", size_hints.max_width, size_hints.max_height);
312
313         int max_width = max(0, size_hints.max_width);
314         int max_height = max(0, size_hints.max_height);
315
316         ASSIGN_IF_CHANGED(win->max_width, max_width);
317         ASSIGN_IF_CHANGED(win->max_height, max_height);
318     } else {
319         DLOG("Clearing maximum size \n");
320
321         ASSIGN_IF_CHANGED(win->max_width, 0);
322         ASSIGN_IF_CHANGED(win->max_height, 0);
323     }
324
325     if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)) {
326         DLOG("Size increments: %d (width) x %d (height)\n", size_hints.width_inc, size_hints.height_inc);
327
328         if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF) {
329             ASSIGN_IF_CHANGED(win->width_increment, size_hints.width_inc);
330         } else {
331             ASSIGN_IF_CHANGED(win->width_increment, 0);
332         }
333
334         if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF) {
335             ASSIGN_IF_CHANGED(win->height_increment, size_hints.height_inc);
336         } else {
337             ASSIGN_IF_CHANGED(win->height_increment, 0);
338         }
339     } else {
340         DLOG("Clearing size increments\n");
341
342         ASSIGN_IF_CHANGED(win->width_increment, 0);
343         ASSIGN_IF_CHANGED(win->height_increment, 0);
344     }
345
346     /* The base width / height is the desired size of the window. */
347     if (size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE &&
348         (win->base_width >= 0) && (win->base_height >= 0)) {
349         DLOG("Base size: %d (width) x %d (height)\n", size_hints.base_width, size_hints.base_height);
350
351         ASSIGN_IF_CHANGED(win->base_width, size_hints.base_width);
352         ASSIGN_IF_CHANGED(win->base_height, size_hints.base_height);
353     } else {
354         DLOG("Clearing base size\n");
355
356         ASSIGN_IF_CHANGED(win->base_width, 0);
357         ASSIGN_IF_CHANGED(win->base_height, 0);
358     }
359
360     if (geom != NULL &&
361         (size_hints.flags & XCB_ICCCM_SIZE_HINT_US_POSITION || size_hints.flags & XCB_ICCCM_SIZE_HINT_P_POSITION) &&
362         (size_hints.flags & XCB_ICCCM_SIZE_HINT_US_SIZE || size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)) {
363         DLOG("Setting geometry x=%d y=%d w=%d h=%d\n", size_hints.x, size_hints.y, size_hints.width, size_hints.height);
364         geom->x = size_hints.x;
365         geom->y = size_hints.y;
366         geom->width = size_hints.width;
367         geom->height = size_hints.height;
368     }
369
370     /* If no aspect ratio was set or if it was invalid, we ignore the hints */
371     if (size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT &&
372         (size_hints.min_aspect_num >= 0) && (size_hints.min_aspect_den > 0) &&
373         (size_hints.max_aspect_num >= 0) && (size_hints.max_aspect_den > 0)) {
374         /* Convert numerator/denominator to a double */
375         double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
376         double max_aspect = (double)size_hints.max_aspect_num / size_hints.max_aspect_den;
377         DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
378         if (fabs(win->min_aspect_ratio - min_aspect) > DBL_EPSILON) {
379             win->min_aspect_ratio = min_aspect;
380             changed = true;
381         }
382         if (fabs(win->max_aspect_ratio - max_aspect) > DBL_EPSILON) {
383             win->max_aspect_ratio = max_aspect;
384             changed = true;
385         }
386     } else {
387         DLOG("Clearing aspect ratios\n");
388
389         ASSIGN_IF_CHANGED(win->min_aspect_ratio, 0.0);
390         ASSIGN_IF_CHANGED(win->max_aspect_ratio, 0.0);
391     }
392
393     return changed;
394 }
395
396 /*
397  * Updates the WM_HINTS (we only care about the input focus handling part).
398  *
399  */
400 void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop, bool *urgency_hint) {
401     if (urgency_hint != NULL)
402         *urgency_hint = false;
403
404     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
405         DLOG("WM_HINTS not set.\n");
406         FREE(prop);
407         return;
408     }
409
410     xcb_icccm_wm_hints_t hints;
411
412     if (!xcb_icccm_get_wm_hints_from_reply(&hints, prop)) {
413         DLOG("Could not get WM_HINTS\n");
414         free(prop);
415         return;
416     }
417
418     if (hints.flags & XCB_ICCCM_WM_HINT_INPUT) {
419         win->doesnt_accept_focus = !hints.input;
420         LOG("WM_HINTS.input changed to \"%d\"\n", hints.input);
421     }
422
423     if (urgency_hint != NULL)
424         *urgency_hint = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
425
426     free(prop);
427 }
428
429 /*
430  * Updates the MOTIF_WM_HINTS. The container's border style should be set to
431  * `motif_border_style' if border style is not BS_NORMAL.
432  *
433  * i3 only uses this hint when it specifies a window should have no
434  * title bar, or no decorations at all, which is how most window managers
435  * handle it.
436  *
437  * The EWMH spec intended to replace Motif hints with _NET_WM_WINDOW_TYPE, but
438  * it is still in use by popular widget toolkits such as GTK+ and Java AWT.
439  *
440  */
441 void window_update_motif_hints(i3Window *win, xcb_get_property_reply_t *prop, border_style_t *motif_border_style) {
442 /* This implementation simply mirrors Gnome's Metacity. Official
443      * documentation of this hint is nowhere to be found.
444      * For more information see:
445      * https://people.gnome.org/~tthurman/docs/metacity/xprops_8h-source.html
446      * https://stackoverflow.com/questions/13787553/detect-if-a-x11-window-has-decorations
447      */
448 #define MWM_HINTS_FLAGS_FIELD 0
449 #define MWM_HINTS_DECORATIONS_FIELD 2
450
451 #define MWM_HINTS_DECORATIONS (1 << 1)
452 #define MWM_DECOR_ALL (1 << 0)
453 #define MWM_DECOR_BORDER (1 << 1)
454 #define MWM_DECOR_TITLE (1 << 3)
455
456     if (motif_border_style != NULL)
457         *motif_border_style = BS_NORMAL;
458
459     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
460         FREE(prop);
461         return;
462     }
463
464     /* The property consists of an array of 5 uint32_t's. The first value is a
465      * bit mask of what properties the hint will specify. We are only interested
466      * in MWM_HINTS_DECORATIONS because it indicates that the third value of the
467      * array tells us which decorations the window should have, each flag being
468      * a particular decoration. Notice that X11 (Xlib) often mentions 32-bit
469      * fields which in reality are implemented using unsigned long variables
470      * (64-bits long on amd64 for example). On the other hand,
471      * xcb_get_property_value() behaves strictly according to documentation,
472      * i.e. returns 32-bit data fields. */
473     uint32_t *motif_hints = (uint32_t *)xcb_get_property_value(prop);
474
475     if (motif_border_style != NULL &&
476         motif_hints[MWM_HINTS_FLAGS_FIELD] & MWM_HINTS_DECORATIONS) {
477         if (motif_hints[MWM_HINTS_DECORATIONS_FIELD] & MWM_DECOR_ALL ||
478             motif_hints[MWM_HINTS_DECORATIONS_FIELD] & MWM_DECOR_TITLE)
479             *motif_border_style = BS_NORMAL;
480         else if (motif_hints[MWM_HINTS_DECORATIONS_FIELD] & MWM_DECOR_BORDER)
481             *motif_border_style = BS_PIXEL;
482         else
483             *motif_border_style = BS_NONE;
484     }
485
486     FREE(prop);
487
488 #undef MWM_HINTS_FLAGS_FIELD
489 #undef MWM_HINTS_DECORATIONS_FIELD
490 #undef MWM_HINTS_DECORATIONS
491 #undef MWM_DECOR_ALL
492 #undef MWM_DECOR_BORDER
493 #undef MWM_DECOR_TITLE
494 }