1

implement a scrollback buffer

This commit is contained in:
churl
2022-05-09 16:19:59 +02:00
parent b5a66f769e
commit 6f6301d5d0
7 changed files with 229 additions and 17 deletions

75
c_os/devices/BufferedCGA.cc Executable file
View File

@ -0,0 +1,75 @@
#include "BufferedCGA.h"
// Can't initialize in constructor as memory management already needs working CGA for output
// NOTE: This has to be called when memorymanagement is active
void BufferedCGA::init() {
this->scrollback_buffer = new ScrollbackBuffer(COLUMNS, ROWS, 5);
this->initialized = true;
this->print("\nInitialized scrollback buffer with 5 pages\n\n", 45);
}
void BufferedCGA::displaypage() {
if (this->current_page == 0) {
// Use pagebuffer
this->scrollback_buffer->copy_page_from_pagebuffer((cga_page_t*)CGA_START);
} else {
// Use scrollback
this->scrollback_buffer->copy_page_from_buffer((cga_line_t*)CGA_START, this->current_page - 1);
}
}
void BufferedCGA::print(char* string, int n, unsigned char attrib) {
if (this->current_page != 0) {
// Display newest content from buffer when new prints happen
this->current_page = 0;
this->displaypage();
}
CGA::print(string, n, attrib);
}
void BufferedCGA::scrollup() {
if (this->initialized) {
this->scrollback_buffer->line_to_buffer((cga_line_t*)CGA_START);
} else {
this->print("ScrollbackBuffer not initialized\n\n", 34);
}
CGA::scrollup();
}
void BufferedCGA::clear() {
CGA::clear();
this->current_page = 0;
if (this->initialized) {
this->scrollback_buffer->clear();
} else {
this->print("ScrollbackBuffer not initialized\n\n", 34);
}
}
unsigned int BufferedCGA::scroll_page_backward() {
// If this is the first scrollback we have to save the current screen content
if (this->current_page == 0) {
this->scrollback_buffer->copy_page_to_pagebuffer((cga_page_t*)CGA_START);
}
// current_page can be equal to scrollback_buffer->pages
// as we have a separate pagebuffer for the current screen content
if (this->current_page < this->scrollback_buffer->pages) {
this->current_page = this->current_page + 1;
}
this->displaypage();
return this->current_page;
}
unsigned int BufferedCGA::scroll_page_forward() {
if (this->current_page > 0) {
this->current_page = this->current_page - 1;
}
this->displaypage();
return this->current_page;
}

32
c_os/devices/BufferedCGA.h Executable file
View File

@ -0,0 +1,32 @@
#ifndef __BUFFEREDCGA_INCLUDE_H_
#define __BUFFEREDCGA_INCLUDE_H_
#include "devices/CGA.h"
#include "devices/Keyboard.h"
#include "lib/ScrollbackBuffer.h"
// NOTE: I added this file
class BufferedCGA : public CGA {
private:
ScrollbackBuffer* scrollback_buffer;
bool initialized; // Don't do ScrollbackBuffer actions if not initialized
BufferedCGA(const CGA& copy);
void displaypage(); // Write the current_page to CGA memory
public:
BufferedCGA() : CGA(), initialized(false), current_page(0) {};
void init(); // Scrollback needs to be initialized after memorymanagement
unsigned char current_page; // The page that is displayed
unsigned int scroll_page_backward(); // Scroll up the page history
unsigned int scroll_page_forward(); // Scroll down the page history (to the current page)
void print(char* string, int n, unsigned char attrib = STD_ATTR) override;
void scrollup() override;
void clear() override;
};
#endif

View File

