Kernel: Add per terminal palette for TerminalDriver

This allows better color approximation on text mode and in future will
allow user to possibly change the palette
This commit is contained in:
2025-04-23 18:44:37 +03:00
parent 4d840a8d9a
commit cc7c1ad30d
9 changed files with 121 additions and 102 deletions

View File

@@ -27,9 +27,10 @@ namespace Kernel
const LibFont::Font& font() const override { return m_font; };
private:
FramebufferTerminalDriver(BAN::RefPtr<FramebufferDevice> framebuffer_device)
: m_framebuffer_device(framebuffer_device)
{ }
FramebufferTerminalDriver(BAN::RefPtr<FramebufferDevice> framebuffer_device, const Palette& palette)
: TerminalDriver(palette)
, m_framebuffer_device(framebuffer_device)
{}
void read_cursor();
void show_cursor(bool use_data);
@@ -42,7 +43,7 @@ namespace Kernel
uint32_t m_cursor_y { 0 };
bool m_cursor_shown { true };
BAN::Vector<uint32_t> m_cursor_data;
static constexpr Color m_cursor_color = TerminalColor::BRIGHT_WHITE;
static constexpr Color m_cursor_color = TerminalColor::WHITE;
};
}

View File

@@ -12,20 +12,28 @@ namespace Kernel
public:
struct Color
{
constexpr Color()
: rgb(0)
{ }
constexpr Color(uint32_t rgb)
: rgb(rgb)
{ }
constexpr Color(uint8_t r, uint8_t g, uint8_t b)
: rgb(((uint32_t)r << 16) | ((uint32_t)g << 8) | b)
{ }
uint8_t red() const { return (rgb >> 0x10) & 0xFF; }
uint8_t green() const { return (rgb >> 0x08) & 0xFF; }
uint8_t blue() const { return (rgb >> 0x00) & 0xFF; }
constexpr uint8_t red() const { return (rgb >> 0x10) & 0xFF; }
constexpr uint8_t green() const { return (rgb >> 0x08) & 0xFF; }
constexpr uint8_t blue() const { return (rgb >> 0x00) & 0xFF; }
uint32_t rgb;
};
using Palette = BAN::Array<Color, 16>;
public:
static BAN::ErrorOr<void> initialize_from_boot_info();
TerminalDriver(const Palette& palette)
: m_palette(palette)
{}
virtual ~TerminalDriver() {}
virtual uint32_t width() const = 0;
virtual uint32_t height() const = 0;
@@ -40,29 +48,19 @@ namespace Kernel
virtual bool has_font() const { return false; }
virtual BAN::ErrorOr<void> set_font(LibFont::Font&&) { return BAN::Error::from_errno(EINVAL); }
virtual const LibFont::Font& font() const { ASSERT_NOT_REACHED(); }
const Palette& palette() const { return m_palette; }
protected:
Palette m_palette;
};
extern BAN::RefPtr<TerminalDriver> g_terminal_driver;
namespace TerminalColor
{
static constexpr TerminalDriver::Color BLACK = 0x000000;
static constexpr TerminalDriver::Color RED = 0xFF0000;
static constexpr TerminalDriver::Color GREEN = 0x00FF00;
static constexpr TerminalDriver::Color YELLOW = 0xFFFF00;
static constexpr TerminalDriver::Color BLUE = 0x0000FF;
static constexpr TerminalDriver::Color MAGENTA = 0xFF00FF;
static constexpr TerminalDriver::Color CYAN = 0x00FFFF;
static constexpr TerminalDriver::Color WHITE = 0xBFBFBF;
static constexpr TerminalDriver::Color BRIGHT_BLACK = 0x3F3F3F;
static constexpr TerminalDriver::Color BRIGHT_RED = 0xFF7F7F;
static constexpr TerminalDriver::Color BRIGHT_GREEN = 0x7FFF7F;
static constexpr TerminalDriver::Color BRIGHT_YELLOW = 0xFFFF7F;
static constexpr TerminalDriver::Color BRIGHT_BLUE = 0x7F7FFF;
static constexpr TerminalDriver::Color BRIGHT_MAGENTA = 0xFF7FFF;
static constexpr TerminalDriver::Color BRIGHT_CYAN = 0x7FFFFF;
static constexpr TerminalDriver::Color BRIGHT_WHITE = 0xFFFFFF;
constexpr TerminalDriver::Color BLACK = 0x000000;
constexpr TerminalDriver::Color WHITE = 0xFFFFFF;
}
}

View File

@@ -21,8 +21,9 @@ namespace Kernel
void set_cursor_position(uint32_t, uint32_t) override;
private:
TextModeTerminalDriver(paddr_t paddr, uint32_t width, uint32_t height, uint32_t pitch)
: m_paddr(paddr)
TextModeTerminalDriver(paddr_t paddr, uint32_t width, uint32_t height, uint32_t pitch, const Palette& palette)
: TerminalDriver(palette)
, m_paddr(paddr)
, m_width(width)
, m_height(height)
, m_pitch(pitch)
@@ -36,7 +37,7 @@ namespace Kernel
const uint32_t m_height;
const uint32_t m_pitch;
vaddr_t m_vaddr { 0 };
static constexpr Color s_cursor_color = TerminalColor::BRIGHT_WHITE;
static constexpr Color s_cursor_color = TerminalColor::WHITE;
};
}

View File

@@ -11,6 +11,9 @@ namespace Kernel
class VirtualTTY : public TTY
{
public:
using Palette = TerminalDriver::Palette;
public:
static BAN::ErrorOr<BAN::RefPtr<VirtualTTY>> create(BAN::RefPtr<TerminalDriver>);
@@ -60,14 +63,16 @@ namespace Kernel
struct Cell
{
TerminalDriver::Color foreground { TerminalColor::BRIGHT_WHITE };
TerminalDriver::Color background { TerminalColor::BLACK };
TerminalDriver::Color foreground;
TerminalDriver::Color background;
uint32_t codepoint { ' ' };
};
private:
BAN::String m_name;
BAN::RefPtr<TerminalDriver> m_terminal_driver;
State m_state { State::Normal };
AnsiState m_ansi_state { };
UTF8State m_utf8_state { };
@@ -84,11 +89,11 @@ namespace Kernel
uint32_t m_column { 0 };
Cell* m_buffer { nullptr };
TerminalDriver::Color m_foreground { TerminalColor::BRIGHT_WHITE };
TerminalDriver::Color m_background { TerminalColor::BLACK };
bool m_colors_inverted { false };
const Palette& m_palette;
BAN::RefPtr<TerminalDriver> m_terminal_driver;
TerminalDriver::Color m_foreground;
TerminalDriver::Color m_background;
bool m_colors_inverted { false };
};
}