]> git.sur5r.net Git - i3/i3/blob - src/move.c
Correct insert_con_into's focus handling
[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->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1) {
260         /* This is the only con on this workspace */
261         move_to_output_directed(con, direction);
262         return;
263     }
264
265     orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT);
266
267     Con *same_orientation = con_parent_with_orientation(con, o);
268     /* The do {} while is used to 'restart' at this point with a different
269      * same_orientation, see the very last lines before the end of this block
270      * */
271     do {
272         /* There is no parent container with the same orientation */
273         if (!same_orientation) {
274             if (con_is_floating(con)) {
275                 /* this is a floating con, we just disable floating */
276                 floating_disable(con, true);
277                 return;
278             }
279             if (con_inside_floating(con)) {
280                 /* 'con' should be moved out of a floating container */
281                 DLOG("Inside floating, moving to workspace\n");
282                 attach_to_workspace(con, con_get_workspace(con), direction);
283                 goto end;
284             }
285             DLOG("Force-changing orientation\n");
286             ws_force_orientation(con_get_workspace(con), o);
287             same_orientation = con_parent_with_orientation(con, o);
288         }
289
290         /* easy case: the move is within this container */
291         if (same_orientation == con->parent) {
292             DLOG("We are in the same container\n");
293             Con *swap;
294             if ((swap = (direction == D_LEFT || direction == D_UP ? TAILQ_PREV(con, nodes_head, nodes) : TAILQ_NEXT(con, nodes)))) {
295                 if (!con_is_leaf(swap)) {
296                     DLOG("Moving into our bordering branch\n");
297                     target = con_descend_direction(swap, direction);
298                     position = (con_orientation(target->parent) != o ||
299                                         direction == D_UP ||
300                                         direction == D_LEFT
301                                     ? AFTER
302                                     : BEFORE);
303                     insert_con_into(con, target, position);
304                     goto end;
305                 }
306                 if (direction == D_LEFT || direction == D_UP)
307                     TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
308                 else
309                     TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
310
311                 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
312                 TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
313
314                 DLOG("Swapped.\n");
315                 ipc_send_window_event("move", con);
316                 ewmh_update_wm_desktop();
317                 return;
318             }
319
320             if (con->parent == con_get_workspace(con)) {
321                 /*  If we couldn't find a place to move it on this workspace,
322                  *  try to move it to a workspace on a different output */
323                 move_to_output_directed(con, direction);
324                 ipc_send_window_event("move", con);
325                 ewmh_update_wm_desktop();
326                 return;
327             }
328
329             /* If there was no con with which we could swap the current one,
330              * search again, but starting one level higher. */
331             same_orientation = con_parent_with_orientation(con->parent, o);
332         }
333     } while (same_orientation == NULL);
334
335     /* this time, we have to move to another container */
336     /* This is the container *above* 'con' (an ancestor of con) which is inside
337      * 'same_orientation' */
338     Con *above = con;
339     while (above->parent != same_orientation)
340         above = above->parent;
341
342     /* Enforce the fullscreen focus restrictions. */
343     if (!con_fullscreen_permits_focusing(above->parent)) {
344         LOG("Cannot move out of fullscreen container\n");
345         return;
346     }
347
348     DLOG("above = %p\n", above);
349
350     Con *next = (direction == D_UP || direction == D_LEFT ? TAILQ_PREV(above, nodes_head, nodes) : TAILQ_NEXT(above, nodes));
351
352     if (next && !con_is_leaf(next)) {
353         DLOG("Moving into the bordering branch of our adjacent container\n");
354         target = con_descend_direction(next, direction);
355         position = (con_orientation(target->parent) != o ||
356                             direction == D_UP ||
357                             direction == D_LEFT
358                         ? AFTER
359                         : BEFORE);
360         insert_con_into(con, target, position);
361     } else if (!next &&
362                con->parent->parent->type == CT_WORKSPACE &&
363                con->parent->layout != L_DEFAULT &&
364                con_num_children(con->parent) == 1) {
365         /* Con is the lone child of a non-default layout container at the edge
366          * of the workspace. Treat it as though the workspace is its parent
367          * and move it to the next output. */
368         DLOG("Grandparent is workspace\n");
369         move_to_output_directed(con, direction);
370     } else {
371         DLOG("Moving into container above\n");
372         position = (direction == D_UP || direction == D_LEFT ? BEFORE : AFTER);
373         insert_con_into(con, above, position);
374     }
375
376 end:
377     /* We need to call con_focus() to fix the focus stack "above" the container
378      * we just inserted the focused container into (otherwise, the parent
379      * container(s) would still point to the old container(s)). */
380     con_focus(con);
381
382     /* force re-painting the indicators */
383     FREE(con->deco_render_params);
384
385     tree_flatten(croot);
386     ipc_send_window_event("move", con);
387     ewmh_update_wm_desktop();
388 }