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