terminal: move col/row count, cell width/height and scroll region to terminal

This commit is contained in:
Daniel Eklöf 2019-06-29 21:08:08 +02:00
parent 1ecd4a6ae1
commit 3d2ab03f62
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
5 changed files with 111 additions and 110 deletions

56
csi.c
View file

@ -294,8 +294,8 @@ csi_dispatch(struct terminal *term, uint8_t final)
/* VPA - vertical line position absolute */
int row = param_get(term, 0, 1);
if (row > term->grid.rows)
row = term->grid.rows;
if (row > term->rows)
row = term->rows;
term_cursor_to(term, row - 1, term->grid.cursor.col);
break;
@ -325,8 +325,8 @@ csi_dispatch(struct terminal *term, uint8_t final)
/* Cursor horizontal absolute */
int col = param_get(term, 0, 1);
if (col > term->grid.cols)
col = term->grid.cols;
if (col > term->cols)
col = term->cols;
term_cursor_to(term, term->grid.cursor.row, col);
break;
@ -337,10 +337,10 @@ csi_dispatch(struct terminal *term, uint8_t final)
int row = param_get(term, 0, 1);
int col = param_get(term, 1, 1);
if (row > term->grid.rows)
row = term->grid.rows;
if (col > term->grid.cols)
col = term->grid.cols;
if (row > term->rows)
row = term->rows;
if (col > term->cols)
col = term->cols;
term_cursor_to(term, row - 1, col - 1);
break;
@ -356,7 +356,7 @@ csi_dispatch(struct terminal *term, uint8_t final)
case 0:
/* From cursor to end of screen */
start = term->grid.linear_cursor;
end = term->grid.cols * term->grid.rows;
end = term->cols * term->rows;
break;
case 1:
@ -368,7 +368,7 @@ csi_dispatch(struct terminal *term, uint8_t final)
case 2:
/* Erase entire screen */
start = 0;
end = term->grid.cols * term->grid.rows;
end = term->cols * term->rows;
break;
default:
@ -390,7 +390,7 @@ csi_dispatch(struct terminal *term, uint8_t final)
case 0:
/* From cursor to end of line */
start = term->grid.linear_cursor;
end = term_cursor_linear(term, term->grid.cursor.row, term->grid.cols);
end = term_cursor_linear(term, term->grid.cursor.row, term->cols);
break;
case 1:
@ -402,7 +402,7 @@ csi_dispatch(struct terminal *term, uint8_t final)
case 2:
/* Entire line */
start = term_cursor_linear(term, term->grid.cursor.row, 0);
end = term_cursor_linear(term, term->grid.cursor.row, term->grid.cols);
end = term_cursor_linear(term, term->grid.cursor.row, term->cols);
break;
default:
@ -415,37 +415,37 @@ csi_dispatch(struct terminal *term, uint8_t final)
}
case 'L': {
if (term->grid.cursor.row < term->grid.scroll_region.start ||
term->grid.cursor.row >= term->grid.scroll_region.end)
if (term->grid.cursor.row < term->scroll_region.start ||
term->grid.cursor.row >= term->scroll_region.end)
break;
int count = min(
param_get(term, 0, 1),
term->grid.scroll_region.end - term->grid.cursor.row);
term->scroll_region.end - term->grid.cursor.row);
term_scroll_reverse_partial(
term,
(struct scroll_region){
.start = term->grid.cursor.row,
.end = term->grid.scroll_region.end},
.end = term->scroll_region.end},
count);
break;
}
case 'M': {
if (term->grid.cursor.row < term->grid.scroll_region.start ||
term->grid.cursor.row >= term->grid.scroll_region.end)
if (term->grid.cursor.row < term->scroll_region.start ||
term->grid.cursor.row >= term->scroll_region.end)
break;
int count = min(
param_get(term, 0, 1),
term->grid.scroll_region.end - term->grid.cursor.row);
term->scroll_region.end - term->grid.cursor.row);
term_scroll_partial(
term,
(struct scroll_region){
.start = term->grid.cursor.row,
.end = term->grid.scroll_region.end},
.end = term->scroll_region.end},
count);
break;
}
@ -456,7 +456,7 @@ csi_dispatch(struct terminal *term, uint8_t final)
/* Only delete up to the right margin */
const int max_end = term_cursor_linear(
term, term->grid.cursor.row, term->grid.cols);
term, term->grid.cursor.row, term->cols);
int start = term->grid.linear_cursor;
int end = min(start + count, max_end);
@ -475,15 +475,15 @@ csi_dispatch(struct terminal *term, uint8_t final)
case 'r': {
int start = param_get(term, 0, 1);
int end = param_get(term, 1, term->grid.rows);
int end = param_get(term, 1, term->rows);
/* 1-based */
term->grid.scroll_region.start = start - 1;
term->grid.scroll_region.end = end;
term->scroll_region.start = start - 1;
term->scroll_region.end = end;
LOG_INFO("scroll region: %d-%d",
term->grid.scroll_region.start,
term->grid.scroll_region.end);
term->scroll_region.start,
term->scroll_region.end);
break;
}
@ -646,8 +646,8 @@ csi_dispatch(struct terminal *term, uint8_t final)
term, term->grid.cursor.row, term->grid.cursor.col);
/* Should these be restored from saved values? */
term->grid.scroll_region.start = 0;
term->grid.scroll_region.end = term->grid.rows;
term->scroll_region.start = 0;
term->scroll_region.end = term->rows;
term_damage_all(term);
}

