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.
13 * Returns the lowest container in the tree that has both a and b as descendants.
16 static Con *lowest_common_ancestor(Con *a, Con *b) {
21 if (parent_a == parent_b) {
24 parent_b = parent_b->parent;
26 parent_a = parent_a->parent;
32 * Returns the direct child of ancestor that contains con.
35 static Con *child_containing_con_recursively(Con *ancestor, Con *con) {
37 while (child && child->parent != ancestor) {
38 child = child->parent;
39 assert(child->parent);
45 * Returns true if the given container is the focused descendant of ancestor, recursively.
48 static bool is_focused_descendant(Con *con, Con *ancestor) {
50 while (current != ancestor) {
51 if (TAILQ_FIRST(&(current->parent->focus_head)) != current) {
54 current = current->parent;
55 assert(current->parent);
61 * This function detaches 'con' from its parent and inserts it either before or
65 void insert_con_into(Con *con, Con *target, position_t position) {
66 Con *parent = target->parent;
67 /* We need to preserve the old con->parent. While it might still be used to
68 * insert the entry before/after it, we call the on_remove_child callback
69 * afterwards which might then close the con if it is empty. */
70 Con *old_parent = con->parent;
72 /* We compare the focus order of the children of the lowest common ancestor. If con or
73 * its ancestor is before target's ancestor then con should be placed before the target
74 * in the focus stack. */
75 Con *lca = lowest_common_ancestor(con, parent);
77 ELOG("Container is being inserted into one of its descendants.\n");
81 Con *con_ancestor = child_containing_con_recursively(lca, con);
82 Con *target_ancestor = child_containing_con_recursively(lca, target);
83 bool moves_focus_from_ancestor = is_focused_descendant(con, con_ancestor);
86 /* Determine if con is going to be placed before or after target in the parent's focus stack. */
87 if (con_ancestor == target_ancestor) {
88 /* Happens when the target is con's old parent. Eg with layout V [ A H [ B C ] ],
89 * if we move C up. Target will be H. */
90 focus_before = moves_focus_from_ancestor;
92 /* Look at the focus stack order of the children of the lowest common ancestor. */
94 TAILQ_FOREACH(current, &(lca->focus_head), focused) {
95 if (current == con_ancestor || current == target_ancestor) {
99 focus_before = (current == con_ancestor);
102 /* If con is the focused container in our old ancestor we place the new ancestor
103 * before the old ancestor in the focus stack. Example:
104 * Consider the layout [ H [ V1 [ A* B ] V2 [ C ] ] ] where A is focused. We move to
105 * a second workspace and from there we move A to the right and switch back to the
106 * original workspace. Without the change focus would move to B instead of staying
108 if (moves_focus_from_ancestor && focus_before) {
109 Con *place = TAILQ_PREV(con_ancestor, focus_head, focused);
110 TAILQ_REMOVE(&(lca->focus_head), target_ancestor, focused);
112 TAILQ_INSERT_AFTER(&(lca->focus_head), place, target_ancestor, focused);
114 TAILQ_INSERT_HEAD(&(lca->focus_head), target_ancestor, focused);
119 con_fix_percent(con->parent);
121 /* When moving to a workspace, we respect the user’s configured
122 * workspace_layout */
123 if (parent->type == CT_WORKSPACE) {
124 Con *split = workspace_attach_to(parent);
125 if (split != parent) {
126 DLOG("Got a new split con, using that one instead\n");
128 con_attach(con, split, false);
131 con_fix_percent(split);
133 DLOG("ok, continuing with con %p instead\n", con);
138 con->parent = parent;
142 /* Example layout: H [ A B* ], we move A up/down. 'target' will be H. */
143 TAILQ_INSERT_BEFORE(target, con, focused);
145 /* Example layout: H [ A B* ], we move A up/down. 'target' will be H. */
146 TAILQ_INSERT_AFTER(&(parent->focus_head), target, con, focused);
150 /* Example layout: V [ H [ A B ] C* ], we move C up. 'target' will be A. */
151 TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
153 /* Example layout: V [ H [ A* B ] C ], we move C up. 'target' will be A. */
154 TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
158 if (position == BEFORE) {
159 TAILQ_INSERT_BEFORE(target, con, nodes);
160 } else if (position == AFTER) {
161 TAILQ_INSERT_AFTER(&(parent->nodes_head), target, con, nodes);
164 /* Pretend the con was just opened with regards to size percent values.
165 * Since the con is moved to a completely different con, the old value
166 * does not make sense anyways. */
168 con_fix_percent(parent);
170 CALL(old_parent, on_remove_child);
174 * This function detaches 'con' from its parent and puts it in the given
175 * workspace. Position is determined by the direction of movement into the
176 * workspace container.
179 static void attach_to_workspace(Con *con, Con *ws, direction_t direction) {
181 con_fix_percent(con->parent);
183 CALL(con->parent, on_remove_child);
187 if (direction == D_RIGHT || direction == D_DOWN) {
188 TAILQ_INSERT_HEAD(&(ws->nodes_head), con, nodes);
189 TAILQ_INSERT_HEAD(&(ws->focus_head), con, focused);
191 TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
192 TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
195 /* Pretend the con was just opened with regards to size percent values.
196 * Since the con is moved to a completely different con, the old value
197 * does not make sense anyways. */
203 * Moves the given container to the closest output in the given direction if
204 * such an output exists.
207 static void move_to_output_directed(Con *con, direction_t direction) {
208 Con *old_ws = con_get_workspace(con);
209 Output *current_output = get_output_for_con(con);
210 Output *output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
213 DLOG("No output in this direction found. Not moving.\n");
218 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
221 DLOG("No workspace on output in this direction found. Not moving.\n");
225 attach_to_workspace(con, ws, direction);
227 /* fix the focus stack */
230 /* force re-painting the indicators */
231 FREE(con->deco_render_params);
235 ipc_send_workspace_event("focus", ws, old_ws);
239 * Moves the given container in the given direction (D_LEFT, D_RIGHT,
243 void tree_move(Con *con, int direction) {
247 DLOG("Moving in direction %d\n", direction);
249 /* 1: get the first parent with the same orientation */
251 if (con->type == CT_WORKSPACE) {
252 DLOG("Not moving workspace\n");
256 if (con->fullscreen_mode == CF_GLOBAL) {
257 DLOG("Not moving fullscreen global container\n");
261 if ((con->fullscreen_mode == CF_OUTPUT) ||
262 (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1)) {
263 /* This is the only con on this workspace */
264 move_to_output_directed(con, direction);
268 orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT);
270 Con *same_orientation = con_parent_with_orientation(con, o);
271 /* The do {} while is used to 'restart' at this point with a different
272 * same_orientation, see the very last lines before the end of this block
275 /* There is no parent container with the same orientation */
276 if (!same_orientation) {
277 if (con_is_floating(con)) {
278 /* this is a floating con, we just disable floating */
279 floating_disable(con, true);
282 if (con_inside_floating(con)) {
283 /* 'con' should be moved out of a floating container */
284 DLOG("Inside floating, moving to workspace\n");
285 attach_to_workspace(con, con_get_workspace(con), direction);
288 DLOG("Force-changing orientation\n");
289 ws_force_orientation(con_get_workspace(con), o);
290 same_orientation = con_parent_with_orientation(con, o);
293 /* easy case: the move is within this container */
294 if (same_orientation == con->parent) {
295 DLOG("We are in the same container\n");
297 if ((swap = (direction == D_LEFT || direction == D_UP ? TAILQ_PREV(con, nodes_head, nodes) : TAILQ_NEXT(con, nodes)))) {
298 if (!con_is_leaf(swap)) {
299 DLOG("Moving into our bordering branch\n");
300 target = con_descend_direction(swap, direction);
301 position = (con_orientation(target->parent) != o ||
306 insert_con_into(con, target, position);
309 if (direction == D_LEFT || direction == D_UP)
310 TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
312 TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
314 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
315 TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
318 ipc_send_window_event("move", con);
319 ewmh_update_wm_desktop();
323 if (con->parent == con_get_workspace(con)) {
324 /* If we couldn't find a place to move it on this workspace,
325 * try to move it to a workspace on a different output */
326 move_to_output_directed(con, direction);
327 ipc_send_window_event("move", con);
328 ewmh_update_wm_desktop();
332 /* If there was no con with which we could swap the current one,
333 * search again, but starting one level higher. */
334 same_orientation = con_parent_with_orientation(con->parent, o);
336 } while (same_orientation == NULL);
338 /* this time, we have to move to another container */
339 /* This is the container *above* 'con' (an ancestor of con) which is inside
340 * 'same_orientation' */
342 while (above->parent != same_orientation)
343 above = above->parent;
345 /* Enforce the fullscreen focus restrictions. */
346 if (!con_fullscreen_permits_focusing(above->parent)) {
347 LOG("Cannot move out of fullscreen container\n");
351 DLOG("above = %p\n", above);
353 Con *next = (direction == D_UP || direction == D_LEFT ? TAILQ_PREV(above, nodes_head, nodes) : TAILQ_NEXT(above, nodes));
355 if (next && !con_is_leaf(next)) {
356 DLOG("Moving into the bordering branch of our adjacent container\n");
357 target = con_descend_direction(next, direction);
358 position = (con_orientation(target->parent) != o ||
363 insert_con_into(con, target, position);
365 con->parent->parent->type == CT_WORKSPACE &&
366 con->parent->layout != L_DEFAULT &&
367 con_num_children(con->parent) == 1) {
368 /* Con is the lone child of a non-default layout container at the edge
369 * of the workspace. Treat it as though the workspace is its parent
370 * and move it to the next output. */
371 DLOG("Grandparent is workspace\n");
372 move_to_output_directed(con, direction);
374 DLOG("Moving into container above\n");
375 position = (direction == D_UP || direction == D_LEFT ? BEFORE : AFTER);
376 insert_con_into(con, above, position);
380 /* force re-painting the indicators */
381 FREE(con->deco_render_params);
384 ipc_send_window_event("move", con);
385 ewmh_update_wm_desktop();