From 95eaad7ce4b40d1401cbc982966a9c7a0036e1fc Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 5 Nov 2019 13:27:37 +0100 Subject: csi: implement DECOM - switch cursor origin between absolute and relative The default is absolute mode, where 0,0 is the upper left corner of the screen. In relative mode, the origin is relative the top scroll margin. Internally, we always track the current cursor position in absolute mode. Every time we the client *sets* or *queries* the cursor position in relative mode, we translate it to absolute. --- terminal.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index b7523d1b..2be46130 100644 --- a/terminal.c +++ b/terminal.c @@ -486,6 +486,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, }, .alpha = conf->colors.alpha, }, + .origin = ORIGIN_ABSOLUTE, .default_cursor_style = conf->cursor.style, .cursor_style = conf->cursor.style, .default_cursor_color = { @@ -991,6 +992,30 @@ term_erase(struct terminal *term, const struct coord *start, const struct coord erase_cell_range(term, grid_row(term->grid, end->row), 0, end->col); } +struct coord +term_cursor_rel_to_abs(const struct terminal *term, int row, int col) +{ + switch (term->origin) { + case ORIGIN_ABSOLUTE: + return (struct coord) { + .col = min(col, term->cols - 1), + .row = min(row, term->rows - 1), + }; + break; + + case ORIGIN_RELATIVE: { + return (struct coord) { + .col = min(col, term->cols - 1), + .row = min(row + term->scroll_region.start, term->rows - 1), + }; + break; + } + } + + assert(false); + return (struct coord){-1, -1}; +} + void term_cursor_to(struct terminal *term, int row, int col) { @@ -1005,6 +1030,13 @@ term_cursor_to(struct terminal *term, int row, int col) term->grid->cur_row = grid_row(term->grid, row); } +void +term_cursor_home(struct terminal *term) +{ + struct coord new_cursor = term_cursor_rel_to_abs(term, 0, 0); + term_cursor_to(term, new_cursor.row, new_cursor.col); +} + void term_cursor_left(struct terminal *term, int count) { -- cgit v1.3