Add debug tree view

This commit is contained in:
Drew DeVault 2018-04-06 11:49:27 -04:00
parent a06052ad9d
commit 603e0e42c5
8 changed files with 145 additions and 2 deletions

7
include/sway/debug.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef SWAY_DEBUG_H
#define SWAY_DEBUG_H
extern bool enable_debug_tree;
void update_debug_tree();
#endif

View File

@ -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;

110
sway/debug-tree.c Normal file
View File

@ -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);
}

View File

@ -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;

View File

@ -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,

View File

@ -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);

View File

@ -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,

View File

@ -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,