]> git.sur5r.net Git - i3/i3/blob - src/move.c
Merge pull request #3192 from Exagone313/next
[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  * Returns the lowest container in the tree that has both a and b as descendants.
17  *
18  */
19 static Con *lowest_common_ancestor(Con *a, Con *b) {
20     Con *parent_a = a;
21     while (parent_a) {
22         Con *parent_b = b;
23         while (parent_b) {
24             if (parent_a == parent_b) {
25                 return parent_a;
26             }
27             parent_b = parent_b->parent;
28         }
29         parent_a = parent_a->parent;
30     }
31     assert(false);
32 }
33
34 /*
35  * Returns the direct child of ancestor that contains con.
36  *
37  */
38 static Con *child_containing_con_recursively(Con *ancestor, Con *con) {
39     Con *child = con;
40     while (child && child->parent != ancestor) {
41         child = child->parent;
42         assert(child->parent);
43     }
44     return child;
45 }
46
47 /*
48  * Returns true if the given container is the focused descendant of ancestor, recursively.
49  *
50  */
51 static bool is_focused_descendant(Con *con, Con *ancestor) {
52     Con *current = con;
53     while (current != ancestor) {
54         if (TAILQ_FIRST(&(current->parent->focus_head)) != current) {
55             return false;
56         }
57         current = current->parent;
58         assert(current->parent);
59     }
60     return true;
61 }
62
63 /*
64  * This function detaches 'con' from its parent and inserts it either before or
65  * after 'target'.
66  *
67  */
68 static void insert_con_into(Con *con, Con *target, position_t position) {
69     Con *parent = target->parent;
70     /* We need to preserve the old con->parent. While it might still be used to
71      * insert the entry before/after it, we call the on_remove_child callback
72      * afterwards which might then close the con if it is empty. */
73     Con *old_parent = con->parent;
74
75     /* We compare the focus order of the children of the lowest common ancestor. If con or
76      * its ancestor is before target's ancestor then con should be placed before the target
77      * in the focus stack. */
78     Con *lca = lowest_common_ancestor(con, parent);
79     if (lca == con) {
80         ELOG("Container is being inserted into one of its descendants.\n");
81         return;
82     }
83
84     Con *con_ancestor = child_containing_con_recursively(lca, con);
85     Con *target_ancestor = child_containing_con_recursively(lca, target);
86     bool moves_focus_from_ancestor = is_focused_descendant(con, con_ancestor);
87     bool focus_before;
88
89     /* Determine if con is going to be placed before or after target in the parent's focus stack. */
90     if (con_ancestor == target_ancestor) {
91         /* Happens when the target is con's old parent. Eg with layout V [ A H [ B C ] ],
92          * if we move C up. Target will be H. */
93         focus_before = moves_focus_from_ancestor;
94     } else {
95         /* Look at the focus stack order of the children of the lowest common ancestor. */
96         Con *current;
97         TAILQ_FOREACH(current, &(lca->focus_head), focused) {
98             if (current == con_ancestor || current == target_ancestor) {
99                 break;
100             }
101         }
102         focus_before = (current == con_ancestor);
103     }
104
105     /* If con is the focused container in our old ancestor we place the new ancestor
106      * before the old ancestor in the focus stack. Example:
107      * Consider the layout [ H [ V1 [ A* B ] V2 [ C ] ] ] where A is focused. We move to
108      * a second workspace and from there we move A to the right and switch back to the
109      * original workspace. Without the change focus would move to B instead of staying
110      * with A. */
111     if (moves_focus_from_ancestor && focus_before) {
112         Con *place = TAILQ_PREV(con_ancestor, focus_head, focused);
113         TAILQ_REMOVE(&(lca->focus_head), target_ancestor, focused);
114         if (place) {
115             TAILQ_INSERT_AFTER(&(lca->focus_head), place, target_ancestor, focused);
116         } else {
117             TAILQ_INSERT_HEAD(&(lca->focus_head), target_ancestor, focused);
118         }
119     }
120
121     con_detach(con);
122     con_fix_percent(con->parent);
123
124     /* When moving to a workspace, we respect the user’s configured
125      * workspace_layout */
126     if (parent->type == CT_WORKSPACE) {
127         Con *split = workspace_attach_to(parent);
128         if (split != parent) {
129             DLOG("Got a new split con, using that one instead\n");
130             con->parent = split;
131             con_attach(con, split, false);
132             DLOG("attached\n");
133             con->percent = 0.0;
134             con_fix_percent(split);
135             con = split;
136             DLOG("ok, continuing with con %p instead\n", con);
137             con_detach(con);
138         }
139     }
140
141     con->parent = parent;
142
143     if (parent == lca) {
144         if (focus_before) {
145             /* Example layout: H [ A B* ], we move A up/down. 'target' will be H. */
146             TAILQ_INSERT_BEFORE(target, con, focused);
147         } else {
148             /* Example layout: H [ A B* ], we move A up/down. 'target' will be H. */
149             TAILQ_INSERT_AFTER(&(parent->focus_head), target, con, focused);
150         }
151     } else {
152         if (focus_before) {
153             /* Example layout: V [ H [ A B ] C* ], we move C up. 'target' will be A. */
154             TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
155         } else {
156             /* Example layout: V [ H [ A* B ] C ], we move C up. 'target' will be A. */
157             TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
158         }
159     }
160
161     if (position == BEFORE) {
162         TAILQ_INSERT_BEFORE(target, con, nodes);
163     } else if (position == AFTER) {
164         TAILQ_INSERT_AFTER(&(parent->nodes_head), target, con, nodes);
165     }
166
167     /* Pretend the con was just opened with regards to size percent values.
168      * Since the con is moved to a completely different con, the old value
169      * does not make sense anyways. */
170     con->percent = 0.0;
171     con_fix_percent(parent);
172
173     CALL(old_parent, on_remove_child);
174 }
175
176 /*
177  * This function detaches 'con' from its parent and puts it in the given
178  * workspace. Position is determined by the direction of movement into the
179  * workspace container.
180  *
181  */
182 static void attach_to_workspace(Con *con, Con *ws, direction_t direction) {
183     con_detach(con);
184     con_fix_percent(con->parent);
185
186     CALL(con->parent, on_remove_child);
187
188     con->parent = ws;
189
190     if (direction == D_RIGHT || direction == D_DOWN) {
191         TAILQ_INSERT_HEAD(&(ws->nodes_head), con, nodes);
192         TAILQ_INSERT_HEAD(&(ws->focus_head), con, focused);
193     } else {
194         TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
195         TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
196     }
197
198     /* Pretend the con was just opened with regards to size percent values.
199      * Since the con is moved to a completely different con, the old value
200      * does not make sense anyways. */
201     con->percent = 0.0;
202     con_fix_percent(ws);
203 }
204
205 /*
206  * Moves the given container to the closest output in the given direction if
207  * such an output exists.
208  *
209  */
210 static void move_to_output_directed(Con *con, direction_t direction) {
211     Con *old_ws = con_get_workspace(con);
212     Output *current_output = get_output_for_con(con);
213     Output *output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
214
215     if (!output) {
216         DLOG("No output in this direction found. Not moving.\n");
217         return;
218     }
219
220     Con *ws = NULL;
221     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
222
223     if (!ws) {
224         DLOG("No workspace on output in this direction found. Not moving.\n");
225         return;
226     }
227
228     attach_to_workspace(con, ws, direction);
229
230     /* fix the focus stack */
231     con_activate(con);
232
233     /* force re-painting the indicators */
234     FREE(con->deco_render_params);
235
236     tree_flatten(croot);
237
238     ipc_send_workspace_event("focus", ws, old_ws);
239 }
240
241 /*
242  * Moves the given container in the given direction (D_LEFT, D_RIGHT,
243  * D_UP, D_DOWN).
244  *
245  */
246 void tree_move(Con *con, int direction) {
247     position_t position;
248     Con *target;
249
250     DLOG("Moving in direction %d\n", direction);
251
252     /* 1: get the first parent with the same orientation */
253
254     if (con->type == CT_WORKSPACE) {
255         DLOG("Not moving workspace\n");
256         return;
257     }
258
259     if (con->fullscreen_mode == CF_GLOBAL) {
260         DLOG("Not moving fullscreen global container\n");
261         return;
262     }
263
264     if ((con->fullscreen_mode == CF_OUTPUT) ||
265         (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1)) {
266         /* This is the only con on this workspace */
267         move_to_output_directed(con, direction);
268         return;
269     }
270
271     orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT);
272
273     Con *same_orientation = con_parent_with_orientation(con, o);
274     /* The do {} while is used to 'restart' at this point with a different
275      * same_orientation, see the very last lines before the end of this block
276      * */
277     do {
278         /* There is no parent container with the same orientation */
279         if (!same_orientation) {
280             if (con_is_floating(con)) {
281                 /* this is a floating con, we just disable floating */
282                 floating_disable(con, true);
283                 return;
284             }
285             if (con_inside_floating(con)) {
286                 /* 'con' should be moved out of a floating container */
287                 DLOG("Inside floating, moving to workspace\n");
288                 attach_to_workspace(con, con_get_workspace(con), direction);
289                 goto end;
290             }
291             DLOG("Force-changing orientation\n");
292             ws_force_orientation(con_get_workspace(con), o);
293             same_orientation = con_parent_with_orientation(con, o);
294         }
295
296         /* easy case: the move is within this container */
297         if (same_orientation == con->parent) {
298             DLOG("We are in the same container\n");
299             Con *swap;
300             if ((swap = (direction == D_LEFT || direction == D_UP ? TAILQ_PREV(con, nodes_head, nodes) : TAILQ_NEXT(con, nodes)))) {
301                 if (!con_is_leaf(swap)) {
302                     DLOG("Moving into our bordering branch\n");
303                     target = con_descend_direction(swap, direction);
304                     position = (con_orientation(target->parent) != o ||
305                                         direction == D_UP ||
306                                         direction == D_LEFT
307                                     ? AFTER
308                                     : BEFORE);
309                     insert_con_into(con, target, position);
310                     goto end;
311                 }
312                 if (direction == D_LEFT || direction == D_UP)
313                     TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
314                 else
315                     TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
316
317                 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
318                 TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
319
320                 DLOG("Swapped.\n");
321                 ipc_send_window_event("move", con);
322                 ewmh_update_wm_desktop();
323                 return;
324             }
325
326             if (con->parent == con_get_workspace(con)) {
327                 /*  If we couldn't find a place to move it on this workspace,
328                  *  try to move it to a workspace on a different output */
329                 move_to_output_directed(con, direction);
330                 ipc_send_window_event("move", con);
331                 ewmh_update_wm_desktop();
332                 return;
333             }
334
335             /* If there was no con with which we could swap the current one,
336              * search again, but starting one level higher. */
337             same_orientation = con_parent_with_orientation(con->parent, o);
338         }
339     } while (same_orientation == NULL);
340
341     /* this time, we have to move to another container */
342     /* This is the container *above* 'con' (an ancestor of con) which is inside
343      * 'same_orientation' */
344     Con *above = con;
345     while (above->parent != same_orientation)
346         above = above->parent;
347
348     /* Enforce the fullscreen focus restrictions. */
349     if (!con_fullscreen_permits_focusing(above->parent)) {
350         LOG("Cannot move out of fullscreen container\n");
351         return;
352     }
353
354     DLOG("above = %p\n", above);
355
356     Con *next = (direction == D_UP || direction == D_LEFT ? TAILQ_PREV(above, nodes_head, nodes) : TAILQ_NEXT(above, nodes));
357
358     if (next && !con_is_leaf(next)) {
359         DLOG("Moving into the bordering branch of our adjacent container\n");
360         target = con_descend_direction(next, direction);
361         position = (con_orientation(target->parent) != o ||
362                             direction == D_UP ||
363                             direction == D_LEFT
364                         ? AFTER
365                         : BEFORE);
366         insert_con_into(con, target, position);
367     } else if (!next &&
368                con->parent->parent->type == CT_WORKSPACE &&
369                con->parent->layout != L_DEFAULT &&
370                con_num_children(con->parent) == 1) {
371         /* Con is the lone child of a non-default layout container at the edge
372          * of the workspace. Treat it as though the workspace is its parent
373          * and move it to the next output. */
374         DLOG("Grandparent is workspace\n");
375         move_to_output_directed(con, direction);
376     } else {
377         DLOG("Moving into container above\n");
378         position = (direction == D_UP || direction == D_LEFT ? BEFORE : AFTER);
379         insert_con_into(con, above, position);
380     }
381
382 end:
383     /* force re-painting the indicators */
384     FREE(con->deco_render_params);
385
386     tree_flatten(croot);
387     ipc_send_window_event("move", con);
388     ewmh_update_wm_desktop();
389 }