i3
|
00001 /* 00002 * vim:ts=4:sw=4:expandtab 00003 * 00004 * i3 - an improved dynamic tiling window manager 00005 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE) 00006 * 00007 * manage.c: Contains all functions for initially managing new windows 00008 * (or existing ones on restart). 00009 * 00010 */ 00011 00012 #include "all.h" 00013 00014 /* 00015 * Go through all existing windows (if the window manager is restarted) and manage them 00016 * 00017 */ 00018 void manage_existing_windows(xcb_window_t root) { 00019 xcb_query_tree_reply_t *reply; 00020 int i, len; 00021 xcb_window_t *children; 00022 xcb_get_window_attributes_cookie_t *cookies; 00023 00024 /* Get the tree of windows whose parent is the root window (= all) */ 00025 if ((reply = xcb_query_tree_reply(conn, xcb_query_tree(conn, root), 0)) == NULL) 00026 return; 00027 00028 len = xcb_query_tree_children_length(reply); 00029 cookies = smalloc(len * sizeof(*cookies)); 00030 00031 /* Request the window attributes for every window */ 00032 children = xcb_query_tree_children(reply); 00033 for (i = 0; i < len; ++i) 00034 cookies[i] = xcb_get_window_attributes(conn, children[i]); 00035 00036 /* Call manage_window with the attributes for every window */ 00037 for (i = 0; i < len; ++i) 00038 manage_window(children[i], cookies[i], true); 00039 00040 free(reply); 00041 free(cookies); 00042 } 00043 00044 /* 00045 * Restores the geometry of each window by reparenting it to the root window 00046 * at the position of its frame. 00047 * 00048 * This is to be called *only* before exiting/restarting i3 because of evil 00049 * side-effects which are to be expected when continuing to run i3. 00050 * 00051 */ 00052 void restore_geometry() { 00053 DLOG("Restoring geometry\n"); 00054 00055 Con *con; 00056 TAILQ_FOREACH(con, &all_cons, all_cons) 00057 if (con->window) { 00058 DLOG("Re-adding X11 border of %d px\n", con->border_width); 00059 con->window_rect.width += (2 * con->border_width); 00060 con->window_rect.height += (2 * con->border_width); 00061 xcb_set_window_rect(conn, con->window->id, con->window_rect); 00062 DLOG("placing window %08x at %d %d\n", con->window->id, con->rect.x, con->rect.y); 00063 xcb_reparent_window(conn, con->window->id, root, 00064 con->rect.x, con->rect.y); 00065 } 00066 00067 /* Make sure our changes reach the X server, we restart/exit now */ 00068 xcb_flush(conn); 00069 } 00070 00071 /* 00072 * Do some sanity checks and then reparent the window. 00073 * 00074 */ 00075 void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cookie, 00076 bool needs_to_be_mapped) { 00077 xcb_drawable_t d = { window }; 00078 xcb_get_geometry_cookie_t geomc; 00079 xcb_get_geometry_reply_t *geom; 00080 xcb_get_window_attributes_reply_t *attr = NULL; 00081 00082 DLOG("---> looking at window 0x%08x\n", window); 00083 00084 xcb_get_property_cookie_t wm_type_cookie, strut_cookie, state_cookie, 00085 utf8_title_cookie, title_cookie, 00086 class_cookie, leader_cookie, transient_cookie; 00087 00088 00089 geomc = xcb_get_geometry(conn, d); 00090 #define FREE_GEOMETRY() do { \ 00091 if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) != NULL) \ 00092 free(geom); \ 00093 } while (0) 00094 00095 /* Check if the window is mapped (it could be not mapped when intializing and 00096 calling manage_window() for every window) */ 00097 if ((attr = xcb_get_window_attributes_reply(conn, cookie, 0)) == NULL) { 00098 DLOG("Could not get attributes\n"); 00099 FREE_GEOMETRY(); 00100 return; 00101 } 00102 00103 if (needs_to_be_mapped && attr->map_state != XCB_MAP_STATE_VIEWABLE) { 00104 DLOG("map_state unviewable\n"); 00105 FREE_GEOMETRY(); 00106 goto out; 00107 } 00108 00109 /* Don’t manage clients with the override_redirect flag */ 00110 DLOG("override_redirect is %d\n", attr->override_redirect); 00111 if (attr->override_redirect) { 00112 FREE_GEOMETRY(); 00113 goto out; 00114 } 00115 00116 /* Check if the window is already managed */ 00117 if (con_by_window_id(window) != NULL) { 00118 DLOG("already managed (by con %p)\n", con_by_window_id(window)); 00119 FREE_GEOMETRY(); 00120 goto out; 00121 } 00122 00123 /* Get the initial geometry (position, size, …) */ 00124 if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL) { 00125 DLOG("could not get geometry\n"); 00126 goto out; 00127 } 00128 00129 uint32_t values[1]; 00130 00131 /* Set a temporary event mask for the new window, consisting only of 00132 * PropertyChange. We need to be notified of PropertyChanges because the 00133 * client can change its properties *after* we requested them but *before* 00134 * we actually reparented it and have set our final event mask. */ 00135 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE; 00136 xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, values); 00137 00138 #define GET_PROPERTY(atom, len) xcb_get_property(conn, false, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, len) 00139 00140 wm_type_cookie = GET_PROPERTY(A__NET_WM_WINDOW_TYPE, UINT32_MAX); 00141 strut_cookie = GET_PROPERTY(A__NET_WM_STRUT_PARTIAL, UINT32_MAX); 00142 state_cookie = GET_PROPERTY(A__NET_WM_STATE, UINT32_MAX); 00143 utf8_title_cookie = GET_PROPERTY(A__NET_WM_NAME, 128); 00144 leader_cookie = GET_PROPERTY(A_WM_CLIENT_LEADER, UINT32_MAX); 00145 transient_cookie = GET_PROPERTY(A_WM_TRANSIENT_FOR, UINT32_MAX); 00146 title_cookie = GET_PROPERTY(A_WM_NAME, 128); 00147 class_cookie = GET_PROPERTY(A_WM_CLASS, 128); 00148 /* TODO: also get wm_normal_hints here. implement after we got rid of xcb-event */ 00149 00150 DLOG("reparenting!\n"); 00151 00152 i3Window *cwindow = scalloc(sizeof(i3Window)); 00153 cwindow->id = window; 00154 00155 /* We need to grab the mouse buttons for click to focus */ 00156 xcb_grab_button(conn, false, window, XCB_EVENT_MASK_BUTTON_PRESS, 00157 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE, 00158 1 /* left mouse button */, 00159 XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */); 00160 00161 xcb_grab_button(conn, false, window, XCB_EVENT_MASK_BUTTON_PRESS, 00162 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE, 00163 3 /* right mouse button */, 00164 XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */); 00165 00166 00167 /* update as much information as possible so far (some replies may be NULL) */ 00168 window_update_class(cwindow, xcb_get_property_reply(conn, class_cookie, NULL), true); 00169 window_update_name_legacy(cwindow, xcb_get_property_reply(conn, title_cookie, NULL), true); 00170 window_update_name(cwindow, xcb_get_property_reply(conn, utf8_title_cookie, NULL), true); 00171 window_update_leader(cwindow, xcb_get_property_reply(conn, leader_cookie, NULL)); 00172 window_update_transient_for(cwindow, xcb_get_property_reply(conn, transient_cookie, NULL)); 00173 window_update_strut_partial(cwindow, xcb_get_property_reply(conn, strut_cookie, NULL)); 00174 00175 /* check if the window needs WM_TAKE_FOCUS */ 00176 cwindow->needs_take_focus = window_supports_protocol(cwindow->id, A_WM_TAKE_FOCUS); 00177 00178 /* Where to start searching for a container that swallows the new one? */ 00179 Con *search_at = croot; 00180 00181 xcb_get_property_reply_t *reply = xcb_get_property_reply(conn, wm_type_cookie, NULL); 00182 if (xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_DOCK)) { 00183 LOG("This window is of type dock\n"); 00184 Output *output = get_output_containing(geom->x, geom->y); 00185 if (output != NULL) { 00186 DLOG("Starting search at output %s\n", output->name); 00187 search_at = output->con; 00188 } 00189 00190 /* find out the desired position of this dock window */ 00191 if (cwindow->reserved.top > 0 && cwindow->reserved.bottom == 0) { 00192 DLOG("Top dock client\n"); 00193 cwindow->dock = W_DOCK_TOP; 00194 } else if (cwindow->reserved.top == 0 && cwindow->reserved.bottom > 0) { 00195 DLOG("Bottom dock client\n"); 00196 cwindow->dock = W_DOCK_BOTTOM; 00197 } else { 00198 DLOG("Ignoring invalid reserved edges (_NET_WM_STRUT_PARTIAL), using position as fallback:\n"); 00199 if (geom->y < (search_at->rect.height / 2)) { 00200 DLOG("geom->y = %d < rect.height / 2 = %d, it is a top dock client\n", 00201 geom->y, (search_at->rect.height / 2)); 00202 cwindow->dock = W_DOCK_TOP; 00203 } else { 00204 DLOG("geom->y = %d >= rect.height / 2 = %d, it is a bottom dock client\n", 00205 geom->y, (search_at->rect.height / 2)); 00206 cwindow->dock = W_DOCK_BOTTOM; 00207 } 00208 } 00209 } 00210 00211 DLOG("Initial geometry: (%d, %d, %d, %d)\n", geom->x, geom->y, geom->width, geom->height); 00212 00213 Con *nc = NULL; 00214 Match *match; 00215 Assignment *assignment; 00216 00217 /* TODO: two matches for one container */ 00218 00219 /* See if any container swallows this new window */ 00220 nc = con_for_window(search_at, cwindow, &match); 00221 if (nc == NULL) { 00222 /* If not, check if it is assigned to a specific workspace / output */ 00223 if ((assignment = assignment_for(cwindow, A_TO_WORKSPACE | A_TO_OUTPUT))) { 00224 DLOG("Assignment matches (%p)\n", match); 00225 if (assignment->type == A_TO_WORKSPACE) { 00226 nc = con_descend_focused(workspace_get(assignment->dest.workspace, NULL)); 00227 DLOG("focused on ws %s: %p / %s\n", assignment->dest.workspace, nc, nc->name); 00228 if (nc->type == CT_WORKSPACE) 00229 nc = tree_open_con(nc, cwindow); 00230 else nc = tree_open_con(nc->parent, cwindow); 00231 } 00232 /* TODO: handle assignments with type == A_TO_OUTPUT */ 00233 } else { 00234 /* If not, insert it at the currently focused position */ 00235 if (focused->type == CT_CON && con_accepts_window(focused)) { 00236 LOG("using current container, focused = %p, focused->name = %s\n", 00237 focused, focused->name); 00238 nc = focused; 00239 } else nc = tree_open_con(NULL, cwindow); 00240 } 00241 } else { 00242 /* M_BELOW inserts the new window as a child of the one which was 00243 * matched (e.g. dock areas) */ 00244 if (match != NULL && match->insert_where == M_BELOW) { 00245 nc = tree_open_con(nc, cwindow); 00246 } 00247 } 00248 00249 DLOG("new container = %p\n", nc); 00250 nc->window = cwindow; 00251 x_reinit(nc); 00252 00253 nc->border_width = geom->border_width; 00254 00255 char *name; 00256 asprintf(&name, "[i3 con] container around %p", cwindow); 00257 x_set_name(nc, name); 00258 free(name); 00259 00260 Con *ws = con_get_workspace(nc); 00261 Con *fs = (ws ? con_get_fullscreen_con(ws, CF_OUTPUT) : NULL); 00262 if (fs == NULL) 00263 fs = con_get_fullscreen_con(croot, CF_GLOBAL); 00264 00265 if (fs == NULL) { 00266 DLOG("Not in fullscreen mode, focusing\n"); 00267 if (!cwindow->dock) 00268 con_focus(nc); 00269 else DLOG("dock, not focusing\n"); 00270 } else { 00271 DLOG("fs = %p, ws = %p, not focusing\n", fs, ws); 00272 /* Insert the new container in focus stack *after* the currently 00273 * focused (fullscreen) con. This way, the new container will be 00274 * focused after we return from fullscreen mode */ 00275 Con *first = TAILQ_FIRST(&(nc->parent->focus_head)); 00276 if (first != nc) { 00277 /* We only modify the focus stack if the container is not already 00278 * the first one. This can happen when existing containers swallow 00279 * new windows, for example when restarting. */ 00280 TAILQ_REMOVE(&(nc->parent->focus_head), nc, focused); 00281 TAILQ_INSERT_AFTER(&(nc->parent->focus_head), first, nc, focused); 00282 } 00283 } 00284 00285 /* set floating if necessary */ 00286 bool want_floating = false; 00287 if (xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_DIALOG) || 00288 xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_UTILITY) || 00289 xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_TOOLBAR) || 00290 xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_SPLASH)) { 00291 LOG("This window is a dialog window, setting floating\n"); 00292 want_floating = true; 00293 } 00294 00295 FREE(reply); 00296 00297 if (cwindow->transient_for != XCB_NONE || 00298 (cwindow->leader != XCB_NONE && 00299 cwindow->leader != cwindow->id && 00300 con_by_window_id(cwindow->leader) != NULL)) { 00301 LOG("This window is transiert for another window, setting floating\n"); 00302 want_floating = true; 00303 00304 if (config.popup_during_fullscreen == PDF_LEAVE_FULLSCREEN && 00305 fs != NULL) { 00306 LOG("There is a fullscreen window, leaving fullscreen mode\n"); 00307 con_toggle_fullscreen(fs, CF_OUTPUT); 00308 } 00309 } 00310 00311 /* dock clients cannot be floating, that makes no sense */ 00312 if (cwindow->dock) 00313 want_floating = false; 00314 00315 /* Store the requested geometry. The width/height gets raised to at least 00316 * 75x50 when entering floating mode, which is the minimum size for a 00317 * window to be useful (smaller windows are usually overlays/toolbars/… 00318 * which are not managed by the wm anyways). We store the original geometry 00319 * here because it’s used for dock clients. */ 00320 nc->geometry = (Rect){ geom->x, geom->y, geom->width, geom->height }; 00321 00322 if (want_floating) { 00323 DLOG("geometry = %d x %d\n", nc->geometry.width, nc->geometry.height); 00324 floating_enable(nc, false); 00325 } 00326 00327 /* to avoid getting an UnmapNotify event due to reparenting, we temporarily 00328 * declare no interest in any state change event of this window */ 00329 values[0] = XCB_NONE; 00330 xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, values); 00331 00332 xcb_void_cookie_t rcookie = xcb_reparent_window_checked(conn, window, nc->frame, 0, 0); 00333 if (xcb_request_check(conn, rcookie) != NULL) { 00334 LOG("Could not reparent the window, aborting\n"); 00335 goto geom_out; 00336 } 00337 00338 values[0] = CHILD_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW; 00339 xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, values); 00340 xcb_flush(conn); 00341 00342 reply = xcb_get_property_reply(conn, state_cookie, NULL); 00343 if (xcb_reply_contains_atom(reply, A__NET_WM_STATE_FULLSCREEN)) 00344 con_toggle_fullscreen(nc, CF_OUTPUT); 00345 00346 FREE(reply); 00347 00348 /* Put the client inside the save set. Upon termination (whether killed or 00349 * normal exit does not matter) of the window manager, these clients will 00350 * be correctly reparented to their most closest living ancestor (= 00351 * cleanup) */ 00352 xcb_change_save_set(conn, XCB_SET_MODE_INSERT, window); 00353 00354 /* Check if any assignments match */ 00355 run_assignments(cwindow); 00356 00357 tree_render(); 00358 00359 geom_out: 00360 free(geom); 00361 out: 00362 free(attr); 00363 return; 00364 }