diff --git a/include/container.h b/include/container.h index aadba5dea..798a31a2c 100644 --- a/include/container.h +++ b/include/container.h @@ -56,6 +56,10 @@ struct sway_container { struct sway_container *focused; }; +enum visibility_mask { + VISIBLE = 1 +}; + // Container Creation swayc_t *new_output(wlc_handle handle); @@ -106,4 +110,7 @@ void container_map(swayc_t *, void (*f)(swayc_t *, void *), void *); void set_view_visibility(swayc_t *view, void *data); void reset_gaps(swayc_t *view, void *data); + +void update_visibility(swayc_t *container); + #endif diff --git a/include/layout.h b/include/layout.h index 11bf1a28a..8cc26ba8f 100644 --- a/include/layout.h +++ b/include/layout.h @@ -22,6 +22,7 @@ swayc_t *remove_child(swayc_t *child); void swap_container(swayc_t *a, swayc_t *b); void move_container(swayc_t* container,swayc_t* root,enum movement_direction direction); +void move_container_to(swayc_t* container, swayc_t* destination); // Layout void update_geometry(swayc_t *view); diff --git a/include/util.h b/include/util.h new file mode 100644 index 000000000..8e65e6d61 --- /dev/null +++ b/include/util.h @@ -0,0 +1,9 @@ +#ifndef _SWAY_UTIL_H +#define _SWAY_UTIL_H + +/** + * Wrap i into the range [0, max[ + */ +int wrap(int i, int max); + +#endif diff --git a/include/workspace.h b/include/workspace.h index d447ac2d7..a731068d8 100644 --- a/include/workspace.h +++ b/include/workspace.h @@ -9,9 +9,9 @@ char *workspace_next_name(void); swayc_t *workspace_create(const char*); swayc_t *workspace_by_name(const char*); void workspace_switch(swayc_t*); -void workspace_output_next(); -void workspace_next(); -void workspace_output_prev(); -void workspace_prev(); +swayc_t *workspace_output_next(); +swayc_t *workspace_next(); +swayc_t *workspace_output_prev(); +swayc_t *workspace_prev(); #endif diff --git a/sway.5.txt b/sway.5.txt index 15a465c19..9747fb790 100644 --- a/sway.5.txt +++ b/sway.5.txt @@ -80,6 +80,10 @@ Commands **move** :: Moves the focused container _left_, _right_, _up_, or _down_. +**move** to workspace :: + Moves the focused container to the workspace identified by _name_. + _name_ may be a special workspace name. See **workspace**. + **output** :: Configures the specified output. It will use the given resolution and be arranged at the given position in the layout tree. You may omit either of @@ -111,6 +115,10 @@ Commands **workspace** :: Switches to the specified workspace. +**workspace** :: + Switches to the next workspace on the current output or on the next output + if currently on the last workspace. + **workspace** :: Switches to the next workspace on the current output. diff --git a/sway/commands.c b/sway/commands.c index 21ff5c7f2..74c19b5b2 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -344,7 +344,7 @@ static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char * } static bool cmd_move(struct sway_config *config, int argc, char **argv) { - if (!checkarg(argc, "workspace", EXPECTED_EQUAL_TO, 1)) { + if (!checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1)) { return false; } @@ -358,6 +358,29 @@ static bool cmd_move(struct sway_config *config, int argc, char **argv) { move_container(view,&root_container,MOVE_UP); } else if (strcasecmp(argv[0], "down") == 0) { move_container(view,&root_container,MOVE_DOWN); + } else if (strcasecmp(argv[0], "container") == 0 || strcasecmp(argv[0], "window") == 0) { + // "move container to workspace x" + if (!checkarg(argc, "move container/window", EXPECTED_EQUAL_TO, 4) || + strcasecmp(argv[1], "to") != 0 || + strcasecmp(argv[2], "workspace") != 0) { + return false; + } + + if (view->type != C_CONTAINER && view->type != C_VIEW) { + return false; + } + + const char *ws_name = argv[3]; + if (argc == 5) { + // move "container to workspace number x" + ws_name = argv[4]; + } + + swayc_t *ws = workspace_by_name(ws_name); + if (ws == NULL) { + ws = workspace_create(ws_name); + } + move_container_to(view, ws); } else { return false; } @@ -645,23 +668,23 @@ static bool cmd_workspace(struct sway_config *config, int argc, char **argv) { if (argc == 1) { // Handle workspace next/prev if (strcmp(argv[0], "next") == 0) { - workspace_next(); + workspace_switch(workspace_next()); return true; } if (strcmp(argv[0], "prev") == 0) { - workspace_next(); + workspace_switch(workspace_prev()); return true; } // Handle workspace output_next/prev if (strcmp(argv[0], "next_on_output") == 0) { - workspace_output_next(); + workspace_switch(workspace_output_next()); return true; } if (strcmp(argv[0], "prev_on_output") == 0) { - workspace_output_prev(); + workspace_switch(workspace_output_prev()); return true; } diff --git a/sway/container.c b/sway/container.c index d23cef8fb..6fbfa3601 100644 --- a/sway/container.c +++ b/sway/container.c @@ -520,16 +520,25 @@ void set_view_visibility(swayc_t *view, void *data) { if (!ASSERT_NONNULL(view)) { return; } - uint32_t *p = data; + bool visible = *(bool *)data; if (view->type == C_VIEW) { - wlc_view_set_mask(view->handle, *p); - if (*p == 2) { + wlc_view_set_output(view->handle, swayc_parent_by_type(view, C_OUTPUT)->handle); + wlc_view_set_mask(view->handle, visible ? VISIBLE : 0); + if (visible) { wlc_view_bring_to_front(view->handle); } else { wlc_view_send_to_back(view->handle); } } - view->visible = (*p == 2); + view->visible = visible; + sway_log(L_DEBUG, "Container %p is now %s", view, visible ? "visible" : "invisible"); +} + +void update_visibility(swayc_t *container) { + swayc_t *ws = swayc_active_workspace_for(container); + bool visible = (ws->parent->focused == ws); + sway_log(L_DEBUG, "Setting visibility of container %p to %s", container, visible ? "visible" : "invisible"); + container_map(ws, set_view_visibility, &visible); } void reset_gaps(swayc_t *view, void *data) { diff --git a/sway/focus.c b/sway/focus.c index e369de30b..1086f1a8c 100644 --- a/sway/focus.c +++ b/sway/focus.c @@ -28,12 +28,11 @@ static void update_focus(swayc_t *c) { if (parent->focused) { swayc_t *ws = parent->focused; // hide visibility of old workspace - uint32_t mask = 1; - container_map(ws, set_view_visibility, &mask); + bool visible = false; + container_map(ws, set_view_visibility, &visible); // set visibility of new workspace - mask = 2; - container_map(c, set_view_visibility, &mask); - wlc_output_set_mask(parent->handle, 2); + visible = true; + container_map(c, set_view_visibility, &visible); destroy_workspace(ws); } break; @@ -45,8 +44,8 @@ static void update_focus(swayc_t *c) { // for example, stacked and tabbing change stuff. break; } + c->parent->focused = c; } - c->parent->focused = c; } bool move_focus(enum movement_direction direction) { diff --git a/sway/handlers.c b/sway/handlers.c index 3a4e31aef..af68b765f 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -90,6 +90,9 @@ swayc_t *container_under_pointer(void) { static bool handle_output_created(wlc_handle output) { swayc_t *op = new_output(output); + // Visibility mask to be able to make view invisible + wlc_output_set_mask(output, VISIBLE); + if (!op) { return false; } diff --git a/sway/layout.c b/sway/layout.c index a37e137c8..cd47037bc 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -203,6 +203,21 @@ void move_container(swayc_t *container,swayc_t* root,enum movement_direction dir } +void move_container_to(swayc_t* container, swayc_t* destination) { + if (container->parent == destination) { + return; + } + destroy_container(remove_child(container)); + set_focused_container(get_focused_view(&root_container)); + if (container->is_floating) { + add_floating(destination, container); + } else { + add_child(destination, container); + } + update_visibility(container); + arrange_windows(&root_container, -1, -1); +} + void update_geometry(swayc_t *container) { if (container->type != C_VIEW) { return; diff --git a/sway/util.c b/sway/util.c new file mode 100644 index 000000000..9a59ddf91 --- /dev/null +++ b/sway/util.c @@ -0,0 +1,5 @@ +#include "util.h" + +int wrap(int i, int max) { + return ((i % max) + max) % max; +} diff --git a/sway/workspace.c b/sway/workspace.c index 4c8bb62f0..252526ce5 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -11,6 +11,7 @@ #include "config.h" #include "stringop.h" #include "focus.h" +#include "util.h" char *workspace_next_name(void) { sway_log(L_DEBUG, "Workspace: Generating new name"); @@ -84,95 +85,95 @@ static bool _workspace_by_name(swayc_t *view, void *data) { } swayc_t *workspace_by_name(const char* name) { - return swayc_by_test(&root_container, _workspace_by_name, (void *) name); -} - -void workspace_output_next() { - // Get the index of the workspace in the current output, and change the view to index+1 workspace. - // if we're currently focused on the last workspace in the output, switch to the first - swayc_t *current_output = swayc_active_workspace()->parent; - int i; - for (i = 0; i < current_output->children->length - 1; i++) { - if (strcmp((((swayc_t *)current_output->children->items[i])->name), swayc_active_workspace()->name) == 0) { - workspace_switch(current_output->children->items[i + 1]); - return; - } + if (strcmp(name, "prev") == 0) { + return workspace_prev(); } - workspace_switch(current_output->children->items[0]); -} - -void workspace_next() { - // Get the index of the workspace in the current output, and change the view to index+1 workspace. - // if we're currently focused on the last workspace in the output, change focus to there - // and call workspace_output_next(), as long as another output actually exists - swayc_t *current_output = swayc_active_workspace()->parent; - int i; - for (i = 0; i < current_output->children->length - 1; i++) { - if (strcmp((((swayc_t *)current_output->children->items[i])->name), swayc_active_workspace()->name) == 0) { - workspace_switch(current_output->children->items[i + 1]); - return; - } + else if (strcmp(name, "prev_on_output") == 0) { + return workspace_output_prev(); } - if (root_container.children->length > 1) { - for (i = 0; i < root_container.children->length - 1; i++) { - if (root_container.children->items[i] == current_output) { - workspace_switch(((swayc_t *)root_container.children->items[i + 1])->focused); - workspace_output_next(); - return; - } - } - // If we're at the last output, then go to the first - workspace_switch(((swayc_t *)root_container.children->items[0])->focused); - workspace_output_next(); - return; - } else { - workspace_switch(current_output->children->items[0]); + else if (strcmp(name, "next") == 0) { + return workspace_next(); + } + else if (strcmp(name, "next_on_output") == 0) { + return workspace_output_next(); + } + else if (strcmp(name, "current") == 0) { + return swayc_active_workspace(); + } + else { + return swayc_by_test(&root_container, _workspace_by_name, (void *) name); } } -void workspace_output_prev() { - // Get the index of the workspace in the current output, and change the view to index+1 workspace - // if we're currently focused on the first workspace in the output, do nothing and return false - swayc_t *current_output = swayc_active_workspace()->parent; +/** + * Get the previous or next workspace on the specified output. + * Wraps around at the end and beginning. + * If next is false, the previous workspace is returned, otherwise the next one is returned. + */ +swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) { + if (!sway_assert(output->type == C_OUTPUT, "Argument must be an output, is %d", output->type)) { + return NULL; + } + int i; - for (i = 1; i < current_output->children->length; i++) { - if (strcmp((((swayc_t *)current_output->children->items[i])->name), swayc_active_workspace()->name) == 0) { - workspace_switch(current_output->children->items[i - 1]); - return; + for (i = 0; i < output->children->length; i++) { + if (output->children->items[i] == output->focused) { + return output->children->items[wrap(i + (next ? 1 : -1), output->children->length)]; } } - workspace_switch(current_output->children->items[current_output->children->length - 1]); + + // Doesn't happen, at worst the for loop returns the previously active workspace + return NULL; } -void workspace_prev() { - // Get the index of the workspace in the current output, and change the view to index-1 workspace. - // if we're currently focused on the last workspace in the output, change focus to there - // and call workspace_output_next(), as long as another output actually exists +/** + * Get the previous or next workspace. If the first/last workspace on an output is active, + * proceed to the previous/next output's previous/next workspace. + * If next is false, the previous workspace is returned, otherwise the next one is returned. + */ +swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) { + if (!sway_assert(workspace->type == C_WORKSPACE, "Argument must be a workspace, is %d", workspace->type)) { + return NULL; + } - swayc_t *current_output = swayc_active_workspace()->parent; + swayc_t *current_output = workspace->parent; + int offset = next ? 1 : -1; + int start = next ? 0 : 1; + int end = next ? (current_output->children->length) - 1 : current_output->children->length; int i; - for (i = 1; i < current_output->children->length; i++) { - if (strcmp((((swayc_t *)current_output->children->items[i])->name), swayc_active_workspace()->name) == 0) { - workspace_switch(current_output->children->items[i - 1]); - return; + for (i = start; i < end; i++) { + if (current_output->children->items[i] == workspace) { + return current_output->children->items[i + offset]; } } - if (root_container.children->length > 1) { - for (i = 1; i < root_container.children->length; i++) { - if (root_container.children->items[i] == current_output) { - workspace_switch(((swayc_t *)root_container.children->items[i - 1])->focused); - workspace_output_next(); - return; - } - } - // If we're at the first output, then go to the last - workspace_switch(((swayc_t *)root_container.children->items[root_container.children->length-1])->focused); - workspace_output_next(); - return; - } else { - workspace_switch(current_output->children->items[current_output->children->length - 1]); - } + // Given workspace is the first/last on the output, jump to the previous/next output + int num_outputs = root_container.children->length; + for (i = 0; i < num_outputs; i++) { + if (root_container.children->items[i] == current_output) { + swayc_t *next_output = root_container.children->items[wrap(i + offset, num_outputs)]; + return workspace_output_prev_next_impl(next_output, next); + } + } + + // Doesn't happen, at worst the for loop returns the previously active workspace on the active output + return NULL; +} + +swayc_t *workspace_output_next() { + return workspace_output_prev_next_impl(swayc_active_output(), true); +} + +swayc_t *workspace_next() { + return workspace_prev_next_impl(swayc_active_workspace(), true); +} + +swayc_t *workspace_output_prev() { + return workspace_output_prev_next_impl(swayc_active_output(), false); +} + +swayc_t *workspace_prev() { + return workspace_prev_next_impl(swayc_active_workspace(), false); } void workspace_switch(swayc_t *workspace) {