aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/swap.c
diff options
context:
space:
mode:
authorLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-08-30 21:00:10 +1000
committerLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-09-05 18:01:43 +1000
commit7586f150c058997d9dde387ea7c091ffa7a3c3c7 (patch)
tree63d19027974c1db62ce3a74ca1d2314eb6d5049b /sway/commands/swap.c
parentMerge pull request #2569 from RyanDwyer/deny-reload-repeat (diff)
downloadsway-7586f150c058997d9dde387ea7c091ffa7a3c3c7.tar.gz
sway-7586f150c058997d9dde387ea7c091ffa7a3c3c7.tar.zst
sway-7586f150c058997d9dde387ea7c091ffa7a3c3c7.zip
Implement type safe arguments and demote sway_container
This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
Diffstat (limited to 'sway/commands/swap.c')
-rw-r--r--sway/commands/swap.c76
1 files changed, 36 insertions, 40 deletions
diff --git a/sway/commands/swap.c b/sway/commands/swap.c
index f25c43a1..a0ffbda8 100644
--- a/sway/commands/swap.c
+++ b/sway/commands/swap.c
@@ -4,6 +4,7 @@
4#include "config.h" 4#include "config.h"
5#include "log.h" 5#include "log.h"
6#include "sway/commands.h" 6#include "sway/commands.h"
7#include "sway/output.h"
7#include "sway/tree/arrange.h" 8#include "sway/tree/arrange.h"
8#include "sway/tree/root.h" 9#include "sway/tree/root.h"
9#include "sway/tree/view.h" 10#include "sway/tree/view.h"
@@ -43,27 +44,28 @@ static void swap_focus(struct sway_container *con1,
43 struct sway_container *con2, struct sway_seat *seat, 44 struct sway_container *con2, struct sway_seat *seat,
44 struct sway_container *focus) { 45 struct sway_container *focus) {
45 if (focus == con1 || focus == con2) { 46 if (focus == con1 || focus == con2) {
46 struct sway_container *ws1 = container_parent(con1, C_WORKSPACE); 47 struct sway_workspace *ws1 = con1->workspace;
47 struct sway_container *ws2 = container_parent(con2, C_WORKSPACE); 48 struct sway_workspace *ws2 = con2->workspace;
48 if (focus == con1 && (con2->parent->layout == L_TABBED 49 enum sway_container_layout layout1 = container_parent_layout(con1);
49 || con2->parent->layout == L_STACKED)) { 50 enum sway_container_layout layout2 = container_parent_layout(con2);
51 if (focus == con1 && (layout2 == L_TABBED || layout2 == L_STACKED)) {
50 if (workspace_is_visible(ws2)) { 52 if (workspace_is_visible(ws2)) {
51 seat_set_focus_warp(seat, con2, false, true); 53 seat_set_focus_warp(seat, &con2->node, false, true);
52 } 54 }
53 seat_set_focus(seat, ws1 != ws2 ? con2 : con1); 55 seat_set_focus(seat, ws1 != ws2 ? &con2->node : &con1->node);
54 } else if (focus == con2 && (con1->parent->layout == L_TABBED 56 } else if (focus == con2 && (layout1 == L_TABBED
55 || con1->parent->layout == L_STACKED)) { 57 || layout1 == L_STACKED)) {
56 if (workspace_is_visible(ws1)) { 58 if (workspace_is_visible(ws1)) {
57 seat_set_focus_warp(seat, con1, false, true); 59 seat_set_focus_warp(seat, &con1->node, false, true);
58 } 60 }
59 seat_set_focus(seat, ws1 != ws2 ? con1 : con2); 61 seat_set_focus(seat, ws1 != ws2 ? &con1->node : &con2->node);
60 } else if (ws1 != ws2) { 62 } else if (ws1 != ws2) {
61 seat_set_focus(seat, focus == con1 ? con2 : con1); 63 seat_set_focus(seat, focus == con1 ? &con2->node : &con1->node);
62 } else { 64 } else {
63 seat_set_focus(seat, focus); 65 seat_set_focus(seat, &focus->node);
64 } 66 }
65 } else { 67 } else {
66 seat_set_focus(seat, focus); 68 seat_set_focus(seat, &focus->node);
67 } 69 }
68} 70}
69 71
@@ -72,10 +74,6 @@ static void container_swap(struct sway_container *con1,
72 if (!sway_assert(con1 && con2, "Cannot swap with nothing")) { 74 if (!sway_assert(con1 && con2, "Cannot swap with nothing")) {
73 return; 75 return;
74 } 76 }
75 if (!sway_assert(con1->type >= C_CONTAINER && con2->type >= C_CONTAINER,
76 "Can only swap containers and views")) {
77 return;
78 }
79 if (!sway_assert(!container_has_ancestor(con1, con2) 77 if (!sway_assert(!container_has_ancestor(con1, con2)
80 && !container_has_ancestor(con2, con1), 78 && !container_has_ancestor(con2, con1),
81 "Cannot swap ancestor and descendant")) { 79 "Cannot swap ancestor and descendant")) {
@@ -87,10 +85,11 @@ static void container_swap(struct sway_container *con1,
87 return; 85 return;
88 } 86 }
89 87
90 wlr_log(WLR_DEBUG, "Swapping containers %zu and %zu", con1->id, con2->id); 88 wlr_log(WLR_DEBUG, "Swapping containers %zu and %zu",
89 con1->node.id, con2->node.id);
91 90
92 int fs1 = con1->is_fullscreen; 91 bool fs1 = con1->is_fullscreen;
93 int fs2 = con2->is_fullscreen; 92 bool fs2 = con2->is_fullscreen;
94 if (fs1) { 93 if (fs1) {
95 container_set_fullscreen(con1, false); 94 container_set_fullscreen(con1, false);
96 } 95 }
@@ -99,13 +98,11 @@ static void container_swap(struct sway_container *con1,
99 } 98 }
100 99
101 struct sway_seat *seat = input_manager_get_default_seat(input_manager); 100 struct sway_seat *seat = input_manager_get_default_seat(input_manager);
102 struct sway_container *focus = seat_get_focus(seat); 101 struct sway_container *focus = seat_get_focused_container(seat);
103 struct sway_container *vis1 = container_parent( 102 struct sway_workspace *vis1 =
104 seat_get_focus_inactive(seat, container_parent(con1, C_OUTPUT)), 103 output_get_active_workspace(con1->workspace->output);
105 C_WORKSPACE); 104 struct sway_workspace *vis2 =
106 struct sway_container *vis2 = container_parent( 105 output_get_active_workspace(con2->workspace->output);
107 seat_get_focus_inactive(seat, container_parent(con2, C_OUTPUT)),
108 C_WORKSPACE);
109 106
110 char *stored_prev_name = NULL; 107 char *stored_prev_name = NULL;
111 if (prev_workspace_name) { 108 if (prev_workspace_name) {
@@ -115,10 +112,10 @@ static void container_swap(struct sway_container *con1,
115 swap_places(con1, con2); 112 swap_places(con1, con2);
116 113
117 if (!workspace_is_visible(vis1)) { 114 if (!workspace_is_visible(vis1)) {
118 seat_set_focus(seat, seat_get_focus_inactive(seat, vis1)); 115 seat_set_focus(seat, seat_get_focus_inactive(seat, &vis1->node));
119 } 116 }
120 if (!workspace_is_visible(vis2)) { 117 if (!workspace_is_visible(vis2)) {
121 seat_set_focus(seat, seat_get_focus_inactive(seat, vis2)); 118 seat_set_focus(seat, seat_get_focus_inactive(seat, &vis2->node));
122 } 119 }
123 120
124 swap_focus(con1, con2, seat, focus); 121 swap_focus(con1, con2, seat, focus);
@@ -137,23 +134,22 @@ static void container_swap(struct sway_container *con1,
137} 134}
138 135
139static bool test_con_id(struct sway_container *container, void *con_id) { 136static bool test_con_id(struct sway_container *container, void *con_id) {
140 return container->id == (size_t)con_id; 137 return container->node.id == (size_t)con_id;
141} 138}
142 139
143static bool test_id(struct sway_container *container, void *id) { 140static bool test_id(struct sway_container *container, void *id) {
144#ifdef HAVE_XWAYLAND 141#ifdef HAVE_XWAYLAND
145 xcb_window_t *wid = id; 142 xcb_window_t *wid = id;
146 return (container->type == C_VIEW 143 return (container->view && container->view->type == SWAY_VIEW_XWAYLAND
147 && container->sway_view->type == SWAY_VIEW_XWAYLAND 144 && container->view->wlr_xwayland_surface->window_id == *wid);
148 && container->sway_view->wlr_xwayland_surface->window_id == *wid);
149#else 145#else
150 return false; 146 return false;
151#endif 147#endif
152} 148}
153 149
154static bool test_mark(struct sway_container *container, void *mark) { 150static bool test_mark(struct sway_container *container, void *mark) {
155 if (container->type == C_VIEW && container->sway_view->marks->length) { 151 if (container->view && container->view->marks->length) {
156 return !list_seq_find(container->sway_view->marks, 152 return !list_seq_find(container->view->marks,
157 (int (*)(const void *, const void *))strcmp, mark); 153 (int (*)(const void *, const void *))strcmp, mark);
158 } 154 }
159 return false; 155 return false;
@@ -169,7 +165,7 @@ struct cmd_results *cmd_swap(int argc, char **argv) {
169 return cmd_results_new(CMD_INVALID, "swap", EXPECTED_SYNTAX); 165 return cmd_results_new(CMD_INVALID, "swap", EXPECTED_SYNTAX);
170 } 166 }
171 167
172 struct sway_container *current = config->handler_context.current_container; 168 struct sway_container *current = config->handler_context.container;
173 struct sway_container *other; 169 struct sway_container *other;
174 170
175 char *value = join_args(argv + 3, argc - 3); 171 char *value = join_args(argv + 3, argc - 3);
@@ -191,7 +187,7 @@ struct cmd_results *cmd_swap(int argc, char **argv) {
191 if (!other) { 187 if (!other) {
192 error = cmd_results_new(CMD_FAILURE, "swap", 188 error = cmd_results_new(CMD_FAILURE, "swap",
193 "Failed to find %s '%s'", argv[2], value); 189 "Failed to find %s '%s'", argv[2], value);
194 } else if (current->type < C_CONTAINER || other->type < C_CONTAINER) { 190 } else if (!current) {
195 error = cmd_results_new(CMD_FAILURE, "swap", 191 error = cmd_results_new(CMD_FAILURE, "swap",
196 "Can only swap with containers and views"); 192 "Can only swap with containers and views");
197 } else if (container_has_ancestor(current, other) 193 } else if (container_has_ancestor(current, other)
@@ -211,9 +207,9 @@ struct cmd_results *cmd_swap(int argc, char **argv) {
211 207
212 container_swap(current, other); 208 container_swap(current, other);
213 209
214 arrange_windows(current->parent); 210 arrange_node(node_get_parent(&current->node));
215 if (other->parent != current->parent) { 211 if (node_get_parent(&other->node) != node_get_parent(&current->node)) {
216 arrange_windows(other->parent); 212 arrange_node(node_get_parent(&other->node));
217 } 213 }
218 214
219 return cmd_results_new(CMD_SUCCESS, NULL, NULL); 215 return cmd_results_new(CMD_SUCCESS, NULL, NULL);