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: 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() { 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 00153 /* We need to grab the mouse buttons for click to focus */ 00154 xcb_grab_button(conn, false, window, XCB_EVENT_MASK_BUTTON_PRESS, 00155 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE, 00156 1 /* left mouse button */, 00157 XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */); 00158 00159 xcb_grab_button(conn, false, window, XCB_EVENT_MASK_BUTTON_PRESS, 00160 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE, 00161 3 /* right mouse button */, 00162 XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */); 00163 00164 00165 /* update as much information as possible so far (some replies may be NULL) */ 00166 window_update_class(cwindow, xcb_get_property_reply(conn, class_cookie, NULL), true); 00167 window_update_name_legacy(cwindow, xcb_get_property_reply(conn, title_cookie, NULL), true); 00168 window_update_name(cwindow, xcb_get_property_reply(conn, utf8_title_cookie, NULL), true); 00169 window_update_leader(cwindow, xcb_get_property_reply(conn, leader_cookie, NULL)); 00170 window_update_transient_for(cwindow, xcb_get_property_reply(conn, transient_cookie, NULL)); 00171 window_update_strut_partial(cwindow, xcb_get_property_reply(conn, strut_cookie, NULL)); 00172 window_update_role(cwindow, xcb_get_property_reply(conn, role_cookie, NULL), true); 00173 window_update_hints(cwindow, xcb_get_property_reply(conn, wm_hints_cookie, NULL)); 00174 00175 xcb_get_property_reply_t *startup_id_reply; 00176 startup_id_reply = xcb_get_property_reply(conn, startup_id_cookie, NULL); 00177 char *startup_ws = startup_workspace_for_window(cwindow, startup_id_reply); 00178 DLOG("startup workspace = %s\n", startup_ws); 00179 00180 /* check if the window needs WM_TAKE_FOCUS */ 00181 cwindow->needs_take_focus = window_supports_protocol(cwindow->id, A_WM_TAKE_FOCUS); 00182 00183 /* Where to start searching for a container that swallows the new one? */ 00184 Con *search_at = croot; 00185 00186 xcb_get_property_reply_t *reply = xcb_get_property_reply(conn, wm_type_cookie, NULL); 00187 if (xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_DOCK)) { 00188 LOG("This window is of type dock\n"); 00189 Output *output = get_output_containing(geom->x, geom->y); 00190 if (output != NULL) { 00191 DLOG("Starting search at output %s\n", output->name); 00192 search_at = output->con; 00193 } 00194 00195 /* find out the desired position of this dock window */ 00196 if (cwindow->reserved.top > 0 && cwindow->reserved.bottom == 0) { 00197 DLOG("Top dock client\n"); 00198 cwindow->dock = W_DOCK_TOP; 00199 } else if (cwindow->reserved.top == 0 && cwindow->reserved.bottom > 0) { 00200 DLOG("Bottom dock client\n"); 00201 cwindow->dock = W_DOCK_BOTTOM; 00202 } else { 00203 DLOG("Ignoring invalid reserved edges (_NET_WM_STRUT_PARTIAL), using position as fallback:\n"); 00204 if (geom->y < (search_at->rect.height / 2)) { 00205 DLOG("geom->y = %d < rect.height / 2 = %d, it is a top dock client\n", 00206 geom->y, (search_at->rect.height / 2)); 00207 cwindow->dock = W_DOCK_TOP; 00208 } else { 00209 DLOG("geom->y = %d >= rect.height / 2 = %d, it is a bottom dock client\n", 00210 geom->y, (search_at->rect.height / 2)); 00211 cwindow->dock = W_DOCK_BOTTOM; 00212 } 00213 } 00214 } 00215 00216 DLOG("Initial geometry: (%d, %d, %d, %d)\n", geom->x, geom->y, geom->width, geom->height); 00217 00218 Con *nc = NULL; 00219 Match *match; 00220 Assignment *assignment; 00221 00222 /* TODO: two matches for one container */ 00223 00224 /* See if any container swallows this new window */ 00225 nc = con_for_window(search_at, cwindow, &match); 00226 if (nc == NULL) { 00227 /* If not, check if it is assigned to a specific workspace / output */ 00228 if ((assignment = assignment_for(cwindow, A_TO_WORKSPACE | A_TO_OUTPUT))) { 00229 DLOG("Assignment matches (%p)\n", match); 00230 if (assignment->type == A_TO_WORKSPACE) { 00231 nc = con_descend_tiling_focused(workspace_get(assignment->dest.workspace, NULL)); 00232 DLOG("focused on ws %s: %p / %s\n", assignment->dest.workspace, nc, nc->name); 00233 if (nc->type == CT_WORKSPACE) 00234 nc = tree_open_con(nc, cwindow); 00235 else nc = tree_open_con(nc->parent, cwindow); 00236 } 00237 /* TODO: handle assignments with type == A_TO_OUTPUT */ 00238 } else if (startup_ws) { 00239 /* If it’s not assigned, but was started on a specific workspace, 00240 * we want to open it there */ 00241 DLOG("Using workspace on which this application was started (%s)\n", startup_ws); 00242 nc = con_descend_tiling_focused(workspace_get(startup_ws, NULL)); 00243 DLOG("focused on ws %s: %p / %s\n", startup_ws, nc, nc->name); 00244 if (nc->type == CT_WORKSPACE) 00245 nc = tree_open_con(nc, cwindow); 00246 else nc = tree_open_con(nc->parent, cwindow); 00247 } else { 00248 /* If not, insert it at the currently focused position */ 00249 if (focused->type == CT_CON && con_accepts_window(focused)) { 00250 LOG("using current container, focused = %p, focused->name = %s\n", 00251 focused, focused->name); 00252 nc = focused; 00253 } else nc = tree_open_con(NULL, cwindow); 00254 } 00255 } else { 00256 /* M_BELOW inserts the new window as a child of the one which was 00257 * matched (e.g. dock areas) */ 00258 if (match != NULL && match->insert_where == M_BELOW) { 00259 nc = tree_open_con(nc, cwindow); 00260 } 00261 } 00262 00263 DLOG("new container = %p\n", nc); 00264 nc->window = cwindow; 00265 x_reinit(nc); 00266 00267 nc->border_width = geom->border_width; 00268 00269 char *name; 00270 sasprintf(&name, "[i3 con] container around %p", cwindow); 00271 x_set_name(nc, name); 00272 free(name); 00273 00274 Con *ws = con_get_workspace(nc); 00275 Con *fs = (ws ? con_get_fullscreen_con(ws, CF_OUTPUT) : NULL); 00276 if (fs == NULL) 00277 fs = con_get_fullscreen_con(croot, CF_GLOBAL); 00278 00279 if (fs == NULL) { 00280 DLOG("Not in fullscreen mode, focusing\n"); 00281 if (!cwindow->dock) { 00282 /* Check that the workspace is visible and on the same output as 00283 * the current focused container. If the window was assigned to an 00284 * invisible workspace, we should not steal focus. */ 00285 Con *current_output = con_get_output(focused); 00286 Con *target_output = con_get_output(ws); 00287 00288 if (workspace_is_visible(ws) && current_output == target_output) { 00289 con_focus(nc); 00290 } else DLOG("workspace not visible, not focusing\n"); 00291 } else DLOG("dock, not focusing\n"); 00292 } else { 00293 DLOG("fs = %p, ws = %p, not focusing\n", fs, ws); 00294 /* Insert the new container in focus stack *after* the currently 00295 * focused (fullscreen) con. This way, the new container will be 00296 * focused after we return from fullscreen mode */ 00297 Con *first = TAILQ_FIRST(&(nc->parent->focus_head)); 00298 if (first != nc) { 00299 /* We only modify the focus stack if the container is not already 00300 * the first one. This can happen when existing containers swallow 00301 * new windows, for example when restarting. */ 00302 TAILQ_REMOVE(&(nc->parent->focus_head), nc, focused); 00303 TAILQ_INSERT_AFTER(&(nc->parent->focus_head), first, nc, focused); 00304 } 00305 } 00306 00307 /* set floating if necessary */ 00308 bool want_floating = false; 00309 if (xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_DIALOG) || 00310 xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_UTILITY) || 00311 xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_TOOLBAR) || 00312 xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_SPLASH)) { 00313 LOG("This window is a dialog window, setting floating\n"); 00314 want_floating = true; 00315 } 00316 00317 FREE(reply); 00318 00319 if (cwindow->transient_for != XCB_NONE || 00320 (cwindow->leader != XCB_NONE && 00321 cwindow->leader != cwindow->id && 00322 con_by_window_id(cwindow->leader) != NULL)) { 00323 LOG("This window is transiert for another window, setting floating\n"); 00324 want_floating = true; 00325 00326 if (config.popup_during_fullscreen == PDF_LEAVE_FULLSCREEN && 00327 fs != NULL) { 00328 LOG("There is a fullscreen window, leaving fullscreen mode\n"); 00329 con_toggle_fullscreen(fs, CF_OUTPUT); 00330 } 00331 } 00332 00333 /* dock clients cannot be floating, that makes no sense */ 00334 if (cwindow->dock) 00335 want_floating = false; 00336 00337 /* Store the requested geometry. The width/height gets raised to at least 00338 * 75x50 when entering floating mode, which is the minimum size for a 00339 * window to be useful (smaller windows are usually overlays/toolbars/… 00340 * which are not managed by the wm anyways). We store the original geometry 00341 * here because it’s used for dock clients. */ 00342 nc->geometry = (Rect){ geom->x, geom->y, geom->width, geom->height }; 00343 00344 if (want_floating) { 00345 DLOG("geometry = %d x %d\n", nc->geometry.width, nc->geometry.height); 00346 floating_enable(nc, true); 00347 } 00348 00349 /* to avoid getting an UnmapNotify event due to reparenting, we temporarily 00350 * declare no interest in any state change event of this window */ 00351 values[0] = XCB_NONE; 00352 xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, values); 00353 00354 xcb_void_cookie_t rcookie = xcb_reparent_window_checked(conn, window, nc->frame, 0, 0); 00355 if (xcb_request_check(conn, rcookie) != NULL) { 00356 LOG("Could not reparent the window, aborting\n"); 00357 goto geom_out; 00358 } 00359 00360 values[0] = CHILD_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW; 00361 xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, values); 00362 xcb_flush(conn); 00363 00364 reply = xcb_get_property_reply(conn, state_cookie, NULL); 00365 if (xcb_reply_contains_atom(reply, A__NET_WM_STATE_FULLSCREEN)) 00366 con_toggle_fullscreen(nc, CF_OUTPUT); 00367 00368 FREE(reply); 00369 00370 /* Put the client inside the save set. Upon termination (whether killed or 00371 * normal exit does not matter) of the window manager, these clients will 00372 * be correctly reparented to their most closest living ancestor (= 00373 * cleanup) */ 00374 xcb_change_save_set(conn, XCB_SET_MODE_INSERT, window); 00375 00376 /* Check if any assignments match */ 00377 run_assignments(cwindow); 00378 00379 tree_render(); 00380 00381 geom_out: 00382 free(geom); 00383 out: 00384 free(attr); 00385 return; 00386 }