X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/e0ee3d449aacd33a84cb1f58961e55f9f06acb46..b3b42765712afed5a35c9be5c832f4a06bd85e7a:/lunaix-os/kernel/tty/tty.c diff --git a/lunaix-os/kernel/tty/tty.c b/lunaix-os/kernel/tty/tty.c index cb4f1b4..d4ac8d0 100644 --- a/lunaix-os/kernel/tty/tty.c +++ b/lunaix-os/kernel/tty/tty.c @@ -1,58 +1,149 @@ +#include +#include +#include +#include +#include #include #include -#define TTY_WIDTH 80 -#define TTY_HEIGHT 25 +vga_attribute* tty_vga_buffer = (vga_attribute*)VGA_BUFFER_PADDR; -vga_atrributes *buffer = 0xB8000; +vga_attribute tty_theme_color = VGA_COLOR_BLACK; -vga_atrributes theme_color = VGA_COLOR_BLACK; +#define TTY_CLEAR \ + asm volatile("rep stosw" ::"D"(tty_vga_buffer), \ + "c"(TTY_HEIGHT * TTY_WIDTH), \ + "a"(tty_theme_color) \ + : "memory"); -uint32_t TTY_COLUMN = 0; -uint16_t TTY_ROW = 0; +void +tty_init(void* vga_buf) +{ + tty_vga_buffer = (vga_attribute*)vga_buf; -void tty_set_theme(vga_atrributes fg, vga_atrributes bg) { - theme_color = (bg << 4 | fg) << 8; + TTY_CLEAR + + io_outb(0x3D4, 0x0A); + io_outb(0x3D5, (io_inb(0x3D5) & 0xC0) | 13); + + io_outb(0x3D4, 0x0B); + io_outb(0x3D5, (io_inb(0x3D5) & 0xE0) | 15); } -void tty_put_char(char chr) { - if (chr == '\n') { - TTY_COLUMN = 0; - TTY_ROW++; - } - else if (chr == '\r') { - TTY_COLUMN = 0; - } - else { - *(buffer + TTY_COLUMN + TTY_ROW * TTY_WIDTH) = (theme_color | chr); - TTY_COLUMN++; - if (TTY_COLUMN >= TTY_WIDTH) { - TTY_COLUMN = 0; - TTY_ROW++; +void +tty_set_theme(vga_attribute fg, vga_attribute bg) +{ + tty_theme_color = (bg << 4 | fg) << 8; +} + +size_t +tty_flush_buffer(char* data, size_t pos, size_t limit, size_t buf_size) +{ + int x = 0, y = 0; + + // Clear screen + TTY_CLEAR + + int state = 0; + int g[2] = { 0, 0 }; + vga_attribute current_theme = tty_theme_color; + while (1) { + size_t ptr = pos % buf_size; + if (pos == limit) { + break; } - } + char chr = data[pos]; + if (state == 0 && chr == '\033') { + state = 1; + } else if (state == 1 && chr == '[') { + state = 2; + } else if (state > 1) { + if ('0' <= chr && chr <= '9') { + g[state - 2] = (chr - '0') + g[state - 2] * 10; + } else if (chr == ';' && state == 2) { + state = 3; + } else { + if (g[0] == 39 && g[1] == 49) { + current_theme = tty_theme_color; + } else { + current_theme = (g[1] << 4 | g[0]) << 8; + } + g[0] = 0; + g[1] = 0; + state = 0; + } + } else { + state = 0; + switch (chr) { + case '\t': + x += 4; + break; + case '\n': + y++; + // fall through + case '\r': + x = 0; + break; + case '\x08': + x = x ? x - 1 : 0; + *(tty_vga_buffer + x + y * TTY_WIDTH) = + (current_theme | 0x20); + break; + default: + *(tty_vga_buffer + x + y * TTY_WIDTH) = + (current_theme | chr); + (x)++; + break; + } - if (TTY_ROW >= TTY_HEIGHT) { - tty_scroll_up(); - TTY_ROW--; - } + if (x >= TTY_WIDTH) { + x = 0; + y++; + } + if (y >= TTY_HEIGHT) { + y--; + break; + } + } + pos++; + } + tty_set_cursor(x, y); + return pos; } -void tty_put_str(char* str) { - while (*str != '\0') { - tty_put_char(*str); - str++; +void +tty_set_cursor(uint8_t x, uint8_t y) +{ + if (x >= TTY_WIDTH || y >= TTY_HEIGHT) { + x = y = 0; } + uint32_t pos = y * TTY_WIDTH + x; + io_outb(0x3D4, 14); + io_outb(0x3D5, pos / 256); + io_outb(0x3D4, 15); + io_outb(0x3D5, pos % 256); } -void tty_scroll_up() { - // TODO use memcpy +void +tty_clear_line(int line_num) +{ + asm volatile("rep stosw" ::"D"(tty_vga_buffer + line_num * TTY_WIDTH), + "c"(TTY_WIDTH), + "a"(tty_theme_color) + : "memory"); } -void tty_clear() { - for (uint32_t x = 0; x < TTY_WIDTH; x++) { - for (uint32_t y = 0; y < TTY_HEIGHT; y++) { - *(buffer + x + y * TTY_WIDTH) = theme_color; +void +tty_put_str_at(char* str, int x, int y) +{ + char c; + while ((c = (*str)) && y < TTY_HEIGHT) { + *(tty_vga_buffer + x + y * TTY_WIDTH) = c | tty_theme_color; + x++; + if (x >= TTY_WIDTH) { + y++; + x = 0; } + str++; } } \ No newline at end of file