2 #define I3__FILE__ "move.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
9 * move.c: Moving containers into some direction.
14 typedef enum { BEFORE, AFTER } position_t;
17 * This function detaches 'con' from its parent and inserts it either before or
21 static void insert_con_into(Con *con, Con *target, position_t position) {
22 Con *parent = target->parent;
23 /* We need to preserve the old con->parent. While it might still be used to
24 * insert the entry before/after it, we call the on_remove_child callback
25 * afterwards which might then close the con if it is empty. */
26 Con *old_parent = con->parent;
29 con_fix_percent(con->parent);
31 /* When moving to a workspace, we respect the user’s configured
33 if (parent->type == CT_WORKSPACE) {
34 Con *split = workspace_attach_to(parent);
35 if (split != parent) {
36 DLOG("Got a new split con, using that one instead\n");
38 con_attach(con, split, false);
41 con_fix_percent(split);
43 DLOG("ok, continuing with con %p instead\n", con);
50 if (position == BEFORE) {
51 TAILQ_INSERT_BEFORE(target, con, nodes);
52 TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
53 } else if (position == AFTER) {
54 TAILQ_INSERT_AFTER(&(parent->nodes_head), target, con, nodes);
55 TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
58 /* Pretend the con was just opened with regards to size percent values.
59 * Since the con is moved to a completely different con, the old value
60 * does not make sense anyways. */
62 con_fix_percent(parent);
64 CALL(old_parent, on_remove_child);
68 * This function detaches 'con' from its parent and puts it in the given
69 * workspace. Position is determined by the direction of movement into the
70 * workspace container.
73 static void attach_to_workspace(Con *con, Con *ws, direction_t direction) {
75 con_fix_percent(con->parent);
77 CALL(con->parent, on_remove_child);
81 if (direction == D_RIGHT || direction == D_DOWN) {
82 TAILQ_INSERT_HEAD(&(ws->nodes_head), con, nodes);
83 TAILQ_INSERT_HEAD(&(ws->focus_head), con, focused);
85 TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
86 TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
89 /* Pretend the con was just opened with regards to size percent values.
90 * Since the con is moved to a completely different con, the old value
91 * does not make sense anyways. */
97 * Moves the given container to the closest output in the given direction if
98 * such an output exists.
101 static void move_to_output_directed(Con *con, direction_t direction) {
102 Con *current_output_con = con_get_output(con);
103 Output *current_output = get_output_by_name(current_output_con->name);
104 Output *output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
107 DLOG("No output in this direction found. Not moving.\n");
112 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
115 DLOG("No workspace on output in this direction found. Not moving.\n");
119 attach_to_workspace(con, ws, direction);
123 * Moves the current container in the given direction (D_LEFT, D_RIGHT,
127 void tree_move(int direction) {
131 DLOG("Moving in direction %d\n", direction);
133 /* 1: get the first parent with the same orientation */
136 if (con->type == CT_WORKSPACE) {
137 DLOG("Not moving workspace\n");
141 if (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1) {
142 /* This is the only con on this workspace */
143 move_to_output_directed(con, direction);
147 orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT);
149 Con *same_orientation = con_parent_with_orientation(con, o);
150 /* The do {} while is used to 'restart' at this point with a different
151 * same_orientation, see the very last lines before the end of this block
154 /* There is no parent container with the same orientation */
155 if (!same_orientation) {
156 if (con_is_floating(con)) {
157 /* this is a floating con, we just disable floating */
158 floating_disable(con, true);
161 if (con_inside_floating(con)) {
162 /* 'con' should be moved out of a floating container */
163 DLOG("Inside floating, moving to workspace\n");
164 attach_to_workspace(con, con_get_workspace(con), direction);
167 DLOG("Force-changing orientation\n");
168 ws_force_orientation(con_get_workspace(con), o);
169 same_orientation = con_parent_with_orientation(con, o);
172 /* easy case: the move is within this container */
173 if (same_orientation == con->parent) {
174 DLOG("We are in the same container\n");
176 if ((swap = (direction == D_LEFT || direction == D_UP ?
177 TAILQ_PREV(con, nodes_head, nodes) :
178 TAILQ_NEXT(con, nodes)))) {
179 if (!con_is_leaf(swap)) {
180 DLOG("Moving into our bordering branch\n");
181 target = con_descend_direction(swap, direction);
182 position = (con_orientation(target->parent) != o ||
184 direction == D_LEFT ?
186 insert_con_into(con, target, position);
189 if (direction == D_LEFT || direction == D_UP)
190 TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
191 else TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
193 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
194 TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
200 if (con->parent == con_get_workspace(con)) {
201 /* If we couldn't find a place to move it on this workspace,
202 * try to move it to a workspace on a different output */
203 move_to_output_directed(con, direction);
207 /* If there was no con with which we could swap the current one,
208 * search again, but starting one level higher. */
209 same_orientation = con_parent_with_orientation(con->parent, o);
211 } while (same_orientation == NULL);
213 /* this time, we have to move to another container */
214 /* This is the container *above* 'con' (an ancestor of con) which is inside
215 * 'same_orientation' */
217 while (above->parent != same_orientation)
218 above = above->parent;
220 /* Enforce the fullscreen focus restrictions. */
221 if (!con_fullscreen_permits_focusing(above->parent)) {
222 LOG("Cannot move out of fullscreen container\n");
226 DLOG("above = %p\n", above);
228 Con *next = (direction == D_UP || direction == D_LEFT ?
229 TAILQ_PREV(above, nodes_head, nodes) :
230 TAILQ_NEXT(above, nodes));
232 if (next && !con_is_leaf(next)) {
233 DLOG("Moving into the bordering branch of our adjacent container\n");
234 target = con_descend_direction(next, direction);
235 position = (con_orientation(target->parent) != o ||
237 direction == D_LEFT ?
239 insert_con_into(con, target, position);
241 DLOG("Moving into container above\n");
242 position = (direction == D_UP || direction == D_LEFT ? BEFORE : AFTER);
243 insert_con_into(con, above, position);
247 /* We need to call con_focus() to fix the focus stack "above" the container
248 * we just inserted the focused container into (otherwise, the parent
249 * container(s) would still point to the old container(s)). */
252 /* force re-painting the indicators */
253 FREE(con->deco_render_params);