109 lines
2.5 KiB
C++

#include "os/tty.hpp"
#include <cstring>
#include <vector>
#include "libs/psf.hpp"
#include "os/draw.hpp"
#include "os/resources/lat9-08.psf.hpp"
namespace tty
{
namespace
{
size_t gTerminalRow = 0;
size_t gTerminalColumn = 0;
size_t gTerminalWidth = 0;
size_t gTerminalHeight = 0;
VgaDoubleColor gTerminalColor = VgaDoubleColor();
// uint16_t* gTerminalBuffer = reinterpret_cast<uint16_t*>(0xB8000);
std::vector<uint16_t> gTerminalBuffer;
psf::Font gTerminalFont;
constexpr uint8_t vgaEntryColor(VgaDoubleColor color)
{
return static_cast<uint8_t>(color.foreground) | static_cast<uint8_t>(color.background) << 4;
}
constexpr uint16_t vgaEntry(const unsigned char chr, const VgaDoubleColor color)
{
return static_cast<uint16_t>(chr) | static_cast<uint16_t>(vgaEntryColor(color) << 8);
}
void newline()
{
gTerminalColumn = 0;
if (gTerminalRow < gTerminalHeight - 1)
{
++gTerminalRow;
}
else
{
draw::scrollBy(0, static_cast<int>(gTerminalFont.getGlyphHeight()) + 1);
}
}
}
void initialize() noexcept
{
setPSFFont(&LAT9_08[0], LAT9_08_SIZE);
}
void setColor(VgaDoubleColor color) noexcept
{
gTerminalColor = color;
}
void putEntryAt(char chr, VgaDoubleColor color, size_t posX, size_t posY) noexcept
{
draw::character(
/* posX = */ (gTerminalFont.getGlyphWidth() + 1) * posX,
/* posY = */ (gTerminalFont.getGlyphHeight() + 1) * posY,
/* font = */ gTerminalFont,
/* chr = */ chr,
/* fgColor = */ draw::VGA_PIXELS[static_cast<uint8_t>(color.foreground)],
/* bgColor = */ draw::VGA_PIXELS[static_cast<uint8_t>(color.background)]
);
}
void putChar(char chr) noexcept
{
if (chr == '\n')
{
newline();
return;
}
putEntryAt(chr, gTerminalColor, gTerminalColumn, gTerminalRow);
if (++gTerminalColumn == gTerminalWidth)
{
newline();
}
}
void write(const char* data, size_t size) noexcept
{
for (size_t idx = 0; idx < size; idx++)
{
putChar(data[idx]);
}
}
void write(const char* data) noexcept
{
write(data, std::strlen(data));
}
bool setPSFFont(const void* data, size_t length) noexcept
{
psf::Font newFont;
if (!psf::Font::create(data, length, newFont)) {
return false;
}
gTerminalFont = newFont;
gTerminalWidth = draw::getPrimaryFramebuffer().getWidth() / (gTerminalFont.getGlyphWidth() + 1);
gTerminalHeight = draw::getPrimaryFramebuffer().getHeight() / (gTerminalFont.getGlyphHeight() + 1);
return true;
}
}