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,
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  Con *current_output_con = con_get_output(con);
105  Output *current_output = get_output_by_name(current_output_con->name);
106  Output *output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
107 
108  if (!output) {
109  DLOG("No output in this direction found. Not moving.\n");
110  return;
111  }
112 
113  Con *ws = NULL;
115 
116  if (!ws) {
117  DLOG("No workspace on output in this direction found. Not moving.\n");
118  return;
119  }
120 
121  attach_to_workspace(con, ws, direction);
122 
123  /* fix the focus stack */
124  con_focus(con);
125 
126  /* force re-painting the indicators */
127  FREE(con->deco_render_params);
128 
130 
131  ipc_send_workspace_focus_event(ws, old_ws);
132 }
133 
134 /*
135  * Moves the current container in the given direction (D_LEFT, D_RIGHT,
136  * D_UP, D_DOWN).
137  *
138  */
139 void tree_move(int direction) {
140  position_t position;
141  Con *target;
142 
143  DLOG("Moving in direction %d\n", direction);
144 
145  /* 1: get the first parent with the same orientation */
146  Con *con = focused;
147 
148  if (con->type == CT_WORKSPACE) {
149  DLOG("Not moving workspace\n");
150  return;
151  }
152 
153  if (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1) {
154  /* This is the only con on this workspace */
155  move_to_output_directed(con, direction);
156  return;
157  }
158 
159  orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT);
160 
161  Con *same_orientation = con_parent_with_orientation(con, o);
162  /* The do {} while is used to 'restart' at this point with a different
163  * same_orientation, see the very last lines before the end of this block
164  * */
165  do {
166  /* There is no parent container with the same orientation */
167  if (!same_orientation) {
168  if (con_is_floating(con)) {
169  /* this is a floating con, we just disable floating */
170  floating_disable(con, true);
171  return;
172  }
173  if (con_inside_floating(con)) {
174  /* 'con' should be moved out of a floating container */
175  DLOG("Inside floating, moving to workspace\n");
176  attach_to_workspace(con, con_get_workspace(con), direction);
177  goto end;
178  }
179  DLOG("Force-changing orientation\n");
181  same_orientation = con_parent_with_orientation(con, o);
182  }
183 
184  /* easy case: the move is within this container */
185  if (same_orientation == con->parent) {
186  DLOG("We are in the same container\n");
187  Con *swap;
188  if ((swap = (direction == D_LEFT || direction == D_UP ? TAILQ_PREV(con, nodes_head, nodes) : TAILQ_NEXT(con, nodes)))) {
189  if (!con_is_leaf(swap)) {
190  DLOG("Moving into our bordering branch\n");
191  target = con_descend_direction(swap, direction);
192  position = (con_orientation(target->parent) != o ||
193  direction == D_UP ||
194  direction == D_LEFT
195  ? AFTER
196  : BEFORE);
197  insert_con_into(con, target, position);
198  goto end;
199  }
200  if (direction == D_LEFT || direction == D_UP)
201  TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
202  else
203  TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
204 
205  TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
206  TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
207 
208  DLOG("Swapped.\n");
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  return;
217  }
218 
219  /* If there was no con with which we could swap the current one,
220  * search again, but starting one level higher. */
221  same_orientation = con_parent_with_orientation(con->parent, o);
222  }
223  } while (same_orientation == NULL);
224 
225  /* this time, we have to move to another container */
226  /* This is the container *above* 'con' (an ancestor of con) which is inside
227  * 'same_orientation' */
228  Con *above = con;
229  while (above->parent != same_orientation)
230  above = above->parent;
231 
232  /* Enforce the fullscreen focus restrictions. */
234  LOG("Cannot move out of fullscreen container\n");
235  return;
236  }
237 
238  DLOG("above = %p\n", above);
239 
240  Con *next = (direction == D_UP || direction == D_LEFT ? TAILQ_PREV(above, nodes_head, nodes) : TAILQ_NEXT(above, nodes));
241 
242  if (next && !con_is_leaf(next)) {
243  DLOG("Moving into the bordering branch of our adjacent container\n");
244  target = con_descend_direction(next, direction);
245  position = (con_orientation(target->parent) != o ||
246  direction == D_UP ||
247  direction == D_LEFT
248  ? AFTER
249  : BEFORE);
250  insert_con_into(con, target, position);
251  } else {
252  DLOG("Moving into container above\n");
253  position = (direction == D_UP || direction == D_LEFT ? BEFORE : AFTER);
254  insert_con_into(con, above, position);
255  }
256 
257 end:
258  /* We need to call con_focus() to fix the focus stack "above" the container
259  * we just inserted the focused container into (otherwise, the parent
260  * container(s) would still point to the old container(s)). */
261  con_focus(con);
262 
263  /* force re-painting the indicators */
264  FREE(con->deco_render_params);
265 
267 }
struct Con * parent
Definition: data.h:512
void ipc_send_workspace_focus_event(Con *current, Con *old)
For the workspace "focus" event we send, along the usual "change" field, also the current and previou...
Definition: ipc.c:1058
static void insert_con_into(Con *con, Con *target, position_t position)
Definition: move.c:22
Output * get_output_by_name(const char *name)
Returns the output with the given name if it is active (!) or NULL.
Definition: randr.c:51
bool con_is_floating(Con *con)
Returns true if the node is floating.
Definition: con.c:419
direction_t
Definition: data.h:54
Con * workspace_attach_to(Con *ws)
Called when a new con (with a window, not an empty or split con) should be attached to the workspace ...
Definition: workspace.c:818
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:386
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
Definition: con.c:103
double percent
Definition: data.h:530
Output * get_output_next(direction_t direction, Output *current, output_close_far_t close_far)
Gets the output which is the next one in the given direction.
Definition: randr.c:161
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:317
Definition: data.h:54
#define TAILQ_INSERT_BEFORE(listelm, elm, field)
Definition: queue.h:379
#define LOG(fmt,...)
Definition: libi3.h:76
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
Definition: workspace.c:232
void con_detach(Con *con)
Detaches the given container from its current parent.
Definition: con.c:197
static void move_to_output_directed(Con *con, direction_t direction)
Definition: move.c:102
void floating_disable(Con *con, bool automatic)
Disables floating mode for the given container by re-attaching the container to its old parent...
Definition: floating.c:313
Con * con_parent_with_orientation(Con *con, orientation_t orientation)
Searches parenst of the given 'con' until it reaches one with the specified 'orientation'.
Definition: con.c:329
Definition: data.h:54
An Output is a physical output on your graphics driver.
Definition: data.h:301
#define TAILQ_NEXT(elm, field)
Definition: queue.h:325
Con * con_descend_direction(Con *con, direction_t direction)
Definition: con.c:1024
enum Con::@18 type
#define DLOG(fmt,...)
Definition: libi3.h:86
position_t
Definition: move.c:14
static void attach_to_workspace(Con *con, Con *ws, direction_t direction)
Definition: move.c:74
bool con_is_leaf(Con *con)
Returns true when this node is a leaf node (has no children)
Definition: con.c:241
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition: data.h:553
Definition: data.h:54
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:352
Con * focused
Definition: tree.c:15
Con * con
Pointer to the Con which represents this output.
Definition: data.h:319
char * name
Definition: data.h:520
bool con_fullscreen_permits_focusing(Con *con)
Returns true if changing the focus to con would be allowed considering the fullscreen focus constrain...
Definition: con.c:1486
#define TAILQ_INSERT_AFTER(head, listelm, elm, field)
Definition: queue.h:369
void con_focus(Con *con)
Sets input focus to the given container.
Definition: con.c:213
int con_num_children(Con *con)
Returns the number of children of this container.
Definition: con.c:526
void ws_force_orientation(Con *ws, orientation_t orientation)
'Forces' workspace orientation by moving all cons into a new split-con with the same orientation as t...
Definition: workspace.c:775
void tree_flatten(Con *con)
tree_flatten() removes pairs of redundant split containers, e.g.
Definition: tree.c:694
Definition: data.h:54
#define TAILQ_PREV(elm, headname, field)
Definition: queue.h:329
Definition: data.h:55
#define GREP_FIRST(dest, head, condition)
Definition: util.h:37
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Definition: con.c:843
Definition: move.c:14
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:479
Definition: move.c:15
orientation_t
Definition: data.h:55
#define CALL(obj, member,...)
Definition: util.h:54
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:362
struct Con * croot
Definition: tree.c:14
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition: output.c:18
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on...
Definition: con.c:303
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition: con.c:430
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition: con.c:542
Definition: data.h:55
#define FREE(pointer)
Definition: util.h:46
#define TAILQ_SWAP(first, second, head, field)
Definition: queue.h:410
void tree_move(int direction)
Moves the current container in the given direction (TOK_LEFT, TOK_RIGHT, TOK_UP, TOK_DOWN from cmdpar...
Definition: move.c:139