]> git.sur5r.net Git - i3/i3/blob - src/move.c
Merge branch 'master' into next
[i3/i3] / src / move.c
1 #undef I3__FILE__
2 #define I3__FILE__ "move.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * move.c: Moving containers into some direction.
10  *
11  */
12 #include "all.h"
13
14 typedef enum { BEFORE, AFTER } position_t;
15
16 /*
17  * This function detaches 'con' from its parent and inserts it either before or
18  * after 'target'.
19  *
20  */
21 static void insert_con_into(Con *con, Con *target, position_t position) {
22     Con *parent = target->parent;
23     /* We need to preserve the old con->parent. While it might still be used to
24      * insert the entry before/after it, we call the on_remove_child callback
25      * afterwards which might then close the con if it is empty. */
26     Con *old_parent = con->parent;
27
28     con_detach(con);
29     con_fix_percent(con->parent);
30
31     /* When moving to a workspace, we respect the user’s configured
32      * workspace_layout */
33     if (parent->type == CT_WORKSPACE) {
34         Con *split = workspace_attach_to(parent);
35         if (split != parent) {
36             DLOG("Got a new split con, using that one instead\n");
37             con->parent = split;
38             con_attach(con, split, false);
39             DLOG("attached\n");
40             con->percent = 0.0;
41             con_fix_percent(split);
42             con = split;
43             DLOG("ok, continuing with con %p instead\n", con);
44             con_detach(con);
45         }
46     }
47
48     con->parent = parent;
49
50     if (position == BEFORE) {
51         TAILQ_INSERT_BEFORE(target, con, nodes);
52         TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
53     } else if (position == AFTER) {
54         TAILQ_INSERT_AFTER(&(parent->nodes_head), target, con, nodes);
55         TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
56     }
57
58     /* Pretend the con was just opened with regards to size percent values.
59      * Since the con is moved to a completely different con, the old value
60      * does not make sense anyways. */
61     con->percent = 0.0;
62     con_fix_percent(parent);
63
64     CALL(old_parent, on_remove_child);
65 }
66
67 /*
68  * This function detaches 'con' from its parent and puts it in the given
69  * workspace. Position is determined by the direction of movement into the
70  * workspace container.
71  *
72  */
73 static void attach_to_workspace(Con *con, Con *ws, direction_t direction) {
74     con_detach(con);
75     con_fix_percent(con->parent);
76
77     CALL(con->parent, on_remove_child);
78
79     con->parent = ws;
80
81     if (direction == D_RIGHT || direction == D_DOWN) {
82         TAILQ_INSERT_HEAD(&(ws->nodes_head), con, nodes);
83         TAILQ_INSERT_HEAD(&(ws->focus_head), con, focused);
84     } else {
85         TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
86         TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
87     }
88
89     /* Pretend the con was just opened with regards to size percent values.
90      * Since the con is moved to a completely different con, the old value
91      * does not make sense anyways. */
92     con->percent = 0.0;
93     con_fix_percent(ws);
94 }
95
96 /*
97  * Moves the given container to the closest output in the given direction if
98  * such an output exists.
99  *
100  */
101 static void move_to_output_directed(Con *con, direction_t direction) {
102     Con *current_output_con = con_get_output(con);
103     Output *current_output = get_output_by_name(current_output_con->name);
104     Output *output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
105
106     if (!output) {
107         DLOG("No output in this direction found. Not moving.\n");
108         return;
109     }
110
111     Con *ws = NULL;
112     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
113
114     if (!ws) {
115         DLOG("No workspace on output in this direction found. Not moving.\n");
116         return;
117     }
118
119     attach_to_workspace(con, ws, direction);
120 }
121
122 /*
123  * Moves the current container in the given direction (D_LEFT, D_RIGHT,
124  * D_UP, D_DOWN).
125  *
126  */
127 void tree_move(int direction) {
128     DLOG("Moving in direction %d\n", direction);
129     /* 1: get the first parent with the same orientation */
130     Con *con = focused;
131
132     if (con->type == CT_WORKSPACE) {
133         DLOG("Not moving workspace\n");
134         return;
135     }
136
137     if (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1) {
138         /* This is the only con on this workspace */
139         move_to_output_directed(con, direction);
140         goto end;
141     }
142
143     orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT);
144
145     Con *same_orientation = con_parent_with_orientation(con, o);
146     /* The do {} while is used to 'restart' at this point with a different
147      * same_orientation, see the very last lines before the end of this block
148      * */
149     do {
150         /* There is no parent container with the same orientation */
151         if (!same_orientation) {
152             if (con_is_floating(con)) {
153                 /* this is a floating con, we just disable floating */
154                 floating_disable(con, true);
155                 return;
156             }
157             if (con_inside_floating(con)) {
158                 /* 'con' should be moved out of a floating container */
159                 DLOG("Inside floating, moving to workspace\n");
160                 attach_to_workspace(con, con_get_workspace(con), direction);
161                 goto end;
162             }
163             DLOG("Force-changing orientation\n");
164             ws_force_orientation(con_get_workspace(con), o);
165             same_orientation = con_parent_with_orientation(con, o);
166         }
167
168         /* easy case: the move is within this container */
169         if (same_orientation == con->parent) {
170             DLOG("We are in the same container\n");
171             Con *swap;
172             if ((swap = (direction == D_LEFT || direction == D_UP ?
173                           TAILQ_PREV(con, nodes_head, nodes) :
174                           TAILQ_NEXT(con, nodes)))) {
175                 if (!con_is_leaf(swap)) {
176                     insert_con_into(con, con_descend_focused(swap), AFTER);
177                     goto end;
178                 }
179                 if (direction == D_LEFT || direction == D_UP)
180                     TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
181                 else TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
182
183                 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
184                 TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
185
186                 DLOG("Swapped.\n");
187                 return;
188             }
189
190             if (con->parent == con_get_workspace(con)) {
191                 /*  If we couldn't find a place to move it on this workspace,
192                  *  try to move it to a workspace on a different output */
193                 move_to_output_directed(con, direction);
194                 goto end;
195             }
196
197             /* If there was no con with which we could swap the current one,
198              * search again, but starting one level higher. */
199             same_orientation = con_parent_with_orientation(con->parent, o);
200         }
201     } while (same_orientation == NULL);
202
203     /* this time, we have to move to another container */
204     /* This is the container *above* 'con' (an ancestor of con) which is inside
205      * 'same_orientation' */
206     Con *above = con;
207     while (above->parent != same_orientation)
208         above = above->parent;
209
210     /* Enforce the fullscreen focus restrictions. */
211     if (!con_fullscreen_permits_focusing(above->parent)) {
212         LOG("Cannot move out of fullscreen container\n");
213         return;
214     }
215
216     DLOG("above = %p\n", above);
217     Con *next;
218     position_t position;
219     if (direction == D_UP || direction == D_LEFT) {
220         position = BEFORE;
221         next = TAILQ_PREV(above, nodes_head, nodes);
222     } else {
223         position = AFTER;
224         next = TAILQ_NEXT(above, nodes);
225     }
226
227     /* special case: there is a split container in the direction we are moving
228      * to, so descend and append */
229     if (next && !con_is_leaf(next))
230         insert_con_into(con, con_descend_focused(next), AFTER);
231     else
232         insert_con_into(con, above, position);
233
234 end:
235     /* We need to call con_focus() to fix the focus stack "above" the container
236      * we just inserted the focused container into (otherwise, the parent
237      * container(s) would still point to the old container(s)). */
238     con_focus(con);
239
240     /* force re-painting the indicators */
241     FREE(con->deco_render_params);
242
243     tree_flatten(croot);
244 }