2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * move.c: Moving containers into some direction.
12 typedef enum { BEFORE,
16 * Returns the lowest container in the tree that has both a and b as descendants.
19 static Con *lowest_common_ancestor(Con *a, Con *b) {
24 if (parent_a == parent_b) {
27 parent_b = parent_b->parent;
29 parent_a = parent_a->parent;
35 * Returns the direct child of ancestor that contains con.
38 static Con *child_containing_con_recursively(Con *ancestor, Con *con) {
40 while (child && child->parent != ancestor) {
41 child = child->parent;
42 assert(child->parent);
48 * Returns true if the given container is the focused descendant of ancestor, recursively.
51 static bool is_focused_descendant(Con *con, Con *ancestor) {
53 while (current != ancestor) {
54 if (TAILQ_FIRST(&(current->parent->focus_head)) != current) {
57 current = current->parent;
58 assert(current->parent);
64 * This function detaches 'con' from its parent and inserts it either before or
68 static void insert_con_into(Con *con, Con *target, position_t position) {
69 Con *parent = target->parent;
70 /* We need to preserve the old con->parent. While it might still be used to
71 * insert the entry before/after it, we call the on_remove_child callback
72 * afterwards which might then close the con if it is empty. */
73 Con *old_parent = con->parent;
75 /* We compare the focus order of the children of the lowest common ancestor. If con or
76 * its ancestor is before target's ancestor then con should be placed before the target
77 * in the focus stack. */
78 Con *lca = lowest_common_ancestor(con, parent);
80 ELOG("Container is being inserted into one of its descendants.\n");
84 Con *con_ancestor = child_containing_con_recursively(lca, con);
85 Con *target_ancestor = child_containing_con_recursively(lca, target);
86 bool moves_focus_from_ancestor = is_focused_descendant(con, con_ancestor);
89 /* Determine if con is going to be placed before or after target in the parent's focus stack. */
90 if (con_ancestor == target_ancestor) {
91 /* Happens when the target is con's old parent. Eg with layout V [ A H [ B C ] ],
92 * if we move C up. Target will be H. */
93 focus_before = moves_focus_from_ancestor;
95 /* Look at the focus stack order of the children of the lowest common ancestor. */
97 TAILQ_FOREACH(current, &(lca->focus_head), focused) {
98 if (current == con_ancestor || current == target_ancestor) {
102 focus_before = (current == con_ancestor);
105 /* If con is the focused container in our old ancestor we place the new ancestor
106 * before the old ancestor in the focus stack. Example:
107 * Consider the layout [ H [ V1 [ A* B ] V2 [ C ] ] ] where A is focused. We move to
108 * a second workspace and from there we move A to the right and switch back to the
109 * original workspace. Without the change focus would move to B instead of staying
111 if (moves_focus_from_ancestor && focus_before) {
112 Con *place = TAILQ_PREV(con_ancestor, focus_head, focused);
113 TAILQ_REMOVE(&(lca->focus_head), target_ancestor, focused);
115 TAILQ_INSERT_AFTER(&(lca->focus_head), place, target_ancestor, focused);
117 TAILQ_INSERT_HEAD(&(lca->focus_head), target_ancestor, focused);
122 con_fix_percent(con->parent);
124 /* When moving to a workspace, we respect the user’s configured
125 * workspace_layout */
126 if (parent->type == CT_WORKSPACE) {
127 Con *split = workspace_attach_to(parent);
128 if (split != parent) {
129 DLOG("Got a new split con, using that one instead\n");
131 con_attach(con, split, false);
134 con_fix_percent(split);
136 DLOG("ok, continuing with con %p instead\n", con);
141 con->parent = parent;
145 /* Example layout: H [ A B* ], we move A up/down. 'target' will be H. */
146 TAILQ_INSERT_BEFORE(target, con, focused);
148 /* Example layout: H [ A B* ], we move A up/down. 'target' will be H. */
149 TAILQ_INSERT_AFTER(&(parent->focus_head), target, con, focused);
153 /* Example layout: V [ H [ A B ] C* ], we move C up. 'target' will be A. */
154 TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
156 /* Example layout: V [ H [ A* B ] C ], we move C up. 'target' will be A. */
157 TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
161 if (position == BEFORE) {
162 TAILQ_INSERT_BEFORE(target, con, nodes);
163 } else if (position == AFTER) {
164 TAILQ_INSERT_AFTER(&(parent->nodes_head), target, con, nodes);
167 /* Pretend the con was just opened with regards to size percent values.
168 * Since the con is moved to a completely different con, the old value
169 * does not make sense anyways. */
171 con_fix_percent(parent);
173 CALL(old_parent, on_remove_child);
177 * This function detaches 'con' from its parent and puts it in the given
178 * workspace. Position is determined by the direction of movement into the
179 * workspace container.
182 static void attach_to_workspace(Con *con, Con *ws, direction_t direction) {
184 con_fix_percent(con->parent);
186 CALL(con->parent, on_remove_child);
190 if (direction == D_RIGHT || direction == D_DOWN) {
191 TAILQ_INSERT_HEAD(&(ws->nodes_head), con, nodes);
192 TAILQ_INSERT_HEAD(&(ws->focus_head), con, focused);
194 TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
195 TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
198 /* Pretend the con was just opened with regards to size percent values.
199 * Since the con is moved to a completely different con, the old value
200 * does not make sense anyways. */
206 * Moves the given container to the closest output in the given direction if
207 * such an output exists.
210 static void move_to_output_directed(Con *con, direction_t direction) {
211 Con *old_ws = con_get_workspace(con);
212 Output *current_output = get_output_for_con(con);
213 Output *output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
216 DLOG("No output in this direction found. Not moving.\n");
221 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
224 DLOG("No workspace on output in this direction found. Not moving.\n");
228 attach_to_workspace(con, ws, direction);
230 /* fix the focus stack */
233 /* force re-painting the indicators */
234 FREE(con->deco_render_params);
238 ipc_send_workspace_event("focus", ws, old_ws);
242 * Moves the given container in the given direction (D_LEFT, D_RIGHT,
246 void tree_move(Con *con, int direction) {
250 DLOG("Moving in direction %d\n", direction);
252 /* 1: get the first parent with the same orientation */
254 if (con->type == CT_WORKSPACE) {
255 DLOG("Not moving workspace\n");
259 if (con->fullscreen_mode == CF_GLOBAL) {
260 DLOG("Not moving fullscreen global container\n");
264 if ((con->fullscreen_mode == CF_OUTPUT) ||
265 (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1)) {
266 /* This is the only con on this workspace */
267 move_to_output_directed(con, direction);
271 orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT);
273 Con *same_orientation = con_parent_with_orientation(con, o);
274 /* The do {} while is used to 'restart' at this point with a different
275 * same_orientation, see the very last lines before the end of this block
278 /* There is no parent container with the same orientation */
279 if (!same_orientation) {
280 if (con_is_floating(con)) {
281 /* this is a floating con, we just disable floating */
282 floating_disable(con, true);
285 if (con_inside_floating(con)) {
286 /* 'con' should be moved out of a floating container */
287 DLOG("Inside floating, moving to workspace\n");
288 attach_to_workspace(con, con_get_workspace(con), direction);
291 DLOG("Force-changing orientation\n");
292 ws_force_orientation(con_get_workspace(con), o);
293 same_orientation = con_parent_with_orientation(con, o);
296 /* easy case: the move is within this container */
297 if (same_orientation == con->parent) {
298 DLOG("We are in the same container\n");
300 if ((swap = (direction == D_LEFT || direction == D_UP ? TAILQ_PREV(con, nodes_head, nodes) : TAILQ_NEXT(con, nodes)))) {
301 if (!con_is_leaf(swap)) {
302 DLOG("Moving into our bordering branch\n");
303 target = con_descend_direction(swap, direction);
304 position = (con_orientation(target->parent) != o ||
309 insert_con_into(con, target, position);
312 if (direction == D_LEFT || direction == D_UP)
313 TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
315 TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
317 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
318 TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
321 ipc_send_window_event("move", con);
322 ewmh_update_wm_desktop();
326 if (con->parent == con_get_workspace(con)) {
327 /* If we couldn't find a place to move it on this workspace,
328 * try to move it to a workspace on a different output */
329 move_to_output_directed(con, direction);
330 ipc_send_window_event("move", con);
331 ewmh_update_wm_desktop();
335 /* If there was no con with which we could swap the current one,
336 * search again, but starting one level higher. */
337 same_orientation = con_parent_with_orientation(con->parent, o);
339 } while (same_orientation == NULL);
341 /* this time, we have to move to another container */
342 /* This is the container *above* 'con' (an ancestor of con) which is inside
343 * 'same_orientation' */
345 while (above->parent != same_orientation)
346 above = above->parent;
348 /* Enforce the fullscreen focus restrictions. */
349 if (!con_fullscreen_permits_focusing(above->parent)) {
350 LOG("Cannot move out of fullscreen container\n");
354 DLOG("above = %p\n", above);
356 Con *next = (direction == D_UP || direction == D_LEFT ? TAILQ_PREV(above, nodes_head, nodes) : TAILQ_NEXT(above, nodes));
358 if (next && !con_is_leaf(next)) {
359 DLOG("Moving into the bordering branch of our adjacent container\n");
360 target = con_descend_direction(next, direction);
361 position = (con_orientation(target->parent) != o ||
366 insert_con_into(con, target, position);
368 con->parent->parent->type == CT_WORKSPACE &&
369 con->parent->layout != L_DEFAULT &&
370 con_num_children(con->parent) == 1) {
371 /* Con is the lone child of a non-default layout container at the edge
372 * of the workspace. Treat it as though the workspace is its parent
373 * and move it to the next output. */
374 DLOG("Grandparent is workspace\n");
375 move_to_output_directed(con, direction);
377 DLOG("Moving into container above\n");
378 position = (direction == D_UP || direction == D_LEFT ? BEFORE : AFTER);
379 insert_con_into(con, above, position);
383 /* force re-painting the indicators */
384 FREE(con->deco_render_params);
387 ipc_send_window_event("move", con);
388 ewmh_update_wm_desktop();