aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/sway/input/seat.h2
-rw-r--r--include/sway/tree/container.h66
-rw-r--r--include/sway/tree/layout.h12
-rw-r--r--sway/commands.c4
-rw-r--r--sway/commands/kill.c27
-rw-r--r--sway/commands/split.c87
-rw-r--r--sway/input/seat.c134
-rw-r--r--sway/main.c2
-rw-r--r--sway/meson.build1
-rw-r--r--sway/tree/container.c36
-rw-r--r--sway/tree/layout.c106
11 files changed, 384 insertions, 93 deletions
diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h
index 5c3c2c4f..c780a52b 100644
--- a/include/sway/input/seat.h
+++ b/include/sway/input/seat.h
@@ -72,7 +72,7 @@ struct sway_container *seat_get_focus_inactive(struct sway_seat *seat,
72 struct sway_container *container); 72 struct sway_container *container);
73 73
74struct sway_container *seat_get_focus_by_type(struct sway_seat *seat, 74struct sway_container *seat_get_focus_by_type(struct sway_seat *seat,
75 enum sway_container_type type); 75 struct sway_container *container, enum sway_container_type type);
76 76
77void seat_apply_config(struct sway_seat *seat, struct seat_config *seat_config); 77void seat_apply_config(struct sway_seat *seat, struct seat_config *seat_config);
78 78
diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h
index aff2e58e..fa22ea75 100644
--- a/include/sway/tree/container.h
+++ b/include/sway/tree/container.h
@@ -90,37 +90,61 @@ struct sway_container {
90 } events; 90 } events;
91}; 91};
92 92
93// TODO make private and use the container-specific create functions
94struct sway_container *container_create(enum sway_container_type type);
95
93const char *container_type_to_str(enum sway_container_type type); 96const char *container_type_to_str(enum sway_container_type type);
94 97
95// TODO only one container create function and pass the type? 98// TODO only one container create function and pass the type?
96struct sway_container *container_output_create( 99struct sway_container *container_output_create(
97 struct sway_output *sway_output); 100 struct sway_output *sway_output);
98 101
99struct sway_container *container_workspace_create( 102/**
100 struct sway_container *output, const char *name); 103 * Create a new container container. A container container can be a a child of
104 * a workspace container or another container container.
105 */
106struct sway_container *container_container_create();
101 107
102struct sway_container *container_view_create( 108/**
103 struct sway_container *sibling, struct sway_view *sway_view); 109 * Create a new output. Outputs are children of the root container and have no
110 * order in the tree structure.
111 */
112struct sway_container *container_output_create(struct sway_output *sway_output);
104 113
105struct sway_container *container_output_destroy(struct sway_container *output); 114/**
115 * Create a new workspace container. Workspaces are children of an output
116 * container and are ordered alphabetically by name.
117 */
118struct sway_container *container_workspace_create(struct sway_container *output, const char *name);
106 119
107struct sway_container *container_workspace_destroy( 120/*
108 struct sway_container *workspace); 121 * Create a new view container. A view can be a child of a workspace container
122 * or a container container and are rendered in the order and structure of
123 * how they are attached to the tree.
124 */
125// TODO view containers should be created in a detached state.
126struct sway_container *container_view_create(
127 struct sway_container *sibling, struct sway_view *sway_view);
109 128
110struct sway_container *container_view_destroy(struct sway_container *view); 129// TODO don't return the parent on destroy
130struct sway_container *container_destroy(struct sway_container *container);
111 131
112struct sway_container *container_destroy(struct sway_container *cont); 132struct sway_container *container_workspace_destroy(struct sway_container *container);
133struct sway_container *container_output_destroy(struct sway_container *container);
134struct sway_container *container_view_destroy(struct sway_container *container);
113 135
136// TODO move to layout.c
114struct sway_container *container_set_layout(struct sway_container *container, 137struct sway_container *container_set_layout(struct sway_container *container,
115 enum sway_container_layout layout); 138 enum sway_container_layout layout);
116 139
140// TODO rename to container_descendants_for_each()
117void container_descendants(struct sway_container *root, 141void container_descendants(struct sway_container *root,
118 enum sway_container_type type, 142 enum sway_container_type type,
119 void (*func)(struct sway_container *item, void *data), void *data); 143 void (*func)(struct sway_container *item, void *data), void *data);
120 144
121/** 145/**
122 * Finds a container based on test criteria. Returns the first container that 146 * Search a container's descendants a container based on test criteria. Returns
123 * passes the test. 147 * the first container that passes the test.
124 */ 148 */
125struct sway_container *container_find(struct sway_container *container, 149struct sway_container *container_find(struct sway_container *container,
126 bool (*test)(struct sway_container *view, void *data), void *data); 150 bool (*test)(struct sway_container *view, void *data), void *data);
@@ -128,18 +152,21 @@ struct sway_container *container_find(struct sway_container *container,
128/** 152/**
129 * Finds a parent container with the given struct sway_containerype. 153 * Finds a parent container with the given struct sway_containerype.
130 */ 154 */
155// TODO rename to container_parent_of_type()
131struct sway_container *container_parent(struct sway_container *container, 156struct sway_container *container_parent(struct sway_container *container,
132 enum sway_container_type type); 157 enum sway_container_type type);
133 158
134/** 159/**
135 * Find a container at the given coordinates. 160 * Find a container at the given coordinates. Returns the the surface and
161 * surface-local coordinates of the given layout coordinates if the container
162 * is a view and the view contains a surface at those coordinates.
136 */ 163 */
137struct sway_container *container_at(struct sway_container *parent, 164struct sway_container *container_at(struct sway_container *container,
138 double lx, double ly, struct wlr_surface **surface, 165 double lx, double ly, struct wlr_surface **surface,
139 double *sx, double *sy); 166 double *sx, double *sy);
140 167
141/** 168/**
142 * Apply the function for each child of the container breadth first. 169 * Apply the function for each descendant of the container breadth first.
143 */ 170 */
144void container_for_each_descendant_bfs(struct sway_container *container, 171void container_for_each_descendant_bfs(struct sway_container *container,
145 void (*f)(struct sway_container *container, void *data), void *data); 172 void (*f)(struct sway_container *container, void *data), void *data);
@@ -150,7 +177,16 @@ void container_for_each_descendant_bfs(struct sway_container *container,
150void container_for_each_descendant_dfs(struct sway_container *container, 177void container_for_each_descendant_dfs(struct sway_container *container,
151 void (*f)(struct sway_container *container, void *data), void *data); 178 void (*f)(struct sway_container *container, void *data), void *data);
152 179
153bool container_has_anscestor(struct sway_container *descendant, 180/**
181 * Returns true if the given container is an ancestor of this container.
182 */
183bool container_has_anscestor(struct sway_container *container,
154 struct sway_container *anscestor); 184 struct sway_container *anscestor);
155 185
186/**
187 * Returns true if the given container is a child descendant of this container.
188 */
189bool container_has_child(struct sway_container *con,
190 struct sway_container *child);
191
156#endif 192#endif
diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h
index e1034657..fbc3d6af 100644
--- a/include/sway/tree/layout.h
+++ b/include/sway/tree/layout.h
@@ -29,31 +29,43 @@ struct sway_root {
29 29
30void layout_init(void); 30void layout_init(void);
31 31
32// TODO move to tree.h
32void container_add_child(struct sway_container *parent, 33void container_add_child(struct sway_container *parent,
33 struct sway_container *child); 34 struct sway_container *child);
34 35
36// TODO move to tree.h
35struct sway_container *container_add_sibling(struct sway_container *parent, 37struct sway_container *container_add_sibling(struct sway_container *parent,
36 struct sway_container *child); 38 struct sway_container *child);
37 39
40// TODO move to tree.h
38struct sway_container *container_remove_child(struct sway_container *child); 41struct sway_container *container_remove_child(struct sway_container *child);
39 42
43// TODO PRIVATE in tree.h
40struct sway_container *container_reap_empty(struct sway_container *container); 44struct sway_container *container_reap_empty(struct sway_container *container);
41 45
46// TODO move to tree.h
42void container_move_to(struct sway_container* container, 47void container_move_to(struct sway_container* container,
43 struct sway_container* destination); 48 struct sway_container* destination);
44 49
45void container_move(struct sway_container *container, 50void container_move(struct sway_container *container,
46 enum movement_direction dir, int move_amt); 51 enum movement_direction dir, int move_amt);
47 52
53// TODO move to output.c
48enum sway_container_layout container_get_default_layout( 54enum sway_container_layout container_get_default_layout(
49 struct sway_container *output); 55 struct sway_container *output);
50 56
57// TODO move to output.c
51void container_sort_workspaces(struct sway_container *output); 58void container_sort_workspaces(struct sway_container *output);
52 59
53void arrange_windows(struct sway_container *container, 60void arrange_windows(struct sway_container *container,
54 double width, double height); 61 double width, double height);
55 62
63// TODO move to container.h
56struct sway_container *container_get_in_direction(struct sway_container 64struct sway_container *container_get_in_direction(struct sway_container
57 *container, struct sway_seat *seat, enum movement_direction dir); 65 *container, struct sway_seat *seat, enum movement_direction dir);
58 66
67// TODO move to tree.h
68struct sway_container *container_split(struct sway_container *child,
69 enum sway_container_layout layout);
70
59#endif 71#endif
diff --git a/sway/commands.c b/sway/commands.c
index 9ba252b0..8156a08e 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -164,6 +164,10 @@ static struct cmd_handler command_handlers[] = {
164 { "layout", cmd_layout }, 164 { "layout", cmd_layout },
165 { "move", cmd_move }, 165 { "move", cmd_move },
166 { "reload", cmd_reload }, 166 { "reload", cmd_reload },
167 { "split", cmd_split },
168 { "splith", cmd_splith },
169 { "splitt", cmd_splitt },
170 { "splitv", cmd_splitv },
167}; 171};
168 172
169static int handler_compare(const void *_a, const void *_b) { 173static int handler_compare(const void *_a, const void *_b) {
diff --git a/sway/commands/kill.c b/sway/commands/kill.c
index f6774767..46d6e98e 100644
--- a/sway/commands/kill.c
+++ b/sway/commands/kill.c
@@ -3,21 +3,28 @@
3#include "sway/input/input-manager.h" 3#include "sway/input/input-manager.h"
4#include "sway/input/seat.h" 4#include "sway/input/seat.h"
5#include "sway/tree/view.h" 5#include "sway/tree/view.h"
6#include "sway/tree/container.h"
6#include "sway/commands.h" 7#include "sway/commands.h"
7 8
8struct cmd_results *cmd_kill(int argc, char **argv) { 9struct cmd_results *cmd_kill(int argc, char **argv) {
9 enum sway_container_type type = config->handler_context.current_container->type; 10 struct sway_container *con =
10 if (type != C_VIEW && type != C_CONTAINER) { 11 config->handler_context.current_container;
12
13 switch (con->type) {
14 case C_ROOT:
15 case C_OUTPUT:
16 case C_WORKSPACE:
17 case C_TYPES:
11 return cmd_results_new(CMD_INVALID, NULL, 18 return cmd_results_new(CMD_INVALID, NULL,
12 "Can only kill views and containers with this command"); 19 "Can only kill views and containers with this command");
13 } 20 break;
14 21 case C_CONTAINER:
15 // TODO close arbitrary containers without a view 22 con = container_destroy(con);
16 struct sway_view *view = 23 arrange_windows(con, -1, -1);
17 config->handler_context.current_container->sway_view; 24 break;
18 25 case C_VIEW:
19 if (view) { 26 view_close(con->sway_view);
20 view_close(view); 27 break;
21 } 28 }
22 29
23 return cmd_results_new(CMD_SUCCESS, NULL, NULL); 30 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
diff --git a/sway/commands/split.c b/sway/commands/split.c
new file mode 100644
index 00000000..ca116aef
--- /dev/null
+++ b/sway/commands/split.c
@@ -0,0 +1,87 @@
1#include <string.h>
2#include <strings.h>
3#include "sway/commands.h"
4#include "sway/tree/container.h"
5#include "sway/tree/layout.h"
6#include "sway/tree/view.h"
7#include "sway/input/input-manager.h"
8#include "sway/input/seat.h"
9#include "log.h"
10
11static struct cmd_results *_do_split(int argc, char **argv, int layout) {
12 char *name = layout == L_VERT ? "splitv" :
13 layout == L_HORIZ ? "splith" : "split";
14 struct cmd_results *error = NULL;
15 if (config->reading) {
16 return cmd_results_new(CMD_FAILURE, name,
17 "Can't be used in config file.");
18 }
19 if (!config->active) {
20 return cmd_results_new(CMD_FAILURE, name,
21 "Can only be used when sway is running.");
22 }
23 if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 0))) {
24 return error;
25 }
26
27 struct sway_container *focused = config->handler_context.current_container;
28 struct sway_container *parent = container_split(focused, layout);
29 arrange_windows(parent, -1, -1);
30
31 // TODO borders: update borders
32
33 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
34}
35
36struct cmd_results *cmd_split(int argc, char **argv) {
37 struct cmd_results *error = NULL;
38 if (config->reading) {
39 return cmd_results_new(CMD_FAILURE, "split",
40 "Can't be used in config file.");
41 }
42 if (!config->active) {
43 return cmd_results_new(CMD_FAILURE, "split",
44 "Can only be used when sway is running.");
45 }
46 if ((error = checkarg(argc, "split", EXPECTED_EQUAL_TO, 1))) {
47 return error;
48 }
49 if (strcasecmp(argv[0], "v") == 0 || strcasecmp(argv[0], "vertical") == 0) {
50 _do_split(argc - 1, argv + 1, L_VERT);
51 } else if (strcasecmp(argv[0], "h") == 0 ||
52 strcasecmp(argv[0], "horizontal") == 0) {
53 _do_split(argc - 1, argv + 1, L_HORIZ);
54 } else if (strcasecmp(argv[0], "t") == 0 ||
55 strcasecmp(argv[0], "toggle") == 0) {
56 struct sway_container *focused =
57 config->handler_context.current_container;
58
59 if (focused->parent->layout == L_VERT) {
60 _do_split(argc - 1, argv + 1, L_HORIZ);
61 } else {
62 _do_split(argc - 1, argv + 1, L_VERT);
63 }
64 } else {
65 error = cmd_results_new(CMD_FAILURE, "split",
66 "Invalid split command (expected either horizontal or vertical).");
67 return error;
68 }
69 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
70}
71
72struct cmd_results *cmd_splitv(int argc, char **argv) {
73 return _do_split(argc, argv, L_VERT);
74}
75
76struct cmd_results *cmd_splith(int argc, char **argv) {
77 return _do_split(argc, argv, L_HORIZ);
78}
79
80struct cmd_results *cmd_splitt(int argc, char **argv) {
81 struct sway_container *focused = config->handler_context.current_container;
82 if (focused->parent->layout == L_VERT) {
83 return _do_split(argc, argv, L_HORIZ);
84 } else {
85 return _do_split(argc, argv, L_VERT);
86 }
87}
diff --git a/sway/input/seat.c b/sway/input/seat.c
index 27636c1e..c41f7b2e 100644
--- a/sway/input/seat.c
+++ b/sway/input/seat.c
@@ -1,4 +1,5 @@
1#define _XOPEN_SOURCE 700 1#define _XOPEN_SOURCE 700
2#include <assert.h>
2#include <wlr/types/wlr_cursor.h> 3#include <wlr/types/wlr_cursor.h>
3#include <wlr/types/wlr_output_layout.h> 4#include <wlr/types/wlr_output_layout.h>
4#include <wlr/types/wlr_xcursor_manager.h> 5#include <wlr/types/wlr_xcursor_manager.h>
@@ -35,31 +36,89 @@ void seat_destroy(struct sway_seat *seat) {
35 wlr_seat_destroy(seat->wlr_seat); 36 wlr_seat_destroy(seat->wlr_seat);
36} 37}
37 38
39static struct sway_seat_container *seat_container_from_container(
40 struct sway_seat *seat, struct sway_container *con);
41
42static void seat_container_destroy(struct sway_seat_container *seat_con) {
43 struct sway_container *con = seat_con->container;
44 struct sway_container *child = NULL;
45
46 if (con->children != NULL) {
47 for (int i = 0; i < con->children->length; ++i) {
48 child = con->children->items[i];
49 struct sway_seat_container *seat_child =
50 seat_container_from_container(seat_con->seat, child);
51 seat_container_destroy(seat_child);
52 }
53 }
54
55 wl_list_remove(&seat_con->destroy.link);
56 wl_list_remove(&seat_con->link);
57 free(seat_con);
58}
59
60static void seat_send_focus(struct sway_seat *seat,
61 struct sway_container *con) {
62 if (con->type != C_VIEW) {
63 return;
64 }
65 struct sway_view *view = con->sway_view;
66 if (view->type == SWAY_XWAYLAND_VIEW) {
67 struct wlr_xwayland *xwayland =
68 seat->input->server->xwayland;
69 wlr_xwayland_set_seat(xwayland, seat->wlr_seat);
70 }
71 view_set_activated(view, true);
72 struct wlr_keyboard *keyboard =
73 wlr_seat_get_keyboard(seat->wlr_seat);
74 if (keyboard) {
75 wlr_seat_keyboard_notify_enter(seat->wlr_seat,
76 view->surface, keyboard->keycodes,
77 keyboard->num_keycodes, &keyboard->modifiers);
78 } else {
79 wlr_seat_keyboard_notify_enter(
80 seat->wlr_seat, view->surface, NULL, 0, NULL);
81 }
82
83}
84
38static void handle_seat_container_destroy(struct wl_listener *listener, 85static void handle_seat_container_destroy(struct wl_listener *listener,
39 void *data) { 86 void *data) {
40 struct sway_seat_container *seat_con = 87 struct sway_seat_container *seat_con =
41 wl_container_of(listener, seat_con, destroy); 88 wl_container_of(listener, seat_con, destroy);
42 struct sway_seat *seat = seat_con->seat; 89 struct sway_seat *seat = seat_con->seat;
43 struct sway_container *con = seat_con->container; 90 struct sway_container *con = seat_con->container;
91 struct sway_container *parent = con->parent;
92 struct sway_container *focus = seat_get_focus(seat);
44 93
45 bool is_focus = (seat_get_focus(seat) == con); 94 bool set_focus =
95 focus != NULL &&
96 (focus == con || container_has_child(con, focus)) &&
97 con->type != C_WORKSPACE;
46 98
47 wl_list_remove(&seat_con->link); 99 seat_container_destroy(seat_con);
48 100
49 if (is_focus) { 101 if (set_focus) {
50 // pick next focus 102 struct sway_container *next_focus = NULL;
51 seat_set_focus(seat, NULL); 103 while (next_focus == NULL) {
52 struct sway_container *next = 104 next_focus = seat_get_focus_by_type(seat, parent, C_VIEW);
53 seat_get_focus_inactive(seat, con->parent);
54 if (next == NULL) {
55 next = con->parent;
56 }
57 seat_set_focus(seat, next);
58 }
59 105
60 wl_list_remove(&seat_con->destroy.link); 106 if (next_focus == NULL && parent->type == C_WORKSPACE) {
107 next_focus = parent;
108 break;
109 }
61 110
62 free(seat_con); 111 parent = parent->parent;
112 }
113
114 // the structure change might have caused it to move up to the top of
115 // the focus stack without sending focus notifications to the view
116 if (seat_get_focus(seat) == next_focus) {
117 seat_send_focus(seat, next_focus);
118 } else {
119 seat_set_focus(seat, next_focus);
120 }
121 }
63} 122}
64 123
65static struct sway_seat_container *seat_container_from_container( 124static struct sway_seat_container *seat_container_from_container(
@@ -310,23 +369,7 @@ void seat_set_focus_warp(struct sway_seat *seat,
310 wl_list_insert(&seat->focus_stack, &seat_con->link); 369 wl_list_insert(&seat->focus_stack, &seat_con->link);
311 370
312 if (container->type == C_VIEW) { 371 if (container->type == C_VIEW) {
313 struct sway_view *view = container->sway_view; 372 seat_send_focus(seat, container);
314 view_set_activated(view, true);
315 if (view->type == SWAY_XWAYLAND_VIEW) {
316 struct wlr_xwayland *xwayland =
317 seat->input->server->xwayland;
318 wlr_xwayland_set_seat(xwayland, seat->wlr_seat);
319 }
320 struct wlr_keyboard *keyboard =
321 wlr_seat_get_keyboard(seat->wlr_seat);
322 if (keyboard) {
323 wlr_seat_keyboard_notify_enter(seat->wlr_seat,
324 view->surface, keyboard->keycodes,
325 keyboard->num_keycodes, &keyboard->modifiers);
326 } else {
327 wlr_seat_keyboard_notify_enter(
328 seat->wlr_seat, view->surface, NULL, 0, NULL);
329 }
330 } 373 }
331 } 374 }
332 375
@@ -378,17 +421,31 @@ void seat_set_focus(struct sway_seat *seat,
378 421
379struct sway_container *seat_get_focus_inactive(struct sway_seat *seat, 422struct sway_container *seat_get_focus_inactive(struct sway_seat *seat,
380 struct sway_container *container) { 423 struct sway_container *container) {
424 return seat_get_focus_by_type(seat, container, C_TYPES);
425}
426
427struct sway_container *sway_seat_get_focus(struct sway_seat *seat) {
428 if (!seat->has_focus) {
429 return NULL;
430 }
431 return seat_get_focus_inactive(seat, &root_container);
432}
433
434struct sway_container *seat_get_focus_by_type(struct sway_seat *seat,
435 struct sway_container *container, enum sway_container_type type) {
381 struct sway_seat_container *current = NULL; 436 struct sway_seat_container *current = NULL;
382 struct sway_container *parent = NULL; 437 struct sway_container *parent = NULL;
383 wl_list_for_each(current, &seat->focus_stack, link) { 438 wl_list_for_each(current, &seat->focus_stack, link) {
384 parent = current->container->parent; 439 parent = current->container->parent;
385 440
386 if (current->container == container) { 441 if (current->container == container &&
442 (type == C_TYPES || container->type == type)) {
387 return current->container; 443 return current->container;
388 } 444 }
389 445
390 while (parent) { 446 while (parent) {
391 if (parent == container) { 447 if (parent == container && (type == C_TYPES ||
448 current->container->type == type)) {
392 return current->container; 449 return current->container;
393 } 450 }
394 parent = parent->parent; 451 parent = parent->parent;
@@ -405,17 +462,6 @@ struct sway_container *seat_get_focus(struct sway_seat *seat) {
405 return seat_get_focus_inactive(seat, &root_container); 462 return seat_get_focus_inactive(seat, &root_container);
406} 463}
407 464
408struct sway_container *seat_get_focus_by_type(struct sway_seat *seat,
409 enum sway_container_type type) {
410 struct sway_container *focus =
411 seat_get_focus_inactive(seat, &root_container);
412 if (focus->type == type) {
413 return focus;
414 }
415
416 return container_parent(focus, type);
417}
418
419void seat_apply_config(struct sway_seat *seat, 465void seat_apply_config(struct sway_seat *seat,
420 struct seat_config *seat_config) { 466 struct seat_config *seat_config) {
421 struct sway_seat_device *seat_device = NULL; 467 struct sway_seat_device *seat_device = NULL;
diff --git a/sway/main.c b/sway/main.c
index ded922ee..e7f8ddd3 100644
--- a/sway/main.c
+++ b/sway/main.c
@@ -118,7 +118,7 @@ void run_as_ipc_client(char *command, char *socket_path) {
118static void log_env() { 118static void log_env() {
119 const char *log_vars[] = { 119 const char *log_vars[] = {
120 "PATH", 120 "PATH",
121 "LD_LOAD_PATH", 121 "LD_LIBRARY_PATH",
122 "LD_PRELOAD_PATH", 122 "LD_PRELOAD_PATH",
123 "LD_LIBRARY_PATH", 123 "LD_LIBRARY_PATH",
124 "SWAY_CURSOR_THEME", 124 "SWAY_CURSOR_THEME",
diff --git a/sway/meson.build b/sway/meson.build
index 38945b4c..a6a633a0 100644
--- a/sway/meson.build
+++ b/sway/meson.build
@@ -19,6 +19,7 @@ sway_sources = files(
19 'commands/input.c', 19 'commands/input.c',
20 'commands/layout.c', 20 'commands/layout.c',
21 'commands/mode.c', 21 'commands/mode.c',
22 'commands/split.c',
22 'commands/move.c', 23 'commands/move.c',
23 'commands/seat.c', 24 'commands/seat.c',
24 'commands/seat/attach.c', 25 'commands/seat/attach.c',
diff --git a/sway/tree/container.c b/sway/tree/container.c
index ea0b7d5c..4db93ce8 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -55,7 +55,7 @@ static void notify_new_container(struct sway_container *container) {
55 ipc_event_window(container, "new"); 55 ipc_event_window(container, "new");
56} 56}
57 57
58static struct sway_container *container_create(enum sway_container_type type) { 58struct sway_container *container_create(enum sway_container_type type) {
59 // next id starts at 1 because 0 is assigned to root_container in layout.c 59 // next id starts at 1 because 0 is assigned to root_container in layout.c
60 static size_t next_id = 1; 60 static size_t next_id = 1;
61 struct sway_container *c = calloc(1, sizeof(struct sway_container)); 61 struct sway_container *c = calloc(1, sizeof(struct sway_container));
@@ -76,7 +76,7 @@ static struct sway_container *container_create(enum sway_container_type type) {
76 return c; 76 return c;
77} 77}
78 78
79struct sway_container *container_destroy(struct sway_container *cont) { 79static struct sway_container *_container_destroy(struct sway_container *cont) {
80 if (cont == NULL) { 80 if (cont == NULL) {
81 return NULL; 81 return NULL;
82 } 82 }
@@ -84,13 +84,14 @@ struct sway_container *container_destroy(struct sway_container *cont) {
84 wl_signal_emit(&cont->events.destroy, cont); 84 wl_signal_emit(&cont->events.destroy, cont);
85 85
86 struct sway_container *parent = cont->parent; 86 struct sway_container *parent = cont->parent;
87 if (cont->children) { 87 if (cont->children != NULL) {
88 // remove children until there are no more, container_destroy calls 88 // remove children until there are no more, container_destroy calls
89 // container_remove_child, which removes child from this container 89 // container_remove_child, which removes child from this container
90 while (cont->children->length) { 90 while (cont->children != NULL && cont->children->length != 0) {
91 container_destroy(cont->children->items[0]); 91 struct sway_container *child = cont->children->items[0];
92 container_remove_child(child);
93 container_destroy(child);
92 } 94 }
93 list_free(cont->children);
94 } 95 }
95 if (cont->marks) { 96 if (cont->marks) {
96 list_foreach(cont->marks, free); 97 list_foreach(cont->marks, free);
@@ -102,10 +103,19 @@ struct sway_container *container_destroy(struct sway_container *cont) {
102 if (cont->name) { 103 if (cont->name) {
103 free(cont->name); 104 free(cont->name);
104 } 105 }
106 list_free(cont->children);
107 cont->children = NULL;
105 free(cont); 108 free(cont);
106 return parent; 109 return parent;
107} 110}
108 111
112struct sway_container *container_destroy(struct sway_container *cont) {
113 struct sway_container *parent = _container_destroy(cont);
114 parent = container_reap_empty(parent);
115 arrange_windows(&root_container, -1, -1);
116 return parent;
117}
118
109struct sway_container *container_output_create( 119struct sway_container *container_output_create(
110 struct sway_output *sway_output) { 120 struct sway_output *sway_output) {
111 struct wlr_box size; 121 struct wlr_box size;
@@ -413,3 +423,17 @@ bool container_has_anscestor(struct sway_container *descendant,
413 } 423 }
414 return false; 424 return false;
415} 425}
426
427static bool find_child_func(struct sway_container *con, void *data) {
428 struct sway_container *child = data;
429 return con == child;
430}
431
432bool container_has_child(struct sway_container *con,
433 struct sway_container *child) {
434 if (child == NULL || child->type == C_VIEW ||
435 child->children->length == 0) {
436 return false;
437 }
438 return container_find(con, find_child_func, child);
439}
diff --git a/sway/tree/layout.c b/sway/tree/layout.c
index 62ae0603..88463e3b 100644
--- a/sway/tree/layout.c
+++ b/sway/tree/layout.c
@@ -1,4 +1,5 @@
1#define _POSIX_C_SOURCE 200809L 1#define _POSIX_C_SOURCE 200809L
2#include <assert.h>
2#include <ctype.h> 3#include <ctype.h>
3#include <math.h> 4#include <math.h>
4#include <stdbool.h> 5#include <stdbool.h>
@@ -103,7 +104,7 @@ void container_add_child(struct sway_container *parent,
103} 104}
104 105
105struct sway_container *container_reap_empty(struct sway_container *container) { 106struct sway_container *container_reap_empty(struct sway_container *container) {
106 if (!sway_assert(container, "reaping null container")) { 107 if (container == NULL) {
107 return NULL; 108 return NULL;
108 } 109 }
109 wlr_log(L_DEBUG, "Reaping %p %s '%s'", container, 110 wlr_log(L_DEBUG, "Reaping %p %s '%s'", container,
@@ -137,7 +138,7 @@ struct sway_container *container_remove_child(struct sway_container *child) {
137 } 138 }
138 } 139 }
139 child->parent = NULL; 140 child->parent = NULL;
140 return container_reap_empty(parent); 141 return parent;
141} 142}
142 143
143void container_move_to(struct sway_container *container, 144void container_move_to(struct sway_container *container,
@@ -348,7 +349,12 @@ static void apply_horiz_layout(struct sway_container *container,
348 wlr_log(L_DEBUG, 349 wlr_log(L_DEBUG,
349 "Calculating arrangement for %p:%d (will scale %f by %f)", 350 "Calculating arrangement for %p:%d (will scale %f by %f)",
350 child, child->type, width, scale); 351 child, child->type, width, scale);
351 view_set_position(child->sway_view, child_x, y); 352 if (child->type == C_VIEW) {
353 view_set_position(child->sway_view, child_x, y);
354 } else {
355 child->x = child_x;
356 child->y = y;
357 }
352 358
353 if (i == end - 1) { 359 if (i == end - 1) {
354 double remaining_width = x + width - child_x; 360 double remaining_width = x + width - child_x;
@@ -399,7 +405,12 @@ void apply_vert_layout(struct sway_container *container,
399 wlr_log(L_DEBUG, 405 wlr_log(L_DEBUG,
400 "Calculating arrangement for %p:%d (will scale %f by %f)", 406 "Calculating arrangement for %p:%d (will scale %f by %f)",
401 child, child->type, height, scale); 407 child, child->type, height, scale);
402 view_set_position(child->sway_view, x, child_y); 408 if (child->type == C_VIEW) {
409 view_set_position(child->sway_view, x, child_y);
410 } else {
411 child->x = x;
412 child->y = child_y;
413 }
403 414
404 if (i == end - 1) { 415 if (i == end - 1) {
405 double remaining_height = y + height - child_y; 416 double remaining_height = y + height - child_y;
@@ -531,9 +542,9 @@ static struct sway_container *sway_output_from_wlr(struct wlr_output *output) {
531 return NULL; 542 return NULL;
532} 543}
533 544
534static struct sway_container *get_swayc_in_direction_under( 545struct sway_container *container_get_in_direction(
535 struct sway_container *container, enum movement_direction dir, 546 struct sway_container *container, struct sway_seat *seat,
536 struct sway_seat *seat, struct sway_container *limit) { 547 enum movement_direction dir) {
537 if (dir == MOVE_CHILD) { 548 if (dir == MOVE_CHILD) {
538 return seat_get_focus_inactive(seat, container); 549 return seat_get_focus_inactive(seat, container);
539 } 550 }
@@ -565,7 +576,6 @@ static struct sway_container *get_swayc_in_direction_under(
565 576
566 struct sway_container *wrap_candidate = NULL; 577 struct sway_container *wrap_candidate = NULL;
567 while (true) { 578 while (true) {
568 // Test if we can even make a difference here
569 bool can_move = false; 579 bool can_move = false;
570 int desired; 580 int desired;
571 int idx = index_child(container); 581 int idx = index_child(container);
@@ -595,7 +605,7 @@ static struct sway_container *get_swayc_in_direction_under(
595 } 605 }
596 if (next->children && next->children->length) { 606 if (next->children && next->children->length) {
597 // TODO consider floating children as well 607 // TODO consider floating children as well
598 return seat_get_focus_inactive(seat, next); 608 return seat_get_focus_by_type(seat, next, C_VIEW);
599 } else { 609 } else {
600 return next; 610 return next;
601 } 611 }
@@ -625,21 +635,23 @@ static struct sway_container *get_swayc_in_direction_under(
625 wrap_candidate = parent->children->items[0]; 635 wrap_candidate = parent->children->items[0];
626 } 636 }
627 if (config->force_focus_wrapping) { 637 if (config->force_focus_wrapping) {
628 return wrap_candidate; 638 return seat_get_focus_by_type(seat,
639 wrap_candidate, C_VIEW);
629 } 640 }
630 } 641 }
631 } else { 642 } else {
632 wlr_log(L_DEBUG, 643 wlr_log(L_DEBUG,
633 "cont %d-%p dir %i sibling %d: %p", idx, 644 "cont %d-%p dir %i sibling %d: %p", idx,
634 container, dir, desired, parent->children->items[desired]); 645 container, dir, desired, parent->children->items[desired]);
635 return parent->children->items[desired]; 646 return seat_get_focus_by_type(seat,
647 parent->children->items[desired], C_VIEW);
636 } 648 }
637 } 649 }
638 650
639 if (!can_move) { 651 if (!can_move) {
640 container = parent; 652 container = parent;
641 parent = parent->parent; 653 parent = parent->parent;
642 if (!parent || container == limit) { 654 if (!parent) {
643 // wrapping is the last chance 655 // wrapping is the last chance
644 return wrap_candidate; 656 return wrap_candidate;
645 } 657 }
@@ -647,8 +659,70 @@ static struct sway_container *get_swayc_in_direction_under(
647 } 659 }
648} 660}
649 661
650struct sway_container *container_get_in_direction( 662struct sway_container *container_replace_child(struct sway_container *child,
651 struct sway_container *container, struct sway_seat *seat, 663 struct sway_container *new_child) {
652 enum movement_direction dir) { 664 struct sway_container *parent = child->parent;
653 return get_swayc_in_direction_under(container, dir, seat, NULL); 665 if (parent == NULL) {
666 return NULL;
667 }
668 int i = index_child(child);
669
670 // TODO floating
671 parent->children->items[i] = new_child;
672 new_child->parent = parent;
673 child->parent = NULL;
674
675 // Set geometry for new child
676 new_child->x = child->x;
677 new_child->y = child->y;
678 new_child->width = child->width;
679 new_child->height = child->height;
680
681 // reset geometry for child
682 child->width = 0;
683 child->height = 0;
684
685 return parent;
686}
687
688struct sway_container *container_split(struct sway_container *child,
689 enum sway_container_layout layout) {
690 // TODO floating: cannot split a floating container
691 if (!sway_assert(child, "child cannot be null")) {
692 return NULL;
693 }
694 struct sway_container *cont = container_create(C_CONTAINER);
695
696 wlr_log(L_DEBUG, "creating container %p around %p", cont, child);
697
698 cont->prev_layout = L_NONE;
699 cont->width = child->width;
700 cont->height = child->height;
701 cont->x = child->x;
702 cont->y = child->y;
703
704 if (child->type == C_WORKSPACE) {
705 struct sway_seat *seat = input_manager_get_default_seat(input_manager);
706 struct sway_container *workspace = child;
707 bool set_focus = (seat_get_focus(seat) == workspace);
708
709 while (workspace->children->length) {
710 struct sway_container *ws_child = workspace->children->items[0];
711 container_remove_child(ws_child);
712 container_add_child(cont, ws_child);
713 }
714
715 container_add_child(workspace, cont);
716 container_set_layout(workspace, layout);
717
718 if (set_focus) {
719 seat_set_focus(seat, cont);
720 }
721 } else {
722 cont->layout = layout;
723 container_replace_child(child, cont);
724 container_add_child(cont, child);
725 }
726
727 return cont;
654} 728}