From 89559d5466502176995720a33316714012a638d1 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 16 Apr 2020 18:51:14 +0200 Subject: grid: move 'cursor' state from terminal to grid This way, the 'normal' and 'alt' grids have their own cursor state, and we don't need to switch between them. --- render.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index df67d85b..f0decd93 100644 --- a/render.c +++ b/render.c @@ -1373,7 +1373,7 @@ grid_render(struct terminal *term) */ bool cursor_is_visible = false; int view_end = (term->grid->view + term->rows - 1) & (term->grid->num_rows - 1); - int cursor_row = (term->grid->offset + term->cursor.point.row) & (term->grid->num_rows - 1); + int cursor_row = (term->grid->offset + term->grid->cursor.point.row) & (term->grid->num_rows - 1); if (view_end >= term->grid->view) { /* Not wrapped */ if (cursor_row >= term->grid->view && cursor_row <= view_end) @@ -1401,21 +1401,21 @@ grid_render(struct terminal *term) int view_aligned_row = (cursor_row - term->grid->view + term->grid->num_rows) & (term->grid->num_rows - 1); - term->render.last_cursor.actual = term->cursor.point; + term->render.last_cursor.actual = term->grid->cursor.point; term->render.last_cursor.in_view = (struct coord) { - term->cursor.point.col, view_aligned_row}; + term->grid->cursor.point.col, view_aligned_row}; struct row *row = grid_row_in_view(term->grid, view_aligned_row); - struct cell *cell = &row->cells[term->cursor.point.col]; + struct cell *cell = &row->cells[term->grid->cursor.point.col]; cell->attrs.clean = 0; term->render.last_cursor.cell = cell; int cols_updated = render_cell( - term, buf->pix, cell, term->cursor.point.col, view_aligned_row, true); + term, buf->pix, cell, term->grid->cursor.point.col, view_aligned_row, true); wl_surface_damage_buffer( term->window->surface, - term->margins.left + term->cursor.point.col * term->cell_width, + term->margins.left + term->grid->cursor.point.col * term->cell_width, term->margins.top + view_aligned_row * term->cell_height, cols_updated * term->cell_width, term->cell_height); } @@ -1804,7 +1804,7 @@ maybe_resize(struct terminal *term, int width, int height, bool force) term_cursor_to( term, cursor_row, - min(term->cursor.point.col, term->cols - 1)); + min(term->grid->cursor.point.col, term->cols - 1)); /* If in alt screen, update the saved 'normal' cursor too */ if (term->grid == &term->alt) { @@ -1813,10 +1813,10 @@ maybe_resize(struct terminal *term, int width, int height, bool force) while (cursor_row < 0) cursor_row += term->grid->num_rows; - term->alt_saved_cursor.lcf = false; - term->alt_saved_cursor.point.row = cursor_row; - term->alt_saved_cursor.point.col = min( - term->alt_saved_cursor.point.col, term->cols - 1); + term->normal.cursor.lcf = false; + term->normal.cursor.point.row = cursor_row; + term->normal.cursor.point.col = min( + term->normal.cursor.point.col, term->cols - 1); } term->render.last_cursor.cell = NULL; -- cgit v1.3 From 5546b40369b7bc20201a8f3a327be0ff84af7f5a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 16 Apr 2020 19:38:30 +0200 Subject: grid: grid_reflow() now translates cursor coordinates --- grid.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++------- grid.h | 2 +- render.c | 36 ++---------------------------------- 3 files changed, 54 insertions(+), 42 deletions(-) (limited to 'render.c') diff --git a/grid.c b/grid.c index 056dd791..b8659b55 100644 --- a/grid.c +++ b/grid.c @@ -53,7 +53,7 @@ grid_row_free(struct row *row) free(row); } -int +void grid_reflow(struct grid *grid, int new_rows, int new_cols, int old_screen_rows, int new_screen_rows) { @@ -79,13 +79,26 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, tll(struct sixel) new_sixels = tll_init(); + /* Turn cursor coordinates into grid absolute coordinates */ + struct coord cursor = grid->cursor.point; + struct coord new_cursor = {}; + cursor.row += grid->offset; + cursor.row &= old_rows - 1; + + struct coord saved_cursor = grid->saved_cursor.point; + struct coord new_saved_cursor = {}; + saved_cursor.row += grid->offset; + saved_cursor.row &= old_rows - 1; + /* * Walk the old grid */ for (int r = 0; r < old_rows; r++) { + const size_t old_row_idx = (offset + r) & (old_rows - 1); + /* Unallocated (empty) rows we can simply skip */ - const struct row *old_row = old_grid[(offset + r) & (old_rows - 1)]; + const struct row *old_row = old_grid[old_row_idx]; if (old_row == NULL) continue; @@ -102,7 +115,7 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, * the "real" sixel list. */ tll_foreach(grid->sixel_images, it) { - if (it->item.pos.row == ((offset + r) & (old_rows - 1))) { + if (it->item.pos.row == old_row_idx) { struct sixel six = it->item; six.pos.row = new_row_idx; @@ -122,7 +135,13 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, /* Walk current line of the old grid */ for (int c = 0; c < old_cols; c++) { - if (old_row->cells[c].wc == 0) { + bool has_cursor = cursor.row == old_row_idx && cursor.col == c; + bool has_saved_cursor + = saved_cursor.row == old_row_idx && saved_cursor.col == c; + + if (old_row->cells[c].wc == 0 && !has_cursor && !has_saved_cursor) { + assert(!has_cursor); + assert(!has_saved_cursor); empty_count++; continue; } @@ -168,6 +187,12 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, new_row->cells[new_col_idx] = *old_cell; new_row->cells[new_col_idx].attrs.clean = 1; + + if (has_cursor) + new_cursor = (struct coord){new_col_idx, new_row_idx}; + if (has_saved_cursor) + new_saved_cursor = (struct coord){new_col_idx, new_row_idx}; + new_col_idx++; } @@ -211,11 +236,32 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, grid_row_free(old_grid[r]); free(grid->rows); - grid->cur_row = new_grid[new_row_idx]; + grid->cur_row = new_grid[new_cursor.row]; grid->rows = new_grid; grid->num_rows = new_rows; grid->num_cols = new_cols; + /* Convert absolute coordinates to screen relative */ + new_cursor.row -= grid->offset; + while (new_cursor.row < 0) + new_cursor.row += grid->num_rows; + + assert(new_cursor.row >= 0); + assert(new_cursor.row < grid->num_rows); + + new_saved_cursor.row -= grid->offset; + while (new_saved_cursor.row < 0) + new_saved_cursor.row += grid->num_rows; + + assert(new_saved_cursor.row >= 0); + assert(new_saved_cursor.row < grid->num_rows); + + grid->cursor.point = new_cursor; + grid->saved_cursor.point = new_saved_cursor; + + grid->cursor.lcf = false; + grid->saved_cursor.lcf = false; + /* Destroy any non-moved sixels */ tll_foreach(grid->sixel_images, it) sixel_destroy(&it->item); @@ -225,6 +271,4 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, tll_foreach(new_sixels, it) tll_push_back(grid->sixel_images, it->item); tll_free(new_sixels); - - return new_row_idx; } diff --git a/grid.h b/grid.h index 50e2c2b7..d4045189 100644 --- a/grid.h +++ b/grid.h @@ -6,7 +6,7 @@ void grid_swap_row(struct grid *grid, int row_a, int row_b, bool initialize); struct row *grid_row_alloc(int cols, bool initialize); void grid_row_free(struct row *row); -int grid_reflow( +void grid_reflow( struct grid *grid, int new_rows, int new_cols, int old_screen_rows, int new_screen_rows); diff --git a/render.c b/render.c index f0decd93..ef0f0608 100644 --- a/render.c +++ b/render.c @@ -1754,10 +1754,8 @@ maybe_resize(struct terminal *term, int width, int height, bool force) } /* Reflow grids */ - int last_normal_row = grid_reflow( - &term->normal, new_normal_grid_rows, new_cols, old_rows, new_rows); - int last_alt_row = grid_reflow( - &term->alt, new_alt_grid_rows, new_cols, old_rows, new_rows); + grid_reflow(&term->normal, new_normal_grid_rows, new_cols, old_rows, new_rows); + grid_reflow(&term->alt, new_alt_grid_rows, new_cols, old_rows, new_rows); /* Reset tab stops */ tll_free(term->tab_stops); @@ -1789,36 +1787,6 @@ maybe_resize(struct terminal *term, int width, int height, bool force) if (term->scroll_region.end >= old_rows) term->scroll_region.end = term->rows; - /* Position cursor at the last copied row */ - /* TODO: can we do better? */ - int cursor_row = term->grid == &term->normal - ? last_normal_row - term->normal.offset - : last_alt_row - term->alt.offset; - - while (cursor_row < 0) - cursor_row += term->grid->num_rows; - - assert(cursor_row >= 0); - assert(cursor_row < term->rows); - - term_cursor_to( - term, - cursor_row, - min(term->grid->cursor.point.col, term->cols - 1)); - - /* If in alt screen, update the saved 'normal' cursor too */ - if (term->grid == &term->alt) { - int cursor_row = last_normal_row - term->normal.offset; - - while (cursor_row < 0) - cursor_row += term->grid->num_rows; - - term->normal.cursor.lcf = false; - term->normal.cursor.point.row = cursor_row; - term->normal.cursor.point.col = min( - term->normal.cursor.point.col, term->cols - 1); - } - term->render.last_cursor.cell = NULL; damage_view: -- cgit v1.3 From ef52ed8a109af7512bd9e9ed62d74e36ae2deb41 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 17 Apr 2020 21:04:32 +0200 Subject: grid: reflow: caller may now pass a list of coordinates that should be translated --- grid.c | 7 ++++++- grid.h | 4 +++- render.c | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) (limited to 'render.c') diff --git a/grid.c b/grid.c index 3e34c891..b50a095d 100644 --- a/grid.c +++ b/grid.c @@ -55,7 +55,9 @@ grid_row_free(struct row *row) void grid_reflow(struct grid *grid, int new_rows, int new_cols, - int old_screen_rows, int new_screen_rows) + int old_screen_rows, int new_screen_rows, + size_t tracking_points_count, + struct coord *_tracking_points[static tracking_points_count]) { struct row *const *old_grid = grid->rows; const int old_rows = grid->num_rows; @@ -90,6 +92,9 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, tll_push_back(tracking_points, &cursor); tll_push_back(tracking_points, &saved_cursor); + for (size_t i = 0; i < tracking_points_count; i++) + tll_push_back(tracking_points, _tracking_points[i]); + /* * Walk the old grid */ diff --git a/grid.h b/grid.h index d4045189..a2ff4b99 100644 --- a/grid.h +++ b/grid.h @@ -8,7 +8,9 @@ struct row *grid_row_alloc(int cols, bool initialize); void grid_row_free(struct row *row); void grid_reflow( struct grid *grid, int new_rows, int new_cols, - int old_screen_rows, int new_screen_rows); + int old_screen_rows, int new_screen_rows, + size_t tracking_points_count, + struct coord *tracking_points[static tracking_points_count]); static inline int grid_row_absolute(const struct grid *grid, int row_no) diff --git a/render.c b/render.c index ef0f0608..ca0342f2 100644 --- a/render.c +++ b/render.c @@ -1754,8 +1754,8 @@ maybe_resize(struct terminal *term, int width, int height, bool force) } /* Reflow grids */ - grid_reflow(&term->normal, new_normal_grid_rows, new_cols, old_rows, new_rows); - grid_reflow(&term->alt, new_alt_grid_rows, new_cols, old_rows, new_rows); + grid_reflow(&term->normal, new_normal_grid_rows, new_cols, old_rows, new_rows, 0, NULL); + grid_reflow(&term->alt, new_alt_grid_rows, new_cols, old_rows, new_rows, 0, NULL); /* Reset tab stops */ tll_free(term->tab_stops); -- cgit v1.3 From 5509fe514afa64c972758ace3b81b462aba9694f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 17 Apr 2020 21:11:30 +0200 Subject: render: resize: no need to clear the selection - just truncate if necessary --- render.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index ca0342f2..7b5391b6 100644 --- a/render.c +++ b/render.c @@ -1711,8 +1711,6 @@ maybe_resize(struct terminal *term, int width, int height, bool force) if (!force && width == term->width && height == term->height && scale == term->scale) return false; - selection_cancel(term); - /* Cancel an application initiated "Synchronized Update" */ term_disable_app_sync_updates(term); @@ -1814,6 +1812,12 @@ damage_view: term->height / term->scale + title_height); } + /* Make sure selection is within bounds */ + term->selection.start.row = min(term->selection.start.row, term->rows - 1); + term->selection.start.col = min(term->selection.start.col, term->cols - 1); + term->selection.end.row = min(term->selection.end.row, term->rows - 1); + term->selection.end.col = min(term->selection.end.col, term->cols - 1); + tll_free(term->normal.scroll_damage); tll_free(term->alt.scroll_damage); -- cgit v1.3