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