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