2 #define I3__FILE__ "window.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
9 * window.c: Updates window attributes (X11 hints/properties).
15 * Updates the WM_CLASS (consisting of the class and instance) for the
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");
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 = xcb_get_property_value(prop);
31 const size_t class_class_index = strnlen(new_class, prop_length) + 1;
33 FREE(win->class_instance);
34 FREE(win->class_class);
36 win->class_instance = sstrndup(new_class, prop_length);
37 if (class_class_index < prop_length)
38 win->class_class = sstrndup(new_class + class_class_index, prop_length - class_class_index);
40 win->class_class = NULL;
41 LOG("WM_CLASS changed to %s (instance), %s (class)\n",
42 win->class_instance, win->class_class);
55 * Updates the name by using _NET_WM_NAME (encoded in UTF-8) for the given
56 * window. Further updates using window_update_name_legacy will be ignored.
59 void window_update_name(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
60 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
61 DLOG("_NET_WM_NAME not specified, not changing\n");
66 i3string_free(win->name);
67 win->name = i3string_from_utf8_with_length(xcb_get_property_value(prop),
68 xcb_get_property_value_length(prop));
69 if (win->title_format != NULL)
70 ewmh_update_visible_name(win->id, i3string_as_utf8(window_parse_title_format(win)));
71 win->name_x_changed = true;
72 LOG("_NET_WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
74 win->uses_net_wm_name = true;
87 * Updates the name by using WM_NAME (encoded in COMPOUND_TEXT). We do not
88 * touch what the client sends us but pass it to xcb_image_text_8. To get
89 * proper unicode rendering, the application has to use _NET_WM_NAME (see
90 * window_update_name()).
93 void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
94 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
95 DLOG("WM_NAME not set (_NET_WM_NAME is what you want anyways).\n");
100 /* ignore update when the window is known to already have a UTF-8 name */
101 if (win->uses_net_wm_name) {
106 i3string_free(win->name);
107 win->name = i3string_from_utf8_with_length(xcb_get_property_value(prop),
108 xcb_get_property_value_length(prop));
109 if (win->title_format != NULL)
110 ewmh_update_visible_name(win->id, i3string_as_utf8(window_parse_title_format(win)));
112 LOG("WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
113 LOG("Using legacy window title. Note that in order to get Unicode window "
114 "titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
116 win->name_x_changed = true;
123 run_assignments(win);
129 * Updates the CLIENT_LEADER (logical parent window).
132 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
133 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
134 DLOG("CLIENT_LEADER not set on window 0x%08x.\n", win->id);
135 win->leader = XCB_NONE;
140 xcb_window_t *leader = xcb_get_property_value(prop);
141 if (leader == NULL) {
146 DLOG("Client leader changed to %08x\n", *leader);
148 win->leader = *leader;
154 * Updates the TRANSIENT_FOR (logical parent window).
157 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
158 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
159 DLOG("TRANSIENT_FOR not set on window 0x%08x.\n", win->id);
160 win->transient_for = XCB_NONE;
165 xcb_window_t transient_for;
166 if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
171 DLOG("Transient for changed to 0x%08x (window 0x%08x)\n", transient_for, win->id);
173 win->transient_for = transient_for;
179 * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
182 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
183 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
184 DLOG("_NET_WM_STRUT_PARTIAL not set.\n");
190 if (!(strut = xcb_get_property_value(prop))) {
195 DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
196 strut[0], strut[1], strut[2], strut[3]);
198 win->reserved = (struct reservedpx){strut[0], strut[1], strut[2], strut[3]};
204 * Updates the WM_WINDOW_ROLE
207 void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
208 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
209 DLOG("WM_WINDOW_ROLE not set.\n");
215 sasprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
216 (char *)xcb_get_property_value(prop));
218 win->role = new_role;
219 LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
226 run_assignments(win);
232 * Updates the _NET_WM_WINDOW_TYPE property.
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 if (new_type == XCB_NONE) {
238 DLOG("cannot read _NET_WM_WINDOW_TYPE from window.\n");
242 window->window_type = new_type;
243 LOG("_NET_WM_WINDOW_TYPE changed to %i.\n", window->window_type);
245 run_assignments(window);
249 * Updates the WM_HINTS (we only care about the input focus handling part).
252 void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop, bool *urgency_hint) {
253 if (urgency_hint != NULL)
254 *urgency_hint = false;
256 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
257 DLOG("WM_HINTS not set.\n");
262 xcb_icccm_wm_hints_t hints;
264 if (!xcb_icccm_get_wm_hints_from_reply(&hints, prop)) {
265 DLOG("Could not get WM_HINTS\n");
270 if (hints.flags & XCB_ICCCM_WM_HINT_INPUT) {
271 win->doesnt_accept_focus = !hints.input;
272 LOG("WM_HINTS.input changed to \"%d\"\n", hints.input);
275 if (urgency_hint != NULL)
276 *urgency_hint = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
282 * Updates the MOTIF_WM_HINTS. The container's border style should be set to
283 * `motif_border_style' if border style is not BS_NORMAL.
285 * i3 only uses this hint when it specifies a window should have no
286 * title bar, or no decorations at all, which is how most window managers
289 * The EWMH spec intended to replace Motif hints with _NET_WM_WINDOW_TYPE, but
290 * it is still in use by popular widget toolkits such as GTK+ and Java AWT.
293 void window_update_motif_hints(i3Window *win, xcb_get_property_reply_t *prop, border_style_t *motif_border_style) {
294 /* This implementation simply mirrors Gnome's Metacity. Official
295 * documentation of this hint is nowhere to be found.
296 * For more information see:
297 * https://people.gnome.org/~tthurman/docs/metacity/xprops_8h-source.html
298 * http://stackoverflow.com/questions/13787553/detect-if-a-x11-window-has-decorations
300 #define MWM_HINTS_DECORATIONS (1 << 1)
301 #define MWM_DECOR_ALL (1 << 0)
302 #define MWM_DECOR_BORDER (1 << 1)
303 #define MWM_DECOR_TITLE (1 << 3)
305 if (motif_border_style != NULL)
306 *motif_border_style = BS_NORMAL;
308 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
313 /* The property consists of an array of 5 uint64_t's. The first value is a bit
314 * mask of what properties the hint will specify. We are only interested in
315 * MWM_HINTS_DECORATIONS because it indicates that the second value of the
316 * array tells us which decorations the window should have, each flag being
317 * a particular decoration. */
318 uint64_t *motif_hints = (uint64_t *)xcb_get_property_value(prop);
320 if (motif_border_style != NULL && motif_hints[0] & MWM_HINTS_DECORATIONS) {
321 if (motif_hints[1] & MWM_DECOR_ALL || motif_hints[1] & MWM_DECOR_TITLE)
322 *motif_border_style = BS_NORMAL;
323 else if (motif_hints[1] & MWM_DECOR_BORDER)
324 *motif_border_style = BS_PIXEL;
326 *motif_border_style = BS_NONE;
331 #undef MWM_HINTS_DECORATIONS
333 #undef MWM_DECOR_BORDER
334 #undef MWM_DECOR_TITLE
338 * Returns the window title considering the current title format.
339 * If no format is set, this will simply return the window's name.
342 i3String *window_parse_title_format(i3Window *win) {
343 /* We need to ensure that we only escape the window title if pango
344 * is used by the current font. */
345 const bool is_markup = font_is_pango();
347 char *format = win->title_format;
349 return i3string_copy(win->name);
351 /* We initialize these lazily so we only escape them if really necessary. */
352 const char *escaped_title = NULL;
353 const char *escaped_class = NULL;
354 const char *escaped_instance = NULL;
356 /* We have to first iterate over the string to see how much buffer space
357 * we need to allocate. */
358 int buffer_len = strlen(format) + 1;
359 for (char *walk = format; *walk != '\0'; walk++) {
360 if (STARTS_WITH(walk, "%title")) {
361 if (escaped_title == NULL)
362 escaped_title = win->name == NULL ? "" : i3string_as_utf8(is_markup ? i3string_escape_markup(win->name) : win->name);
364 buffer_len = buffer_len - strlen("%title") + strlen(escaped_title);
365 walk += strlen("%title") - 1;
366 } else if (STARTS_WITH(walk, "%class")) {
367 if (escaped_class == NULL)
368 escaped_class = is_markup ? g_markup_escape_text(win->class_class, -1) : win->class_class;
370 buffer_len = buffer_len - strlen("%class") + strlen(escaped_class);
371 walk += strlen("%class") - 1;
372 } else if (STARTS_WITH(walk, "%instance")) {
373 if (escaped_instance == NULL)
374 escaped_instance = is_markup ? g_markup_escape_text(win->class_instance, -1) : win->class_instance;
376 buffer_len = buffer_len - strlen("%instance") + strlen(escaped_instance);
377 walk += strlen("%instance") - 1;
381 /* Now we can parse the format string. */
382 char buffer[buffer_len];
383 char *outwalk = buffer;
384 for (char *walk = format; *walk != '\0'; walk++) {
386 *(outwalk++) = *walk;
390 if (STARTS_WITH(walk + 1, "title")) {
391 outwalk += sprintf(outwalk, "%s", escaped_title);
392 walk += strlen("title");
393 } else if (STARTS_WITH(walk + 1, "class")) {
394 outwalk += sprintf(outwalk, "%s", escaped_class);
395 walk += strlen("class");
396 } else if (STARTS_WITH(walk + 1, "instance")) {
397 outwalk += sprintf(outwalk, "%s", escaped_instance);
398 walk += strlen("instance");
403 i3String *formatted = i3string_from_utf8(buffer);
404 i3string_set_markup(formatted, is_markup);