From 57954a2b24f1e211c3b8811fb898ef4e076cb098 Mon Sep 17 00:00:00 2001
From: Drew DeVault <sir@cmpwn.com>
Date: Mon, 2 Apr 2018 13:49:37 -0400
Subject: Implement move [left|right|up|down]

The exact semantics of this command are complicated. I'll describe each
test scenario as s-expressions. Everything assumes L_HORIZ if not
specified, but if you rotate everything 90 degrees the same test cases
hold.

```
(container (view a) (view b focus) (view c))
-> move left
(container (view b focus) (view a) (view c))

(container (view a) (view b focus) (view c))
-> move right
(container (view a) (view c) (view b focus))

(container L_VERT (view a))
(container L_HORIZ
  (view b) (view c focus))
-> move up
(container L_VERT
  (view a) (view c focus))
(container L_HORIZ (view b))

(workspace
  (view a) (view b focus) (view c))
-> move up
(workspace [split direction flipped]
  (view b focus)
  (container (view a) (view c)))

(workspace
  (view a) (view b focus) (view c))
-> move down
(workspace [split direction flipped]
  (container (view a) (view c))
  (view b focus)))

Note: outputs use wlr_output_layout instead of assuming that i+/-1 is
the next output in the move direction.

(root
  (output X11-1
    (workspace 1))
  (output X11-2
    (workspace 1 (view a focus) (view b)))))
-> move left
(root
  (output X11-1
    (workspace 1 (view a focus)))
  (output X11-2
    (workspace 1 (view b)))))

(root
  (output X11-1
    (workspace 1
      (container (view a) (view b)))
  (output X11-2
    (workspace 1 (view c focus)))))
-> move left
(root
  (output X11-1
    (workspace 1
      (container (view a) (view b))
      (view c focus)))
  (output X11-2
    (workspace 1)))
```
---
 include/sway/tree/container.h | 7 +++++++
 1 file changed, 7 insertions(+)

(limited to 'include/sway')

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);
 
 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_flatten(struct sway_container *container);
+
 #endif
-- 
cgit v1.2.3


From 603e0e42c577026f1c688c393989e65dc3482808 Mon Sep 17 00:00:00 2001
From: Drew DeVault <sir@cmpwn.com>
Date: Fri, 6 Apr 2018 11:49:27 -0400
Subject: Add debug tree view

---
 include/sway/debug.h       |   7 +++
 include/sway/tree/layout.h |   4 +-
 sway/debug-tree.c          | 110 +++++++++++++++++++++++++++++++++++++++++++++
 sway/desktop/output.c      |   5 +++
 sway/input/seat.c          |   3 ++
 sway/main.c                |   6 ++-
 sway/meson.build           |   4 ++
 sway/tree/layout.c         |   8 ++++
 8 files changed, 145 insertions(+), 2 deletions(-)
 create mode 100644 include/sway/debug.h
 create mode 100644 sway/debug-tree.c

(limited to 'include/sway')

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 @@
+#ifndef SWAY_DEBUG_H
+#define SWAY_DEBUG_H
+
+extern bool enable_debug_tree;
+void update_debug_tree();
+
+#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 @@
 #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"
 
 enum movement_direction {
@@ -29,6 +29,8 @@ struct sway_root {
 
 	struct wl_list xwayland_unmanaged; // sway_xwayland_unmanaged::link
 
+	struct wlr_texture *debug_tree;
+
 	struct {
 		struct wl_signal new_container;
 	} events;
diff --git a/sway/debug-tree.c b/sway/debug-tree.c
new file mode 100644
index 00000000..08ee3585
--- /dev/null
+++ b/sway/debug-tree.c
@@ -0,0 +1,110 @@
+#include <pango/pangocairo.h>
+#include <wlr/backend.h>
+#include <wlr/render/wlr_texture.h>
+#include <wlr/util/log.h>
+#include "config.h"
+#include "sway/input/input-manager.h"
+#include "sway/input/seat.h"
+#include "sway/server.h"
+#include "sway/tree/container.h"
+#include "sway/tree/layout.h"
+#include "cairo.h"
+#include "config.h"
+#include "pango.h"
+
+static const char *layout_to_str(enum sway_container_layout layout) {
+	switch (layout) {
+	case L_HORIZ:
+		return "L_HORIZ";
+	case L_VERT:
+		return "L_VERT";
+	case L_STACKED:
+		return "L_STACKED";
+	case L_TABBED:
+		return "L_TABBED";
+	case L_FLOATING:
+		return "L_FLOATING";
+	case L_NONE:
+	default:
+		return "L_NONE";
+	}
+}
+
+static int draw_container(cairo_t *cairo, struct sway_container *container,
+		struct sway_container *focus, int x, int y) {
+	int text_width, text_height;
+	get_text_size(cairo, "monospace", &text_width, &text_height,
+		1, false, "%s '%s' %s %dx%d@%d,%d",
+		container_type_to_str(container->type), container->name,
+		layout_to_str(container->layout),
+		container->width, container->height, container->x, container->y);
+	cairo_rectangle(cairo, x, y, text_width, text_height);
+	cairo_set_source_u32(cairo, 0xFFFFFFFF);
+	cairo_fill(cairo);
+	cairo_move_to(cairo, x, y);
+	if (focus == container) {
+		cairo_set_source_u32(cairo, 0xFF0000FF);
+	} else {
+		cairo_set_source_u32(cairo, 0x000000FF);
+	}
+	pango_printf(cairo, "monospace", 1, false, "%s '%s' %s %dx%d@%d,%d",
+		container_type_to_str(container->type), container->name,
+		layout_to_str(container->layout),
+		container->width, container->height, container->x, container->y);
+	int height = text_height;
+	if (container->children) {
+		for (int i = 0; i < container->children->length; ++i) {
+			struct sway_container *child = container->children->items[i];
+			height += draw_container(cairo, child, focus, x + 10, y + height);
+		}
+	}
+	return height;
+}
+
+bool enable_debug_tree = false;
+
+void update_debug_tree() {
+	if (!enable_debug_tree) {
+		return;
+	}
+
+	int width = 640, height = 480;
+	for (int i = 0; i < root_container.children->length; ++i) {
+		struct sway_container *container = root_container.children->items[i];
+		if (container->width > width) {
+			width = container->width;
+		}
+		if (container->height > height) {
+			height = container->height;
+		}
+	}
+	cairo_surface_t *surface =
+		cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
+	cairo_t *cairo = cairo_create(surface);
+	PangoContext *pango = pango_cairo_create_context(cairo);
+
+	struct sway_seat *seat = NULL;
+	wl_list_for_each(seat, &input_manager->seats, link) {
+		break;
+	}
+
+	struct sway_container *focus;
+	if (seat != NULL) {
+		focus = seat_get_focus(seat);
+	}
+	draw_container(cairo, &root_container, focus, 0, 0);
+
+	cairo_surface_flush(surface);
+	struct wlr_renderer *renderer = wlr_backend_get_renderer(server.backend);
+	if (root_container.sway_root->debug_tree) {
+		wlr_texture_destroy(root_container.sway_root->debug_tree);
+	}
+	unsigned char *data = cairo_image_surface_get_data(surface);
+	int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
+	struct wlr_texture *texture = wlr_texture_from_pixels(renderer,
+		WL_SHM_FORMAT_ARGB8888, stride, width, height, data);
+	root_container.sway_root->debug_tree = texture;
+	cairo_surface_destroy(surface);
+	g_object_unref(pango);
+	cairo_destroy(cairo);
+}
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,
 		&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]);
 
 renderer_end:
