2 #define I3__FILE__ "window.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 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 = smalloc(prop_length + 1);
31 memcpy(new_class, xcb_get_property_value(prop), prop_length);
32 new_class[prop_length] = '\0';
34 FREE(win->class_instance);
35 FREE(win->class_class);
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);
41 win->class_class = NULL;
42 LOG("WM_CLASS changed to %s (instance), %s (class)\n",
43 win->class_instance, win->class_class);
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.
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");
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));
75 win->uses_net_wm_name = true;
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()).
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");
101 /* ignore update when the window is known to already have a UTF-8 name */
102 if (win->uses_net_wm_name) {
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));
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");
115 win->name_x_changed = true;
122 run_assignments(win);
128 * Updates the CLIENT_LEADER (logical parent window).
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;
139 xcb_window_t *leader = xcb_get_property_value(prop);
140 if (leader == NULL) {
145 DLOG("Client leader changed to %08x\n", *leader);
147 win->leader = *leader;
153 * Updates the TRANSIENT_FOR (logical parent window).
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;
164 xcb_window_t transient_for;
165 if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
170 DLOG("Transient for changed to 0x%08x (window 0x%08x)\n", transient_for, win->id);
172 win->transient_for = transient_for;
178 * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
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");
189 if (!(strut = xcb_get_property_value(prop))) {
194 DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
195 strut[0], strut[1], strut[2], strut[3]);
197 win->reserved = (struct reservedpx){strut[0], strut[1], strut[2], strut[3]};
203 * Updates the WM_WINDOW_ROLE
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");
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");
222 win->role = new_role;
223 LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
230 run_assignments(win);
236 * Updates the _NET_WM_WINDOW_TYPE property.
239 void window_update_type(i3Window *window, xcb_get_property_reply_t *reply) {
240 xcb_atom_t new_type = xcb_get_preferred_window_type(reply);
241 if (new_type == XCB_NONE) {
242 DLOG("cannot read _NET_WM_WINDOW_TYPE from window.\n");
246 window->window_type = new_type;
247 LOG("_NET_WM_WINDOW_TYPE changed to %i", window->window_type);
249 run_assignments(window);
253 * Updates the WM_HINTS (we only care about the input focus handling part).
256 void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop, bool *urgency_hint) {
257 if (urgency_hint != NULL)
258 *urgency_hint = false;
260 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
261 DLOG("WM_HINTS not set.\n");
266 xcb_icccm_wm_hints_t hints;
268 if (!xcb_icccm_get_wm_hints_from_reply(&hints, prop)) {
269 DLOG("Could not get WM_HINTS\n");
274 win->doesnt_accept_focus = !hints.input;
275 LOG("WM_HINTS.input changed to \"%d\"\n", hints.input);
277 if (urgency_hint != NULL)
278 *urgency_hint = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
284 * Updates the MOTIF_WM_HINTS. The container's border style should be set to
285 * `motif_border_style' if border style is not BS_NORMAL.
287 * i3 only uses this hint when it specifies a window should have no
288 * title bar, or no decorations at all, which is how most window managers
291 * The EWMH spec intended to replace Motif hints with _NET_WM_WINDOW_TYPE, but
292 * it is still in use by popular widget toolkits such as GTK+ and Java AWT.
295 void window_update_motif_hints(i3Window *win, xcb_get_property_reply_t *prop, border_style_t *motif_border_style) {
296 /* This implementation simply mirrors Gnome's Metacity. Official
297 * documentation of this hint is nowhere to be found.
298 * For more information see:
299 * https://people.gnome.org/~tthurman/docs/metacity/xprops_8h-source.html
300 * http://stackoverflow.com/questions/13787553/detect-if-a-x11-window-has-decorations
302 #define MWM_HINTS_DECORATIONS (1 << 1)
303 #define MWM_DECOR_ALL (1 << 0)
304 #define MWM_DECOR_BORDER (1 << 1)
305 #define MWM_DECOR_TITLE (1 << 3)
307 if (motif_border_style != NULL)
308 *motif_border_style = BS_NORMAL;
310 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
315 /* The property consists of an array of 5 uint64_t's. The first value is a bit
316 * mask of what properties the hint will specify. We are only interested in
317 * MWM_HINTS_DECORATIONS because it indicates that the second value of the
318 * array tells us which decorations the window should have, each flag being
319 * a particular decoration. */
320 uint64_t *motif_hints = (uint64_t *)xcb_get_property_value(prop);
322 if (motif_border_style != NULL && motif_hints[0] & MWM_HINTS_DECORATIONS) {
323 if (motif_hints[1] & MWM_DECOR_ALL || motif_hints[1] & MWM_DECOR_TITLE)
324 *motif_border_style = BS_NORMAL;
325 else if (motif_hints[1] & MWM_DECOR_BORDER)
326 *motif_border_style = BS_PIXEL;
328 *motif_border_style = BS_NONE;
333 #undef MWM_HINTS_DECORATIONS
335 #undef MWM_DECOR_BORDER
336 #undef MWM_DECOR_TITLE