Updated vgaterminal.cpp to newer coding conventions.

This commit is contained in:
Jonas 'Sortie' Termansen 2012-07-22 18:22:38 +02:00
parent d75a7145ef
commit 3907e14cb8
2 changed files with 515 additions and 533 deletions

View File

@ -1,6 +1,6 @@
/****************************************************************************** /*******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of Sortix. This file is part of Sortix.
@ -14,13 +14,13 @@
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details. details.
You should have received a copy of the GNU General Public License along You should have received a copy of the GNU General Public License along with
with Sortix. If not, see <http://www.gnu.org/licenses/>. Sortix. If not, see <http://www.gnu.org/licenses/>.
vgaterminal.cpp vgaterminal.cpp
A terminal based on the VGA text mode buffer. A terminal based on a text mode buffer.
******************************************************************************/ *******************************************************************************/
#include <sortix/kernel/platform.h> #include <sortix/kernel/platform.h>
#include <sortix/kernel/log.h> #include <sortix/kernel/log.h>
@ -31,46 +31,41 @@
using namespace Maxsi; using namespace Maxsi;
namespace Sortix namespace Sortix {
{ namespace VGATerminal {
namespace VGATerminal
{
const nat width = 80;
const nat height = 25;
const uint16_t defaultcolor = (COLOR8_LIGHT_GREY << 8) | (COLOR8_BLACK << 12);
uint16_t* const vga = (uint16_t* const) 0xB8000;
uint16_t vgaattr[width * height];
const uint16_t VGAATTR_CHAR = (1U<<0U);
nat line;
nat column;
uint16_t currentcolor;
nat ansisavedposx;
nat ansisavedposy;
bool showcursor;
enum const unsigned width = 80;
{ const unsigned height = 25;
const uint16_t DEFAULT_COLOR = (COLOR8_LIGHT_GREY << 8) | (COLOR8_BLACK << 12);
uint16_t* const vga = (uint16_t* const) 0xB8000;
uint16_t vgaattr[width * height];
const uint16_t VGAATTR_CHAR = (1U<<0U);
unsigned line;
unsigned column;
uint16_t currentcolor;
unsigned ansisavedposx;
unsigned ansisavedposy;
bool showcursor;
enum
{
NONE, NONE,
CSI, CSI,
COMMAND, COMMAND,
} ansimode; } ansimode;
void UpdateCursor() void UpdateCursor()
{ {
if ( showcursor ) if ( showcursor )
{
VGA::SetCursor(column, line); VGA::SetCursor(column, line);
}
else else
{
VGA::SetCursor(width, height-1); VGA::SetCursor(width, height-1);
} }
}
// Clear the screen, put the cursor at the top left corner, set default // Clear the screen, put the cursor at the top left corner, set default
// text color, and reset ANSI escape sequence state. // text color, and reset ANSI escape sequence state.
void Reset() void Reset()
{ {
ansimode = NONE; ansimode = NONE;
line = 0; line = 0;
@ -78,14 +73,14 @@ namespace Sortix
ansisavedposx = 0; ansisavedposx = 0;
ansisavedposy = 0; ansisavedposy = 0;
currentcolor = defaultcolor; currentcolor = DEFAULT_COLOR;
for ( nat y = 0; y < height; y++ ) for ( nat y = 0; y < height; y++ )
{ {
for ( nat x = 0; x < width; x++ ) for ( nat x = 0; x < width; x++ )
{ {
unsigned index = y * width + x; unsigned index = y * width + x;
vga[index] = ' ' | defaultcolor; vga[index] = ' ' | DEFAULT_COLOR;
vgaattr[index] = 0; vgaattr[index] = 0;
} }
} }
@ -93,17 +88,17 @@ namespace Sortix
// Reset the VGA cursor. // Reset the VGA cursor.
showcursor = true; showcursor = true;
UpdateCursor(); UpdateCursor();
} }
// Initialize the terminal driver. // Initialize the terminal driver.
void Init() void Init()
{ {
Reset(); Reset();
} }
// Move every line one row up and leaves an empty line at the bottom. // Move every line one row up and leaves an empty line at the bottom.
void ScrollUp() void ScrollUp()
{ {
for ( nat y = 1; y < height; y++ ) for ( nat y = 1; y < height; y++ )
{ {
size_t linesize = width * sizeof(uint16_t); size_t linesize = width * sizeof(uint16_t);
@ -119,11 +114,11 @@ namespace Sortix
vga[index] = ' ' | currentcolor; vga[index] = ' ' | currentcolor;
vgaattr[index] = 0; vgaattr[index] = 0;
} }
} }
// Move every line one row down and leaves an empty line at the top. // Move every line one row down and leaves an empty line at the top.
void ScrollDown() void ScrollDown()
{ {
for ( nat y = 1; y < height; y++ ) for ( nat y = 1; y < height; y++ )
{ {
size_t linesize = width * sizeof(uint16_t); size_t linesize = width * sizeof(uint16_t);
@ -139,44 +134,27 @@ namespace Sortix
vga[index] = ' ' | currentcolor; vga[index] = ' ' | currentcolor;
vgaattr[index] = 0; vgaattr[index] = 0;
} }
} }
// Move to the next line. If at bottom, scroll one line up. // Move to the next line. If at bottom, scroll one line up.
void Newline() void Newline()
{ {
vgaattr[line * width + column] |= VGAATTR_CHAR; vgaattr[line * width + column] |= VGAATTR_CHAR;
if ( line < height - 1 ) if ( line < height - 1 )
{ line++;
line++; column = 0;
}
else else
{ ScrollUp();
ScrollUp(); column = 0; column = 0;
}
UpdateCursor(); UpdateCursor();
} }
void ANSIReset(); void ANSIReset();
void ParseANSIEscape(char c); void ParseANSIEscape(char c);
// Print text to the vga framebuffer, simulating how a terminal would
// display the text.
size_t Print(void* /*user*/, const char* string, size_t stringlen)
{
// Check for the bug where string contains the address 0x80000000
// which is not legal to print. It looks like we are trying to
// print a string from the stack that wasn't NUL-terminated. I
// tracked down the string value comes from LogTerminal, but it
// doesn't seem to originate from a write(2) call. Weird stuff.
addr_t straddr = (addr_t) string;
if ( straddr <= 0x80000000UL && 0x80000000UL <= straddr + stringlen )
{
PanicF("Trying to print bad string 0x%zx + 0x%zx bytes: this "
"is a known bug, but with an unknown cause.", straddr,
stringlen);
}
// Print text to the vga framebuffer, simulating how a terminal would
// display the text.
size_t Print(void* /*user*/, const char* string, size_t stringlen)
{
// Iterate over each character. // Iterate over each character.
size_t left = stringlen; size_t left = stringlen;
while ( (left--) > 0 ) while ( (left--) > 0 )
@ -253,20 +231,20 @@ namespace Sortix
UpdateCursor(); UpdateCursor();
return stringlen; return stringlen;
} }
const size_t ANSI_NUM_PARAMS = 16; const size_t ANSI_NUM_PARAMS = 16;
nat ansiusedparams; nat ansiusedparams;
nat ansiparams[ANSI_NUM_PARAMS]; nat ansiparams[ANSI_NUM_PARAMS];
nat currentparamindex; nat currentparamindex;
bool paramundefined; bool paramundefined;
bool ignoresequence; bool ignoresequence;
// TODO: The ANSI escape code is only partially implemented! // TODO: The ANSI escape code is only partially implemented!
// Begin an ANSI esacpe sequence. // Begin an ANSI esacpe sequence.
void ANSIReset() void ANSIReset()
{ {
if ( ansimode == NONE ) if ( ansimode == NONE )
{ {
ansiusedparams = 0; ansiusedparams = 0;
@ -276,14 +254,17 @@ namespace Sortix
ignoresequence = false; ignoresequence = false;
ansimode = CSI; ansimode = CSI;
} }
} }
// Reads parameters and changes font properties accordingly. // Reads parameters and changes font properties accordingly.
void AnsiSetFont() void AnsiSetFont()
{ {
// Convert from the ANSI color scheme to the VGA color scheme. // Convert from the ANSI color scheme to the VGA color scheme.
nat conversion[8] = { COLOR8_BLACK, COLOR8_RED, COLOR8_GREEN, COLOR8_BROWN, const unsigned conversion[8] =
COLOR8_BLUE, COLOR8_MAGENTA, COLOR8_CYAN, COLOR8_LIGHT_GREY }; {
COLOR8_BLACK, COLOR8_RED, COLOR8_GREEN, COLOR8_BROWN,
COLOR8_BLUE, COLOR8_MAGENTA, COLOR8_CYAN, COLOR8_LIGHT_GREY,
};
for ( size_t i = 0; i < ansiusedparams; i++ ) for ( size_t i = 0; i < ansiusedparams; i++ )
{ {
@ -291,7 +272,7 @@ namespace Sortix
// Turn all attributes off. // Turn all attributes off.
if ( cmd == 0 ) if ( cmd == 0 )
{ {
currentcolor = defaultcolor; currentcolor = DEFAULT_COLOR;
} }
// Set text color. // Set text color.
else if ( 30 <= cmd && cmd <= 37 ) else if ( 30 <= cmd && cmd <= 37 )
@ -309,11 +290,11 @@ namespace Sortix
} }
// TODO: There are many other things we don't support. // TODO: There are many other things we don't support.
} }
} }
// Executes an ASNI escape command based on given parameters. // Executes an ASNI escape command based on given parameters.
void RunANSICommand(char c) void RunANSICommand(char c)
{ {
switch ( c ) switch ( c )
{ {
// Cursor up // Cursor up
@ -502,11 +483,11 @@ namespace Sortix
} }
ansimode = NONE; ansimode = NONE;
} }
// Parse another char of an ASNI escape code. // Parse another char of an ASNI escape code.
void ParseANSIEscape(char c) void ParseANSIEscape(char c)
{ {
// Check the proper prefixes are used. // Check the proper prefixes are used.
if ( ansimode == CSI ) if ( ansimode == CSI )
{ {
@ -560,6 +541,7 @@ namespace Sortix
ansimode = NONE; ansimode = NONE;
} }
} }
}
}
} }
} // namespace VGATerminal
} // namespace Sortix

View File

@ -1,6 +1,6 @@
/****************************************************************************** /*******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of Sortix. This file is part of Sortix.
@ -14,26 +14,26 @@
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details. details.
You should have received a copy of the GNU General Public License along You should have received a copy of the GNU General Public License along with
with Sortix. If not, see <http://www.gnu.org/licenses/>. Sortix. If not, see <http://www.gnu.org/licenses/>.
vgaterminal.h vgaterminal.cpp
A terminal based on the VGA text mode buffer. A terminal based on a text mode buffer.
******************************************************************************/ *******************************************************************************/
#ifndef SORTIX_VGATERMINAL_H #ifndef SORTIX_VGATERMINAL_H
#define SORTIX_VGATERMINAL_H #define SORTIX_VGATERMINAL_H
namespace Sortix namespace Sortix {
{ namespace VGATerminal {
namespace VGATerminal
{ void Init();
void Init(); void Reset();
void Reset(); size_t Print(void* user, const char* string, size_t stringlen);
size_t Print(void* user, const char* string, size_t stringlen);
} } // namespace VGATerminal
} } // namespace Sortix
#endif #endif