From 0c7e50433a9a7e4947efca468fe3e72df2a0ae2f Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Fri, 3 Feb 2012 23:47:09 +0100 Subject: [PATCH] console: implement proper scrolling Add two new functions to rotate the scroll-area of the buffer. We push lines to the scroll-back buffer if they are pushed out to the top. Lines pushed out to the bottom are simply freed. We never take back lines from the scrollback buffer as applications expect the new lines to be empty. Signed-off-by: David Herrmann --- src/console.h | 2 ++ src/console_cell.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/console.h b/src/console.h index 0731ab4..b9157ea 100644 --- a/src/console.h +++ b/src/console.h @@ -71,6 +71,8 @@ void kmscon_buffer_write(struct kmscon_buffer *buf, unsigned int x, kmscon_symbol_t kmscon_buffer_read(struct kmscon_buffer *buf, unsigned int x, unsigned int y); void kmscon_buffer_newline(struct kmscon_buffer *buf); +void kmscon_buffer_scroll_down(struct kmscon_buffer *buf, unsigned int num); +void kmscon_buffer_scroll_up(struct kmscon_buffer *buf, unsigned int num); /* console objects */ diff --git a/src/console_cell.c b/src/console_cell.c index 9aeaf28..076bc86 100644 --- a/src/console_cell.c +++ b/src/console_cell.c @@ -864,3 +864,42 @@ void kmscon_buffer_newline(struct kmscon_buffer *buf) buf->scroll_buf[buf->scroll_fill] = nl; buf->scroll_fill++; } + +void kmscon_buffer_scroll_down(struct kmscon_buffer *buf, unsigned int num) +{ + unsigned int i; + + if (!buf || !num) + return; + + if (num > buf->scroll_y) + num = buf->scroll_y; + + for (i = 0; i < num; ++i) + free_line(buf->scroll_buf[buf->scroll_y - i - 1]); + + memmove(&buf->scroll_buf[num], buf->scroll_buf, + (buf->scroll_y - num) * sizeof(struct line*)); + memset(buf->scroll_buf, 0, num * sizeof(struct line*)); + buf->scroll_fill = buf->scroll_y; +} + +void kmscon_buffer_scroll_up(struct kmscon_buffer *buf, unsigned int num) +{ + unsigned int i; + + if (!buf || !num) + return; + + if (num > buf->scroll_y) + num = buf->scroll_y; + + for (i = 0; i < num; ++i) + link_to_scrollback(buf, buf->scroll_buf[i]); + + memmove(buf->scroll_buf, &buf->scroll_buf[num], + (buf->scroll_y - num) * sizeof(struct line*)); + memset(&buf->scroll_buf[buf->scroll_y - num], 0, + num * sizeof(struct line*)); + buf->scroll_fill = buf->scroll_y; +}