]> git.sur5r.net Git - i3/i3/blob - src/move.c
same fix, but for moving (search above the current con when moving is not possible...
[i3/i3] / src / move.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4
5 #include "all.h"
6 #include "cmdparse.tab.h"
7
8 typedef enum { BEFORE, AFTER } position_t;
9
10 /*
11  * This function detaches 'con' from its parent and inserts it either before or
12  * after 'target'.
13  *
14  */
15 static void insert_con_into(Con *con, Con *target, position_t position) {
16     Con *parent = target->parent;
17     /* We need to preserve the old con->parent. While it might still be used to
18      * insert the entry before/after it, we call the on_remove_child callback
19      * afterwards which might then close the con if it is empty. */
20     Con *old_parent = con->parent;
21
22     con_detach(con);
23     con_fix_percent(con->parent);
24
25     con->parent = parent;
26
27     if (position == BEFORE) {
28         TAILQ_INSERT_BEFORE(target, con, nodes);
29         TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
30     } else if (position == AFTER) {
31         TAILQ_INSERT_AFTER(&(parent->nodes_head), target, con, nodes);
32         TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
33     }
34
35     /* Pretend the con was just opened with regards to size percent values.
36      * Since the con is moved to a completely different con, the old value
37      * does not make sense anyways. */
38     con->percent = 0.0;
39     con_fix_percent(parent);
40
41     CALL(old_parent, on_remove_child);
42 }
43
44 /*
45  * This function detaches 'con' from its parent and inserts it at the given
46  * workspace.
47  *
48  */
49 static void attach_to_workspace(Con *con, Con *ws) {
50     con_detach(con);
51     con_fix_percent(con->parent);
52
53     CALL(con->parent, on_remove_child);
54
55     con->parent = ws;
56
57     TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
58     TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
59
60     /* Pretend the con was just opened with regards to size percent values.
61      * Since the con is moved to a completely different con, the old value
62      * does not make sense anyways. */
63     con->percent = 0.0;
64     con_fix_percent(ws);
65 }
66
67 /*
68  * Moves the current container in the given direction (TOK_LEFT, TOK_RIGHT,
69  * TOK_UP, TOK_DOWN from cmdparse.l)
70  *
71  */
72 void tree_move(int direction) {
73     DLOG("Moving in direction %d\n", direction);
74     /* 1: get the first parent with the same orientation */
75     Con *con = focused;
76
77     if (con->type == CT_WORKSPACE) {
78         DLOG("Not moving workspace\n");
79         return;
80     }
81
82     if (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1) {
83         DLOG("This is the only con on this workspace, not doing anything\n");
84         return;
85     }
86
87     orientation_t o = (direction == TOK_LEFT || direction == TOK_RIGHT ? HORIZ : VERT);
88
89     Con *same_orientation = con_parent_with_orientation(con, o);
90     /* The do {} while is used to 'restart' at this point with a different
91      * same_orientation, see the very last lines before the end of this block
92      * */
93     do {
94         /* There is no parent container with the same orientation */
95         if (!same_orientation) {
96             if (con_is_floating(con)) {
97                 /* this is a floating con, we just disable floating */
98                 floating_disable(con, true);
99                 return;
100             }
101             if (con_inside_floating(con)) {
102                 /* 'con' should be moved out of a floating container */
103                 DLOG("Inside floating, moving to workspace\n");
104                 attach_to_workspace(con, con_get_workspace(con));
105                 goto end;
106             }
107             DLOG("Force-changing orientation\n");
108             ws_force_orientation(con_get_workspace(con), o);
109             same_orientation = con_parent_with_orientation(con, o);
110         }
111
112         /* easy case: the move is within this container */
113         if (same_orientation == con->parent) {
114             DLOG("We are in the same container\n");
115             Con *swap;
116             if ((swap = (direction == TOK_LEFT || direction == TOK_UP ?
117                           TAILQ_PREV(con, nodes_head, nodes) :
118                           TAILQ_NEXT(con, nodes)))) {
119                 if (!con_is_leaf(swap)) {
120                     insert_con_into(con, con_descend_focused(swap), AFTER);
121                     goto end;
122                 }
123                 if (direction == TOK_LEFT || direction == TOK_UP)
124                     TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
125                 else TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
126
127                 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
128                 TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
129
130                 DLOG("Swapped.\n");
131                 return;
132             }
133
134             /* If there was no con with which we could swap the current one, search
135              * again, but starting one level higher. If we are on the workspace
136              * level, don’t do that. The result would be a force change of
137              * workspace orientation, which is not necessary. */
138             if (con->parent == con_get_workspace(con))
139                 return;
140             same_orientation = con_parent_with_orientation(con->parent, o);
141         }
142     } while (same_orientation == NULL);
143
144     /* this time, we have to move to another container */
145     /* This is the container *above* 'con' which is inside 'same_orientation' */
146     Con *above = con;
147     while (above->parent != same_orientation)
148         above = above->parent;
149
150     DLOG("above = %p\n", above);
151     Con *next;
152     position_t position;
153     if (direction == TOK_UP || direction == TOK_LEFT) {
154         position = BEFORE;
155         next = TAILQ_PREV(above, nodes_head, nodes);
156     } else if (direction == TOK_DOWN || direction == TOK_RIGHT) {
157         position = AFTER;
158         next = TAILQ_NEXT(above, nodes);
159     }
160
161     /* special case: there is a split container in the direction we are moving
162      * to, so descend and append */
163     if (next && !con_is_leaf(next))
164         insert_con_into(con, con_descend_focused(next), AFTER);
165     else
166         insert_con_into(con, above, position);
167
168 end:
169     /* We need to call con_focus() to fix the focus stack "above" the container
170      * we just inserted the focused container into (otherwise, the parent
171      * container(s) would still point to the old container(s)). */
172     con_focus(con);
173
174     tree_flatten(croot);
175 }