i3
move.c
Go to the documentation of this file.
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 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,
14 
15 /*
16  * This function detaches 'con' from its parent and inserts it either before or
17  * after 'target'.
18  *
19  */
20 static void insert_con_into(Con *con, Con *target, position_t position) {
21  Con *parent = target->parent;
22  /* We need to preserve the old con->parent. While it might still be used to
23  * insert the entry before/after it, we call the on_remove_child callback
24  * afterwards which might then close the con if it is empty. */
25  Con *old_parent = con->parent;
26 
27  con_detach(con);
28  con_fix_percent(con->parent);
29 
30  /* When moving to a workspace, we respect the user’s configured
31  * workspace_layout */
32  if (parent->type == CT_WORKSPACE) {
33  Con *split = workspace_attach_to(parent);
34  if (split != parent) {
35  DLOG("Got a new split con, using that one instead\n");
36  con->parent = split;
37  con_attach(con, split, false);
38  DLOG("attached\n");
39  con->percent = 0.0;
40  con_fix_percent(split);
41  con = split;
42  DLOG("ok, continuing with con %p instead\n", con);
43  con_detach(con);
44  }
45  }
46 
47  con->parent = parent;
48 
49  if (position == BEFORE) {
50  TAILQ_INSERT_BEFORE(target, con, nodes);
51  TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
52  } else if (position == AFTER) {
53  TAILQ_INSERT_AFTER(&(parent->nodes_head), target, con, nodes);
54  TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
55  }
56 
57  /* Pretend the con was just opened with regards to size percent values.
58  * Since the con is moved to a completely different con, the old value
59  * does not make sense anyways. */
60  con->percent = 0.0;
61  con_fix_percent(parent);
62 
63  CALL(old_parent, on_remove_child);
64 }
65 
66 /*
67  * This function detaches 'con' from its parent and puts it in the given
68  * workspace. Position is determined by the direction of movement into the
69  * workspace container.
70  *
71  */
72 static void attach_to_workspace(Con *con, Con *ws, direction_t direction) {
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  if (direction == D_RIGHT || direction == D_DOWN) {
81  TAILQ_INSERT_HEAD(&(ws->nodes_head), con, nodes);
82  TAILQ_INSERT_HEAD(&(ws->focus_head), con, focused);
83  } else {
84  TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
85  TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
86  }
87 
88  /* Pretend the con was just opened with regards to size percent values.
89  * Since the con is moved to a completely different con, the old value
90  * does not make sense anyways. */
91  con->percent = 0.0;
92  con_fix_percent(ws);
93 }
94 
95 /*
96  * Moves the given container to the closest output in the given direction if
97  * such an output exists.
98  *
99  */
100 static void move_to_output_directed(Con *con, direction_t direction) {
101  Con *old_ws = con_get_workspace(con);
102  Output *current_output = get_output_for_con(con);
103  Output *output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
104 
105  if (!output) {
106  DLOG("No output in this direction found. Not moving.\n");
107  return;
108  }
109 
110  Con *ws = NULL;
112 
113  if (!ws) {
114  DLOG("No workspace on output in this direction found. Not moving.\n");
115  return;
116  }
117 
118  attach_to_workspace(con, ws, direction);
119 
120  /* fix the focus stack */
121  con_focus(con);
122 
123  /* force re-painting the indicators */
124  FREE(con->deco_render_params);
125 
127 
128  ipc_send_workspace_event("focus", ws, old_ws);
129 }
130 
131 /*
132  * Moves the given container in the given direction (D_LEFT, D_RIGHT,
133  * D_UP, D_DOWN).
134  *
135  */
136 void tree_move(Con *con, int direction) {
137  position_t position;
138  Con *target;
139 
140  DLOG("Moving in direction %d\n", direction);
141 
142  /* 1: get the first parent with the same orientation */
143 
144  if (con->type == CT_WORKSPACE) {
145  DLOG("Not moving workspace\n");
146  return;
147  }
148 
149  if (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1) {
150  /* This is the only con on this workspace */
151  move_to_output_directed(con, direction);
152  return;
153  }
154 
155  orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT);
156 
157  Con *same_orientation = con_parent_with_orientation(con, o);
158  /* The do {} while is used to 'restart' at this point with a different
159  * same_orientation, see the very last lines before the end of this block
160  * */
161  do {
162  /* There is no parent container with the same orientation */
163  if (!same_orientation) {
164  if (con_is_floating(con)) {
165  /* this is a floating con, we just disable floating */
166  floating_disable(con, true);
167  return;
168  }
169  if (con_inside_floating(con)) {
170  /* 'con' should be moved out of a floating container */
171  DLOG("Inside floating, moving to workspace\n");
172  attach_to_workspace(con, con_get_workspace(con), direction);
173  goto end;
174  }
175  DLOG("Force-changing orientation\n");
177  same_orientation = con_parent_with_orientation(con, o);
178  }
179 
180  /* easy case: the move is within this container */
181  if (same_orientation == con->parent) {
182  DLOG("We are in the same container\n");
183  Con *swap;
184  if ((swap = (direction == D_LEFT || direction == D_UP ? TAILQ_PREV(con, nodes_head, nodes) : TAILQ_NEXT(con, nodes)))) {
185  if (!con_is_leaf(swap)) {
186  DLOG("Moving into our bordering branch\n");
187  target = con_descend_direction(swap, direction);
188  position = (con_orientation(target->parent) != o ||
189  direction == D_UP ||
190  direction == D_LEFT
191  ? AFTER
192  : BEFORE);
193  insert_con_into(con, target, position);
194  goto end;
195  }
196  if (direction == D_LEFT || direction == D_UP)
197  TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
198  else
199  TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
200 
201  TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
202  TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
203 
204  DLOG("Swapped.\n");
205  ipc_send_window_event("move", con);
207  return;
208  }
209 
210  if (con->parent == con_get_workspace(con)) {
211  /* If we couldn't find a place to move it on this workspace,
212  * try to move it to a workspace on a different output */
213  move_to_output_directed(con, direction);
214  ipc_send_window_event("move", con);
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 if (!next &&
252  con->parent->parent->type == CT_WORKSPACE &&
253  con->parent->layout != L_DEFAULT &&
254  con_num_children(con->parent) == 1) {
255  /* Con is the lone child of a non-default layout container at the edge
256  * of the workspace. Treat it as though the workspace is its parent
257  * and move it to the next output. */
258  DLOG("Grandparent is workspace\n");
259  move_to_output_directed(con, direction);
260  } else {
261  DLOG("Moving into container above\n");
262  position = (direction == D_UP || direction == D_LEFT ? BEFORE : AFTER);
263  insert_con_into(con, above, position);
264  }
265 
266 end:
267  /* We need to call con_focus() to fix the focus stack "above" the container
268  * we just inserted the focused container into (otherwise, the parent
269  * container(s) would still point to the old container(s)). */
270  con_focus(con);
271 
272  /* force re-painting the indicators */
273  FREE(con->deco_render_params);
274 
276  ipc_send_window_event("move", con);
278 }
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition: con.c:861
void con_detach(Con *con)
Detaches the given container from its current parent.
Definition: con.c:207
Con * focused
Definition: tree.c:13
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:366
void tree_flatten(Con *con)
tree_flatten() removes pairs of redundant split containers, e.g.
Definition: tree.c:694
Output * get_output_for_con(Con *con)
Returns the output for the given con.
Definition: output.c:55
#define CALL(obj, member,...)
Definition: util.h:58
direction_t
Definition: data.h:55
void ewmh_update_wm_desktop(void)
Updates _NET_WM_DESKTOP for all windows.
Definition: ewmh.c:182
#define TAILQ_INSERT_BEFORE(listelm, elm, field)
Definition: queue.h:394
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:591
#define TAILQ_PREV(elm, headname, field)
Definition: queue.h:342
#define LOG(fmt,...)
Definition: libi3.h:94
void con_focus(Con *con)
Sets input focus to the given container.
Definition: con.c:223
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:376
bool con_is_leaf(Con *con)
Returns true when this node is a leaf node (has no children)
Definition: con.c:289
position_t
Definition: move.c:12
void tree_move(Con *con, int direction)
Moves the given container in the given direction (TOK_LEFT, TOK_RIGHT, TOK_UP, TOK_DOWN from cmdparse...
Definition: move.c:136
void ipc_send_window_event(const char *property, Con *con)
For the window events we send, along the usual "change" field, also the window container, in "container".
Definition: ipc.c:1342
enum Con::@20 type
Definition: data.h:57
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition: con.c:535
#define FREE(pointer)
Definition: util.h:50
static void insert_con_into(Con *con, Con *target, position_t position)
Definition: move.c:20
Definition: data.h:60
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:204
nodes_head
Definition: data.h:672
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:405
int con_num_children(Con *con)
Returns the number of children of this container.
Definition: con.c:802
bool con_is_floating(Con *con)
Returns true if the node is floating.
Definition: con.c:510
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:805
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:848
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Definition: con.c:1317
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:342
#define TAILQ_INSERT_AFTER(head, listelm, elm, field)
Definition: queue.h:384
layout_t layout
Definition: data.h:701
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
Definition: data.h:61
Definition: data.h:55
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition: output.c:16
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:2016
Definition: data.h:92
double percent
Definition: data.h:653
An Output is a physical output on your graphics driver.
Definition: data.h:366
Con * con_descend_direction(Con *con, direction_t direction)
Definition: con.c:1499
static void move_to_output_directed(Con *con, direction_t direction)
Definition: move.c:100
#define DLOG(fmt,...)
Definition: libi3.h:104
Definition: data.h:56
#define TAILQ_NEXT(elm, field)
Definition: queue.h:338
Con * con
Pointer to the Con which represents this output.
Definition: data.h:387
#define TAILQ_SWAP(first, second, head, field)
Definition: queue.h:426
Con * con_parent_with_orientation(Con *con, orientation_t orientation)
Searches parents of the given 'con' until it reaches one with the specified 'orientation'.
Definition: con.c:417
orientation_t
Definition: data.h:59
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
Definition: con.c:199
Definition: move.c:12
void ipc_send_workspace_event(const char *change, Con *current, Con *old)
For the workspace events we send, along with the usual "change" field, also the workspace container i...
Definition: ipc.c:1326
static void attach_to_workspace(Con *con, Con *ws, direction_t direction)
Definition: move.c:72
Definition: data.h:58
#define GREP_FIRST(dest, head, condition)
Definition: util.h:41
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition: data.h:665
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
Definition: workspace.c:254
Definition: move.c:13
struct Con * parent
Definition: data.h:623
struct Con * croot
Definition: tree.c:12
focus_head
Definition: data.h:675