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-2012 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  * Updates the WM_CLASS (consisting of the class and instance) for the
16  * given window.
17  *
18  */
19 void window_update_class(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
20  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
21  DLOG("WM_CLASS not set.\n");
22  FREE(prop);
23  return;
24  }
25 
26  /* We cannot use asprintf here since this property contains two
27  * null-terminated strings (for compatibility reasons). Instead, we
28  * use strdup() on both strings */
29  char *new_class = xcb_get_property_value(prop);
30 
31  FREE(win->class_instance);
32  FREE(win->class_class);
33 
34  win->class_instance = sstrdup(new_class);
35  if ((strlen(new_class) + 1) < (size_t)xcb_get_property_value_length(prop))
36  win->class_class = sstrdup(new_class + strlen(new_class) + 1);
37  else
38  win->class_class = NULL;
39  LOG("WM_CLASS changed to %s (instance), %s (class)\n",
40  win->class_instance, win->class_class);
41 
42  if (before_mgmt) {
43  free(prop);
44  return;
45  }
46 
47  run_assignments(win);
48 
49  free(prop);
50 }
51 
52 /*
53  * Updates the name by using _NET_WM_NAME (encoded in UTF-8) for the given
54  * window. Further updates using window_update_name_legacy will be ignored.
55  *
56  */
57 void window_update_name(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
58  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
59  DLOG("_NET_WM_NAME not specified, not changing\n");
60  FREE(prop);
61  return;
62  }
63 
64  i3string_free(win->name);
65  win->name = i3string_from_utf8_with_length(xcb_get_property_value(prop),
66  xcb_get_property_value_length(prop));
67  win->name_x_changed = true;
68  LOG("_NET_WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
69 
70  win->uses_net_wm_name = true;
71 
72  if (before_mgmt) {
73  free(prop);
74  return;
75  }
76 
77  run_assignments(win);
78 
79  free(prop);
80 }
81 
82 /*
83  * Updates the name by using WM_NAME (encoded in COMPOUND_TEXT). We do not
84  * touch what the client sends us but pass it to xcb_image_text_8. To get
85  * proper unicode rendering, the application has to use _NET_WM_NAME (see
86  * window_update_name()).
87  *
88  */
89 void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
90  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
91  DLOG("WM_NAME not set (_NET_WM_NAME is what you want anyways).\n");
92  FREE(prop);
93  return;
94  }
95 
96  /* ignore update when the window is known to already have a UTF-8 name */
97  if (win->uses_net_wm_name) {
98  free(prop);
99  return;
100  }
101 
102  i3string_free(win->name);
103  win->name = i3string_from_utf8_with_length(xcb_get_property_value(prop),
104  xcb_get_property_value_length(prop));
105 
106  LOG("WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
107  LOG("Using legacy window title. Note that in order to get Unicode window "
108  "titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
109 
110  win->name_x_changed = true;
111 
112  if (before_mgmt) {
113  free(prop);
114  return;
115  }
116 
117  run_assignments(win);
118 
119  free(prop);
120 }
121 
122 /*
123  * Updates the qubes vmname by using _QUBES_VMNAME (encoded in UTF-8) for the given
124  * window.
125  *
126  */
127 void window_update_qubes_vmname(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
128  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
129  win->qubes_vmname = i3string_from_utf8("dom0");
130  FREE(prop);
131  return;
132  }
133 
135  win->qubes_vmname = i3string_from_utf8_with_length(xcb_get_property_value(prop),
136  xcb_get_property_value_length(prop));
137  LOG("_QUBES_VMNAME set to \"%s\"\n", i3string_as_utf8(win->qubes_vmname));
138 
139  if (before_mgmt) {
140  free(prop);
141  return;
142  }
143 
144  run_assignments(win);
145 
146  free(prop);
147 }
148 
149 /*
150  * Updates the qubes label by using _QUBES_LABEL (encoded in UTF-8) for the given
151  * window.
152  *
153  */
154 void window_update_qubes_label(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
155  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
156  win->qubes_label = 0;
157  FREE(prop);
158  return;
159  }
160 
161  win->qubes_label = *(int*) xcb_get_property_value(prop);
162 
163  if (before_mgmt) {
164  free(prop);
165  return;
166  }
167 
168  run_assignments(win);
169 
170  free(prop);
171 }
172 
173 /*
174  * Updates the CLIENT_LEADER (logical parent window).
175  *
176  */
177 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
178  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
179  DLOG("CLIENT_LEADER not set.\n");
180  FREE(prop);
181  return;
182  }
183 
184  xcb_window_t *leader = xcb_get_property_value(prop);
185  if (leader == NULL) {
186  free(prop);
187  return;
188  }
189 
190  DLOG("Client leader changed to %08x\n", *leader);
191 
192  win->leader = *leader;
193 
194  free(prop);
195 }
196 
197 /*
198  * Updates the TRANSIENT_FOR (logical parent window).
199  *
200  */
201 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
202  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
203  DLOG("TRANSIENT_FOR not set.\n");
204  FREE(prop);
205  return;
206  }
207 
208  xcb_window_t transient_for;
209  if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
210  free(prop);
211  return;
212  }
213 
214  DLOG("Transient for changed to %08x\n", transient_for);
215 
216  win->transient_for = transient_for;
217 
218  free(prop);
219 }
220 
221 /*
222  * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
223  *
224  */
225 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
226  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
227  DLOG("_NET_WM_STRUT_PARTIAL not set.\n");
228  FREE(prop);
229  return;
230  }
231 
232  uint32_t *strut;
233  if (!(strut = xcb_get_property_value(prop))) {
234  free(prop);
235  return;
236  }
237 
238  DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
239  strut[0], strut[1], strut[2], strut[3]);
240 
241  win->reserved = (struct reservedpx) {strut[0], strut[1], strut[2], strut[3]};
242 
243  free(prop);
244 }
245 
246 /*
247  * Updates the WM_WINDOW_ROLE
248  *
249  */
250 void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
251  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
252  DLOG("WM_WINDOW_ROLE not set.\n");
253  FREE(prop);
254  return;
255  }
256 
257  char *new_role;
258  if (asprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
259  (char *)xcb_get_property_value(prop)) == -1) {
260  perror("asprintf()");
261  DLOG("Could not get WM_WINDOW_ROLE\n");
262  free(prop);
263  return;
264  }
265  FREE(win->role);
266  win->role = new_role;
267  LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
268 
269  if (before_mgmt) {
270  free(prop);
271  return;
272  }
273 
274  run_assignments(win);
275 
276  free(prop);
277 }
278 
279 /*
280  * Updates the WM_HINTS (we only care about the input focus handling part).
281  *
282  */
283 void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop, bool *urgency_hint) {
284  if (urgency_hint != NULL)
285  *urgency_hint = false;
286 
287  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
288  DLOG("WM_HINTS not set.\n");
289  FREE(prop);
290  return;
291  }
292 
293  xcb_icccm_wm_hints_t hints;
294 
295  if (!xcb_icccm_get_wm_hints_from_reply(&hints, prop)) {
296  DLOG("Could not get WM_HINTS\n");
297  free(prop);
298  return;
299  }
300 
301  win->doesnt_accept_focus = !hints.input;
302  LOG("WM_HINTS.input changed to \"%d\"\n", hints.input);
303 
304  if (urgency_hint != NULL)
305  *urgency_hint = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
306 
307  free(prop);
308 }
309 
310 /*
311  * Updates the MOTIF_WM_HINTS. The container's border style should be set to
312  * `motif_border_style' if border style is not BS_NORMAL.
313  *
314  * i3 only uses this hint when it specifies a window should have no
315  * title bar, or no decorations at all, which is how most window managers
316  * handle it.
317  *
318  * The EWMH spec intended to replace Motif hints with _NET_WM_WINDOW_TYPE, but
319  * it is still in use by popular widget toolkits such as GTK+ and Java AWT.
320  *
321  */
322 void window_update_motif_hints(i3Window *win, xcb_get_property_reply_t *prop, border_style_t *motif_border_style) {
323 /* This implementation simply mirrors Gnome's Metacity. Official
324  * documentation of this hint is nowhere to be found.
325  * For more information see:
326  * https://people.gnome.org/~tthurman/docs/metacity/xprops_8h-source.html
327  * http://stackoverflow.com/questions/13787553/detect-if-a-x11-window-has-decorations
328  */
329 #define MWM_HINTS_DECORATIONS (1 << 1)
330 #define MWM_DECOR_ALL (1 << 0)
331 #define MWM_DECOR_BORDER (1 << 1)
332 #define MWM_DECOR_TITLE (1 << 3)
333 
334  if (motif_border_style != NULL)
335  *motif_border_style = BS_NORMAL;
336 
337  if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
338  FREE(prop);
339  return;
340  }
341 
342  /* The property consists of an array of 5 uint64_t's. The first value is a bit
343  * mask of what properties the hint will specify. We are only interested in
344  * MWM_HINTS_DECORATIONS because it indicates that the second value of the
345  * array tells us which decorations the window should have, each flag being
346  * a particular decoration. */
347  uint64_t *motif_hints = (uint64_t *)xcb_get_property_value(prop);
348 
349  if (motif_border_style != NULL && motif_hints[0] & MWM_HINTS_DECORATIONS) {
350  if (motif_hints[1] & MWM_DECOR_ALL || motif_hints[1] & MWM_DECOR_TITLE)
351  *motif_border_style = BS_NORMAL;
352  else if (motif_hints[1] & MWM_DECOR_BORDER)
353  *motif_border_style = BS_PIXEL;
354  else
355  *motif_border_style = BS_NONE;
356  }
357 
358  FREE(prop);
359 
360 #undef MWM_HINTS_DECORATIONS
361 #undef MWM_DECOR_ALL
362 #undef MWM_DECOR_BORDER
363 #undef MWM_DECOR_TITLE
364 }
const char * i3string_as_utf8(i3String *str)
Returns the UTF-8 encoded version of the i3String.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
Definition: data.h:56
#define MWM_DECOR_BORDER
border_style_t
Definition: data.h:56
bool uses_net_wm_name
Whether the application used _NET_WM_NAME.
Definition: data.h:366
#define LOG(fmt,...)
Definition: libi3.h:76
Definition: data.h:56
int qubes_label
The qubes label.
Definition: data.h:355
void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop)
Updates the TRANSIENT_FOR (logical parent window).
Definition: window.c:201
#define xcb_icccm_wm_hints_get_urgency
Definition: xcb_compat.h:36
struct reservedpx reserved
Pixels the window reserves.
Definition: data.h:382
void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop)
Updates the CLIENT_LEADER (logical parent window).
Definition: window.c:177
char * class_instance
Definition: data.h:346
bool name_x_changed
Flag to force re-rendering the decoration upon changes.
Definition: data.h:363
A 'Window' is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition: data.h:332
i3String * i3string_from_utf8(const char *from_utf8)
Build an i3String from an UTF-8 encoded string.
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:127
#define DLOG(fmt,...)
Definition: libi3.h:86
xcb_window_t transient_for
Definition: data.h:338
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:154
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:225
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:89
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.
#define xcb_icccm_get_wm_transient_for_from_reply
Definition: xcb_compat.h:37
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
Stores the reserved pixels on each screen edge read from a _NET_WM_STRUT_PARTIAL. ...
Definition: data.h:134
#define xcb_icccm_wm_hints_t
Definition: xcb_compat.h:31
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:322
void i3string_free(i3String *str)
Free an i3String.
i3String * qubes_vmname
The name of the qubes vm.
Definition: data.h:352
#define MWM_DECOR_TITLE
char * role
The WM_WINDOW_ROLE of this window (for example, the pidgin buddy window sets "buddy list")...
Definition: data.h:360
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:19
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:283
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:57
#define MWM_HINTS_DECORATIONS
#define xcb_icccm_get_wm_hints_from_reply
Definition: xcb_compat.h:33
i3String * name
The name of the window.
Definition: data.h:349
char * class_class
Definition: data.h:345
bool doesnt_accept_focus
Whether this window accepts focus.
Definition: data.h:373
void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt)
Updates the WM_WINDOW_ROLE.
Definition: window.c:250
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:337
#define MWM_DECOR_ALL
#define FREE(pointer)
Definition: util.h:46
Definition: data.h:56