wip: initial scrolling support (no scrollback though)

This commit is contained in:
Daniel Eklöf 2019-06-19 10:27:31 +02:00
parent 71dde121e6
commit efc8cc4914
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 39 additions and 2 deletions

24
grid.c
View file

@ -3,6 +3,10 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#define LOG_MODULE "grid"
#define LOG_ENABLE_DBG 1
#include "log.h"
#define min(x, y) ((x) < (y) ? (x) : (y)) #define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y))
@ -11,6 +15,7 @@ grid_erase(struct grid *grid, int start, int end)
{ {
for (int i = start; i < end; i++) { for (int i = start; i < end; i++) {
struct cell *cell = &grid->cells[i]; struct cell *cell = &grid->cells[i];
/* TODO: memset entire range */
memset(cell, 0, sizeof(*cell)); memset(cell, 0, sizeof(*cell));
cell->attrs.foreground = grid->foreground; cell->attrs.foreground = grid->foreground;
@ -76,3 +81,22 @@ grid_cursor_down(struct grid *grid, int count)
int move_amount = min(grid->rows - grid->cursor.row - 1, count); int move_amount = min(grid->rows - grid->cursor.row - 1, count);
grid_cursor_to(grid, grid->cursor.row + move_amount, grid->cursor.col); grid_cursor_to(grid, grid->cursor.row + move_amount, grid->cursor.col);
} }
void
grid_scroll(struct grid *grid, int rows)
{
if (rows >= grid->rows) {
grid_erase(grid, 0, grid->rows * grid->cols);
return;
}
int first_row = rows;
int cell_start = first_row * grid->cols;
int cell_end = grid->rows * grid->cols;
int count = cell_end - cell_start;
LOG_DBG("moving %d cells from %d", count, cell_start);
memmove(&grid->cells[0], &grid->cells[cell_start], count * sizeof(grid->cells[0]));
grid_erase(grid, cell_end - rows * grid->cols, grid->rows * grid->cols);
grid->all_dirty = true;
}

3
grid.h
View file

@ -10,7 +10,10 @@ void grid_cursor_right(struct grid *grid, int count);
void grid_cursor_up(struct grid *grid, int count); void grid_cursor_up(struct grid *grid, int count);
void grid_cursor_down(struct grid *grid, int count); void grid_cursor_down(struct grid *grid, int count);
void grid_scroll(struct grid *grid, int rows);
int grid_cursor_linear(const struct grid *grid, int row, int col); int grid_cursor_linear(const struct grid *grid, int row, int col);
//void grid_cursor_to(struct grid *grid, int pos); //void grid_cursor_to(struct grid *grid, int pos);
//void grid_cursor_move(struct grid *grid, int cols); //void grid_cursor_move(struct grid *grid, int cols);

14
vt.c
View file

@ -158,7 +158,11 @@ action(struct terminal *term, enum action action, uint8_t c)
LOG_DBG("execute: 0x%02x", c); LOG_DBG("execute: 0x%02x", c);
switch (c) { switch (c) {
case '\n': case '\n':
grid_cursor_down(&term->grid, 1); LOG_DBG("NEWLINE: %d %d", term->grid.cursor.row, term->grid.rows);
if (term->grid.cursor.row == term->grid.rows - 1) {
grid_scroll(&term->grid, 1);
} else
grid_cursor_down(&term->grid, 1);
break; break;
case '\r': case '\r':
@ -188,8 +192,14 @@ action(struct terminal *term, enum action action, uint8_t c)
break; break;
case ACTION_PRINT: { case ACTION_PRINT: {
if (term->grid.print_needs_wrap) if (term->grid.print_needs_wrap) {
if (term->grid.cursor.row == term->grid.rows - 1) {
assert(false);
grid_scroll(&term->grid, 1);
}
grid_cursor_to(&term->grid, term->grid.cursor.row + 1, 0); grid_cursor_to(&term->grid, term->grid.cursor.row + 1, 0);
}
struct cell *cell = &term->grid.cells[term->grid.linear_cursor]; struct cell *cell = &term->grid.cells[term->grid.linear_cursor];