From 61cabdac13986e6cb2ba69e81d5934f55d92a1a9 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 17:23:28 +0200 Subject: search: wip: re-direct input while searching, and build a search buffer This adds a new state, 'is_searching'. While active, input is re-directed, and stored in a search buffer. In the future, we'll use this buffer and search for its content in the scrollback buffer, and move the view and create a selection on matches. When rendering in 'is_searching', everything is dimmed. In the future, we'll render the current search buffer on-top of the dimmed "regular" terminal output. --- render.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 84c86232..990726d8 100644 --- a/render.c +++ b/render.c @@ -204,6 +204,11 @@ render_cell(struct terminal *term, pixman_image_t *pix, bg = color_hex_to_pixman(term->cursor_color.cursor); } + if (term->is_searching) { + pixman_color_dim(&fg); + pixman_color_dim(&bg); + } + struct font *font = attrs_to_font(term, &cell->attrs); const struct glyph *glyph = font_glyph_for_wc(font, cell->wc); @@ -216,9 +221,13 @@ render_cell(struct terminal *term, pixman_image_t *pix, /* Non-block cursors */ if (has_cursor && !block_cursor) { - pixman_color_t cursor_color = term->cursor_color.text >> 31 - ? color_hex_to_pixman(term->cursor_color.cursor) - : fg; + pixman_color_t cursor_color; + if (term->cursor_color.text >> 31) { + cursor_color = color_hex_to_pixman(term->cursor_color.cursor); + if (term->is_searching) + pixman_color_dim(&cursor_color); + } else + cursor_color = fg; if (term->cursor_style == CURSOR_BAR) draw_bar(term, pix, &cursor_color, x, y); @@ -435,9 +444,10 @@ grid_render(struct terminal *term) term_damage_view(term); /* If we resized the window, or is flashing, or just stopped flashing */ - if (term->render.last_buf != buf || term->flash.active || term->render.was_flashing) { - LOG_DBG("new buffer"); - + if (term->render.last_buf != buf || + term->flash.active || term->render.was_flashing || + term->is_searching != term->render.was_searching) + { /* Fill area outside the cell grid with the default background color */ int rmargin = term->x_margin + term->cols * term->cell_width; int bmargin = term->y_margin + term->rows * term->cell_height; @@ -445,8 +455,10 @@ grid_render(struct terminal *term) int bmargin_height = term->height - bmargin; uint32_t _bg = !term->reverse ? term->colors.bg : term->colors.fg; - pixman_color_t bg = color_hex_to_pixman_with_alpha(_bg, term->colors.alpha); + if (term->is_searching) + pixman_color_dim(&bg); + pixman_image_fill_rectangles( PIXMAN_OP_SRC, pix, &bg, 4, (pixman_rectangle16_t[]){ @@ -469,6 +481,7 @@ grid_render(struct terminal *term) term->render.last_buf = buf; term->render.was_flashing = term->flash.active; + term->render.was_searching = term->is_searching; } tll_foreach(term->grid->scroll_damage, it) { @@ -626,6 +639,7 @@ grid_render(struct terminal *term) if (term->flash.active) { /* Note: alpha is pre-computed in each color component */ + /* TODO: dim while searching */ pixman_image_fill_rectangles( PIXMAN_OP_OVER, pix, &(pixman_color_t){.red=0x7fff, .green=0x7fff, .blue=0, .alpha=0x7fff}, -- cgit v1.3 From bb4fd58223612330f150fe590de3673d36995c05 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 19:40:07 +0200 Subject: render: don't dim selection while searching --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 990726d8..1ed2202f 100644 --- a/render.c +++ b/render.c @@ -204,7 +204,7 @@ render_cell(struct terminal *term, pixman_image_t *pix, bg = color_hex_to_pixman(term->cursor_color.cursor); } - if (term->is_searching) { + if (term->is_searching && !is_selected) { pixman_color_dim(&fg); pixman_color_dim(&bg); } -- cgit v1.3 From 242ab66c97771259d0f6d750b6407e8de1bb9ce0 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 28 Aug 2019 21:53:01 +0200 Subject: render: double-dim fg+bg when searching the scrollback buffer By toning down the screen content more, the selection (i.e. the search match) pops out much more. --- render.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'render.c') diff --git a/render.c b/render.c index 1ed2202f..6501aa4b 100644 --- a/render.c +++ b/render.c @@ -207,6 +207,8 @@ render_cell(struct terminal *term, pixman_image_t *pix, if (term->is_searching && !is_selected) { pixman_color_dim(&fg); pixman_color_dim(&bg); + pixman_color_dim(&fg); + pixman_color_dim(&bg); } struct font *font = attrs_to_font(term, &cell->attrs); -- cgit v1.3 From 2a31c2fbbc2f38abef54090767f842a9e8016c29 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 19:33:25 +0200 Subject: render: reduce amount of dim while searching scrollback history --- render.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 6501aa4b..8bdaeb0f 100644 --- a/render.c +++ b/render.c @@ -72,6 +72,14 @@ pixman_color_dim(pixman_color_t *color) color->blue /= 2; } +static inline void +pixman_color_dim_for_search(pixman_color_t *color) +{ + color->red /= 3; + color->green /= 3; + color->blue /= 3; +} + static void draw_bar(const struct terminal *term, pixman_image_t *pix, const pixman_color_t *color, int x, int y) @@ -205,10 +213,8 @@ render_cell(struct terminal *term, pixman_image_t *pix, } if (term->is_searching && !is_selected) { - pixman_color_dim(&fg); - pixman_color_dim(&bg); - pixman_color_dim(&fg); - pixman_color_dim(&bg); + pixman_color_dim_for_search(&fg); + pixman_color_dim_for_search(&bg); } struct font *font = attrs_to_font(term, &cell->attrs); -- cgit v1.3 From 2c3ab701e7fc95f30b573b20873dc4dfb5c2fa66 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 20:18:06 +0200 Subject: search: move render() function to the 'render' module --- render.c | 54 +++++++++++++++++++++++++++++++++++++++++ render.h | 1 + search.c | 84 ++++------------------------------------------------------------ 3 files changed, 59 insertions(+), 80 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 8bdaeb0f..9c01b15f 100644 --- a/render.c +++ b/render.c @@ -691,6 +691,60 @@ frame_callback(void *data, struct wl_callback *wl_callback, uint32_t callback_da grid_render(term); } +void +render_search_box(struct terminal *term) +{ + assert(term->wl.search_sub_surface != NULL); + + /* TODO: at least sway allows the subsurface to extend outside the + * main window. Do we want that? */ + const int scale = term->scale >= 1 ? term->scale : 1; + const int margin = scale * 3; + const int width = 2 * margin + max(20, term->search.len) * term->cell_width; + const int height = 2 * margin + 1 * term->cell_height; + + struct buffer *buf = shm_get_buffer(term->wl.shm, width, height, 1); + + /* Background - yellow on empty/match, red on mismatch */ + pixman_color_t color = color_hex_to_pixman( + term->search.match_len == term->search.len ? 0xffff00 : 0xff0000); + + pixman_image_fill_rectangles( + PIXMAN_OP_SRC, buf->pix, &color, + 1, &(pixman_rectangle16_t){0, 0, width, height}); + + int x = margin; + int y = margin; + pixman_color_t fg = color_hex_to_pixman(0x000000); + + /* Text (what the user entered - *not* match(es)) */ + for (size_t i = 0; i < term->search.len; i++) { + const struct glyph *glyph = font_glyph_for_wc(&term->fonts[0], term->search.buf[i]); + if (glyph == NULL) + continue; + + pixman_image_t *src = pixman_image_create_solid_fill(&fg); + pixman_image_composite32( + PIXMAN_OP_OVER, src, glyph->pix, buf->pix, 0, 0, 0, 0, + x + glyph->x, y + term->fextents.ascent - glyph->y, + glyph->width, glyph->height); + pixman_image_unref(src); + + x += glyph->width; + } + + LOG_INFO("match length: %zu", term->search.match_len); + + wl_subsurface_set_position( + term->wl.search_sub_surface, + term->width - width - margin, term->height - height - margin); + + wl_surface_damage_buffer(term->wl.search_surface, 0, 0, width, height); + wl_surface_attach(term->wl.search_surface, buf->wl_buf, 0, 0); + wl_surface_set_buffer_scale(term->wl.search_surface, scale); + wl_surface_commit(term->wl.search_surface); +} + static void reflow(struct row **new_grid, int new_cols, int new_rows, struct row *const *old_grid, int old_cols, int old_rows) diff --git a/render.h b/render.h index a3620665..c2fa489e 100644 --- a/render.h +++ b/render.h @@ -10,6 +10,7 @@ void render_resize(struct terminal *term, int width, int height); void render_set_title(struct terminal *term, const char *title); void render_refresh(struct terminal *term); +void render_search_box(struct terminal *term); bool render_reload_cursor_theme(struct terminal *term); void render_update_cursor_surface(struct terminal *term); diff --git a/search.c b/search.c index 150bd050..81bdf8f3 100644 --- a/search.c +++ b/search.c @@ -15,82 +15,6 @@ #define max(x, y) ((x) > (y) ? (x) : (y)) - - -static inline pixman_color_t -color_hex_to_pixman_with_alpha(uint32_t color, uint16_t alpha) -{ - int alpha_div = 0xffff / alpha; - return (pixman_color_t){ - .red = ((color >> 16 & 0xff) | (color >> 8 & 0xff00)) / alpha_div, - .green = ((color >> 8 & 0xff) | (color >> 0 & 0xff00)) / alpha_div, - .blue = ((color >> 0 & 0xff) | (color << 8 & 0xff00)) / alpha_div, - .alpha = alpha, - }; -} - -static inline pixman_color_t -color_hex_to_pixman(uint32_t color) -{ - /* Count on the compiler optimizing this */ - return color_hex_to_pixman_with_alpha(color, 0xffff); -} - -/* TODO: move to render.c? */ -static void -render(struct terminal *term) -{ - assert(term->wl.search_sub_surface != NULL); - - /* TODO: at least sway allows the subsurface to extend outside the - * main window. Do we want that? */ - const int scale = term->scale >= 1 ? term->scale : 1; - const int margin = scale * 3; - const int width = 2 * margin + max(20, term->search.len) * term->cell_width; - const int height = 2 * margin + 1 * term->cell_height; - - struct buffer *buf = shm_get_buffer(term->wl.shm, width, height, 1); - - /* Background - yellow on empty/match, red on mismatch */ - pixman_color_t color = color_hex_to_pixman( - term->search.match_len == term->search.len ? 0xffff00 : 0xff0000); - - pixman_image_fill_rectangles( - PIXMAN_OP_SRC, buf->pix, &color, - 1, &(pixman_rectangle16_t){0, 0, width, height}); - - int x = margin; - int y = margin; - pixman_color_t fg = color_hex_to_pixman(0x000000); - - /* Text (what the user entered - *not* match(es)) */ - for (size_t i = 0; i < term->search.len; i++) { - const struct glyph *glyph = font_glyph_for_wc(&term->fonts[0], term->search.buf[i]); - if (glyph == NULL) - continue; - - pixman_image_t *src = pixman_image_create_solid_fill(&fg); - pixman_image_composite32( - PIXMAN_OP_OVER, src, glyph->pix, buf->pix, 0, 0, 0, 0, - x + glyph->x, y + term->fextents.ascent - glyph->y, - glyph->width, glyph->height); - pixman_image_unref(src); - - x += glyph->width; - } - - LOG_INFO("match length: %zu", term->search.match_len); - - wl_subsurface_set_position( - term->wl.search_sub_surface, - term->width - width - margin, term->height - height - margin); - - wl_surface_damage_buffer(term->wl.search_surface, 0, 0, width, height); - wl_surface_attach(term->wl.search_surface, buf->wl_buf, 0, 0); - wl_surface_set_buffer_scale(term->wl.search_surface, scale); - wl_surface_commit(term->wl.search_surface); -} - static void search_cancel_keep_selection(struct terminal *term) { @@ -145,7 +69,7 @@ search_begin(struct terminal *term) assert(term->wl.search_sub_surface == NULL); } - render(term); + render_search_box(term); render_refresh(term); } @@ -167,7 +91,7 @@ search_update(struct terminal *term) term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; selection_cancel(term); - render(term); + render_search_box(term); return; } @@ -305,7 +229,7 @@ search_update(struct terminal *term) term->search.match.col = start_col; term->search.match_len = match_len; - render(term); + render_search_box(term); return; } @@ -317,7 +241,7 @@ search_update(struct terminal *term) term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; selection_cancel(term); - render(term); + render_search_box(term); #undef ROW_DEC } -- cgit v1.3 From 3ec7fbf0a320f2c7a07a4d03d3c5cbe65c2aeb18 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 20:23:44 +0200 Subject: render: remove INFO log (that really was a debug log) --- render.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 9c01b15f..566db615 100644 --- a/render.c +++ b/render.c @@ -733,8 +733,6 @@ render_search_box(struct terminal *term) x += glyph->width; } - LOG_INFO("match length: %zu", term->search.match_len); - wl_subsurface_set_position( term->wl.search_sub_surface, term->width - width - margin, term->height - height - margin); -- cgit v1.3 From 013cf61ffbd58f5f706ae6688aea56a14fce6622 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 20:39:22 +0200 Subject: render: add font_baseline() - calculates the y-coordinate for the baseline The old baseline calculation was copy-pasted to a couple of places, and also assumed that the font's height was equal to ascent+descent. While this is typically true, it isn't necessarily so. Now, we assume that height >= ascent+descent, and then position the baseline in "center" (but adjusted for the descent). --- render.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'render.c') diff --git a/render.c b/render.c index 566db615..88d747b6 100644 --- a/render.c +++ b/render.c @@ -80,6 +80,25 @@ pixman_color_dim_for_search(pixman_color_t *color) color->blue /= 3; } +static inline int +font_baseline(const struct terminal *term) +{ + assert(term->fextents.ascent >= 0); + assert(term->fextents.descent >= 0); + + int diff = term->fextents.height - (term->fextents.ascent + term->fextents.descent); + assert(diff >= 0); + +#if 0 + LOG_INFO("height=%d, ascent=%d, descent=%d, diff=%d", + term->fextents.height, + term->fextents.ascent, term->fextents.descent, + diff); +#endif + + return term->fextents.height - diff / 2 - term->fextents.descent; +} + static void draw_bar(const struct terminal *term, pixman_image_t *pix, const pixman_color_t *color, int x, int y) -- cgit v1.3 From 6e5688d7da131acf025c9a51a0466a9fbf5c7b56 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 20:41:40 +0200 Subject: render: draw_bar: don't assume height == ascent+descent Instead, draw a bar that is ascent+descent tall, positioned around the baseline such that it starts at the ascent and ends at the descent. --- render.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 88d747b6..66c76f23 100644 --- a/render.c +++ b/render.c @@ -103,10 +103,12 @@ static void draw_bar(const struct terminal *term, pixman_image_t *pix, const pixman_color_t *color, int x, int y) { - /* TODO: investigate if this is the best way */ + int baseline = y + font_baseline(term) - term->fextents.ascent; pixman_image_fill_rectangles( PIXMAN_OP_SRC, pix, color, - 1, &(pixman_rectangle16_t){x, y, 1, term->cell_height}); + 1, &(pixman_rectangle16_t){ + x, baseline, + 1, term->fextents.ascent + term->fextents.descent}); } static void -- cgit v1.3 From 1f98c8f7879a4066aa089b520084f77568a247d2 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 20:42:45 +0200 Subject: render: draw_underline: use font_baseline() for baseline calculation --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 66c76f23..34367cdd 100644 --- a/render.c +++ b/render.c @@ -116,7 +116,7 @@ draw_underline(const struct terminal *term, pixman_image_t *pix, const struct font *font, const pixman_color_t *color, int x, int y, int cols) { - int baseline = y + term->fextents.height - term->fextents.descent; + int baseline = y + font_baseline(term); int width = font->underline.thickness; int y_under = baseline - font->underline.position - width / 2; -- cgit v1.3 From bffa1f05ac0a5234ee21e1a46a363af19ddee367 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 20:42:57 +0200 Subject: render: draw_strikeout: use font_baseline() for baseline calculation --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 34367cdd..eaba6b03 100644 --- a/render.c +++ b/render.c @@ -130,7 +130,7 @@ draw_strikeout(const struct terminal *term, pixman_image_t *pix, const struct font *font, const pixman_color_t *color, int x, int y, int cols) { - int baseline = y + term->fextents.height - term->fextents.descent; + int baseline = y + font_baseline(term); int width = font->strikeout.thickness; int y_strike = baseline - font->strikeout.position - width / 2; -- cgit v1.3 From 2ca7400cc6ed922582adcd6a2455b6608a5a5f20 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 21:03:00 +0200 Subject: render: render_search_box: draw cursor as a bar --- render.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'render.c') diff --git a/render.c b/render.c index eaba6b03..0be0d3d4 100644 --- a/render.c +++ b/render.c @@ -740,6 +740,9 @@ render_search_box(struct terminal *term) /* Text (what the user entered - *not* match(es)) */ for (size_t i = 0; i < term->search.len; i++) { + if (i == term->search.cursor) + draw_bar(term, buf->pix, &fg, x, y); + const struct glyph *glyph = font_glyph_for_wc(&term->fonts[0], term->search.buf[i]); if (glyph == NULL) continue; @@ -754,6 +757,9 @@ render_search_box(struct terminal *term) x += glyph->width; } + if (term->search.cursor >= term->search.len) + draw_bar(term, buf->pix, &fg, x, y); + wl_subsurface_set_position( term->wl.search_sub_surface, term->width - width - margin, term->height - height - margin); -- cgit v1.3 From fcb0e050094f9d864afd958537e11cedb13e40dc Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 30 Aug 2019 19:35:47 +0200 Subject: render: search box: fix glyph spacing We're using the same font as in the terminal, so use the same glyph spacing. --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 0be0d3d4..1611b972 100644 --- a/render.c +++ b/render.c @@ -754,7 +754,7 @@ render_search_box(struct terminal *term) glyph->width, glyph->height); pixman_image_unref(src); - x += glyph->width; + x += term->cell_width; } if (term->search.cursor >= term->search.len) -- cgit v1.3 From e8d187e4f3068f9a35d4b9752f913cc0efe90a23 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 30 Aug 2019 19:42:33 +0200 Subject: render: draw (cursor) bar using the same thickness as underlines --- render.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 1611b972..e5ec9c90 100644 --- a/render.c +++ b/render.c @@ -101,6 +101,7 @@ font_baseline(const struct terminal *term) static void draw_bar(const struct terminal *term, pixman_image_t *pix, + const struct font *font, const pixman_color_t *color, int x, int y) { int baseline = y + font_baseline(term) - term->fextents.ascent; @@ -108,7 +109,7 @@ draw_bar(const struct terminal *term, pixman_image_t *pix, PIXMAN_OP_SRC, pix, color, 1, &(pixman_rectangle16_t){ x, baseline, - 1, term->fextents.ascent + term->fextents.descent}); + font->underline.thickness, term->fextents.ascent + term->fextents.descent}); } static void @@ -259,7 +260,7 @@ render_cell(struct terminal *term, pixman_image_t *pix, cursor_color = fg; if (term->cursor_style == CURSOR_BAR) - draw_bar(term, pix, &cursor_color, x, y); + draw_bar(term, pix, font, &cursor_color, x, y); else if (term->cursor_style == CURSOR_UNDERLINE) draw_underline( term, pix, attrs_to_font(term, &cell->attrs), &cursor_color, @@ -734,6 +735,7 @@ render_search_box(struct terminal *term) PIXMAN_OP_SRC, buf->pix, &color, 1, &(pixman_rectangle16_t){0, 0, width, height}); + struct font *font = &term->fonts[0]; int x = margin; int y = margin; pixman_color_t fg = color_hex_to_pixman(0x000000); @@ -741,9 +743,9 @@ render_search_box(struct terminal *term) /* Text (what the user entered - *not* match(es)) */ for (size_t i = 0; i < term->search.len; i++) { if (i == term->search.cursor) - draw_bar(term, buf->pix, &fg, x, y); + draw_bar(term, buf->pix, font, &fg, x, y); - const struct glyph *glyph = font_glyph_for_wc(&term->fonts[0], term->search.buf[i]); + const struct glyph *glyph = font_glyph_for_wc(font, term->search.buf[i]); if (glyph == NULL) continue; @@ -758,7 +760,7 @@ render_search_box(struct terminal *term) } if (term->search.cursor >= term->search.len) - draw_bar(term, buf->pix, &fg, x, y); + draw_bar(term, buf->pix, font, &fg, x, y); wl_subsurface_set_position( term->wl.search_sub_surface, -- cgit v1.3