aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2018-04-07 11:19:25 -0400
committerLibravatar GitHub <noreply@github.com>2018-04-07 11:19:25 -0400
commitf5a9bd5cb75b37376b98eadbff2facb7e0021d57 (patch)
treef72219a5a2adec3380da5269fee025715f7bf333
parentMerge pull request #1762 from DanySpin97/wlroots (diff)
parentDon't rejigger if parent has two children (diff)
downloadsway-f5a9bd5cb75b37376b98eadbff2facb7e0021d57.tar.gz
sway-f5a9bd5cb75b37376b98eadbff2facb7e0021d57.tar.zst
sway-f5a9bd5cb75b37376b98eadbff2facb7e0021d57.zip
Merge pull request #1700 from swaywm/move-cmd-full
Implement move [left|right|up|down]
-rw-r--r--include/sway/debug.h7
-rw-r--r--include/sway/tree/container.h7
-rw-r--r--include/sway/tree/layout.h4
-rw-r--r--sway/commands/move.c4
-rw-r--r--sway/debug-tree.c119
-rw-r--r--sway/desktop/output.c5
-rw-r--r--sway/input/seat.c3
-rw-r--r--sway/main.c6
-rw-r--r--sway/meson.build4
-rw-r--r--sway/tree/container.c46
-rw-r--r--sway/tree/layout.c342
-rw-r--r--swaybar/ipc.c2
12 files changed, 501 insertions, 48 deletions
diff --git a/include/sway/debug.h b/include/sway/debug.h
new file mode 100644
index 00000000..2430d319
--- /dev/null
+++ b/include/sway/debug.h
@@ -0,0 +1,7 @@
1#ifndef SWAY_DEBUG_H
2#define SWAY_DEBUG_H
3
4extern bool enable_debug_tree;
5void update_debug_tree();
6
7#endif
diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h
index 4c60530f..2a8b8aba 100644
--- a/include/sway/tree/container.h
+++ b/include/sway/tree/container.h
@@ -182,4 +182,11 @@ void container_create_notify(struct sway_container *container);
182 182
183void container_damage_whole(struct sway_container *container); 183void container_damage_whole(struct sway_container *container);
184 184
185bool container_reap_empty(struct sway_container *con);
186
187struct sway_container *container_reap_empty_recursive(
188 struct sway_container *con);
189
190struct sway_container *container_flatten(struct sway_container *container);
191
185#endif 192#endif
diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h
index fc5ce21f..49ae00e4 100644
--- a/include/sway/tree/layout.h
+++ b/include/sway/tree/layout.h
@@ -1,7 +1,7 @@
1#ifndef _SWAY_LAYOUT_H 1#ifndef _SWAY_LAYOUT_H
2#define _SWAY_LAYOUT_H 2#define _SWAY_LAYOUT_H
3
4#include <wlr/types/wlr_output_layout.h> 3#include <wlr/types/wlr_output_layout.h>
4#include <wlr/render/wlr_texture.h>
5#include "sway/tree/container.h" 5#include "sway/tree/container.h"
6 6
7enum movement_direction { 7enum movement_direction {
@@ -29,6 +29,8 @@ struct sway_root {
29 29
30 struct wl_list xwayland_unmanaged; // sway_xwayland_unmanaged::link 30 struct wl_list xwayland_unmanaged; // sway_xwayland_unmanaged::link
31 31
32 struct wlr_texture *debug_tree;
33
32 struct { 34 struct {
33 struct wl_signal new_container; 35 struct wl_signal new_container;
34 } events; 36 } events;
diff --git a/sway/commands/move.c b/sway/commands/move.c
index c954ab94..15a5ebc4 100644
--- a/sway/commands/move.c
+++ b/sway/commands/move.c
@@ -82,6 +82,8 @@ static struct cmd_results *cmd_move_container(struct sway_container *current,
82 config->handler_context.seat, ws); 82 config->handler_context.seat, ws);
83 container_move_to(current, focus); 83 container_move_to(current, focus);
84 seat_set_focus(config->handler_context.seat, old_parent); 84 seat_set_focus(config->handler_context.seat, old_parent);
85 container_reap_empty(old_parent);
86 container_reap_empty(focus->parent);
85 return cmd_results_new(CMD_SUCCESS, NULL, NULL); 87 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
86 } else if (strcasecmp(argv[1], "to") == 0 88 } else if (strcasecmp(argv[1], "to") == 0
87 && strcasecmp(argv[2], "output") == 0) { 89 && strcasecmp(argv[2], "output") == 0) {
@@ -109,6 +111,8 @@ static struct cmd_results *cmd_move_container(struct sway_container *current,
109 struct sway_container *old_parent = current->parent; 111 struct sway_container *old_parent = current->parent;
110 container_move_to(current, focus); 112 container_move_to(current, focus);
111 seat_set_focus(config->handler_context.seat, old_parent); 113 seat_set_focus(config->handler_context.seat, old_parent);
114 container_reap_empty(old_parent);
115 container_reap_empty(focus->parent);
112 return cmd_results_new(CMD_SUCCESS, NULL, NULL); 116 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
113 } 117 }
114 return cmd_results_new(CMD_INVALID, "move", expected_syntax); 118 return cmd_results_new(CMD_INVALID, "move", expected_syntax);
diff --git a/sway/debug-tree.c b/sway/debug-tree.c
new file mode 100644
index 00000000..ae0a1869
--- /dev/null
+++ b/sway/debug-tree.c
@@ -0,0 +1,119 @@
1#include <pango/pangocairo.h>
2#include <wlr/backend.h>
3#include <wlr/render/wlr_texture.h>
4#include <wlr/util/log.h>
5#include "config.h"
6#include "sway/input/input-manager.h"
7#include "sway/input/seat.h"
8#include "sway/server.h"
9#include "sway/tree/container.h"
10#include "sway/tree/layout.h"
11#include "cairo.h"
12#include "config.h"
13#include "pango.h"
14
15static const char *layout_to_str(enum sway_container_layout layout) {
16 switch (layout) {
17 case L_HORIZ:
18 return "L_HORIZ";
19 case L_VERT:
20 return "L_VERT";
21 case L_STACKED:
22 return "L_STACKED";
23 case L_TABBED:
24 return "L_TABBED";
25 case L_FLOATING:
26 return "L_FLOATING";
27 case L_NONE:
28 default:
29 return "L_NONE";
30 }
31}
32
33static int draw_container(cairo_t *cairo, struct sway_container *container,
34 struct sway_container *focus, int x, int y) {
35 int text_width, text_height;
36 get_text_size(cairo, "monospace", &text_width, &text_height,
37 1, false, "%s id:%zd '%s' %s %.fx%.f@%.f,%.f",
38 container_type_to_str(container->type), container->id, container->name,
39 layout_to_str(container->layout),
40 container->width, container->height, container->x, container->y);
41 cairo_save(cairo);
42 cairo_rectangle(cairo, x + 2, y, text_width - 2, text_height);
43 cairo_set_source_u32(cairo, 0xFFFFFFE0);
44 cairo_fill(cairo);
45 int height = text_height;
46 if (container->children) {
47 for (int i = 0; i < container->children->length; ++i) {
48 struct sway_container *child = container->children->items[i];
49 if (child->parent == container) {
50 cairo_set_source_u32(cairo, 0x000000FF);
51 } else {
52 cairo_set_source_u32(cairo, 0xFF0000FF);
53 }
54 height += draw_container(cairo, child, focus, x + 10, y + height);
55 }
56 }
57 cairo_set_source_u32(cairo, 0xFFFFFFE0);
58 cairo_rectangle(cairo, x, y, 2, height);
59 cairo_fill(cairo);
60 cairo_restore(cairo);
61 cairo_move_to(cairo, x, y);
62 if (focus == container) {
63 cairo_set_source_u32(cairo, 0x0000FFFF);
64 }
65 pango_printf(cairo, "monospace", 1, false, "%s id:%zd '%s' %s %.fx%.f@%.f,%.f",
66 container_type_to_str(container->type), container->id, container->name,
67 layout_to_str(container->layout),
68 container->width, container->height, container->x, container->y);
69 return height;
70}
71
72bool enable_debug_tree = false;
73
74void update_debug_tree() {
75 if (!enable_debug_tree) {
76 return;
77 }
78
79 int width = 640, height = 480;
80 for (int i = 0; i < root_container.children->length; ++i) {
81 struct sway_container *container = root_container.children->items[i];
82 if (container->width > width) {
83 width = container->width;
84 }
85 if (container->height > height) {
86 height = container->height;
87 }
88 }
89 cairo_surface_t *surface =
90 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
91 cairo_t *cairo = cairo_create(surface);
92 PangoContext *pango = pango_cairo_create_context(cairo);
93
94 struct sway_seat *seat = NULL;
95 wl_list_for_each(seat, &input_manager->seats, link) {
96 break;
97 }
98
99 struct sway_container *focus = NULL;
100 if (seat != NULL) {
101 focus = seat_get_focus(seat);
102 }
103 cairo_set_source_u32(cairo, 0x000000FF);
104 draw_container(cairo, &root_container, focus, 0, 0);
105
106 cairo_surface_flush(surface);
107 struct wlr_renderer *renderer = wlr_backend_get_renderer(server.backend);
108 if (root_container.sway_root->debug_tree) {
109 wlr_texture_destroy(root_container.sway_root->debug_tree);
110 }
111 unsigned char *data = cairo_image_surface_get_data(surface);
112 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
113 struct wlr_texture *texture = wlr_texture_from_pixels(renderer,
114 WL_SHM_FORMAT_ARGB8888, stride, width, height, data);
115 root_container.sway_root->debug_tree = texture;
116 cairo_surface_destroy(surface);
117 g_object_unref(pango);
118 cairo_destroy(cairo);
119}
diff --git a/sway/desktop/output.c b/sway/desktop/output.c
index aa18f1b8..ad777796 100644
--- a/sway/desktop/output.c
+++ b/sway/desktop/output.c
@@ -294,6 +294,11 @@ static void render_output(struct sway_output *output, struct timespec *when,
294 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]); 294 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]);
295 295
296renderer_end: 296renderer_end:
297 if (root_container.sway_root->debug_tree) {
298 wlr_render_texture(renderer, root_container.sway_root->debug_tree,
299 wlr_output->transform_matrix, 0, 0, 1);
300 }
301
297 wlr_renderer_end(renderer); 302 wlr_renderer_end(renderer);
298 if (!wlr_output_damage_swap_buffers(output->damage, when, damage)) { 303 if (!wlr_output_damage_swap_buffers(output->damage, when, damage)) {
299 return; 304 return;
diff --git a/sway/input/seat.c b/sway/input/seat.c
index ad3584a0..e8cf9824 100644
--- a/sway/input/seat.c
+++ b/sway/input/seat.c
@@ -5,6 +5,7 @@
5#include <wlr/types/wlr_cursor.h> 5#include <wlr/types/wlr_cursor.h>
6#include <wlr/types/wlr_output_layout.h> 6#include <wlr/types/wlr_output_layout.h>
7#include <wlr/types/wlr_xcursor_manager.h> 7#include <wlr/types/wlr_xcursor_manager.h>
8#include "sway/debug.h"
8#include "sway/tree/container.h" 9#include "sway/tree/container.h"
9#include "sway/tree/workspace.h" 10#include "sway/tree/workspace.h"
10#include "sway/input/seat.h" 11#include "sway/input/seat.h"
@@ -432,6 +433,8 @@ void seat_set_focus_warp(struct sway_seat *seat,
432 } 433 }
433 434
434 seat->has_focus = (container != NULL); 435 seat->has_focus = (container != NULL);
436
437 update_debug_tree();
435} 438}
436 439
437void seat_set_focus(struct sway_seat *seat, 440void seat_set_focus(struct sway_seat *seat,
diff --git a/sway/main.c b/sway/main.c
index e7f8ddd3..efb674b6 100644
--- a/sway/main.c
+++ b/sway/main.c
@@ -17,6 +17,7 @@
17#endif 17#endif
18#include <wlr/util/log.h> 18#include <wlr/util/log.h>
19#include "sway/config.h" 19#include "sway/config.h"
20#include "sway/debug.h"
20#include "sway/server.h" 21#include "sway/server.h"
21#include "sway/tree/layout.h" 22#include "sway/tree/layout.h"
22#include "sway/ipc-server.h" 23#include "sway/ipc-server.h"
@@ -288,7 +289,7 @@ int main(int argc, char **argv) {
288 int c; 289 int c;
289 while (1) { 290 while (1) {
290 int option_index = 0; 291 int option_index = 0;
291 c = getopt_long(argc, argv, "hCdvVc:", long_options, &option_index); 292 c = getopt_long(argc, argv, "hCdDvVc:", long_options, &option_index);
292 if (c == -1) { 293 if (c == -1) {
293 break; 294 break;
294 } 295 }
@@ -306,6 +307,9 @@ int main(int argc, char **argv) {
306 case 'd': // debug 307 case 'd': // debug
307 debug = 1; 308 debug = 1;
308 break; 309 break;
310 case 'D': // extended debug options
311 enable_debug_tree = true;
312 break;
309 case 'v': // version 313 case 'v': // version
310 fprintf(stdout, "sway version " SWAY_VERSION "\n"); 314 fprintf(stdout, "sway version " SWAY_VERSION "\n");
311 exit(EXIT_SUCCESS); 315 exit(EXIT_SUCCESS);
diff --git a/sway/meson.build b/sway/meson.build
index 29aaa7b7..1fe0f29a 100644
--- a/sway/meson.build
+++ b/sway/meson.build
@@ -4,6 +4,7 @@ sway_sources = files(
4 'commands.c', 4 'commands.c',
5 'config.c', 5 'config.c',
6 'criteria.c', 6 'criteria.c',
7 'debug-tree.c',
7 'ipc-json.c', 8 'ipc-json.c',
8 'ipc-server.c', 9 'ipc-server.c',
9 'security.c', 10 'security.c',
@@ -102,10 +103,13 @@ sway_sources = files(
102) 103)
103 104
104sway_deps = [ 105sway_deps = [
106 cairo,
107 gdk_pixbuf,
105 jsonc, 108 jsonc,
106 libcap, 109 libcap,
107 libinput, 110 libinput,
108 math, 111 math,
112 pango,
109 pcre, 113 pcre,
110 pixman, 114 pixman,
111 server_protos, 115 server_protos,
diff --git a/sway/tree/container.c b/sway/tree/container.c
index ab8363bc..ea1c93bb 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -208,7 +208,7 @@ static void container_root_finish(struct sway_container *con) {
208 wlr_log(L_ERROR, "TODO: destroy the root container"); 208 wlr_log(L_ERROR, "TODO: destroy the root container");
209} 209}
210 210
211static bool container_reap_empty(struct sway_container *con) { 211bool container_reap_empty(struct sway_container *con) {
212 switch (con->type) { 212 switch (con->type) {
213 case C_ROOT: 213 case C_ROOT:
214 case C_OUTPUT: 214 case C_OUTPUT:
@@ -225,14 +225,6 @@ static bool container_reap_empty(struct sway_container *con) {
225 if (con->children->length == 0) { 225 if (con->children->length == 0) {
226 _container_destroy(con); 226 _container_destroy(con);
227 return true; 227 return true;
228 } else if (con->children->length == 1) {
229 struct sway_container *child = con->children->items[0];
230 if (child->type == C_CONTAINER) {
231 container_remove_child(child);
232 container_replace_child(con, child);
233 _container_destroy(con);
234 return true;
235 }
236 } 228 }
237 case C_VIEW: 229 case C_VIEW:
238 break; 230 break;
@@ -245,6 +237,29 @@ static bool container_reap_empty(struct sway_container *con) {
245 return false; 237 return false;
246} 238}
247 239
240struct sway_container *container_reap_empty_recursive(
241 struct sway_container *con) {
242 while (con) {
243 struct sway_container *next = con->parent;
244 if (!container_reap_empty(con)) {
245 break;
246 }
247 con = next;
248 }
249 return con;
250}
251
252struct sway_container *container_flatten(struct sway_container *container) {
253 while (container->type == C_CONTAINER && container->children->length == 1) {
254 struct sway_container *child = container->children->items[0];
255 struct sway_container *parent = container->parent;
256 container_replace_child(container, child);
257 container_destroy(container);
258 container = parent;
259 }
260 return container;
261}
262
248struct sway_container *container_destroy(struct sway_container *con) { 263struct sway_container *container_destroy(struct sway_container *con) {
249 if (con == NULL) { 264 if (con == NULL) {
250 return NULL; 265 return NULL;
@@ -283,18 +298,7 @@ struct sway_container *container_destroy(struct sway_container *con) {
283 break; 298 break;
284 } 299 }
285 300
286 struct sway_container *tmp = parent; 301 return container_reap_empty_recursive(parent);
287 while (parent) {
288 tmp = parent->parent;
289
290 if (!container_reap_empty(parent)) {
291 break;
292 }
293
294 parent = tmp;
295 }
296
297 return tmp;
298} 302}
299 303
300static void container_close_func(struct sway_container *container, void *data) { 304static void container_close_func(struct sway_container *container, void *data) {
diff --git a/sway/tree/layout.c b/sway/tree/layout.c
index 343f349a..78af8b8c 100644
--- a/sway/tree/layout.c
+++ b/sway/tree/layout.c
@@ -6,6 +6,7 @@
6#include <string.h> 6#include <string.h>
7#include <wlr/types/wlr_output.h> 7#include <wlr/types/wlr_output.h>
8#include <wlr/types/wlr_output_layout.h> 8#include <wlr/types/wlr_output_layout.h>
9#include "sway/debug.h"
9#include "sway/tree/container.h" 10#include "sway/tree/container.h"
10#include "sway/tree/layout.h" 11#include "sway/tree/layout.h"
11#include "sway/output.h" 12#include "sway/output.h"
@@ -100,13 +101,31 @@ static int index_child(const struct sway_container *child) {
100 return i; 101 return i;
101} 102}
102 103
104void container_insert_child(struct sway_container *parent,
105 struct sway_container *child, int i) {
106 struct sway_container *old_parent = child->parent;
107 if (old_parent) {
108 container_remove_child(child);
109 }
110 wlr_log(L_DEBUG, "Inserting id:%zd at index %d", child->id, i);
111 list_insert(parent->children, i, child);
112 child->parent = parent;
113 wl_signal_emit(&child->events.reparent, old_parent);
114}
115
103struct sway_container *container_add_sibling(struct sway_container *fixed, 116struct sway_container *container_add_sibling(struct sway_container *fixed,
104 struct sway_container *active) { 117 struct sway_container *active) {
105 // TODO handle floating 118 // TODO handle floating
119 struct sway_container *old_parent = NULL;
120 if (active->parent) {
121 old_parent = active->parent;
122 container_remove_child(active);
123 }
106 struct sway_container *parent = fixed->parent; 124 struct sway_container *parent = fixed->parent;
107 int i = index_child(fixed); 125 int i = index_child(fixed);
108 list_insert(parent->children, i + 1, active); 126 list_insert(parent->children, i + 1, active);
109 active->parent = parent; 127 active->parent = parent;
128 wl_signal_emit(&active->events.reparent, old_parent);
110 return active->parent; 129 return active->parent;
111} 130}
112 131
@@ -166,9 +185,290 @@ void container_move_to(struct sway_container *container,
166 arrange_windows(new_parent, -1, -1); 185 arrange_windows(new_parent, -1, -1);
167} 186}
168 187
188static bool sway_dir_to_wlr(enum movement_direction dir,
189 enum wlr_direction *out) {
190 switch (dir) {
191 case MOVE_UP:
192 *out = WLR_DIRECTION_UP;
193 break;
194 case MOVE_DOWN:
195 *out = WLR_DIRECTION_DOWN;
196 break;
197 case MOVE_LEFT:
198 *out = WLR_DIRECTION_LEFT;
199 break;
200 case MOVE_RIGHT:
201 *out = WLR_DIRECTION_RIGHT;
202 break;
203 default:
204 return false;
205 }
206
207 return true;
208}
209
210static bool is_parallel(enum sway_container_layout layout,
211 enum movement_direction dir) {
212 switch (layout) {
213 case L_TABBED:
214 case L_STACKED:
215 case L_HORIZ:
216 return dir == MOVE_LEFT || dir == MOVE_RIGHT;
217 case L_VERT:
218 return dir == MOVE_UP || dir == MOVE_DOWN;
219 default:
220 return false;
221 }
222}
223
224static enum movement_direction invert_movement(enum movement_direction dir) {
225 switch (dir) {
226 case MOVE_LEFT:
227 return MOVE_RIGHT;
228 case MOVE_RIGHT:
229 return MOVE_LEFT;
230 case MOVE_UP:
231 return MOVE_DOWN;
232 case MOVE_DOWN:
233 return MOVE_UP;
234 default:
235 sway_assert(0, "This function expects left|right|up|down");
236 return MOVE_LEFT;
237 }
238}
239
240static int move_offs(enum movement_direction move_dir) {
241 return move_dir == MOVE_LEFT || move_dir == MOVE_UP ? -1 : 1;
242}
243
244/* Gets the index of the most extreme member based on the movement offset */
245static int container_limit(struct sway_container *container,
246 enum movement_direction move_dir) {
247 return move_offs(move_dir) < 0 ? 0 : container->children->length;
248}
249
250/* Takes one child, sets it aside, wraps the rest of the children in a new
251 * container, switches the layout of the workspace, and drops the child back in.
252 * In other words, rejigger it. */
253static void workspace_rejigger(struct sway_container *ws,
254 struct sway_container *child, enum movement_direction move_dir) {
255 struct sway_container *original_parent = child->parent;
256 struct sway_container *new_parent =
257 container_split(ws, ws->layout);
258
259 container_remove_child(child);
260 for (int i = 0; i < ws->children->length; ++i) {
261 struct sway_container *_child = ws->children->items[i];
262 container_move_to(new_parent, _child);
263 }
264
265 int index = move_offs(move_dir);
266 container_insert_child(ws, child, index < 0 ? 0 : 1);
267 container_set_layout(ws,
268 move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT);
269
270 container_flatten(ws);
271 container_reap_empty_recursive(original_parent);
272 wl_signal_emit(&child->events.reparent, original_parent);
273 arrange_windows(ws, -1, -1);
274}
275
169void container_move(struct sway_container *container, 276void container_move(struct sway_container *container,
170 enum movement_direction dir, int move_amt) { 277 enum movement_direction move_dir, int move_amt) {
171 // TODO 278 if (!sway_assert(
279 container->type != C_CONTAINER || container->type != C_VIEW,
280 "Can only move containers and views")) {
281 return;
282 }
283 int offs = move_offs(move_dir);
284
285 struct sway_container *sibling = NULL;
286 struct sway_container *current = container;
287 struct sway_container *parent = current->parent;
288
289 if (parent != container_flatten(parent)) {
290 // Special case: we were the last one in this container, so flatten it
291 // and leave
292 update_debug_tree();
293 return;
294 }
295
296 while (!sibling) {
297 if (current->type == C_ROOT) {
298 return;
299 }
300
301 parent = current->parent;
302 wlr_log(L_DEBUG, "Visiting %p %s '%s'", current,
303 container_type_to_str(current->type), current->name);
304
305 int index = index_child(current);
306
307 switch (current->type) {
308 case C_OUTPUT: {
309 enum wlr_direction wlr_dir;
310 sway_dir_to_wlr(move_dir, &wlr_dir);
311 double ref_lx = current->x + current->width / 2;
312 double ref_ly = current->y + current->height / 2;
313 struct wlr_output *next = wlr_output_layout_adjacent_output(
314 root_container.sway_root->output_layout, wlr_dir,
315 current->sway_output->wlr_output, ref_lx, ref_ly);
316 if (!next) {
317 wlr_log(L_DEBUG, "Hit edge of output, nowhere else to go");
318 return;
319 }
320 struct sway_output *next_output = next->data;
321 current = next_output->swayc;
322 wlr_log(L_DEBUG, "Selected next output (%s)", current->name);
323 // Select workspace and get outta here
324 current = seat_get_focus_inactive(
325 config->handler_context.seat, current);
326 if (current->type != C_WORKSPACE) {
327 current = container_parent(current, C_WORKSPACE);
328 }
329 sibling = current;
330 break;
331 }
332 case C_WORKSPACE:
333 if (!is_parallel(current->layout, move_dir)) {
334 if (current->children->length > 2) {
335 wlr_log(L_DEBUG, "Rejiggering the workspace (%d kiddos)",
336 current->children->length);
337 workspace_rejigger(current, container, move_dir);
338 } else if (current->children->length == 2) {
339 wlr_log(L_DEBUG, "Changing workspace layout");
340 container_set_layout(current,
341 move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ?
342 L_HORIZ : L_VERT);
343 container_insert_child(current, container, offs < 0 ? 0 : 1);
344 arrange_windows(current, -1, -1);
345 }
346 return;
347 } else {
348 wlr_log(L_DEBUG, "Selecting output");
349 current = current->parent;
350 }
351 break;
352 case C_CONTAINER:
353 case C_VIEW:
354 if (is_parallel(parent->layout, move_dir)) {
355 if ((index == parent->children->length - 1 && offs > 0)
356 || (index == 0 && offs < 0)) {
357 if (current->parent == container->parent) {
358 wlr_log(L_DEBUG, "Hit limit, selecting parent");
359 current = current->parent;
360 } else {
361 wlr_log(L_DEBUG, "Hit limit, "
362 "promoting descendant to sibling");
363 // Special case
364 struct sway_container *old_parent = container->parent;
365 container_insert_child(current->parent, container,
366 index + (offs < 0 ? 0 : 1));
367 container->width = container->height = 0;
368 arrange_windows(current->parent, -1, -1);
369 arrange_windows(old_parent, -1, -1);
370 return;
371 }
372 } else {
373 sibling = parent->children->items[index + offs];
374 wlr_log(L_DEBUG, "Selecting sibling id:%zd", sibling->id);
375 }
376 } else {
377 wlr_log(L_DEBUG, "Moving up to find a parallel container");
378 current = current->parent;
379 }
380 break;
381 default:
382 sway_assert(0, "Not expecting to see container of type %s here",
383 container_type_to_str(current->type));
384 return;
385 }
386 }
387
388 // Part two: move stuff around
389 int index = index_child(container);
390 struct sway_container *old_parent = container->parent;
391
392 while (sibling) {
393 switch (sibling->type) {
394 case C_VIEW:
395 if (sibling->parent == container->parent) {
396 wlr_log(L_DEBUG, "Swapping siblings");
397 sibling->parent->children->items[index + offs] = container;
398 sibling->parent->children->items[index] = sibling;
399 arrange_windows(sibling->parent, -1, -1);
400 } else {
401 wlr_log(L_DEBUG, "Promoting to sibling of cousin");
402 container_insert_child(sibling->parent, container,
403 index_child(sibling) + (offs > 0 ? 0 : 1));
404 container->width = container->height = 0;
405 arrange_windows(sibling->parent, -1, -1);
406 arrange_windows(old_parent, -1, -1);
407 }
408 sibling = NULL;
409 break;
410 case C_WORKSPACE: // Note: only in the case of moving between outputs
411 case C_CONTAINER:
412 if (is_parallel(sibling->layout, move_dir)) {
413 int limit = container_limit(sibling, invert_movement(move_dir));
414 wlr_log(L_DEBUG, "limit: %d", limit);
415 wlr_log(L_DEBUG,
416 "Reparenting container (parallel) to index %d "
417 "(move dir: %d)", limit, move_dir);
418 container_insert_child(sibling, container, limit);
419 container->width = container->height = 0;
420 arrange_windows(sibling, -1, -1);
421 arrange_windows(old_parent, -1, -1);
422 sibling = NULL;
423 } else {
424 wlr_log(L_DEBUG, "Reparenting container (perpendicular)");
425 container_remove_child(container);
426 struct sway_container *focus_inactive = seat_get_focus_inactive(
427 config->handler_context.seat, sibling);
428 if (focus_inactive) {
429 while (focus_inactive->parent != sibling) {
430 focus_inactive = focus_inactive->parent;
431 }
432 wlr_log(L_DEBUG, "Focus inactive: id:%zd",
433 focus_inactive->id);
434 sibling = focus_inactive;
435 continue;
436 } else if (sibling->children->length) {
437 wlr_log(L_DEBUG, "No focus-inactive, adding arbitrarily");
438 container_add_sibling(sibling->children->items[0], container);
439 } else {
440 wlr_log(L_DEBUG, "No kiddos, adding container alone");
441 container_add_child(sibling, container);
442 }
443 container->width = container->height = 0;
444 arrange_windows(sibling, -1, -1);
445 arrange_windows(old_parent, -1, -1);
446 sibling = NULL;
447 }
448 break;
449 default:
450 sway_assert(0, "Not expecting to see container of type %s here",
451 container_type_to_str(sibling->type));
452 return;
453 }
454 }
455
456 if (old_parent) {
457 seat_set_focus(config->handler_context.seat, old_parent);
458 seat_set_focus(config->handler_context.seat, container);
459 }
460
461 struct sway_container *last_ws = old_parent;
462 struct sway_container *next_ws = container->parent;
463 if (last_ws && last_ws->type != C_WORKSPACE) {
464 last_ws = container_parent(last_ws, C_WORKSPACE);
465 }
466 if (next_ws && next_ws->type != C_WORKSPACE) {
467 next_ws = container_parent(next_ws, C_WORKSPACE);
468 }
469 if (last_ws && next_ws && last_ws != next_ws) {
470 ipc_event_workspace(last_ws, container, "focus");
471 }
172} 472}
173 473
174enum sway_container_layout container_get_default_layout( 474enum sway_container_layout container_get_default_layout(
@@ -320,6 +620,9 @@ void arrange_windows(struct sway_container *container,
320 container->children->length); 620 container->children->length);
321 break; 621 break;
322 } 622 }
623 container_damage_whole(container);
624 // TODO: Make this less shitty
625 update_debug_tree();
323} 626}
324 627
325static void apply_horiz_layout(struct sway_container *container, 628static void apply_horiz_layout(struct sway_container *container,
@@ -512,28 +815,6 @@ static void get_layout_center_position(struct sway_container *container,
512 } 815 }
513} 816}
514 817
515static bool sway_dir_to_wlr(enum movement_direction dir,
516 enum wlr_direction *out) {
517 switch (dir) {
518 case MOVE_UP:
519 *out = WLR_DIRECTION_UP;
520 break;
521 case MOVE_DOWN:
522 *out = WLR_DIRECTION_DOWN;
523 break;
524 case MOVE_LEFT:
525 *out = WLR_DIRECTION_LEFT;
526 break;
527 case MOVE_RIGHT:
528 *out = WLR_DIRECTION_RIGHT;
529 break;
530 default:
531 return false;
532 }
533
534 return true;
535}
536
537static struct sway_container *sway_output_from_wlr(struct wlr_output *output) { 818static struct sway_container *sway_output_from_wlr(struct wlr_output *output) {
538 if (output == NULL) { 819 if (output == NULL) {
539 return NULL; 820 return NULL;
@@ -673,6 +954,9 @@ struct sway_container *container_replace_child(struct sway_container *child,
673 int i = index_child(child); 954 int i = index_child(child);
674 955
675 // TODO floating 956 // TODO floating
957 if (new_child->parent) {
958 container_remove_child(new_child);
959 }
676 parent->children->items[i] = new_child; 960 parent->children->items[i] = new_child;
677 new_child->parent = parent; 961 new_child->parent = parent;
678 child->parent = NULL; 962 child->parent = NULL;
@@ -696,6 +980,14 @@ struct sway_container *container_split(struct sway_container *child,
696 if (!sway_assert(child, "child cannot be null")) { 980 if (!sway_assert(child, "child cannot be null")) {
697 return NULL; 981 return NULL;
698 } 982 }
983 if (child->type == C_WORKSPACE && child->children->length == 0) {
984 // Special case: this just behaves like splitt
985 child->prev_layout = child->layout;
986 child->layout = layout;
987 arrange_windows(child, -1, -1);
988 return child;
989 }
990
699 struct sway_container *cont = container_create(C_CONTAINER); 991 struct sway_container *cont = container_create(C_CONTAINER);
700 992
701 wlr_log(L_DEBUG, "creating container %p around %p", cont, child); 993 wlr_log(L_DEBUG, "creating container %p around %p", cont, child);
@@ -718,7 +1010,9 @@ struct sway_container *container_split(struct sway_container *child,
718 } 1010 }
719 1011
720 container_add_child(workspace, cont); 1012 container_add_child(workspace, cont);
1013 enum sway_container_layout old_layout = workspace->layout;
721 container_set_layout(workspace, layout); 1014 container_set_layout(workspace, layout);
1015 cont->layout = old_layout;
722 1016
723 if (set_focus) { 1017 if (set_focus) {
724 seat_set_focus(seat, cont); 1018 seat_set_focus(seat, cont);
diff --git a/swaybar/ipc.c b/swaybar/ipc.c
index 92dbb8ea..e6231bd2 100644
--- a/swaybar/ipc.c
+++ b/swaybar/ipc.c
@@ -331,7 +331,7 @@ bool handle_ipc_readable(struct swaybar *bar) {
331 switch (resp->type) { 331 switch (resp->type) {
332 case IPC_EVENT_WORKSPACE: 332 case IPC_EVENT_WORKSPACE:
333 ipc_get_workspaces(bar); 333 ipc_get_workspaces(bar);
334 break; 334 return true;
335 case IPC_EVENT_MODE: { 335 case IPC_EVENT_MODE: {
336 json_object *result = json_tokener_parse(resp->payload); 336 json_object *result = json_tokener_parse(resp->payload);
337 if (!result) { 337 if (!result) {