i3
move.c
Go to the documentation of this file.
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 inserts it at the given
69  * workspace.
70  *
71  */
72 static void attach_to_workspace(Con *con, Con *ws) {
73  con_detach(con);
74  con_fix_percent(con->parent);
75 
76  CALL(con->parent, on_remove_child);
77 
78  con->parent = ws;
79 
80  TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
81  TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
82 
83  /* Pretend the con was just opened with regards to size percent values.
84  * Since the con is moved to a completely different con, the old value
85  * does not make sense anyways. */
86  con->percent = 0.0;
87  con_fix_percent(ws);
88 }
89 
90 /*
91  * Moves the current container in the given direction (D_LEFT, D_RIGHT,
92  * D_UP, D_DOWN).
93  *
94  */
95 void tree_move(int direction) {
96  DLOG("Moving in direction %d\n", direction);
97  /* 1: get the first parent with the same orientation */
98  Con *con = focused;
99 
100  if (con->type == CT_WORKSPACE) {
101  DLOG("Not moving workspace\n");
102  return;
103  }
104 
105  if (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1) {
106  DLOG("This is the only con on this workspace, not doing anything\n");
107  return;
108  }
109 
110  orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT);
111 
112  Con *same_orientation = con_parent_with_orientation(con, o);
113  /* The do {} while is used to 'restart' at this point with a different
114  * same_orientation, see the very last lines before the end of this block
115  * */
116  do {
117  /* There is no parent container with the same orientation */
118  if (!same_orientation) {
119  if (con_is_floating(con)) {
120  /* this is a floating con, we just disable floating */
121  floating_disable(con, true);
122  return;
123  }
124  if (con_inside_floating(con)) {
125  /* 'con' should be moved out of a floating container */
126  DLOG("Inside floating, moving to workspace\n");
128  goto end;
129  }
130  DLOG("Force-changing orientation\n");
132  same_orientation = con_parent_with_orientation(con, o);
133  }
134 
135  /* easy case: the move is within this container */
136  if (same_orientation == con->parent) {
137  DLOG("We are in the same container\n");
138  Con *swap;
139  if ((swap = (direction == D_LEFT || direction == D_UP ?
140  TAILQ_PREV(con, nodes_head, nodes) :
141  TAILQ_NEXT(con, nodes)))) {
142  if (!con_is_leaf(swap)) {
144  goto end;
145  }
146  if (direction == D_LEFT || direction == D_UP)
147  TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
148  else TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
149 
150  TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
151  TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
152 
153  DLOG("Swapped.\n");
154  return;
155  }
156 
157  /* If there was no con with which we could swap the current one, search
158  * again, but starting one level higher. If we are on the workspace
159  * level, don’t do that. The result would be a force change of
160  * workspace orientation, which is not necessary. */
161  if (con->parent == con_get_workspace(con))
162  return;
163  same_orientation = con_parent_with_orientation(con->parent, o);
164  }
165  } while (same_orientation == NULL);
166 
167  /* this time, we have to move to another container */
168  /* This is the container *above* 'con' (an ancestor of con) which is inside
169  * 'same_orientation' */
170  Con *above = con;
171  while (above->parent != same_orientation)
172  above = above->parent;
173 
174  /* Enforce the fullscreen focus restrictions. */
176  LOG("Cannot move out of fullscreen container\n");
177  return;
178  }
179 
180  DLOG("above = %p\n", above);
181  Con *next;
182  position_t position;
183  if (direction == D_UP || direction == D_LEFT) {
184  position = BEFORE;
185  next = TAILQ_PREV(above, nodes_head, nodes);
186  } else {
187  position = AFTER;
188  next = TAILQ_NEXT(above, nodes);
189  }
190 
191  /* special case: there is a split container in the direction we are moving
192  * to, so descend and append */
193  if (next && !con_is_leaf(next))
195  else
196  insert_con_into(con, above, position);
197 
198 end:
199  /* We need to call con_focus() to fix the focus stack "above" the container
200  * we just inserted the focused container into (otherwise, the parent
201  * container(s) would still point to the old container(s)). */
202  con_focus(con);
203 
204  /* force re-painting the indicators */
205  FREE(con->deco_render_params);
206 
208 }