2 #define I3__FILE__ "move.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
9 * move.c: Moving containers into some direction.
14 typedef enum { BEFORE,
18 * This function detaches 'con' from its parent and inserts it either before or
22 static void insert_con_into(Con *con, Con *target, position_t position) {
23 Con *parent = target->parent;
24 /* We need to preserve the old con->parent. While it might still be used to
25 * insert the entry before/after it, we call the on_remove_child callback
26 * afterwards which might then close the con if it is empty. */
27 Con *old_parent = con->parent;
30 con_fix_percent(con->parent);
32 /* When moving to a workspace, we respect the user’s configured
34 if (parent->type == CT_WORKSPACE) {
35 Con *split = workspace_attach_to(parent);
36 if (split != parent) {
37 DLOG("Got a new split con, using that one instead\n");
39 con_attach(con, split, false);
42 con_fix_percent(split);
44 DLOG("ok, continuing with con %p instead\n", con);
51 if (position == BEFORE) {
52 TAILQ_INSERT_BEFORE(target, con, nodes);
53 TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
54 } else if (position == AFTER) {
55 TAILQ_INSERT_AFTER(&(parent->nodes_head), target, con, nodes);
56 TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
59 /* Pretend the con was just opened with regards to size percent values.
60 * Since the con is moved to a completely different con, the old value
61 * does not make sense anyways. */
63 con_fix_percent(parent);
65 CALL(old_parent, on_remove_child);
69 * This function detaches 'con' from its parent and puts it in the given
70 * workspace. Position is determined by the direction of movement into the
71 * workspace container.
74 static void attach_to_workspace(Con *con, Con *ws, direction_t direction) {
76 con_fix_percent(con->parent);
78 CALL(con->parent, on_remove_child);
82 if (direction == D_RIGHT || direction == D_DOWN) {
83 TAILQ_INSERT_HEAD(&(ws->nodes_head), con, nodes);
84 TAILQ_INSERT_HEAD(&(ws->focus_head), con, focused);
86 TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
87 TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
90 /* Pretend the con was just opened with regards to size percent values.
91 * Since the con is moved to a completely different con, the old value
92 * does not make sense anyways. */
98 * Moves the given container to the closest output in the given direction if
99 * such an output exists.
102 static void move_to_output_directed(Con *con, direction_t direction) {
103 Con *old_ws = con_get_workspace(con);
104 Con *current_output_con = con_get_output(con);
105 Output *current_output = get_output_by_name(current_output_con->name);
106 Output *output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
109 DLOG("No output in this direction found. Not moving.\n");
114 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
117 DLOG("No workspace on output in this direction found. Not moving.\n");
121 attach_to_workspace(con, ws, direction);
123 /* fix the focus stack */
126 /* force re-painting the indicators */
127 FREE(con->deco_render_params);
131 ipc_send_workspace_event("focus", ws, old_ws);
135 * Moves the given container in the given direction (D_LEFT, D_RIGHT,
139 void tree_move(Con *con, int direction) {
143 DLOG("Moving in direction %d\n", direction);
145 /* 1: get the first parent with the same orientation */
147 if (con->type == CT_WORKSPACE) {
148 DLOG("Not moving workspace\n");
152 if (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1) {
153 /* This is the only con on this workspace */
154 move_to_output_directed(con, direction);
158 orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT);
160 Con *same_orientation = con_parent_with_orientation(con, o);
161 /* The do {} while is used to 'restart' at this point with a different
162 * same_orientation, see the very last lines before the end of this block
165 /* There is no parent container with the same orientation */
166 if (!same_orientation) {
167 if (con_is_floating(con)) {
168 /* this is a floating con, we just disable floating */
169 floating_disable(con, true);
172 if (con_inside_floating(con)) {
173 /* 'con' should be moved out of a floating container */
174 DLOG("Inside floating, moving to workspace\n");
175 attach_to_workspace(con, con_get_workspace(con), direction);
178 DLOG("Force-changing orientation\n");
179 ws_force_orientation(con_get_workspace(con), o);
180 same_orientation = con_parent_with_orientation(con, o);
183 /* easy case: the move is within this container */
184 if (same_orientation == con->parent) {
185 DLOG("We are in the same container\n");
187 if ((swap = (direction == D_LEFT || direction == D_UP ? TAILQ_PREV(con, nodes_head, nodes) : TAILQ_NEXT(con, nodes)))) {
188 if (!con_is_leaf(swap)) {
189 DLOG("Moving into our bordering branch\n");
190 target = con_descend_direction(swap, direction);
191 position = (con_orientation(target->parent) != o ||
196 insert_con_into(con, target, position);
199 if (direction == D_LEFT || direction == D_UP)
200 TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
202 TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
204 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
205 TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
208 ipc_send_window_event("move", con);
209 ewmh_update_wm_desktop();
213 if (con->parent == con_get_workspace(con)) {
214 /* If we couldn't find a place to move it on this workspace,
215 * try to move it to a workspace on a different output */
216 move_to_output_directed(con, direction);
217 ipc_send_window_event("move", con);
218 ewmh_update_wm_desktop();
222 /* If there was no con with which we could swap the current one,
223 * search again, but starting one level higher. */
224 same_orientation = con_parent_with_orientation(con->parent, o);
226 } while (same_orientation == NULL);
228 /* this time, we have to move to another container */
229 /* This is the container *above* 'con' (an ancestor of con) which is inside
230 * 'same_orientation' */
232 while (above->parent != same_orientation)
233 above = above->parent;
235 /* Enforce the fullscreen focus restrictions. */
236 if (!con_fullscreen_permits_focusing(above->parent)) {
237 LOG("Cannot move out of fullscreen container\n");
241 DLOG("above = %p\n", above);
243 Con *next = (direction == D_UP || direction == D_LEFT ? TAILQ_PREV(above, nodes_head, nodes) : TAILQ_NEXT(above, nodes));
245 if (next && !con_is_leaf(next)) {
246 DLOG("Moving into the bordering branch of our adjacent container\n");
247 target = con_descend_direction(next, direction);
248 position = (con_orientation(target->parent) != o ||
253 insert_con_into(con, target, position);
254 } else if (con->parent->parent->type == CT_WORKSPACE &&
255 con->parent->layout != L_DEFAULT &&
256 con_num_children(con->parent) == 1) {
257 /* Con is the lone child of a non-default layout container at the edge
258 * of the workspace. Treat it as though the workspace is its parent
259 * and move it to the next output. */
260 DLOG("Grandparent is workspace\n");
261 move_to_output_directed(con, direction);
263 DLOG("Moving into container above\n");
264 position = (direction == D_UP || direction == D_LEFT ? BEFORE : AFTER);
265 insert_con_into(con, above, position);
269 /* We need to call con_focus() to fix the focus stack "above" the container
270 * we just inserted the focused container into (otherwise, the parent
271 * container(s) would still point to the old container(s)). */
274 /* force re-painting the indicators */
275 FREE(con->deco_render_params);
278 ipc_send_window_event("move", con);
279 ewmh_update_wm_desktop();