From fd87bca102321e5804fef74dac8a6d267ae46732 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 13 Feb 2021 12:31:55 +0100 Subject: grid: enable rows to have ‘extra’ data associated with them MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds an ‘extra’ member to the row struct. It is a pointer to a struct containing extra data to be associated with this row. Initially, this struct contains a list of URL ranges. These define (OSC-8) URLs on this row. The ‘extra’ data is allocated on-demand. I.e. the pointer is NULL by default; it is *not* allocated by grid_row_alloc(). --- grid.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 6d6b6b3c..2f0033ec 100644 --- a/grid.c +++ b/grid.c @@ -33,6 +33,7 @@ grid_row_alloc(int cols, bool initialize) struct row *row = xmalloc(sizeof(*row)); row->dirty = false; row->linebreak = false; + row->extra = NULL; if (initialize) { row->cells = xcalloc(cols, sizeof(row->cells[0])); @@ -50,6 +51,14 @@ grid_row_free(struct row *row) if (row == NULL) return; + if (row->extra != NULL) { + tll_foreach(row->extra->uri_ranges, it) { + free(it->item.uri); + tll_remove(row->extra->uri_ranges, it); + } + } + + free(row->extra); free(row->cells); free(row); } -- cgit v1.3 From 17f90eeec44d272e926582d2bc4fae98c7073c5b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Feb 2021 20:34:25 +0100 Subject: grid: grid_row_free(): use grid_row_reset_extra() --- grid.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 2f0033ec..8a04bb07 100644 --- a/grid.c +++ b/grid.c @@ -51,13 +51,7 @@ grid_row_free(struct row *row) if (row == NULL) return; - if (row->extra != NULL) { - tll_foreach(row->extra->uri_ranges, it) { - free(it->item.uri); - tll_remove(row->extra->uri_ranges, it); - } - } - + grid_row_reset_extra(row); free(row->extra); free(row->cells); free(row); -- cgit v1.3 From 3ca5a65c339c1214d903e4b897f9b288a1083137 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Feb 2021 20:34:49 +0100 Subject: grid: reflow: translate URI ranges URI ranges are per row. Translate by detecting URI range start/end coordinates, and opening and closing a corresponding URI range on the new grid. We need to take care when line-wrapping the new grid; here we need to manually close the still-open URI ranges (on the new grid), and re-opening them on the next row. --- grid.c | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 137 insertions(+), 22 deletions(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 8a04bb07..d81e92c4 100644 --- a/grid.c +++ b/grid.c @@ -169,6 +169,109 @@ grid_resize_without_reflow( #endif } +static void +reflow_uri_ranges(const struct row *old_row, struct row *new_row, + int old_col_idx, int new_col_idx) +{ + if (old_row->extra == NULL) + return; + + /* + * Check for URI range start/end points on the “old” row, and + * open/close a corresponding URI range on the “new” row. + */ + + tll_foreach(old_row->extra->uri_ranges, it) { + if (it->item.start == old_col_idx) { + struct row_uri_range new_range = { + .start = new_col_idx, + .end = -1, + .id = it->item.id, + .uri = xstrdup(it->item.uri), + }; + + if (new_row->extra == NULL) + new_row->extra = xcalloc(1, sizeof(*new_row->extra)); + tll_push_back(new_row->extra->uri_ranges, new_range); + } + + else if (it->item.end == old_col_idx) { + xassert(new_row->extra != NULL); + + bool found_it = false; + tll_foreach(new_row->extra->uri_ranges, it2) { + if (it2->item.id != it->item.id) + continue; + if (it2->item.end >= 0) + continue; + + it2->item.end = new_col_idx; + found_it = true; + break; + } + xassert(found_it); + } + } +} + +static struct row * +_line_wrap(struct grid *old_grid, struct row **new_grid, struct row *row, + int *row_idx, int *col_idx, int row_count, int col_count) +{ + *col_idx = 0; + *row_idx = (*row_idx + 1) & (row_count - 1); + + struct row *new_row = new_grid[*row_idx]; + + if (new_row == NULL) { + /* Scrollback not yet full, allocate a completely new row */ + new_row = grid_row_alloc(col_count, true); + new_grid[*row_idx] = new_row; + } else { + /* Scrollback is full, need to re-use a row */ + memset(new_row->cells, 0, col_count * sizeof(new_row->cells[0])); + grid_row_reset_extra(new_row); + new_row->linebreak = false; + + tll_foreach(old_grid->sixel_images, it) { + if (it->item.pos.row == *row_idx) { + sixel_destroy(&it->item); + tll_remove(old_grid->sixel_images, it); + } + } + } + + if (row->extra == NULL) + return new_row; + + /* + * URI ranges are per row. Thus, we need to ‘close’ the still-open + * ranges on the previous row, and re-open them on the + * next/current row. + */ + tll_foreach(row->extra->uri_ranges, it) { + if (it->item.end >= 0) + continue; + + /* Terminate URI range on the previous row */ + it->item.end = col_count - 1; + + /* Open a new range on the new/current row */ + struct row_uri_range new_range = { + .start = 0, + .end = -1, + .id = it->item.id, + .uri = xstrdup(it->item.uri), + }; + + if (new_row->extra == NULL) + new_row->extra = xcalloc(1, sizeof(*new_row->extra)); + tll_push_back(new_row->extra->uri_ranges, new_range); + } + + return new_row; +} + void grid_resize_and_reflow( struct grid *grid, int new_rows, int new_cols, @@ -248,29 +351,13 @@ grid_resize_and_reflow( tll_remove(untranslated_sixels, it); } -#define line_wrap() \ +#define line_wrap() \ + new_row = _line_wrap( \ + grid, new_grid, new_row, &new_row_idx, &new_col_idx, \ + new_rows, new_cols) + +#define print_spacer() \ do { \ - new_col_idx = 0; \ - new_row_idx = (new_row_idx + 1) & (new_rows - 1); \ - \ - new_row = new_grid[new_row_idx]; \ - if (new_row == NULL) { \ - new_row = grid_row_alloc(new_cols, true); \ - new_grid[new_row_idx] = new_row; \ - } else { \ - memset(new_row->cells, 0, new_cols * sizeof(new_row->cells[0])); \ - new_row->linebreak = false; \ - tll_foreach(grid->sixel_images, it) { \ - if (it->item.pos.row == new_row_idx) { \ - sixel_destroy(&it->item); \ - tll_remove(grid->sixel_images, it); \ - } \ - } \ - } \ - } while(0) - -#define print_spacer() \ - do { \ new_row->cells[new_col_idx].wc = CELL_MULT_COL_SPACER; \ new_row->cells[new_col_idx].attrs = old_cell->attrs; \ new_row->cells[new_col_idx].attrs.clean = 1; \ @@ -297,6 +384,17 @@ grid_resize_and_reflow( } } + /* If there’s an URI start/end point here, we need to make + * sure we handle it */ + if (old_row->extra != NULL) { + tll_foreach(old_row->extra->uri_ranges, it) { + if (it->item.start == c || it->item.end == c) { + is_tracking_point = true; + break; + } + } + } + if (old_row->cells[c].wc == 0 && !is_tracking_point) { empty_count++; continue; @@ -359,6 +457,8 @@ grid_resize_and_reflow( tll_remove(tracking_points, it); } } + + reflow_uri_ranges(old_row, new_row, c, new_col_idx); } new_col_idx++; } @@ -385,6 +485,21 @@ grid_resize_and_reflow( #undef line_wrap } +#if defined(_DEBUG) + /* Verify all URI ranges have been “closed” */ + for (int r = 0; r < new_rows; r++) { + const struct row *row = new_grid[r]; + + if (row == NULL) + continue; + if (row->extra == NULL) + continue; + + tll_foreach(row->extra->uri_ranges, it) + xassert(it->item.end >= 0); + } +#endif + /* Set offset such that the last reflowed row is at the bottom */ grid->offset = new_row_idx - new_screen_rows + 1; while (grid->offset < 0) -- cgit v1.3 From 8da82c897b82220bb96a55500e33b06850296c1e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Feb 2021 20:42:42 +0100 Subject: grid: grid_resize_without_reflow: transfer URI ranges --- grid.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index d81e92c4..16924d1c 100644 --- a/grid.c +++ b/grid.c @@ -114,6 +114,27 @@ grid_resize_without_reflow( sixel_destroy(&it->item); tll_remove(untranslated_sixels, it); } + + /* Copy URI ranges, truncating them if necessary */ + if (old_row->extra == NULL) + continue; + + new_row->extra = xcalloc(1, sizeof(*new_row->extra)); + tll_foreach(old_row->extra->uri_ranges, it) { + if (it->item.start >= new_rows) { + /* The whole range is truncated */ + continue; + } + + struct row_uri_range range = { + .start = it->item.start, + .end = min(it->item.end, new_cols - 1), + .id = it->item.id, + .uri = xstrdup(it->item.uri), + }; + + tll_push_back(new_row->extra->uri_ranges, range); + } } /* Clear "new" lines */ @@ -122,7 +143,6 @@ grid_resize_without_reflow( new_grid[(new_offset + r) & (new_rows - 1)] = new_row; memset(new_row->cells, 0, sizeof(struct cell) * new_cols); - new_row->linebreak = false; new_row->dirty = true; } -- cgit v1.3 From fd505f2274343d03e26847217c68e3c8f1f86e30 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Feb 2021 20:45:12 +0100 Subject: grid: resize_without_reflow: allocate ‘extra’ on-demand on ‘new’ rows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Even if we have URI ranges on the old row, all those ranges may lay outside the new grid’s range. --- grid.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 16924d1c..7e000b42 100644 --- a/grid.c +++ b/grid.c @@ -119,7 +119,6 @@ grid_resize_without_reflow( if (old_row->extra == NULL) continue; - new_row->extra = xcalloc(1, sizeof(*new_row->extra)); tll_foreach(old_row->extra->uri_ranges, it) { if (it->item.start >= new_rows) { /* The whole range is truncated */ @@ -133,6 +132,8 @@ grid_resize_without_reflow( .uri = xstrdup(it->item.uri), }; + if (new_row->extra == NULL) + new_row->extra = xcalloc(1, sizeof(*new_row->extra)); tll_push_back(new_row->extra->uri_ranges, range); } } -- cgit v1.3 From 5eea06cff99abbac234d517e84e31576234c7e2e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Feb 2021 20:50:33 +0100 Subject: grid: add new function grid_row_add_uri_range() --- grid.c | 9 +++++++++ grid.h | 2 ++ 2 files changed, 11 insertions(+) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 7e000b42..72d8505e 100644 --- a/grid.c +++ b/grid.c @@ -588,3 +588,12 @@ grid_resize_and_reflow( tll_free(tracking_points); } + +void +grid_row_add_uri_range(struct row *row, struct row_uri_range range) +{ + if (row->extra == NULL) + row->extra = xcalloc(1, sizeof(*row->extra)); + + tll_push_back(row->extra->uri_ranges, range); +} diff --git a/grid.h b/grid.h index fffeca8a..4cb339f2 100644 --- a/grid.h +++ b/grid.h @@ -73,6 +73,8 @@ grid_row_in_view(struct grid *grid, int row_no) return row; } +void grid_row_add_uri_range(struct row *row, struct row_uri_range range); + static inline void grid_row_reset_extra(struct row *row) { -- cgit v1.3 From d42b129814e120bf01ee3bfa06f399a81dec6c3d Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Feb 2021 20:50:46 +0100 Subject: grid: refactor: use grid_row_add_uri_range() --- grid.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 72d8505e..93e4f8c8 100644 --- a/grid.c +++ b/grid.c @@ -131,10 +131,7 @@ grid_resize_without_reflow( .id = it->item.id, .uri = xstrdup(it->item.uri), }; - - if (new_row->extra == NULL) - new_row->extra = xcalloc(1, sizeof(*new_row->extra)); - tll_push_back(new_row->extra->uri_ranges, range); + grid_row_add_uri_range(new_row, range); } } @@ -210,10 +207,7 @@ reflow_uri_ranges(const struct row *old_row, struct row *new_row, .id = it->item.id, .uri = xstrdup(it->item.uri), }; - - if (new_row->extra == NULL) - new_row->extra = xcalloc(1, sizeof(*new_row->extra)); - tll_push_back(new_row->extra->uri_ranges, new_range); + grid_row_add_uri_range(new_row, new_range); } else if (it->item.end == old_col_idx) { @@ -284,10 +278,7 @@ _line_wrap(struct grid *old_grid, struct row **new_grid, struct row *row, .id = it->item.id, .uri = xstrdup(it->item.uri), }; - - if (new_row->extra == NULL) - new_row->extra = xcalloc(1, sizeof(*new_row->extra)); - tll_push_back(new_row->extra->uri_ranges, new_range); + grid_row_add_uri_range(new_row, new_range); } return new_row; -- cgit v1.3 From a0b977fcee7a3095d548b6cb9131e77f16ac3770 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Feb 2021 20:52:26 +0100 Subject: grid: refactor: break out allocation of ‘extra’ member to separate function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- grid.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 93e4f8c8..00890a1d 100644 --- a/grid.c +++ b/grid.c @@ -580,11 +580,16 @@ grid_resize_and_reflow( tll_free(tracking_points); } -void -grid_row_add_uri_range(struct row *row, struct row_uri_range range) +static void +ensure_row_has_extra_data(struct row *row) { if (row->extra == NULL) row->extra = xcalloc(1, sizeof(*row->extra)); +} +void +grid_row_add_uri_range(struct row *row, struct row_uri_range range) +{ + ensure_row_has_extra_data(row); tll_push_back(row->extra->uri_ranges, range); } -- cgit v1.3