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 win->name_x_changed = true;
70 LOG("_NET_WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
72 win->uses_net_wm_name = true;
85 * Updates the name by using WM_NAME (encoded in COMPOUND_TEXT). We do not
86 * touch what the client sends us but pass it to xcb_image_text_8. To get
87 * proper unicode rendering, the application has to use _NET_WM_NAME (see
88 * window_update_name()).
91 void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
92 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
93 DLOG("WM_NAME not set (_NET_WM_NAME is what you want anyways).\n");
98 /* ignore update when the window is known to already have a UTF-8 name */
99 if (win->uses_net_wm_name) {
104 i3string_free(win->name);
105 win->name = i3string_from_utf8_with_length(xcb_get_property_value(prop),
106 xcb_get_property_value_length(prop));
108 LOG("WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
109 LOG("Using legacy window title. Note that in order to get Unicode window "
110 "titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
112 win->name_x_changed = true;
119 run_assignments(win);
125 * Updates the CLIENT_LEADER (logical parent window).
128 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
129 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
130 DLOG("CLIENT_LEADER not set on window 0x%08x.\n", win->id);
131 win->leader = XCB_NONE;
136 xcb_window_t *leader = xcb_get_property_value(prop);
137 if (leader == NULL) {
142 DLOG("Client leader changed to %08x\n", *leader);
144 win->leader = *leader;
150 * Updates the TRANSIENT_FOR (logical parent window).
153 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
154 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
155 DLOG("TRANSIENT_FOR not set on window 0x%08x.\n", win->id);
156 win->transient_for = XCB_NONE;
161 xcb_window_t transient_for;
162 if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
167 DLOG("Transient for changed to 0x%08x (window 0x%08x)\n", transient_for, win->id);
169 win->transient_for = transient_for;
175 * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
178 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
179 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
180 DLOG("_NET_WM_STRUT_PARTIAL not set.\n");
186 if (!(strut = xcb_get_property_value(prop))) {
191 DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
192 strut[0], strut[1], strut[2], strut[3]);
194 win->reserved = (struct reservedpx){strut[0], strut[1], strut[2], strut[3]};
200 * Updates the WM_WINDOW_ROLE
203 void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
204 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
205 DLOG("WM_WINDOW_ROLE not set.\n");
211 sasprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
212 (char *)xcb_get_property_value(prop));
214 win->role = new_role;
215 LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
222 run_assignments(win);
228 * Updates the _NET_WM_WINDOW_TYPE property.
231 void window_update_type(i3Window *window, xcb_get_property_reply_t *reply) {
232 xcb_atom_t new_type = xcb_get_preferred_window_type(reply);
233 if (new_type == XCB_NONE) {
234 DLOG("cannot read _NET_WM_WINDOW_TYPE from window.\n");
238 window->window_type = new_type;
239 LOG("_NET_WM_WINDOW_TYPE changed to %i.\n", window->window_type);
241 run_assignments(window);
245 * Updates the WM_HINTS (we only care about the input focus handling part).
248 void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop, bool *urgency_hint) {
249 if (urgency_hint != NULL)
250 *urgency_hint = false;
252 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
253 DLOG("WM_HINTS not set.\n");
258 xcb_icccm_wm_hints_t hints;
260 if (!xcb_icccm_get_wm_hints_from_reply(&hints, prop)) {
261 DLOG("Could not get WM_HINTS\n");
266 if (hints.flags & XCB_ICCCM_WM_HINT_INPUT) {
267 win->doesnt_accept_focus = !hints.input;
268 LOG("WM_HINTS.input changed to \"%d\"\n", hints.input);
271 if (urgency_hint != NULL)
272 *urgency_hint = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
278 * Updates the MOTIF_WM_HINTS. The container's border style should be set to
279 * `motif_border_style' if border style is not BS_NORMAL.
281 * i3 only uses this hint when it specifies a window should have no
282 * title bar, or no decorations at all, which is how most window managers
285 * The EWMH spec intended to replace Motif hints with _NET_WM_WINDOW_TYPE, but
286 * it is still in use by popular widget toolkits such as GTK+ and Java AWT.
289 void window_update_motif_hints(i3Window *win, xcb_get_property_reply_t *prop, border_style_t *motif_border_style) {
290 /* This implementation simply mirrors Gnome's Metacity. Official
291 * documentation of this hint is nowhere to be found.
292 * For more information see:
293 * https://people.gnome.org/~tthurman/docs/metacity/xprops_8h-source.html
294 * http://stackoverflow.com/questions/13787553/detect-if-a-x11-window-has-decorations
296 #define MWM_HINTS_DECORATIONS (1 << 1)
297 #define MWM_DECOR_ALL (1 << 0)
298 #define MWM_DECOR_BORDER (1 << 1)
299 #define MWM_DECOR_TITLE (1 << 3)
301 if (motif_border_style != NULL)
302 *motif_border_style = BS_NORMAL;
304 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
309 /* The property consists of an array of 5 uint64_t's. The first value is a bit
310 * mask of what properties the hint will specify. We are only interested in
311 * MWM_HINTS_DECORATIONS because it indicates that the second value of the
312 * array tells us which decorations the window should have, each flag being
313 * a particular decoration. */
314 uint64_t *motif_hints = (uint64_t *)xcb_get_property_value(prop);
316 if (motif_border_style != NULL && motif_hints[0] & MWM_HINTS_DECORATIONS) {
317 if (motif_hints[1] & MWM_DECOR_ALL || motif_hints[1] & MWM_DECOR_TITLE)
318 *motif_border_style = BS_NORMAL;
319 else if (motif_hints[1] & MWM_DECOR_BORDER)
320 *motif_border_style = BS_PIXEL;
322 *motif_border_style = BS_NONE;
327 #undef MWM_HINTS_DECORATIONS
329 #undef MWM_DECOR_BORDER
330 #undef MWM_DECOR_TITLE