aboutsummaryrefslogtreecommitdiff
path: root/terminal.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2019-07-09 07:24:26 +0000
committerDaniel Eklöf <daniel@ekloef.se>2019-07-09 07:24:26 +0000
commit8cf7d7f7aba8f4324a66735b941ff3649c1901d1 (patch)
tree3e3c3b4e6307d614c49036a6b8a3e7b897aaf489 /terminal.c
parentcell: packing the cell struct seems to help performance (diff)
parentrender: store frame callback pointer in terminal struct (diff)
Merge branch 'no-more-linear'
Diffstat (limited to 'terminal.c')
-rw-r--r--terminal.c317
1 files changed, 70 insertions, 247 deletions
diff --git a/terminal.c b/terminal.c
index 7c15ce87..1cf15cfb 100644
--- a/terminal.c
+++ b/terminal.c
@@ -4,6 +4,8 @@
#include <unistd.h>
#include <assert.h>
+#include <linux/input-event-codes.h>
+
#define LOG_MODULE "terminal"
#define LOG_ENABLE_DBG 0
#include "log.h"
@@ -12,131 +14,17 @@
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
-static bool
-damage_merge_range(struct terminal *term, const struct damage *dmg)
-{
- if (tll_length(term->grid->damage) == 0)
- return false;
-
- struct damage *old = &tll_back(term->grid->damage);
- if (old->type != dmg->type)
- return false;
-
- const int start = dmg->range.start;
- const int end = start + dmg->range.length;
-
- const int prev_start = old->range.start;
- const int prev_end = prev_start + old->range.length;
-
- if ((start >= prev_start && start <= prev_end) ||
- (end >= prev_start && end <= prev_end) ||
- (start <= prev_start && end >= prev_end))
- {
- /* The two damage ranges intersect */
- int new_start = min(start, prev_start);
- int new_end = max(end, prev_end);
-
- old->range.start = new_start;
- old->range.length = new_end - new_start;
-
- return true;
- }
-
- return false;
-}
-
-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 = term->grid->offset + start, .length = length},
- };
-
- if (damage_merge_range(term, &dmg))
- return;
-
- tll_push_back(term->grid->damage, dmg);
-}
-
-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);
-}
-
void
term_damage_all(struct terminal *term)
{
- tll_free(term->grid->damage);
- tll_free(term->grid->scroll_damage);
- term_damage_update(term, 0, term->rows * term->cols);
+ for (int i = 0; i < term->rows; i++)
+ grid_row(term->grid, i)->dirty = true;
}
-#if 0
-static void
-damage_adjust_after_scroll(struct terminal *term, enum damage_type damage_type,
- struct scroll_region region, int lines)
-{
- tll_foreach(term->grid->damage, it) {
-#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
-
- int start = it->item.range.start;
- int end = start + it->item.range.length;
-
- 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);
- 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);
@@ -155,43 +43,51 @@ term_damage_scroll(struct terminal *term, enum damage_type damage_type,
tll_push_back(term->grid->scroll_damage, dmg);
}
-void
-term_erase(struct terminal *term, int start, int end)
+static inline void
+erase_cell_range(struct terminal *term, struct row *row, int start, int end)
{
- LOG_DBG("erase: %d-%d", start, end);
- assert(end >= start);
- assert(end <= term->rows * term->cols);
-
- if (term->vt.attrs.have_background) {
- int erase_start = start;
- int left = end - erase_start;
-
- while (left > 0) {
- int len = left;
- struct cell *cells = grid_get_range(term->grid, erase_start, &len);
-
- memset(cells, 0, len * sizeof(cells[0]));
+ assert(start < term->cols);
+ assert(end < term->cols);
- for (int i = 0; i < len; i++) {
- cells[i].attrs.background = term->vt.attrs.background;
- cells[i].attrs.have_background = true;
- }
-
- erase_start += len;
- left -= len;
+ if (unlikely(term->vt.attrs.have_background)) {
+ for (int col = start; col <= end; col++) {
+ row->cells[col].c[0] = '\0';
+ row->cells[col].attrs.have_background = true;
+ row->cells[col].attrs.background = term->vt.attrs.background;
}
-
- term_damage_update(term, start, end - start);
} else {
- grid_memclear(term->grid, start, end - start);
- term_damage_erase(term, start, end - start);
+ memset(&row->cells[start], 0, (end - start + 1) * sizeof(row->cells[0]));
}
+ row->dirty = true;
+}
+
+static inline void
+erase_line(struct terminal *term, struct row *row)
+{
+ erase_cell_range(term, row, 0, term->cols - 1);
}
-int
-term_cursor_linear(const struct terminal *term, int row, int col)
+void
+term_erase(struct terminal *term, const struct coord *start, const struct coord *end)
{
- return row * term->cols + col;
+ assert(start->row <= end->row);
+ assert(start->col <= end->col || start->row < end->row);
+
+ if (start->row == end->row) {
+ struct row *row = grid_row(term->grid, start->row);
+ erase_cell_range(term, row, start->col, end->col);
+ return;
+ }
+
+ assert(end->row > start->row);
+
+ erase_cell_range(
+ term, grid_row(term->grid, start->row), start->col, term->cols - 1);
+
+ for (int r = start->row + 1; r < end->row; r++)
+ erase_line(term, grid_row(term->grid, r));
+
+ erase_cell_range(term, grid_row(term->grid, end->row), 0, end->col);
}
void
@@ -200,28 +96,20 @@ term_cursor_to(struct terminal *term, int row, int col)
assert(row < term->rows);
assert(col < term->cols);
- int new_linear = row * term->cols + col;
- assert(new_linear < term->rows * term->cols);
-
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);
+ term->grid->cur_row = grid_row(term->grid, row);
}
void
term_cursor_left(struct terminal *term, int count)
{
int move_amount = min(term->cursor.col, count);
- term->cursor.linear -= move_amount;
term->cursor.col -= move_amount;
+ assert(term->cursor.col >= 0);
term->print_needs_wrap = false;
}
@@ -229,8 +117,8 @@ void
term_cursor_right(struct terminal *term, int count)
{
int move_amount = min(term->cols - term->cursor.col - 1, count);
- term->cursor.linear += move_amount;
term->cursor.col += move_amount;
+ assert(term->cursor.col < term->cols);
term->print_needs_wrap = false;
}
@@ -253,58 +141,25 @@ term_scroll_partial(struct terminal *term, struct scroll_region region, int rows
{
LOG_DBG("scroll: %d rows", rows);
- if (region.start > 0) {
- /* TODO: check if it's worth memoving the scroll area instead,
- * under certain circumstances */
+ assert(rows < term->rows && "unimplemented");
- grid_memmove(term->grid, rows * term->cols, 0, region.start * term->cols);
+ /* Top non-scrolling region */
+ for (int i = region.start - 1; i >= 0; i--)
+ grid_swap_row(term->grid, i, i + rows);
- 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;
- }
- }
- }
-
- 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);
-
- 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;
- }
- }
- }
+ /* Bottom non-scrolling region */
+ for (int i = term->rows - 1; i >= region.end; i--)
+ grid_swap_row(term->grid, i, i + rows);
/* Offset grid origin */
- term->grid->offset += rows * term->cols;
+ term->grid->offset += rows;
+ term->grid->offset %= term->grid->num_rows;
- /* Clear scrolled-in lines */
- grid_memclear(
- term->grid,
- max(0, region.end - rows) * term->cols,
- min(rows, term->rows) * term->cols);
+ for (int r = max(region.end - rows, 0); r < region.end; r++)
+ erase_line(term, grid_row(term->grid, r));
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);
+ term->grid->cur_row = grid_row(term->grid, term->cursor.row);
}
void
@@ -317,56 +172,26 @@ void
term_scroll_reverse_partial(struct terminal *term,
struct scroll_region region, int rows)
{
- if (region.end < term->rows) {
- grid_memmove(
- term->grid,
- (region.end - rows) * term->cols,
- region.end * term->cols,
- (term->rows - region.end) * term->cols);
+ assert(rows < term->rows && "unimplemented");
- tll_foreach(term->grid->damage, it) {
- int start = it->item.range.start - term->grid->offset;
- int end = start + it->item.range.length;
+ term->grid->offset += term->grid->num_rows - rows;
+ term->grid->offset %= term->grid->num_rows;
- if (end > region.end * term->cols) {
- assert(start >= region.end * term->cols);
- it->item.range.start -= rows * term->cols;
- }
- }
+ /* Bottom non-scrolling region */
+ for (int i = region.end + rows; i < term->rows + rows; i++)
+ grid_swap_row(term->grid, i, i - rows);
- }
+ /* Top non-scrolling region */
+ for (int i = 0 + rows; i < region.start + rows; i++)
+ grid_swap_row(term->grid, i, i - rows);
- if (region.start > 0) {
- 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) {
- 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;
- }
- }
- }
-
- term->grid->offset -= rows * term->cols;
-
- grid_memclear(term->grid, region.start * term->cols, rows * term->cols);
+ term_erase(
+ term,
+ &(struct coord){0, region.start},
+ &(struct coord){term->cols - 1, min(region.start + rows, region.end) - 1});
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);
+ term->grid->cur_row = grid_row(term->grid, term->cursor.row);
}
void
@@ -375,8 +200,6 @@ term_scroll_reverse(struct terminal *term, int rows)
term_scroll_reverse_partial(term, term->scroll_region, rows);
}
-#include <linux/input-event-codes.h>
-
static int
linux_mouse_button_to_x(int button)
{