banan-os/kernel/arch/i386/tty.cpp

354 lines
8.8 KiB
C++
Raw Normal View History

2022-12-07 02:41:18 +02:00
#include <kernel/IO.h>
2022-11-15 21:42:14 +02:00
#include <kernel/panic.h>
2022-12-10 03:53:44 +02:00
#include <kernel/Serial.h>
#include <kernel/tty.h>
2022-12-15 19:05:07 +02:00
#include <kernel/VESA.h>
2022-11-15 21:42:14 +02:00
#include <string.h>
2022-12-10 03:53:44 +02:00
#define BEL 0x07
#define BS 0x08
#define HT 0x09
#define LF 0x0A
#define FF 0x0C
#define CR 0x0D
#define ESC 0x1B
#define CSI '['
namespace TTY
{
2022-12-15 19:05:07 +02:00
static uint32_t terminal_height = 0;
static uint32_t terminal_width = 0;
static uint32_t terminal_row = 0;
static uint32_t terminal_col = 0;
static VESA::Color terminal_fg = VESA::Color::BRIGHT_WHITE;
2022-12-15 19:05:07 +02:00
static VESA::Color terminal_bg = VESA::Color::BLACK;
2022-12-10 03:53:44 +02:00
static char s_ansi_escape_mode = '\0';
static int s_ansi_escape_index = 0;
static int s_ansi_escape_nums[2] = { -1, -1 };
2022-12-15 19:05:07 +02:00
template<typename T> inline constexpr T max(T a, T b) { return a > b ? a : b; }
template<typename T> inline constexpr T min(T a, T b) { return a < b ? a : b; }
template<typename T> inline constexpr T clamp(T x, T a, T b) { return x < a ? a : x > b ? b : x; }
2022-12-10 03:53:44 +02:00
2022-12-15 19:05:07 +02:00
void initialize()
2022-11-15 21:42:14 +02:00
{
terminal_width = VESA::GetTerminalWidth();
terminal_height = VESA::GetTerminalHeight();
2022-11-15 21:42:14 +02:00
}
void clear()
2022-11-15 21:42:14 +02:00
{
2022-12-15 19:05:07 +02:00
VESA::Clear(VESA::Color::BLACK);
}
2022-12-15 19:05:07 +02:00
void setcolor(VESA::Color fg, VESA::Color bg)
{
2022-12-15 19:05:07 +02:00
terminal_fg = fg;
terminal_bg = bg;
}
void clear_line(size_t line)
2022-12-07 04:35:02 +02:00
{
2022-12-15 19:05:07 +02:00
for (size_t x = 0; x < terminal_width; x++)
VESA::PutEntryAt(' ', x, line, terminal_fg, terminal_bg);
2022-12-07 04:35:02 +02:00
}
static void update_cursor()
{
2022-12-15 19:05:07 +02:00
uint16_t pos = terminal_row * terminal_width + terminal_col;
IO::outb(0x3D4, 0x0F);
IO::outb(0x3D5, (uint8_t) (pos & 0xFF));
IO::outb(0x3D4, 0x0E);
IO::outb(0x3D5, (uint8_t) ((pos >> 8) & 0xFF));
}
void set_cursor_pos(int x, int y)
{
terminal_row = y;
terminal_col = x;
update_cursor();
}
2022-12-16 03:53:55 +02:00
static uint16_t handle_unicode(uint8_t ch)
{
static uint8_t unicode_left = 0;
static uint16_t codepoint = 0;
if (unicode_left)
{
if ((ch >> 6) == 0b10)
{
codepoint = (codepoint << 6) | ch;
unicode_left--;
if (unicode_left > 0)
return 0xFFFF;
return codepoint;
}
else
{
// invalid utf-8
unicode_left = 0;
return 0x00;
}
}
else
{
if ((ch >> 3) == 0b11110)
{
unicode_left = 3;
codepoint = ch & 0b00000111;
return 0xFFFF;
}
if ((ch >> 4) == 0b1110)
{
unicode_left = 2;
codepoint = ch & 0b00001111;
return 0xFFFF;
}
if ((ch >> 5) == 0b110)
{
unicode_left = 1;
codepoint = ch & 0b00011111;
return 0xFFFF;
}
}
return ch & 0x7F;
}
2022-12-10 03:53:44 +02:00
static void reset_ansi_escape()
{
2022-12-10 03:53:44 +02:00
s_ansi_escape_mode = '\0';
s_ansi_escape_index = 0;
s_ansi_escape_nums[0] = -1;
s_ansi_escape_nums[1] = -1;
}
2022-12-10 03:53:44 +02:00
static void handle_ansi_SGR()
{
switch (s_ansi_escape_nums[0])
{
2022-12-15 19:05:07 +02:00
case -1:
case 0:
terminal_fg = VESA::Color::BRIGHT_WHITE;
2022-12-15 19:05:07 +02:00
terminal_bg = VESA::Color::BLACK;
2022-12-10 03:53:44 +02:00
break;
case 30: terminal_fg = VESA::Color::BRIGHT_BLACK; break;
case 31: terminal_fg = VESA::Color::BRIGHT_RED; break;
case 32: terminal_fg = VESA::Color::BRIGHT_GREEN; break;
case 33: terminal_fg = VESA::Color::BRIGHT_YELLOW; break;
case 34: terminal_fg = VESA::Color::BRIGHT_BLUE; break;
case 35: terminal_fg = VESA::Color::BRIGHT_MAGENTA; break;
case 36: terminal_fg = VESA::Color::BRIGHT_CYAN; break;
case 37: terminal_fg = VESA::Color::BRIGHT_WHITE; break;
case 40: terminal_bg = VESA::Color::BRIGHT_BLACK; break;
case 41: terminal_bg = VESA::Color::BRIGHT_RED; break;
case 42: terminal_bg = VESA::Color::BRIGHT_GREEN; break;
case 43: terminal_bg = VESA::Color::BRIGHT_YELLOW; break;
case 44: terminal_bg = VESA::Color::BRIGHT_BLUE; break;
case 45: terminal_bg = VESA::Color::BRIGHT_MAGENTA; break;
case 46: terminal_bg = VESA::Color::BRIGHT_CYAN; break;
case 47: terminal_bg = VESA::Color::BRIGHT_WHITE; break;
}
2022-12-10 03:53:44 +02:00
}
2022-12-16 03:53:55 +02:00
static void handle_ansi_escape(uint16_t c)
2022-12-10 03:53:44 +02:00
{
switch (s_ansi_escape_mode)
{
2022-12-10 03:53:44 +02:00
case '\1':
{
if (c == CSI)
{
s_ansi_escape_mode = CSI;
return;
}
return reset_ansi_escape();
}
case CSI:
{
switch (c)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
int& val = s_ansi_escape_nums[s_ansi_escape_index];
val = (val == -1) ? (c - '0') : (val * 10 + c - '0');
return;
}
case ';':
s_ansi_escape_index++;
return;
case 'A': // Cursor Up
if (s_ansi_escape_nums[0] == -1)
s_ansi_escape_nums[0] = 1;
2022-12-15 19:05:07 +02:00
terminal_row = max<int32_t>(terminal_row - s_ansi_escape_nums[0], 0);
2022-12-10 03:53:44 +02:00
return reset_ansi_escape();
case 'B': // Curson Down
if (s_ansi_escape_nums[0] == -1)
s_ansi_escape_nums[0] = 1;
2022-12-15 19:05:07 +02:00
terminal_row = min<int32_t>(terminal_row + s_ansi_escape_nums[0], terminal_height - 1);
2022-12-10 03:53:44 +02:00
return reset_ansi_escape();
case 'C': // Cursor Forward
if (s_ansi_escape_nums[0] == -1)
s_ansi_escape_nums[0] = 1;
2022-12-15 19:05:07 +02:00
terminal_col = min<int32_t>(terminal_col + s_ansi_escape_nums[0], terminal_width - 1);
2022-12-10 03:53:44 +02:00
return reset_ansi_escape();
case 'D': // Cursor Back
if (s_ansi_escape_nums[0] == -1)
s_ansi_escape_nums[0] = 1;
2022-12-15 19:05:07 +02:00
terminal_col = max<int32_t>(terminal_col - s_ansi_escape_nums[0], 0);
2022-12-10 03:53:44 +02:00
return reset_ansi_escape();
case 'E': // Cursor Next Line
if (s_ansi_escape_nums[0] == -1)
s_ansi_escape_nums[0] = 1;
2022-12-15 19:05:07 +02:00
terminal_row = min<int32_t>(terminal_row + s_ansi_escape_nums[0], terminal_height - 1);
2022-12-10 03:53:44 +02:00
terminal_col = 0;
return reset_ansi_escape();
case 'F': // Cursor Previous Line
if (s_ansi_escape_nums[0] == -1)
s_ansi_escape_nums[0] = 1;
2022-12-15 19:05:07 +02:00
terminal_row = max<int32_t>(terminal_row - s_ansi_escape_nums[0], 0);
2022-12-10 03:53:44 +02:00
terminal_col = 0;
return reset_ansi_escape();
case 'G': // Cursor Horizontal Absolute
if (s_ansi_escape_nums[0] == -1)
s_ansi_escape_nums[0] = 1;
2022-12-15 19:05:07 +02:00
terminal_col = clamp<int32_t>(s_ansi_escape_nums[0] - 1, 0, terminal_width - 1);
2022-12-10 03:53:44 +02:00
return reset_ansi_escape();
case 'H': // Cursor Position
if (s_ansi_escape_nums[0] == -1)
s_ansi_escape_nums[0] = 1;
if (s_ansi_escape_nums[1] == -1)
s_ansi_escape_nums[1] = 1;
2022-12-15 19:05:07 +02:00
terminal_row = clamp<int32_t>(s_ansi_escape_nums[0] - 1, 0, terminal_height - 1);
terminal_col = clamp<int32_t>(s_ansi_escape_nums[1] - 1, 0, terminal_width - 1);
2022-12-10 03:53:44 +02:00
return reset_ansi_escape();
case 'J': // Erase in Display
dprintln("Unsupported ANSI CSI character J");
return reset_ansi_escape();
case 'K': // Erase in Line
switch (s_ansi_escape_nums[0])
{
case -1: case 0:
2022-12-15 19:05:07 +02:00
for (size_t i = terminal_col; i < terminal_width; i++)
VESA::PutEntryAt(' ', i, terminal_row, terminal_fg, terminal_bg);
2022-12-10 03:53:44 +02:00
break;
case 1:
for (size_t i = 0; i <= terminal_col; i++)
2022-12-15 19:05:07 +02:00
VESA::PutEntryAt(' ', i, terminal_row, terminal_fg, terminal_bg);
2022-12-10 03:53:44 +02:00
break;
case 2:
2022-12-15 19:05:07 +02:00
for (size_t i = 0; i < terminal_width; i++)
VESA::PutEntryAt(' ', i, terminal_row, terminal_fg, terminal_bg);
2022-12-10 03:53:44 +02:00
break;
}
return reset_ansi_escape();
case 'S': // Scroll Up
dprintln("Unsupported ANSI CSI character S");
return reset_ansi_escape();
case 'T': // Scroll Down
dprintln("Unsupported ANSI CSI character T");
return reset_ansi_escape();
case 'f': // Horizontal Vertical Position
dprintln("Unsupported ANSI CSI character f");
return reset_ansi_escape();
case 'm':
handle_ansi_SGR();
return reset_ansi_escape();
default:
dprintln("Unsupported ANSI CSI character {}", c);
return reset_ansi_escape();
}
}
default:
dprintln("Unsupported ANSI mode");
return reset_ansi_escape();
}
2022-12-10 03:53:44 +02:00
}
2022-12-16 03:53:55 +02:00
void putchar(char _c)
2022-12-10 03:53:44 +02:00
{
2022-12-16 03:53:55 +02:00
uint16_t cp = handle_unicode(_c);
if (cp == 0xFFFF)
return;
2022-12-10 03:53:44 +02:00
if (s_ansi_escape_mode)
2022-12-16 03:53:55 +02:00
return handle_ansi_escape(cp);
2022-12-10 03:53:44 +02:00
// https://en.wikipedia.org/wiki/ANSI_escape_code
2022-12-16 03:53:55 +02:00
switch (cp)
{
2022-12-10 03:53:44 +02:00
case BEL: // TODO
break;
case BS:
if (terminal_col > 0)
terminal_col--;
break;
case HT:
terminal_col++;
while (terminal_col % 8)
terminal_col++;
break;
case LF:
terminal_col = 0;
terminal_row++;
break;
case FF:
terminal_row++;
break;
case CR:
terminal_col = 0;
break;
case ESC:
s_ansi_escape_mode = '\1';
break;
default:
2022-12-16 03:53:55 +02:00
VESA::PutEntryAt(cp, terminal_col, terminal_row, terminal_fg, terminal_bg);
2022-12-10 03:53:44 +02:00
terminal_col++;
break;
}
2022-12-15 19:05:07 +02:00
if (terminal_col >= terminal_width)
{
terminal_col = 0;
terminal_row++;
}
2022-12-15 19:05:07 +02:00
while (terminal_row >= terminal_height)
{
2022-12-15 19:05:07 +02:00
for (size_t line = 1; line < terminal_height; line++)
VESA::ScrollLine(line);
clear_line(terminal_height - 1);
terminal_col = 0;
2022-12-10 03:53:44 +02:00
terminal_row--;
}
update_cursor();
}
void write(const char* data, size_t size)
{
for (size_t i = 0; i < size; i++)
putchar(data[i]);
}
void writestring(const char* data)
{
while (*data)
{
putchar(*data);
data++;
}
}
}