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