1 #include <klibc/string.h>
2 #include <lunaix/tty/tty.h>
3 #include <lunaix/common.h>
10 static vga_attribute* tty_vga_buffer = (vga_attribute*)VGA_BUFFER_PADDR;
12 static vga_attribute tty_theme_color = VGA_COLOR_BLACK;
14 static uint32_t tty_x = 0;
15 static uint32_t tty_y = 0;
17 void tty_init(void* vga_buf) {
18 tty_vga_buffer = (vga_attribute*)vga_buf;
22 io_outb(0x3D5, (io_inb(0x3D5) & 0xC0) | 13);
25 io_outb(0x3D5, (io_inb(0x3D5) & 0xE0) | 15);
28 void tty_set_buffer(void* vga_buf) {
29 tty_vga_buffer = (vga_attribute*)vga_buf;
33 tty_set_theme(vga_attribute fg, vga_attribute bg)
35 tty_theme_color = (bg << 4 | fg) << 8;
39 tty_put_char(char chr)
52 tty_x = tty_x ? tty_x - 1 : 0;
53 *(tty_vga_buffer + tty_x + tty_y * TTY_WIDTH) = (tty_theme_color | 0x20);
56 *(tty_vga_buffer + tty_x + tty_y * TTY_WIDTH) = (tty_theme_color | chr);
61 if (tty_x >= TTY_WIDTH) {
65 if (tty_y >= TTY_HEIGHT) {
70 void tty_sync_cursor() {
71 tty_set_cursor(tty_x, tty_y);
75 void tty_set_cursor(uint8_t x, uint8_t y) {
76 if (x >= TTY_WIDTH || y >= TTY_HEIGHT) {
79 uint32_t pos = y * TTY_WIDTH + x;
81 io_outb(0x3D5, pos / 256);
83 io_outb(0x3D5, pos % 256);
87 tty_put_str(char* str)
89 while (*str != '\0') {
99 size_t last_line = TTY_WIDTH * (TTY_HEIGHT - 1);
100 memcpy(tty_vga_buffer, tty_vga_buffer + TTY_WIDTH, last_line * 2);
101 for (size_t i = 0; i < TTY_WIDTH; i++) {
102 *(tty_vga_buffer + i + last_line) = tty_theme_color;
104 tty_y = tty_y == 0 ? 0 : TTY_HEIGHT - 1;
110 for (uint32_t i = 0; i < TTY_WIDTH * TTY_HEIGHT; i++) {
111 *(tty_vga_buffer + i) = tty_theme_color;
118 tty_clear_line(unsigned int y) {
119 for (size_t i = 0; i < TTY_WIDTH; i++)
121 *(tty_vga_buffer + i + y * TTY_WIDTH) = tty_theme_color;
126 tty_set_cpos(unsigned int x, unsigned int y) {
127 tty_x = x % TTY_WIDTH;
128 tty_y = y % TTY_HEIGHT;
132 tty_get_cpos(unsigned int* x, unsigned int* y) {
139 return tty_theme_color;