From 0e2cc0af3049c6d1b91bda3081238e2e723e81b7 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Tue, 15 May 2018 13:14:18 +1000 Subject: Implement show_marks --- include/sway/tree/view.h | 7 +++++ sway/commands.c | 1 + sway/commands/mark.c | 1 + sway/commands/show_marks.c | 43 +++++++++++++++++++++++++ sway/commands/unmark.c | 2 ++ sway/desktop/output.c | 27 ++++++++++++++-- sway/meson.build | 1 + sway/tree/view.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 sway/commands/show_marks.c diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h index 7ed4d3df..951912d0 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -63,6 +63,11 @@ struct sway_view { list_t *executed_criteria; // struct criteria * list_t *marks; // char * + struct wlr_texture *marks_focused; + struct wlr_texture *marks_focused_inactive; + struct wlr_texture *marks_unfocused; + struct wlr_texture *marks_urgent; + union { struct wlr_xdg_surface_v6 *wlr_xdg_surface_v6; struct wlr_xdg_surface *wlr_xdg_surface; @@ -267,4 +272,6 @@ void view_clear_marks(struct sway_view *view); bool view_has_mark(struct sway_view *view, char *mark); +void view_update_marks_textures(struct sway_view *view); + #endif diff --git a/sway/commands.c b/sway/commands.c index 9b6d6459..6cba0a1c 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -116,6 +116,7 @@ static struct cmd_handler handlers[] = { { "mouse_warping", cmd_mouse_warping }, { "output", cmd_output }, { "seat", cmd_seat }, + { "show_marks", cmd_show_marks }, { "workspace", cmd_workspace }, { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth }, }; diff --git a/sway/commands/mark.c b/sway/commands/mark.c index b131f2f3..5a897e69 100644 --- a/sway/commands/mark.c +++ b/sway/commands/mark.c @@ -62,6 +62,7 @@ struct cmd_results *cmd_mark(int argc, char **argv) { } free(mark); + view_update_marks_textures(view); view_execute_criteria(view); return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/commands/show_marks.c b/sway/commands/show_marks.c new file mode 100644 index 00000000..c7fdc538 --- /dev/null +++ b/sway/commands/show_marks.c @@ -0,0 +1,43 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/tree/view.h" +#include "sway/output.h" +#include "list.h" +#include "log.h" +#include "stringop.h" + +static void rebuild_marks_iterator(struct sway_container *con, void *data) { + if (con->type == C_VIEW) { + view_update_marks_textures(con->sway_view); + } +} + +struct cmd_results *cmd_show_marks(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "show_marks", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (strcmp(*argv, "yes") == 0) { + config->show_marks = true; + } else if (strcmp(*argv, "no") == 0) { + config->show_marks = false; + } else { + return cmd_results_new(CMD_INVALID, "show_marks", + "Expected 'show_marks '"); + } + + if (config->show_marks) { + container_for_each_descendant_dfs(&root_container, + rebuild_marks_iterator, NULL); + } + + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *con = root_container.children->items[i]; + output_damage_whole(con->sway_output); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/unmark.c b/sway/commands/unmark.c index ea2a5709..1ce2c56b 100644 --- a/sway/commands/unmark.c +++ b/sway/commands/unmark.c @@ -10,6 +10,7 @@ static void remove_all_marks_iterator(struct sway_container *con, void *data) { if (con->type == C_VIEW) { view_clear_marks(con->sway_view); + view_update_marks_textures(con->sway_view); } } @@ -45,6 +46,7 @@ struct cmd_results *cmd_unmark(int argc, char **argv) { } else if (view && !mark) { // Clear all marks from the given view view_clear_marks(view); + view_update_marks_textures(view); } else if (!view && mark) { // Remove mark from whichever view has it view_find_and_unmark(mark); diff --git a/sway/desktop/output.c b/sway/desktop/output.c index b12130d9..57d71d5e 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -317,7 +317,7 @@ damage_finish: static void render_container_simple_border_normal(struct sway_output *output, pixman_region32_t *output_damage, struct sway_container *con, struct border_colors *colors, - struct wlr_texture *title_texture) { + struct wlr_texture *title_texture, struct wlr_texture *marks_texture) { struct wlr_box box; float color[4]; @@ -413,6 +413,25 @@ static void render_container_simple_border_normal(struct sway_output *output, render_texture(output->wlr_output, output_damage, title_texture, &texture_box, matrix, 1.0); } + + // Marks + if (config->show_marks && marks_texture) { + float output_scale = output->wlr_output->scale; + struct wlr_box texture_box; + wlr_texture_get_size(marks_texture, + &texture_box.width, &texture_box.height); + texture_box.x = (box.x + box.width) * output_scale - texture_box.width; + texture_box.y = (box.y + box.height) + * output_scale - texture_box.height; + + float matrix[9]; + wlr_matrix_project_box(matrix, &texture_box, + WL_OUTPUT_TRANSFORM_NORMAL, + 0.0, output->wlr_output->transform_matrix); + + render_texture(output->wlr_output, output_damage, marks_texture, + &texture_box, matrix, 1.0); + } } /** @@ -501,20 +520,24 @@ static void render_container_simple(struct sway_output *output, if (child->sway_view->border != B_NONE) { struct border_colors *colors; struct wlr_texture *title_texture; + struct wlr_texture *marks_texture; if (focus == child || parent_focused) { colors = &config->border_colors.focused; title_texture = child->title_focused; + marks_texture = child->sway_view->marks_focused; } else if (seat_get_focus_inactive(seat, con) == child) { colors = &config->border_colors.focused_inactive; title_texture = child->title_focused_inactive; + marks_texture = child->sway_view->marks_focused_inactive; } else { colors = &config->border_colors.unfocused; title_texture = child->title_unfocused; + marks_texture = child->sway_view->marks_unfocused; } if (child->sway_view->border == B_NORMAL) { render_container_simple_border_normal(output, damage, - child, colors, title_texture); + child, colors, title_texture, marks_texture); } else { render_container_simple_border_pixel(output, damage, child, colors); diff --git a/sway/meson.build b/sway/meson.build index ed14b51a..72347d51 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -60,6 +60,7 @@ sway_sources = files( 'commands/seat/cursor.c', 'commands/seat/fallback.c', 'commands/set.c', + 'commands/show_marks.c', 'commands/split.c', 'commands/swaybg_command.c', 'commands/title_format.c', diff --git a/sway/tree/view.c b/sway/tree/view.c index 833345c5..e26159d2 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -746,6 +746,7 @@ bool view_find_and_unmark(char *mark) { if (strcmp(view_mark, mark) == 0) { free(view_mark); list_del(view->marks, i); + view_update_marks_textures(view); return true; } } @@ -769,3 +770,80 @@ bool view_has_mark(struct sway_view *view, char *mark) { } return false; } + +static void update_marks_texture(struct sway_view *view, + struct wlr_texture **texture, struct border_colors *class) { + struct sway_container *output = container_parent(view->swayc, C_OUTPUT); + if (!output) { + return; + } + if (*texture) { + wlr_texture_destroy(*texture); + } + if (!view->marks->length) { + return; + } + + size_t len = 0; + for (int i = 0; i < view->marks->length; ++i) { + len += strlen((char *)view->marks->items[i]) + 2; + } + char *buffer = calloc(len + 1, 1); + char *part = malloc(len + 1); + + for (int i = 0; i < view->marks->length; ++i) { + char *mark = view->marks->items[i]; + sprintf(part, "[%s]", mark); + strcat(buffer, part); + } + free(part); + + double scale = output->sway_output->wlr_output->scale; + int width = 0; + int height = config->font_height * scale; + + cairo_t *c = cairo_create(NULL); + get_text_size(c, config->font, &width, NULL, scale, false, "%s", buffer); + cairo_destroy(c); + + cairo_surface_t *surface = cairo_image_surface_create( + CAIRO_FORMAT_ARGB32, width, height); + cairo_t *cairo = cairo_create(surface); + cairo_set_source_rgba(cairo, class->background[0], class->background[1], + class->background[2], class->background[3]); + cairo_paint(cairo); + PangoContext *pango = pango_cairo_create_context(cairo); + cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST); + cairo_set_source_rgba(cairo, class->text[0], class->text[1], + class->text[2], class->text[3]); + cairo_move_to(cairo, 0, 0); + + pango_printf(cairo, config->font, scale, false, "%s", buffer); + + cairo_surface_flush(surface); + unsigned char *data = cairo_image_surface_get_data(surface); + int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); + struct wlr_renderer *renderer = wlr_backend_get_renderer( + output->sway_output->wlr_output->backend); + *texture = wlr_texture_from_pixels( + renderer, WL_SHM_FORMAT_ARGB8888, stride, width, height, data); + cairo_surface_destroy(surface); + g_object_unref(pango); + cairo_destroy(cairo); + free(buffer); +} + +void view_update_marks_textures(struct sway_view *view) { + if (!config->show_marks) { + return; + } + update_marks_texture(view, &view->marks_focused, + &config->border_colors.focused); + update_marks_texture(view, &view->marks_focused_inactive, + &config->border_colors.focused_inactive); + update_marks_texture(view, &view->marks_unfocused, + &config->border_colors.unfocused); + update_marks_texture(view, &view->marks_urgent, + &config->border_colors.urgent); + container_damage_whole(view->swayc); +} -- cgit v1.2.3-54-g00ecf