aboutsummaryrefslogtreecommitdiffstats
path: root/sway/debug-tree.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2018-04-06 11:49:27 -0400
committerLibravatar Drew DeVault <sir@cmpwn.com>2018-04-06 11:49:27 -0400
commit603e0e42c577026f1c688c393989e65dc3482808 (patch)
tree7e2635f8745ed6141f900359eeff9b8d9f418cea /sway/debug-tree.c
parentFix splitting workspaces (diff)
downloadsway-603e0e42c577026f1c688c393989e65dc3482808.tar.gz
sway-603e0e42c577026f1c688c393989e65dc3482808.tar.zst
sway-603e0e42c577026f1c688c393989e65dc3482808.zip
Add debug tree view
Diffstat (limited to 'sway/debug-tree.c')
-rw-r--r--sway/debug-tree.c110
1 files changed, 110 insertions, 0 deletions
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 @@
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 '%s' %s %dx%d@%d,%d",
38 container_type_to_str(container->type), container->name,
39 layout_to_str(container->layout),
40 container->width, container->height, container->x, container->y);
41 cairo_rectangle(cairo, x, y, text_width, text_height);
42 cairo_set_source_u32(cairo, 0xFFFFFFFF);
43 cairo_fill(cairo);
44 cairo_move_to(cairo, x, y);
45 if (focus == container) {
46 cairo_set_source_u32(cairo, 0xFF0000FF);
47 } else {
48 cairo_set_source_u32(cairo, 0x000000FF);
49 }
50 pango_printf(cairo, "monospace", 1, false, "%s '%s' %s %dx%d@%d,%d",
51 container_type_to_str(container->type), container->name,
52 layout_to_str(container->layout),
53 container->width, container->height, container->x, container->y);
54 int height = text_height;
55 if (container->children) {
56 for (int i = 0; i < container->children->length; ++i) {
57 struct sway_container *child = container->children->items[i];
58 height += draw_container(cairo, child, focus, x + 10, y + height);
59 }
60 }
61 return height;
62}
63
64bool enable_debug_tree = false;
65
66void update_debug_tree() {
67 if (!enable_debug_tree) {
68 return;
69 }
70
71 int width = 640, height = 480;
72 for (int i = 0; i < root_container.children->length; ++i) {
73 struct sway_container *container = root_container.children->items[i];
74 if (container->width > width) {
75 width = container->width;
76 }
77 if (container->height > height) {
78 height = container->height;
79 }
80 }
81 cairo_surface_t *surface =
82 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
83 cairo_t *cairo = cairo_create(surface);
84 PangoContext *pango = pango_cairo_create_context(cairo);
85
86 struct sway_seat *seat = NULL;
87 wl_list_for_each(seat, &input_manager->seats, link) {
88 break;
89 }
90
91 struct sway_container *focus;
92 if (seat != NULL) {
93 focus = seat_get_focus(seat);
94 }
95 draw_container(cairo, &root_container, focus, 0, 0);
96
97 cairo_surface_flush(surface);
98 struct wlr_renderer *renderer = wlr_backend_get_renderer(server.backend);
99 if (root_container.sway_root->debug_tree) {
100 wlr_texture_destroy(root_container.sway_root->debug_tree);
101 }
102 unsigned char *data = cairo_image_surface_get_data(surface);
103 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
104 struct wlr_texture *texture = wlr_texture_from_pixels(renderer,
105 WL_SHM_FORMAT_ARGB8888, stride, width, height, data);
106 root_container.sway_root->debug_tree = texture;
107 cairo_surface_destroy(surface);
108 g_object_unref(pango);
109 cairo_destroy(cairo);
110}