diff options
Diffstat (limited to 'terminal.c')
| -rw-r--r-- | terminal.c | 259 |
1 files changed, 151 insertions, 108 deletions
@@ -4,8 +4,9 @@ #include <assert.h> #define LOG_MODULE "terminal" -#define LOG_ENABLE_DBG 1 +#define LOG_ENABLE_DBG 0 #include "log.h" +#include "grid.h" #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) @@ -37,10 +38,6 @@ damage_merge_range(struct terminal *term, const struct damage *dmg) old->range.start = new_start; old->range.length = new_end - new_start; - assert(old->range.start >= 0); - assert(old->range.start < term->rows * term->cols); - assert(old->range.length >= 0); - assert(old->range.start + old->range.length <= term->rows * term->cols); return true; } @@ -51,16 +48,18 @@ static void term_damage_update_or_erase(struct terminal *term, enum damage_type damage_type, int start, int length) { +#if 1 + if (tll_length(term->grid->damage) > 1024) { + term_damage_all(term); + return; + } +#endif + struct damage dmg = { .type = damage_type, - .range = {.start = start, .length = length}, + .range = {.start = term->grid->offset + start, .length = length}, }; - assert(dmg.range.start >= 0); - assert(dmg.range.start < term->rows * term->cols); - assert(dmg.range.length >= 0); - assert(dmg.range.start + dmg.range.length <= term->rows * term->cols); - if (damage_merge_range(term, &dmg)) return; @@ -70,12 +69,14 @@ term_damage_update_or_erase(struct terminal *term, enum damage_type damage_type, void term_damage_update(struct terminal *term, int start, int length) { + assert(start + length <= term->rows * term->cols); term_damage_update_or_erase(term, DAMAGE_UPDATE, start, length); } void term_damage_erase(struct terminal *term, int start, int length) { + assert(start + length <= term->rows * term->cols); term_damage_update_or_erase(term, DAMAGE_ERASE, start, length); } @@ -87,82 +88,53 @@ term_damage_all(struct terminal *term) term_damage_update(term, 0, term->rows * term->cols); } +#if 0 static void damage_adjust_after_scroll(struct terminal *term, enum damage_type damage_type, struct scroll_region region, int lines) { - const int adjustment - = lines * term->cols * (damage_type == DAMAGE_SCROLL_REVERSE ? -1 : 1); - - const int scroll_start = region.start * term->cols; - const int scroll_end = region.end * term->cols; - tll_foreach(term->grid->damage, it) { - int start = it->item.range.start; - int length = it->item.range.length; - int end = start + length; - - if (start < scroll_start && end > scroll_start) { - /* Start outside, end either inside or on the other side */ - struct damage outside = { - .type = it->item.type, - .range = {.start = start, .length = scroll_start - start}, - }; - - tll_push_back(term->grid->damage, outside); - start = scroll_start; - length = end - start; - - } - - if (start < scroll_end && end > scroll_end) { - /* End outside, start either inside or on the other side */ - struct damage outside = { - .type = it->item.type, - .range = {.start = scroll_end, .length = length - scroll_end}, - }; - - tll_push_back(term->grid->damage, outside); - end = scroll_end; - length = end - start; +#if 0 + if (it->item.range.start < term->grid->offset) { + int end = it->item.range.start + it->item.range.length; + if (end >= term->grid->offset) { + it->item.range.start = term->grid->offset; + it->item.range.length = end - it->item.range.start; + } else { + tll_remove(term->grid->damage, it); + } } +#endif - if (start >= scroll_start && end <= scroll_end) { - /* Completely inside scroll region */ - start -= adjustment; - it->item.range.start = start; - - if (start < scroll_start) { - /* Scrolled up outside scroll region */ - int new_length = length - (scroll_start - start); - assert(new_length < length); - - if (new_length <= 0) - tll_remove(term->grid->damage, it); - else { - it->item.range.start = scroll_start; - it->item.range.length = new_length; - } - } + int start = it->item.range.start; + int end = start + it->item.range.length; - if (start + length > scroll_end) { - /* Scrolled down outside scroll region */ - if (start >= scroll_end) - tll_remove(term->grid->damage, it); - else { - it->item.range.start = start; - it->item.range.length = scroll_end - start; - } - } - } + if (start - } } +#endif void term_damage_scroll(struct terminal *term, enum damage_type damage_type, struct scroll_region region, int lines) { - damage_adjust_after_scroll(term, damage_type, region, lines); + //damage_adjust_after_scroll(term, damage_type, region, lines); + if (damage_type == DAMAGE_SCROLL) { + tll_foreach(term->grid->damage, it) { + int start = it->item.range.start; + int length = it->item.range.length; + + if (start < term->grid->offset) { + int end = start + length; + if (end >= term->grid->offset) { + it->item.range.start = term->grid->offset; + it->item.range.length = end - it->item.range.start; + } else + tll_remove(term->grid->damage, it); + } else + break; + } + } if (tll_length(term->grid->scroll_damage) > 0) { struct damage *dmg = &tll_back(term->grid->scroll_damage); @@ -185,8 +157,11 @@ term_damage_scroll(struct terminal *term, enum damage_type damage_type, void term_erase(struct terminal *term, int start, int end) { + LOG_DBG("erase: %d-%d", start, end); assert(end >= start); - memset(&term->grid->cells[start], 0, (end - start) * sizeof(term->grid->cells[0])); + assert(end <= term->rows * term->cols); + + grid_memset(term->grid, start, 0, end - start); term_damage_erase(term, start, end - start); } @@ -199,36 +174,41 @@ term_cursor_linear(const struct terminal *term, int row, int col) void term_cursor_to(struct terminal *term, int row, int col) { - assert(row >= 0); assert(row < term->rows); - assert(col >= 0); assert(col < term->cols); int new_linear = row * term->cols + col; - assert(new_linear >= 0); assert(new_linear < term->rows * term->cols); - term_damage_update(term, term->cursor.linear, 1); - term_damage_update(term, new_linear, 1); term->print_needs_wrap = false; term->cursor.linear = new_linear; term->cursor.col = col; term->cursor.row = row; + + int len = term->cols; + term->grid->cur_line = grid_get_range( + term->grid, term->cursor.linear - col, &len); + + assert(len == term->cols); } void term_cursor_left(struct terminal *term, int count) { int move_amount = min(term->cursor.col, count); - term_cursor_to(term, term->cursor.row, term->cursor.col - move_amount); + term->cursor.linear -= move_amount; + term->cursor.col -= move_amount; + term->print_needs_wrap = false; } void term_cursor_right(struct terminal *term, int count) { int move_amount = min(term->cols - term->cursor.col - 1, count); - term_cursor_to(term, term->cursor.row, term->cursor.col + move_amount); + term->cursor.linear += move_amount; + term->cursor.col += move_amount; + term->print_needs_wrap = false; } void @@ -248,27 +228,61 @@ term_cursor_down(struct terminal *term, int count) void term_scroll_partial(struct terminal *term, struct scroll_region region, int rows) { - if (rows >= region.end - region.start) { - assert(false && "untested"); - return; + LOG_DBG("scroll: %d rows", rows); + + if (region.start > 0) { + /* TODO: check if it's worth memoving the scroll area instead, + * under certain circumstances */ + + grid_memmove(term->grid, rows * term->cols, 0, region.start * term->cols); + + tll_foreach(term->grid->damage, it) { + int start = it->item.range.start - term->grid->offset; + int end __attribute__((unused)) = start + it->item.range.length; + + if (start < region.start * term->cols) { + assert(end <= region.start * term->cols); + it->item.range.start += rows * term->cols; + } + } } - int cell_dst = (region.start + 0) * term->cols; - int cell_src = (region.start + rows) * term->cols; - int cell_count = (region.end - region.start - rows) * term->cols; + if (region.end < term->rows) { + /* Copy scrolled-up bottom region to new bottom region */ + grid_memmove( + term->grid, + (region.end + rows) * term->cols, + region.end * term->cols, + (term->rows - region.end) * term->cols); - LOG_DBG("moving %d lines from row %d to row %d", cell_count / term->cols, - cell_src / term->cols, cell_dst / term->cols); + tll_foreach(term->grid->damage, it) { + int start = it->item.range.start - term->grid->offset; + int end = start + it->item.range.length; - const size_t bytes = cell_count * sizeof(term->grid->cells[0]); - memmove( - &term->grid->cells[cell_dst], &term->grid->cells[cell_src], - bytes); + if (end > region.end * term->cols) { + assert(start >= region.end * term->cols); + it->item.range.start += rows * term->cols; + } + } + } + + /* Offset grid origin */ + term->grid->offset += rows * term->cols; - memset(&term->grid->cells[(region.end - rows) * term->cols], 0, - rows * term->cols * sizeof(term->grid->cells[0])); + /* Clear scrolled-in lines */ + grid_memset( + term->grid, + max(0, region.end - rows) * term->cols, + 0, + min(rows, term->rows) * term->cols); term_damage_scroll(term, DAMAGE_SCROLL, region, rows); + + int len = term->cols; + term->grid->cur_line = grid_get_range( + term->grid, term->cursor.linear - term->cursor.col, &len); + + assert(len == term->cols); } void @@ -281,27 +295,56 @@ void term_scroll_reverse_partial(struct terminal *term, struct scroll_region region, int rows) { - if (rows >= region.end - region.start) { - assert(false && "todo"); - return; + if (region.end < term->rows) { + grid_memmove( + term->grid, + (region.end - rows) * term->cols, + region.end * term->cols, + (term->rows - region.end) * term->cols); + + tll_foreach(term->grid->damage, it) { + int start = it->item.range.start - term->grid->offset; + int end = start + it->item.range.length; + + if (end > region.end * term->cols) { + assert(start >= region.end * term->cols); + it->item.range.start -= rows * term->cols; + } + } + } - int cell_dst = (region.start + rows) * term->cols; - int cell_src = (region.start + 0) * term->cols; - int cell_count = (region.end - region.start - rows) * term->cols; + if (region.start > 0) { + grid_memmove( + term->grid, -rows * term->cols, 0, region.start * term->cols); - LOG_DBG("moving %d lines from row %d to row %d", cell_count / term->cols, - cell_src / term->cols, cell_dst / term->cols); + tll_foreach(term->grid->damage, it) { + int start = it->item.range.start - term->grid->offset; + int end __attribute__((unused)) = start + it->item.range.length; - const size_t bytes = cell_count * sizeof(term->grid->cells[0]); - memmove( - &term->grid->cells[cell_dst], &term->grid->cells[cell_src], - bytes); + if (start < region.start * term->cols) { + if (end > region.start * term->cols) { + LOG_ERR("region.start = %d, rows = %d, damage.start = %d, damage.end = %d (%s)", + region.start, rows, start, end, it->item.type == DAMAGE_UPDATE ? "UPDATE" : "ERASE"); + abort(); + } + assert(end <= region.start * term->cols); + it->item.range.start -= rows * term->cols; + } + } + } - memset(&term->grid->cells[cell_src], 0, - rows * term->cols * sizeof(term->grid->cells[0])); + term->grid->offset -= rows * term->cols; + + grid_memset(term->grid, region.start * term->cols, 0, rows * term->cols); term_damage_scroll(term, DAMAGE_SCROLL_REVERSE, region, rows); + + int len = term->cols; + term->grid->cur_line = grid_get_range( + term->grid, term->cursor.linear - term->cursor.col, &len); + + assert(len == term->cols); } void |
