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