i3
src/window.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=4:sw=4:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
00006  *
00007  * window.c: Updates window attributes (X11 hints/properties).
00008  *
00009  */
00010 #include "all.h"
00011 
00012 /*
00013  * Updates the WM_CLASS (consisting of the class and instance) for the
00014  * given window.
00015  *
00016  */
00017 void window_update_class(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
00018     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
00019         DLOG("WM_CLASS not set.\n");
00020         FREE(prop);
00021         return;
00022     }
00023 
00024     /* We cannot use asprintf here since this property contains two
00025      * null-terminated strings (for compatibility reasons). Instead, we
00026      * use strdup() on both strings */
00027     char *new_class = xcb_get_property_value(prop);
00028 
00029     FREE(win->class_instance);
00030     FREE(win->class_class);
00031 
00032     win->class_instance = sstrdup(new_class);
00033     if ((strlen(new_class) + 1) < xcb_get_property_value_length(prop))
00034         win->class_class = sstrdup(new_class + strlen(new_class) + 1);
00035     else win->class_class = NULL;
00036     LOG("WM_CLASS changed to %s (instance), %s (class)\n",
00037         win->class_instance, win->class_class);
00038 
00039     if (before_mgmt) {
00040         free(prop);
00041         return;
00042     }
00043 
00044     run_assignments(win);
00045 
00046     free(prop);
00047 }
00048 
00049 /*
00050  * Updates the name by using _NET_WM_NAME (encoded in UTF-8) for the given
00051  * window. Further updates using window_update_name_legacy will be ignored.
00052  *
00053  */
00054 void window_update_name(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
00055     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
00056         DLOG("_NET_WM_NAME not specified, not changing\n");
00057         FREE(prop);
00058         return;
00059     }
00060 
00061     /* Save the old pointer to make the update atomic */
00062     char *new_name;
00063     if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop),
00064                  (char*)xcb_get_property_value(prop)) == -1) {
00065         perror("asprintf()");
00066         DLOG("Could not get window name\n");
00067         free(prop);
00068         return;
00069     }
00070     /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
00071     size_t len;
00072     xcb_char2b_t *ucs2_name = convert_utf8_to_ucs2(new_name, &len);
00073     if (ucs2_name == NULL) {
00074         LOG("Could not convert _NET_WM_NAME to UCS-2, ignoring new hint\n");
00075         FREE(new_name);
00076         free(prop);
00077         return;
00078     }
00079     FREE(win->name_x);
00080     FREE(win->name_json);
00081     win->name_json = new_name;
00082     win->name_x = (char*)ucs2_name;
00083     win->name_len = len;
00084     win->name_x_changed = true;
00085     LOG("_NET_WM_NAME changed to \"%s\"\n", win->name_json);
00086 
00087     win->uses_net_wm_name = true;
00088 
00089     if (before_mgmt) {
00090         free(prop);
00091         return;
00092     }
00093 
00094     run_assignments(win);
00095 
00096     free(prop);
00097 }
00098 
00099 /*
00100  * Updates the name by using WM_NAME (encoded in COMPOUND_TEXT). We do not
00101  * touch what the client sends us but pass it to xcb_image_text_8. To get
00102  * proper unicode rendering, the application has to use _NET_WM_NAME (see
00103  * window_update_name()).
00104  *
00105  */
00106 void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
00107     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
00108         DLOG("WM_NAME not set (_NET_WM_NAME is what you want anyways).\n");
00109         FREE(prop);
00110         return;
00111     }
00112 
00113     /* ignore update when the window is known to already have a UTF-8 name */
00114     if (win->uses_net_wm_name) {
00115         free(prop);
00116         return;
00117     }
00118 
00119     char *new_name;
00120     if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop),
00121                  (char*)xcb_get_property_value(prop)) == -1) {
00122         perror("asprintf()");
00123         DLOG("Could not get legacy window name\n");
00124         free(prop);
00125         return;
00126     }
00127 
00128     LOG("WM_NAME changed to \"%s\"\n", new_name);
00129     LOG("Using legacy window title. Note that in order to get Unicode window "
00130         "titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
00131 
00132     FREE(win->name_x);
00133     FREE(win->name_json);
00134     win->name_x = new_name;
00135     win->name_json = sstrdup(new_name);
00136     win->name_len = strlen(new_name);
00137     win->name_x_changed = true;
00138 
00139     if (before_mgmt) {
00140         free(prop);
00141         return;
00142     }
00143 
00144     run_assignments(win);
00145 
00146     free(prop);
00147 }
00148 
00149 /*
00150  * Updates the CLIENT_LEADER (logical parent window).
00151  *
00152  */
00153 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
00154     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
00155         DLOG("CLIENT_LEADER not set.\n");
00156         FREE(prop);
00157         return;
00158     }
00159 
00160     xcb_window_t *leader = xcb_get_property_value(prop);
00161     if (leader == NULL) {
00162         free(prop);
00163         return;
00164     }
00165 
00166     DLOG("Client leader changed to %08x\n", *leader);
00167 
00168     win->leader = *leader;
00169 
00170     free(prop);
00171 }
00172 
00173 /*
00174  * Updates the TRANSIENT_FOR (logical parent window).
00175  *
00176  */
00177 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
00178     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
00179         DLOG("TRANSIENT_FOR not set.\n");
00180         FREE(prop);
00181         return;
00182     }
00183 
00184     xcb_window_t transient_for;
00185     if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
00186         free(prop);
00187         return;
00188     }
00189 
00190     DLOG("Transient for changed to %08x\n", transient_for);
00191 
00192     win->transient_for = transient_for;
00193 
00194     free(prop);
00195 }
00196 
00197 /*
00198  * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
00199  *
00200  */
00201 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
00202     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
00203         DLOG("_NET_WM_STRUT_PARTIAL not set.\n");
00204         FREE(prop);
00205         return;
00206     }
00207 
00208     uint32_t *strut;
00209     if (!(strut = xcb_get_property_value(prop))) {
00210         free(prop);
00211         return;
00212     }
00213 
00214     DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
00215          strut[0], strut[1], strut[2], strut[3]);
00216 
00217     win->reserved = (struct reservedpx){ strut[0], strut[1], strut[2], strut[3] };
00218 
00219     free(prop);
00220 }
00221 
00222 /*
00223  * Updates the WM_WINDOW_ROLE
00224  *
00225  */
00226 void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
00227     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
00228         DLOG("WM_WINDOW_ROLE not set.\n");
00229         FREE(prop);
00230         return;
00231     }
00232 
00233     char *new_role;
00234     if (asprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
00235                  (char*)xcb_get_property_value(prop)) == -1) {
00236         perror("asprintf()");
00237         DLOG("Could not get WM_WINDOW_ROLE\n");
00238         free(prop);
00239         return;
00240     }
00241     FREE(win->role);
00242     win->role = new_role;
00243     LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
00244 
00245     if (before_mgmt) {
00246         free(prop);
00247         return;
00248     }
00249 
00250     run_assignments(win);
00251 
00252     free(prop);
00253 }
00254 
00255 /*
00256  * Updates the WM_HINTS (we only care about the input focus handling part).
00257  *
00258  */
00259 void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop) {
00260     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
00261         DLOG("WM_HINTS not set.\n");
00262         FREE(prop);
00263         return;
00264     }
00265 
00266     xcb_icccm_wm_hints_t hints;
00267 
00268     if (!xcb_icccm_get_wm_hints_from_reply(&hints, prop)) {
00269         DLOG("Could not get WM_HINTS\n");
00270         free(prop);
00271         return;
00272     }
00273 
00274     win->doesnt_accept_focus = !hints.input;
00275     LOG("WM_HINTS.input changed to \"%d\"\n", hints.input);
00276 
00277     free(prop);
00278 }