84
main.c
View file

@ -82,7 +82,7 @@ grid_render_update(struct context *c, struct buffer *buf, const struct damage *d
LOG_DBG("damage: UPDATE: %d -> %d",
dmg->range.start, dmg->range.start + dmg->range.length);
const int cols = c->term.grid.cols;
const int cols = c->term.cols;
for (int linear_cursor = dmg->range.start,
row = dmg->range.start / cols,
@ -90,7 +90,7 @@ grid_render_update(struct context *c, struct buffer *buf, const struct damage *d
linear_cursor < dmg->range.start + dmg->range.length;
linear_cursor++,
//col = (col + 1) % cols,
col = col + 1 >= c->term.grid.cols ? 0 : col + 1,
col = col + 1 >= c->term.cols ? 0 : col + 1,
row += col == 0 ? 1 : 0)
{
//LOG_DBG("UPDATE: %d (%dx%d)", linear_cursor, row, col);
@ -98,10 +98,10 @@ grid_render_update(struct context *c, struct buffer *buf, const struct damage *d
const struct cell *cell = &c->term.grid.cells[linear_cursor];
bool has_cursor = c->term.grid.linear_cursor == linear_cursor;
int x = col * c->term.grid.cell_width;
int y = row * c->term.grid.cell_height;
int width = c->term.grid.cell_width;
int height = c->term.grid.cell_height;
int x = col * c->term.cell_width;
int y = row * c->term.cell_height;
int width = c->term.cell_width;
int height = c->term.cell_height;
struct rgba foreground = cell->attrs.have_foreground
? cell->attrs.foreground : c->term.grid.foreground;
@ -160,8 +160,8 @@ grid_render_update(struct context *c, struct buffer *buf, const struct damage *d
wl_surface_damage_buffer(
c->wl.surface,
0, (dmg->range.start / cols) * c->term.grid.cell_height,
buf->width, (dmg->range.length + cols - 1) / cols * c->term.grid.cell_height);
0, (dmg->range.start / cols) * c->term.cell_height,
buf->width, (dmg->range.length + cols - 1) / cols * c->term.cell_height);
}
static void
@ -174,7 +174,7 @@ grid_render_erase(struct context *c, struct buffer *buf, const struct damage *dm
buf->cairo, default_background.r, default_background.g,
default_background.b, default_background.a);
const int cols = c->term.grid.cols;
const int cols = c->term.cols;
int start = dmg->range.start;
int left = dmg->range.length;
@ -186,10 +186,10 @@ grid_render_erase(struct context *c, struct buffer *buf, const struct damage *dm
if (col != 0) {
int cell_count = min(left, cols - col);
int x = col * c->term.grid.cell_width;
int y = row * c->term.grid.cell_height;
int width = cell_count * c->term.grid.cell_width;
int height = c->term.grid.cell_height;
int x = col * c->term.cell_width;
int y = row * c->term.cell_height;
int width = cell_count * c->term.cell_width;
int height = c->term.cell_height;
cairo_rectangle(buf->cairo, x, y, width, height);
cairo_fill(buf->cairo);
@ -209,9 +209,9 @@ grid_render_erase(struct context *c, struct buffer *buf, const struct damage *dm
int line_count = left / cols;
int x = 0;
int y = row * c->term.grid.cell_height;
int y = row * c->term.cell_height;
int width = buf->width;
int height = line_count * c->term.grid.cell_height;
int height = line_count * c->term.cell_height;
cairo_rectangle(buf->cairo, x, y, width, height);
cairo_fill(buf->cairo);
@ -230,9 +230,9 @@ grid_render_erase(struct context *c, struct buffer *buf, const struct damage *dm
/* Partial last line */
if (left > 0) {
int x = 0;
int y = row * c->term.grid.cell_height;
int width = left * c->term.grid.cell_width;
int height = c->term.grid.cell_height;
int y = row * c->term.cell_height;
int width = left * c->term.cell_width;
int height = c->term.cell_height;
cairo_rectangle(buf->cairo, x, y, width, height);
cairo_fill(buf->cairo);
@ -255,10 +255,10 @@ static void
grid_render_scroll(struct context *c, struct buffer *buf,
const struct damage *dmg)
{
int dst_y = (dmg->scroll.region.start + 0) * c->term.grid.cell_height;
int src_y = (dmg->scroll.region.start + dmg->scroll.lines) * c->term.grid.cell_height;
int dst_y = (dmg->scroll.region.start + 0) * c->term.cell_height;
int src_y = (dmg->scroll.region.start + dmg->scroll.lines) * c->term.cell_height;
int width = buf->width;
int height = (dmg->scroll.region.end - dmg->scroll.region.start - dmg->scroll.lines) * c->term.grid.cell_height;
int height = (dmg->scroll.region.end - dmg->scroll.region.start - dmg->scroll.lines) * c->term.cell_height;
const uint32_t stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
@ -279,7 +279,7 @@ grid_render_scroll(struct context *c, struct buffer *buf,
wl_surface_damage_buffer(c->wl.surface, 0, dst_y, width, height);
}
const int cols = c->term.grid.cols;
const int cols = c->term.cols;
struct damage erase = {
.type = DAMAGE_ERASE,
@ -297,10 +297,10 @@ static void
grid_render_scroll_reverse(struct context *c, struct buffer *buf,
const struct damage *dmg)
{
int src_y = (dmg->scroll.region.start + 0) * c->term.grid.cell_height;
int dst_y = (dmg->scroll.region.start + dmg->scroll.lines) * c->term.grid.cell_height;
int src_y = (dmg->scroll.region.start + 0) * c->term.cell_height;
int dst_y = (dmg->scroll.region.start + dmg->scroll.lines) * c->term.cell_height;
int width = buf->width;
int height = (dmg->scroll.region.end - dmg->scroll.region.start - dmg->scroll.lines) * c->term.grid.cell_height;
int height = (dmg->scroll.region.end - dmg->scroll.region.start - dmg->scroll.lines) * c->term.cell_height;
const uint32_t stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
@ -321,7 +321,7 @@ grid_render_scroll_reverse(struct context *c, struct buffer *buf,
wl_surface_damage_buffer(c->wl.surface, 0, dst_y, width, height);
}
const int cols = c->term.grid.cols;
const int cols = c->term.cols;
struct damage erase = {
.type = DAMAGE_ERASE,
@ -415,23 +415,23 @@ resize(struct context *c, int width, int height)
c->width = width;
c->height = height;
const size_t old_rows = c->term.grid.rows;
const size_t old_cols = c->term.grid.cols;
const size_t old_rows = c->term.rows;
const size_t old_cols = c->term.cols;
const size_t old_cells_len = old_rows * old_cols;
c->term.grid.cell_width = (int)ceil(c->fextents.max_x_advance);
c->term.grid.cell_height = (int)ceil(c->fextents.height);
c->term.grid.cols = c->width / c->term.grid.cell_width;
c->term.grid.rows = c->height / c->term.grid.cell_height;
c->term.cell_width = (int)ceil(c->fextents.max_x_advance);
c->term.cell_height = (int)ceil(c->fextents.height);
c->term.cols = c->width / c->term.cell_width;
c->term.rows = c->height / c->term.cell_height;
c->term.grid.normal_grid = realloc(
c->term.grid.normal_grid,
c->term.grid.cols * c->term.grid.rows * sizeof(c->term.grid.cells[0]));
c->term.cols * c->term.rows * sizeof(c->term.grid.cells[0]));
c->term.grid.alt_grid = realloc(
c->term.grid.alt_grid,
c->term.grid.cols * c->term.grid.rows * sizeof(c->term.grid.cells[0]));
c->term.cols * c->term.rows * sizeof(c->term.grid.cells[0]));
size_t new_cells_len = c->term.grid.cols * c->term.grid.rows;
size_t new_cells_len = c->term.cols * c->term.rows;
for (size_t i = old_cells_len; i < new_cells_len; i++) {
c->term.grid.normal_grid[i] = (struct cell){
.attrs = {.foreground = default_foreground,
@ -447,28 +447,28 @@ resize(struct context *c, int width, int height)
? c->term.grid.alt_grid : c->term.grid.normal_grid;
LOG_DBG("resize: %dx%d, grid: cols=%d, rows=%d",
c->width, c->height, c->term.grid.cols, c->term.grid.rows);
c->width, c->height, c->term.cols, c->term.rows);
/* Update environment variables */
char cols_s[12], rows_s[12];
sprintf(cols_s, "%u", c->term.grid.cols);
sprintf(rows_s, "%u", c->term.grid.rows);
sprintf(cols_s, "%u", c->term.cols);
sprintf(rows_s, "%u", c->term.rows);
setenv("COLUMNS", cols_s, 1);
setenv("LINES", rows_s, 1);
/* Signal TIOCSWINSZ */
if (ioctl(c->term.ptmx, TIOCSWINSZ,
&(struct winsize){
.ws_row = c->term.grid.rows,
.ws_col = c->term.grid.cols,
.ws_row = c->term.rows,
.ws_col = c->term.cols,
.ws_xpixel = c->width,
.ws_ypixel = c->height}) == -1)
{
LOG_ERRNO("TIOCSWINSZ");
}
if (c->term.grid.scroll_region.end == old_rows)
c->term.grid.scroll_region.end = c->term.grid.rows;
if (c->term.scroll_region.end == old_rows)
c->term.scroll_region.end = c->term.rows;
term_damage_all(&c->term);

View file

@ -38,9 +38,9 @@ damage_merge_range(struct terminal *term, const struct damage *dmg)
old->range.length = new_end - new_start;
assert(old->range.start >= 0);
assert(old->range.start < term->grid.rows * term->grid.cols);
assert(old->range.start < term->rows * term->cols);
assert(old->range.length >= 0);
assert(old->range.start + old->range.length <= term->grid.rows * term->grid.cols);
assert(old->range.start + old->range.length <= term->rows * term->cols);
return true;
}
@ -57,9 +57,9 @@ term_damage_update_or_erase(struct terminal *term, enum damage_type damage_type,
};
assert(dmg.range.start >= 0);
assert(dmg.range.start < term->grid.rows * term->grid.cols);
assert(dmg.range.start < term->rows * term->cols);
assert(dmg.range.length >= 0);
assert(dmg.range.start + dmg.range.length <= term->grid.rows * term->grid.cols);
assert(dmg.range.start + dmg.range.length <= term->rows * term->cols);
if (damage_merge_range(term, &dmg))
return;
@ -84,7 +84,7 @@ term_damage_all(struct terminal *term)
{
tll_free(term->grid.damage);
tll_free(term->grid.scroll_damage);
term_damage_update(term, 0, term->grid.rows * term->grid.cols);
term_damage_update(term, 0, term->rows * term->cols);
}
static void
@ -92,10 +92,10 @@ damage_adjust_after_scroll(struct terminal *term, enum damage_type damage_type,
struct scroll_region region, int lines)
{
const int adjustment
= lines * term->grid.cols * (damage_type == DAMAGE_SCROLL_REVERSE ? -1 : 1);
= lines * term->cols * (damage_type == DAMAGE_SCROLL_REVERSE ? -1 : 1);
const int scroll_start = region.start * term->grid.cols;
const int scroll_end = region.end * term->grid.cols;
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;
@ -193,20 +193,20 @@ term_erase(struct terminal *term, int start, int end)
int
term_cursor_linear(const struct terminal *term, int row, int col)
{
return row * term->grid.cols + col;
return row * term->cols + col;
}
void
term_cursor_to(struct terminal *term, int row, int col)
{
assert(row >= 0);
assert(row < term->grid.rows);
assert(row < term->rows);
assert(col >= 0);
assert(col < term->grid.cols);
assert(col < term->cols);
int new_linear = row * term->grid.cols + col;
int new_linear = row * term->cols + col;
assert(new_linear >= 0);
assert(new_linear < term->grid.rows * term->grid.cols);
assert(new_linear < term->rows * term->cols);
term_damage_update(term, term->grid.linear_cursor, 1);
term_damage_update(term, new_linear, 1);
@ -227,7 +227,7 @@ term_cursor_left(struct terminal *term, int count)
void
term_cursor_right(struct terminal *term, int count)
{
int move_amount = min(term->grid.cols - term->grid.cursor.col - 1, count);
int move_amount = min(term->cols - term->grid.cursor.col - 1, count);
term_cursor_to(term, term->grid.cursor.row, term->grid.cursor.col + move_amount);
}
@ -241,7 +241,7 @@ term_cursor_up(struct terminal *term, int count)
void
term_cursor_down(struct terminal *term, int count)
{
int move_amount = min(term->grid.rows - term->grid.cursor.row - 1, count);
int move_amount = min(term->rows - term->grid.cursor.row - 1, count);
term_cursor_to(term, term->grid.cursor.row + move_amount, term->grid.cursor.col);
}
@ -253,20 +253,20 @@ term_scroll_partial(struct terminal *term, struct scroll_region region, int rows
return;
}
int cell_dst = (region.start + 0) * term->grid.cols;
int cell_src = (region.start + rows) * term->grid.cols;
int cell_count = (region.end - region.start - rows) * term->grid.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;
LOG_DBG("moving %d lines from row %d to row %d", cell_count / term->grid.cols,
cell_src / term->grid.cols, cell_dst / term->grid.cols);
LOG_DBG("moving %d lines from row %d to row %d", cell_count / term->cols,
cell_src / term->cols, cell_dst / term->cols);
const size_t bytes = cell_count * sizeof(term->grid.cells[0]);
memmove(
&term->grid.cells[cell_dst], &term->grid.cells[cell_src],
bytes);
memset(&term->grid.cells[(region.end - rows) * term->grid.cols], 0,
rows * term->grid.cols * sizeof(term->grid.cells[0]));
memset(&term->grid.cells[(region.end - rows) * term->cols], 0,
rows * term->cols * sizeof(term->grid.cells[0]));
term_damage_scroll(term, DAMAGE_SCROLL, region, rows);
}
@ -274,7 +274,7 @@ term_scroll_partial(struct terminal *term, struct scroll_region region, int rows
void
term_scroll(struct terminal *term, int rows)
{
term_scroll_partial(term, term->grid.scroll_region, rows);
term_scroll_partial(term, term->scroll_region, rows);
}
void
@ -286,12 +286,12 @@ term_scroll_reverse_partial(struct terminal *term,
return;
}
int cell_dst = (region.start + rows) * term->grid.cols;
int cell_src = (region.start + 0) * term->grid.cols;
int cell_count = (region.end - region.start - rows) * term->grid.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;
LOG_DBG("moving %d lines from row %d to row %d", cell_count / term->grid.cols,
cell_src / term->grid.cols, cell_dst / term->grid.cols);
LOG_DBG("moving %d lines from row %d to row %d", cell_count / term->cols,
cell_src / term->cols, cell_dst / term->cols);
const size_t bytes = cell_count * sizeof(term->grid.cells[0]);
memmove(
@ -299,7 +299,7 @@ term_scroll_reverse_partial(struct terminal *term,
bytes);
memset(&term->grid.cells[cell_src], 0,
rows * term->grid.cols * sizeof(term->grid.cells[0]));
rows * term->cols * sizeof(term->grid.cells[0]));
term_damage_scroll(term, DAMAGE_SCROLL_REVERSE, region, rows);
}
@ -307,5 +307,5 @@ term_scroll_reverse_partial(struct terminal *term,
void
term_scroll_reverse(struct terminal *term, int rows)
{
term_scroll_reverse_partial(term, term->grid.scroll_region, rows);
term_scroll_reverse_partial(term, term->scroll_region, rows);
}

View file

@ -56,13 +56,6 @@ struct damage {
};
struct grid {
int cols;
int rows;
int cell_width;
int cell_height;
struct scroll_region scroll_region;
int linear_cursor;
struct {
int row;
@ -151,6 +144,14 @@ struct terminal {
struct vt vt;
struct grid grid;
struct kbd kbd;
int cols;
int rows;
int cell_width;
int cell_height;
struct scroll_region scroll_region;
};
void term_damage_all(struct terminal *term);

6
vt.c
View file

@ -626,7 +626,7 @@ action(struct terminal *term, enum action action, uint8_t c)
switch (c) {
case '\n':
/* LF - line feed */
if (term->grid.cursor.row == term->grid.scroll_region.end - 1) {
if (term->grid.cursor.row == term->scroll_region.end - 1) {
term_scroll(term, 1);
} else
term_cursor_down(term, 1);
@ -671,7 +671,7 @@ action(struct terminal *term, enum action action, uint8_t c)
case ACTION_PRINT: {
if (term->grid.print_needs_wrap) {
if (term->grid.cursor.row == term->grid.scroll_region.end - 1) {
if (term->grid.cursor.row == term->scroll_region.end - 1) {
term_scroll(term, 1);
term_cursor_to(term, term->grid.cursor.row, 0);
} else
@ -694,7 +694,7 @@ action(struct terminal *term, enum action action, uint8_t c)
cell->attrs = term->vt.attrs;
if (term->grid.cursor.col < term->grid.cols - 1)
if (term->grid.cursor.col < term->cols - 1)
term_cursor_right(term, 1);
else
term->grid.print_needs_wrap = true;