From 2cc84db97978388475504592c88911046bcd2eb6 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 31 Jan 2021 11:12:07 +0100 Subject: urls: initial support for detecting URLs and rendering jump-labels The jump labels work, but is currently hardcoded to use xdg-open --- render.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 97 insertions(+), 26 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index aa415a18..e19d187c 100644 --- a/render.c +++ b/render.c @@ -28,13 +28,14 @@ #include "config.h" #include "grid.h" #include "hsl.h" +#include "ime.h" #include "quirks.h" #include "selection.h" -#include "sixel.h" #include "shm.h" +#include "sixel.h" +#include "url-mode.h" #include "util.h" #include "xmalloc.h" -#include "ime.h" #define TIME_SCROLL_DAMAGE 0 @@ -1706,10 +1707,10 @@ static void render_osd(struct terminal *term, struct wl_surface *surf, struct wl_subsurface *sub_surf, struct buffer *buf, - const wchar_t *text, uint32_t _fg, uint32_t _bg, + const wchar_t *text, uint32_t _fg, uint32_t _bg, int alpha, unsigned width, unsigned height, unsigned x, unsigned y) { - pixman_color_t bg = color_hex_to_pixman(_bg); + pixman_color_t bg = color_hex_to_pixman_with_alpha(_bg, alpha); pixman_image_fill_rectangles( PIXMAN_OP_SRC, buf->pix[0], &bg, 1, &(pixman_rectangle16_t){0, 0, width, height}); @@ -1896,7 +1897,7 @@ render_scrollback_position(struct terminal *term) term, win->scrollback_indicator_surface, win->scrollback_indicator_sub_surface, buf, text, - term->colors.table[0], term->colors.table[8 + 4], + term->colors.table[0], term->colors.table[8 + 4], 0xffff, width, height, width - margin - wcslen(text) * term->cell_width, margin); } @@ -1928,7 +1929,7 @@ render_render_timer(struct terminal *term, struct timeval render_time) term, win->render_timer_surface, win->render_timer_sub_surface, buf, text, - term->colors.table[0], term->colors.table[8 + 1], + term->colors.table[0], term->colors.table[8 + 1], 0xffff, width, height, margin, margin); } @@ -2517,6 +2518,61 @@ render_search_box(struct terminal *term) #undef WINDOW_Y } +static void +render_urls(struct terminal *term) +{ + struct wl_window *win = term->window; + xassert(tll_length(win->urls) > 0); + + tll_foreach(win->urls, it) { + const struct url *url = it->item.url; + const wchar_t *text = url->text; + const wchar_t *key = url->key; + + struct wl_surface *surf = it->item.surf; + struct wl_subsurface *sub_surf = it->item.sub_surf; + + if (surf == NULL || sub_surf == NULL) + continue; + + size_t text_len = wcslen(text); + size_t chars = wcslen(key) + (text_len > 0 ? 3 + text_len : 0); + + const size_t max_chars = 50; + chars = max(chars, max_chars); + + wchar_t label[chars + 2]; + if (text_len == 0) + wcscpy(label, key); + else { + int count = swprintf(label, chars, L"%s - %s", key, text); + if (count >= max_chars) { + label[max_chars] = L'…'; + label[max_chars + 1] = L'\0'; + } + } + + size_t len = wcslen(label); + int cols = wcswidth(label, len); + + const int margin = 3 * term->scale; + int width = 2 * margin + cols * term->cell_width; + int height = 2 * margin + term->cell_height; + + struct buffer *buf = shm_get_buffer( + term->wl->shm, width, height, shm_cookie_url(url), false, 1); + + const struct coord *pos = &url->start; + wl_subsurface_set_position( + sub_surf, + (term->margins.left + pos->col * term->cell_width - term->cell_width) / term->scale, + (term->margins.top + pos->row * term->cell_height - term->cell_height / 2) / term->scale); + render_osd(term, surf, sub_surf, buf, label, + term->colors.table[0], term->colors.table[3], 0xf000, + width, height, margin, margin); + } +} + static void render_update_title(struct terminal *term) { @@ -2545,13 +2601,15 @@ frame_callback(void *data, struct wl_callback *wl_callback, uint32_t callback_da bool grid = term->render.pending.grid; bool csd = term->render.pending.csd; - bool search = term->render.pending.search; + bool search = term->is_searching && term->render.pending.search; bool title = term->render.pending.title; + bool urls = urls_mode_is_active(term) > 0 && term->render.pending.urls; term->render.pending.grid = false; term->render.pending.csd = false; term->render.pending.search = false; term->render.pending.title = false; + term->render.pending.urls = false; if (csd && term->window->use_csd == CSD_YES) { quirk_weston_csd_on(term); @@ -2562,15 +2620,19 @@ frame_callback(void *data, struct wl_callback *wl_callback, uint32_t callback_da if (title) render_update_title(term); - if (search && term->is_searching) + if (search) render_search_box(term); - tll_foreach(term->wl->seats, it) - if (it->item.kbd_focus == term) - ime_update_cursor_rect(&it->item, term); + if (urls) + render_urls(term); - if (grid && (!term->delayed_render_timer.is_armed || csd || search)) + if (grid && (!term->delayed_render_timer.is_armed | csd | search | urls)) grid_render(term); + + tll_foreach(term->wl->seats, it) { + if (it->item.kbd_focus == term) + ime_update_cursor_rect(&it->item, term); + } } static void @@ -2755,6 +2817,9 @@ maybe_resize(struct terminal *term, int width, int height, bool force) /* Cancel an application initiated "Synchronized Update" */ term_disable_app_sync_updates(term); + /* Drop out of URL mode */ + urls_reset(term); + term->width = width; term->height = height; term->scale = scale; @@ -2981,27 +3046,21 @@ fdm_hook_refresh_pending_terminals(struct fdm *fdm, void *data) bool grid = term->render.refresh.grid; bool csd = term->render.refresh.csd; - bool search = term->render.refresh.search; + bool search = term->is_searching && term->render.refresh.search; bool title = term->render.refresh.title; + bool urls = urls_mode_is_active(term) && term->render.refresh.urls; - if (!term->is_searching) - search = false; - - if (!(grid | csd | search | title)) + if (!(grid | csd | search | title | urls)) continue; - if (term->render.app_sync_updates.enabled && !(csd | search | title)) + if (term->render.app_sync_updates.enabled && !(csd | search | title | urls)) continue; - if (csd | search) { - /* Force update of parent surface */ - grid = true; - } - term->render.refresh.grid = false; term->render.refresh.csd = false; term->render.refresh.search = false; term->render.refresh.title = false; + term->render.refresh.urls = false; if (term->window->frame_callback == NULL) { if (csd && term->window->use_csd == CSD_YES) { @@ -3013,17 +3072,22 @@ fdm_hook_refresh_pending_terminals(struct fdm *fdm, void *data) render_update_title(term); if (search) render_search_box(term); - tll_foreach(term->wl->seats, it) + if (urls) + render_urls(term); + if (grid | csd | search | urls) + grid_render(term); + + tll_foreach(term->wl->seats, it) { if (it->item.kbd_focus == term) ime_update_cursor_rect(&it->item, term); - if (grid) - grid_render(term); + } } else { /* Tells the frame callback to render again */ term->render.pending.grid |= grid; term->render.pending.csd |= csd; term->render.pending.search |= search; term->render.pending.title |= title; + term->render.pending.urls |= urls; } } @@ -3065,6 +3129,13 @@ render_refresh_search(struct terminal *term) term->render.refresh.search = true; } +void +render_refresh_urls(struct terminal *term) +{ + if (urls_mode_is_active(term)) + term->render.refresh.urls = true; +} + bool render_xcursor_set(struct seat *seat, struct terminal *term, const char *xcursor) { -- cgit v1.3 From 44b7758416d488911f9722ecccd5cc6bf8efddf8 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 31 Jan 2021 11:52:44 +0100 Subject: render: urls: positioning: place a bit further away from the starting position --- render.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index e19d187c..40e6b698 100644 --- a/render.c +++ b/render.c @@ -2565,8 +2565,8 @@ render_urls(struct terminal *term) const struct coord *pos = &url->start; wl_subsurface_set_position( sub_surf, - (term->margins.left + pos->col * term->cell_width - term->cell_width) / term->scale, - (term->margins.top + pos->row * term->cell_height - term->cell_height / 2) / term->scale); + (term->margins.left + pos->col * term->cell_width - 2 * term->cell_width) / term->scale, + (term->margins.top + pos->row * term->cell_height - term->cell_height) / term->scale); render_osd(term, surf, sub_surf, buf, label, term->colors.table[0], term->colors.table[3], 0xf000, width, height, margin, margin); -- cgit v1.3 From eb0b244c891f3cb6d5ba08fb3ea8af4bc421398e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 1 Feb 2021 10:04:25 +0100 Subject: render: url labels: don’t position outside window MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- render.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 40e6b698..00b6290d 100644 --- a/render.c +++ b/render.c @@ -2563,10 +2563,19 @@ render_urls(struct terminal *term) term->wl->shm, width, height, shm_cookie_url(url), false, 1); const struct coord *pos = &url->start; + int x = pos->col * term->cell_width - 2 * term->cell_width; + int y = pos->row * term->cell_height - term->cell_height; + + if (x < 0) + x += 4 * term->cell_width; + if (y < 0) + y += 2 * term->cell_height; + wl_subsurface_set_position( sub_surf, - (term->margins.left + pos->col * term->cell_width - 2 * term->cell_width) / term->scale, - (term->margins.top + pos->row * term->cell_height - term->cell_height) / term->scale); + (term->margins.left + x) / term->scale, + (term->margins.top + y) / term->scale); + render_osd(term, surf, sub_surf, buf, label, term->colors.table[0], term->colors.table[3], 0xf000, width, height, margin, margin); -- cgit v1.3 From d69497efe4025b5650ede2a5d24e38da4b23f2c5 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 4 Feb 2021 19:43:32 +0100 Subject: render: reduce URL jump label margins --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 00b6290d..710ef4a0 100644 --- a/render.c +++ b/render.c @@ -2555,7 +2555,7 @@ render_urls(struct terminal *term) size_t len = wcslen(label); int cols = wcswidth(label, len); - const int margin = 3 * term->scale; + const int margin = 2 * term->scale; int width = 2 * margin + cols * term->cell_width; int height = 2 * margin + term->cell_height; -- cgit v1.3 From 013b26d21221795d1107a74e28f5008a16864049 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 4 Feb 2021 19:43:47 +0100 Subject: render: urls: tweak positioning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don’t offset the labels too much vertically, _or_ horizontally. --- render.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 710ef4a0..451b0ff3 100644 --- a/render.c +++ b/render.c @@ -2563,13 +2563,15 @@ render_urls(struct terminal *term) term->wl->shm, width, height, shm_cookie_url(url), false, 1); const struct coord *pos = &url->start; - int x = pos->col * term->cell_width - 2 * term->cell_width; - int y = pos->row * term->cell_height - term->cell_height; + int x = pos->col * term->cell_width - 15 * term->cell_width / 10; + int y = pos->row * term->cell_height - 5 * term->cell_height / 10; if (x < 0) - x += 4 * term->cell_width; + x = 0; +#if 0 if (y < 0) - y += 2 * term->cell_height; + y += 15 * term->cell_height / 10; +#endif wl_subsurface_set_position( sub_surf, -- cgit v1.3 From fcbb5a0bf7c6f77eb067fa223b37a792c575df56 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 10:35:48 +0100 Subject: config: use a packed bitfield for “use custom color” flags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.c | 6 ++++-- config.h | 5 ++++- render.c | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) (limited to 'render.c') diff --git a/config.c b/config.c index 74cff1f3..285bc889 100644 --- a/config.c +++ b/config.c @@ -2281,7 +2281,9 @@ config_load(struct config *conf, const char *conf_path, .alpha = 0xffff, .selection_fg = 0x80000000, /* Use default bg */ .selection_bg = 0x80000000, /* Use default fg */ - .selection_uses_custom_colors = false, + .use_custom = { + .selection = false, + }, }, .cursor = { @@ -2378,7 +2380,7 @@ config_load(struct config *conf, const char *conf_path, ret = parse_config_file(f, conf, conf_file.path, errors_are_fatal); fclose(f); - conf->colors.selection_uses_custom_colors = + conf->colors.use_custom.selection = conf->colors.selection_fg >> 24 == 0 && conf->colors.selection_bg >> 24 == 0; diff --git a/config.h b/config.h index d6ad2b24..f8ebb572 100644 --- a/config.h +++ b/config.h @@ -139,7 +139,10 @@ struct config { uint16_t alpha; uint32_t selection_fg; uint32_t selection_bg; - bool selection_uses_custom_colors; + + struct { + bool selection:1; + } use_custom; } colors; struct { diff --git a/render.c b/render.c index 451b0ff3..8415ac81 100644 --- a/render.c +++ b/render.c @@ -412,7 +412,7 @@ render_cell(struct terminal *term, pixman_image_t *pix, uint32_t _fg = 0; uint32_t _bg = 0; - if (is_selected && term->conf->colors.selection_uses_custom_colors) { + if (is_selected && term->conf->colors.use_custom.selection) { _fg = term->conf->colors.selection_fg; _bg = term->conf->colors.selection_bg; } else { -- cgit v1.3 From e9c3d03837bb50ec39b85ee92f2906e2bb40971f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 11:10:40 +0100 Subject: config: add colors.jump_labels and colors.urls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * colors.jump_labels configures the foreground and background colors used when rendering URL jump labels. Defaults to “regular0 regular3” (i.e. black on yellow). * colors.urls configures the color to use when highlighting URLs in URL mode. Note that we aren’t currently doing any highlighting... Defaults to regular3 (i.e. yellow). --- config.c | 68 +++++++++++++++++++++++++++++++++++++++++++----------- config.h | 8 +++++++ doc/foot.ini.5.scd | 9 ++++++++ foot.ini | 2 ++ render.c | 12 ++++++++-- 5 files changed, 84 insertions(+), 15 deletions(-) (limited to 'render.c') diff --git a/config.c b/config.c index 285bc889..f7452994 100644 --- a/config.c +++ b/config.c @@ -420,6 +420,30 @@ str_to_color(const char *s, uint32_t *color, bool allow_alpha, return true; } +static bool +str_to_two_colors(const char *s, uint32_t *first, uint32_t *second, + bool allow_alpha, struct config *conf, const char *path, + int lineno, const char *section, const char *key) +{ + /* TODO: do this without strdup() */ + char *value_copy = xstrdup(s); + const char *first_as_str = strtok(value_copy, " "); + const char *second_as_str = strtok(NULL, " "); + + if (first_as_str == NULL || second_as_str == NULL || + !str_to_color(first_as_str, first, allow_alpha, conf, path, lineno, section, key) || + !str_to_color(second_as_str, second, allow_alpha, conf, path, lineno, section, key)) + { + LOG_AND_NOTIFY_ERR("%s:%d: [%s]: %s: invalid colors: %s", + path, lineno, section, key, s); + free(value_copy); + return false; + } + + free(value_copy); + return true; +} + static bool str_to_pt_or_px(const char *s, struct pt_or_px *res, struct config *conf, const char *path, int lineno, const char *section, const char *key) @@ -845,6 +869,30 @@ parse_section_colors(const char *key, const char *value, struct config *conf, else if (strcmp(key, "bright7") == 0) color = &conf->colors.bright[7]; else if (strcmp(key, "selection-foreground") == 0) color = &conf->colors.selection_fg; else if (strcmp(key, "selection-background") == 0) color = &conf->colors.selection_bg; + + else if (strcmp(key, "jump-labels") == 0) { + if (!str_to_two_colors( + value, &conf->colors.jump_label.fg, &conf->colors.jump_label.bg, + false, conf, path, lineno, "colors", "jump-labels")) + { + return false; + } + + conf->colors.use_custom.jump_label = true; + return true; + } + + else if (strcmp(key, "urls") == 0) { + if (!str_to_color(value, &conf->colors.url, false, + conf, path, lineno, "colors", "urls")) + { + return false; + } + + conf->colors.use_custom.url = true; + return true; + } + else if (strcmp(key, "alpha") == 0) { double alpha; if (!str_to_double(value, &alpha) || alpha < 0. || alpha > 1.) { @@ -892,23 +940,15 @@ parse_section_cursor(const char *key, const char *value, struct config *conf, conf->cursor.blink = str_to_bool(value); else if (strcmp(key, "color") == 0) { - char *value_copy = xstrdup(value); - const char *text = strtok(value_copy, " "); - const char *cursor = strtok(NULL, " "); - - uint32_t text_color, cursor_color; - if (text == NULL || cursor == NULL || - !str_to_color(text, &text_color, false, conf, path, lineno, "cursor", "color") || - !str_to_color(cursor, &cursor_color, false, conf, path, lineno, "cursor", "color")) + if (!str_to_two_colors( + value, &conf->cursor.color.text, &conf->cursor.color.cursor, + false, conf, path, lineno, "cursor", "color")) { - LOG_AND_NOTIFY_ERR("%s:%d: invalid cursor colors: %s", path, lineno, value); - free(value_copy); return false; } - conf->cursor.color.text = 1u << 31 | text_color; - conf->cursor.color.cursor = 1u << 31 | cursor_color; - free(value_copy); + conf->cursor.color.text |= 1u << 31; + conf->cursor.color.cursor |= 1u << 31; } else { @@ -2283,6 +2323,8 @@ config_load(struct config *conf, const char *conf_path, .selection_bg = 0x80000000, /* Use default fg */ .use_custom = { .selection = false, + .jump_label = false, + .url = false, }, }, diff --git a/config.h b/config.h index f8ebb572..94998f28 100644 --- a/config.h +++ b/config.h @@ -139,9 +139,17 @@ struct config { uint16_t alpha; uint32_t selection_fg; uint32_t selection_bg; + uint32_t url; + + struct { + uint32_t fg; + uint32_t bg; + } jump_label; struct { bool selection:1; + bool jump_label:1; + bool url:1; } use_custom; } colors; diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index 727b81f1..3b913eda 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -351,6 +351,15 @@ _alpha_ option. text. Note that *both* options must be set, or the default will be used. Default: _inverse foreground/background_. +*jump-labels* + To RRGGBB values specifying the foreground (text) and background + colors to use when rendering jump labels in URL mode. Default: + _regular0 regular3_. + +*urls* + Color to use for the underline used to highlight URLs in URL + mode. Default: _regular3_. + # SECTION: csd diff --git a/foot.ini b/foot.ini index d90c0a06..8896eef0 100644 --- a/foot.ini +++ b/foot.ini @@ -66,6 +66,8 @@ # bright7=ffffff # bright white # selection-foreground= # selection-background= +# jump-labels= +# urls= [csd] # preferred=server diff --git a/render.c b/render.c index 8415ac81..040fa34a 100644 --- a/render.c +++ b/render.c @@ -2578,9 +2578,17 @@ render_urls(struct terminal *term) (term->margins.left + x) / term->scale, (term->margins.top + y) / term->scale); + uint32_t fg = term->conf->colors.use_custom.jump_label + ? term->conf->colors.jump_label.fg + : term->colors.table[0]; + uint32_t bg = term->conf->colors.use_custom.jump_label + ? term->conf->colors.jump_label.bg + : term->colors.table[3]; + + uint16_t alpha = 0xffff; + render_osd(term, surf, sub_surf, buf, label, - term->colors.table[0], term->colors.table[3], 0xf000, - width, height, margin, margin); + fg, bg, alpha, width, height, margin, margin); } } -- cgit v1.3 From ee1d179b8f147e6314e4823e4c66a35030ab2a0f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 11:12:22 +0100 Subject: config: use a bitfield for flags tracking whether to use custom colors for CSD or not --- config.h | 8 ++++---- render.c | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'render.c') diff --git a/config.h b/config.h index 94998f28..5ae70ae3 100644 --- a/config.h +++ b/config.h @@ -192,10 +192,10 @@ struct config { int button_width; struct { - bool title_set; - bool minimize_set; - bool maximize_set; - bool close_set; + bool title_set:1; + bool minimize_set:1; + bool maximize_set:1; + bool close_set:1; uint32_t title; uint32_t minimize; uint32_t maximize; diff --git a/render.c b/render.c index 040fa34a..d04fd5d4 100644 --- a/render.c +++ b/render.c @@ -1605,27 +1605,27 @@ render_csd_button(struct terminal *term, enum csd_surface surf_idx) uint32_t _color; uint16_t alpha = 0xffff; bool is_active = false; - const bool *is_set = NULL; + bool is_set = false; const uint32_t *conf_color = NULL; switch (surf_idx) { case CSD_SURF_MINIMIZE: _color = term->colors.default_table[4]; /* blue */ - is_set = &term->conf->csd.color.minimize_set; + is_set = term->conf->csd.color.minimize_set; conf_color = &term->conf->csd.color.minimize; is_active = term->active_surface == TERM_SURF_BUTTON_MINIMIZE; break; case CSD_SURF_MAXIMIZE: _color = term->colors.default_table[2]; /* green */ - is_set = &term->conf->csd.color.maximize_set; + is_set = term->conf->csd.color.maximize_set; conf_color = &term->conf->csd.color.maximize; is_active = term->active_surface == TERM_SURF_BUTTON_MAXIMIZE; break; case CSD_SURF_CLOSE: _color = term->colors.default_table[1]; /* red */ - is_set = &term->conf->csd.color.close_set; + is_set = term->conf->csd.color.close_set; conf_color = &term->conf->csd.color.close; is_active = term->active_surface == TERM_SURF_BUTTON_CLOSE; break; @@ -1636,7 +1636,7 @@ render_csd_button(struct terminal *term, enum csd_surface surf_idx) } if (is_active) { - if (*is_set) { + if (is_set) { _color = *conf_color; alpha = _color >> 24 | (_color >> 24 << 8); } -- cgit v1.3 From 6726494f4c2836a5ed1c4611c181e4a57a4adbf1 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 11:47:59 +0100 Subject: url-mode: store absolute row numbers in start/end coordinates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows us to update the jump label positions when the viewport changes. This in turn allows us to stay in URL mode while the user is using the mouse to scroll in the scrollback history. Scrolling with the keyboard is currently not possible, since input handling in URL mode does not recognize “regular” key bindings. We _could_ add scrollback up/down bindings to URL mode too, but lets not, for the time being. (Note: an alternative to this patch is to disallow mouse scrolling too. Then we could have kept the URL start/end as viewport local coordinates). --- commands.c | 2 ++ render.c | 32 +++++++++++++++++++++++++++++--- url-mode.c | 3 +++ 3 files changed, 34 insertions(+), 3 deletions(-) (limited to 'render.c') diff --git a/commands.c b/commands.c index ac91ab19..01ad4e1e 100644 --- a/commands.c +++ b/commands.c @@ -83,6 +83,7 @@ cmd_scrollback_up(struct terminal *term, int rows) } else term_damage_view(term); + render_refresh_urls(term); render_refresh(term); } @@ -157,5 +158,6 @@ cmd_scrollback_down(struct terminal *term, int rows) } else term_damage_view(term); + render_refresh_urls(term); render_refresh(term); } diff --git a/render.c b/render.c index d04fd5d4..fff280fc 100644 --- a/render.c +++ b/render.c @@ -2524,6 +2524,15 @@ render_urls(struct terminal *term) struct wl_window *win = term->window; xassert(tll_length(win->urls) > 0); + /* Calculate view start, counted from the *current* scrollback start */ + const int scrollback_end + = (term->grid->offset + term->rows) & (term->grid->num_rows - 1); + const int view_start + = (term->grid->view + - scrollback_end + + term->grid->num_rows) & (term->grid->num_rows - 1); + const int view_end = view_start + term->rows - 1; + tll_foreach(win->urls, it) { const struct url *url = it->item.url; const wchar_t *text = url->text; @@ -2535,6 +2544,18 @@ render_urls(struct terminal *term) if (surf == NULL || sub_surf == NULL) continue; + const struct coord *pos = &url->start; + const int _row + = (pos->row + - scrollback_end + + term->grid->num_rows) & (term->grid->num_rows - 1); + + if (_row < view_start || _row > view_end) { + wl_surface_attach(surf, NULL, 0, 0); + wl_surface_commit(surf); + continue; + } + size_t text_len = wcslen(text); size_t chars = wcslen(key) + (text_len > 0 ? 3 + text_len : 0); @@ -2562,9 +2583,14 @@ render_urls(struct terminal *term) struct buffer *buf = shm_get_buffer( term->wl->shm, width, height, shm_cookie_url(url), false, 1); - const struct coord *pos = &url->start; - int x = pos->col * term->cell_width - 15 * term->cell_width / 10; - int y = pos->row * term->cell_height - 5 * term->cell_height / 10; + int col = pos->col; + int row = pos->row - term->grid->view; + while (row < 0) + row += term->grid->num_rows; + row &= (term->grid->num_rows - 1); + + int x = col * term->cell_width - 15 * term->cell_width / 10; + int y = row * term->cell_height - 5 * term->cell_height / 10; if (x < 0) x = 0; diff --git a/url-mode.c b/url-mode.c index 4feea2df..8f3b867e 100644 --- a/url-mode.c +++ b/url-mode.c @@ -294,6 +294,9 @@ auto_detected(struct terminal *term, enum url_action action) url[len] = L'\0'; + start.row += term->grid->view; + end.row += term->grid->view; + tll_push_back( term->urls, ((struct url){ -- cgit v1.3 From 2c10a147ea0e471f12facfa11913272d42163f76 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 11:51:58 +0100 Subject: url-mode: underline URLs using the color from colors.urls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is implemented by allocating one of the (few!) remaining bits in the cells’ attribute struct to indicate the cell should be “URL highlighted”. render_cell() looks at this bit and draws an underline using the color from colors.urls (defaults to regular3 - i.e. yellow). A new function, url_tag_cells(), iterates the currently detected URLs and sets the new ‘url’ attribute flag on the affected cells. Note: this is done in a separate function to keep urls_collect() free from as many dependencies as possible. urls_reset() is updated to *clear* the ‘url’ flag (and thus implicitly also results in a grid refresh, _if_ there were any URLs). We now exit URL mode on *any* client application input. This needs to be so since we can’t know if the URLs we previously detected are still valid. --- input.c | 1 + render.c | 9 +++++++++ terminal.c | 2 ++ terminal.h | 3 ++- url-mode.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ url-mode.h | 1 + 6 files changed, 61 insertions(+), 1 deletion(-) (limited to 'render.c') diff --git a/input.c b/input.c index 10507aa8..7ba8e8e0 100644 --- a/input.c +++ b/input.c @@ -281,6 +281,7 @@ execute_binding(struct seat *seat, struct terminal *term, : URL_ACTION_LAUNCH; urls_collect(term, url_action); + urls_tag_cells(term); render_refresh_urls(term); return true; } diff --git a/render.c b/render.c index fff280fc..4e41f48a 100644 --- a/render.c +++ b/render.c @@ -616,6 +616,15 @@ render_cell(struct terminal *term, pixman_image_t *pix, if (cell->attrs.strikethrough) draw_strikeout(term, pix, font, &fg, x, y, cell_cols); + if (unlikely(cell->attrs.url)) { + pixman_color_t url_color = color_hex_to_pixman( + term->conf->colors.use_custom.url + ? term->conf->colors.url + : term->colors.table[3] + ); + draw_underline(term, pix, font, &url_color, x, y, cell_cols); + } + draw_cursor: if (has_cursor && (term->cursor_style != CURSOR_BLOCK || !term->kbd_focus)) draw_cursor(term, cell, font, pix, &fg, &bg, x, y, cell_cols); diff --git a/terminal.c b/terminal.c index c67dda58..91a442ec 100644 --- a/terminal.c +++ b/terminal.c @@ -228,6 +228,8 @@ fdm_ptmx(struct fdm *fdm, int fd, int events, void *data) cursor_blink_rearm_timer(term); } + urls_reset(term); + uint8_t buf[24 * 1024]; ssize_t count = sizeof(buf); diff --git a/terminal.h b/terminal.h index c6a618bf..bb98efc2 100644 --- a/terminal.h +++ b/terminal.h @@ -40,7 +40,8 @@ struct attributes { uint32_t have_fg:1; uint32_t have_bg:1; uint32_t selected:2; - uint32_t reserved:3; + uint32_t url:1; + uint32_t reserved:2; uint32_t bg:24; }; static_assert(sizeof(struct attributes) == 8, "bad size"); diff --git a/url-mode.c b/url-mode.c index 8f3b867e..e418eb7e 100644 --- a/url-mode.c +++ b/url-mode.c @@ -7,6 +7,7 @@ #define LOG_ENABLE_DBG 1 #include "log.h" #include "grid.h" +#include "render.h" #include "selection.h" #include "spawn.h" #include "terminal.h" @@ -399,6 +400,49 @@ urls_collect(struct terminal *term, enum url_action action) } } +static void +tag_cells_for_url(struct terminal *term, const struct url *url, bool value) +{ + const struct coord *start = &url->start; + const struct coord *end = &url->end; + + size_t end_r = end->row & (term->grid->num_rows - 1); + + size_t r = start->row & (term->grid->num_rows - 1); + size_t c = start->col; + + struct row *row = term->grid->rows[r]; + row->dirty = true; + + while (true) { + struct cell *cell = &row->cells[c]; + cell->attrs.url = value; + cell->attrs.clean = 0; + + if (r == end_r && c == end->col) + break; + + if (++c >= term->cols) { + r = (r + 1) & (term->grid->num_rows - 1); + c = 0; + + row = term->grid->rows[r]; + row->dirty = true; + } + } +} + +void +urls_tag_cells(struct terminal *term) +{ + if (unlikely(tll_length(term->urls)) == 0) + return; + + tll_foreach(term->urls, it) + tag_cells_for_url(term, &it->item, true); + render_refresh(term); +} + void urls_reset(struct terminal *term) { @@ -416,10 +460,12 @@ urls_reset(struct terminal *term) } tll_foreach(term->urls, it) { + tag_cells_for_url(term, &it->item, false); free(it->item.url); free(it->item.text); } tll_free(term->urls); memset(term->url_keys, 0, sizeof(term->url_keys)); + render_refresh(term); } diff --git a/url-mode.h b/url-mode.h index 94c1e675..6651e119 100644 --- a/url-mode.h +++ b/url-mode.h @@ -12,6 +12,7 @@ static inline bool urls_mode_is_active(const struct terminal *term) } void urls_collect(struct terminal *term, enum url_action action); +void urls_tag_cells(struct terminal *term); void urls_reset(struct terminal *term); void urls_input(struct seat *seat, struct terminal *term, uint32_t key, -- cgit v1.3 From 5df2e990e33a027a4517f59aefad4c6be20d53bc Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 20:52:04 +0100 Subject: render: urls: don’t display URLs whose key sequence doesn’t match MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- render.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 4e41f48a..60b41dee 100644 --- a/render.c +++ b/render.c @@ -2546,6 +2546,7 @@ render_urls(struct terminal *term) const struct url *url = it->item.url; const wchar_t *text = url->text; const wchar_t *key = url->key; + const size_t entered_key_len = wcslen(term->url_keys); struct wl_surface *surf = it->item.surf; struct wl_subsurface *sub_surf = it->item.sub_surf; @@ -2553,13 +2554,21 @@ render_urls(struct terminal *term) if (surf == NULL || sub_surf == NULL) continue; + bool hide = false; const struct coord *pos = &url->start; const int _row = (pos->row - scrollback_end + term->grid->num_rows) & (term->grid->num_rows - 1); - if (_row < view_start || _row > view_end) { + if (_row < view_start || _row > view_end) + hide = true; + if (wcslen(key) <= entered_key_len) + hide = true; + if (wcsncmp(term->url_keys, key, entered_key_len) != 0) + hide = true; + + if (hide) { wl_surface_attach(surf, NULL, 0, 0); wl_surface_commit(surf); continue; -- cgit v1.3 From 01c0535b5ab2e4c78b04eccbfc46dd359315ab6b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 20:52:48 +0100 Subject: render: urls: add TODO: highlight already entered keys --- render.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'render.c') diff --git a/render.c b/render.c index 60b41dee..5d2f4263 100644 --- a/render.c +++ b/render.c @@ -2633,6 +2633,12 @@ render_urls(struct terminal *term) render_osd(term, surf, sub_surf, buf, label, fg, bg, alpha, width, height, margin, margin); + +#if 0 + /* TODO: somehow highlight the key(s) entered so far */ + pixman_color_t color = color_hex_to_pixman(fg); + draw_strikeout(term, buf->pix[0], term->fonts[0], &color, margin, margin, entered_key_len); +#endif } } -- cgit v1.3 From 19e23254fbce1d4233133754617e01aa24494de2 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 23:03:05 +0100 Subject: render: urls: uppercase the activation key sequence --- render.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'render.c') diff --git a/render.c b/render.c index 5d2f4263..57af555c 100644 --- a/render.c +++ b/render.c @@ -1,6 +1,7 @@ #include "render.h" #include +#include #include #include @@ -2591,6 +2592,9 @@ render_urls(struct terminal *term) } } + for (size_t i = 0; i < wcslen(key); i++) + label[i] = towupper(label[i]); + size_t len = wcslen(label); int cols = wcswidth(label, len); -- cgit v1.3 From 672c414b91eaa6a1a46c1ea8de14cae7f63faca3 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 23:04:46 +0100 Subject: render: urls: reduce vertical margin --- render.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 57af555c..06065436 100644 --- a/render.c +++ b/render.c @@ -2598,9 +2598,10 @@ render_urls(struct terminal *term) size_t len = wcslen(label); int cols = wcswidth(label, len); - const int margin = 2 * term->scale; - int width = 2 * margin + cols * term->cell_width; - int height = 2 * margin + term->cell_height; + const int x_margin = 2 * term->scale; + const int y_margin = 1 * term->scale; + int width = 2 * x_margin + cols * term->cell_width; + int height = 2 * y_margin + term->cell_height; struct buffer *buf = shm_get_buffer( term->wl->shm, width, height, shm_cookie_url(url), false, 1); @@ -2636,12 +2637,12 @@ render_urls(struct terminal *term) uint16_t alpha = 0xffff; render_osd(term, surf, sub_surf, buf, label, - fg, bg, alpha, width, height, margin, margin); + fg, bg, alpha, width, height, x_margin, y_margin); #if 0 /* TODO: somehow highlight the key(s) entered so far */ pixman_color_t color = color_hex_to_pixman(fg); - draw_strikeout(term, buf->pix[0], term->fonts[0], &color, margin, margin, entered_key_len); + draw_strikeout(term, buf->pix[0], term->fonts[0], &color, x_margin, y_margin, entered_key_len); #endif } } -- cgit v1.3 From 0f57e6c422885d1c2abc13711ad40ec176c177d3 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Feb 2021 10:34:36 +0100 Subject: render: render_osd(): remove ‘alpha’ parameter - it’s always 0xffff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- render.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 06065436..985f97de 100644 --- a/render.c +++ b/render.c @@ -1717,10 +1717,10 @@ static void render_osd(struct terminal *term, struct wl_surface *surf, struct wl_subsurface *sub_surf, struct buffer *buf, - const wchar_t *text, uint32_t _fg, uint32_t _bg, int alpha, + const wchar_t *text, uint32_t _fg, uint32_t _bg, unsigned width, unsigned height, unsigned x, unsigned y) { - pixman_color_t bg = color_hex_to_pixman_with_alpha(_bg, alpha); + pixman_color_t bg = color_hex_to_pixman(_bg); pixman_image_fill_rectangles( PIXMAN_OP_SRC, buf->pix[0], &bg, 1, &(pixman_rectangle16_t){0, 0, width, height}); @@ -1907,7 +1907,7 @@ render_scrollback_position(struct terminal *term) term, win->scrollback_indicator_surface, win->scrollback_indicator_sub_surface, buf, text, - term->colors.table[0], term->colors.table[8 + 4], 0xffff, + term->colors.table[0], term->colors.table[8 + 4], width, height, width - margin - wcslen(text) * term->cell_width, margin); } @@ -1939,7 +1939,7 @@ render_render_timer(struct terminal *term, struct timeval render_time) term, win->render_timer_surface, win->render_timer_sub_surface, buf, text, - term->colors.table[0], term->colors.table[8 + 1], 0xffff, + term->colors.table[0], term->colors.table[8 + 1], width, height, margin, margin); } @@ -2634,12 +2634,10 @@ render_urls(struct terminal *term) ? term->conf->colors.jump_label.bg : term->colors.table[3]; - uint16_t alpha = 0xffff; - render_osd(term, surf, sub_surf, buf, label, - fg, bg, alpha, width, height, x_margin, y_margin); + fg, bg, width, height, x_margin, y_margin); -#if 0 +#if 1 /* TODO: somehow highlight the key(s) entered so far */ pixman_color_t color = color_hex_to_pixman(fg); draw_strikeout(term, buf->pix[0], term->fonts[0], &color, x_margin, y_margin, entered_key_len); -- cgit v1.3 From b3043e92f6e12d809ba101baa6bad68e24eaa0ca Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Feb 2021 10:50:31 +0100 Subject: render: urls: don’t use/allocate maximum allowed characters unless needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 985f97de..8e0e8f9c 100644 --- a/render.c +++ b/render.c @@ -2579,7 +2579,7 @@ render_urls(struct terminal *term) size_t chars = wcslen(key) + (text_len > 0 ? 3 + text_len : 0); const size_t max_chars = 50; - chars = max(chars, max_chars); + chars = min(chars, max_chars); wchar_t label[chars + 2]; if (text_len == 0) -- cgit v1.3 From 85f7503aec7fe1f525d47ebfd670e1545b44f357 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Feb 2021 10:51:09 +0100 Subject: render: urls: fix string formatter in swprintf() --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 8e0e8f9c..7ed27f44 100644 --- a/render.c +++ b/render.c @@ -2585,7 +2585,7 @@ render_urls(struct terminal *term) if (text_len == 0) wcscpy(label, key); else { - int count = swprintf(label, chars, L"%s - %s", key, text); + int count = swprintf(label, chars + 1, L"%ls - %ls", key, text); if (count >= max_chars) { label[max_chars] = L'…'; label[max_chars + 1] = L'\0'; -- cgit v1.3 From 5af481cd89ba83c7228b0f40c709b6da36a7a34e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Feb 2021 10:51:28 +0100 Subject: render: urls: blank out keys already pressed --- render.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 7ed27f44..c64692b8 100644 --- a/render.c +++ b/render.c @@ -2595,6 +2595,9 @@ render_urls(struct terminal *term) for (size_t i = 0; i < wcslen(key); i++) label[i] = towupper(label[i]); + for (size_t i = 0; i < entered_key_len; i++) + label[i] = L' '; + size_t len = wcslen(label); int cols = wcswidth(label, len); @@ -2636,12 +2639,6 @@ render_urls(struct terminal *term) render_osd(term, surf, sub_surf, buf, label, fg, bg, width, height, x_margin, y_margin); - -#if 1 - /* TODO: somehow highlight the key(s) entered so far */ - pixman_color_t color = color_hex_to_pixman(fg); - draw_strikeout(term, buf->pix[0], term->fonts[0], &color, x_margin, y_margin, entered_key_len); -#endif } } -- cgit v1.3