+	if (root_container.sway_root->debug_tree) {
+		wlr_render_texture(renderer, root_container.sway_root->debug_tree,
+			wlr_output->transform_matrix, 0, 0, 1);
+	}
+
 	wlr_renderer_end(renderer);
 	if (!wlr_output_damage_swap_buffers(output->damage, when, damage)) {
 		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 @@
 #include <wlr/types/wlr_cursor.h>
 #include <wlr/types/wlr_output_layout.h>
 #include <wlr/types/wlr_xcursor_manager.h>
+#include "sway/debug.h"
 #include "sway/tree/container.h"
 #include "sway/tree/workspace.h"
 #include "sway/input/seat.h"
@@ -432,6 +433,8 @@ void seat_set_focus_warp(struct sway_seat *seat,
 	}
 
 	seat->has_focus = (container != NULL);
+
+	update_debug_tree();
 }
 
 void 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 @@
 #endif
 #include <wlr/util/log.h>
 #include "sway/config.h"
+#include "sway/debug.h"
 #include "sway/server.h"
 #include "sway/tree/layout.h"
 #include "sway/ipc-server.h"
@@ -288,7 +289,7 @@ int main(int argc, char **argv) {
 	int c;
 	while (1) {
 		int option_index = 0;
-		c = getopt_long(argc, argv, "hCdvVc:", long_options, &option_index);
+		c = getopt_long(argc, argv, "hCdDvVc:", long_options, &option_index);
 		if (c == -1) {
 			break;
 		}
@@ -306,6 +307,9 @@ int main(int argc, char **argv) {
 		case 'd': // debug
 			debug = 1;
 			break;
+		case 'D': // extended debug options
+			enable_debug_tree = true;
+			break;
 		case 'v': // version
 			fprintf(stdout, "sway version " SWAY_VERSION "\n");
 			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(
 	'commands.c',
 	'config.c',
 	'criteria.c',
+	'debug-tree.c',
 	'ipc-json.c',
 	'ipc-server.c',
 	'security.c',
@@ -102,10 +103,13 @@ sway_sources = files(
 )
 
 sway_deps = [
+	cairo,
+	gdk_pixbuf,
 	jsonc,
 	libcap,
 	libinput,
 	math,
+	pango,
 	pcre,
 	pixman,
 	server_protos,
diff --git a/sway/tree/layout.c b/sway/tree/layout.c
index 0011a9e3..e633acc6 100644
--- a/sway/tree/layout.c
+++ b/sway/tree/layout.c
@@ -6,6 +6,7 @@
 #include <string.h>
 #include <wlr/types/wlr_output.h>
 #include <wlr/types/wlr_output_layout.h>
+#include "sway/debug.h"
 #include "sway/tree/container.h"
 #include "sway/tree/layout.h"
 #include "sway/output.h"
@@ -431,6 +432,11 @@ void container_move(struct sway_container *container,
 		return;
 	}
 
+	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) {
@@ -594,6 +600,8 @@ void arrange_windows(struct sway_container *container,
 		break;
 	}
 	container_damage_whole(container);
+	// TODO: Make this less shitty
+	update_debug_tree();
 }
 
 static void apply_horiz_layout(struct sway_container *container,
-- 
cgit v1.2.3