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