@ -15,7 +15,7 @@
#ifndef __CGA_Stream_include__
#define __CGA_Stream_include__
#include "devices/CGA.h"
#include "devices/BufferedCGA.h"
#include "lib/OutStream.h"
// NOTE: I added this
@ -32,7 +32,8 @@ public:
CGA::color bg;
};
class CGA_Stream : public OutStream, public CGA {
// NOTE: I added this (changed this) to use BufferedCGA
class CGA_Stream : public OutStream, public BufferedCGA {
private:
CGA_Stream(CGA_Stream& copy); // Verhindere Kopieren
@ -41,7 +42,7 @@ public:
CGA::color color_bg;
bool blink;
CGA_Stream() : OutStream(), CGA() {
CGA_Stream() : OutStream(), BufferedCGA() {
color_fg = CGA::LIGHT_GREY;
color_bg = CGA::BLACK;
blink = false;

61
c_os/lib/ScrollbackBuffer.cc Executable file
View File

@ -0,0 +1,61 @@
#include "ScrollbackBuffer.h"
// NOTE: I added this file
void ScrollbackBuffer::buffer_next_line() {
this->current_linenumber = this->current_linenumber + 1;
this->current_linenumber = this->current_linenumber % this->lines;
}
CGA::cga_line_t* ScrollbackBuffer::get_current_line() const {
return (CGA::cga_line_t*)(this->buffer + this->current_linenumber);
}
void ScrollbackBuffer::line_to_buffer(CGA::cga_line_t* line) {
mymemcpy<CGA::cga_line_t>(this->get_current_line(), line);
this->buffer_next_line();
}
void ScrollbackBuffer::copy_page_from_buffer(CGA::cga_line_t* destination, unsigned char page) const {
if (page < 0 || page >= this->pages) {
return;
}
// We reverse the pagenumber so page 0 is always the current page
// (The last written line is always before curent_line
// so we need to take the last page if we want the newest lines)
unsigned char rpage = this->pages - page - 1;
// Copy linewise because page may wrap around buffer borders
// Pageheight: 4, Pages: 2 | page = 0 => rpage = 2 - 0 - 1 = 1
// LINE 0 | - line = 0 => wrapline = (5 + 1 * 4 + 0) % 8 = 1
// LINE 1 | - [...]
// LINE 2 | - line = 3 => wrapline = (5 + 1 * 4 + 3) % 8 = 4
// LINE 3 |
// LINE 4 | page = 1 => rpage = 2 - 1 - 1 = 0
// LINE 5 - current_linenumber | - line = 0 => wrapline = (5 + 0 * 4 + 0) % 8 = 5
// LINE 6 | - [...]
// LINE 7 | - line = 3 => wrapline = (5 + 0 * 4 + 3) % 8 = 0
unsigned int wrapline;
for (unsigned int line = 0; line < this->pageheight; ++line) {
wrapline = (this->current_linenumber + rpage * this->pageheight + line) % this->lines;
mymemcpy<CGA::cga_line_t>(destination + line, this->buffer + wrapline);
}
}
void ScrollbackBuffer::copy_page_from_pagebuffer(CGA::cga_page_t* destination) const {
mymemcpy<CGA::cga_page_t>(destination, this->pagebuffer);
}
void ScrollbackBuffer::copy_page_to_pagebuffer(CGA::cga_page_t* source) {
mymemcpy<CGA::cga_page_t>(this->pagebuffer, source);
}
void ScrollbackBuffer::clear() {
for (unsigned char page = 0; page < this->pages; ++page) {
myzero<CGA::cga_page_t>((CGA::cga_page_t*)this->buffer + page);
}
myzero<CGA::cga_page_t>(this->pagebuffer);
this->current_linenumber = 0;
}

39
c_os/lib/ScrollbackBuffer.h Executable file
View File

@ -0,0 +1,39 @@
#ifndef __SCROLLBACKBUFFER_INCLUDE_H_
#define __SCROLLBACKBUFFER_INCLUDE_H_
#include "devices/CGA.h"
#include "lib/MyStdLib.h"
#include <stddef.h>
// NOTE: I added this file
// The buffer dimensions must match with the destination dimensions
class ScrollbackBuffer {
private:
CGA::cga_line_t* buffer; // Circular buffer to store lines that left the screen
CGA::cga_page_t* pagebuffer; // Contains the current page separately from the scrollback
unsigned int current_linenumber;
CGA::cga_line_t* get_current_line() const; // Translate linenumber to memory location
void buffer_next_line(); // Moves the current_line one line forward
public:
const unsigned int pageheight, // Number of lines per page
pages, // Number of pages in buffer
lines; // Total number of lines in buffer
ScrollbackBuffer(unsigned char width, unsigned char height, unsigned char pages)
: current_linenumber(0), pageheight(height), pages(pages), lines(height * pages) {
this->buffer = new CGA::cga_line_t[lines];
this->pagebuffer = new CGA::cga_page_t;
this->clear(); // Null out the buffer so no crap gets displayed
}
void line_to_buffer(CGA::cga_line_t* line);
void copy_page_from_buffer(CGA::cga_line_t* destination, unsigned char page) const;
void copy_page_from_pagebuffer(CGA::cga_page_t* destination) const;
void copy_page_to_pagebuffer(CGA::cga_page_t* source);
void clear();
};
#endif

View File

@ -14,19 +14,21 @@
#include "kernel/Globals.h"
#include "user/HeapDemo.h"
// TODO: Where to put this?
// Waits for keys to control the scrollback buffer display
void scroll_mode() {
Key key;
// while (true) {
// key = kb.key_hit();
while (true) {
key = kb.key_hit();
// switch (key.ascii()) {
// case 'k':
// kout.pageup();
// case 'j':
// kout.pagedown();
// }
// }
switch ((char)key) {
case 'k':
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.scroll_page_backward()));
break;
case 'j':
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.scroll_page_forward()));
break;
}
}
}
int main() {
@ -35,8 +37,8 @@ int main() {
// Speicherverwaltung initialisieren
allocator.init();
// Initialize scrollback buffer
// kout.init();
// Initialize scrollback buffer after allocator.init()
kout.init();
allocator.dump_free_memory();
@ -45,7 +47,9 @@ int main() {
// keyboard_demo();
heap_demo();
// scroll_mode();
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.current_page));
scroll_mode();
while (1) {};
return 0;

View File

@ -21,7 +21,7 @@ void waitForReturn() {
Key key;
do {
key = kb.key_hit();
} while (key.ascii() != '\n');
} while ((char)key != '\n');
}
void heap_demo() {