diff --git a/docs/userguide b/docs/userguide index f1aba9da..0ba705a6 100644 --- a/docs/userguide +++ b/docs/userguide @@ -1055,9 +1055,9 @@ specific workspace and immediately switch to that workspace, you can configure the following keybinding: *Example*: -------------------------------------------- -bindsym mod+x move workspace 3; workspace 3 -------------------------------------------- +-------------------------------------------------------- +bindsym mod+x move container to workspace 3; workspace 3 +-------------------------------------------------------- [[command_criteria]] @@ -1241,24 +1241,29 @@ bindsym mod+j move left 20 px To change to a specific workspace, use the +workspace+ command, followed by the number or name of the workspace. To move containers to specific workspaces, use -+move workspace+. ++move container to workspace+. You can also switch to the next and previous workspace with the commands +workspace next+ and +workspace prev+, which is handy, for example, if you have workspace 1, 3, 4 and 9 and you want to cycle through them with a single key combination. To restrict those to the current output, use +workspace next_on_output+ and +workspace prev_on_output+. Similarly, you can use +move -workspace next+ and +move workspace prev+ to move a container to the -next/previous workspace. +container to workspace next+ and +move container to workspace prev+ to move a +container to the next/previous workspace. [[back_and_forth]] To switch back to the previously focused workspace, use +workspace back_and_forth+. To move a container to another xrandr output such as +LVDS1+ or +VGA1+, you can -use the +move output+ command followed by the name of the target output. You -may also use +left+, +right+, +up+, +down+ instead of the xrandr output name to -move to the next output in the specified direction. +use the +move container to output+ command followed by the name of the target +output. You may also use +left+, +right+, +up+, +down+ instead of the xrandr +output name to move to the next output in the specified direction. + +To move a whole workspace to another xrandr output such as +LVDS1+ or +VGA1+, +you can use the +move workspace to output+ command followed by the name of the +target output. You may also use +left+, +right+, +up+, +down+ instead of the +xrandr output name to move to the next output in the specified direction. *Examples*: ------------------------- @@ -1266,12 +1271,15 @@ bindsym mod+1 workspace 1 bindsym mod+2 workspace 2 ... -bindsym mod+Shift+1 move workspace 1 -bindsym mod+Shift+2 move workspace 2 +bindsym mod+Shift+1 move container to workspace 1 +bindsym mod+Shift+2 move container to workspace 2 ... # switch between the current and the previously focused one bindsym mod+b workspace back_and_forth + +# move the whole workspace to the next output +bindsym mod+x move workspace to output right ------------------------- ==== Named workspaces diff --git a/i3-migrate-config-to-v4 b/i3-migrate-config-to-v4 index 4f4d0134..c8ff41c9 100755 --- a/i3-migrate-config-to-v4 +++ b/i3-migrate-config-to-v4 @@ -316,10 +316,10 @@ sub convert_command { if ($command =~ /^m[0-9]+/) { my ($number) = ($command =~ /^m([0-9]+)/); if (exists $workspace_names{$number}) { - print qq|$statement $key move workspace $workspace_names{$number}\n|; + print qq|$statement $key move container to workspace $workspace_names{$number}\n|; return; } else { - print qq|$statement $key move workspace $number\n|; + print qq|$statement $key move container to workspace $number\n|; return; } } diff --git a/i3.config b/i3.config index f5dc13f6..4ee61d4b 100644 --- a/i3.config +++ b/i3.config @@ -87,16 +87,16 @@ bindsym Mod1+9 workspace 9 bindsym Mod1+0 workspace 10 # move focused container to workspace -bindsym Mod1+Shift+1 move workspace 1 -bindsym Mod1+Shift+2 move workspace 2 -bindsym Mod1+Shift+3 move workspace 3 -bindsym Mod1+Shift+4 move workspace 4 -bindsym Mod1+Shift+5 move workspace 5 -bindsym Mod1+Shift+6 move workspace 6 -bindsym Mod1+Shift+7 move workspace 7 -bindsym Mod1+Shift+8 move workspace 8 -bindsym Mod1+Shift+9 move workspace 9 -bindsym Mod1+Shift+0 move workspace 10 +bindsym Mod1+Shift+1 move container to workspace 1 +bindsym Mod1+Shift+2 move container to workspace 2 +bindsym Mod1+Shift+3 move container to workspace 3 +bindsym Mod1+Shift+4 move container to workspace 4 +bindsym Mod1+Shift+5 move container to workspace 5 +bindsym Mod1+Shift+6 move container to workspace 6 +bindsym Mod1+Shift+7 move container to workspace 7 +bindsym Mod1+Shift+8 move container to workspace 8 +bindsym Mod1+Shift+9 move container to workspace 9 +bindsym Mod1+Shift+0 move container to workspace 10 # reload the configuration file bindsym Mod1+Shift+c reload diff --git a/i3.config.keycodes b/i3.config.keycodes index dcab5196..af081de0 100644 --- a/i3.config.keycodes +++ b/i3.config.keycodes @@ -88,16 +88,16 @@ bindcode $mod+18 workspace 9 bindcode $mod+19 workspace 10 # move focused container to workspace -bindcode $mod+Shift+10 move workspace 1 -bindcode $mod+Shift+11 move workspace 2 -bindcode $mod+Shift+12 move workspace 3 -bindcode $mod+Shift+13 move workspace 4 -bindcode $mod+Shift+14 move workspace 5 -bindcode $mod+Shift+15 move workspace 6 -bindcode $mod+Shift+16 move workspace 7 -bindcode $mod+Shift+17 move workspace 8 -bindcode $mod+Shift+18 move workspace 9 -bindcode $mod+Shift+19 move workspace 10 +bindcode $mod+Shift+10 move container to workspace 1 +bindcode $mod+Shift+11 move container to workspace 2 +bindcode $mod+Shift+12 move container to workspace 3 +bindcode $mod+Shift+13 move container to workspace 4 +bindcode $mod+Shift+14 move container to workspace 5 +bindcode $mod+Shift+15 move container to workspace 6 +bindcode $mod+Shift+16 move container to workspace 7 +bindcode $mod+Shift+17 move container to workspace 8 +bindcode $mod+Shift+18 move container to workspace 9 +bindcode $mod+Shift+19 move container to workspace 10 # reload the configuration file bindcode $mod+Shift+54 reload diff --git a/src/cmdparse.l b/src/cmdparse.l index d382c8db..cbce18d4 100644 --- a/src/cmdparse.l +++ b/src/cmdparse.l @@ -48,6 +48,9 @@ EOL (\r?\n) /* handle a quoted string or everything up to the next whitespace */ %s WANT_QSTRING +%x MOVE +%x MOVE_WS + %x EXEC %x BUFFER_LINE @@ -74,14 +77,38 @@ EOL (\r?\n) cmdyycolumn = 1; } - /* the next/prev/back_and_forth tokens are here to recognize them *before* - * handling strings ('workspace' command) */ + /* The next/prev/back_and_forth tokens are here to recognize them *before* + * handling strings ('workspace' command). While flex uses the longest + * match, in case of a tie the order of rules becomes relevant. Since the + * input is fully consumed (these are the last tokens), it comes to a tie. + * */ next { BEGIN(INITIAL); return TOK_NEXT; } prev { BEGIN(INITIAL); return TOK_PREV; } next_on_output { BEGIN(INITIAL); return TOK_NEXT_ON_OUTPUT; } prev_on_output { BEGIN(INITIAL); return TOK_PREV_ON_OUTPUT; } back_and_forth { BEGIN(INITIAL); return TOK_BACK_AND_FORTH; } + /* MOVE is the state after a 'move' token was processed. We need this state + * to skip some tokens (for making the commands clearer) and to properly + * move to the MOVE_WS state. */ +to { /* eat this token */ } +window { /* eat this token */ } +container { /* eat this token */ } +workspace { yy_pop_state(); yy_push_state(MOVE_WS); yy_push_state(EAT_WHITESPACE); return TOK_WORKSPACE; } +scratchpad { yy_pop_state(); return TOK_SCRATCHPAD; } +up { yy_pop_state(); return TOK_UP; } +down { yy_pop_state(); return TOK_DOWN; } +left { yy_pop_state(); return TOK_LEFT; } +right { yy_pop_state(); return TOK_RIGHT; } + + /* MOVE_WS is the state after a 'workspace' token was processed in the MOVE + * state. We need a separate state to deal with the fact that the old + * 'move workspace ' command needs to be supported (the new command is + * 'move to workspace') while we also need to support + * 'move workspace to output '. */ +to { yy_pop_state(); return TOK_TO; } +[^to] { yy_pop_state(); yy_push_state(WANT_STRING); yyless(0); } + \"[^\"]+\" { BEGIN(INITIAL); /* strip quotes */ @@ -113,6 +140,7 @@ reload { return TOK_RELOAD; } restart { return TOK_RESTART; } kill { return TOK_KILL; } window { return TOK_WINDOW; } +container { return TOK_CONTAINER; } client { return TOK_CLIENT; } fullscreen { return TOK_FULLSCREEN; } global { return TOK_GLOBAL; } @@ -133,7 +161,7 @@ mode_toggle { return TOK_MODE_TOGGLE; } workspace { WS_STRING; return TOK_WORKSPACE; } output { WS_STRING; return TOK_OUTPUT; } focus { return TOK_FOCUS; } -move { return TOK_MOVE; } +move { yy_push_state(MOVE); return TOK_MOVE; } open { return TOK_OPEN; } scratchpad { return TOK_SCRATCHPAD; } show { return TOK_SHOW; } @@ -152,6 +180,7 @@ grow { return TOK_GROW; } px { return TOK_PX; } or { return TOK_OR; } ppt { return TOK_PPT; } +to { return TOK_TO; } nop { WS_STRING; return TOK_NOP; } append_layout { WS_STRING; return TOK_APPEND_LAYOUT; } mark { WS_STRING; return TOK_MARK; } diff --git a/src/cmdparse.y b/src/cmdparse.y index 28aa564e..9ae17ff9 100644 --- a/src/cmdparse.y +++ b/src/cmdparse.y @@ -105,6 +105,31 @@ char *parse_cmd(const char *new) { return json_output; } +static Output *get_output_from_string(Output *current_output, const char *output_str) { + Output *output; + + if (strcasecmp(output_str, "left") == 0) { + output = get_output_next(D_LEFT, current_output); + if (!output) + output = get_output_most(D_RIGHT, current_output); + } else if (strcasecmp(output_str, "right") == 0) { + output = get_output_next(D_RIGHT, current_output); + if (!output) + output = get_output_most(D_LEFT, current_output); + } else if (strcasecmp(output_str, "up") == 0) { + output = get_output_next(D_UP, current_output); + if (!output) + output = get_output_most(D_DOWN, current_output); + } else if (strcasecmp(output_str, "down") == 0) { + output = get_output_next(D_DOWN, current_output); + if (!output) + output = get_output_most(D_UP, current_output); + } else output = get_output_by_name(output_str); + + return output; +} + + /* * Returns true if a is definitely greater than b (using the given epsilon) * @@ -130,6 +155,7 @@ bool definitelyGreaterThan(float a, float b, float epsilon) { %token TOK_RESTART "restart" %token TOK_KILL "kill" %token TOK_WINDOW "window" +%token TOK_CONTAINER "container" %token TOK_CLIENT "client" %token TOK_FULLSCREEN "fullscreen" %token TOK_GLOBAL "global" @@ -179,6 +205,7 @@ bool definitelyGreaterThan(float a, float b, float epsilon) { %token TOK_NOP "nop" %token TOK_BACK_AND_FORTH "back_and_forth" %token TOK_NO_STARTUP_ID "--no-startup-id" +%token TOK_TO "to" %token TOK_CLASS "class" %token TOK_INSTANCE "instance" @@ -544,23 +571,7 @@ focus: current_output = get_output_containing(current->con->rect.x, current->con->rect.y); assert(current_output != NULL); - if (strcasecmp($3, "left") == 0) { - output = get_output_next(D_LEFT, current_output); - if (!output) - output = get_output_most(D_RIGHT, current_output); - } else if (strcasecmp($3, "right") == 0) { - output = get_output_next(D_RIGHT, current_output); - if (!output) - output = get_output_most(D_LEFT, current_output); - } else if (strcasecmp($3, "up") == 0) { - output = get_output_next(D_UP, current_output); - if (!output) - output = get_output_most(D_DOWN, current_output); - } else if (strcasecmp($3, "down") == 0) { - output = get_output_next(D_DOWN, current_output); - if (!output) - output = get_output_most(D_UP, current_output); - } else output = get_output_by_name($3); + output = get_output_from_string(current_output, $3); free($3); @@ -1017,6 +1028,51 @@ move: tree_render(); } + | TOK_MOVE TOK_WORKSPACE TOK_TO TOK_OUTPUT STR + { + printf("should move workspace to output %s\n", $5); + + HANDLE_EMPTY_MATCH; + + owindow *current; + TAILQ_FOREACH(current, &owindows, owindows) { + Output *current_output = get_output_containing(current->con->rect.x, + current->con->rect.y); + Output *output = get_output_from_string(current_output, $5); + if (!output) { + printf("No such output\n"); + break; + } + + Con *content = output_get_content(output->con); + LOG("got output %p with content %p\n", output, content); + + Con *ws = con_get_workspace(current->con); + printf("should move workspace %p / %s\n", ws, ws->name); + if (con_num_children(ws->parent) == 1) { + printf("Not moving workspace \"%s\", it is the only workspace on its output.\n", ws->name); + continue; + } + bool workspace_was_visible = workspace_is_visible(ws); + Con *old_content = ws->parent; + con_detach(ws); + if (workspace_was_visible) { + /* The workspace which we just detached was visible, so focus + * the next one in the focus-stack. */ + Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head)); + printf("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name); + workspace_show(focus_ws); + } + con_attach(ws, content, false); + ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}"); + if (workspace_was_visible) { + /* Focus the moved workspace on the destination output. */ + workspace_show(ws); + } + } + + tree_render(); + } ; append_layout: diff --git a/testcases/t/132-move-workspace.t b/testcases/t/132-move-workspace.t index 1643fc75..a7c5544b 100644 --- a/testcases/t/132-move-workspace.t +++ b/testcases/t/132-move-workspace.t @@ -1,7 +1,7 @@ #!perl # vim:ts=4:sw=4:expandtab # -# Checks if the 'move workspace' command works correctly +# Checks if the 'move [window/container] to workspace' command works correctly # use i3test; @@ -11,30 +11,40 @@ my $i3 = i3(get_socket_path()); # be set to the window under the cursor $x->root->warp_pointer(0, 0); -my $tmp = get_unused_workspace(); -my $tmp2 = get_unused_workspace(); -cmd "workspace $tmp"; +sub move_workspace_test { + my ($movecmd) = @_; -ok(@{get_ws_content($tmp)} == 0, 'no containers yet'); + my $tmp = get_unused_workspace(); + my $tmp2 = get_unused_workspace(); + cmd "workspace $tmp"; -my $first = open_empty_con($i3); -my $second = open_empty_con($i3); -ok(@{get_ws_content($tmp)} == 2, 'two containers on first ws'); + ok(@{get_ws_content($tmp)} == 0, 'no containers yet'); -cmd "workspace $tmp2"; -ok(@{get_ws_content($tmp2)} == 0, 'no containers on second ws yet'); + my $first = open_empty_con($i3); + my $second = open_empty_con($i3); + ok(@{get_ws_content($tmp)} == 2, 'two containers on first ws'); -cmd "workspace $tmp"; + cmd "workspace $tmp2"; + ok(@{get_ws_content($tmp2)} == 0, 'no containers on second ws yet'); -cmd "move workspace $tmp2"; -ok(@{get_ws_content($tmp)} == 1, 'one container on first ws anymore'); -ok(@{get_ws_content($tmp2)} == 1, 'one container on second ws'); -my ($nodes, $focus) = get_ws_content($tmp2); + cmd "workspace $tmp"; -is($focus->[0], $second, 'same container on different ws'); + cmd "$movecmd $tmp2"; + ok(@{get_ws_content($tmp)} == 1, 'one container on first ws anymore'); + ok(@{get_ws_content($tmp2)} == 1, 'one container on second ws'); + my ($nodes, $focus) = get_ws_content($tmp2); -($nodes, $focus) = get_ws_content($tmp); -ok($nodes->[0]->{focused}, 'first container focused on first ws'); + is($focus->[0], $second, 'same container on different ws'); + + ($nodes, $focus) = get_ws_content($tmp); + ok($nodes->[0]->{focused}, 'first container focused on first ws'); +} + +move_workspace_test('move workspace'); # supported for legacy reasons +move_workspace_test('move to workspace'); +# Those are just synonyms and more verbose ways of saying the same thing: +move_workspace_test('move window to workspace'); +move_workspace_test('move container to workspace'); ################################################################### # check if 'move workspace next' and 'move workspace prev' work @@ -43,12 +53,12 @@ ok($nodes->[0]->{focused}, 'first container focused on first ws'); # Open two containers on the first workspace, one container on the second # workspace. Because the workspaces are named, they will be sorted by order of # creation. -$tmp = get_unused_workspace(); -$tmp2 = get_unused_workspace(); +my $tmp = get_unused_workspace(); +my $tmp2 = get_unused_workspace(); cmd "workspace $tmp"; ok(@{get_ws_content($tmp)} == 0, 'no containers yet'); -$first = open_empty_con($i3); -$second = open_empty_con($i3); +my $first = open_empty_con($i3); +my $second = open_empty_con($i3); ok(@{get_ws_content($tmp)} == 2, 'two containers on first ws'); cmd "workspace $tmp2"; diff --git a/testcases/t/171-config-migrate.t b/testcases/t/171-config-migrate.t index f64d3239..6efe6071 100644 --- a/testcases/t/171-config-migrate.t +++ b/testcases/t/171-config-migrate.t @@ -315,7 +315,7 @@ $output = migrate_config('bindsym Mod1+3 3'); ok(line_exists($output, qr|^bindsym Mod1\+3 workspace 3|), 'workspace changed'); $output = migrate_config('bindsym Mod1+3 m3'); -ok(line_exists($output, qr|^bindsym Mod1\+3 move workspace 3|), 'move workspace changed'); +ok(line_exists($output, qr|^bindsym Mod1\+3 move container to workspace 3|), 'move workspace changed'); $input = < to [output] ' command works +# +use List::Util qw(first); +use i3test; + +# TODO: +# introduce 'move workspace 3 to output ' with synonym 'move workspace 3 to ' + +################################################################################ +# Setup workspaces so that they stay open (with an empty container). +################################################################################ + +is(focused_ws, '1', 'starting on workspace 1'); +# ensure workspace 1 stays open +open_window; + +cmd 'focus output right'; +is(focused_ws, '2', 'workspace 2 on second output'); +# ensure workspace 2 stays open +open_window; + +cmd 'focus output right'; +is(focused_ws, '1', 'back on workspace 1'); + +# We don’t use fresh_workspace with named workspaces here since they come last +# when using 'workspace next'. +cmd 'workspace 5'; +# ensure workspace 5 stays open +open_window; + +################################################################################ +# Move a workspace over and verify that it is on the right output. +################################################################################ + +# The current order should be: +# output 1: 1, 5 +# output 2: 2 +cmd 'workspace 5'; +is(focused_ws, '5', 'workspace 5 focused'); + +my ($x0, $x1) = workspaces_per_screen(); +ok('5' ~~ @$x0, 'workspace 5 on xinerama-0'); + +cmd 'move workspace to output xinerama-1'; + +sub workspaces_per_screen { + my $i3 = i3(get_socket_path()); + my $tree = $i3->get_tree->recv; + my @outputs = @{$tree->{nodes}}; + + my $xinerama0 = first { $_->{name} eq 'xinerama-0' } @outputs; + my $xinerama0_content = first { $_->{type} == 2 } @{$xinerama0->{nodes}}; + + my $xinerama1 = first { $_->{name} eq 'xinerama-1' } @outputs; + my $xinerama1_content = first { $_->{type} == 2 } @{$xinerama1->{nodes}}; + + my @xinerama0_workspaces = map { $_->{name} } @{$xinerama0_content->{nodes}}; + my @xinerama1_workspaces = map { $_->{name} } @{$xinerama1_content->{nodes}}; + + return \@xinerama0_workspaces, \@xinerama1_workspaces; +} + +($x0, $x1) = workspaces_per_screen(); +ok('5' ~~ @$x1, 'workspace 5 now on xinerama-1'); + +################################################################################ +# Verify that the last workspace on an output cannot be moved. +################################################################################ + +cmd 'workspace 1'; +cmd 'move workspace to output xinerama-1'; + +($x0, $x1) = workspaces_per_screen(); +ok('1' ~~ @$x0, 'workspace 1 still on xinerama-0'); + +################################################################################ +# Verify that 'move workspace to output ' works +################################################################################ + +cmd 'workspace 5'; +cmd 'move workspace to output left'; + +($x0, $x1) = workspaces_per_screen(); +ok('5' ~~ @$x0, 'workspace 5 back on xinerama-0'); + + +done_testing;