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 * This function detaches 'con' from its parent and inserts it either before or
20 static void insert_con_into(Con *con, Con *target, position_t position) {
21 Con *parent = target->parent;
22 /* We need to preserve the old con->parent. While it might still be used to
23 * insert the entry before/after it, we call the on_remove_child callback
24 * afterwards which might then close the con if it is empty. */
25 Con *old_parent = con->parent;
28 con_fix_percent(con->parent);
30 /* When moving to a workspace, we respect the user’s configured
32 if (parent->type == CT_WORKSPACE) {
33 Con *split = workspace_attach_to(parent);
34 if (split != parent) {
35 DLOG("Got a new split con, using that one instead\n");
37 con_attach(con, split, false);
40 con_fix_percent(split);
42 DLOG("ok, continuing with con %p instead\n", con);
49 if (position == BEFORE) {
50 TAILQ_INSERT_BEFORE(target, con, nodes);
51 TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
52 } else if (position == AFTER) {
53 TAILQ_INSERT_AFTER(&(parent->nodes_head), target, con, nodes);
54 TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
57 /* Pretend the con was just opened with regards to size percent values.
58 * Since the con is moved to a completely different con, the old value
59 * does not make sense anyways. */
61 con_fix_percent(parent);
63 CALL(old_parent, on_remove_child);
67 * This function detaches 'con' from its parent and puts it in the given
68 * workspace. Position is determined by the direction of movement into the
69 * workspace container.
72 static void attach_to_workspace(Con *con, Con *ws, direction_t direction) {
74 con_fix_percent(con->parent);
76 CALL(con->parent, on_remove_child);
80 if (direction == D_RIGHT || direction == D_DOWN) {
81 TAILQ_INSERT_HEAD(&(ws->nodes_head), con, nodes);
82 TAILQ_INSERT_HEAD(&(ws->focus_head), con, focused);
84 TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
85 TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
88 /* Pretend the con was just opened with regards to size percent values.
89 * Since the con is moved to a completely different con, the old value
90 * does not make sense anyways. */
96 * Moves the given container to the closest output in the given direction if
97 * such an output exists.
100 static void move_to_output_directed(Con *con, direction_t direction) {
101 Con *old_ws = con_get_workspace(con);
102 Output *current_output = get_output_for_con(con);
103 Output *output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
106 DLOG("No output in this direction found. Not moving.\n");
111 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
114 DLOG("No workspace on output in this direction found. Not moving.\n");
118 attach_to_workspace(con, ws, direction);
120 /* fix the focus stack */
123 /* force re-painting the indicators */
124 FREE(con->deco_render_params);
128 ipc_send_workspace_event("focus", ws, old_ws);
132 * Moves the given container in the given direction (D_LEFT, D_RIGHT,
136 void tree_move(Con *con, int direction) {
140 DLOG("Moving in direction %d\n", direction);
142 /* 1: get the first parent with the same orientation */
144 if (con->type == CT_WORKSPACE) {
145 DLOG("Not moving workspace\n");
149 if (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1) {
150 /* This is the only con on this workspace */
151 move_to_output_directed(con, direction);
155 orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT);
157 Con *same_orientation = con_parent_with_orientation(con, o);
158 /* The do {} while is used to 'restart' at this point with a different
159 * same_orientation, see the very last lines before the end of this block
162 /* There is no parent container with the same orientation */
163 if (!same_orientation) {
164 if (con_is_floating(con)) {
165 /* this is a floating con, we just disable floating */
166 floating_disable(con, true);
169 if (con_inside_floating(con)) {
170 /* 'con' should be moved out of a floating container */
171 DLOG("Inside floating, moving to workspace\n");
172 attach_to_workspace(con, con_get_workspace(con), direction);
175 DLOG("Force-changing orientation\n");
176 ws_force_orientation(con_get_workspace(con), o);
177 same_orientation = con_parent_with_orientation(con, o);
180 /* easy case: the move is within this container */
181 if (same_orientation == con->parent) {
182 DLOG("We are in the same container\n");
184 if ((swap = (direction == D_LEFT || direction == D_UP ? TAILQ_PREV(con, nodes_head, nodes) : TAILQ_NEXT(con, nodes)))) {
185 if (!con_is_leaf(swap)) {
186 DLOG("Moving into our bordering branch\n");
187 target = con_descend_direction(swap, direction);
188 position = (con_orientation(target->parent) != o ||
193 insert_con_into(con, target, position);
196 if (direction == D_LEFT || direction == D_UP)
197 TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
199 TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
201 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
202 TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
205 ipc_send_window_event("move", con);
206 ewmh_update_wm_desktop();
210 if (con->parent == con_get_workspace(con)) {
211 /* If we couldn't find a place to move it on this workspace,
212 * try to move it to a workspace on a different output */
213 move_to_output_directed(con, direction);
214 ipc_send_window_event("move", con);
215 ewmh_update_wm_desktop();
219 /* If there was no con with which we could swap the current one,
220 * search again, but starting one level higher. */
221 same_orientation = con_parent_with_orientation(con->parent, o);
223 } while (same_orientation == NULL);
225 /* this time, we have to move to another container */
226 /* This is the container *above* 'con' (an ancestor of con) which is inside
227 * 'same_orientation' */
229 while (above->parent != same_orientation)
230 above = above->parent;
232 /* Enforce the fullscreen focus restrictions. */
233 if (!con_fullscreen_permits_focusing(above->parent)) {
234 LOG("Cannot move out of fullscreen container\n");
238 DLOG("above = %p\n", above);
240 Con *next = (direction == D_UP || direction == D_LEFT ? TAILQ_PREV(above, nodes_head, nodes) : TAILQ_NEXT(above, nodes));
242 if (next && !con_is_leaf(next)) {
243 DLOG("Moving into the bordering branch of our adjacent container\n");
244 target = con_descend_direction(next, direction);
245 position = (con_orientation(target->parent) != o ||
250 insert_con_into(con, target, position);
252 con->parent->parent->type == CT_WORKSPACE &&
253 con->parent->layout != L_DEFAULT &&
254 con_num_children(con->parent) == 1) {
255 /* Con is the lone child of a non-default layout container at the edge
256 * of the workspace. Treat it as though the workspace is its parent
257 * and move it to the next output. */
258 DLOG("Grandparent is workspace\n");
259 move_to_output_directed(con, direction);
261 DLOG("Moving into container above\n");
262 position = (direction == D_UP || direction == D_LEFT ? BEFORE : AFTER);
263 insert_con_into(con, above, position);
267 /* We need to call con_focus() to fix the focus stack "above" the container
268 * we just inserted the focused container into (otherwise, the parent
269 * container(s) would still point to the old container(s)). */
272 /* force re-painting the indicators */
273 FREE(con->deco_render_params);
276 ipc_send_window_event("move", con);
277 ewmh_update_wm_desktop();