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