]> git.sur5r.net Git - i3/i3/blob - src/move.c
97ca6d40b42423a07582d44f3ade0f9001d7ef7f
[i3/i3] / src / move.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * move.c: Moving containers into some direction.
8  *
9  */
10 #include "all.h"
11
12 typedef enum { BEFORE,
13                AFTER } position_t;
14
15 /*
16  * This function detaches 'con' from its parent and inserts it either before or
17  * after 'target'.
18  *
19  */
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;
26
27     con_detach(con);
28     con_fix_percent(con->parent);
29
30     /* When moving to a workspace, we respect the user’s configured
31      * workspace_layout */
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");
36             con->parent = split;
37             con_attach(con, split, false);
38             DLOG("attached\n");
39             con->percent = 0.0;
40             con_fix_percent(split);
41             con = split;
42             DLOG("ok, continuing with con %p instead\n", con);
43             con_detach(con);
44         }
45     }
46
47     con->parent = parent;
48
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);
55     }
56
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. */
60     con->percent = 0.0;
61     con_fix_percent(parent);
62
63     CALL(old_parent, on_remove_child);
64 }
65
66 /*
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.
70  *
71  */
72 static void attach_to_workspace(Con *con, Con *ws, direction_t direction) {
73     con_detach(con);
74     con_fix_percent(con->parent);
75
76     CALL(con->parent, on_remove_child);
77
78     con->parent = ws;
79
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);
83     } else {
84         TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
85         TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
86     }
87
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. */
91     con->percent = 0.0;
92     con_fix_percent(ws);
93 }
94
95 /*
96  * Moves the given container to the closest output in the given direction if
97  * such an output exists.
98  *
99  */
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);
104
105     if (!output) {
106         DLOG("No output in this direction found. Not moving.\n");
107         return;
108     }
109
110     Con *ws = NULL;
111     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
112
113     if (!ws) {
114         DLOG("No workspace on output in this direction found. Not moving.\n");
115         return;
116     }
117
118     attach_to_workspace(con, ws, direction);
119
120     /* fix the focus stack */
121     con_activate(con);
122
123     /* force re-painting the indicators */
124     FREE(con->deco_render_params);
125
126     tree_flatten(croot);
127
128     ipc_send_workspace_event("focus", ws, old_ws);
129 }
130
131 /*
132  * Moves the given container in the given direction (D_LEFT, D_RIGHT,
133  * D_UP, D_DOWN).
134  *
135  */
136 void tree_move(Con *con, int direction) {
137     position_t position;
138     Con *target;
139
140     DLOG("Moving in direction %d\n", direction);
141
142     /* 1: get the first parent with the same orientation */
143
144     if (con->type == CT_WORKSPACE) {
145         DLOG("Not moving workspace\n");
146         return;
147     }
148
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);
152         return;
153     }
154
155     orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT);
156
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
160      * */
161     do {
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);
167                 return;
168             }
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);
173                 goto end;
174             }
175             DLOG("Force-changing orientation\n");
176             ws_force_orientation(con_get_workspace(con), o);
177             same_orientation = con_parent_with_orientation(con, o);
178         }
179
180         /* easy case: the move is within this container */
181         if (same_orientation == con->parent) {
182             DLOG("We are in the same container\n");
183             Con *swap;
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 ||
189                                         direction == D_UP ||
190                                         direction == D_LEFT
191                                     ? AFTER
192                                     : BEFORE);
193                     insert_con_into(con, target, position);
194                     goto end;
195                 }
196                 if (direction == D_LEFT || direction == D_UP)
197                     TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
198                 else
199                     TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
200
201                 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
202                 TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
203
204                 DLOG("Swapped.\n");
205                 ipc_send_window_event("move", con);
206                 ewmh_update_wm_desktop();
207                 return;
208             }
209
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();
216                 return;
217             }
218
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);
222         }
223     } while (same_orientation == NULL);
224
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' */
228     Con *above = con;
229     while (above->parent != same_orientation)
230         above = above->parent;
231
232     /* Enforce the fullscreen focus restrictions. */
233     if (!con_fullscreen_permits_focusing(above->parent)) {
234         LOG("Cannot move out of fullscreen container\n");
235         return;
236     }
237
238     DLOG("above = %p\n", above);
239
240     Con *next = (direction == D_UP || direction == D_LEFT ? TAILQ_PREV(above, nodes_head, nodes) : TAILQ_NEXT(above, nodes));
241
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 ||
246                             direction == D_UP ||
247                             direction == D_LEFT
248                         ? AFTER
249                         : BEFORE);
250         insert_con_into(con, target, position);
251     } else if (!next &&
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);
260     } else {
261         DLOG("Moving into container above\n");
262         position = (direction == D_UP || direction == D_LEFT ? BEFORE : AFTER);
263         insert_con_into(con, above, position);
264     }
265
266 end:
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)). */
270     con_focus(con);
271
272     /* force re-painting the indicators */
273     FREE(con->deco_render_params);
274
275     tree_flatten(croot);
276     ipc_send_window_event("move", con);
277     ewmh_update_wm_desktop();
278 }