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 * tree.c: Everything that primarily modifies the layout tree data structure. 00008 * 00009 */ 00010 #include "all.h" 00011 00012 struct Con *croot; 00013 struct Con *focused; 00014 00015 struct all_cons_head all_cons = TAILQ_HEAD_INITIALIZER(all_cons); 00016 00017 /* 00018 * Create the pseudo-output __i3. Output-independent workspaces such as 00019 * __i3_scratch will live there. 00020 * 00021 */ 00022 static Con *_create___i3(void) { 00023 Con *__i3 = con_new(croot, NULL); 00024 FREE(__i3->name); 00025 __i3->name = sstrdup("__i3"); 00026 __i3->type = CT_OUTPUT; 00027 __i3->layout = L_OUTPUT; 00028 con_fix_percent(croot); 00029 x_set_name(__i3, "[i3 con] pseudo-output __i3"); 00030 /* For retaining the correct position/size of a scratchpad window, the 00031 * dimensions of the real outputs should be multiples of the __i3 00032 * pseudo-output. */ 00033 __i3->rect.width = 1280; 00034 __i3->rect.height = 1024; 00035 00036 /* Add a content container. */ 00037 DLOG("adding main content container\n"); 00038 Con *content = con_new(NULL, NULL); 00039 content->type = CT_CON; 00040 FREE(content->name); 00041 content->name = sstrdup("content"); 00042 00043 x_set_name(content, "[i3 con] content __i3"); 00044 con_attach(content, __i3, false); 00045 00046 /* Attach the __i3_scratch workspace. */ 00047 Con *ws = con_new(NULL, NULL); 00048 ws->type = CT_WORKSPACE; 00049 ws->num = -1; 00050 ws->name = sstrdup("__i3_scratch"); 00051 con_attach(ws, content, false); 00052 x_set_name(ws, "[i3 con] workspace __i3_scratch"); 00053 ws->fullscreen_mode = CF_OUTPUT; 00054 00055 return __i3; 00056 } 00057 00058 /* 00059 * Loads tree from 'path' (used for in-place restarts). 00060 * 00061 */ 00062 bool tree_restore(const char *path, xcb_get_geometry_reply_t *geometry) { 00063 char *globbed = resolve_tilde(path); 00064 00065 if (!path_exists(globbed)) { 00066 LOG("%s does not exist, not restoring tree\n", globbed); 00067 free(globbed); 00068 return false; 00069 } 00070 00071 /* TODO: refactor the following */ 00072 croot = con_new(NULL, NULL); 00073 croot->rect = (Rect){ 00074 geometry->x, 00075 geometry->y, 00076 geometry->width, 00077 geometry->height 00078 }; 00079 focused = croot; 00080 00081 tree_append_json(globbed); 00082 00083 printf("appended tree, using new root\n"); 00084 croot = TAILQ_FIRST(&(croot->nodes_head)); 00085 printf("new root = %p\n", croot); 00086 Con *out = TAILQ_FIRST(&(croot->nodes_head)); 00087 printf("out = %p\n", out); 00088 Con *ws = TAILQ_FIRST(&(out->nodes_head)); 00089 printf("ws = %p\n", ws); 00090 00091 /* For in-place restarting into v4.2, we need to make sure the new 00092 * pseudo-output __i3 is present. */ 00093 if (strcmp(out->name, "__i3") != 0) { 00094 DLOG("Adding pseudo-output __i3 during inplace restart\n"); 00095 Con *__i3 = _create___i3(); 00096 /* Ensure that it is the first output, other places in the code make 00097 * that assumption. */ 00098 TAILQ_REMOVE(&(croot->nodes_head), __i3, nodes); 00099 TAILQ_INSERT_HEAD(&(croot->nodes_head), __i3, nodes); 00100 } 00101 00102 return true; 00103 } 00104 00105 /* 00106 * Initializes the tree by creating the root node. The CT_OUTPUT Cons below the 00107 * root node are created in randr.c for each Output. 00108 * 00109 */ 00110 void tree_init(xcb_get_geometry_reply_t *geometry) { 00111 croot = con_new(NULL, NULL); 00112 FREE(croot->name); 00113 croot->name = "root"; 00114 croot->type = CT_ROOT; 00115 croot->rect = (Rect){ 00116 geometry->x, 00117 geometry->y, 00118 geometry->width, 00119 geometry->height 00120 }; 00121 00122 _create___i3(); 00123 } 00124 00125 /* 00126 * Opens an empty container in the current container 00127 * 00128 */ 00129 Con *tree_open_con(Con *con, i3Window *window) { 00130 if (con == NULL) { 00131 /* every focusable Con has a parent (outputs have parent root) */ 00132 con = focused->parent; 00133 /* If the parent is an output, we are on a workspace. In this case, 00134 * the new container needs to be opened as a leaf of the workspace. */ 00135 if (con->parent->type == CT_OUTPUT && con->type != CT_DOCKAREA) { 00136 con = focused; 00137 } 00138 00139 /* If the currently focused container is a floating container, we 00140 * attach the new container to the currently focused spot in its 00141 * workspace. */ 00142 if (con->type == CT_FLOATING_CON) { 00143 con = con_descend_tiling_focused(con->parent); 00144 if (con->type != CT_WORKSPACE) 00145 con = con->parent; 00146 } 00147 DLOG("con = %p\n", con); 00148 } 00149 00150 assert(con != NULL); 00151 00152 /* 3. create the container and attach it to its parent */ 00153 Con *new = con_new(con, window); 00154 00155 /* 4: re-calculate child->percent for each child */ 00156 con_fix_percent(con); 00157 00158 return new; 00159 } 00160 00161 static bool _is_con_mapped(Con *con) { 00162 Con *child; 00163 00164 TAILQ_FOREACH(child, &(con->nodes_head), nodes) 00165 if (_is_con_mapped(child)) 00166 return true; 00167 00168 return con->mapped; 00169 } 00170 00171 /* 00172 * Closes the given container including all children. 00173 * Returns true if the container was killed or false if just WM_DELETE was sent 00174 * and the window is expected to kill itself. 00175 * 00176 * The dont_kill_parent flag is specified when the function calls itself 00177 * recursively while deleting a containers children. 00178 * 00179 * The force_set_focus flag is specified in the case of killing a floating 00180 * window: tree_close() will be invoked for the CT_FLOATINGCON (the parent 00181 * container) and focus should be set there. 00182 * 00183 */ 00184 bool tree_close(Con *con, kill_window_t kill_window, bool dont_kill_parent, bool force_set_focus) { 00185 bool was_mapped = con->mapped; 00186 Con *parent = con->parent; 00187 00188 if (!was_mapped) { 00189 /* Even if the container itself is not mapped, its children may be 00190 * mapped (for example split containers don't have a mapped window on 00191 * their own but usually contain mapped children). */ 00192 was_mapped = _is_con_mapped(con); 00193 } 00194 00195 /* Get the container which is next focused */ 00196 Con *next = con_next_focused(con); 00197 DLOG("next = %p, focused = %p\n", next, focused); 00198 00199 DLOG("closing %p, kill_window = %d\n", con, kill_window); 00200 Con *child, *nextchild; 00201 bool abort_kill = false; 00202 /* We cannot use TAILQ_FOREACH because the children get deleted 00203 * in their parent’s nodes_head */ 00204 for (child = TAILQ_FIRST(&(con->nodes_head)); child; ) { 00205 nextchild = TAILQ_NEXT(child, nodes); 00206 DLOG("killing child=%p\n", child); 00207 if (!tree_close(child, kill_window, true, false)) 00208 abort_kill = true; 00209 child = nextchild; 00210 } 00211 00212 if (abort_kill) { 00213 DLOG("One of the children could not be killed immediately (WM_DELETE sent), aborting.\n"); 00214 return false; 00215 } 00216 00217 if (con->window != NULL) { 00218 if (kill_window != DONT_KILL_WINDOW) { 00219 x_window_kill(con->window->id, kill_window); 00220 return false; 00221 } else { 00222 xcb_void_cookie_t cookie; 00223 /* un-parent the window */ 00224 cookie = xcb_reparent_window(conn, con->window->id, root, 0, 0); 00225 00226 /* Ignore X11 errors for the ReparentWindow request. 00227 * X11 Errors are returned when the window was already destroyed */ 00228 add_ignore_event(cookie.sequence, 0); 00229 00230 /* We are no longer handling this window, thus set WM_STATE to 00231 * WM_STATE_WITHDRAWN (see ICCCM 4.1.3.1) */ 00232 long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE }; 00233 cookie = xcb_change_property(conn, XCB_PROP_MODE_REPLACE, 00234 con->window->id, A_WM_STATE, A_WM_STATE, 32, 2, data); 00235 00236 /* Ignore X11 errors for the ReparentWindow request. 00237 * X11 Errors are returned when the window was already destroyed */ 00238 add_ignore_event(cookie.sequence, 0); 00239 } 00240 FREE(con->window->class_class); 00241 FREE(con->window->class_instance); 00242 FREE(con->window->name_x); 00243 FREE(con->window->name_json); 00244 free(con->window); 00245 } 00246 00247 /* kill the X11 part of this container */ 00248 x_con_kill(con); 00249 00250 con_detach(con); 00251 if (con->type != CT_FLOATING_CON) { 00252 /* If the container is *not* floating, we might need to re-distribute 00253 * percentage values for the resized containers. */ 00254 con_fix_percent(parent); 00255 } 00256 00257 if (con_is_floating(con)) { 00258 Con *ws = con_get_workspace(con); 00259 DLOG("Container was floating, killing floating container\n"); 00260 tree_close(parent, DONT_KILL_WINDOW, false, (con == focused)); 00261 DLOG("parent container killed\n"); 00262 if (con == focused) { 00263 DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws); 00264 /* go down the focus stack as far as possible */ 00265 next = con_descend_focused(ws); 00266 00267 dont_kill_parent = true; 00268 DLOG("Alright, focusing %p\n", next); 00269 } else { 00270 next = NULL; 00271 } 00272 } 00273 00274 free(con->name); 00275 FREE(con->deco_render_params); 00276 TAILQ_REMOVE(&all_cons, con, all_cons); 00277 free(con); 00278 00279 /* in the case of floating windows, we already focused another container 00280 * when closing the parent, so we can exit now. */ 00281 if (!next) { 00282 DLOG("No next container, i will just exit now\n"); 00283 return true; 00284 } 00285 00286 if (was_mapped || con == focused) { 00287 if ((kill_window != DONT_KILL_WINDOW) || !dont_kill_parent || con == focused) { 00288 DLOG("focusing %p / %s\n", next, next->name); 00289 if (next->type == CT_DOCKAREA) { 00290 /* Instead of focusing the dockarea, we need to restore focus to the workspace */ 00291 con_focus(con_descend_focused(output_get_content(next->parent))); 00292 } else { 00293 if (!force_set_focus && con != focused) 00294 DLOG("not changing focus, the container was not focused before\n"); 00295 else con_focus(next); 00296 } 00297 } 00298 else { 00299 DLOG("not focusing because we're not killing anybody"); 00300 } 00301 } else { 00302 DLOG("not focusing, was not mapped\n"); 00303 } 00304 00305 /* check if the parent container is empty now and close it */ 00306 if (!dont_kill_parent) 00307 CALL(parent, on_remove_child); 00308 00309 return true; 00310 } 00311 00312 /* 00313 * Closes the current container using tree_close(). 00314 * 00315 */ 00316 void tree_close_con(kill_window_t kill_window) { 00317 assert(focused != NULL); 00318 if (focused->type == CT_WORKSPACE) { 00319 LOG("Cannot close workspace\n"); 00320 return; 00321 } 00322 00323 /* There *should* be no possibility to focus outputs / root container */ 00324 assert(focused->type != CT_OUTPUT); 00325 assert(focused->type != CT_ROOT); 00326 00327 /* Kill con */ 00328 tree_close(focused, kill_window, false, false); 00329 } 00330 00331 /* 00332 * Splits (horizontally or vertically) the given container by creating a new 00333 * container which contains the old one and the future ones. 00334 * 00335 */ 00336 void tree_split(Con *con, orientation_t orientation) { 00337 /* for a workspace, we just need to change orientation */ 00338 if (con->type == CT_WORKSPACE) { 00339 DLOG("Workspace, simply changing orientation to %d\n", orientation); 00340 con->orientation = orientation; 00341 return; 00342 } 00343 00344 Con *parent = con->parent; 00345 00346 /* Force re-rendering to make the indicator border visible. */ 00347 FREE(con->deco_render_params); 00348 FREE(parent->deco_render_params); 00349 00350 /* if we are in a container whose parent contains only one 00351 * child (its split functionality is unused so far), we just change the 00352 * orientation (more intuitive than splitting again) */ 00353 if (con_num_children(parent) == 1 && 00354 parent->layout == L_DEFAULT) { 00355 parent->orientation = orientation; 00356 DLOG("Just changing orientation of existing container\n"); 00357 return; 00358 } 00359 00360 DLOG("Splitting in orientation %d\n", orientation); 00361 00362 /* 2: replace it with a new Con */ 00363 Con *new = con_new(NULL, NULL); 00364 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes); 00365 TAILQ_REPLACE(&(parent->focus_head), con, new, focused); 00366 new->parent = parent; 00367 new->orientation = orientation; 00368 00369 /* 3: swap 'percent' (resize factor) */ 00370 new->percent = con->percent; 00371 con->percent = 0.0; 00372 00373 /* 4: add it as a child to the new Con */ 00374 con_attach(con, new, false); 00375 } 00376 00377 /* 00378 * Moves focus one level up. 00379 * 00380 */ 00381 void level_up(void) { 00382 /* We cannot go up when we are in fullscreen mode at the moment, that would 00383 * be totally not intuitive */ 00384 if (focused->fullscreen_mode != CF_NONE) { 00385 LOG("Currently in fullscreen, not going up\n"); 00386 return; 00387 } 00388 /* We can focus up to the workspace, but not any higher in the tree */ 00389 if ((focused->parent->type != CT_CON && 00390 focused->parent->type != CT_WORKSPACE) || 00391 focused->type == CT_WORKSPACE) { 00392 LOG("Cannot go up any further\n"); 00393 return; 00394 } 00395 con_focus(focused->parent); 00396 } 00397 00398 /* 00399 * Moves focus one level down. 00400 * 00401 */ 00402 void level_down(void) { 00403 /* Go down the focus stack of the current node */ 00404 Con *next = TAILQ_FIRST(&(focused->focus_head)); 00405 if (next == TAILQ_END(&(focused->focus_head))) { 00406 printf("cannot go down\n"); 00407 return; 00408 } 00409 con_focus(next); 00410 } 00411 00412 static void mark_unmapped(Con *con) { 00413 Con *current; 00414 00415 con->mapped = false; 00416 TAILQ_FOREACH(current, &(con->nodes_head), nodes) 00417 mark_unmapped(current); 00418 if (con->type == CT_WORKSPACE) { 00419 /* We need to call mark_unmapped on floating nodes aswell since we can 00420 * make containers floating. */ 00421 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) 00422 mark_unmapped(current); 00423 } 00424 } 00425 00426 /* 00427 * Renders the tree, that is rendering all outputs using render_con() and 00428 * pushing the changes to X11 using x_push_changes(). 00429 * 00430 */ 00431 void tree_render(void) { 00432 if (croot == NULL) 00433 return; 00434 00435 DLOG("-- BEGIN RENDERING --\n"); 00436 /* Reset map state for all nodes in tree */ 00437 /* TODO: a nicer method to walk all nodes would be good, maybe? */ 00438 mark_unmapped(croot); 00439 croot->mapped = true; 00440 00441 render_con(croot, false); 00442 00443 x_push_changes(croot); 00444 DLOG("-- END RENDERING --\n"); 00445 } 00446 00447 /* 00448 * Recursive function to walk the tree until a con can be found to focus. 00449 * 00450 */ 00451 static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap) { 00452 /* Stop recursing at workspaces after attempting to switch to next 00453 * workspace if possible. */ 00454 if (con->type == CT_WORKSPACE) { 00455 Output *current_output = get_output_containing(con->rect.x, con->rect.y); 00456 Output *next_output; 00457 00458 if (!current_output) 00459 return false; 00460 DLOG("Current output is %s\n", current_output->name); 00461 00462 /* Try to find next output */ 00463 direction_t direction; 00464 if (way == 'n' && orientation == HORIZ) 00465 direction = D_RIGHT; 00466 else if (way == 'p' && orientation == HORIZ) 00467 direction = D_LEFT; 00468 else if (way == 'n' && orientation == VERT) 00469 direction = D_DOWN; 00470 else if (way == 'p' && orientation == VERT) 00471 direction = D_UP; 00472 else 00473 return false; 00474 00475 next_output = get_output_next(direction, current_output); 00476 if (!next_output) 00477 return false; 00478 DLOG("Next output is %s\n", next_output->name); 00479 00480 /* Find visible workspace on next output */ 00481 Con *workspace = NULL; 00482 GREP_FIRST(workspace, output_get_content(next_output->con), workspace_is_visible(child)); 00483 00484 /* Show next workspace and focus appropriate container if possible. */ 00485 if (!workspace) 00486 return false; 00487 00488 workspace_show(workspace); 00489 Con *focus = con_descend_direction(workspace, direction); 00490 if (focus) { 00491 con_focus(focus); 00492 x_set_warp_to(&(focus->rect)); 00493 } 00494 return true; 00495 } 00496 00497 Con *parent = con->parent; 00498 00499 if (con->type == CT_FLOATING_CON) { 00500 /* left/right focuses the previous/next floating container */ 00501 if (orientation == HORIZ) { 00502 Con *next; 00503 if (way == 'n') 00504 next = TAILQ_NEXT(con, floating_windows); 00505 else next = TAILQ_PREV(con, floating_head, floating_windows); 00506 00507 /* If there is no next/previous container, wrap */ 00508 if (!next) { 00509 if (way == 'n') 00510 next = TAILQ_FIRST(&(parent->floating_head)); 00511 else next = TAILQ_LAST(&(parent->floating_head), floating_head); 00512 } 00513 00514 /* Still no next/previous container? bail out */ 00515 if (!next) 00516 return false; 00517 00518 con_focus(con_descend_focused(next)); 00519 return true; 00520 } else { 00521 /* up/down cycles through the Z-index */ 00522 /* TODO: implement cycling through the z-index */ 00523 return false; 00524 } 00525 } 00526 00527 /* If the orientation does not match or there is no other con to focus, we 00528 * need to go higher in the hierarchy */ 00529 if (con_orientation(parent) != orientation || 00530 con_num_children(parent) == 1) 00531 return _tree_next(parent, way, orientation, wrap); 00532 00533 Con *current = TAILQ_FIRST(&(parent->focus_head)); 00534 /* TODO: when can the following happen (except for floating windows, which 00535 * are handled above)? */ 00536 if (TAILQ_EMPTY(&(parent->nodes_head))) { 00537 DLOG("nothing to focus\n"); 00538 return false; 00539 } 00540 00541 Con *next; 00542 if (way == 'n') 00543 next = TAILQ_NEXT(current, nodes); 00544 else next = TAILQ_PREV(current, nodes_head, nodes); 00545 00546 if (!next) { 00547 if (!config.force_focus_wrapping) { 00548 /* If there is no next/previous container, we check if we can focus one 00549 * when going higher (without wrapping, though). If so, we are done, if 00550 * not, we wrap */ 00551 if (_tree_next(parent, way, orientation, false)) 00552 return true; 00553 00554 if (!wrap) 00555 return false; 00556 } 00557 00558 if (way == 'n') 00559 next = TAILQ_FIRST(&(parent->nodes_head)); 00560 else next = TAILQ_LAST(&(parent->nodes_head), nodes_head); 00561 } 00562 00563 /* 3: focus choice comes in here. at the moment we will go down 00564 * until we find a window */ 00565 /* TODO: check for window, atm we only go down as far as possible */ 00566 con_focus(con_descend_focused(next)); 00567 return true; 00568 } 00569 00570 /* 00571 * Changes focus in the given way (next/previous) and given orientation 00572 * (horizontal/vertical). 00573 * 00574 */ 00575 void tree_next(char way, orientation_t orientation) { 00576 _tree_next(focused, way, orientation, true); 00577 } 00578 00579 /* 00580 * tree_flatten() removes pairs of redundant split containers, e.g.: 00581 * [workspace, horizontal] 00582 * [v-split] [child3] 00583 * [h-split] 00584 * [child1] [child2] 00585 * In this example, the v-split and h-split container are redundant. 00586 * Such a situation can be created by moving containers in a direction which is 00587 * not the orientation of their parent container. i3 needs to create a new 00588 * split container then and if you move containers this way multiple times, 00589 * redundant chains of split-containers can be the result. 00590 * 00591 */ 00592 void tree_flatten(Con *con) { 00593 Con *current, *child, *parent = con->parent; 00594 DLOG("Checking if I can flatten con = %p / %s\n", con, con->name); 00595 00596 /* We only consider normal containers without windows */ 00597 if (con->type != CT_CON || con->window != NULL) 00598 goto recurse; 00599 00600 /* Ensure it got only one child */ 00601 child = TAILQ_FIRST(&(con->nodes_head)); 00602 if (child == NULL || TAILQ_NEXT(child, nodes) != NULL) 00603 goto recurse; 00604 00605 /* The child must have a different orientation than the con but the same as 00606 * the con’s parent to be redundant */ 00607 if (con->orientation == NO_ORIENTATION || 00608 child->orientation == NO_ORIENTATION || 00609 con->orientation == child->orientation || 00610 child->orientation != parent->orientation) 00611 goto recurse; 00612 00613 DLOG("Alright, I have to flatten this situation now. Stay calm.\n"); 00614 /* 1: save focus */ 00615 Con *focus_next = TAILQ_FIRST(&(child->focus_head)); 00616 00617 DLOG("detaching...\n"); 00618 /* 2: re-attach the children to the parent before con */ 00619 while (!TAILQ_EMPTY(&(child->nodes_head))) { 00620 current = TAILQ_FIRST(&(child->nodes_head)); 00621 DLOG("detaching current=%p / %s\n", current, current->name); 00622 con_detach(current); 00623 DLOG("re-attaching\n"); 00624 /* We don’t use con_attach() here because for a CT_CON, the special 00625 * case handling of con_attach() does not trigger. So all it would do 00626 * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we 00627 * directly use the TAILQ macros. */ 00628 current->parent = parent; 00629 TAILQ_INSERT_BEFORE(con, current, nodes); 00630 DLOG("attaching to focus list\n"); 00631 TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused); 00632 current->percent = con->percent; 00633 } 00634 DLOG("re-attached all\n"); 00635 00636 /* 3: restore focus, if con was focused */ 00637 if (focus_next != NULL && 00638 TAILQ_FIRST(&(parent->focus_head)) == con) { 00639 DLOG("restoring focus to focus_next=%p\n", focus_next); 00640 TAILQ_REMOVE(&(parent->focus_head), focus_next, focused); 00641 TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused); 00642 DLOG("restored focus.\n"); 00643 } 00644 00645 /* 4: close the redundant cons */ 00646 DLOG("closing redundant cons\n"); 00647 tree_close(con, DONT_KILL_WINDOW, true, false); 00648 00649 /* Well, we got to abort the recursion here because we destroyed the 00650 * container. However, if tree_flatten() is called sufficiently often, 00651 * there can’t be the situation of having two pairs of redundant containers 00652 * at once. Therefore, we can safely abort the recursion on this level 00653 * after flattening. */ 00654 return; 00655 00656 recurse: 00657 /* We cannot use normal foreach here because tree_flatten might close the 00658 * current container. */ 00659 current = TAILQ_FIRST(&(con->nodes_head)); 00660 while (current != NULL) { 00661 Con *next = TAILQ_NEXT(current, nodes); 00662 tree_flatten(current); 00663 current = next; 00664 } 00665 00666 current = TAILQ_FIRST(&(con->floating_head)); 00667 while (current != NULL) { 00668 Con *next = TAILQ_NEXT(current, floating_windows); 00669 tree_flatten(current); 00670 current = next; 00671 } 00672 }