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 /*
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  win->name = i3string_from_utf8_with_length(xcb_get_property_value(prop),
78  xcb_get_property_value_length(prop));
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);
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  if (before_mgmt) {
92  free(prop);
93  return;
94  }
95 
96  run_assignments(win);
97 
98  free(prop);
99 }
100 
101 /*
102  * Updates the name by using WM_NAME (encoded in COMPOUND_TEXT). We do not
103  * touch what the client sends us but pass it to xcb_image_text_8. To get
104  * proper unicode rendering, the application has to use _NET_WM_NAME (see
105  * window_update_name()).
106  *
107  */
108 void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
109  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
110  DLOG("WM_NAME not set (_NET_WM_NAME is what you want anyways).\n");
111  FREE(prop);
112  return;
113  }
114 
115  /* ignore update when the window is known to already have a UTF-8 name */
116  if (win->uses_net_wm_name) {
117  free(prop);
118  return;
119  }
120 
121  i3string_free(win->name);
122  win->name = i3string_from_utf8_with_length(xcb_get_property_value(prop),
123  xcb_get_property_value_length(prop));
124 
125  Con *con = con_by_window_id(win->id);
126  if (con != NULL && con->title_format != NULL) {
127  i3String *name = con_parse_title_format(con);
129  I3STRING_FREE(name);
130  }
131 
132  LOG("WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
133  LOG("Using legacy window title. Note that in order to get Unicode window "
134  "titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
135 
136  win->name_x_changed = true;
137 
138  if (before_mgmt) {
139  free(prop);
140  return;
141  }
142 
143  run_assignments(win);
144 
145  free(prop);
146 }
147 
148 /*
149  * Updates the qubes vmname by using _QUBES_VMNAME (encoded in UTF-8) for the given
150  * window.
151  *
152  */
153 void window_update_qubes_vmname(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
154  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
155  win->qubes_vmname = i3string_from_utf8("dom0");
156  FREE(prop);
157  return;
158  }
159 
161  win->qubes_vmname = i3string_from_utf8_with_length(xcb_get_property_value(prop),
162  xcb_get_property_value_length(prop));
163  LOG("_QUBES_VMNAME set to \"%s\"\n", i3string_as_utf8(win->qubes_vmname));
164 
165  if (before_mgmt) {
166  free(prop);
167  return;
168  }
169 
170  run_assignments(win);
171 
172  free(prop);
173 }
174 
175 /*
176  * Updates the qubes label by using _QUBES_LABEL (encoded in UTF-8) for the given
177  * window.
178  *
179  */
180 void window_update_qubes_label(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
181  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
182  win->qubes_label = 0;
183  FREE(prop);
184  return;
185  }
186 
187  win->qubes_label = *(int*) xcb_get_property_value(prop);
188 
189  if (before_mgmt) {
190  free(prop);
191  return;
192  }
193 
194  run_assignments(win);
195 
196  free(prop);
197 }
198 
199 /*
200  * Updates the CLIENT_LEADER (logical parent window).
201  *
202  */
203 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
204  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
205  DLOG("CLIENT_LEADER not set on window 0x%08x.\n", win->id);
206  win->leader = XCB_NONE;
207  FREE(prop);
208  return;
209  }
210 
211  xcb_window_t *leader = xcb_get_property_value(prop);
212  if (leader == NULL) {
213  free(prop);
214  return;
215  }
216 
217  DLOG("Client leader changed to %08x\n", *leader);
218 
219  win->leader = *leader;
220 
221  free(prop);
222 }
223 
224 /*
225  * Updates the TRANSIENT_FOR (logical parent window).
226  *
227  */
228 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
229  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
230  DLOG("TRANSIENT_FOR not set on window 0x%08x.\n", win->id);
231  win->transient_for = XCB_NONE;
232  FREE(prop);
233  return;
234  }
235 
236  xcb_window_t transient_for;
237  if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
238  free(prop);
239  return;
240  }
241 
242  DLOG("Transient for changed to 0x%08x (window 0x%08x)\n", transient_for, win->id);
243 
244  win->transient_for = transient_for;
245 
246  free(prop);
247 }
248 
249 /*
250  * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
251  *
252  */
253 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
254  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
255  DLOG("_NET_WM_STRUT_PARTIAL not set.\n");
256  FREE(prop);
257  return;
258  }
259 
260  uint32_t *strut;
261  if (!(strut = xcb_get_property_value(prop))) {
262  free(prop);
263  return;
264  }
265 
266  DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
267  strut[0], strut[1], strut[2], strut[3]);
268 
269  win->reserved = (struct reservedpx){strut[0], strut[1], strut[2], strut[3]};
270 
271  free(prop);
272 }
273 
274 /*
275  * Updates the WM_WINDOW_ROLE
276  *
277  */
278 void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
279  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
280  DLOG("WM_WINDOW_ROLE not set.\n");
281  FREE(prop);
282  return;
283  }
284 
285  char *new_role;
286  sasprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
287  (char *)xcb_get_property_value(prop));
288  FREE(win->role);
289  win->role = new_role;
290  LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
291 
292  if (before_mgmt) {
293  free(prop);
294  return;
295  }
296 
297  run_assignments(win);
298 
299  free(prop);
300 }
301 
302 /*
303  * Updates the _NET_WM_WINDOW_TYPE property.
304  *
305  */
306 void window_update_type(i3Window *window, xcb_get_property_reply_t *reply) {
307  xcb_atom_t new_type = xcb_get_preferred_window_type(reply);
308  free(reply);
309  if (new_type == XCB_NONE) {
310  DLOG("cannot read _NET_WM_WINDOW_TYPE from window.\n");
311  return;
312  }
313 
314  window->window_type = new_type;
315  LOG("_NET_WM_WINDOW_TYPE changed to %i.\n", window->window_type);
316 
317  run_assignments(window);
318 }
319 
320 /*
321  * Updates the WM_HINTS (we only care about the input focus handling part).
322  *
323  */
324 void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop, bool *urgency_hint) {
325  if (urgency_hint != NULL)
326  *urgency_hint = false;
327 
328  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
329  DLOG("WM_HINTS not set.\n");
330  FREE(prop);
331  return;
332  }
333 
334  xcb_icccm_wm_hints_t hints;
335 
336  if (!xcb_icccm_get_wm_hints_from_reply(&hints, prop)) {
337  DLOG("Could not get WM_HINTS\n");
338  free(prop);
339  return;
340  }
341 
342  if (hints.flags & XCB_ICCCM_WM_HINT_INPUT) {
343  win->doesnt_accept_focus = !hints.input;
344  LOG("WM_HINTS.input changed to \"%d\"\n", hints.input);
345  }
346 
347  if (urgency_hint != NULL)
348  *urgency_hint = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
349 
350  free(prop);
351 }
352 
353 /*
354  * Updates the MOTIF_WM_HINTS. The container's border style should be set to
355  * `motif_border_style' if border style is not BS_NORMAL.
356  *
357  * i3 only uses this hint when it specifies a window should have no
358  * title bar, or no decorations at all, which is how most window managers
359  * handle it.
360  *
361  * The EWMH spec intended to replace Motif hints with _NET_WM_WINDOW_TYPE, but
362  * it is still in use by popular widget toolkits such as GTK+ and Java AWT.
363  *
364  */
365 void window_update_motif_hints(i3Window *win, xcb_get_property_reply_t *prop, border_style_t *motif_border_style) {
366 /* This implementation simply mirrors Gnome's Metacity. Official
367  * documentation of this hint is nowhere to be found.
368  * For more information see:
369  * https://people.gnome.org/~tthurman/docs/metacity/xprops_8h-source.html
370  * https://stackoverflow.com/questions/13787553/detect-if-a-x11-window-has-decorations
371  */
372 #define MWM_HINTS_FLAGS_FIELD 0
373 #define MWM_HINTS_DECORATIONS_FIELD 2
374 
375 #define MWM_HINTS_DECORATIONS (1 << 1)
376 #define MWM_DECOR_ALL (1 << 0)
377 #define MWM_DECOR_BORDER (1 << 1)
378 #define MWM_DECOR_TITLE (1 << 3)
379 
380  if (motif_border_style != NULL)
381  *motif_border_style = BS_NORMAL;
382 
383  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
384  FREE(prop);
385  return;
386  }
387 
388  /* The property consists of an array of 5 uint32_t's. The first value is a
389  * bit mask of what properties the hint will specify. We are only interested
390  * in MWM_HINTS_DECORATIONS because it indicates that the third value of the
391  * array tells us which decorations the window should have, each flag being
392  * a particular decoration. Notice that X11 (Xlib) often mentions 32-bit
393  * fields which in reality are implemented using unsigned long variables
394  * (64-bits long on amd64 for example). On the other hand,
395  * xcb_get_property_value() behaves strictly according to documentation,
396  * i.e. returns 32-bit data fields. */
397  uint32_t *motif_hints = (uint32_t *)xcb_get_property_value(prop);
398 
399  if (motif_border_style != NULL &&
401  if (motif_hints[MWM_HINTS_DECORATIONS_FIELD] & MWM_DECOR_ALL ||
403  *motif_border_style = BS_NORMAL;
404  else if (motif_hints[MWM_HINTS_DECORATIONS_FIELD] & MWM_DECOR_BORDER)
405  *motif_border_style = BS_PIXEL;
406  else
407  *motif_border_style = BS_NONE;
408  }
409 
410  FREE(prop);
411 
412 #undef MWM_HINTS_FLAGS_FIELD
413 #undef MWM_HINTS_DECORATIONS_FIELD
414 #undef MWM_HINTS_DECORATIONS
415 #undef MWM_DECOR_ALL
416 #undef MWM_DECOR_BORDER
417 #undef MWM_DECOR_TITLE
418 }
#define FREE(pointer)
Definition: util.h:47
void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop)
Updates the TRANSIENT_FOR (logical parent window).
Definition: window.c:228
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...
void window_update_qubes_label(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt)
Updates the qubes label by using _QUBES_LABEL (encoded in UTF-8) for the given window.
Definition: window.c:180
#define MWM_DECOR_ALL
void window_update_class(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt)
Updates the WM_CLASS (consisting of the class and instance) for the given window. ...
Definition: window.c:29
border_style_t
Definition: data.h:62
#define MWM_HINTS_DECORATIONS_FIELD
#define DLOG(fmt,...)
Definition: libi3.h:104
xcb_window_t id
Definition: data.h:428
struct reservedpx reserved
Pixels the window reserves.
Definition: data.h:485
char * class_instance
Definition: data.h:441
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:133
void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt)
Updates the name by using WM_NAME (encoded in COMPOUND_TEXT).
Definition: window.c:108
#define MWM_DECOR_BORDER
xcb_atom_t window_type
The _NET_WM_WINDOW_TYPE for this window.
Definition: data.h:471
void window_update_name(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt)
Updates the name by using _NET_WM_NAME (encoded in UTF-8) for the given window.
Definition: window.c:69
char * class_class
Definition: data.h:440
Stores the reserved pixels on each screen edge read from a _NET_WM_STRUT_PARTIAL. ...
Definition: data.h:186
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:614
#define MWM_HINTS_FLAGS_FIELD
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...
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:365
void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt)
Updates the WM_WINDOW_ROLE.
Definition: window.c:278
void window_free(i3Window *win)
Frees an i3Window and all its members.
Definition: window.c:16
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:324
Definition: data.h:62
bool doesnt_accept_focus
Whether this window accepts focus.
Definition: data.h:468
#define LOG(fmt,...)
Definition: libi3.h:94
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
struct _i3String i3String
Opaque data structure for storing strings.
Definition: libi3.h:48
A &#39;Window&#39; is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition: data.h:427
Definition: data.h:63
i3String * i3string_from_utf8_with_length(const char *from_utf8, ssize_t num_bytes)
Build an i3String from an UTF-8 encoded string with fixed length.
int qubes_label
The qubes label.
Definition: data.h:450
i3String * qubes_vmname
The name of the qubes vm.
Definition: data.h:447
xcb_window_t transient_for
Definition: data.h:433
char * role
The WM_WINDOW_ROLE of this window (for example, the pidgin buddy window sets "buddy list")...
Definition: data.h:455
void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop)
Updates the CLIENT_LEADER (logical parent window).
Definition: window.c:203
void window_update_qubes_vmname(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt)
Updates the qubes vmname by using _QUBES_VMNAME (encoded in UTF-8) for the given window.
Definition: window.c:153
void ewmh_update_visible_name(xcb_window_t window, const char *name)
Updates _NET_WM_VISIBLE_NAME.
Definition: ewmh.c:214
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:630
const char * i3string_as_utf8(i3String *str)
Returns the UTF-8 encoded version of the i3String.
bool name_x_changed
Flag to force re-rendering the decoration upon changes.
Definition: data.h:458
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:432
Definition: data.h:64
void i3string_free(i3String *str)
Free an i3String.
i3String * i3string_from_utf8(const char *from_utf8)
Build an i3String from an UTF-8 encoded string.
char * title_format
The format with which the window&#39;s name should be displayed.
Definition: data.h:679
#define MWM_DECOR_TITLE
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:253
Assignment ** ran_assignments
Definition: data.h:438
bool uses_net_wm_name
Whether the application used _NET_WM_NAME.
Definition: data.h:461
void window_update_type(i3Window *window, xcb_get_property_reply_t *reply)
Updates the _NET_WM_WINDOW_TYPE property.
Definition: window.c:306
#define MWM_HINTS_DECORATIONS
i3String * con_parse_title_format(Con *con)
Returns the window title considering the current title format.
Definition: con.c:2277
i3String * name
The name of the window.
Definition: data.h:444
#define I3STRING_FREE(str)
Securely i3string_free by setting the pointer to NULL to prevent accidentally using freed memory...
Definition: libi3.h:236