From 4585df532c3c34ef86a94fda6d53a3f6df5244a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Mon, 17 Jun 2019 19:33:10 +0200 Subject: [PATCH] wip: vt parsing: break out grid operating functions --- csi.c | 34 +++++++--------------------------- grid.c | 34 ++++++++++++++++++++++++++++++++++ grid.h | 8 ++++++++ meson.build | 1 + vt.c | 15 ++++++--------- 5 files changed, 56 insertions(+), 36 deletions(-) create mode 100644 grid.c create mode 100644 grid.h diff --git a/csi.c b/csi.c index 15e364f5..49bb1eb7 100644 --- a/csi.c +++ b/csi.c @@ -8,6 +8,7 @@ #define LOG_MODULE "csi" #define LOG_ENABLE_DBG 1 #include "log.h" +#include "grid.h" static bool csi_sgr(struct terminal *term) @@ -74,21 +75,6 @@ csi_sgr(struct terminal *term) return true; } -static bool -erase(struct terminal *term, int start, int end) -{ - for (int i = start; i < end; i++) { - struct cell *cell = &term->grid.cells[i]; - memset(cell, 0, sizeof(*cell)); - cell->attrs.foreground = term->grid.foreground; - cell->attrs.background = term->grid.background; - cell->dirty = true; - term->grid.dirty = true; - } - - return true; -} - bool csi_dispatch(struct terminal *term, uint8_t final) { @@ -112,7 +98,8 @@ csi_dispatch(struct terminal *term, uint8_t final) assert(term->vt.params.idx == 0); int start = term->grid.cursor / term->grid.cols * term->grid.cols; int end = term->grid.cols * term->grid.rows; - return erase(term, start, end); + grid_erase(&term->grid, start, end); + return true; } case 'K': { @@ -122,26 +109,19 @@ csi_dispatch(struct terminal *term, uint8_t final) assert(start <= end); assert((end + 1) % term->grid.cols == 0); - return erase(term, start, end); + grid_erase(&term->grid, start, end); + return true; } case 'C': { int count = term->vt.params.idx > 0 ? term->vt.params.v[0].value : 1; - int new_cursor = term->grid.cursor + count; - term->grid.cells[term->grid.cursor].dirty = true; - term->grid.cells[new_cursor].dirty = true; - term->grid.cursor = new_cursor; - term->grid.dirty = true; + grid_cursor_move(&term->grid, count); return true; } case 'D': { int count = term->vt.params.idx > 0 ? term->vt.params.v[0].value : 1; - int new_cursor = term->grid.cursor - count; - term->grid.cells[term->grid.cursor].dirty = true; - term->grid.cells[new_cursor].dirty = true; - term->grid.cursor = new_cursor; - term->grid.dirty = true; + grid_cursor_move(&term->grid, -count); return true; } diff --git a/grid.c b/grid.c new file mode 100644 index 00000000..dc94ceae --- /dev/null +++ b/grid.c @@ -0,0 +1,34 @@ +#include "grid.h" + +#include + +void +grid_erase(struct grid *grid, int start, int end) +{ + for (int i = start; i < end; i++) { + struct cell *cell = &grid->cells[i]; + memset(cell, 0, sizeof(*cell)); + + cell->attrs.foreground = grid->foreground; + cell->attrs.background = grid->background; + + cell->dirty = true; + grid->dirty = true; + } +} + +void +grid_cursor_move(struct grid *grid, int cols) +{ + int new_cursor = grid->cursor + cols; + grid->cells[grid->cursor].dirty = true; + grid->cells[new_cursor].dirty = true; + grid->cursor = new_cursor; + grid->dirty = true; +} + +void +grid_cursor_to(struct grid *grid, int pos) +{ + grid_cursor_move(grid, pos - grid->cursor); +} diff --git a/grid.h b/grid.h new file mode 100644 index 00000000..3f347cff --- /dev/null +++ b/grid.h @@ -0,0 +1,8 @@ +#pragma once + +#include "terminal.h" + +void grid_erase(struct grid *grid, int start, int end); + +void grid_cursor_to(struct grid *grid, int pos); +void grid_cursor_move(struct grid *grid, int cols); diff --git a/meson.build b/meson.build index f1953050..249c5f43 100644 --- a/meson.build +++ b/meson.build @@ -59,6 +59,7 @@ executable( 'f00ter', 'csi.c', 'csi.h', 'font.c', 'font.h', + 'grid.c', 'grid.h', 'log.c', 'log.h', 'main.c', 'osc.c', 'osc.h', diff --git a/vt.c b/vt.c index 3b6d98c3..21630292 100644 --- a/vt.c +++ b/vt.c @@ -9,6 +9,7 @@ #include "log.h" #include "csi.h" #include "osc.h" +#include "grid.h" /* https://vt100.net/emu/dec_ansi_parser */ @@ -157,17 +158,13 @@ action(struct terminal *term, enum action action, uint8_t c) LOG_DBG("execute: 0x%02x", c); switch (c) { case '\r': - term->grid.cells[term->grid.cursor].dirty = true; - term->grid.cursor = term->grid.cursor / term->grid.cols * term->grid.cols; - term->grid.cells[term->grid.cursor].dirty = true; - term->grid.dirty = true; + grid_cursor_to( + &term->grid, + term->grid.cursor / term->grid.cols * term->grid.cols); break; case '\b': - term->grid.cells[term->grid.cursor].dirty = true; - term->grid.cursor--; - term->grid.cells[term->grid.cursor].dirty = true; - term->grid.dirty = true; + grid_cursor_move(&term->grid, -1); break; } @@ -197,7 +194,7 @@ action(struct terminal *term, enum action action, uint8_t c) cell->attrs = term->vt.attrs; - term->grid.cells[++term->grid.cursor].dirty = true; + grid_cursor_move(&term->grid, 1); term->grid.dirty = true; break; }