diff options
60 files changed, 1815 insertions, 1720 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/completions/zsh/_swaylock b/completions/zsh/_swaylock index 478c7512..8fb4834c 100644 --- a/completions/zsh/_swaylock +++ b/completions/zsh/_swaylock @@ -6,9 +6,9 @@ _arguments -s \ '(-v --version)'{-v,--version}'[Show the version number and quit]' \ '(-h --help)'{-h,--help}'[Show help message and quit]' \ - '(-f --daemonize)'{-f, --daemonize}'[Detach from the controlling terminal]'\ + '(-f --daemonize)'{-f,--daemonize}'[Detach from the controlling terminal]' \ '(-c --color)'{-c,--color}'[Specify a color (rrggbb)]' \ '(-i --image)'{-i,--image}'[Display an image]:files:_files' \ '(-s --scaling)'{-s,--scaling}'[Scaling mode]:mode:(stretch fill fit center tile)' \ - '(-u --no-unlock-indicator)'{-u,--no-unlock-indicator}'[Disable the unlock indicator]' - '(--socket)'{--socket}'[Use the specified socket path.]:files:_files' \ + '(-u --no-unlock-indicator)'{-u,--no-unlock-indicator}'[Disable the unlock indicator]' \ + '(--socket)'--socket'[Use the specified socket path.]:files:_files' \ 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/debug.h b/include/sway/debug.h index 38d4eccd..bf3a5f6d 100644 --- a/include/sway/debug.h +++ b/include/sway/debug.h @@ -1,15 +1,22 @@ #ifndef SWAY_DEBUG_H #define SWAY_DEBUG_H +#include <stdbool.h> -// Tree -extern bool enable_debug_tree; -void update_debug_tree(); +struct sway_debug { + bool noatomic; // Ignore atomic layout updates + bool render_tree; // Render the tree overlay + bool txn_timings; // Log verbose messages about transactions + bool txn_wait; // Always wait for the timeout before applying + + enum { + DAMAGE_DEFAULT, // Default behaviour + DAMAGE_HIGHLIGHT, // Highlight regions of the screen being damaged + DAMAGE_RERENDER, // Render the full output when any damage occurs + } damage; +}; -// Damage -extern const char *damage_debug; +extern struct sway_debug debug; -// Transactions -extern int txn_timeout_ms; -extern bool txn_debug; +void update_debug_tree(); #endif 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/server.h b/include/sway/server.h index b93584b6..1e20f2c8 100644 --- a/include/sway/server.h +++ b/include/sway/server.h @@ -54,8 +54,7 @@ struct sway_server { struct wl_listener server_decoration; struct wl_list decorations; // sway_server_decoration::link - bool debug_txn_timings; - + size_t txn_timeout_ms; list_t *transactions; list_t *dirty_containers; }; diff --git a/include/sway/tree/arrange.h b/include/sway/tree/arrange.h index d6abcc81..346103d3 100644 --- a/include/sway/tree/arrange.h +++ b/include/sway/tree/arrange.h @@ -4,12 +4,6 @@ struct sway_container; -// Remove gaps around container -void remove_gaps(struct sway_container *c); - -// Add gaps around container -void add_gaps(struct sway_container *c); - /** * Arrange layout for all the children of the given container. */ diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 5eccedc1..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; @@ -138,6 +141,9 @@ struct sway_container { struct sway_container *parent; + // Outputs currently being intersected + list_t *outputs; // struct sway_output + // Indicates that the container is a scratchpad container. // Both hidden and visible scratchpad containers have scratchpad=true. // Hidden scratchpad containers have a NULL parent. @@ -166,39 +172,13 @@ struct sway_container { struct { struct wl_signal destroy; - // Raised after the tree updates, but before arrange_windows - // Passed the previous parent - struct wl_signal reparent; } events; - - struct wl_listener reparent; }; 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 @@ -207,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); @@ -257,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); @@ -353,4 +330,43 @@ bool container_is_floating_or_child(struct sway_container *container); */ bool container_is_fullscreen_or_child(struct sway_container *container); +/** + * Return the output which will be used for scale purposes. + * This is the most recently entered output. + */ +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 5b803dfe..00000000 --- a/include/sway/tree/layout.h +++ /dev/null @@ -1,59 +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 resize_edge { - RESIZE_EDGE_NONE = 0, - RESIZE_EDGE_LEFT = 1, - RESIZE_EDGE_RIGHT = 2, - RESIZE_EDGE_TOP = 4, - RESIZE_EDGE_BOTTOM = 8, -}; - -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 resize_edge 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 2747e7c4..f73ce571 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -120,7 +120,6 @@ struct sway_view { } events; struct wl_listener surface_new_subsurface; - struct wl_listener container_reparent; }; struct sway_xdg_shell_v6_view { @@ -285,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/swaynag/swaynag.h b/include/swaynag/swaynag.h index 1bf8b640..a32d1503 100644 --- a/include/swaynag/swaynag.h +++ b/include/swaynag/swaynag.h @@ -58,7 +58,7 @@ struct swaynag_details { int offset; int visible_lines; int total_lines; - struct swaynag_button button_details; + struct swaynag_button *button_details; struct swaynag_button button_up; struct swaynag_button button_down; }; 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/move.c b/sway/commands/move.c index e788d32f..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, @@ -236,7 +671,6 @@ static void workspace_move_to_output(struct sway_container *workspace, seat_get_focus_inactive(seat, output); container_add_child(output, workspace); - wl_signal_emit(&workspace->events.reparent, old_output); // If moving the last workspace from the old output, create a new workspace // on the old output @@ -248,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); @@ -360,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) { @@ -372,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; } @@ -386,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"); @@ -396,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); } @@ -430,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 0f3005f4..ad659ef5 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c @@ -5,6 +5,7 @@ #include <stdlib.h> #include <string.h> #include <strings.h> +#include <wlr/util/edges.h> #include <wlr/util/log.h> #include "sway/commands.h" #include "sway/tree/arrange.h" @@ -158,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; @@ -250,10 +272,10 @@ static void resize_tiled(struct sway_container *parent, int amount, } } - enum resize_edge minor_edge = axis == RESIZE_AXIS_HORIZONTAL ? - RESIZE_EDGE_LEFT : RESIZE_EDGE_TOP; - enum resize_edge major_edge = axis == RESIZE_AXIS_HORIZONTAL ? - RESIZE_EDGE_RIGHT : RESIZE_EDGE_BOTTOM; + enum wlr_edges minor_edge = axis == RESIZE_AXIS_HORIZONTAL ? + WLR_EDGE_LEFT : WLR_EDGE_TOP; + enum wlr_edges major_edge = axis == RESIZE_AXIS_HORIZONTAL ? + WLR_EDGE_RIGHT : WLR_EDGE_BOTTOM; for (int i = 0; i < parent->parent->children->length; i++) { struct sway_container *sibling = parent->parent->children->items[i]; 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 0cb499e7..2768cf58 100644 --- a/sway/debug-tree.c +++ b/sway/debug-tree.c @@ -3,15 +3,19 @@ #include <wlr/render/wlr_texture.h> #include <wlr/util/log.h> #include "config.h" +#include "sway/debug.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" +#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" +struct sway_debug debug; + static const char *layout_to_str(enum sway_container_layout layout) { switch (layout) { case L_HORIZ: @@ -67,10 +71,8 @@ static int draw_container(cairo_t *cairo, struct sway_container *container, return height; } -bool enable_debug_tree = false; - void update_debug_tree() { - if (!enable_debug_tree) { + if (!debug.render_tree) { return; } 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 2253eb51..bbebe453 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -23,7 +23,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" @@ -163,8 +163,10 @@ void output_view_for_each_popup(struct sway_output *output, .user_iterator = iterator, .user_data = user_data, .output = output, - .ox = view->swayc->current.view_x - output->swayc->current.swayc_x, - .oy = view->swayc->current.view_y - output->swayc->current.swayc_y, + .ox = view->swayc->current.view_x - output->swayc->current.swayc_x + - view->geometry.x, + .oy = view->swayc->current.view_y - output->swayc->current.swayc_y + - view->geometry.y, .width = view->swayc->current.view_width, .height = view->swayc->current.view_height, .rotation = 0, // TODO @@ -496,7 +498,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) { @@ -504,7 +506,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); @@ -554,7 +556,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 aa70903e..b5a10370 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" @@ -813,8 +813,6 @@ static void render_floating(struct sway_output *soutput, } } -const char *damage_debug = NULL; - void output_render(struct sway_output *output, struct timespec *when, pixman_region32_t *damage) { struct wlr_output *wlr_output = output->wlr_output; @@ -828,21 +826,17 @@ void output_render(struct sway_output *output, struct timespec *when, wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height); - bool damage_whole_before_swap = false; if (!pixman_region32_not_empty(damage)) { // Output isn't damaged but needs buffer swap goto renderer_end; } - if (damage_debug != NULL) { - if (strcmp(damage_debug, "highlight") == 0) { - wlr_renderer_clear(renderer, (float[]){1, 1, 0, 1}); - damage_whole_before_swap = true; - } else if (strcmp(damage_debug, "rerender") == 0) { - int width, height; - wlr_output_transformed_resolution(wlr_output, &width, &height); - pixman_region32_union_rect(damage, damage, 0, 0, width, height); - } + if (debug.damage == DAMAGE_HIGHLIGHT) { + wlr_renderer_clear(renderer, (float[]){1, 1, 0, 1}); + } else if (debug.damage == DAMAGE_RERENDER) { + int width, height; + wlr_output_transformed_resolution(wlr_output, &width, &height); + pixman_region32_union_rect(damage, damage, 0, 0, width, height); } struct sway_container *workspace = output_get_active_workspace(output); @@ -916,12 +910,12 @@ render_overlay: render_drag_icons(output, damage, &root_container.sway_root->drag_icons); renderer_end: - if (root_container.sway_root->debug_tree) { + if (debug.render_tree) { + wlr_renderer_scissor(renderer, NULL); wlr_render_texture(renderer, root_container.sway_root->debug_tree, - wlr_output->transform_matrix, 0, 0, 1); + wlr_output->transform_matrix, 0, 40, 1); } - - if (damage_whole_before_swap || root_container.sway_root->debug_tree) { + if (debug.damage == DAMAGE_HIGHLIGHT) { int width, height; wlr_output_transformed_resolution(wlr_output, &width, &height); pixman_region32_union_rect(damage, damage, 0, 0, width, height); diff --git a/sway/desktop/transaction.c b/sway/desktop/transaction.c index 692fb447..c18529fb 100644 --- a/sway/desktop/transaction.c +++ b/sway/desktop/transaction.c @@ -1,5 +1,6 @@ #define _POSIX_C_SOURCE 200809L #include <errno.h> +#include <limits.h> #include <stdbool.h> #include <stdlib.h> #include <string.h> @@ -16,26 +17,12 @@ #include "list.h" #include "log.h" -/** - * How long we should wait for views to respond to the configure before giving - * up and applying the transaction anyway. - */ -int txn_timeout_ms = 200; - -/** - * If enabled, sway will always wait for the transaction timeout before - * applying it, rather than applying it when the views are ready. This allows us - * to observe the rendered state while a transaction is in progress. - */ -bool txn_debug = false; - struct sway_transaction { struct wl_event_source *timer; list_t *instructions; // struct sway_transaction_instruction * size_t num_waiting; size_t num_configures; uint32_t con_ids; // Bitwise XOR of view container IDs - struct timespec create_time; struct timespec commit_time; }; @@ -53,9 +40,6 @@ static struct sway_transaction *transaction_create() { return NULL; } transaction->instructions = create_list(); - if (server.debug_txn_timings) { - clock_gettime(CLOCK_MONOTONIC, &transaction->create_time); - } return transaction; } @@ -70,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); } @@ -150,19 +149,14 @@ static void transaction_add_container(struct sway_transaction *transaction, */ static void transaction_apply(struct sway_transaction *transaction) { wlr_log(WLR_DEBUG, "Applying transaction %p", transaction); - if (server.debug_txn_timings) { + if (debug.txn_timings) { struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); - struct timespec *create = &transaction->create_time; struct timespec *commit = &transaction->commit_time; - float ms_arranging = (commit->tv_sec - create->tv_sec) * 1000 + - (commit->tv_nsec - create->tv_nsec) / 1000000.0; - float ms_waiting = (now.tv_sec - commit->tv_sec) * 1000 + + float ms = (now.tv_sec - commit->tv_sec) * 1000 + (now.tv_nsec - commit->tv_nsec) / 1000000.0; - float ms_total = ms_arranging + ms_waiting; - wlr_log(WLR_DEBUG, "Transaction %p: %.1fms arranging, %.1fms waiting, " - "%.1fms total (%.1f frames if 60Hz)", transaction, - ms_arranging, ms_waiting, ms_total, ms_total / (1000.0f / 60)); + wlr_log(WLR_DEBUG, "Transaction %p: %.1fms waiting " + "(%.1f frames if 60Hz)", transaction, ms, ms / (1000.0f / 60)); } // Apply the instruction state to the container's current state @@ -196,7 +190,9 @@ static void transaction_apply(struct sway_transaction *transaction) { sizeof(struct sway_container_state)); if (container->type == C_VIEW && container->sway_view->saved_buffer) { - view_remove_saved_buffer(container->sway_view); + if (!container->destroying || container->ntxnrefs == 1) { + view_remove_saved_buffer(container->sway_view); + } } // Damage the new location @@ -214,6 +210,9 @@ static void transaction_apply(struct sway_transaction *transaction) { } container->instruction = NULL; + if (container->type == C_CONTAINER || container->type == C_VIEW) { + container_discover_outputs(container); + } } } @@ -304,7 +303,7 @@ static void transaction_commit(struct sway_transaction *transaction) { struct timespec when; wlr_surface_send_frame_done(con->sway_view->surface, &when); } - if (con->type == C_VIEW) { + if (con->type == C_VIEW && !con->sway_view->saved_buffer) { view_save_buffer(con->sway_view); memcpy(&con->sway_view->saved_geometry, &con->sway_view->geometry, sizeof(struct wlr_box)); @@ -312,25 +311,30 @@ static void transaction_commit(struct sway_transaction *transaction) { con->instruction = instruction; } transaction->num_configures = transaction->num_waiting; - if (server.debug_txn_timings) { + if (debug.txn_timings) { clock_gettime(CLOCK_MONOTONIC, &transaction->commit_time); } + if (debug.noatomic) { + transaction->num_waiting = 0; + } else if (debug.txn_wait) { + // Force the transaction to time out even if all views are ready. + // We do this by inflating the waiting counter. + transaction->num_waiting += 1000000; + } if (transaction->num_waiting) { // Set up a timer which the views must respond within transaction->timer = wl_event_loop_add_timer(server.wl_event_loop, handle_timeout, transaction); if (transaction->timer) { - wl_event_source_timer_update(transaction->timer, txn_timeout_ms); + wl_event_source_timer_update(transaction->timer, + server.txn_timeout_ms); } else { wlr_log(WLR_ERROR, "Unable to create transaction timer (%s). " "Some imperfect frames might be rendered.", strerror(errno)); - handle_timeout(transaction); + transaction->num_waiting = 0; } - } else { - wlr_log(WLR_DEBUG, - "Transaction %p has nothing to wait for", transaction); } // The debug tree shows the pending/live tree. Here is a good place to @@ -343,7 +347,7 @@ static void set_instruction_ready( struct sway_transaction_instruction *instruction) { struct sway_transaction *transaction = instruction->transaction; - if (server.debug_txn_timings) { + if (debug.txn_timings) { struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); struct timespec *start = &transaction->commit_time; @@ -354,21 +358,16 @@ static void set_instruction_ready( transaction->num_configures - transaction->num_waiting + 1, transaction->num_configures, ms, instruction->container->name); - } // If the transaction has timed out then its num_waiting will be 0 already. if (transaction->num_waiting > 0 && --transaction->num_waiting == 0) { - if (!txn_debug) { - wlr_log(WLR_DEBUG, "Transaction %p is ready", transaction); - wl_event_source_timer_update(transaction->timer, 0); - } + wlr_log(WLR_DEBUG, "Transaction %p is ready", transaction); + wl_event_source_timer_update(transaction->timer, 0); } instruction->container->instruction = NULL; - if (!txn_debug) { - transaction_progress_queue(); - } + transaction_progress_queue(); } void transaction_notify_view_ready_by_serial(struct sway_view *view, diff --git a/sway/desktop/xdg_shell.c b/sway/desktop/xdg_shell.c index aae129bd..7d1824f1 100644 --- a/sway/desktop/xdg_shell.c +++ b/sway/desktop/xdg_shell.c @@ -13,7 +13,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 struct sway_view_child_impl popup_impl; @@ -448,7 +447,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..522fddca 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -12,7 +12,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 struct sway_view_child_impl popup_impl; @@ -441,7 +440,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 caee37a6..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(); } @@ -984,7 +973,7 @@ void seat_begin_resize_floating(struct sway_seat *seat, seat->op_resize_preserve_ratio = keyboard && (wlr_keyboard_get_modifiers(keyboard) & WLR_MODIFIER_SHIFT); seat->op_resize_edge = edge == WLR_EDGE_NONE ? - RESIZE_EDGE_BOTTOM | RESIZE_EDGE_RIGHT : edge; + WLR_EDGE_BOTTOM | WLR_EDGE_RIGHT : edge; seat->op_button = button; seat->op_ref_lx = seat->cursor->cursor->x; seat->op_ref_ly = seat->cursor->cursor->y; diff --git a/sway/ipc-json.c b/sway/ipc-json.c index f40af043..06cb7e11 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -5,6 +5,7 @@ #include "sway/config.h" #include "sway/ipc-json.h" #include "sway/tree/container.h" +#include "sway/tree/view.h" #include "sway/tree/workspace.h" #include "sway/output.h" #include "sway/input/input-manager.h" @@ -192,6 +193,16 @@ static void ipc_json_describe_view(struct sway_container *c, json_object *object c->name ? json_object_new_string(c->name) : NULL); json_object_object_add(object, "type", json_object_new_string("con")); + if (c->type == C_VIEW) { + const char *app_id = view_get_app_id(c->sway_view); + json_object_object_add(object, "app_id", + app_id ? json_object_new_string(app_id) : NULL); + + const char *class = view_get_class(c->sway_view); + json_object_object_add(object, "class", + class ? json_object_new_string(class) : NULL); + } + if (c->parent) { json_object_object_add(object, "layout", json_object_new_string(ipc_json_layout_description(c->layout))); 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 54f48340..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" @@ -235,14 +235,20 @@ static void drop_permissions(bool keep_caps) { } void enable_debug_flag(const char *flag) { - if (strcmp(flag, "render-tree") == 0) { - enable_debug_tree = true; - } else if (strncmp(flag, "damage=", 7) == 0) { - damage_debug = &flag[7]; - } else if (strcmp(flag, "txn-debug") == 0) { - txn_debug = true; + if (strcmp(flag, "damage=highlight") == 0) { + debug.damage = DAMAGE_HIGHLIGHT; + } else if (strcmp(flag, "damage=rerender") == 0) { + debug.damage = DAMAGE_RERENDER; + } else if (strcmp(flag, "noatomic") == 0) { + debug.noatomic = true; + } else if (strcmp(flag, "render-tree") == 0) { + debug.render_tree = true; + } else if (strcmp(flag, "txn-wait") == 0) { + debug.txn_wait = true; + } else if (strcmp(flag, "txn-timings") == 0) { + debug.txn_timings = true; } else if (strncmp(flag, "txn-timeout=", 12) == 0) { - txn_timeout_ms = atoi(&flag[12]); + server.txn_timeout_ms = atoi(&flag[12]); } } 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 e8dc63be..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" @@ -128,10 +128,11 @@ bool server_init(struct sway_server *server) { return false; } - const char *debug = getenv("SWAY_DEBUG"); - if (debug != NULL && strcmp(debug, "txn_timings") == 0) { - server->debug_txn_timings = true; + // This may have been set already via -Dtxn-timeout + if (!server->txn_timeout_ms) { + server->txn_timeout_ms = 200; } + server->dirty_containers = create_list(); server->transactions = create_list(); 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 cf4a5d9a..60e5b951 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -7,7 +7,6 @@ #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" @@ -39,7 +38,7 @@ static void apply_horiz_layout(struct sway_container *parent) { child->width = parent->width; } } - remove_gaps(child); + container_remove_gaps(child); total_width += child->width; } double scale = parent->width / total_width; @@ -62,7 +61,7 @@ static void apply_horiz_layout(struct sway_container *parent) { if (i == num_children - 1) { child->width = parent->x + parent->width - child->x; } - add_gaps(child); + container_add_gaps(child); } } @@ -91,7 +90,7 @@ static void apply_vert_layout(struct sway_container *parent) { child->height = parent_height; } } - remove_gaps(child); + container_remove_gaps(child); total_height += child->height; } double scale = parent_height / total_height; @@ -115,7 +114,7 @@ static void apply_vert_layout(struct sway_container *parent) { child->height = parent->y + parent_offset + parent_height - child->y; } - add_gaps(child); + container_add_gaps(child); } } @@ -133,12 +132,12 @@ static void apply_tabbed_or_stacked_layout(struct sway_container *parent) { 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); + 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); } } @@ -205,12 +204,32 @@ static void arrange_workspace(struct sway_container *workspace) { 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; workspace->width = area->width; workspace->height = area->height; workspace->x = output->x + area->x; workspace->y = output->y + area->y; - add_gaps(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); + } + } + + workspace_add_gaps(workspace); container_set_dirty(workspace); wlr_log(WLR_DEBUG, "Arranging workspace '%s' at %f, %f", workspace->name, workspace->x, workspace->y); @@ -294,41 +313,3 @@ void arrange_windows(struct sway_container *container) { 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 ff947ca8..04454ab6 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) { @@ -76,31 +73,6 @@ void container_update_textures_recursive(struct sway_container *con) { } } -static void handle_reparent(struct wl_listener *listener, - void *data) { - struct sway_container *container = - wl_container_of(listener, container, reparent); - struct sway_container *old_parent = data; - - struct sway_container *old_output = old_parent; - if (old_output != NULL && old_output->type != C_OUTPUT) { - old_output = container_parent(old_output, C_OUTPUT); - } - - struct sway_container *new_output = container->parent; - if (new_output != NULL && new_output->type != C_OUTPUT) { - new_output = container_parent(new_output, C_OUTPUT); - } - - if (old_output && new_output) { - float old_scale = old_output->sway_output->wlr_output->scale; - float new_scale = new_output->sway_output->wlr_output->scale; - if (old_scale != new_scale) { - container_update_textures_recursive(container); - } - } -} - struct sway_container *container_create(enum sway_container_type type) { // next id starts at 1 because 0 is assigned to root_container in layout.c static size_t next_id = 1; @@ -117,12 +89,9 @@ struct sway_container *container_create(enum sway_container_type type) { c->children = create_list(); c->current.children = create_list(); } + c->outputs = create_list(); wl_signal_init(&c->events.destroy); - wl_signal_init(&c->events.reparent); - - wl_signal_add(&c->events.reparent, &c->reparent); - c->reparent.notify = handle_reparent; c->has_gaps = false; c->gaps_inner = 0; @@ -132,199 +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); - - 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 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); } } - 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); @@ -335,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; } @@ -388,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); @@ -794,13 +566,24 @@ void container_damage_whole(struct sway_container *container) { } } +/** + * Return the output which will be used for scale purposes. + * This is the most recently entered output. + */ +struct sway_output *container_get_effective_output(struct sway_container *con) { + if (con->outputs->length == 0) { + return NULL; + } + return con->outputs->items[con->outputs->length - 1]; +} + static void update_title_texture(struct sway_container *con, struct wlr_texture **texture, struct border_colors *class) { if (!sway_assert(con->type == C_CONTAINER || con->type == C_VIEW, "Unexpected type %s", container_type_to_str(con->type))) { return; } - struct sway_container *output = container_parent(con, C_OUTPUT); + struct sway_output *output = container_get_effective_output(con); if (!output) { return; } @@ -812,7 +595,7 @@ static void update_title_texture(struct sway_container *con, return; } - double scale = output->sway_output->wlr_output->scale; + double scale = output->wlr_output->scale; int width = 0; int height = con->title_height * scale; @@ -840,7 +623,7 @@ static void update_title_texture(struct sway_container *con, unsigned char *data = cairo_image_surface_get_data(surface); int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); struct wlr_renderer *renderer = wlr_backend_get_renderer( - output->sway_output->wlr_output->backend); + output->wlr_output->backend); *texture = wlr_texture_from_pixels( renderer, WL_SHM_FORMAT_ARGB8888, stride, width, height, data); cairo_surface_destroy(surface); @@ -1224,6 +1007,7 @@ void container_set_fullscreen(struct sway_container *container, bool enable) { container_set_fullscreen(workspace->sway_workspace->fullscreen, false); } + set_fullscreen_iterator(container, &enable); container_for_each_child(container, set_fullscreen_iterator, &enable); container->is_fullscreen = enable; @@ -1289,3 +1073,323 @@ bool container_is_fullscreen_or_child(struct sway_container *container) { return false; } + +static void surface_send_enter_iterator(struct wlr_surface *surface, + int x, int y, void *data) { + struct wlr_output *wlr_output = data; + wlr_surface_send_enter(surface, wlr_output); +} + +static void surface_send_leave_iterator(struct wlr_surface *surface, + int x, int y, void *data) { + struct wlr_output *wlr_output = data; + wlr_surface_send_leave(surface, wlr_output); +} + +void container_discover_outputs(struct sway_container *con) { + if (!sway_assert(con->type == C_CONTAINER || con->type == C_VIEW, + "Expected a container or view")) { + return; + } + struct wlr_box con_box = { + .x = con->current.swayc_x, + .y = con->current.swayc_y, + .width = con->current.swayc_width, + .height = con->current.swayc_height, + }; + struct sway_output *old_output = container_get_effective_output(con); + + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *output = root_container.children->items[i]; + struct sway_output *sway_output = output->sway_output; + struct wlr_box output_box; + container_get_box(output, &output_box); + struct wlr_box intersection; + bool intersects = + wlr_box_intersection(&con_box, &output_box, &intersection); + int index = list_find(con->outputs, sway_output); + + if (intersects && index == -1) { + // Send enter + wlr_log(WLR_DEBUG, "Con %p entered output %p", con, sway_output); + if (con->type == C_VIEW) { + view_for_each_surface(con->sway_view, + surface_send_enter_iterator, sway_output->wlr_output); + } + list_add(con->outputs, sway_output); + } else if (!intersects && index != -1) { + // Send leave + wlr_log(WLR_DEBUG, "Con %p left output %p", con, sway_output); + if (con->type == C_VIEW) { + view_for_each_surface(con->sway_view, + surface_send_leave_iterator, sway_output->wlr_output); + } + list_del(con->outputs, index); + } + } + struct sway_output *new_output = container_get_effective_output(con); + double old_scale = old_output ? old_output->wlr_output->scale : -1; + double new_scale = new_output ? new_output->wlr_output->scale : -1; + if (old_scale != new_scale) { + container_update_title_textures(con); + if (con->type == C_VIEW) { + view_update_marks_textures(con->sway_view); + } + } +} + +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 || c->type != C_VIEW) { + return; + } + + c->current_gaps = c->has_gaps ? c->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); + + child->type == C_WORKSPACE ? workspace_remove_gaps(child) + : container_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); + + container_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; +} diff --git a/sway/tree/layout.c b/sway/tree/layout.c deleted file mode 100644 index 2f22a3dd..00000000 --- a/sway/tree/layout.c +++ /dev/null @@ -1,1027 +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); - wl_signal_emit(&child->events.reparent, 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); - wl_signal_emit(&active->events.reparent, 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); - } - } - - wl_signal_emit(&container->events.reparent, old_parent); - - 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); - wl_signal_emit(&child->events.reparent, 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); - wl_signal_emit(&ws_child->events.reparent, workspace); - } - - container_add_child(workspace, cont); - enum sway_container_layout old_layout = workspace->layout; - workspace->layout = layout; - cont->layout = old_layout; - } else { - struct sway_container *old_parent = child->parent; - cont->layout = layout; - container_replace_child(child, cont); - container_add_child(cont, child); - wl_signal_emit(&child->events.reparent, old_parent); - } - - 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 resize_edge edge) { - bool layout_match = true; - wlr_log(WLR_DEBUG, "Resizing %p with amount: %f", container, amount); - if (edge == RESIZE_EDGE_LEFT || edge == RESIZE_EDGE_RIGHT) { - container->width += amount; - layout_match = container->layout == L_HORIZ; - } else if (edge == RESIZE_EDGE_TOP || edge == RESIZE_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..2dd8f9f2 100644 --- a/sway/tree/root.c +++ b/sway/tree/root.c @@ -32,13 +32,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 +51,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 b77a9bb2..2870d4f5 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -18,7 +18,6 @@ #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 +34,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 +74,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); } } @@ -236,7 +235,12 @@ void view_autoconfigure(struct sway_view *view) { view->border_top = false; } - switch (view->border) { + enum sway_container_border border = view->border; + if (view->using_csd) { + border = B_NONE; + } + + switch (border) { case B_NONE: x = con->x; y = con->y + y_offset; @@ -364,48 +368,6 @@ static void view_handle_surface_new_subsurface(struct wl_listener *listener, view_subsurface_create(view, subsurface); } -static void surface_send_enter_iterator(struct wlr_surface *surface, - int x, int y, void *data) { - struct wlr_output *wlr_output = data; - wlr_surface_send_enter(surface, wlr_output); -} - -static void surface_send_leave_iterator(struct wlr_surface *surface, - int x, int y, void *data) { - struct wlr_output *wlr_output = data; - wlr_surface_send_leave(surface, wlr_output); -} - -static void view_handle_container_reparent(struct wl_listener *listener, - void *data) { - struct sway_view *view = - wl_container_of(listener, view, container_reparent); - struct sway_container *old_parent = data; - - struct sway_container *old_output = old_parent; - if (old_output != NULL && old_output->type != C_OUTPUT) { - old_output = container_parent(old_output, C_OUTPUT); - } - - struct sway_container *new_output = view->swayc->parent; - if (new_output != NULL && new_output->type != C_OUTPUT) { - new_output = container_parent(new_output, C_OUTPUT); - } - - if (old_output == new_output) { - return; - } - - if (old_output != NULL) { - view_for_each_surface(view, surface_send_leave_iterator, - old_output->sway_output->wlr_output); - } - if (new_output != NULL) { - view_for_each_surface(view, surface_send_enter_iterator, - new_output->sway_output->wlr_output); - } -} - static bool view_has_executed_criteria(struct sway_view *view, struct criteria *criteria) { for (int i = 0; i < view->executed_criteria->length; ++i) { @@ -567,9 +529,6 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { &view->surface_new_subsurface); view->surface_new_subsurface.notify = view_handle_surface_new_subsurface; - wl_signal_add(&view->swayc->events.reparent, &view->container_reparent); - view->container_reparent.notify = view_handle_container_reparent; - if (view->impl->wants_floating && view->impl->wants_floating(view)) { view->border = config->floating_border; view->border_thickness = config->floating_border_thickness; @@ -587,15 +546,12 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { view_update_title(view, false); container_notify_subtree_changed(view->swayc->parent); view_execute_criteria(view); - - view_handle_container_reparent(&view->container_reparent, NULL); } void view_unmap(struct sway_view *view) { wl_signal_emit(&view->events.unmap, view); wl_list_remove(&view->surface_new_subsurface.link); - wl_list_remove(&view->container_reparent.link); if (view->urgent_timer) { wl_event_source_remove(view->urgent_timer); @@ -603,7 +559,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) { @@ -937,7 +895,7 @@ void view_add_mark(struct sway_view *view, char *mark) { static void update_marks_texture(struct sway_view *view, struct wlr_texture **texture, struct border_colors *class) { - struct sway_container *output = container_parent(view->swayc, C_OUTPUT); + struct sway_output *output = container_get_effective_output(view->swayc); if (!output) { return; } @@ -973,7 +931,7 @@ static void update_marks_texture(struct sway_view *view, } free(part); - double scale = output->sway_output->wlr_output->scale; + double scale = output->wlr_output->scale; int width = 0; int height = view->swayc->title_height * scale; @@ -999,7 +957,7 @@ static void update_marks_texture(struct sway_view *view, unsigned char *data = cairo_image_surface_get_data(surface); int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); struct wlr_renderer *renderer = wlr_backend_get_renderer( - output->sway_output->wlr_output->backend); + output->wlr_output->backend); *texture = wlr_texture_from_pixels( renderer, WL_SHM_FORMAT_ARGB8888, stride, width, height, data); cairo_surface_destroy(surface); diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 292f2c9a..d930826e 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) { @@ -205,6 +264,7 @@ char *workspace_next_name(const char *output_name) { && workspace_by_name(wso->workspace) == NULL) { free(target); target = strdup(wso->workspace); + break; } } if (target != NULL) { @@ -420,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); @@ -607,3 +665,38 @@ 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_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; +} diff --git a/swaynag/config.c b/swaynag/config.c index d6c5739d..4d0824c9 100644 --- a/swaynag/config.c +++ b/swaynag/config.c @@ -180,8 +180,8 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, break; case 'L': // Detailed Button Text if (swaynag) { - free(swaynag->details.button_details.text); - swaynag->details.button_details.text = strdup(optarg); + free(swaynag->details.button_details->text); + swaynag->details.button_details->text = strdup(optarg); } break; case 'm': // Message diff --git a/swaynag/main.c b/swaynag/main.c index 6b0b5236..d1a0d236 100644 --- a/swaynag/main.c +++ b/swaynag/main.c @@ -34,9 +34,10 @@ int main(int argc, char **argv) { button_close->type = SWAYNAG_ACTION_DISMISS; list_add(swaynag.buttons, button_close); - swaynag.details.button_details.text = strdup("Toggle Details"); - swaynag.details.button_details.type = SWAYNAG_ACTION_EXPAND; - + swaynag.details.button_details = + calloc(sizeof(struct swaynag_button), 1); + swaynag.details.button_details->text = strdup("Toggle Details"); + swaynag.details.button_details->type = SWAYNAG_ACTION_EXPAND; char *config_path = NULL; bool debug = false; @@ -99,9 +100,10 @@ int main(int argc, char **argv) { swaynag_types_free(types); if (swaynag.details.message) { - list_add(swaynag.buttons, &swaynag.details.button_details); + list_add(swaynag.buttons, swaynag.details.button_details); } else { - free(swaynag.details.button_details.text); + free(swaynag.details.button_details->text); + free(swaynag.details.button_details); } wlr_log(WLR_DEBUG, "Output: %s", swaynag.type->output); @@ -123,7 +125,8 @@ int main(int argc, char **argv) { cleanup: swaynag_types_free(types); - free(swaynag.details.button_details.text); + free(swaynag.details.button_details->text); + free(swaynag.details.button_details); swaynag_destroy(&swaynag); return exit_code; } |