i3
move.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved 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/*
13 * Returns the lowest container in the tree that has both a and b as descendants.
14 *
15 */
17 Con *parent_a = a;
18 while (parent_a) {
19 Con *parent_b = b;
20 while (parent_b) {
21 if (parent_a == parent_b) {
22 return parent_a;
23 }
24 parent_b = parent_b->parent;
25 }
26 parent_a = parent_a->parent;
27 }
28 assert(false);
29}
30
31/*
32 * Returns the direct child of ancestor that contains con.
33 *
34 */
35static Con *child_containing_con_recursively(Con *ancestor, Con *con) {
36 Con *child = con;
37 while (child && child->parent != ancestor) {
38 child = child->parent;
39 assert(child->parent);
40 }
41 return child;
42}
43
44/*
45 * Returns true if the given container is the focused descendant of ancestor, recursively.
46 *
47 */
48static bool is_focused_descendant(Con *con, Con *ancestor) {
49 Con *current = con;
50 while (current != ancestor) {
51 if (TAILQ_FIRST(&(current->parent->focus_head)) != current) {
52 return false;
53 }
54 current = current->parent;
55 assert(current->parent);
56 }
57 return true;
58}
59
60/*
61 * This function detaches 'con' from its parent and inserts it either before or
62 * after 'target'.
63 *
64 */
65void insert_con_into(Con *con, Con *target, position_t position) {
66 Con *parent = target->parent;
67 /* We need to preserve the old con->parent. While it might still be used to
68 * insert the entry before/after it, we call the on_remove_child callback
69 * afterwards which might then close the con if it is empty. */
70 Con *old_parent = con->parent;
71
72 /* We compare the focus order of the children of the lowest common ancestor. If con or
73 * its ancestor is before target's ancestor then con should be placed before the target
74 * in the focus stack. */
75 Con *lca = lowest_common_ancestor(con, parent);
76 if (lca == con) {
77 ELOG("Container is being inserted into one of its descendants.\n");
78 return;
79 }
80
81 Con *con_ancestor = child_containing_con_recursively(lca, con);
82 Con *target_ancestor = child_containing_con_recursively(lca, target);
83 bool moves_focus_from_ancestor = is_focused_descendant(con, con_ancestor);
84 bool focus_before;
85
86 /* Determine if con is going to be placed before or after target in the parent's focus stack. */
87 if (con_ancestor == target_ancestor) {
88 /* Happens when the target is con's old parent. Eg with layout V [ A H [ B C ] ],
89 * if we move C up. Target will be H. */
90 focus_before = moves_focus_from_ancestor;
91 } else {
92 /* Look at the focus stack order of the children of the lowest common ancestor. */
93 Con *current;
94 TAILQ_FOREACH (current, &(lca->focus_head), focused) {
95 if (current == con_ancestor || current == target_ancestor) {
96 break;
97 }
98 }
99 focus_before = (current == con_ancestor);
100 }
101
102 /* If con is the focused container in our old ancestor we place the new ancestor
103 * before the old ancestor in the focus stack. Example:
104 * Consider the layout [ H [ V1 [ A* B ] V2 [ C ] ] ] where A is focused. We move to
105 * a second workspace and from there we move A to the right and switch back to the
106 * original workspace. Without the change focus would move to B instead of staying
107 * with A. */
108 if (moves_focus_from_ancestor && focus_before) {
109 Con *place = TAILQ_PREV(con_ancestor, focus_head, focused);
110 TAILQ_REMOVE(&(lca->focus_head), target_ancestor, focused);
111 if (place) {
112 TAILQ_INSERT_AFTER(&(lca->focus_head), place, target_ancestor, focused);
113 } else {
114 TAILQ_INSERT_HEAD(&(lca->focus_head), target_ancestor, focused);
115 }
116 }
117
118 con_detach(con);
120
121 /* When moving to a workspace, we respect the user’s configured
122 * workspace_layout */
123 if (parent->type == CT_WORKSPACE) {
124 Con *split = workspace_attach_to(parent);
125 if (split != parent) {
126 DLOG("Got a new split con, using that one instead\n");
127 con->parent = split;
128 con_attach(con, split, false);
129 DLOG("attached\n");
130 con->percent = 0.0;
131 con_fix_percent(split);
132 con = split;
133 DLOG("ok, continuing with con %p instead\n", con);
134 con_detach(con);
135 }
136 }
137
138 con->parent = parent;
139
140 if (parent == lca) {
141 if (focus_before) {
142 /* Example layout: H [ A B* ], we move A up/down. 'target' will be H. */
143 TAILQ_INSERT_BEFORE(target, con, focused);
144 } else {
145 /* Example layout: H [ A B* ], we move A up/down. 'target' will be H. */
146 TAILQ_INSERT_AFTER(&(parent->focus_head), target, con, focused);
147 }
148 } else {
149 if (focus_before) {
150 /* Example layout: V [ H [ A B ] C* ], we move C up. 'target' will be A. */
151 TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
152 } else {
153 /* Example layout: V [ H [ A* B ] C ], we move C up. 'target' will be A. */
154 TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
155 }
156 }
157
158 if (position == BEFORE) {
159 TAILQ_INSERT_BEFORE(target, con, nodes);
160 } else if (position == AFTER) {
161 TAILQ_INSERT_AFTER(&(parent->nodes_head), target, con, nodes);
162 }
163
164 /* Pretend the con was just opened with regards to size percent values.
165 * Since the con is moved to a completely different con, the old value
166 * does not make sense anyways. */
167 con->percent = 0.0;
168 con_fix_percent(parent);
169
170 CALL(old_parent, on_remove_child);
171}
172
173/*
174 * This function detaches 'con' from its parent and puts it in the given
175 * workspace. Position is determined by the direction of movement into the
176 * workspace container.
177 *
178 */
179static void attach_to_workspace(Con *con, Con *ws, direction_t direction) {
180 con_detach(con);
181 Con *old_parent = con->parent;
182 con->parent = ws;
183
184 if (direction == D_RIGHT || direction == D_DOWN) {
185 TAILQ_INSERT_HEAD(&(ws->nodes_head), con, nodes);
186 } else {
187 TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
188 }
189 TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
190
191 /* Pretend the con was just opened with regards to size percent values.
192 * Since the con is moved to a completely different con, the old value
193 * does not make sense anyways. */
194 con->percent = 0.0;
195 con_fix_percent(ws);
196
197 con_fix_percent(old_parent);
198 CALL(old_parent, on_remove_child);
199}
200
201/*
202 * Moves the given container to the closest output in the given direction if
203 * such an output exists.
204 *
205 */
206static void move_to_output_directed(Con *con, direction_t direction) {
207 Output *current_output = get_output_for_con(con);
208 Output *output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
209
210 if (!output) {
211 DLOG("No output in this direction found. Not moving.\n");
212 return;
213 }
214
215 Con *ws = NULL;
217
218 if (!ws) {
219 DLOG("No workspace on output in this direction found. Not moving.\n");
220 return;
221 }
222
223 Con *old_ws = con_get_workspace(con);
224 const bool moves_focus = (focused == con);
225 attach_to_workspace(con, ws, direction);
226 if (moves_focus) {
227 /* workspace_show will not correctly update the active workspace because
228 * the focused container, con, is now a child of ws. To work around this
229 * and still produce the correct workspace focus events (see
230 * 517-regress-move-direction-ipc.t) we need to temporarily set focused
231 * to the old workspace.
232 *
233 * The following happen:
234 * 1. Focus con to push it on the top of the focus stack in its new
235 * workspace
236 * 2. Set focused to the old workspace to force workspace_show to
237 * execute
238 * 3. workspace_show will descend focus and target our con for
239 * focusing. This also ensures that the mouse warps correctly.
240 * See: #3518. */
241 con_focus(con);
242 focused = old_ws;
243 workspace_show(ws);
244 con_focus(con);
245 }
246
247 /* force re-painting the indicators */
249
250 ipc_send_window_event("move", con);
253}
254
255/*
256 * Moves the given container in the given direction
257 *
258 */
259void tree_move(Con *con, direction_t direction) {
260 position_t position;
261 Con *target;
262
263 DLOG("Moving in direction %d\n", direction);
264
265 /* 1: get the first parent with the same orientation */
266
267 if (con->type == CT_WORKSPACE) {
268 DLOG("Not moving workspace\n");
269 return;
270 }
271
272 if (con->fullscreen_mode == CF_GLOBAL) {
273 DLOG("Not moving fullscreen global container\n");
274 return;
275 }
276
277 if ((con->fullscreen_mode == CF_OUTPUT) ||
278 (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1)) {
279 /* This is the only con on this workspace */
280 move_to_output_directed(con, direction);
281 return;
282 }
283
285
286 Con *same_orientation = con_parent_with_orientation(con, o);
287 /* The do {} while is used to 'restart' at this point with a different
288 * same_orientation, see the very last lines before the end of this block
289 * */
290 do {
291 /* There is no parent container with the same orientation */
292 if (!same_orientation) {
293 if (con_is_floating(con)) {
294 /* this is a floating con, we just disable floating */
295 floating_disable(con);
296 return;
297 }
298 if (con_inside_floating(con)) {
299 /* 'con' should be moved out of a floating container */
300 DLOG("Inside floating, moving to workspace\n");
301 attach_to_workspace(con, con_get_workspace(con), direction);
302 goto end;
303 }
304 DLOG("Force-changing orientation\n");
306 same_orientation = con_parent_with_orientation(con, o);
307 }
308
309 /* easy case: the move is within this container */
310 if (same_orientation == con->parent) {
311 Con *swap = (direction == D_LEFT || direction == D_UP)
312 ? TAILQ_PREV(con, nodes_head, nodes)
313 : TAILQ_NEXT(con, nodes);
314 if (swap) {
315 if (!con_is_leaf(swap)) {
316 DLOG("Moving into our bordering branch\n");
317 target = con_descend_direction(swap, direction);
318 position = (con_orientation(target->parent) != o ||
319 direction == D_UP ||
320 direction == D_LEFT
321 ? AFTER
322 : BEFORE);
323 insert_con_into(con, target, position);
324 goto end;
325 }
326
327 DLOG("Swapping with sibling.\n");
328 if (direction == D_LEFT || direction == D_UP) {
329 TAILQ_SWAP(swap, con, &(swap->parent->nodes_head), nodes);
330 } else {
331 TAILQ_SWAP(con, swap, &(swap->parent->nodes_head), nodes);
332 }
333
334 /* redraw parents to ensure all parent split container titles are updated correctly */
336
337 ipc_send_window_event("move", con);
338 return;
339 }
340
341 if (con->parent == con_get_workspace(con)) {
342 /* If we couldn't find a place to move it on this workspace, try
343 * to move it to a workspace on a different output */
344 move_to_output_directed(con, direction);
345 return;
346 }
347
348 /* If there was no con with which we could swap the current one,
349 * search again, but starting one level higher. */
350 same_orientation = con_parent_with_orientation(con->parent, o);
351 }
352 } while (same_orientation == NULL);
353
354 /* this time, we have to move to another container */
355 /* This is the container *above* 'con' (an ancestor of con) which is inside
356 * 'same_orientation' */
357 Con *above = con;
358 while (above->parent != same_orientation) {
359 above = above->parent;
360 }
361
362 /* Enforce the fullscreen focus restrictions. */
364 LOG("Cannot move out of fullscreen container\n");
365 return;
366 }
367
368 DLOG("above = %p\n", above);
369
370 Con *next = (direction == D_UP || direction == D_LEFT ? TAILQ_PREV(above, nodes_head, nodes) : TAILQ_NEXT(above, nodes));
371
372 if (next && !con_is_leaf(next)) {
373 DLOG("Moving into the bordering branch of our adjacent container\n");
374 target = con_descend_direction(next, direction);
375 position = (con_orientation(target->parent) != o ||
376 direction == D_UP ||
377 direction == D_LEFT
378 ? AFTER
379 : BEFORE);
380 insert_con_into(con, target, position);
381 } else if (!next &&
382 con->parent->parent->type == CT_WORKSPACE &&
383 con->parent->layout != L_DEFAULT &&
384 con_num_children(con->parent) == 1) {
385 /* Con is the lone child of a non-default layout container at the edge
386 * of the workspace. Treat it as though the workspace is its parent
387 * and move it to the next output. */
388 DLOG("Grandparent is workspace\n");
389 move_to_output_directed(con, direction);
390 return;
391 } else {
392 DLOG("Moving into container above\n");
393 position = (direction == D_UP || direction == D_LEFT ? BEFORE : AFTER);
394 insert_con_into(con, above, position);
395 }
396
397end:
398 /* force re-painting the indicators */
400
401 ipc_send_window_event("move", con);
404}
bool con_is_floating(Con *con)
Returns true if the node is floating.
Definition con.c:670
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Definition con.c:1625
void con_force_split_parents_redraw(Con *con)
force parent split containers to be redrawn
Definition con.c:21
Con * con_descend_direction(Con *con, direction_t direction)
Returns the leftmost, rightmost, etc.
Definition con.c:1742
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition con.c:547
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition con.c:696
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:2334
void con_detach(Con *con)
Detaches the given container from its current parent.
Definition con.c:234
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition con.c:1144
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
Definition con.c:226
bool con_is_leaf(Con *con)
Returns true when this node is a leaf node (has no children)
Definition con.c:366
int con_num_children(Con *con)
Returns the number of children of this container.
Definition con.c:1075
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:560
void con_focus(Con *con)
Sets input focus to the given container.
Definition con.c:250
void ewmh_update_wm_desktop(void)
Updates _NET_WM_DESKTOP for all windows.
Definition ewmh.c:186
void floating_disable(Con *con)
Disables floating mode for the given container by re-attaching the container to its old parent.
Definition floating.c:420
static bool is_focused_descendant(Con *con, Con *ancestor)
Definition move.c:48
static void attach_to_workspace(Con *con, Con *ws, direction_t direction)
Definition move.c:179
void insert_con_into(Con *con, Con *target, position_t position)
This function detaches 'con' from its parent and inserts it either before or after 'target'.
Definition move.c:65
void tree_move(Con *con, direction_t direction)
Moves the given container in the given direction.
Definition move.c:259
static Con * child_containing_con_recursively(Con *ancestor, Con *con)
Definition move.c:35
static void move_to_output_directed(Con *con, direction_t direction)
Definition move.c:206
static Con * lowest_common_ancestor(Con *a, Con *b)
Definition move.c:16
Output * get_output_for_con(Con *con)
Retrieves the output for a given container.
Definition output.c:63
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition output.c:16
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:255
struct Con * focused
Definition tree.c:13
void tree_flatten(Con *con)
tree_flatten() removes pairs of redundant split containers, e.g.: [workspace, horizontal] [v-split] [...
Definition tree.c:660
struct Con * croot
Definition tree.c:12
orientation_t orientation_from_direction(direction_t direction)
Convert a direction to its corresponding orientation.
Definition util.c:466
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition workspace.c:438
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
Definition workspace.c:320
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:996
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:953
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,...
Definition ipc.c:1650
position_t
Definition data.h:63
@ AFTER
Definition data.h:64
@ BEFORE
Definition data.h:63
@ L_DEFAULT
Definition data.h:102
orientation_t
Definition data.h:60
@ CF_OUTPUT
Definition data.h:630
@ CF_GLOBAL
Definition data.h:631
direction_t
Definition data.h:56
@ D_RIGHT
Definition data.h:57
@ D_LEFT
Definition data.h:56
@ D_UP
Definition data.h:58
@ D_DOWN
Definition data.h:59
#define DLOG(fmt,...)
Definition libi3.h:105
#define LOG(fmt,...)
Definition libi3.h:95
#define ELOG(fmt,...)
Definition libi3.h:100
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:347
#define TAILQ_SWAP(first, second, head, field)
Definition queue.h:426
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:376
#define TAILQ_PREV(elm, headname, field)
Definition queue.h:342
#define TAILQ_FIRST(head)
Definition queue.h:336
#define TAILQ_REMOVE(head, elm, field)
Definition queue.h:402
#define TAILQ_NEXT(elm, field)
Definition queue.h:338
#define TAILQ_INSERT_BEFORE(listelm, elm, field)
Definition queue.h:394
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition queue.h:366
#define TAILQ_INSERT_AFTER(head, listelm, elm, field)
Definition queue.h:384
@ CLOSEST_OUTPUT
Definition randr.h:23
#define CALL(obj, member,...)
Definition util.h:53
#define GREP_FIRST(dest, head, condition)
Definition util.h:38
#define FREE(pointer)
Definition util.h:47
An Output is a physical output on your graphics driver.
Definition data.h:391
Con * con
Pointer to the Con which represents this output.
Definition data.h:411
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition data.h:643
struct Con * parent
Definition data.h:678
enum Con::@18 type
double percent
Definition data.h:712
layout_t layout
Definition data.h:755
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition data.h:724
fullscreen_mode_t fullscreen_mode
Definition data.h:734