From 138c0aa5ee2b5324e760bea74ae0b8f69b1e8564 Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Sat, 4 Feb 2012 16:24:01 +0100 Subject: [PATCH] console: add function to erase buffer New function which helps erasing a whole region of the buffer. Signed-off-by: David Herrmann --- src/console.h | 2 ++ src/console_cell.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/console.h b/src/console.h index 7df814d..7665d58 100644 --- a/src/console.h +++ b/src/console.h @@ -72,6 +72,8 @@ kmscon_symbol_t kmscon_buffer_read(struct kmscon_buffer *buf, unsigned int x, unsigned int y); void kmscon_buffer_scroll_down(struct kmscon_buffer *buf, unsigned int num); void kmscon_buffer_scroll_up(struct kmscon_buffer *buf, unsigned int num); +void kmscon_buffer_erase_region(struct kmscon_buffer *buf, unsigned int x_from, + unsigned int y_from, unsigned int x_to, unsigned int y_to); /* console objects */ diff --git a/src/console_cell.c b/src/console_cell.c index 8ef2898..37758ec 100644 --- a/src/console_cell.c +++ b/src/console_cell.c @@ -137,6 +137,14 @@ static int init_cell(struct cell *cell) return 0; } +static void reset_cell(struct cell *cell) +{ + if (!cell) + return; + + memset(cell, 0, sizeof(*cell)); +} + static void free_line(struct line *line) { unsigned int i; @@ -201,6 +209,21 @@ static int resize_line(struct line *line, unsigned int width) return 0; } +static struct line *get_line(struct kmscon_buffer *buf, unsigned int y) +{ + if (y < buf->mtop_y) { + return buf->mtop_buf[y]; + } else if (y < buf->mtop_y + buf->scroll_y) { + y -= buf->mtop_y; + return buf->scroll_buf[y]; + } else if (y < buf->mtop_y + buf->scroll_y + buf->mbottom_y) { + y = y - buf->mtop_y - buf->scroll_y; + return buf->mbottom_buf[y]; + } + + return NULL; +} + int kmscon_buffer_new(struct kmscon_buffer **out, unsigned int x, unsigned int y) { @@ -878,3 +901,32 @@ void kmscon_buffer_scroll_up(struct kmscon_buffer *buf, unsigned int num) num * sizeof(struct line*)); buf->scroll_fill = buf->scroll_y; } + +void kmscon_buffer_erase_region(struct kmscon_buffer *buf, unsigned int x_from, + unsigned int y_from, unsigned int x_to, unsigned int y_to) +{ + unsigned int to; + struct line *line; + + if (!buf) + return; + + if (y_to >= buf->size_y) + y_to = buf->size_y - 1; + if (x_to >= buf->size_x) + x_to = buf->size_x - 1; + + for ( ; y_from <= y_to; ++y_from) { + line = get_line(buf, y_from); + if (y_from == y_to) + to = x_to; + else + to = buf->size_x - 1; + for ( ; x_from <= to; ++x_from) { + if (x_from >= line->size) + break; + reset_cell(&line->cells[x_from]); + } + x_from = 0; + } +}