2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * window.c: Updates window attributes (X11 hints/properties).
13 * Frees an i3Window and all its members.
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);
25 * Updates the WM_CLASS (consisting of the class and instance) for the
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");
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;
43 FREE(win->class_instance);
44 FREE(win->class_class);
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);
50 win->class_class = NULL;
51 LOG("WM_CLASS changed to %s (instance), %s (class)\n",
52 win->class_instance, win->class_class);
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.
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");
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));
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);
83 ewmh_update_visible_name(win->id, i3string_as_utf8(name));
86 win->name_x_changed = true;
87 LOG("_NET_WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
89 win->uses_net_wm_name = true;
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()).
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");
115 /* ignore update when the window is known to already have a UTF-8 name */
116 if (win->uses_net_wm_name) {
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));
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);
128 ewmh_update_visible_name(win->id, i3string_as_utf8(name));
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");
136 win->name_x_changed = true;
143 run_assignments(win);
149 * Updates the CLIENT_LEADER (logical parent window).
152 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
153 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
154 DLOG("CLIENT_LEADER not set on window 0x%08x.\n", win->id);
155 win->leader = XCB_NONE;
160 xcb_window_t *leader = xcb_get_property_value(prop);
161 if (leader == NULL) {
166 DLOG("Client leader changed to %08x\n", *leader);
168 win->leader = *leader;
174 * Updates the TRANSIENT_FOR (logical parent window).
177 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
178 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
179 DLOG("TRANSIENT_FOR not set on window 0x%08x.\n", win->id);
180 win->transient_for = XCB_NONE;
185 xcb_window_t transient_for;
186 if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
191 DLOG("Transient for changed to 0x%08x (window 0x%08x)\n", transient_for, win->id);
193 win->transient_for = transient_for;
199 * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
202 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
203 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
204 DLOG("_NET_WM_STRUT_PARTIAL not set.\n");
210 if (!(strut = xcb_get_property_value(prop))) {
215 DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
216 strut[0], strut[1], strut[2], strut[3]);
218 win->reserved = (struct reservedpx){strut[0], strut[1], strut[2], strut[3]};
224 * Updates the WM_WINDOW_ROLE
227 void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
228 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
229 DLOG("WM_WINDOW_ROLE not set.\n");
235 sasprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
236 (char *)xcb_get_property_value(prop));
238 win->role = new_role;
239 LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
246 run_assignments(win);
252 * Updates the _NET_WM_WINDOW_TYPE property.
255 void window_update_type(i3Window *window, xcb_get_property_reply_t *reply) {
256 xcb_atom_t new_type = xcb_get_preferred_window_type(reply);
258 if (new_type == XCB_NONE) {
259 DLOG("cannot read _NET_WM_WINDOW_TYPE from window.\n");
263 window->window_type = new_type;
264 LOG("_NET_WM_WINDOW_TYPE changed to %i.\n", window->window_type);
266 run_assignments(window);
270 * Updates the WM_HINTS (we only care about the input focus handling part).
273 void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop, bool *urgency_hint) {
274 if (urgency_hint != NULL)
275 *urgency_hint = false;
277 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
278 DLOG("WM_HINTS not set.\n");
283 xcb_icccm_wm_hints_t hints;
285 if (!xcb_icccm_get_wm_hints_from_reply(&hints, prop)) {
286 DLOG("Could not get WM_HINTS\n");
291 if (hints.flags & XCB_ICCCM_WM_HINT_INPUT) {
292 win->doesnt_accept_focus = !hints.input;
293 LOG("WM_HINTS.input changed to \"%d\"\n", hints.input);
296 if (urgency_hint != NULL)
297 *urgency_hint = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
303 * Updates the MOTIF_WM_HINTS. The container's border style should be set to
304 * `motif_border_style' if border style is not BS_NORMAL.
306 * i3 only uses this hint when it specifies a window should have no
307 * title bar, or no decorations at all, which is how most window managers
310 * The EWMH spec intended to replace Motif hints with _NET_WM_WINDOW_TYPE, but
311 * it is still in use by popular widget toolkits such as GTK+ and Java AWT.
314 void window_update_motif_hints(i3Window *win, xcb_get_property_reply_t *prop, border_style_t *motif_border_style) {
315 /* This implementation simply mirrors Gnome's Metacity. Official
316 * documentation of this hint is nowhere to be found.
317 * For more information see:
318 * https://people.gnome.org/~tthurman/docs/metacity/xprops_8h-source.html
319 * https://stackoverflow.com/questions/13787553/detect-if-a-x11-window-has-decorations
321 #define MWM_HINTS_FLAGS_FIELD 0
322 #define MWM_HINTS_DECORATIONS_FIELD 2
324 #define MWM_HINTS_DECORATIONS (1 << 1)
325 #define MWM_DECOR_ALL (1 << 0)
326 #define MWM_DECOR_BORDER (1 << 1)
327 #define MWM_DECOR_TITLE (1 << 3)
329 if (motif_border_style != NULL)
330 *motif_border_style = BS_NORMAL;
332 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
337 /* The property consists of an array of 5 uint32_t's. The first value is a
338 * bit mask of what properties the hint will specify. We are only interested
339 * in MWM_HINTS_DECORATIONS because it indicates that the third value of the
340 * array tells us which decorations the window should have, each flag being
341 * a particular decoration. Notice that X11 (Xlib) often mentions 32-bit
342 * fields which in reality are implemented using unsigned long variables
343 * (64-bits long on amd64 for example). On the other hand,
344 * xcb_get_property_value() behaves strictly according to documentation,
345 * i.e. returns 32-bit data fields. */
346 uint32_t *motif_hints = (uint32_t *)xcb_get_property_value(prop);
348 if (motif_border_style != NULL &&
349 motif_hints[MWM_HINTS_FLAGS_FIELD] & MWM_HINTS_DECORATIONS) {
350 if (motif_hints[MWM_HINTS_DECORATIONS_FIELD] & MWM_DECOR_ALL ||
351 motif_hints[MWM_HINTS_DECORATIONS_FIELD] & MWM_DECOR_TITLE)
352 *motif_border_style = BS_NORMAL;
353 else if (motif_hints[MWM_HINTS_DECORATIONS_FIELD] & MWM_DECOR_BORDER)
354 *motif_border_style = BS_PIXEL;
356 *motif_border_style = BS_NONE;
361 #undef MWM_HINTS_FLAGS_FIELD
362 #undef MWM_HINTS_DECORATIONS_FIELD
363 #undef MWM_HINTS_DECORATIONS
365 #undef MWM_DECOR_BORDER
366 #undef MWM_DECOR_TITLE