feature: multiple screens + cursor move (bug to fix)

style: reorganise code structure
This commit is contained in:
2024-09-08 05:03:50 +02:00
parent d270657fc9
commit cf617d6984
13 changed files with 206 additions and 173 deletions

130
src/terminal/put.c Normal file
View File

@ -0,0 +1,130 @@
#include "ctype.h"
#include "kprintf.h"
#include "string.h"
#include "sys/io.h"
#include "terminal.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
static struct screen screens[TERM_COUNT];
struct screen *screen = &screens[0];
static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg)
{
return fg | bg << 4;
}
static inline uint16_t vga_entry(unsigned char uc, uint8_t color)
{
return (uint16_t)uc | (uint16_t)color << 8;
}
void terminal_initialize(void)
{
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
TERM_BUF[index] = vga_entry(' ', screen->color);
}
}
for (int i = 0; i < TERM_COUNT; i++) {
screens[i].row = 0;
screens[i].column = 0;
screens[i].color =
vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
memcpy(screens[i].buffer, TERM_BUF, sizeof(screen->buffer));
}
}
void terminal_set_screen(int pos)
{
memcpy(screen->buffer, TERM_BUF, sizeof(screen->buffer));
screen = &screens[pos];
memcpy(TERM_BUF, screen->buffer, sizeof(screen->buffer));
}
void terminal_setcolor(uint8_t color)
{
screen->color = color;
}
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y)
{
const size_t index = y * VGA_WIDTH + x;
TERM_BUF[index] = vga_entry(c, color);
}
static void terminal_scroll(void)
{
screen->row--;
for (size_t i = 0; i < VGA_WIDTH * (VGA_HEIGHT - 1); i++)
TERM_BUF[i] = TERM_BUF[i + VGA_WIDTH];
for (size_t i = 0; i < VGA_WIDTH; i++)
terminal_putentryat(' ', screen->color, i, VGA_HEIGHT - 1);
}
static void terminal_new_line(void)
{
screen->column = 0;
if (++screen->row == VGA_HEIGHT)
terminal_scroll();
}
int terminal_putchar(char c)
{
if (c == '\r')
screen->column = 0;
else if (c == '\n')
terminal_new_line();
if (!isprint(c))
return 1;
terminal_putentryat(c, screen->color, screen->column, screen->row);
if (++screen->column == VGA_WIDTH)
terminal_new_line();
return 1;
}
int terminal_write(const char *data, size_t size)
{
size_t i;
for (i = 0; i < size; i++)
terminal_putchar(data[i]);
return (int)i;
}
int terminal_writestring(const char *data)
{
size_t len = strlen(data);
terminal_write(data, len);
return len;
}
int terminal_writelong(long n)
{
long div = 10;
int rv = 0;
if (n < 0) {
rv += terminal_putchar('-');
n *= -1;
}
div = 1;
while (div <= n / 10)
div *= 10;
while (div > 0) {
rv += terminal_putchar('0' + n / div % 10);
div /= 10;
}
return rv;
}
void update_cursor(void)
{
uint16_t pos = screen->row * VGA_WIDTH + screen->column;
outb(0x3D4, 0x0F);
outb(0x3D5, pos & 0xFF);
outb(0x3D4, 0x0E);
outb(0x3D5, (pos >> 8) & 0xFF);
}