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