diff --git a/src/move.c b/src/move.c index 46b90177..0b3ab660 100644 --- a/src/move.c +++ b/src/move.c @@ -65,11 +65,12 @@ static void insert_con_into(Con *con, Con *target, position_t position) { } /* - * This function detaches 'con' from its parent and inserts it at the given - * workspace. + * This function detaches 'con' from its parent and puts it in the given + * workspace. Position is determined by the direction of movement into the + * workspace container. * */ -static void attach_to_workspace(Con *con, Con *ws) { +static void attach_to_workspace(Con *con, Con *ws, direction_t direction) { con_detach(con); con_fix_percent(con->parent); @@ -77,8 +78,13 @@ static void attach_to_workspace(Con *con, Con *ws) { con->parent = ws; - TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes); - TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused); + if (direction == D_RIGHT || direction == D_DOWN) { + TAILQ_INSERT_HEAD(&(ws->nodes_head), con, nodes); + TAILQ_INSERT_HEAD(&(ws->focus_head), con, focused); + } else { + TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes); + TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused); + } /* Pretend the con was just opened with regards to size percent values. * Since the con is moved to a completely different con, the old value @@ -87,6 +93,32 @@ static void attach_to_workspace(Con *con, Con *ws) { con_fix_percent(ws); } +/* + * Moves the given container to the closest output in the given direction if + * such an output exists. + * + */ +static void move_to_output_directed(Con *con, direction_t direction) { + Con *current_output_con = con_get_output(con); + Output *current_output = get_output_by_name(current_output_con->name); + Output *output = get_output_next(direction, current_output, CLOSEST_OUTPUT); + + if (!output) { + DLOG("No output in this direction found. Not moving.\n"); + return; + } + + Con *ws = NULL; + GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child)); + + if (!ws) { + DLOG("No workspace on output in this direction found. Not moving.\n"); + return; + } + + attach_to_workspace(con, ws, direction); +} + /* * Moves the current container in the given direction (D_LEFT, D_RIGHT, * D_UP, D_DOWN). @@ -103,8 +135,9 @@ void tree_move(int direction) { } if (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1) { - DLOG("This is the only con on this workspace, not doing anything\n"); - return; + /* This is the only con on this workspace */ + move_to_output_directed(con, direction); + goto end; } orientation_t o = (direction == D_LEFT || direction == D_RIGHT ? HORIZ : VERT); @@ -124,7 +157,7 @@ void tree_move(int direction) { if (con_inside_floating(con)) { /* 'con' should be moved out of a floating container */ DLOG("Inside floating, moving to workspace\n"); - attach_to_workspace(con, con_get_workspace(con)); + attach_to_workspace(con, con_get_workspace(con), direction); goto end; } DLOG("Force-changing orientation\n"); @@ -154,12 +187,15 @@ void tree_move(int direction) { return; } - /* If there was no con with which we could swap the current one, search - * again, but starting one level higher. If we are on the workspace - * level, don’t do that. The result would be a force change of - * workspace orientation, which is not necessary. */ - if (con->parent == con_get_workspace(con)) - return; + if (con->parent == con_get_workspace(con)) { + /* If we couldn't find a place to move it on this workspace, + * try to move it to a workspace on a different output */ + move_to_output_directed(con, direction); + goto end; + } + + /* If there was no con with which we could swap the current one, + * search again, but starting one level higher. */ same_orientation = con_parent_with_orientation(con->parent, o); } } while (same_orientation == NULL); diff --git a/testcases/t/516-move.t b/testcases/t/516-move.t new file mode 100644 index 00000000..512e20b9 --- /dev/null +++ b/testcases/t/516-move.t @@ -0,0 +1,106 @@ +#!perl +# vim:ts=4:sw=4:expandtab +# +# Please read the following documents before working on tests: +# • http://build.i3wm.org/docs/testsuite.html +# (or docs/testsuite) +# +# • http://build.i3wm.org/docs/lib-i3test.html +# (alternatively: perldoc ./testcases/lib/i3test.pm) +# +# • http://build.i3wm.org/docs/ipc.html +# (or docs/ipc) +# +# • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf +# (unless you are already familiar with Perl) +# +# Tests if a simple 'move ' command will move containers across outputs. +# +use i3test i3_autostart => 0; + +# Ensure the pointer is at (0, 0) so that we really start on the first +# (the left) workspace. +$x->root->warp_pointer(0, 0); + +my $config = <unmap; +wait_for_unmap; + +##################################################################### +# Try to move a window on a workspace with two windows across outputs in each +# direction +##################################################################### + +# from left-top to right-top +cmd('workspace left-top'); +cmd('split h'); +my $first_window = open_window; +my $social_window = open_window( name => 'CORRECT_WINDOW' ); +cmd('move right'); +is(scalar @{get_ws_content('right-top')}, 1, 'moved some window to right-top workspace'); +my $compare_window = shift @{get_ws_content('right-top')}; +is($compare_window->{name}, $social_window->name, 'moved correct window to right-top workspace'); +# unamp the first window so we don't confuse it when we move back here +$first_window->unmap; +wait_for_unmap; + +# from right-top to right-bottom +cmd('split v'); +open_window; +# this window opened above - we need to move down twice +cmd('focus up; move down; move down'); +is(scalar @{get_ws_content('right-bottom')}, 1, 'moved some window to right-bottom workspace'); +$compare_window = shift @{get_ws_content('right-bottom')}; +is($compare_window->{name}, $social_window->name, 'moved correct window to right-bottom workspace'); + +# from right-bottom to left-bottom +cmd('split h'); +open_window; +cmd('focus left; move left'); +is(scalar @{get_ws_content('left-bottom')}, 1, 'moved some window to left-bottom workspace'); +$compare_window = shift @{get_ws_content('left-bottom')}; +is($social_window->name, $compare_window->{name}, 'moved correct window to left-bottom workspace'); + +# from left-bottom to left-top +cmd('split v'); +open_window; +cmd('focus up; move up'); +is(scalar @{get_ws_content('left-top')}, 1, 'moved some window to left-bottom workspace'); +$compare_window = shift @{get_ws_content('left-top')}; +is($social_window->name, $compare_window->{name}, 'moved correct window to left-bottom workspace'); + +exit_gracefully($pid); + +done_testing;