diff options
54 files changed, 1752 insertions, 1678 deletions
diff --git a/common/util.c b/common/util.c index 467aa4b5..561b3804 100644 --- a/common/util.c +++ b/common/util.c @@ -175,3 +175,24 @@ failed: free(current); return NULL; } + +bool sway_dir_to_wlr(enum movement_direction dir, enum wlr_direction *out) { + switch (dir) { + case MOVE_UP: + *out = WLR_DIRECTION_UP; + break; + case MOVE_DOWN: + *out = WLR_DIRECTION_DOWN; + break; + case MOVE_LEFT: + *out = WLR_DIRECTION_LEFT; + break; + case MOVE_RIGHT: + *out = WLR_DIRECTION_RIGHT; + break; + default: + return false; + } + + return true; +} diff --git a/completions/fish/sway.fish b/completions/fish/sway.fish new file mode 100644 index 00000000..31165ef4 --- /dev/null +++ b/completions/fish/sway.fish @@ -0,0 +1,10 @@ +# sway(1) completion + +complete -c sway -s h -l help --description "Show help message and quit." +complete -c sway -s c -l config --description "Specifies a config file." +complete -c sway -s C -l validate --description "Check the validity of the config file, then exit." +complete -c sway -s d -l debug --description "Enables full logging, including debug information." +complete -c sway -s v -l version --description "Show the version number and quit." +complete -c sway -s V -l verbose --description "Enables more verbose logging." +complete -c sway -l get-socketpath --description "Gets the IPC socket path and prints it, then exits." + diff --git a/completions/fish/swaylock.fish b/completions/fish/swaylock.fish new file mode 100644 index 00000000..965a22d2 --- /dev/null +++ b/completions/fish/swaylock.fish @@ -0,0 +1,11 @@ +# swaylock(1) completion + +complete -c swaylock -s h -l help --description "Show help message and quit." +complete -c swaylock -s c -l color --description "Turn the screen into the given color. If -i is used, this sets the background of the image into the given color. Defaults to white (ffffff), or transparent (00000000) if an image is in use." +complete -c swaylock -s f -l daemonize --description "Fork into the background after spawning. Note: this is the default bahavior of i3lock." +complete -c swaylock -s i -l image --description "Display the given image, optionally on the given output. Use -c to set a background color." +complete -c swaylock -l scaling --description "Scaling mode for images: stretch, fill, fit, center, or tile." +complete -c swaylock -s t -l tiling --description "Same as --scaling=tile." +complete -c swaylock -s u -l no-unlock-indicator --description "Disable the unlock indicator." +complete -c swaylock -s v -l version --description "Show the version number and quit." +complete -c swaylock -l socket --description "Use the specified socket path. Othherwise, swaymsg will as sway where the socket is (which is the value of $SWAYSOCK, then of $I350CK)." diff --git a/completions/fish/swaymsg.fish b/completions/fish/swaymsg.fish new file mode 100644 index 00000000..e798db77 --- /dev/null +++ b/completions/fish/swaymsg.fish @@ -0,0 +1,8 @@ +# swaymsg(1) completion + +complete -c swaymsg -s h -l help --description "Show help message and quit." +complete -c swaymsg -s q -l quiet --description "Sends the IPC message but does not print the response from sway." +complete -c swaymsg -s r -l raw --description "Use raw output even if using tty." +complete -c swaymsg -s s -l socket --description "Use the specified socket path. Otherwise, swaymsg will ask where the socket is (which is the value of $SWAYSOCK, then of $I3SOCK)." +complete -c swaymsg -s t -l type --description "Specify the type of IPC message." +complete -c swaymsg -s v -l version --description "Print the version (of swaymsg) and quit." diff --git a/include/sway/config.h b/include/sway/config.h index c2eaea1b..18d10faa 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -8,8 +8,8 @@ #include <xkbcommon/xkbcommon.h> #include "list.h" #include "swaynag.h" -#include "tree/layout.h" #include "tree/container.h" +#include "sway/tree/root.h" #include "wlr-layer-shell-unstable-v1-protocol.h" // TODO: Refactor this shit diff --git a/include/sway/output.h b/include/sway/output.h index d0d034b3..651fdfe7 100644 --- a/include/sway/output.h +++ b/include/sway/output.h @@ -39,6 +39,14 @@ struct sway_output { } events; }; +struct sway_container *output_create(struct sway_output *sway_output); + +void output_destroy(struct sway_container *output); + +void output_begin_destroy(struct sway_container *output); + +struct sway_container *output_from_wlr_output(struct wlr_output *output); + typedef void (*sway_surface_iterator_func_t)(struct sway_output *output, struct wlr_surface *surface, struct wlr_box *box, float rotation, void *user_data); diff --git a/include/sway/tree/arrange.h b/include/sway/tree/arrange.h index d6abcc81..f47e8db5 100644 --- a/include/sway/tree/arrange.h +++ b/include/sway/tree/arrange.h @@ -1,18 +1,16 @@ #ifndef _SWAY_ARRANGE_H #define _SWAY_ARRANGE_H -#include "sway/desktop/transaction.h" struct sway_container; -// Remove gaps around container -void remove_gaps(struct sway_container *c); +void arrange_container(struct sway_container *container); -// Add gaps around container -void add_gaps(struct sway_container *c); +void arrange_workspace(struct sway_container *workspace); + +void arrange_output(struct sway_container *output); + +void arrange_root(void); -/** - * Arrange layout for all the children of the given container. - */ void arrange_windows(struct sway_container *container); #endif diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index cd886cd0..e4071cfe 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -53,6 +53,9 @@ struct sway_output; struct sway_workspace; struct sway_view; +enum movement_direction; +enum wlr_direction; + struct sway_container_state { // Container/swayc properties enum sway_container_layout layout; @@ -176,27 +179,6 @@ struct sway_container *container_create(enum sway_container_type type); const char *container_type_to_str(enum sway_container_type type); -struct sway_container *output_create(struct sway_output *sway_output); - -/** - * Create a new container container. A container container can be a a child of - * a workspace container or another container container. - */ -struct sway_container *container_container_create(); - -/** - * Create a new output. Outputs are children of the root container and have no - * order in the tree structure. - */ -struct sway_container *output_create(struct sway_output *sway_output); - -/** - * Create a new workspace container. Workspaces are children of an output - * container and are ordered alphabetically by name. - */ -struct sway_container *workspace_create(struct sway_container *output, - const char *name); - /* * Create a new view container. A view can be a child of a workspace container * or a container container and are rendered in the order and structure of @@ -205,9 +187,9 @@ struct sway_container *workspace_create(struct sway_container *output, struct sway_container *container_view_create( struct sway_container *sibling, struct sway_view *sway_view); -void container_free(struct sway_container *cont); +void container_destroy(struct sway_container *con); -struct sway_container *container_destroy(struct sway_container *container); +void container_begin_destroy(struct sway_container *con); struct sway_container *container_close(struct sway_container *container); @@ -255,10 +237,7 @@ void container_update_textures_recursive(struct sway_container *con); void container_damage_whole(struct sway_container *container); -bool container_reap_empty(struct sway_container *con); - -struct sway_container *container_reap_empty_recursive( - struct sway_container *con); +struct sway_container *container_reap_empty(struct sway_container *con); struct sway_container *container_flatten(struct sway_container *container); @@ -359,4 +338,35 @@ struct sway_output *container_get_effective_output(struct sway_container *con); void container_discover_outputs(struct sway_container *con); +void container_remove_gaps(struct sway_container *container); + +void container_add_gaps(struct sway_container *container); + +int container_sibling_index(const struct sway_container *child); + +void container_handle_fullscreen_reparent(struct sway_container *con, + struct sway_container *old_parent); + +void container_add_child(struct sway_container *parent, + struct sway_container *child); + +void container_insert_child(struct sway_container *parent, + struct sway_container *child, int i); + +struct sway_container *container_add_sibling(struct sway_container *parent, + struct sway_container *child); + +struct sway_container *container_remove_child(struct sway_container *child); + +struct sway_container *container_replace_child(struct sway_container *child, + struct sway_container *new_child); + +bool sway_dir_to_wlr(enum movement_direction dir, enum wlr_direction *out); + +enum sway_container_layout container_get_default_layout( + struct sway_container *con); + +struct sway_container *container_split(struct sway_container *child, + enum sway_container_layout layout); + #endif diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h deleted file mode 100644 index 519189d9..00000000 --- a/include/sway/tree/layout.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef _SWAY_LAYOUT_H -#define _SWAY_LAYOUT_H -#include <wlr/types/wlr_output_layout.h> -#include <wlr/render/wlr_texture.h> -#include "sway/tree/container.h" -#include "sway/tree/root.h" -#include "config.h" - -enum movement_direction { - MOVE_LEFT, - MOVE_RIGHT, - MOVE_UP, - MOVE_DOWN, - MOVE_PARENT, - MOVE_CHILD, -}; - -enum wlr_edges; - -struct sway_container; - -void container_add_child(struct sway_container *parent, - struct sway_container *child); - -struct sway_container *container_add_sibling(struct sway_container *parent, - struct sway_container *child); - -struct sway_container *container_remove_child(struct sway_container *child); - -struct sway_container *container_replace_child(struct sway_container *child, - struct sway_container *new_child); - -void container_move_to(struct sway_container* container, - struct sway_container* destination); - -void container_move(struct sway_container *container, - enum movement_direction dir, int move_amt); - -enum sway_container_layout container_get_default_layout( - struct sway_container *con); - -struct sway_container *container_get_in_direction(struct sway_container - *container, struct sway_seat *seat, enum movement_direction dir); - -struct sway_container *container_split(struct sway_container *child, - enum sway_container_layout layout); - -void container_recursive_resize(struct sway_container *container, - double amount, enum wlr_edges edge); - -void container_swap(struct sway_container *con1, struct sway_container *con2); - -#endif diff --git a/include/sway/tree/root.h b/include/sway/tree/root.h index d1f04a96..ec6516c9 100644 --- a/include/sway/tree/root.h +++ b/include/sway/tree/root.h @@ -21,9 +21,11 @@ struct sway_root { struct wlr_texture *debug_tree; - struct wl_list outputs; // sway_output::link + // Includes disabled outputs + struct wl_list all_outputs; // sway_output::link list_t *scratchpad; // struct sway_container + list_t *saved_workspaces; // For when there's no connected outputs struct { struct wl_signal new_container; diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h index 5fdecc2b..f73ce571 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -284,10 +284,10 @@ void view_for_each_popup(struct sway_view *view, void view_init(struct sway_view *view, enum sway_view_type type, const struct sway_view_impl *impl); -void view_free(struct sway_view *view); - void view_destroy(struct sway_view *view); +void view_begin_destroy(struct sway_view *view); + void view_map(struct sway_view *view, struct wlr_surface *wlr_surface); void view_unmap(struct sway_view *view); diff --git a/include/sway/tree/workspace.h b/include/sway/tree/workspace.h index 35c91017..04325919 100644 --- a/include/sway/tree/workspace.h +++ b/include/sway/tree/workspace.h @@ -18,6 +18,15 @@ extern char *prev_workspace_name; struct sway_container *workspace_get_initial_output(const char *name); +struct sway_container *workspace_create(struct sway_container *output, + const char *name); + +void workspace_destroy(struct sway_container *workspace); + +void workspace_begin_destroy(struct sway_container *workspace); + +void workspace_consider_destroy(struct sway_container *ws); + char *workspace_next_name(const char *output_name); bool workspace_switch(struct sway_container *workspace, @@ -66,4 +75,8 @@ struct sway_container *workspace_wrap_children(struct sway_container *ws); void workspace_add_floating(struct sway_container *workspace, struct sway_container *con); +void workspace_remove_gaps(struct sway_container *ws); + +void workspace_add_gaps(struct sway_container *ws); + #endif diff --git a/include/util.h b/include/util.h index 9277fa6e..46ed1533 100644 --- a/include/util.h +++ b/include/util.h @@ -4,9 +4,19 @@ #include <stdint.h> #include <stdbool.h> #include <unistd.h> -#include <sys/types.h> +#include <sys/types.h> +#include <wlr/types/wlr_output_layout.h> #include <xkbcommon/xkbcommon.h> +enum movement_direction { + MOVE_LEFT, + MOVE_RIGHT, + MOVE_UP, + MOVE_DOWN, + MOVE_PARENT, + MOVE_CHILD, +}; + /** * Wrap i into the range [0, max[ */ @@ -71,4 +81,6 @@ char* resolve_path(const char* path); char *b64_encode(const char* binaryData, size_t len, size_t *flen); unsigned char *b64_decode(const char *ascii, size_t len, size_t *flen); +bool sway_dir_to_wlr(enum movement_direction dir, enum wlr_direction *out); + #endif diff --git a/meson.build b/meson.build index 2a020323..d5b33e6b 100644 --- a/meson.build +++ b/meson.build @@ -12,6 +12,7 @@ project( add_project_arguments('-Wno-unused-parameter', language: 'c') add_project_arguments('-Wno-unused-function', language: 'c') add_project_arguments('-Wno-unused-result', language: 'c') +add_project_arguments('-DWL_HIDE_DEPRECATED', language: 'c') add_project_arguments('-DWLR_USE_UNSTABLE', language: 'c') cc = meson.get_compiler('c') @@ -205,3 +206,14 @@ if (get_option('bash_completions')) install_data(bash_files, install_dir: bash_install_dir) endif + +if (get_option('fish_completions')) + fish_files = files( + 'completions/fish/sway.fish', + 'completions/fish/swaylock.fish', + 'completions/fish/swaymsg.fish', + ) + fish_install_dir = datadir + '/fish/completions' + + install_data(fish_files, install_dir: fish_install_dir) +endif diff --git a/meson_options.txt b/meson_options.txt index 7a23c206..5e54607f 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -2,4 +2,5 @@ option('sway_version', type : 'string', description: 'The version string reporte option('default_wallpaper', type: 'boolean', value: true, description: 'Install the default wallpaper.') option('zsh_completions', type: 'boolean', value: true, description: 'Install zsh shell completions.') option('bash_completions', type: 'boolean', value: true, description: 'Install bash shell completions.') +option('fish_completions', type: 'boolean', value: true, description: 'Install fish shell completions.') option('enable-xwayland', type: 'boolean', value: true, description: 'Enable support for X11 applications') diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c index 5ce7919b..bc07c2aa 100644 --- a/sway/commands/exec_always.c +++ b/sway/commands/exec_always.c @@ -8,6 +8,7 @@ #include "sway/commands.h" #include "sway/config.h" #include "sway/tree/container.h" +#include "sway/tree/root.h" #include "sway/tree/workspace.h" #include "log.h" #include "stringop.h" diff --git a/sway/commands/floating.c b/sway/commands/floating.c index beafd9fb..436376e3 100644 --- a/sway/commands/floating.c +++ b/sway/commands/floating.c @@ -6,7 +6,6 @@ #include "sway/output.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" -#include "sway/tree/layout.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" #include "list.h" diff --git a/sway/commands/focus.c b/sway/commands/focus.c index 6659a683..f342e524 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -6,9 +6,11 @@ #include "sway/input/seat.h" #include "sway/output.h" #include "sway/tree/arrange.h" +#include "sway/tree/root.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" #include "stringop.h" +#include "util.h" static bool parse_movement_direction(const char *name, enum movement_direction *out) { @@ -31,6 +33,199 @@ static bool parse_movement_direction(const char *name, return true; } +/** + * Get swayc in the direction of newly entered output. + */ +static struct sway_container *get_swayc_in_output_direction( + struct sway_container *output, enum movement_direction dir, + struct sway_seat *seat) { + if (!output) { + return NULL; + } + + struct sway_container *ws = seat_get_focus_inactive(seat, output); + if (ws->type != C_WORKSPACE) { + ws = container_parent(ws, C_WORKSPACE); + } + + if (ws == NULL) { + wlr_log(WLR_ERROR, "got an output without a workspace"); + return NULL; + } + + if (ws->children->length > 0) { + switch (dir) { + case MOVE_LEFT: + if (ws->layout == L_HORIZ || ws->layout == L_TABBED) { + // get most right child of new output + return ws->children->items[ws->children->length-1]; + } else { + return seat_get_focus_inactive(seat, ws); + } + case MOVE_RIGHT: + if (ws->layout == L_HORIZ || ws->layout == L_TABBED) { + // get most left child of new output + return ws->children->items[0]; + } else { + return seat_get_focus_inactive(seat, ws); + } + case MOVE_UP: + case MOVE_DOWN: { + struct sway_container *focused = + seat_get_focus_inactive(seat, ws); + if (focused && focused->parent) { + struct sway_container *parent = focused->parent; + if (parent->layout == L_VERT) { + if (dir == MOVE_UP) { + // get child furthest down on new output + int idx = parent->children->length - 1; + return parent->children->items[idx]; + } else if (dir == MOVE_DOWN) { + // get child furthest up on new output + return parent->children->items[0]; + } + } + return focused; + } + break; + } + default: + break; + } + } + + return ws; +} + +static struct sway_container *container_get_in_direction( + struct sway_container *container, struct sway_seat *seat, + enum movement_direction dir) { + struct sway_container *parent = container->parent; + + if (dir == MOVE_CHILD) { + return seat_get_focus_inactive(seat, container); + } + if (container->is_fullscreen) { + if (dir == MOVE_PARENT) { + return NULL; + } + container = container_parent(container, C_OUTPUT); + parent = container->parent; + } else { + if (dir == MOVE_PARENT) { + if (parent->type == C_OUTPUT || container_is_floating(container)) { + return NULL; + } else { + return parent; + } + } + } + + struct sway_container *wrap_candidate = NULL; + while (true) { + bool can_move = false; + int desired; + int idx = list_find(container->parent->children, container); + if (idx == -1) { + return NULL; + } + if (parent->type == C_ROOT) { + enum wlr_direction wlr_dir = 0; + if (!sway_assert(sway_dir_to_wlr(dir, &wlr_dir), + "got invalid direction: %d", dir)) { + return NULL; + } + int lx = container->x + container->width / 2; + int ly = container->y + container->height / 2; + struct wlr_output_layout *layout = + root_container.sway_root->output_layout; + struct wlr_output *wlr_adjacent = + wlr_output_layout_adjacent_output(layout, wlr_dir, + container->sway_output->wlr_output, lx, ly); + struct sway_container *adjacent = + output_from_wlr_output(wlr_adjacent); + + if (!adjacent || adjacent == container) { + if (!wrap_candidate) { + return NULL; + } + return seat_get_focus_inactive_view(seat, wrap_candidate); + } + struct sway_container *next = + get_swayc_in_output_direction(adjacent, dir, seat); + if (next == NULL) { + return NULL; + } + struct sway_container *next_workspace = next; + if (next_workspace->type != C_WORKSPACE) { + next_workspace = container_parent(next_workspace, C_WORKSPACE); + } + sway_assert(next_workspace, "Next container has no workspace"); + if (next_workspace->sway_workspace->fullscreen) { + return seat_get_focus_inactive(seat, + next_workspace->sway_workspace->fullscreen); + } + if (next->children && next->children->length) { + // TODO consider floating children as well + return seat_get_focus_inactive_view(seat, next); + } else { + return next; + } + } else { + if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { + if (parent->layout == L_HORIZ || parent->layout == L_TABBED) { + can_move = true; + desired = idx + (dir == MOVE_LEFT ? -1 : 1); + } + } else { + if (parent->layout == L_VERT || parent->layout == L_STACKED) { + can_move = true; + desired = idx + (dir == MOVE_UP ? -1 : 1); + } + } + } + + if (can_move) { + // TODO handle floating + if (desired < 0 || desired >= parent->children->length) { + can_move = false; + int len = parent->children->length; + if (config->focus_wrapping != WRAP_NO && !wrap_candidate + && len > 1) { + if (desired < 0) { + wrap_candidate = parent->children->items[len-1]; + } else { + wrap_candidate = parent->children->items[0]; + } + if (config->focus_wrapping == WRAP_FORCE) { + return seat_get_focus_inactive_view(seat, + wrap_candidate); + } + } + } else { + struct sway_container *desired_con = + parent->children->items[desired]; + wlr_log(WLR_DEBUG, + "cont %d-%p dir %i sibling %d: %p", idx, + container, dir, desired, desired_con); + return seat_get_focus_inactive_view(seat, desired_con); + } + } + + if (!can_move) { + container = parent; + parent = parent->parent; + if (!parent) { + // wrapping is the last chance + if (!wrap_candidate) { + return NULL; + } + return seat_get_focus_inactive_view(seat, wrap_candidate); + } + } + } +} + static struct cmd_results *focus_mode(struct sway_container *con, struct sway_seat *seat, bool floating) { struct sway_container *ws = con->type == C_WORKSPACE ? diff --git a/sway/commands/fullscreen.c b/sway/commands/fullscreen.c index a0661200..ac65dffb 100644 --- a/sway/commands/fullscreen.c +++ b/sway/commands/fullscreen.c @@ -5,7 +5,6 @@ #include "sway/tree/container.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" -#include "sway/tree/layout.h" #include "util.h" struct cmd_results *cmd_fullscreen(int argc, char **argv) { diff --git a/sway/commands/hide_edge_borders.c b/sway/commands/hide_edge_borders.c index d59c9fdb..e494f6aa 100644 --- a/sway/commands/hide_edge_borders.c +++ b/sway/commands/hide_edge_borders.c @@ -1,6 +1,7 @@ #include "sway/commands.h" #include "sway/config.h" #include "sway/tree/container.h" +#include "sway/tree/root.h" #include "sway/tree/view.h" static void _configure_view(struct sway_container *con, void *data) { diff --git a/sway/commands/layout.c b/sway/commands/layout.c index f4e4dda9..a06832de 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -103,7 +103,7 @@ struct cmd_results *cmd_layout(int argc, char **argv) { parent->prev_split_layout = prev; } container_notify_subtree_changed(parent); - arrange_windows(parent); + arrange_windows(parent->parent); } return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/commands/move.c b/sway/commands/move.c index c6dc0775..7b7cb8f3 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -19,6 +19,7 @@ #include "stringop.h" #include "list.h" #include "log.h" +#include "util.h" static const char *expected_syntax = "Expected 'move <left|right|up|down> <[px] px>' or " @@ -26,7 +27,20 @@ static const char *expected_syntax = "'move <container|window|workspace> [to] output <name|direction>' or " "'move <container|window> [to] mark <mark>'"; -static struct sway_container *output_in_direction(const char *direction, +enum wlr_direction opposite_direction(enum wlr_direction d) { + switch (d) { + case WLR_DIRECTION_UP: + return WLR_DIRECTION_DOWN; + case WLR_DIRECTION_DOWN: + return WLR_DIRECTION_UP; + case WLR_DIRECTION_RIGHT: + return WLR_DIRECTION_LEFT; + default: + return WLR_DIRECTION_RIGHT; + } +} + +static struct sway_container *output_in_direction(const char *direction_string, struct wlr_output *reference, int ref_lx, int ref_ly) { struct { char *name; @@ -37,19 +51,440 @@ static struct sway_container *output_in_direction(const char *direction, { "left", WLR_DIRECTION_LEFT }, { "right", WLR_DIRECTION_RIGHT }, }; + + enum wlr_direction direction = 0; + for (size_t i = 0; i < sizeof(names) / sizeof(names[0]); ++i) { - if (strcasecmp(names[i].name, direction) == 0) { - struct wlr_output *adjacent = wlr_output_layout_adjacent_output( + if (strcasecmp(names[i].name, direction_string) == 0) { + direction = names[i].direction; + break; + } + } + + if (direction) { + struct wlr_output *target = wlr_output_layout_adjacent_output( + root_container.sway_root->output_layout, + direction, reference, ref_lx, ref_ly); + + if (!target) { + target = wlr_output_layout_farthest_output( root_container.sway_root->output_layout, - names[i].direction, reference, ref_lx, ref_ly); - if (adjacent) { - struct sway_output *sway_output = adjacent->data; - return sway_output->swayc; + opposite_direction(direction), reference, ref_lx, ref_ly); + } + + if (target) { + struct sway_output *sway_output = target->data; + return sway_output->swayc; + } + } + + return output_by_name(direction_string); +} + +static void container_move_to(struct sway_container *container, + struct sway_container *destination) { + if (!sway_assert(container->type == C_CONTAINER || + container->type == C_VIEW, "Expected a container or view")) { + return; + } + if (container == destination + || container_has_ancestor(container, destination)) { + return; + } + struct sway_container *old_parent = NULL; + struct sway_container *new_parent = NULL; + if (container_is_floating(container)) { + // Resolve destination into a workspace + struct sway_container *new_ws = NULL; + if (destination->type == C_OUTPUT) { + new_ws = output_get_active_workspace(destination->sway_output); + } else if (destination->type == C_WORKSPACE) { + new_ws = destination; + } else { + new_ws = container_parent(destination, C_WORKSPACE); + } + if (!new_ws) { + // This can happen if the user has run "move container to mark foo", + // where mark foo is on a hidden scratchpad container. + return; + } + struct sway_container *old_output = + container_parent(container, C_OUTPUT); + old_parent = container_remove_child(container); + workspace_add_floating(new_ws, container); + container_handle_fullscreen_reparent(container, old_parent); + // If changing output, center it within the workspace + if (old_output != new_ws->parent && !container->is_fullscreen) { + container_floating_move_to_center(container); + } + } else { + old_parent = container_remove_child(container); + container->width = container->height = 0; + container->saved_width = container->saved_height = 0; + + if (destination->type == C_VIEW) { + new_parent = container_add_sibling(destination, container); + } else { + new_parent = destination; + container_add_child(destination, container); + } + } + + if (container->type == C_VIEW) { + ipc_event_window(container, "move"); + } + container_notify_subtree_changed(old_parent); + container_notify_subtree_changed(new_parent); + + // If view was moved to a fullscreen workspace, refocus the fullscreen view + struct sway_container *new_workspace = container; + if (new_workspace->type != C_WORKSPACE) { + new_workspace = container_parent(new_workspace, C_WORKSPACE); + } + if (new_workspace->sway_workspace->fullscreen) { + struct sway_seat *seat; + struct sway_container *focus, *focus_ws; + wl_list_for_each(seat, &input_manager->seats, link) { + focus = seat_get_focus(seat); + focus_ws = focus; + if (focus_ws->type != C_WORKSPACE) { + focus_ws = container_parent(focus_ws, C_WORKSPACE); } + if (focus_ws == new_workspace) { + struct sway_container *new_focus = seat_get_focus_inactive(seat, + new_workspace->sway_workspace->fullscreen); + seat_set_focus(seat, new_focus); + } + } + } + // Update workspace urgent state + struct sway_container *old_workspace = old_parent; + if (old_workspace->type != C_WORKSPACE) { + old_workspace = container_parent(old_workspace, C_WORKSPACE); + } + if (new_workspace != old_workspace) { + workspace_detect_urgent(new_workspace); + if (old_workspace) { + workspace_detect_urgent(old_workspace); + } + } +} + +static bool is_parallel(enum sway_container_layout layout, + enum movement_direction dir) { + switch (layout) { + case L_TABBED: + case L_HORIZ: + return dir == MOVE_LEFT || dir == MOVE_RIGHT; + case L_STACKED: + case L_VERT: + return dir == MOVE_UP || dir == MOVE_DOWN; + default: + return false; + } +} + +static enum movement_direction invert_movement(enum movement_direction dir) { + switch (dir) { + case MOVE_LEFT: + return MOVE_RIGHT; + case MOVE_RIGHT: + return MOVE_LEFT; + case MOVE_UP: + return MOVE_DOWN; + case MOVE_DOWN: + return MOVE_UP; + default: + sway_assert(0, "This function expects left|right|up|down"); + return MOVE_LEFT; + } +} + +static int move_offs(enum movement_direction move_dir) { + return move_dir == MOVE_LEFT || move_dir == MOVE_UP ? -1 : 1; +} + +/* Gets the index of the most extreme member based on the movement offset */ +static int container_limit(struct sway_container *container, + enum movement_direction move_dir) { + return move_offs(move_dir) < 0 ? 0 : container->children->length; +} + +/* Takes one child, sets it aside, wraps the rest of the children in a new + * container, switches the layout of the workspace, and drops the child back in. + * In other words, rejigger it. */ +static void workspace_rejigger(struct sway_container *ws, + struct sway_container *child, enum movement_direction move_dir) { + struct sway_container *original_parent = child->parent; + struct sway_container *new_parent = + container_split(ws, ws->layout); + + container_remove_child(child); + for (int i = 0; i < ws->children->length; ++i) { + struct sway_container *_child = ws->children->items[i]; + container_move_to(new_parent, _child); + } + + int index = move_offs(move_dir); + container_insert_child(ws, child, index < 0 ? 0 : 1); + ws->layout = + move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT; + + container_flatten(ws); + container_reap_empty(original_parent); + container_create_notify(new_parent); +} + +static void move_out_of_tabs_stacks(struct sway_container *container, + struct sway_container *current, enum movement_direction move_dir, + int offs) { + if (container->parent == current->parent + && current->parent->children->length == 1) { + wlr_log(WLR_DEBUG, "Changing layout of %zd", current->parent->id); + current->parent->layout = move_dir == + MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT; + return; + } + + wlr_log(WLR_DEBUG, "Moving out of tab/stack into a split"); + bool is_workspace = current->parent->type == C_WORKSPACE; + struct sway_container *new_parent = container_split(current->parent, + move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT); + if (is_workspace) { + container_insert_child(new_parent->parent, container, offs < 0 ? 0 : 1); + } else { + container_insert_child(new_parent, container, offs < 0 ? 0 : 1); + container_reap_empty(new_parent->parent); + container_flatten(new_parent->parent); + } + container_create_notify(new_parent); + container_notify_subtree_changed(new_parent); +} + +static void container_move(struct sway_container *container, + enum movement_direction move_dir, int move_amt) { + if (!sway_assert( + container->type != C_CONTAINER || container->type != C_VIEW, + "Can only move containers and views")) { + return; + } + int offs = move_offs(move_dir); + + struct sway_container *sibling = NULL; + struct sway_container *current = container; + struct sway_container *parent = current->parent; + struct sway_container *top = &root_container; + + // If moving a fullscreen view, only consider outputs + if (container->is_fullscreen) { + current = container_parent(container, C_OUTPUT); + } else if (container_is_fullscreen_or_child(container) || + container_is_floating_or_child(container)) { + // If we've fullscreened a split container, only allow the child to move + // around within the fullscreen parent. + // Same with floating a split container. + struct sway_container *ws = container_parent(container, C_WORKSPACE); + top = ws->sway_workspace->fullscreen; + } + + struct sway_container *new_parent = container_flatten(parent); + if (new_parent != parent) { + // Special case: we were the last one in this container, so leave + return; + } + + while (!sibling) { + if (current == top) { + return; + } + + parent = current->parent; + wlr_log(WLR_DEBUG, "Visiting %p %s '%s'", current, + container_type_to_str(current->type), current->name); + + int index = container_sibling_index(current); + + switch (current->type) { + case C_OUTPUT: { + enum wlr_direction wlr_dir = 0; + if (!sway_assert(sway_dir_to_wlr(move_dir, &wlr_dir), + "got invalid direction: %d", move_dir)) { + return; + } + double ref_lx = current->x + current->width / 2; + double ref_ly = current->y + current->height / 2; + struct wlr_output *next = wlr_output_layout_adjacent_output( + root_container.sway_root->output_layout, wlr_dir, + current->sway_output->wlr_output, ref_lx, ref_ly); + if (!next) { + wlr_log(WLR_DEBUG, "Hit edge of output, nowhere else to go"); + return; + } + struct sway_output *next_output = next->data; + current = next_output->swayc; + wlr_log(WLR_DEBUG, "Selected next output (%s)", current->name); + // Select workspace and get outta here + current = seat_get_focus_inactive( + config->handler_context.seat, current); + if (current->type != C_WORKSPACE) { + current = container_parent(current, C_WORKSPACE); + } + sibling = current; break; } + case C_WORKSPACE: + if (!is_parallel(current->layout, move_dir)) { + if (current->children->length >= 2) { + wlr_log(WLR_DEBUG, "Rejiggering the workspace (%d kiddos)", + current->children->length); + workspace_rejigger(current, container, move_dir); + return; + } else { + wlr_log(WLR_DEBUG, "Selecting output"); + current = current->parent; + } + } else if (current->layout == L_TABBED + || current->layout == L_STACKED) { + wlr_log(WLR_DEBUG, "Rejiggering out of tabs/stacks"); + workspace_rejigger(current, container, move_dir); + } else { + wlr_log(WLR_DEBUG, "Selecting output"); + current = current->parent; + } + break; + case C_CONTAINER: + case C_VIEW: + if (is_parallel(parent->layout, move_dir)) { + if ((index == parent->children->length - 1 && offs > 0) + || (index == 0 && offs < 0)) { + if (current->parent == container->parent) { + if (!parent->is_fullscreen && + (parent->layout == L_TABBED || + parent->layout == L_STACKED)) { + move_out_of_tabs_stacks(container, current, + move_dir, offs); + return; + } else { + wlr_log(WLR_DEBUG, "Hit limit, selecting parent"); + current = current->parent; + } + } else { + wlr_log(WLR_DEBUG, "Hit limit, " + "promoting descendant to sibling"); + // Special case + container_insert_child(current->parent, container, + index + (offs < 0 ? 0 : 1)); + container->width = container->height = 0; + return; + } + } else { + sibling = parent->children->items[index + offs]; + wlr_log(WLR_DEBUG, "Selecting sibling id:%zd", sibling->id); + } + } else if (!parent->is_fullscreen && (parent->layout == L_TABBED || + parent->layout == L_STACKED)) { + move_out_of_tabs_stacks(container, current, move_dir, offs); + return; + } else { + wlr_log(WLR_DEBUG, "Moving up to find a parallel container"); + current = current->parent; + } + break; + default: + sway_assert(0, "Not expecting to see container of type %s here", + container_type_to_str(current->type)); + return; + } + } + + // Part two: move stuff around + int index = container_sibling_index(container); + struct sway_container *old_parent = container->parent; + + while (sibling) { + switch (sibling->type) { + case C_VIEW: + if (sibling->parent == container->parent) { + wlr_log(WLR_DEBUG, "Swapping siblings"); + sibling->parent->children->items[index + offs] = container; + sibling->parent->children->items[index] = sibling; + } else { + wlr_log(WLR_DEBUG, "Promoting to sibling of cousin"); + container_insert_child(sibling->parent, container, + container_sibling_index(sibling) + (offs > 0 ? 0 : 1)); + container->width = container->height = 0; + } + sibling = NULL; + break; + case C_WORKSPACE: // Note: only in the case of moving between outputs + case C_CONTAINER: + if (is_parallel(sibling->layout, move_dir)) { + int limit = container_limit(sibling, invert_movement(move_dir)); + wlr_log(WLR_DEBUG, "limit: %d", limit); + wlr_log(WLR_DEBUG, + "Reparenting container (parallel) to index %d " + "(move dir: %d)", limit, move_dir); + container_insert_child(sibling, container, limit); + container->width = container->height = 0; + sibling = NULL; + } else { + wlr_log(WLR_DEBUG, "Reparenting container (perpendicular)"); + struct sway_container *focus_inactive = seat_get_focus_inactive( + config->handler_context.seat, sibling); + if (focus_inactive && focus_inactive != sibling) { + while (focus_inactive->parent != sibling) { + focus_inactive = focus_inactive->parent; + } + wlr_log(WLR_DEBUG, "Focus inactive: id:%zd", + focus_inactive->id); + sibling = focus_inactive; + continue; + } else if (sibling->children->length) { + wlr_log(WLR_DEBUG, "No focus-inactive, adding arbitrarily"); + container_remove_child(container); + container_add_sibling(sibling->children->items[0], container); + } else { + wlr_log(WLR_DEBUG, "No kiddos, adding container alone"); + container_remove_child(container); + container_add_child(sibling, container); + } + container->width = container->height = 0; + sibling = NULL; + } + break; + default: + sway_assert(0, "Not expecting to see container of type %s here", + container_type_to_str(sibling->type)); + return; + } + } + + container_notify_subtree_changed(old_parent); + container_notify_subtree_changed(container->parent); + + if (container->type == C_VIEW) { + ipc_event_window(container, "move"); + } + + if (old_parent) { + seat_set_focus(config->handler_context.seat, old_parent); + seat_set_focus(config->handler_context.seat, container); + } + + struct sway_container *last_ws = old_parent; + struct sway_container *next_ws = container->parent; + if (last_ws && last_ws->type != C_WORKSPACE) { + last_ws = container_parent(last_ws, C_WORKSPACE); + } + if (next_ws && next_ws->type != C_WORKSPACE) { + next_ws = container_parent(next_ws, C_WORKSPACE); } - return output_by_name(direction); + if (last_ws && next_ws && last_ws != next_ws) { + ipc_event_workspace(last_ws, next_ws, "focus"); + workspace_detect_urgent(last_ws); + workspace_detect_urgent(next_ws); + } + container_end_mouse_operation(container); } static struct cmd_results *cmd_move_container(struct sway_container *current, @@ -247,7 +682,7 @@ static void workspace_move_to_output(struct sway_container *workspace, } // Try to remove an empty workspace from the destination output. - container_reap_empty_recursive(new_output_focus); + container_reap_empty(new_output_focus); output_sort_workspaces(output); seat_set_focus(seat, output); @@ -359,7 +794,8 @@ static struct cmd_results *move_in_direction(struct sway_container *container, static const char *expected_position_syntax = "Expected 'move [absolute] position <x> [px] <y> [px]' or " - "'move [absolute] position center|mouse'"; + "'move [absolute] position center' or " + "'move position cursor|mouse|pointer'"; static struct cmd_results *move_to_position(struct sway_container *container, int argc, char **argv) { @@ -371,7 +807,10 @@ static struct cmd_results *move_to_position(struct sway_container *container, if (!argc) { return cmd_results_new(CMD_FAILURE, "move", expected_position_syntax); } + + bool absolute = false; if (strcmp(argv[0], "absolute") == 0) { + absolute = true; --argc; ++argv; } @@ -385,7 +824,8 @@ static struct cmd_results *move_to_position(struct sway_container *container, if (!argc) { return cmd_results_new(CMD_FAILURE, "move", expected_position_syntax); } - if (strcmp(argv[0], "mouse") == 0) { + if (strcmp(argv[0], "cursor") == 0 || strcmp(argv[0], "mouse") == 0 || + strcmp(argv[0], "pointer") == 0) { struct sway_seat *seat = config->handler_context.seat; if (!seat->cursor) { return cmd_results_new(CMD_FAILURE, "move", "No cursor device"); @@ -395,9 +835,15 @@ static struct cmd_results *move_to_position(struct sway_container *container, container_floating_move_to(container, lx, ly); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } else if (strcmp(argv[0], "center") == 0) { - struct sway_container *ws = container_parent(container, C_WORKSPACE); - double lx = ws->x + (ws->width - container->width) / 2; - double ly = ws->y + (ws->height - container->height) / 2; + double lx, ly; + if (absolute) { + lx = root_container.x + (root_container.width - container->width) / 2; + ly = root_container.y + (root_container.height - container->height) / 2; + } else { + struct sway_container *ws = container_parent(container, C_WORKSPACE); + lx = ws->x + (ws->width - container->width) / 2; + ly = ws->y + (ws->height - container->height) / 2; + } container_floating_move_to(container, lx, ly); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } @@ -429,6 +875,11 @@ static struct cmd_results *move_to_position(struct sway_container *container, "Invalid position specified"); } + if (!absolute) { + struct sway_container *ws = container_parent(container, C_WORKSPACE); + lx += ws->x; + ly += ws->y; + } container_floating_move_to(container, lx, ly); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/output.c b/sway/commands/output.c index ef1b7a69..00910843 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -1,7 +1,6 @@ #include "sway/commands.h" #include "sway/config.h" #include "sway/output.h" -#include "sway/tree/layout.h" #include "list.h" #include "log.h" diff --git a/sway/commands/resize.c b/sway/commands/resize.c index ea1e36ff..ad659ef5 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c @@ -159,6 +159,27 @@ static int parallel_size(struct sway_container *c, enum resize_axis a) { return normalize_axis(a) == RESIZE_AXIS_HORIZONTAL ? c->width : c->height; } +static void container_recursive_resize(struct sway_container *container, + double amount, enum wlr_edges edge) { + bool layout_match = true; + wlr_log(WLR_DEBUG, "Resizing %p with amount: %f", container, amount); + if (edge == WLR_EDGE_LEFT || edge == WLR_EDGE_RIGHT) { + container->width += amount; + layout_match = container->layout == L_HORIZ; + } else if (edge == WLR_EDGE_TOP || edge == WLR_EDGE_BOTTOM) { + container->height += amount; + layout_match = container->layout == L_VERT; + } + if (container->children) { + for (int i = 0; i < container->children->length; i++) { + struct sway_container *child = container->children->items[i]; + double amt = layout_match ? + amount / container->children->length : amount; + container_recursive_resize(child, amt, edge); + } + } +} + static void resize_tiled(struct sway_container *parent, int amount, enum resize_axis axis) { struct sway_container *focused = parent; diff --git a/sway/commands/show_marks.c b/sway/commands/show_marks.c index dd7d170c..1844e917 100644 --- a/sway/commands/show_marks.c +++ b/sway/commands/show_marks.c @@ -2,6 +2,7 @@ #include <string.h> #include "sway/commands.h" #include "sway/config.h" +#include "sway/tree/root.h" #include "sway/tree/view.h" #include "sway/output.h" #include "list.h" diff --git a/sway/commands/sticky.c b/sway/commands/sticky.c index a0dd7215..8692e08d 100644 --- a/sway/commands/sticky.c +++ b/sway/commands/sticky.c @@ -6,8 +6,8 @@ #include "sway/output.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" -#include "sway/tree/layout.h" #include "sway/tree/view.h" +#include "sway/tree/workspace.h" #include "list.h" struct cmd_results *cmd_sticky(int argc, char **argv) { @@ -44,7 +44,9 @@ struct cmd_results *cmd_sticky(int argc, char **argv) { struct sway_container *focused_workspace = container_parent(focus, C_WORKSPACE); struct sway_container *current_workspace = container_parent(container, C_WORKSPACE); if (current_workspace != focused_workspace) { - container_move_to(container, focused_workspace); + container_remove_child(container); + workspace_add_floating(focused_workspace, container); + container_handle_fullscreen_reparent(container, current_workspace); arrange_windows(focused_workspace); if (!container_reap_empty(current_workspace)) { arrange_windows(current_workspace); diff --git a/sway/commands/swap.c b/sway/commands/swap.c index f881a002..f25c43a1 100644 --- a/sway/commands/swap.c +++ b/sway/commands/swap.c @@ -1,15 +1,141 @@ +#define _POSIX_C_SOURCE 200809L #include <strings.h> #include <wlr/util/log.h> #include "config.h" +#include "log.h" #include "sway/commands.h" #include "sway/tree/arrange.h" -#include "sway/tree/layout.h" +#include "sway/tree/root.h" #include "sway/tree/view.h" +#include "sway/tree/workspace.h" #include "stringop.h" static const char* EXPECTED_SYNTAX = "Expected 'swap container with id|con_id|mark <arg>'"; +static void swap_places(struct sway_container *con1, + struct sway_container *con2) { + struct sway_container *temp = malloc(sizeof(struct sway_container)); + temp->x = con1->x; + temp->y = con1->y; + temp->width = con1->width; + temp->height = con1->height; + temp->parent = con1->parent; + + con1->x = con2->x; + con1->y = con2->y; + con1->width = con2->width; + con1->height = con2->height; + + con2->x = temp->x; + con2->y = temp->y; + con2->width = temp->width; + con2->height = temp->height; + + int temp_index = container_sibling_index(con1); + container_insert_child(con2->parent, con1, container_sibling_index(con2)); + container_insert_child(temp->parent, con2, temp_index); + + free(temp); +} + +static void swap_focus(struct sway_container *con1, + struct sway_container *con2, struct sway_seat *seat, + struct sway_container *focus) { + if (focus == con1 || focus == con2) { + struct sway_container *ws1 = container_parent(con1, C_WORKSPACE); + struct sway_container *ws2 = container_parent(con2, C_WORKSPACE); + if (focus == con1 && (con2->parent->layout == L_TABBED + || con2->parent->layout == L_STACKED)) { + if (workspace_is_visible(ws2)) { + seat_set_focus_warp(seat, con2, false, true); + } + seat_set_focus(seat, ws1 != ws2 ? con2 : con1); + } else if (focus == con2 && (con1->parent->layout == L_TABBED + || con1->parent->layout == L_STACKED)) { + if (workspace_is_visible(ws1)) { + seat_set_focus_warp(seat, con1, false, true); + } + seat_set_focus(seat, ws1 != ws2 ? con1 : con2); + } else if (ws1 != ws2) { + seat_set_focus(seat, focus == con1 ? con2 : con1); + } else { + seat_set_focus(seat, focus); + } + } else { + seat_set_focus(seat, focus); + } +} + +static void container_swap(struct sway_container *con1, + struct sway_container *con2) { + if (!sway_assert(con1 && con2, "Cannot swap with nothing")) { + return; + } + if (!sway_assert(con1->type >= C_CONTAINER && con2->type >= C_CONTAINER, + "Can only swap containers and views")) { + return; + } + if (!sway_assert(!container_has_ancestor(con1, con2) + && !container_has_ancestor(con2, con1), + "Cannot swap ancestor and descendant")) { + return; + } + if (!sway_assert(!container_is_floating(con1) + && !container_is_floating(con2), + "Swapping with floating containers is not supported")) { + return; + } + + wlr_log(WLR_DEBUG, "Swapping containers %zu and %zu", con1->id, con2->id); + + int fs1 = con1->is_fullscreen; + int fs2 = con2->is_fullscreen; + if (fs1) { + container_set_fullscreen(con1, false); + } + if (fs2) { + container_set_fullscreen(con2, false); + } + + struct sway_seat *seat = input_manager_get_default_seat(input_manager); + struct sway_container *focus = seat_get_focus(seat); + struct sway_container *vis1 = container_parent( + seat_get_focus_inactive(seat, container_parent(con1, C_OUTPUT)), + C_WORKSPACE); + struct sway_container *vis2 = container_parent( + seat_get_focus_inactive(seat, container_parent(con2, C_OUTPUT)), + C_WORKSPACE); + + char *stored_prev_name = NULL; + if (prev_workspace_name) { + stored_prev_name = strdup(prev_workspace_name); + } + + swap_places(con1, con2); + + if (!workspace_is_visible(vis1)) { + seat_set_focus(seat, seat_get_focus_inactive(seat, vis1)); + } + if (!workspace_is_visible(vis2)) { + seat_set_focus(seat, seat_get_focus_inactive(seat, vis2)); + } + + swap_focus(con1, con2, seat, focus); + + if (stored_prev_name) { + free(prev_workspace_name); + prev_workspace_name = stored_prev_name; + } + + if (fs1) { + container_set_fullscreen(con2, true); + } + if (fs2) { + container_set_fullscreen(con1, true); + } +} + static bool test_con_id(struct sway_container *container, void *con_id) { return container->id == (size_t)con_id; } diff --git a/sway/commands/unmark.c b/sway/commands/unmark.c index c183785b..62127c97 100644 --- a/sway/commands/unmark.c +++ b/sway/commands/unmark.c @@ -2,6 +2,7 @@ #include <string.h> #include "sway/commands.h" #include "sway/config.h" +#include "sway/tree/root.h" #include "sway/tree/view.h" #include "list.h" #include "log.h" diff --git a/sway/commands/urgent.c b/sway/commands/urgent.c index 51c497c4..bccb33fe 100644 --- a/sway/commands/urgent.c +++ b/sway/commands/urgent.c @@ -4,7 +4,6 @@ #include "sway/tree/arrange.h" #include "sway/tree/container.h" #include "sway/tree/view.h" -#include "sway/tree/layout.h" #include "util.h" struct cmd_results *cmd_urgent(int argc, char **argv) { diff --git a/sway/config.c b/sway/config.c index 642abbac..8105722a 100644 --- a/sway/config.c +++ b/sway/config.c @@ -27,7 +27,7 @@ #include "sway/criteria.h" #include "sway/swaynag.h" #include "sway/tree/arrange.h" -#include "sway/tree/layout.h" +#include "sway/tree/root.h" #include "sway/tree/workspace.h" #include "cairo.h" #include "pango.h" diff --git a/sway/config/output.c b/sway/config/output.c index 1d8cb3ef..16ec9339 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -9,6 +9,7 @@ #include <wlr/types/wlr_output_layout.h> #include "sway/config.h" #include "sway/output.h" +#include "sway/tree/root.h" #include "log.h" int output_name_cmp(const void *item, const void *data) { @@ -181,13 +182,11 @@ void apply_output_config(struct output_config *oc, struct sway_container *output struct wlr_output *wlr_output = output->sway_output->wlr_output; if (oc && oc->enabled == 0) { - struct sway_output *sway_output = output->sway_output; if (output->sway_output->bg_pid != 0) { terminate_swaybg(output->sway_output->bg_pid); output->sway_output->bg_pid = 0; } - container_destroy(output); - sway_output->swayc = NULL; + output_begin_destroy(output); wlr_output_layout_remove(root_container.sway_root->output_layout, wlr_output); return; @@ -290,7 +289,8 @@ void apply_output_config_to_outputs(struct output_config *oc) { bool wildcard = strcmp(oc->name, "*") == 0; char id[128]; struct sway_output *sway_output; - wl_list_for_each(sway_output, &root_container.sway_root->outputs, link) { + wl_list_for_each(sway_output, + &root_container.sway_root->all_outputs, link) { char *name = sway_output->wlr_output->name; output_get_identifier(id, sizeof(id), sway_output); if (wildcard || !strcmp(name, oc->name) || !strcmp(id, oc->name)) { @@ -350,7 +350,8 @@ static void default_output_config(struct output_config *oc, void create_default_output_configs(void) { struct sway_output *sway_output; - wl_list_for_each(sway_output, &root_container.sway_root->outputs, link) { + wl_list_for_each(sway_output, + &root_container.sway_root->all_outputs, link) { char *name = sway_output->wlr_output->name; struct output_config *oc = new_output_config(name); default_output_config(oc, sway_output->wlr_output); diff --git a/sway/criteria.c b/sway/criteria.c index 81c2325a..5452c4ee 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -6,6 +6,7 @@ #include "sway/criteria.h" #include "sway/tree/container.h" #include "sway/config.h" +#include "sway/tree/root.h" #include "sway/tree/view.h" #include "stringop.h" #include "list.h" diff --git a/sway/debug-tree.c b/sway/debug-tree.c index ea0826b9..2768cf58 100644 --- a/sway/debug-tree.c +++ b/sway/debug-tree.c @@ -9,7 +9,7 @@ #include "sway/output.h" #include "sway/server.h" #include "sway/tree/container.h" -#include "sway/tree/layout.h" +#include "sway/tree/root.h" #include "cairo.h" #include "config.h" #include "pango.h" diff --git a/sway/desktop/layer_shell.c b/sway/desktop/layer_shell.c index a2935883..1fae5db2 100644 --- a/sway/desktop/layer_shell.c +++ b/sway/desktop/layer_shell.c @@ -14,7 +14,6 @@ #include "sway/output.h" #include "sway/server.h" #include "sway/tree/arrange.h" -#include "sway/tree/layout.h" #include "log.h" static void apply_exclusive(struct wlr_box *usable_area, diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 3d8bbff5..c228979d 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -16,6 +16,7 @@ #include "log.h" #include "config.h" #include "sway/config.h" +#include "sway/desktop/transaction.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" #include "sway/layers.h" @@ -23,7 +24,7 @@ #include "sway/server.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" -#include "sway/tree/layout.h" +#include "sway/tree/root.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" @@ -498,7 +499,7 @@ void output_damage_whole_container(struct sway_output *output, static void damage_handle_destroy(struct wl_listener *listener, void *data) { struct sway_output *output = wl_container_of(listener, output, damage_destroy); - container_destroy(output->swayc); + output_begin_destroy(output->swayc); } static void handle_destroy(struct wl_listener *listener, void *data) { @@ -506,7 +507,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_signal_emit(&output->events.destroy, output); if (output->swayc) { - container_destroy(output->swayc); + output_begin_destroy(output->swayc); } wl_list_remove(&output->link); @@ -556,7 +557,7 @@ void handle_new_output(struct wl_listener *listener, void *data) { wl_signal_add(&wlr_output->events.destroy, &output->destroy); output->destroy.notify = handle_destroy; - wl_list_insert(&root_container.sway_root->outputs, &output->link); + wl_list_insert(&root_container.sway_root->all_outputs, &output->link); if (!wl_list_empty(&wlr_output->modes)) { struct wlr_output_mode *mode = diff --git a/sway/desktop/render.c b/sway/desktop/render.c index 5cf8abc0..5556e5b3 100644 --- a/sway/desktop/render.c +++ b/sway/desktop/render.c @@ -24,7 +24,7 @@ #include "sway/server.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" -#include "sway/tree/layout.h" +#include "sway/tree/root.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" @@ -619,9 +619,7 @@ static void render_container_tabbed(struct sway_output *output, struct sway_container *current = pstate->focused_inactive_child; struct border_colors *current_colors = &config->border_colors.unfocused; - double width_gap_adjustment = 2 * pstate->current_gaps; - int tab_width = - (pstate->swayc_width - width_gap_adjustment) / pstate->children->length; + int tab_width = (pstate->swayc_width) / pstate->children->length; // Render tabs for (int i = 0; i < pstate->children->length; ++i) { @@ -656,11 +654,10 @@ static void render_container_tabbed(struct sway_output *output, // Make last tab use the remaining width of the parent if (i == pstate->children->length - 1) { - tab_width = - pstate->swayc_width - width_gap_adjustment - tab_width * i; + tab_width = pstate->swayc_width - tab_width * i; } - render_titlebar(output, damage, child, x, cstate->swayc_y, tab_width, + render_titlebar(output, damage, child, x, pstate->swayc_y, tab_width, colors, title_texture, marks_texture); if (child == current) { @@ -721,9 +718,9 @@ static void render_container_stacked(struct sway_output *output, marks_texture = view ? view->marks_unfocused : NULL; } - int y = cstate->swayc_y + titlebar_height * i; - render_titlebar(output, damage, child, cstate->swayc_x, y, - cstate->swayc_width, colors, title_texture, marks_texture); + int y = pstate->swayc_y + titlebar_height * i; + render_titlebar(output, damage, child, pstate->swayc_x, y, + pstate->swayc_width, colors, title_texture, marks_texture); if (child == current) { current_colors = colors; diff --git a/sway/desktop/transaction.c b/sway/desktop/transaction.c index f82e5ef2..c18529fb 100644 --- a/sway/desktop/transaction.c +++ b/sway/desktop/transaction.c @@ -54,7 +54,22 @@ static void transaction_destroy(struct sway_transaction *transaction) { con->instruction = NULL; } if (con->destroying && con->ntxnrefs == 0) { - container_free(con); + switch (con->type) { + case C_ROOT: + break; + case C_OUTPUT: + output_destroy(con); + break; + case C_WORKSPACE: + workspace_destroy(con); + break; + case C_CONTAINER: + case C_VIEW: + container_destroy(con); + break; + case C_TYPES: + break; + } } free(instruction); } diff --git a/sway/desktop/xdg_shell.c b/sway/desktop/xdg_shell.c index aae129bd..587deb0f 100644 --- a/sway/desktop/xdg_shell.c +++ b/sway/desktop/xdg_shell.c @@ -8,12 +8,12 @@ #include "log.h" #include "sway/decoration.h" #include "sway/desktop.h" +#include "sway/desktop/transaction.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" #include "sway/server.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" -#include "sway/tree/layout.h" #include "sway/tree/view.h" static const struct sway_view_child_impl popup_impl; @@ -448,7 +448,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_list_remove(&xdg_shell_view->map.link); wl_list_remove(&xdg_shell_view->unmap.link); view->wlr_xdg_surface = NULL; - view_destroy(view); + view_begin_destroy(view); } struct sway_view *view_from_wlr_xdg_surface( diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 277c53a3..175416f3 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -7,12 +7,12 @@ #include "log.h" #include "sway/decoration.h" #include "sway/desktop.h" +#include "sway/desktop/transaction.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" #include "sway/server.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" -#include "sway/tree/layout.h" #include "sway/tree/view.h" static const struct sway_view_child_impl popup_impl; @@ -441,7 +441,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_list_remove(&xdg_shell_v6_view->map.link); wl_list_remove(&xdg_shell_v6_view->unmap.link); view->wlr_xdg_surface_v6 = NULL; - view_destroy(view); + view_begin_destroy(view); } struct sway_view *view_from_wlr_xdg_surface_v6( diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index ce7235e4..4e401008 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -14,7 +14,6 @@ #include "sway/server.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" -#include "sway/tree/layout.h" #include "sway/tree/view.h" static const char *atom_map[ATOM_LAST] = { @@ -341,7 +340,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_list_remove(&xwayland_view->set_hints.link); wl_list_remove(&xwayland_view->map.link); wl_list_remove(&xwayland_view->unmap.link); - view_destroy(&xwayland_view->view); + view_begin_destroy(&xwayland_view->view); } static void handle_unmap(struct wl_listener *listener, void *data) { diff --git a/sway/input/cursor.c b/sway/input/cursor.c index ba5e0400..00240e84 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -20,6 +20,7 @@ #include "sway/layers.h" #include "sway/output.h" #include "sway/tree/arrange.h" +#include "sway/tree/root.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" #include "wlr-layer-shell-unstable-v1-protocol.h" diff --git a/sway/input/seat.c b/sway/input/seat.c index 4077a8dd..36e1d232 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -25,10 +25,9 @@ #include "sway/output.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" -#include "sway/tree/container.h" +#include "sway/tree/root.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" -#include "sway/tree/workspace.h" static void seat_device_destroy(struct sway_seat_device *seat_device) { if (!seat_device) { @@ -594,7 +593,7 @@ void seat_set_focus_warp(struct sway_seat *seat, } struct sway_container *last_focus = seat_get_focus(seat); - if (container && last_focus == container) { + if (last_focus == container) { return; } @@ -602,14 +601,26 @@ void seat_set_focus_warp(struct sway_seat *seat, if (last_workspace && last_workspace->type != C_WORKSPACE) { last_workspace = container_parent(last_workspace, C_WORKSPACE); } + + if (container == NULL) { + // Close any popups on the old focus + if (last_focus->type == C_VIEW) { + view_close_popups(last_focus->sway_view); + } + seat_send_unfocus(last_focus, seat); + seat->has_focus = false; + update_debug_tree(); + return; + } + struct sway_container *new_workspace = container; - if (new_workspace && new_workspace->type != C_WORKSPACE) { + if (new_workspace->type != C_WORKSPACE) { new_workspace = container_parent(new_workspace, C_WORKSPACE); } - if (last_workspace && last_workspace == new_workspace + if (last_workspace == new_workspace && last_workspace->sway_workspace->fullscreen - && container && !container_is_fullscreen_or_child(container)) { + && !container_is_fullscreen_or_child(container)) { return; } @@ -618,17 +629,17 @@ void seat_set_focus_warp(struct sway_seat *seat, last_output = container_parent(last_output, C_OUTPUT); } struct sway_container *new_output = container; - if (new_output && new_output->type != C_OUTPUT) { + if (new_output->type != C_OUTPUT) { new_output = container_parent(new_output, C_OUTPUT); } // find new output's old workspace, which might have to be removed if empty struct sway_container *new_output_last_ws = NULL; - if (last_output && new_output && last_output != new_output) { + if (last_output != new_output) { new_output_last_ws = seat_get_active_child(seat, new_output); } - if (container && container->parent) { + if (container->parent) { struct sway_seat_container *seat_con = seat_container_from_container(seat, container); if (seat_con == NULL) { @@ -643,8 +654,7 @@ void seat_set_focus_warp(struct sway_seat *seat, wl_list_insert(&seat->focus_stack, &parent->link); container_set_dirty(parent->container); - parent = - seat_container_from_container(seat, + parent = seat_container_from_container(seat, parent->container->parent); } @@ -653,19 +663,33 @@ void seat_set_focus_warp(struct sway_seat *seat, if (last_focus) { seat_send_unfocus(last_focus, seat); + container_set_dirty(last_focus); } seat_send_focus(container, seat); container_set_dirty(container); container_set_dirty(container->parent); // for focused_inactive_child - if (last_focus) { - container_set_dirty(last_focus); - } + } + + // emit ipc events + if (notify && new_workspace && last_workspace != new_workspace) { + ipc_event_workspace(last_workspace, new_workspace, "focus"); + } + if (container->type == C_VIEW) { + ipc_event_window(container, "focus"); + } + + if (new_output_last_ws) { + workspace_consider_destroy(new_output_last_ws); + } + + // Close any popups on the old focus + if (last_focus && last_focus->type == C_VIEW) { + view_close_popups(last_focus->sway_view); } // If urgent, either unset the urgency or start a timer to unset it - if (container && container->type == C_VIEW && - view_is_urgent(container->sway_view) && + if (container->type == C_VIEW && view_is_urgent(container->sway_view) && !container->sway_view->urgent_timer) { struct sway_view *view = container->sway_view; if (last_workspace && last_workspace != new_workspace && @@ -687,46 +711,20 @@ void seat_set_focus_warp(struct sway_seat *seat, // If we've focused a floating container, bring it to the front. // We do this by putting it at the end of the floating list. - if (container && container_is_floating(container)) { - list_move_to_end( - container->parent->sway_workspace->floating, container); + struct sway_container *floater = container; + while (floater->parent && floater->parent->type != C_WORKSPACE) { + floater = floater->parent; } - - // clean up unfocused empty workspace on new output - if (new_output_last_ws) { - if (!workspace_is_visible(new_output_last_ws) - && workspace_is_empty(new_output_last_ws)) { - if (last_workspace == new_output_last_ws) { - last_focus = NULL; - last_workspace = NULL; - } - container_destroy(new_output_last_ws); - } - } - - // Close any popups on the old focus - if (last_focus && last_focus != container) { - if (last_focus->type == C_VIEW) { - view_close_popups(last_focus->sway_view); - } + if (container_is_floating(floater)) { + list_move_to_end(floater->parent->sway_workspace->floating, floater); } if (last_focus) { if (last_workspace) { - if (notify && last_workspace != new_workspace) { - ipc_event_workspace(last_workspace, new_workspace, "focus"); - } - if (!workspace_is_visible(last_workspace) - && workspace_is_empty(last_workspace)) { - if (last_workspace == last_focus) { - last_focus = NULL; - } - container_destroy(last_workspace); - } + workspace_consider_destroy(last_workspace); } - if (config->mouse_warping && warp) { - if (new_output && last_output && new_output != last_output) { + if (config->mouse_warping && warp && new_output != last_output) { double x = container->x + container->width / 2.0; double y = container->y + container->height / 2.0; struct wlr_output *wlr_output = @@ -737,20 +735,11 @@ void seat_set_focus_warp(struct sway_seat *seat, seat->cursor->cursor->y)) { wlr_cursor_warp(seat->cursor->cursor, NULL, x, y); cursor_send_pointer_motion(seat->cursor, 0, true); - } } } } - if (container) { - if (container->type == C_VIEW) { - ipc_event_window(container, "focus"); - } else if (container->type == C_WORKSPACE) { - ipc_event_workspace(NULL, container, "focus"); - } - } - - seat->has_focus = (container != NULL); + seat->has_focus = true; update_debug_tree(); } diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 34e940ad..ed710be5 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -31,6 +31,7 @@ #include "sway/server.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" +#include "sway/tree/root.h" #include "sway/tree/view.h" #include "list.h" #include "log.h" @@ -615,7 +616,7 @@ void ipc_client_handle_command(struct ipc_client *client) { } } struct sway_output *output; - wl_list_for_each(output, &root_container.sway_root->outputs, link) { + wl_list_for_each(output, &root_container.sway_root->all_outputs, link) { if (!output->swayc) { json_object_array_add(outputs, ipc_json_describe_disabled_output(output)); diff --git a/sway/main.c b/sway/main.c index 3ba4ba75..7ed10c86 100644 --- a/sway/main.c +++ b/sway/main.c @@ -23,7 +23,7 @@ #include "sway/desktop/transaction.h" #include "sway/server.h" #include "sway/swaynag.h" -#include "sway/tree/layout.h" +#include "sway/tree/root.h" #include "sway/ipc-server.h" #include "ipc-client.h" #include "readline.h" diff --git a/sway/meson.build b/sway/meson.build index 676422d0..bcb44e8b 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -150,7 +150,6 @@ sway_sources = files( 'tree/arrange.c', 'tree/container.c', - 'tree/layout.c', 'tree/root.c', 'tree/view.c', 'tree/workspace.c', diff --git a/sway/server.c b/sway/server.c index 00acaa01..7fa6007e 100644 --- a/sway/server.c +++ b/sway/server.c @@ -24,7 +24,7 @@ #include "sway/desktop/idle_inhibit_v1.h" #include "sway/input/input-manager.h" #include "sway/server.h" -#include "sway/tree/layout.h" +#include "sway/tree/root.h" #include "config.h" #ifdef HAVE_XWAYLAND #include "sway/xwayland.h" diff --git a/sway/sway.5.scd b/sway/sway.5.scd index 83188067..927bf55c 100644 --- a/sway/sway.5.scd +++ b/sway/sway.5.scd @@ -133,10 +133,15 @@ They are expected to be used with *bindsym* or at runtime through *swaymsg*(1). tiled containers. *move* [absolute] position <pos\_x> [px] <pos\_y> [px] - Moves the focused container to the specified position. + Moves the focused container to the specified position in the workspace. If + _absolute_ is used, the position is relative to all outputs. -*move* [absolute] position center|mouse - Moves the focused container to be centered on the workspace or mouse. +*move* [absolute] position center + Moves the focused container to be centered on the workspace. If _absolute_ + is used, it is moved to the center of all outputs. + +*move* position cursor|mouse|pointer + Moves the focused container to be centered on the cursor. *move* container|window [to] mark <mark> Moves the focused container to the specified mark. diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index a4b058f3..92f20fcc 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -7,39 +7,29 @@ #include <wlr/types/wlr_output_layout.h> #include "sway/tree/arrange.h" #include "sway/tree/container.h" -#include "sway/tree/layout.h" #include "sway/output.h" #include "sway/tree/workspace.h" #include "sway/tree/view.h" #include "list.h" #include "log.h" -static void apply_horiz_layout(struct sway_container *parent) { - size_t num_children = parent->children->length; - if (!num_children) { +static void apply_horiz_layout(list_t *children, struct wlr_box *parent) { + if (!children->length) { return; } - size_t parent_offset = 0; - if (parent->parent->layout == L_TABBED) { - parent_offset = container_titlebar_height(); - } else if (parent->parent->layout == L_STACKED) { - parent_offset = container_titlebar_height() * - parent->parent->children->length; - } - size_t parent_height = parent->height - parent_offset; // Calculate total width of children double total_width = 0; - for (size_t i = 0; i < num_children; ++i) { - struct sway_container *child = parent->children->items[i]; + for (int i = 0; i < children->length; ++i) { + struct sway_container *child = children->items[i]; if (child->width <= 0) { - if (num_children > 1) { - child->width = parent->width / (num_children - 1); + if (children->length > 1) { + child->width = parent->width / (children->length - 1); } else { child->width = parent->width; } } - remove_gaps(child); + container_remove_gaps(child); total_width += child->width; } double scale = parent->width / total_width; @@ -47,63 +37,48 @@ static void apply_horiz_layout(struct sway_container *parent) { // Resize windows wlr_log(WLR_DEBUG, "Arranging %p horizontally", parent); double child_x = parent->x; - for (size_t i = 0; i < num_children; ++i) { - struct sway_container *child = parent->children->items[i]; - wlr_log(WLR_DEBUG, - "Calculating arrangement for %p:%d (will scale %f by %f)", - child, child->type, child->width, scale); + for (int i = 0; i < children->length; ++i) { + struct sway_container *child = children->items[i]; child->x = child_x; - child->y = parent->y + parent_offset; + child->y = parent->y; child->width = floor(child->width * scale); - child->height = parent_height; + child->height = parent->height; child_x += child->width; // Make last child use remaining width of parent - if (i == num_children - 1) { + if (i == children->length - 1) { child->width = parent->x + parent->width - child->x; } - add_gaps(child); + container_add_gaps(child); } } -static void apply_vert_layout(struct sway_container *parent) { - size_t num_children = parent->children->length; - if (!num_children) { +static void apply_vert_layout(list_t *children, struct wlr_box *parent) { + if (!children->length) { return; } - size_t parent_offset = 0; - if (parent->parent->layout == L_TABBED) { - parent_offset = container_titlebar_height(); - } else if (parent->parent->layout == L_STACKED) { - parent_offset = - container_titlebar_height() * parent->parent->children->length; - } - size_t parent_height = parent->height + parent_offset; // Calculate total height of children double total_height = 0; - for (size_t i = 0; i < num_children; ++i) { - struct sway_container *child = parent->children->items[i]; + for (int i = 0; i < children->length; ++i) { + struct sway_container *child = children->items[i]; if (child->height <= 0) { - if (num_children > 1) { - child->height = parent_height / (num_children - 1); + if (children->length > 1) { + child->height = parent->height / (children->length - 1); } else { - child->height = parent_height; + child->height = parent->height; } } - remove_gaps(child); + container_remove_gaps(child); total_height += child->height; } - double scale = parent_height / total_height; + double scale = parent->height / total_height; // Resize wlr_log(WLR_DEBUG, "Arranging %p vertically", parent); - double child_y = parent->y + parent_offset; - for (size_t i = 0; i < num_children; ++i) { - struct sway_container *child = parent->children->items[i]; - wlr_log(WLR_DEBUG, - "Calculating arrangement for %p:%d (will scale %f by %f)", - child, child->type, child->height, scale); + double child_y = parent->y; + for (int i = 0; i < children->length; ++i) { + struct sway_container *child = children->items[i]; child->x = parent->x; child->y = child_y; child->width = parent->width; @@ -111,101 +86,112 @@ static void apply_vert_layout(struct sway_container *parent) { child_y += child->height; // Make last child use remaining height of parent - if (i == num_children - 1) { - child->height = - parent->y + parent_offset + parent_height - child->y; + if (i == children->length - 1) { + child->height = parent->y + parent->height - child->y; } - add_gaps(child); + container_add_gaps(child); } } -static void apply_tabbed_or_stacked_layout(struct sway_container *parent) { - if (!parent->children->length) { +static void apply_tabbed_layout(list_t *children, struct wlr_box *parent) { + if (!children->length) { return; } - size_t parent_offset = 0; - if (parent->parent->layout == L_TABBED) { - parent_offset = container_titlebar_height(); - } else if (parent->parent->layout == L_STACKED) { - parent_offset = - container_titlebar_height() * parent->parent->children->length; - } + size_t parent_offset = container_titlebar_height(); size_t parent_height = parent->height - parent_offset; - for (int i = 0; i < parent->children->length; ++i) { - struct sway_container *child = parent->children->items[i]; - remove_gaps(child); + for (int i = 0; i < children->length; ++i) { + struct sway_container *child = children->items[i]; + container_remove_gaps(child); child->x = parent->x; child->y = parent->y + parent_offset; child->width = parent->width; child->height = parent_height; - add_gaps(child); + container_add_gaps(child); } } -static void arrange_children_of(struct sway_container *parent); +static void apply_stacked_layout(list_t *children, struct wlr_box *parent) { + if (!children->length) { + return; + } + size_t parent_offset = container_titlebar_height() * children->length; + size_t parent_height = parent->height - parent_offset; + for (int i = 0; i < children->length; ++i) { + struct sway_container *child = children->items[i]; + container_remove_gaps(child); + child->x = parent->x; + child->y = parent->y + parent_offset; + child->width = parent->width; + child->height = parent_height; + container_add_gaps(child); + } +} static void arrange_floating(list_t *floating) { for (int i = 0; i < floating->length; ++i) { struct sway_container *floater = floating->items[i]; - if (floater->type == C_VIEW) { - view_autoconfigure(floater->sway_view); - } else { - arrange_children_of(floater); - } - container_set_dirty(floater); + arrange_container(floater); } } -static void arrange_children_of(struct sway_container *parent) { - if (config->reloading) { - return; - } - wlr_log(WLR_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", parent, - parent->name, parent->width, parent->height, parent->x, parent->y); - +static void arrange_children(list_t *children, + enum sway_container_layout layout, struct wlr_box *parent) { // Calculate x, y, width and height of children - switch (parent->layout) { + switch (layout) { case L_HORIZ: - apply_horiz_layout(parent); + apply_horiz_layout(children, parent); break; case L_VERT: - apply_vert_layout(parent); + apply_vert_layout(children, parent); break; case L_TABBED: + apply_tabbed_layout(children, parent); + break; case L_STACKED: - apply_tabbed_or_stacked_layout(parent); + apply_stacked_layout(children, parent); break; case L_NONE: - apply_horiz_layout(parent); + apply_horiz_layout(children, parent); break; } // Recurse into child containers - for (int i = 0; i < parent->children->length; ++i) { - struct sway_container *child = parent->children->items[i]; - if (parent->has_gaps && !child->has_gaps) { - child->has_gaps = true; - child->gaps_inner = parent->gaps_inner; - child->gaps_outer = parent->gaps_outer; - } - if (child->type == C_VIEW) { - view_autoconfigure(child->sway_view); - } else { - arrange_children_of(child); - } - container_set_dirty(child); + for (int i = 0; i < children->length; ++i) { + struct sway_container *child = children->items[i]; + arrange_container(child); } } -static void arrange_workspace(struct sway_container *workspace) { +void arrange_container(struct sway_container *container) { if (config->reloading) { return; } + if (container->type == C_VIEW) { + view_autoconfigure(container->sway_view); + container_set_dirty(container); + return; + } + if (!sway_assert(container->type == C_CONTAINER, "Expected a container")) { + return; + } + struct wlr_box box; + container_get_box(container, &box); + arrange_children(container->children, container->layout, &box); + container_set_dirty(container); +} + +void arrange_workspace(struct sway_container *workspace) { + if (config->reloading) { + return; + } + if (!sway_assert(workspace->type == C_WORKSPACE, "Expected a workspace")) { + return; + } struct sway_container *output = workspace->parent; struct wlr_box *area = &output->sway_output->usable_area; wlr_log(WLR_DEBUG, "Usable area for ws: %dx%d@%d,%d", area->width, area->height, area->x, area->y); - remove_gaps(workspace); + workspace_remove_gaps(workspace); double prev_x = workspace->x; double prev_y = workspace->y; @@ -217,20 +203,22 @@ static void arrange_workspace(struct sway_container *workspace) { // Adjust any floating containers double diff_x = workspace->x - prev_x; double diff_y = workspace->y - prev_y; - for (int i = 0; i < workspace->sway_workspace->floating->length; ++i) { - struct sway_container *floater = - workspace->sway_workspace->floating->items[i]; - container_floating_translate(floater, diff_x, diff_y); - double center_x = floater->x + floater->width / 2; - double center_y = floater->y + floater->height / 2; - struct wlr_box workspace_box; - container_get_box(workspace, &workspace_box); - if (!wlr_box_contains_point(&workspace_box, center_x, center_y)) { - container_floating_move_to_center(floater); + if (diff_x != 0 || diff_y != 0) { + for (int i = 0; i < workspace->sway_workspace->floating->length; ++i) { + struct sway_container *floater = + workspace->sway_workspace->floating->items[i]; + container_floating_translate(floater, diff_x, diff_y); + double center_x = floater->x + floater->width / 2; + double center_y = floater->y + floater->height / 2; + struct wlr_box workspace_box; + container_get_box(workspace, &workspace_box); + if (!wlr_box_contains_point(&workspace_box, center_x, center_y)) { + container_floating_move_to_center(floater); + } } } - add_gaps(workspace); + workspace_add_gaps(workspace); container_set_dirty(workspace); wlr_log(WLR_DEBUG, "Arranging workspace '%s' at %f, %f", workspace->name, workspace->x, workspace->y); @@ -240,22 +228,22 @@ static void arrange_workspace(struct sway_container *workspace) { fs->y = workspace->parent->y; fs->width = workspace->parent->width; fs->height = workspace->parent->height; - if (fs->type == C_VIEW) { - view_autoconfigure(fs->sway_view); - } else { - arrange_children_of(fs); - } - container_set_dirty(fs); + arrange_container(fs); } else { + struct wlr_box box; + container_get_box(workspace, &box); + arrange_children(workspace->children, workspace->layout, &box); arrange_floating(workspace->sway_workspace->floating); - arrange_children_of(workspace); } } -static void arrange_output(struct sway_container *output) { +void arrange_output(struct sway_container *output) { if (config->reloading) { return; } + if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) { + return; + } const struct wlr_box *output_box = wlr_output_layout_get_box( root_container.sway_root->output_layout, output->sway_output->wlr_output); @@ -272,7 +260,7 @@ static void arrange_output(struct sway_container *output) { } } -static void arrange_root() { +void arrange_root(void) { if (config->reloading) { return; } @@ -303,52 +291,10 @@ void arrange_windows(struct sway_container *container) { arrange_workspace(container); break; case C_CONTAINER: - arrange_children_of(container); - container_set_dirty(container); - break; case C_VIEW: - view_autoconfigure(container->sway_view); - container_set_dirty(container); + arrange_container(container); break; case C_TYPES: break; } } - -void remove_gaps(struct sway_container *c) { - if (c->current_gaps == 0) { - wlr_log(WLR_DEBUG, "Removing gaps: not gapped: %p", c); - return; - } - - c->width += c->current_gaps * 2; - c->height += c->current_gaps * 2; - c->x -= c->current_gaps; - c->y -= c->current_gaps; - - c->current_gaps = 0; - - wlr_log(WLR_DEBUG, "Removing gaps %p", c); -} - -void add_gaps(struct sway_container *c) { - if (c->current_gaps > 0 || c->type == C_CONTAINER) { - wlr_log(WLR_DEBUG, "Not adding gaps: %p", c); - return; - } - - if (c->type == C_WORKSPACE && - !(config->edge_gaps || (config->smart_gaps && c->children->length > 1))) { - return; - } - - double gaps = c->has_gaps ? c->gaps_inner : config->gaps_inner; - - c->x += gaps; - c->y += gaps; - c->width -= 2 * gaps; - c->height -= 2 * gaps; - c->current_gaps = gaps; - - wlr_log(WLR_DEBUG, "Adding gaps: %p", c); -} diff --git a/sway/tree/container.c b/sway/tree/container.c index a8c6e667..ee019098 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -19,7 +19,6 @@ #include "sway/output.h" #include "sway/server.h" #include "sway/tree/arrange.h" -#include "sway/tree/layout.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" #include "log.h" @@ -43,14 +42,12 @@ const char *container_type_to_str(enum sway_container_type type) { } void container_create_notify(struct sway_container *container) { - // TODO send ipc event type based on the container type - wl_signal_emit(&root_container.sway_root->events.new_container, container); - if (container->type == C_VIEW) { ipc_event_window(container, "new"); } else if (container->type == C_WORKSPACE) { ipc_event_workspace(NULL, container, "init"); } + wl_signal_emit(&root_container.sway_root->events.new_container, container); } void container_update_textures_recursive(struct sway_container *con) { @@ -104,210 +101,53 @@ struct sway_container *container_create(enum sway_container_type type) { return c; } -static void container_workspace_free(struct sway_workspace *ws) { - list_foreach(ws->output_priority, free); - list_free(ws->output_priority); - list_free(ws->floating); - free(ws); -} - -void container_free(struct sway_container *cont) { - if (!sway_assert(cont->destroying, +void container_destroy(struct sway_container *con) { + if (!sway_assert(con->type == C_CONTAINER || con->type == C_VIEW, + "Expected a container or view")) { + return; + } + if (!sway_assert(con->destroying, "Tried to free container which wasn't marked as destroying")) { return; } - if (!sway_assert(cont->ntxnrefs == 0, "Tried to free container " + if (!sway_assert(con->ntxnrefs == 0, "Tried to free container " "which is still referenced by transactions")) { return; } - free(cont->name); - free(cont->formatted_title); - wlr_texture_destroy(cont->title_focused); - wlr_texture_destroy(cont->title_focused_inactive); - wlr_texture_destroy(cont->title_unfocused); - wlr_texture_destroy(cont->title_urgent); - list_free(cont->children); - list_free(cont->current.children); - list_free(cont->outputs); - - switch (cont->type) { - case C_ROOT: - break; - case C_OUTPUT: - break; - case C_WORKSPACE: - container_workspace_free(cont->sway_workspace); - break; - case C_CONTAINER: - break; - case C_VIEW: - { - struct sway_view *view = cont->sway_view; - view->swayc = NULL; - free(view->title_format); - view->title_format = NULL; - - if (view->destroying) { - view_free(view); - } - } - break; - case C_TYPES: - sway_assert(false, "Didn't expect to see C_TYPES here"); - break; - } - - free(cont); -} - -static struct sway_container *container_destroy_noreaping( - struct sway_container *con); - -static struct sway_container *container_workspace_destroy( - struct sway_container *workspace) { - if (!sway_assert(workspace, "cannot destroy null workspace")) { - return NULL; - } - - struct sway_container *output = container_parent(workspace, C_OUTPUT); - - // If we're destroying the output, it will be NULL here. Return the root so - // that it doesn't appear that the workspace has refused to be destoyed, - // which would leave it in a broken state with no parent. - if (output == NULL) { - return &root_container; - } - - // Do not destroy this if it's the last workspace on this output - if (output->children->length == 1) { - return NULL; - } - - wlr_log(WLR_DEBUG, "destroying workspace '%s'", workspace->name); - - if (!workspace_is_empty(workspace)) { - // Move children to a different workspace on this output - struct sway_container *new_workspace = NULL; - for (int i = 0; i < output->children->length; i++) { - if (output->children->items[i] != workspace) { - new_workspace = output->children->items[i]; - break; - } - } - - wlr_log(WLR_DEBUG, "moving children to different workspace '%s' -> '%s'", - workspace->name, new_workspace->name); - for (int i = 0; i < workspace->children->length; i++) { - container_move_to(workspace->children->items[i], new_workspace); - } - list_t *floating = workspace->sway_workspace->floating; - for (int i = 0; i < floating->length; i++) { - struct sway_container *floater = floating->items[i]; - container_remove_child(floater); - workspace_add_floating(new_workspace, floater); - } - } - - return output; -} - -static void untrack_output(struct sway_container *con, void *data) { - struct sway_output *output = data; - int index = list_find(con->outputs, output); - if (index != -1) { - list_del(con->outputs, index); - } -} - -static struct sway_container *container_output_destroy( - struct sway_container *output) { - if (!sway_assert(output, "cannot destroy null output")) { - return NULL; - } - - if (output->children->length > 0) { - // TODO save workspaces when there are no outputs. - // TODO also check if there will ever be no outputs except for exiting - // program - if (root_container.children->length > 1) { - // Move workspace from this output to another output - struct sway_container *fallback_output = - root_container.children->items[0]; - if (fallback_output == output) { - fallback_output = root_container.children->items[1]; - } - - while (output->children->length) { - struct sway_container *workspace = output->children->items[0]; - - struct sway_container *new_output = - workspace_output_get_highest_available(workspace, output); - if (!new_output) { - new_output = fallback_output; - workspace_output_add_priority(workspace, new_output); - } + free(con->name); + free(con->formatted_title); + wlr_texture_destroy(con->title_focused); + wlr_texture_destroy(con->title_focused_inactive); + wlr_texture_destroy(con->title_unfocused); + wlr_texture_destroy(con->title_urgent); + list_free(con->children); + list_free(con->current.children); + list_free(con->outputs); - container_remove_child(workspace); - if (!workspace_is_empty(workspace)) { - container_add_child(new_output, workspace); - ipc_event_workspace(NULL, workspace, "move"); - } else { - container_destroy(workspace); - } + if (con->type == C_VIEW) { + struct sway_view *view = con->sway_view; + view->swayc = NULL; + free(view->title_format); + view->title_format = NULL; - output_sort_workspaces(new_output); - } + if (view->destroying) { + view_destroy(view); } } - root_for_each_container(untrack_output, output->sway_output); - - wl_list_remove(&output->sway_output->mode.link); - wl_list_remove(&output->sway_output->transform.link); - wl_list_remove(&output->sway_output->scale.link); - - wl_list_remove(&output->sway_output->damage_destroy.link); - wl_list_remove(&output->sway_output->damage_frame.link); - - output->sway_output->swayc = NULL; - output->sway_output = NULL; - - wlr_log(WLR_DEBUG, "OUTPUT: Destroying output '%s'", output->name); - - return &root_container; + free(con); } -/** - * Implement the actual destroy logic, without reaping. - */ -static struct sway_container *container_destroy_noreaping( - struct sway_container *con) { - if (con == NULL) { - return NULL; - } - if (con->destroying) { - return NULL; +void container_begin_destroy(struct sway_container *con) { + if (!sway_assert(con->type == C_CONTAINER || con->type == C_VIEW, + "Expected a container or view")) { + return; } - wl_signal_emit(&con->events.destroy, con); - - // emit IPC event if (con->type == C_VIEW) { ipc_event_window(con, "close"); - } else if (con->type == C_WORKSPACE) { - ipc_event_workspace(NULL, con, "empty"); - } - - // The below functions move their children to somewhere else. - if (con->type == C_OUTPUT) { - container_output_destroy(con); - } else if (con->type == C_WORKSPACE) { - // Workspaces will refuse to be destroyed if they're the last workspace - // on their output. - if (!container_workspace_destroy(con)) { - return NULL; - } } + wl_signal_emit(&con->events.destroy, con); container_end_mouse_operation(con); @@ -318,51 +158,22 @@ static struct sway_container *container_destroy_noreaping( root_scratchpad_remove_container(con); } - if (!con->parent) { - return NULL; - } - - return container_remove_child(con); -} - -bool container_reap_empty(struct sway_container *con) { - switch (con->type) { - case C_ROOT: - case C_OUTPUT: - // dont reap these - break; - case C_WORKSPACE: - if (!workspace_is_visible(con) && workspace_is_empty(con)) { - wlr_log(WLR_DEBUG, "Destroying workspace via reaper"); - container_destroy_noreaping(con); - return true; - } - break; - case C_CONTAINER: - if (con->children->length == 0) { - container_destroy_noreaping(con); - return true; - } - case C_VIEW: - break; - case C_TYPES: - sway_assert(false, "container_reap_empty called on an invalid " - "container"); - break; + if (con->parent) { + container_remove_child(con); } - - return false; } -struct sway_container *container_reap_empty_recursive( - struct sway_container *con) { - while (con) { +struct sway_container *container_reap_empty(struct sway_container *con) { + while (con && con->type == C_CONTAINER) { struct sway_container *next = con->parent; - if (!container_reap_empty(con)) { - break; + if (con->children->length == 0) { + container_begin_destroy(con); } con = next; } + if (con && con->type == C_WORKSPACE) { + workspace_consider_destroy(con); + } return con; } @@ -371,34 +182,12 @@ struct sway_container *container_flatten(struct sway_container *container) { struct sway_container *child = container->children->items[0]; struct sway_container *parent = container->parent; container_replace_child(container, child); - container_destroy_noreaping(container); + container_begin_destroy(container); container = parent; } return container; } -/** - * container_destroy() is the first step in destroying a container. We'll emit - * events, detach it from the tree and mark it as destroying. The container will - * remain in memory until it's no longer used by a transaction, then it will be - * freed via container_free(). - * - * This function just wraps container_destroy_noreaping(), then does reaping. - */ -struct sway_container *container_destroy(struct sway_container *con) { - if (con->is_fullscreen) { - struct sway_container *ws = container_parent(con, C_WORKSPACE); - ws->sway_workspace->fullscreen = NULL; - } - struct sway_container *parent = container_destroy_noreaping(con); - - if (!parent) { - return NULL; - } - - return container_reap_empty_recursive(parent); -} - static void container_close_func(struct sway_container *container, void *data) { if (container->type == C_VIEW) { view_close(container->sway_view); @@ -687,10 +476,11 @@ struct sway_container *container_at(struct sway_container *workspace, return NULL; } struct sway_container *c; - // Focused view's popups struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *focus = seat_get_focus_inactive(seat, &root_container); + bool is_floating = focus && container_is_floating_or_child(focus); + // Focused view's popups if (focus && focus->type == C_VIEW) { surface_at_view(focus, lx, ly, surface, sx, sy); if (*surface && surface_is_popup(*surface)) { @@ -698,11 +488,27 @@ struct sway_container *container_at(struct sway_container *workspace, } *surface = NULL; } - // Floating + // If focused is floating, focused view's non-popups + if (focus && focus->type == C_VIEW && is_floating) { + surface_at_view(focus, lx, ly, surface, sx, sy); + if (*surface) { + return focus; + } + *surface = NULL; + } + // Floating (non-focused) if ((c = floating_container_at(lx, ly, surface, sx, sy))) { return c; } - // Tiling + // If focused is tiling, focused view's non-popups + if (focus && focus->type == C_VIEW && !is_floating) { + surface_at_view(focus, lx, ly, surface, sx, sy); + if (*surface) { + return focus; + } + *surface = NULL; + } + // Tiling (non-focused) if ((c = tiling_container_at(workspace, lx, ly, surface, sx, sy))) { return c; } @@ -1331,3 +1137,267 @@ void container_discover_outputs(struct sway_container *con) { } } } + +void container_remove_gaps(struct sway_container *c) { + if (!sway_assert(c->type == C_CONTAINER || c->type == C_VIEW, + "Expected a container or view")) { + return; + } + if (c->current_gaps == 0) { + return; + } + + c->width += c->current_gaps * 2; + c->height += c->current_gaps * 2; + c->x -= c->current_gaps; + c->y -= c->current_gaps; + c->current_gaps = 0; +} + +void container_add_gaps(struct sway_container *c) { + if (!sway_assert(c->type == C_CONTAINER || c->type == C_VIEW, + "Expected a container or view")) { + return; + } + if (c->current_gaps > 0) { + return; + } + // Linear containers don't have gaps because it'd create double gaps + if (c->type == C_CONTAINER && + c->layout != L_TABBED && c->layout != L_STACKED) { + return; + } + // Children of tabbed/stacked containers re-use the gaps of the container + enum sway_container_layout layout = c->parent->layout; + if (layout == L_TABBED || layout == L_STACKED) { + return; + } + + struct sway_container *ws = container_parent(c, C_WORKSPACE); + + c->current_gaps = ws->has_gaps ? ws->gaps_inner : config->gaps_inner; + c->x += c->current_gaps; + c->y += c->current_gaps; + c->width -= 2 * c->current_gaps; + c->height -= 2 * c->current_gaps; +} + +int container_sibling_index(const struct sway_container *child) { + return list_find(child->parent->children, child); +} + +void container_handle_fullscreen_reparent(struct sway_container *con, + struct sway_container *old_parent) { + if (!con->is_fullscreen) { + return; + } + struct sway_container *old_workspace = old_parent; + if (old_workspace && old_workspace->type != C_WORKSPACE) { + old_workspace = container_parent(old_workspace, C_WORKSPACE); + } + struct sway_container *new_workspace = container_parent(con, C_WORKSPACE); + if (old_workspace == new_workspace) { + return; + } + // Unmark the old workspace as fullscreen + if (old_workspace) { + old_workspace->sway_workspace->fullscreen = NULL; + } + + // Mark the new workspace as fullscreen + if (new_workspace->sway_workspace->fullscreen) { + container_set_fullscreen( + new_workspace->sway_workspace->fullscreen, false); + } + new_workspace->sway_workspace->fullscreen = con; + + // Resize container to new output dimensions + struct sway_container *output = new_workspace->parent; + con->x = output->x; + con->y = output->y; + con->width = output->width; + con->height = output->height; + + if (con->type == C_VIEW) { + struct sway_view *view = con->sway_view; + view->x = output->x; + view->y = output->y; + view->width = output->width; + view->height = output->height; + } else { + arrange_windows(new_workspace); + } +} + +void container_insert_child(struct sway_container *parent, + struct sway_container *child, int i) { + struct sway_container *old_parent = child->parent; + if (old_parent) { + container_remove_child(child); + } + wlr_log(WLR_DEBUG, "Inserting id:%zd at index %d", child->id, i); + list_insert(parent->children, i, child); + child->parent = parent; + container_handle_fullscreen_reparent(child, old_parent); +} + +struct sway_container *container_add_sibling(struct sway_container *fixed, + struct sway_container *active) { + // TODO handle floating + struct sway_container *old_parent = NULL; + if (active->parent) { + old_parent = active->parent; + container_remove_child(active); + } + struct sway_container *parent = fixed->parent; + int i = container_sibling_index(fixed); + list_insert(parent->children, i + 1, active); + active->parent = parent; + container_handle_fullscreen_reparent(active, old_parent); + return active->parent; +} + +void container_add_child(struct sway_container *parent, + struct sway_container *child) { + wlr_log(WLR_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", + child, child->type, child->width, child->height, + parent, parent->type, parent->width, parent->height); + struct sway_container *old_parent = child->parent; + list_add(parent->children, child); + child->parent = parent; + container_handle_fullscreen_reparent(child, old_parent); + if (old_parent) { + container_set_dirty(old_parent); + } + container_set_dirty(child); +} + +struct sway_container *container_remove_child(struct sway_container *child) { + if (child->is_fullscreen) { + struct sway_container *workspace = container_parent(child, C_WORKSPACE); + workspace->sway_workspace->fullscreen = NULL; + } + + struct sway_container *parent = child->parent; + list_t *list = container_is_floating(child) ? + parent->sway_workspace->floating : parent->children; + int index = list_find(list, child); + if (index != -1) { + list_del(list, index); + } + child->parent = NULL; + container_notify_subtree_changed(parent); + + container_set_dirty(parent); + container_set_dirty(child); + + return parent; +} + +enum sway_container_layout container_get_default_layout( + struct sway_container *con) { + if (con->type != C_OUTPUT) { + con = container_parent(con, C_OUTPUT); + } + + if (!sway_assert(con != NULL, + "container_get_default_layout must be called on an attached" + " container below the root container")) { + return 0; + } + + if (config->default_layout != L_NONE) { + return config->default_layout; + } else if (config->default_orientation != L_NONE) { + return config->default_orientation; + } else if (con->width >= con->height) { + return L_HORIZ; + } else { + return L_VERT; + } +} + +struct sway_container *container_replace_child(struct sway_container *child, + struct sway_container *new_child) { + struct sway_container *parent = child->parent; + if (parent == NULL) { + return NULL; + } + + list_t *list = container_is_floating(child) ? + parent->sway_workspace->floating : parent->children; + int i = list_find(list, child); + + if (new_child->parent) { + container_remove_child(new_child); + } + list->items[i] = new_child; + new_child->parent = parent; + child->parent = NULL; + + // Set geometry for new child + new_child->x = child->x; + new_child->y = child->y; + new_child->width = child->width; + new_child->height = child->height; + + // reset geometry for child + child->width = 0; + child->height = 0; + + return parent; +} + +struct sway_container *container_split(struct sway_container *child, + enum sway_container_layout layout) { + // TODO floating: cannot split a floating container + if (!sway_assert(child, "child cannot be null")) { + return NULL; + } + if (child->type == C_WORKSPACE && child->children->length == 0) { + // Special case: this just behaves like splitt + child->prev_split_layout = child->layout; + child->layout = layout; + return child; + } + + struct sway_container *cont = container_create(C_CONTAINER); + + wlr_log(WLR_DEBUG, "creating container %p around %p", cont, child); + + cont->prev_split_layout = L_NONE; + cont->width = child->width; + cont->height = child->height; + cont->x = child->x; + cont->y = child->y; + cont->current_gaps = child->current_gaps; + + struct sway_seat *seat = input_manager_get_default_seat(input_manager); + bool set_focus = (seat_get_focus(seat) == child); + + if (child->type == C_WORKSPACE) { + struct sway_container *workspace = child; + while (workspace->children->length) { + struct sway_container *ws_child = workspace->children->items[0]; + container_remove_child(ws_child); + container_add_child(cont, ws_child); + } + + container_add_child(workspace, cont); + enum sway_container_layout old_layout = workspace->layout; + workspace->layout = layout; + cont->layout = old_layout; + } else { + cont->layout = layout; + container_replace_child(child, cont); + container_add_child(cont, child); + } + + if (set_focus) { + seat_set_focus(seat, cont); + seat_set_focus(seat, child); + } + + container_notify_subtree_changed(cont); + return cont; +} diff --git a/sway/tree/layout.c b/sway/tree/layout.c deleted file mode 100644 index a3de44ce..00000000 --- a/sway/tree/layout.c +++ /dev/null @@ -1,1019 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include <math.h> -#include <stdbool.h> -#include <stdlib.h> -#include <string.h> -#include <wlr/types/wlr_output.h> -#include <wlr/types/wlr_output_layout.h> -#include "config.h" -#include "sway/debug.h" -#include "sway/tree/arrange.h" -#include "sway/tree/container.h" -#include "sway/tree/layout.h" -#include "sway/output.h" -#include "sway/tree/workspace.h" -#include "sway/tree/view.h" -#include "sway/input/seat.h" -#include "sway/ipc-server.h" -#include "list.h" -#include "log.h" - -static int index_child(const struct sway_container *child) { - return list_find(child->parent->children, child); -} - -static void container_handle_fullscreen_reparent(struct sway_container *con, - struct sway_container *old_parent) { - if (!con->is_fullscreen) { - return; - } - struct sway_container *old_workspace = old_parent; - if (old_workspace && old_workspace->type != C_WORKSPACE) { - old_workspace = container_parent(old_workspace, C_WORKSPACE); - } - struct sway_container *new_workspace = container_parent(con, C_WORKSPACE); - if (old_workspace == new_workspace) { - return; - } - // Unmark the old workspace as fullscreen - if (old_workspace) { - old_workspace->sway_workspace->fullscreen = NULL; - } - - // Mark the new workspace as fullscreen - if (new_workspace->sway_workspace->fullscreen) { - container_set_fullscreen( - new_workspace->sway_workspace->fullscreen, false); - } - new_workspace->sway_workspace->fullscreen = con; - - // Resize container to new output dimensions - struct sway_container *output = new_workspace->parent; - con->x = output->x; - con->y = output->y; - con->width = output->width; - con->height = output->height; - - if (con->type == C_VIEW) { - struct sway_view *view = con->sway_view; - view->x = output->x; - view->y = output->y; - view->width = output->width; - view->height = output->height; - } else { - arrange_windows(new_workspace); - } -} - -void container_insert_child(struct sway_container *parent, - struct sway_container *child, int i) { - struct sway_container *old_parent = child->parent; - if (old_parent) { - container_remove_child(child); - } - wlr_log(WLR_DEBUG, "Inserting id:%zd at index %d", child->id, i); - list_insert(parent->children, i, child); - child->parent = parent; - container_handle_fullscreen_reparent(child, old_parent); -} - -struct sway_container *container_add_sibling(struct sway_container *fixed, - struct sway_container *active) { - // TODO handle floating - struct sway_container *old_parent = NULL; - if (active->parent) { - old_parent = active->parent; - container_remove_child(active); - } - struct sway_container *parent = fixed->parent; - int i = index_child(fixed); - list_insert(parent->children, i + 1, active); - active->parent = parent; - container_handle_fullscreen_reparent(active, old_parent); - return active->parent; -} - -void container_add_child(struct sway_container *parent, - struct sway_container *child) { - wlr_log(WLR_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", - child, child->type, child->width, child->height, - parent, parent->type, parent->width, parent->height); - struct sway_container *old_parent = child->parent; - list_add(parent->children, child); - child->parent = parent; - container_handle_fullscreen_reparent(child, old_parent); - if (old_parent) { - container_set_dirty(old_parent); - } - container_set_dirty(child); -} - -struct sway_container *container_remove_child(struct sway_container *child) { - if (child->is_fullscreen) { - struct sway_container *workspace = container_parent(child, C_WORKSPACE); - workspace->sway_workspace->fullscreen = NULL; - } - - struct sway_container *parent = child->parent; - list_t *list = container_is_floating(child) ? - parent->sway_workspace->floating : parent->children; - int index = list_find(list, child); - if (index != -1) { - list_del(list, index); - } - child->parent = NULL; - container_notify_subtree_changed(parent); - - container_set_dirty(parent); - container_set_dirty(child); - - return parent; -} - -void container_move_to(struct sway_container *container, - struct sway_container *destination) { - if (!sway_assert(container->type == C_CONTAINER || - container->type == C_VIEW, "Expected a container or view")) { - return; - } - if (container == destination - || container_has_ancestor(container, destination)) { - return; - } - struct sway_container *old_parent = NULL; - struct sway_container *new_parent = NULL; - if (container_is_floating(container)) { - // Resolve destination into a workspace - struct sway_container *new_ws = NULL; - if (destination->type == C_OUTPUT) { - new_ws = output_get_active_workspace(destination->sway_output); - } else if (destination->type == C_WORKSPACE) { - new_ws = destination; - } else { - new_ws = container_parent(destination, C_WORKSPACE); - } - if (!new_ws) { - // This can happen if the user has run "move container to mark foo", - // where mark foo is on a hidden scratchpad container. - return; - } - struct sway_container *old_output = - container_parent(container, C_OUTPUT); - old_parent = container_remove_child(container); - workspace_add_floating(new_ws, container); - container_handle_fullscreen_reparent(container, old_parent); - // If changing output, center it within the workspace - if (old_output != new_ws->parent && !container->is_fullscreen) { - container_floating_move_to_center(container); - } - } else { - old_parent = container_remove_child(container); - container->width = container->height = 0; - container->saved_width = container->saved_height = 0; - - if (destination->type == C_VIEW) { - new_parent = container_add_sibling(destination, container); - } else { - new_parent = destination; - container_add_child(destination, container); - } - } - - if (container->type == C_VIEW) { - ipc_event_window(container, "move"); - } - container_notify_subtree_changed(old_parent); - container_notify_subtree_changed(new_parent); - - // If view was moved to a fullscreen workspace, refocus the fullscreen view - struct sway_container *new_workspace = container; - if (new_workspace->type != C_WORKSPACE) { - new_workspace = container_parent(new_workspace, C_WORKSPACE); - } - if (new_workspace->sway_workspace->fullscreen) { - struct sway_seat *seat; - struct sway_container *focus, *focus_ws; - wl_list_for_each(seat, &input_manager->seats, link) { - focus = seat_get_focus(seat); - focus_ws = focus; - if (focus_ws->type != C_WORKSPACE) { - focus_ws = container_parent(focus_ws, C_WORKSPACE); - } - if (focus_ws == new_workspace) { - struct sway_container *new_focus = seat_get_focus_inactive(seat, - new_workspace->sway_workspace->fullscreen); - seat_set_focus(seat, new_focus); - } - } - } - // Update workspace urgent state - struct sway_container *old_workspace = old_parent; - if (old_workspace->type != C_WORKSPACE) { - old_workspace = container_parent(old_workspace, C_WORKSPACE); - } - if (new_workspace != old_workspace) { - workspace_detect_urgent(new_workspace); - if (old_workspace) { - workspace_detect_urgent(old_workspace); - } - } -} - -static bool sway_dir_to_wlr(enum movement_direction dir, - enum wlr_direction *out) { - switch (dir) { - case MOVE_UP: - *out = WLR_DIRECTION_UP; - break; - case MOVE_DOWN: - *out = WLR_DIRECTION_DOWN; - break; - case MOVE_LEFT: - *out = WLR_DIRECTION_LEFT; - break; - case MOVE_RIGHT: - *out = WLR_DIRECTION_RIGHT; - break; - default: - return false; - } - - return true; -} - -static bool is_parallel(enum sway_container_layout layout, - enum movement_direction dir) { - switch (layout) { - case L_TABBED: - case L_HORIZ: - return dir == MOVE_LEFT || dir == MOVE_RIGHT; - case L_STACKED: - case L_VERT: - return dir == MOVE_UP || dir == MOVE_DOWN; - default: - return false; - } -} - -static enum movement_direction invert_movement(enum movement_direction dir) { - switch (dir) { - case MOVE_LEFT: - return MOVE_RIGHT; - case MOVE_RIGHT: - return MOVE_LEFT; - case MOVE_UP: - return MOVE_DOWN; - case MOVE_DOWN: - return MOVE_UP; - default: - sway_assert(0, "This function expects left|right|up|down"); - return MOVE_LEFT; - } -} - -static int move_offs(enum movement_direction move_dir) { - return move_dir == MOVE_LEFT || move_dir == MOVE_UP ? -1 : 1; -} - -/* Gets the index of the most extreme member based on the movement offset */ -static int container_limit(struct sway_container *container, - enum movement_direction move_dir) { - return move_offs(move_dir) < 0 ? 0 : container->children->length; -} - -/* Takes one child, sets it aside, wraps the rest of the children in a new - * container, switches the layout of the workspace, and drops the child back in. - * In other words, rejigger it. */ -static void workspace_rejigger(struct sway_container *ws, - struct sway_container *child, enum movement_direction move_dir) { - struct sway_container *original_parent = child->parent; - struct sway_container *new_parent = - container_split(ws, ws->layout); - - container_remove_child(child); - for (int i = 0; i < ws->children->length; ++i) { - struct sway_container *_child = ws->children->items[i]; - container_move_to(new_parent, _child); - } - - int index = move_offs(move_dir); - container_insert_child(ws, child, index < 0 ? 0 : 1); - ws->layout = - move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT; - - container_flatten(ws); - container_reap_empty_recursive(original_parent); - container_create_notify(new_parent); -} - -static void move_out_of_tabs_stacks(struct sway_container *container, - struct sway_container *current, enum movement_direction move_dir, - int offs) { - if (container->parent == current->parent - && current->parent->children->length == 1) { - wlr_log(WLR_DEBUG, "Changing layout of %zd", current->parent->id); - current->parent->layout = move_dir == - MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT; - return; - } - - wlr_log(WLR_DEBUG, "Moving out of tab/stack into a split"); - bool is_workspace = current->parent->type == C_WORKSPACE; - struct sway_container *new_parent = container_split(current->parent, - move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT); - if (is_workspace) { - container_insert_child(new_parent->parent, container, offs < 0 ? 0 : 1); - } else { - container_insert_child(new_parent, container, offs < 0 ? 0 : 1); - container_reap_empty_recursive(new_parent->parent); - container_flatten(new_parent->parent); - } - container_create_notify(new_parent); - container_notify_subtree_changed(new_parent); -} - -void container_move(struct sway_container *container, - enum movement_direction move_dir, int move_amt) { - if (!sway_assert( - container->type != C_CONTAINER || container->type != C_VIEW, - "Can only move containers and views")) { - return; - } - int offs = move_offs(move_dir); - - struct sway_container *sibling = NULL; - struct sway_container *current = container; - struct sway_container *parent = current->parent; - struct sway_container *top = &root_container; - - // If moving a fullscreen view, only consider outputs - if (container->is_fullscreen) { - current = container_parent(container, C_OUTPUT); - } else if (container_is_fullscreen_or_child(container) || - container_is_floating_or_child(container)) { - // If we've fullscreened a split container, only allow the child to move - // around within the fullscreen parent. - // Same with floating a split container. - struct sway_container *ws = container_parent(container, C_WORKSPACE); - top = ws->sway_workspace->fullscreen; - } - - struct sway_container *new_parent = container_flatten(parent); - if (new_parent != parent) { - // Special case: we were the last one in this container, so leave - return; - } - - while (!sibling) { - if (current == top) { - return; - } - - parent = current->parent; - wlr_log(WLR_DEBUG, "Visiting %p %s '%s'", current, - container_type_to_str(current->type), current->name); - - int index = index_child(current); - - switch (current->type) { - case C_OUTPUT: { - enum wlr_direction wlr_dir = 0; - if (!sway_assert(sway_dir_to_wlr(move_dir, &wlr_dir), - "got invalid direction: %d", move_dir)) { - return; - } - double ref_lx = current->x + current->width / 2; - double ref_ly = current->y + current->height / 2; - struct wlr_output *next = wlr_output_layout_adjacent_output( - root_container.sway_root->output_layout, wlr_dir, - current->sway_output->wlr_output, ref_lx, ref_ly); - if (!next) { - wlr_log(WLR_DEBUG, "Hit edge of output, nowhere else to go"); - return; - } - struct sway_output *next_output = next->data; - current = next_output->swayc; - wlr_log(WLR_DEBUG, "Selected next output (%s)", current->name); - // Select workspace and get outta here - current = seat_get_focus_inactive( - config->handler_context.seat, current); - if (current->type != C_WORKSPACE) { - current = container_parent(current, C_WORKSPACE); - } - sibling = current; - break; - } - case C_WORKSPACE: - if (!is_parallel(current->layout, move_dir)) { - if (current->children->length >= 2) { - wlr_log(WLR_DEBUG, "Rejiggering the workspace (%d kiddos)", - current->children->length); - workspace_rejigger(current, container, move_dir); - return; - } else { - wlr_log(WLR_DEBUG, "Selecting output"); - current = current->parent; - } - } else if (current->layout == L_TABBED - || current->layout == L_STACKED) { - wlr_log(WLR_DEBUG, "Rejiggering out of tabs/stacks"); - workspace_rejigger(current, container, move_dir); - } else { - wlr_log(WLR_DEBUG, "Selecting output"); - current = current->parent; - } - break; - case C_CONTAINER: - case C_VIEW: - if (is_parallel(parent->layout, move_dir)) { - if ((index == parent->children->length - 1 && offs > 0) - || (index == 0 && offs < 0)) { - if (current->parent == container->parent) { - if (!parent->is_fullscreen && - (parent->layout == L_TABBED || - parent->layout == L_STACKED)) { - move_out_of_tabs_stacks(container, current, - move_dir, offs); - return; - } else { - wlr_log(WLR_DEBUG, "Hit limit, selecting parent"); - current = current->parent; - } - } else { - wlr_log(WLR_DEBUG, "Hit limit, " - "promoting descendant to sibling"); - // Special case - container_insert_child(current->parent, container, - index + (offs < 0 ? 0 : 1)); - container->width = container->height = 0; - return; - } - } else { - sibling = parent->children->items[index + offs]; - wlr_log(WLR_DEBUG, "Selecting sibling id:%zd", sibling->id); - } - } else if (!parent->is_fullscreen && (parent->layout == L_TABBED || - parent->layout == L_STACKED)) { - move_out_of_tabs_stacks(container, current, move_dir, offs); - return; - } else { - wlr_log(WLR_DEBUG, "Moving up to find a parallel container"); - current = current->parent; - } - break; - default: - sway_assert(0, "Not expecting to see container of type %s here", - container_type_to_str(current->type)); - return; - } - } - - // Part two: move stuff around - int index = index_child(container); - struct sway_container *old_parent = container->parent; - - while (sibling) { - switch (sibling->type) { - case C_VIEW: - if (sibling->parent == container->parent) { - wlr_log(WLR_DEBUG, "Swapping siblings"); - sibling->parent->children->items[index + offs] = container; - sibling->parent->children->items[index] = sibling; - } else { - wlr_log(WLR_DEBUG, "Promoting to sibling of cousin"); - container_insert_child(sibling->parent, container, - index_child(sibling) + (offs > 0 ? 0 : 1)); - container->width = container->height = 0; - } - sibling = NULL; - break; - case C_WORKSPACE: // Note: only in the case of moving between outputs - case C_CONTAINER: - if (is_parallel(sibling->layout, move_dir)) { - int limit = container_limit(sibling, invert_movement(move_dir)); - wlr_log(WLR_DEBUG, "limit: %d", limit); - wlr_log(WLR_DEBUG, - "Reparenting container (parallel) to index %d " - "(move dir: %d)", limit, move_dir); - container_insert_child(sibling, container, limit); - container->width = container->height = 0; - sibling = NULL; - } else { - wlr_log(WLR_DEBUG, "Reparenting container (perpendicular)"); - struct sway_container *focus_inactive = seat_get_focus_inactive( - config->handler_context.seat, sibling); - if (focus_inactive && focus_inactive != sibling) { - while (focus_inactive->parent != sibling) { - focus_inactive = focus_inactive->parent; - } - wlr_log(WLR_DEBUG, "Focus inactive: id:%zd", - focus_inactive->id); - sibling = focus_inactive; - continue; - } else if (sibling->children->length) { - wlr_log(WLR_DEBUG, "No focus-inactive, adding arbitrarily"); - container_remove_child(container); - container_add_sibling(sibling->children->items[0], container); - } else { - wlr_log(WLR_DEBUG, "No kiddos, adding container alone"); - container_remove_child(container); - container_add_child(sibling, container); - } - container->width = container->height = 0; - sibling = NULL; - } - break; - default: - sway_assert(0, "Not expecting to see container of type %s here", - container_type_to_str(sibling->type)); - return; - } - } - - container_notify_subtree_changed(old_parent); - container_notify_subtree_changed(container->parent); - - if (container->type == C_VIEW) { - ipc_event_window(container, "move"); - } - - if (old_parent) { - seat_set_focus(config->handler_context.seat, old_parent); - seat_set_focus(config->handler_context.seat, container); - } - - struct sway_container *last_ws = old_parent; - struct sway_container *next_ws = container->parent; - if (last_ws && last_ws->type != C_WORKSPACE) { - last_ws = container_parent(last_ws, C_WORKSPACE); - } - if (next_ws && next_ws->type != C_WORKSPACE) { - next_ws = container_parent(next_ws, C_WORKSPACE); - } - if (last_ws && next_ws && last_ws != next_ws) { - ipc_event_workspace(last_ws, next_ws, "focus"); - workspace_detect_urgent(last_ws); - workspace_detect_urgent(next_ws); - } - container_end_mouse_operation(container); -} - -enum sway_container_layout container_get_default_layout( - struct sway_container *con) { - if (con->type != C_OUTPUT) { - con = container_parent(con, C_OUTPUT); - } - - if (!sway_assert(con != NULL, - "container_get_default_layout must be called on an attached" - " container below the root container")) { - return 0; - } - - if (config->default_layout != L_NONE) { - return config->default_layout; - } else if (config->default_orientation != L_NONE) { - return config->default_orientation; - } else if (con->width >= con->height) { - return L_HORIZ; - } else { - return L_VERT; - } -} - -/** - * Get swayc in the direction of newly entered output. - */ -static struct sway_container *get_swayc_in_output_direction( - struct sway_container *output, enum movement_direction dir, - struct sway_seat *seat) { - if (!output) { - return NULL; - } - - struct sway_container *ws = seat_get_focus_inactive(seat, output); - if (ws->type != C_WORKSPACE) { - ws = container_parent(ws, C_WORKSPACE); - } - - if (ws == NULL) { - wlr_log(WLR_ERROR, "got an output without a workspace"); - return NULL; - } - - if (ws->children->length > 0) { - switch (dir) { - case MOVE_LEFT: - if (ws->layout == L_HORIZ || ws->layout == L_TABBED) { - // get most right child of new output - return ws->children->items[ws->children->length-1]; - } else { - return seat_get_focus_inactive(seat, ws); - } - case MOVE_RIGHT: - if (ws->layout == L_HORIZ || ws->layout == L_TABBED) { - // get most left child of new output - return ws->children->items[0]; - } else { - return seat_get_focus_inactive(seat, ws); - } - case MOVE_UP: - case MOVE_DOWN: { - struct sway_container *focused = - seat_get_focus_inactive(seat, ws); - if (focused && focused->parent) { - struct sway_container *parent = focused->parent; - if (parent->layout == L_VERT) { - if (dir == MOVE_UP) { - // get child furthest down on new output - int idx = parent->children->length - 1; - return parent->children->items[idx]; - } else if (dir == MOVE_DOWN) { - // get child furthest up on new output - return parent->children->items[0]; - } - } - return focused; - } - break; - } - default: - break; - } - } - - return ws; -} - -static struct sway_container *sway_output_from_wlr(struct wlr_output *output) { - if (output == NULL) { - return NULL; - } - for (int i = 0; i < root_container.children->length; ++i) { - struct sway_container *o = root_container.children->items[i]; - if (o->type == C_OUTPUT && o->sway_output->wlr_output == output) { - return o; - } - } - return NULL; -} - -struct sway_container *container_get_in_direction( - struct sway_container *container, struct sway_seat *seat, - enum movement_direction dir) { - struct sway_container *parent = container->parent; - - if (dir == MOVE_CHILD) { - return seat_get_focus_inactive(seat, container); - } - if (container->is_fullscreen) { - if (dir == MOVE_PARENT) { - return NULL; - } - container = container_parent(container, C_OUTPUT); - parent = container->parent; - } else { - if (dir == MOVE_PARENT) { - if (parent->type == C_OUTPUT || container_is_floating(container)) { - return NULL; - } else { - return parent; - } - } - } - - struct sway_container *wrap_candidate = NULL; - while (true) { - bool can_move = false; - int desired; - int idx = index_child(container); - if (idx == -1) { - return NULL; - } - if (parent->type == C_ROOT) { - enum wlr_direction wlr_dir = 0; - if (!sway_assert(sway_dir_to_wlr(dir, &wlr_dir), - "got invalid direction: %d", dir)) { - return NULL; - } - int lx = container->x + container->width / 2; - int ly = container->y + container->height / 2; - struct wlr_output_layout *layout = - root_container.sway_root->output_layout; - struct wlr_output *wlr_adjacent = - wlr_output_layout_adjacent_output(layout, wlr_dir, - container->sway_output->wlr_output, lx, ly); - struct sway_container *adjacent = - sway_output_from_wlr(wlr_adjacent); - - if (!adjacent || adjacent == container) { - if (!wrap_candidate) { - return NULL; - } - return seat_get_focus_inactive_view(seat, wrap_candidate); - } - struct sway_container *next = - get_swayc_in_output_direction(adjacent, dir, seat); - if (next == NULL) { - return NULL; - } - struct sway_container *next_workspace = next; - if (next_workspace->type != C_WORKSPACE) { - next_workspace = container_parent(next_workspace, C_WORKSPACE); - } - sway_assert(next_workspace, "Next container has no workspace"); - if (next_workspace->sway_workspace->fullscreen) { - return seat_get_focus_inactive(seat, - next_workspace->sway_workspace->fullscreen); - } - if (next->children && next->children->length) { - // TODO consider floating children as well - return seat_get_focus_inactive_view(seat, next); - } else { - return next; - } - } else { - if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { - if (parent->layout == L_HORIZ || parent->layout == L_TABBED) { - can_move = true; - desired = idx + (dir == MOVE_LEFT ? -1 : 1); - } - } else { - if (parent->layout == L_VERT || parent->layout == L_STACKED) { - can_move = true; - desired = idx + (dir == MOVE_UP ? -1 : 1); - } - } - } - - if (can_move) { - // TODO handle floating - if (desired < 0 || desired >= parent->children->length) { - can_move = false; - int len = parent->children->length; - if (config->focus_wrapping != WRAP_NO && !wrap_candidate - && len > 1) { - if (desired < 0) { - wrap_candidate = parent->children->items[len-1]; - } else { - wrap_candidate = parent->children->items[0]; - } - if (config->focus_wrapping == WRAP_FORCE) { - return seat_get_focus_inactive_view(seat, - wrap_candidate); - } - } - } else { - struct sway_container *desired_con = - parent->children->items[desired]; - wlr_log(WLR_DEBUG, - "cont %d-%p dir %i sibling %d: %p", idx, - container, dir, desired, desired_con); - return seat_get_focus_inactive_view(seat, desired_con); - } - } - - if (!can_move) { - container = parent; - parent = parent->parent; - if (!parent) { - // wrapping is the last chance - if (!wrap_candidate) { - return NULL; - } - return seat_get_focus_inactive_view(seat, wrap_candidate); - } - } - } -} - -struct sway_container *container_replace_child(struct sway_container *child, - struct sway_container *new_child) { - struct sway_container *parent = child->parent; - if (parent == NULL) { - return NULL; - } - - list_t *list = container_is_floating(child) ? - parent->sway_workspace->floating : parent->children; - int i = list_find(list, child); - - if (new_child->parent) { - container_remove_child(new_child); - } - list->items[i] = new_child; - new_child->parent = parent; - child->parent = NULL; - - // Set geometry for new child - new_child->x = child->x; - new_child->y = child->y; - new_child->width = child->width; - new_child->height = child->height; - - // reset geometry for child - child->width = 0; - child->height = 0; - - return parent; -} - -struct sway_container *container_split(struct sway_container *child, - enum sway_container_layout layout) { - // TODO floating: cannot split a floating container - if (!sway_assert(child, "child cannot be null")) { - return NULL; - } - if (child->type == C_WORKSPACE && child->children->length == 0) { - // Special case: this just behaves like splitt - child->prev_split_layout = child->layout; - child->layout = layout; - return child; - } - - struct sway_container *cont = container_create(C_CONTAINER); - - wlr_log(WLR_DEBUG, "creating container %p around %p", cont, child); - - remove_gaps(child); - - cont->prev_split_layout = L_NONE; - cont->width = child->width; - cont->height = child->height; - cont->x = child->x; - cont->y = child->y; - - struct sway_seat *seat = input_manager_get_default_seat(input_manager); - bool set_focus = (seat_get_focus(seat) == child); - - add_gaps(cont); - - if (child->type == C_WORKSPACE) { - struct sway_container *workspace = child; - while (workspace->children->length) { - struct sway_container *ws_child = workspace->children->items[0]; - container_remove_child(ws_child); - container_add_child(cont, ws_child); - } - - container_add_child(workspace, cont); - enum sway_container_layout old_layout = workspace->layout; - workspace->layout = layout; - cont->layout = old_layout; - } else { - cont->layout = layout; - container_replace_child(child, cont); - container_add_child(cont, child); - } - - if (set_focus) { - seat_set_focus(seat, cont); - seat_set_focus(seat, child); - } - - container_notify_subtree_changed(cont); - return cont; -} - -void container_recursive_resize(struct sway_container *container, - double amount, enum wlr_edges edge) { - bool layout_match = true; - wlr_log(WLR_DEBUG, "Resizing %p with amount: %f", container, amount); - if (edge == WLR_EDGE_LEFT || edge == WLR_EDGE_RIGHT) { - container->width += amount; - layout_match = container->layout == L_HORIZ; - } else if (edge == WLR_EDGE_TOP || edge == WLR_EDGE_BOTTOM) { - container->height += amount; - layout_match = container->layout == L_VERT; - } - if (container->children) { - for (int i = 0; i < container->children->length; i++) { - struct sway_container *child = container->children->items[i]; - double amt = layout_match ? - amount / container->children->length : amount; - container_recursive_resize(child, amt, edge); - } - } -} - -static void swap_places(struct sway_container *con1, - struct sway_container *con2) { - struct sway_container *temp = malloc(sizeof(struct sway_container)); - temp->x = con1->x; - temp->y = con1->y; - temp->width = con1->width; - temp->height = con1->height; - temp->parent = con1->parent; - - con1->x = con2->x; - con1->y = con2->y; - con1->width = con2->width; - con1->height = con2->height; - - con2->x = temp->x; - con2->y = temp->y; - con2->width = temp->width; - con2->height = temp->height; - - int temp_index = index_child(con1); - container_insert_child(con2->parent, con1, index_child(con2)); - container_insert_child(temp->parent, con2, temp_index); - - free(temp); -} - -static void swap_focus(struct sway_container *con1, - struct sway_container *con2, struct sway_seat *seat, - struct sway_container *focus) { - if (focus == con1 || focus == con2) { - struct sway_container *ws1 = container_parent(con1, C_WORKSPACE); - struct sway_container *ws2 = container_parent(con2, C_WORKSPACE); - if (focus == con1 && (con2->parent->layout == L_TABBED - || con2->parent->layout == L_STACKED)) { - if (workspace_is_visible(ws2)) { - seat_set_focus_warp(seat, con2, false, true); - } - seat_set_focus(seat, ws1 != ws2 ? con2 : con1); - } else if (focus == con2 && (con1->parent->layout == L_TABBED - || con1->parent->layout == L_STACKED)) { - if (workspace_is_visible(ws1)) { - seat_set_focus_warp(seat, con1, false, true); - } - seat_set_focus(seat, ws1 != ws2 ? con1 : con2); - } else if (ws1 != ws2) { - seat_set_focus(seat, focus == con1 ? con2 : con1); - } else { - seat_set_focus(seat, focus); - } - } else { - seat_set_focus(seat, focus); - } -} - -void container_swap(struct sway_container *con1, struct sway_container *con2) { - if (!sway_assert(con1 && con2, "Cannot swap with nothing")) { - return; - } - if (!sway_assert(con1->type >= C_CONTAINER && con2->type >= C_CONTAINER, - "Can only swap containers and views")) { - return; - } - if (!sway_assert(!container_has_ancestor(con1, con2) - && !container_has_ancestor(con2, con1), - "Cannot swap ancestor and descendant")) { - return; - } - if (!sway_assert(!container_is_floating(con1) - && !container_is_floating(con2), - "Swapping with floating containers is not supported")) { - return; - } - - wlr_log(WLR_DEBUG, "Swapping containers %zu and %zu", con1->id, con2->id); - - int fs1 = con1->is_fullscreen; - int fs2 = con2->is_fullscreen; - if (fs1) { - container_set_fullscreen(con1, false); - } - if (fs2) { - container_set_fullscreen(con2, false); - } - - struct sway_seat *seat = input_manager_get_default_seat(input_manager); - struct sway_container *focus = seat_get_focus(seat); - struct sway_container *vis1 = container_parent( - seat_get_focus_inactive(seat, container_parent(con1, C_OUTPUT)), - C_WORKSPACE); - struct sway_container *vis2 = container_parent( - seat_get_focus_inactive(seat, container_parent(con2, C_OUTPUT)), - C_WORKSPACE); - - char *stored_prev_name = NULL; - if (prev_workspace_name) { - stored_prev_name = strdup(prev_workspace_name); - } - - swap_places(con1, con2); - - if (!workspace_is_visible(vis1)) { - seat_set_focus(seat, seat_get_focus_inactive(seat, vis1)); - } - if (!workspace_is_visible(vis2)) { - seat_set_focus(seat, seat_get_focus_inactive(seat, vis2)); - } - - swap_focus(con1, con2, seat, focus); - - if (stored_prev_name) { - free(prev_workspace_name); - prev_workspace_name = stored_prev_name; - } - - if (fs1) { - container_set_fullscreen(con2, true); - } - if (fs2) { - container_set_fullscreen(con1, true); - } -} diff --git a/sway/tree/output.c b/sway/tree/output.c index 6da63064..6601220b 100644 --- a/sway/tree/output.c +++ b/sway/tree/output.c @@ -10,6 +10,7 @@ #include "log.h" static void restore_workspaces(struct sway_container *output) { + // Workspace output priority for (int i = 0; i < root_container.children->length; i++) { struct sway_container *other = root_container.children->items[i]; if (other == output) { @@ -29,6 +30,15 @@ static void restore_workspaces(struct sway_container *output) { } } + // Saved workspaces + list_t *saved = root_container.sway_root->saved_workspaces; + for (int i = 0; i < saved->length; ++i) { + struct sway_container *ws = saved->items[i]; + container_add_child(output, ws); + ipc_event_workspace(NULL, ws, "move"); + } + saved->length = 0; + output_sort_workspaces(output); } @@ -68,7 +78,7 @@ struct sway_container *output_create( output->sway_output = sway_output; output->name = strdup(name); if (output->name == NULL) { - container_destroy(output); + output_begin_destroy(output); return NULL; } @@ -103,6 +113,120 @@ struct sway_container *output_create( return output; } +static void output_evacuate(struct sway_container *output) { + if (!output->children->length) { + return; + } + struct sway_container *fallback_output = NULL; + if (root_container.children->length > 1) { + fallback_output = root_container.children->items[0]; + if (fallback_output == output) { + fallback_output = root_container.children->items[1]; + } + } + + while (output->children->length) { + struct sway_container *workspace = output->children->items[0]; + + container_remove_child(workspace); + + if (workspace_is_empty(workspace)) { + workspace_begin_destroy(workspace); + continue; + } + + struct sway_container *new_output = + workspace_output_get_highest_available(workspace, output); + if (!new_output) { + new_output = fallback_output; + } + + if (new_output) { + workspace_output_add_priority(workspace, new_output); + container_add_child(new_output, workspace); + output_sort_workspaces(new_output); + ipc_event_workspace(NULL, workspace, "move"); + } else { + list_add(root_container.sway_root->saved_workspaces, workspace); + } + } +} + +void output_destroy(struct sway_container *output) { + if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) { + return; + } + if (!sway_assert(output->destroying, + "Tried to free output which wasn't marked as destroying")) { + return; + } + if (!sway_assert(output->ntxnrefs == 0, "Tried to free output " + "which is still referenced by transactions")) { + return; + } + free(output->name); + free(output->formatted_title); + wlr_texture_destroy(output->title_focused); + wlr_texture_destroy(output->title_focused_inactive); + wlr_texture_destroy(output->title_unfocused); + wlr_texture_destroy(output->title_urgent); + list_free(output->children); + list_free(output->current.children); + list_free(output->outputs); + free(output); + + // NOTE: We don't actually destroy the sway_output here +} + +static void untrack_output(struct sway_container *con, void *data) { + struct sway_output *output = data; + int index = list_find(con->outputs, output); + if (index != -1) { + list_del(con->outputs, index); + } +} + +void output_begin_destroy(struct sway_container *output) { + if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) { + return; + } + wlr_log(WLR_DEBUG, "OUTPUT: Destroying output '%s'", output->name); + wl_signal_emit(&output->events.destroy, output); + + output_evacuate(output); + + output->destroying = true; + container_set_dirty(output); + + root_for_each_container(untrack_output, output->sway_output); + + wl_list_remove(&output->sway_output->mode.link); + wl_list_remove(&output->sway_output->transform.link); + wl_list_remove(&output->sway_output->scale.link); + wl_list_remove(&output->sway_output->damage_destroy.link); + wl_list_remove(&output->sway_output->damage_frame.link); + + output->sway_output->swayc = NULL; + output->sway_output = NULL; + + if (output->parent) { + container_remove_child(output); + } +} + +struct sway_container *output_from_wlr_output(struct wlr_output *output) { + if (output == NULL) { + return NULL; + } + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *o = root_container.children->items[i]; + if (o->type == C_OUTPUT && o->sway_output->wlr_output == output) { + return o; + } + } + return NULL; +} + void output_for_each_workspace(struct sway_container *output, void (*f)(struct sway_container *con, void *data), void *data) { if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) { diff --git a/sway/tree/root.c b/sway/tree/root.c index c27ff2c3..b42371de 100644 --- a/sway/tree/root.c +++ b/sway/tree/root.c @@ -3,6 +3,7 @@ #include <stdlib.h> #include <string.h> #include <wlr/types/wlr_output_layout.h> +#include "sway/desktop/transaction.h" #include "sway/input/seat.h" #include "sway/output.h" #include "sway/tree/arrange.h" @@ -32,13 +33,14 @@ void root_create(void) { root_container.sway_root = calloc(1, sizeof(*root_container.sway_root)); root_container.sway_root->output_layout = wlr_output_layout_create(); - wl_list_init(&root_container.sway_root->outputs); + wl_list_init(&root_container.sway_root->all_outputs); #ifdef HAVE_XWAYLAND wl_list_init(&root_container.sway_root->xwayland_unmanaged); #endif wl_list_init(&root_container.sway_root->drag_icons); wl_signal_init(&root_container.sway_root->events.new_container); root_container.sway_root->scratchpad = create_list(); + root_container.sway_root->saved_workspaces = create_list(); root_container.sway_root->output_layout_change.notify = output_layout_handle_change; @@ -50,6 +52,7 @@ void root_destroy(void) { // sway_root wl_list_remove(&root_container.sway_root->output_layout_change.link); list_free(root_container.sway_root->scratchpad); + list_free(root_container.sway_root->saved_workspaces); wlr_output_layout_destroy(root_container.sway_root->output_layout); free(root_container.sway_root); diff --git a/sway/tree/view.c b/sway/tree/view.c index 7bf7325a..1a98c5f2 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -13,12 +13,12 @@ #include "log.h" #include "sway/criteria.h" #include "sway/commands.h" +#include "sway/desktop/transaction.h" #include "sway/ipc-server.h" #include "sway/output.h" #include "sway/input/seat.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" -#include "sway/tree/layout.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" #include "sway/config.h" @@ -35,7 +35,7 @@ void view_init(struct sway_view *view, enum sway_view_type type, wl_signal_init(&view->events.unmap); } -void view_free(struct sway_view *view) { +void view_destroy(struct sway_view *view) { if (!sway_assert(view->surface == NULL, "Tried to free mapped view")) { return; } @@ -75,14 +75,14 @@ void view_free(struct sway_view *view) { * destroying flag will make the view get freed when the transaction is * finished. */ -void view_destroy(struct sway_view *view) { +void view_begin_destroy(struct sway_view *view) { if (!sway_assert(view->surface == NULL, "Tried to destroy a mapped view")) { return; } view->destroying = true; if (!view->swayc) { - view_free(view); + view_destroy(view); } } @@ -225,15 +225,13 @@ void view_autoconfigure(struct sway_view *view) { x = y = width = height = 0; double y_offset = 0; - // In a tabbed or stacked container, the swayc's y is the top of the title - // area. We have to offset the surface y by the height of the title bar, and - // disable any top border because we'll always have the title bar. - if (con->parent->layout == L_TABBED) { - y_offset = container_titlebar_height(); - view->border_top = false; - } else if (con->parent->layout == L_STACKED) { - y_offset = container_titlebar_height() * con->parent->children->length; + // In a tabbed or stacked container, the swayc's y is the bottom of the + // title area. We have to disable any top border because the title bar is + // rendered by the parent. + if (con->parent->layout == L_TABBED || con->parent->layout == L_STACKED) { view->border_top = false; + } else { + y_offset = container_titlebar_height(); } enum sway_container_border border = view->border; @@ -244,17 +242,17 @@ void view_autoconfigure(struct sway_view *view) { switch (border) { case B_NONE: x = con->x; - y = con->y + y_offset; + y = con->y; width = con->width; - height = con->height - y_offset; + height = con->height; break; case B_PIXEL: x = con->x + view->border_thickness * view->border_left; - y = con->y + view->border_thickness * view->border_top + y_offset; + y = con->y + view->border_thickness * view->border_top; width = con->width - view->border_thickness * view->border_left - view->border_thickness * view->border_right; - height = con->height - y_offset + height = con->height - view->border_thickness * view->border_top - view->border_thickness * view->border_bottom; break; @@ -264,15 +262,9 @@ void view_autoconfigure(struct sway_view *view) { width = con->width - view->border_thickness * view->border_left - view->border_thickness * view->border_right; - if (y_offset) { - y = con->y + y_offset; - height = con->height - y_offset - - view->border_thickness * view->border_bottom; - } else { - y = con->y + container_titlebar_height(); - height = con->height - container_titlebar_height() - - view->border_thickness * view->border_bottom; - } + y = con->y + y_offset; + height = con->height - y_offset + - view->border_thickness * view->border_bottom; break; } @@ -560,7 +552,9 @@ void view_unmap(struct sway_view *view) { } bool was_fullscreen = view->swayc->is_fullscreen; - struct sway_container *surviving_ancestor = container_destroy(view->swayc); + struct sway_container *parent = view->swayc->parent; + container_begin_destroy(view->swayc); + struct sway_container *surviving_ancestor = container_reap_empty(parent); // If the workspace wasn't reaped if (surviving_ancestor && surviving_ancestor->type >= C_WORKSPACE) { diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index cf50ee09..60256336 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -79,6 +79,65 @@ struct sway_container *workspace_create(struct sway_container *output, return workspace; } +void workspace_destroy(struct sway_container *workspace) { + if (!sway_assert(workspace->type == C_WORKSPACE, "Expected a workspace")) { + return; + } + if (!sway_assert(workspace->destroying, + "Tried to free workspace which wasn't marked as destroying")) { + return; + } + if (!sway_assert(workspace->ntxnrefs == 0, "Tried to free workspace " + "which is still referenced by transactions")) { + return; + } + // sway_workspace + struct sway_workspace *ws = workspace->sway_workspace; + list_foreach(ws->output_priority, free); + list_free(ws->output_priority); + list_free(ws->floating); + free(ws); + + // swayc + free(workspace->name); + free(workspace->formatted_title); + wlr_texture_destroy(workspace->title_focused); + wlr_texture_destroy(workspace->title_focused_inactive); + wlr_texture_destroy(workspace->title_unfocused); + wlr_texture_destroy(workspace->title_urgent); + list_free(workspace->children); + list_free(workspace->current.children); + list_free(workspace->outputs); + free(workspace); +} + +void workspace_begin_destroy(struct sway_container *workspace) { + if (!sway_assert(workspace->type == C_WORKSPACE, "Expected a workspace")) { + return; + } + wlr_log(WLR_DEBUG, "Destroying workspace '%s'", workspace->name); + wl_signal_emit(&workspace->events.destroy, workspace); + ipc_event_workspace(NULL, workspace, "empty"); // intentional + + workspace->destroying = true; + container_set_dirty(workspace); + + if (workspace->parent) { + container_remove_child(workspace); + } +} + +void workspace_consider_destroy(struct sway_container *ws) { + if (!sway_assert(ws->type == C_WORKSPACE, "Expected a workspace")) { + return; + } + struct sway_seat *seat = input_manager_current_seat(input_manager); + if (ws->children->length == 0 && ws->sway_workspace->floating->length == 0 + && seat_get_active_child(seat, ws->parent) != ws) { + workspace_begin_destroy(ws); + } +} + char *prev_workspace_name = NULL; void next_name_map(struct sway_container *ws, void *data) { @@ -421,9 +480,7 @@ bool workspace_switch(struct sway_container *workspace, // no op. We therefore need to send the IPC event and clean up the old // workspace here. ipc_event_workspace(active_ws, workspace, "focus"); - if (!workspace_is_visible(active_ws) && workspace_is_empty(active_ws)) { - container_destroy(active_ws); - } + workspace_consider_destroy(active_ws); } seat_set_focus(seat, next); struct sway_container *output = container_parent(workspace, C_OUTPUT); @@ -608,3 +665,46 @@ void workspace_add_floating(struct sway_container *workspace, container_set_dirty(workspace); container_set_dirty(con); } + +void workspace_remove_gaps(struct sway_container *ws) { + if (!sway_assert(ws->type == C_WORKSPACE, "Expected a workspace")) { + return; + } + if (ws->current_gaps == 0) { + return; + } + + ws->width += ws->current_gaps * 2; + ws->height += ws->current_gaps * 2; + ws->x -= ws->current_gaps; + ws->y -= ws->current_gaps; + ws->current_gaps = 0; +} + +void workspace_add_gaps(struct sway_container *ws) { + if (!sway_assert(ws->type == C_WORKSPACE, "Expected a workspace")) { + return; + } + if (ws->current_gaps > 0) { + return; + } + bool should_apply = + config->edge_gaps || (config->smart_gaps && ws->children->length > 1); + if (!should_apply) { + return; + } + + ws->current_gaps = ws->has_gaps ? ws->gaps_outer : config->gaps_outer; + + if (ws->layout == L_TABBED || ws->layout == L_STACKED) { + // We have to add inner gaps for this, because children of tabbed and + // stacked containers don't apply their own gaps - they assume the + // tabbed/stacked container is using gaps. + ws->current_gaps += ws->has_gaps ? ws->gaps_inner : config->gaps_inner; + } + + ws->x += ws->current_gaps; + ws->y += ws->current_gaps; + ws->width -= 2 * ws->current_gaps; + ws->height -= 2 * ws->current_gaps; +} |