2019-06-29 21:03:28 +02:00
|
|
|
#include "terminal.h"
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
#define LOG_MODULE "terminal"
|
|
|
|
|
#define LOG_ENABLE_DBG 1
|
|
|
|
|
#include "log.h"
|
2019-07-01 12:23:38 +02:00
|
|
|
#include "grid.h"
|
2019-06-29 21:03:28 +02:00
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
{
|
2019-06-29 21:23:36 +02:00
|
|
|
if (tll_length(term->grid->damage) == 0)
|
2019-06-29 21:03:28 +02:00
|
|
|
return false;
|
|
|
|
|
|
2019-06-29 21:23:36 +02:00
|
|
|
struct damage *old = &tll_back(term->grid->damage);
|
2019-06-29 21:03:28 +02:00
|
|
|
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)
|
|
|
|
|
{
|
2019-07-02 19:45:17 +02:00
|
|
|
#if 1
|
|
|
|
|
if (tll_length(term->grid->damage) > 1024) {
|
|
|
|
|
term_damage_all(term);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-06-29 21:03:28 +02:00
|
|
|
struct damage dmg = {
|
|
|
|
|
.type = damage_type,
|
2019-07-01 12:23:38 +02:00
|
|
|
.range = {.start = term->grid->offset + start, .length = length},
|
2019-06-29 21:03:28 +02:00
|
|
|
};
|
2019-07-02 19:45:17 +02:00
|
|
|
|
2019-06-29 21:03:28 +02:00
|
|
|
if (damage_merge_range(term, &dmg))
|
|
|
|
|
return;
|
|
|
|
|
|
2019-06-29 21:23:36 +02:00
|
|
|
tll_push_back(term->grid->damage, dmg);
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_damage_update(struct terminal *term, int start, int length)
|
|
|
|
|
{
|
2019-07-01 12:23:38 +02:00
|
|
|
assert(start + length <= term->rows * term->cols);
|
2019-06-29 21:03:28 +02:00
|
|
|
term_damage_update_or_erase(term, DAMAGE_UPDATE, start, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_damage_erase(struct terminal *term, int start, int length)
|
|
|
|
|
{
|
2019-07-01 12:23:38 +02:00
|
|
|
assert(start + length <= term->rows * term->cols);
|
2019-06-29 21:03:28 +02:00
|
|
|
term_damage_update_or_erase(term, DAMAGE_ERASE, start, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_damage_all(struct terminal *term)
|
|
|
|
|
{
|
2019-06-29 21:23:36 +02:00
|
|
|
tll_free(term->grid->damage);
|
|
|
|
|
tll_free(term->grid->scroll_damage);
|
2019-06-29 21:08:08 +02:00
|
|
|
term_damage_update(term, 0, term->rows * term->cols);
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-01 12:23:38 +02:00
|
|
|
#if 0
|
2019-06-29 21:03:28 +02:00
|
|
|
static void
|
|
|
|
|
damage_adjust_after_scroll(struct terminal *term, enum damage_type damage_type,
|
|
|
|
|
struct scroll_region region, int lines)
|
|
|
|
|
{
|
2019-06-29 21:23:36 +02:00
|
|
|
tll_foreach(term->grid->damage, it) {
|
2019-07-01 19:20:21 +02:00
|
|
|
#if 0
|
2019-07-01 12:23:38 +02:00
|
|
|
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);
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-01 19:20:21 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
int start = it->item.range.start;
|
|
|
|
|
int end = start + it->item.range.length;
|
|
|
|
|
|
|
|
|
|
if (start -
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-01 12:23:38 +02:00
|
|
|
#endif
|
2019-06-29 21:03:28 +02:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_damage_scroll(struct terminal *term, enum damage_type damage_type,
|
|
|
|
|
struct scroll_region region, int lines)
|
|
|
|
|
{
|
2019-07-01 12:23:38 +02:00
|
|
|
//damage_adjust_after_scroll(term, damage_type, region, lines);
|
2019-06-29 21:03:28 +02:00
|
|
|
|
2019-06-29 21:23:36 +02:00
|
|
|
if (tll_length(term->grid->scroll_damage) > 0) {
|
|
|
|
|
struct damage *dmg = &tll_back(term->grid->scroll_damage);
|
2019-06-29 21:03:28 +02:00
|
|
|
|
|
|
|
|
if (dmg->type == damage_type &&
|
|
|
|
|
dmg->scroll.region.start == region.start &&
|
|
|
|
|
dmg->scroll.region.end == region.end)
|
|
|
|
|
{
|
|
|
|
|
dmg->scroll.lines += lines;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
struct damage dmg = {
|
|
|
|
|
.type = damage_type,
|
|
|
|
|
.scroll = {.region = region, .lines = lines},
|
|
|
|
|
};
|
2019-06-29 21:23:36 +02:00
|
|
|
tll_push_back(term->grid->scroll_damage, dmg);
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_erase(struct terminal *term, int start, int end)
|
|
|
|
|
{
|
2019-07-01 12:23:38 +02:00
|
|
|
LOG_DBG("erase: %d-%d", start, end);
|
2019-06-29 21:03:28 +02:00
|
|
|
assert(end >= start);
|
2019-07-01 12:23:38 +02:00
|
|
|
assert(end <= term->rows * term->cols);
|
|
|
|
|
|
|
|
|
|
grid_memset(term->grid, start, 0, end - start);
|
2019-06-29 21:03:28 +02:00
|
|
|
term_damage_erase(term, start, end - start);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
term_cursor_linear(const struct terminal *term, int row, int col)
|
|
|
|
|
{
|
2019-06-29 21:08:08 +02:00
|
|
|
return row * term->cols + col;
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_cursor_to(struct terminal *term, int row, int col)
|
|
|
|
|
{
|
2019-06-29 21:08:08 +02:00
|
|
|
assert(row < term->rows);
|
|
|
|
|
assert(col < term->cols);
|
2019-06-29 21:03:28 +02:00
|
|
|
|
2019-06-29 21:08:08 +02:00
|
|
|
int new_linear = row * term->cols + col;
|
|
|
|
|
assert(new_linear < term->rows * term->cols);
|
2019-06-29 21:03:28 +02:00
|
|
|
|
2019-06-29 21:16:06 +02:00
|
|
|
term->print_needs_wrap = false;
|
2019-06-29 21:03:28 +02:00
|
|
|
|
2019-06-29 21:15:32 +02:00
|
|
|
term->cursor.linear = new_linear;
|
|
|
|
|
term->cursor.col = col;
|
|
|
|
|
term->cursor.row = row;
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_cursor_left(struct terminal *term, int count)
|
|
|
|
|
{
|
2019-06-29 21:15:32 +02:00
|
|
|
int move_amount = min(term->cursor.col, count);
|
2019-07-02 21:43:49 +02:00
|
|
|
term->cursor.linear -= move_amount;
|
|
|
|
|
term->cursor.col -= move_amount;
|
|
|
|
|
term->print_needs_wrap = false;
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_cursor_right(struct terminal *term, int count)
|
|
|
|
|
{
|
2019-06-29 21:15:32 +02:00
|
|
|
int move_amount = min(term->cols - term->cursor.col - 1, count);
|
2019-07-02 21:43:49 +02:00
|
|
|
term->cursor.linear += move_amount;
|
|
|
|
|
term->cursor.col += move_amount;
|
|
|
|
|
term->print_needs_wrap = false;
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_cursor_up(struct terminal *term, int count)
|
|
|
|
|
{
|
2019-06-29 21:15:32 +02:00
|
|
|
int move_amount = min(term->cursor.row, count);
|
|
|
|
|
term_cursor_to(term, term->cursor.row - move_amount, term->cursor.col);
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_cursor_down(struct terminal *term, int count)
|
|
|
|
|
{
|
2019-06-29 21:15:32 +02:00
|
|
|
int move_amount = min(term->rows - term->cursor.row - 1, count);
|
|
|
|
|
term_cursor_to(term, term->cursor.row + move_amount, term->cursor.col);
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_scroll_partial(struct terminal *term, struct scroll_region region, int rows)
|
|
|
|
|
{
|
2019-07-01 12:23:38 +02:00
|
|
|
LOG_DBG("scroll: %d rows", rows);
|
2019-06-29 21:03:28 +02:00
|
|
|
|
2019-07-01 19:20:21 +02:00
|
|
|
if (region.start > 0) {
|
|
|
|
|
/* TODO: check if it's worth memoving the scroll area instead,
|
|
|
|
|
* under certain circumstances */
|
2019-06-29 21:03:28 +02:00
|
|
|
|
2019-07-01 19:20:21 +02:00
|
|
|
grid_memmove(term->grid, rows * term->cols, 0, region.start * term->cols);
|
2019-06-29 21:03:28 +02:00
|
|
|
|
2019-07-01 19:20:21 +02:00
|
|
|
tll_foreach(term->grid->damage, it) {
|
|
|
|
|
int start = it->item.range.start - term->grid->offset;
|
2019-07-01 21:14:31 +02:00
|
|
|
int end __attribute__((unused)) = start + it->item.range.length;
|
2019-06-29 21:03:28 +02:00
|
|
|
|
2019-07-01 19:20:21 +02:00
|
|
|
if (start < region.start) {
|
|
|
|
|
assert(end <= region.start);
|
|
|
|
|
it->item.range.start += rows * term->cols;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-29 21:03:28 +02:00
|
|
|
|
2019-07-01 19:20:21 +02:00
|
|
|
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) {
|
|
|
|
|
assert(start >= region.end);
|
|
|
|
|
it->item.range.start += rows * term->cols;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-01 12:23:38 +02:00
|
|
|
|
2019-07-01 19:20:21 +02:00
|
|
|
/* Offset grid origin */
|
2019-07-01 12:23:38 +02:00
|
|
|
term->grid->offset += rows * term->cols;
|
2019-07-01 19:20:21 +02:00
|
|
|
|
|
|
|
|
/* Clear scrolled-in lines */
|
|
|
|
|
grid_memset(
|
|
|
|
|
term->grid,
|
|
|
|
|
max(0, region.end - rows) * term->cols, 0, rows * term->cols);
|
|
|
|
|
|
2019-07-01 12:23:38 +02:00
|
|
|
term_damage_scroll(term, DAMAGE_SCROLL, region, rows);
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_scroll(struct terminal *term, int rows)
|
|
|
|
|
{
|
2019-06-29 21:08:08 +02:00
|
|
|
term_scroll_partial(term, term->scroll_region, rows);
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_scroll_reverse_partial(struct terminal *term,
|
|
|
|
|
struct scroll_region region, int rows)
|
|
|
|
|
{
|
|
|
|
|
if (rows >= region.end - region.start) {
|
|
|
|
|
assert(false && "todo");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-01 12:23:38 +02:00
|
|
|
#if 0
|
2019-06-29 21:08:08 +02:00
|
|
|
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;
|
2019-06-29 21:03:28 +02:00
|
|
|
|
2019-06-29 21:08:08 +02:00
|
|
|
LOG_DBG("moving %d lines from row %d to row %d", cell_count / term->cols,
|
|
|
|
|
cell_src / term->cols, cell_dst / term->cols);
|
2019-06-29 21:03:28 +02:00
|
|
|
|
2019-07-01 12:23:38 +02:00
|
|
|
const int bytes = cell_count * sizeof(term->grid->cells[0]);
|
2019-06-29 21:03:28 +02:00
|
|
|
memmove(
|
2019-06-29 21:23:36 +02:00
|
|
|
&term->grid->cells[cell_dst], &term->grid->cells[cell_src],
|
2019-06-29 21:03:28 +02:00
|
|
|
bytes);
|
|
|
|
|
|
2019-06-29 21:23:36 +02:00
|
|
|
memset(&term->grid->cells[cell_src], 0,
|
|
|
|
|
rows * term->cols * sizeof(term->grid->cells[0]));
|
2019-06-29 21:03:28 +02:00
|
|
|
|
|
|
|
|
term_damage_scroll(term, DAMAGE_SCROLL_REVERSE, region, rows);
|
2019-07-01 12:23:38 +02:00
|
|
|
#else
|
|
|
|
|
/* TODO */
|
|
|
|
|
assert(false);
|
|
|
|
|
assert(region.start == 0 && region.end == 0);
|
|
|
|
|
assert(rows < term->rows);
|
|
|
|
|
|
|
|
|
|
term->grid->offset -= rows * term->cols;
|
|
|
|
|
term_damage_scroll(term, DAMAGE_SCROLL_REVERSE, region, rows);
|
|
|
|
|
#endif
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
term_scroll_reverse(struct terminal *term, int rows)
|
|
|
|
|
{
|
2019-06-29 21:08:08 +02:00
|
|
|
term_scroll_reverse_partial(term, term->scroll_region, rows);
|
2019-06-29 21:03:28 +02:00
|
|
|
}
|