From 2c3ab701e7fc95f30b573b20873dc4dfb5c2fa66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 29 Aug 2019 20:18:06 +0200 Subject: [PATCH] search: move render() function to the 'render' module --- render.c | 54 ++++++++++++++++++++++++++++++++++++ render.h | 1 + search.c | 84 +++----------------------------------------------------- 3 files changed, 59 insertions(+), 80 deletions(-) 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 }