Merge branch 'master' of github.com:Minep/lunaix-os
[lunaix-os.git] / lunaix-os / kernel / tty / tty.c
index bef5c5a90213a1271c7d10cca1a7bdb6f67a2efb..c779ed524029b6eaa89f6efc11a92f6a0512ff86 100644 (file)
@@ -1,21 +1,38 @@
-#include <libc/string.h>
+#include <klibc/string.h>
 #include <lunaix/tty/tty.h>
+#include <lunaix/common.h>
 #include <stdint.h>
+#include <hal/io.h>
 
 #define TTY_WIDTH 80
 #define TTY_HEIGHT 25
 
-vga_attribute* buffer = (vga_attribute*)0xB8000;
+static vga_attribute* tty_vga_buffer = (vga_attribute*)VGA_BUFFER_PADDR;
 
-vga_attribute theme_color = VGA_COLOR_BLACK;
+static vga_attribute tty_theme_color = VGA_COLOR_BLACK;
 
-uint32_t tty_x = 0;
-uint16_t tty_y = 0;
+static uint32_t tty_x = 0;
+static uint32_t tty_y = 0;
+
+void tty_init(void* vga_buf) {
+    tty_vga_buffer = (vga_attribute*)vga_buf;
+    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_set_buffer(void* vga_buf) {
+    tty_vga_buffer = (vga_attribute*)vga_buf;
+}
 
 void
 tty_set_theme(vga_attribute fg, vga_attribute bg)
 {
-    theme_color = (bg << 4 | fg) << 8;
+    tty_theme_color = (bg << 4 | fg) << 8;
 }
 
 void
@@ -26,14 +43,17 @@ tty_put_char(char chr)
             tty_x += 4;
             break;
         case '\n':
-            tty_x = 0;
             tty_y++;
-            break;
+            // fall through
         case '\r':
             tty_x = 0;
             break;
+        case '\x08':
+            tty_x = tty_x ? tty_x - 1 : 0;
+            *(tty_vga_buffer + tty_x + tty_y * TTY_WIDTH) = (tty_theme_color | 0x20);
+            break;
         default:
-            *(buffer + tty_x + tty_y * TTY_WIDTH) = (theme_color | chr);
+            *(tty_vga_buffer + tty_x + tty_y * TTY_WIDTH) = (tty_theme_color | chr);
             tty_x++;
             break;
     }
@@ -47,6 +67,22 @@ tty_put_char(char chr)
     }
 }
 
+void tty_sync_cursor() {
+    tty_set_cursor(tty_x, tty_y);
+}
+
+
+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_put_str(char* str)
 {
@@ -54,25 +90,51 @@ tty_put_str(char* str)
         tty_put_char(*str);
         str++;
     }
+    tty_sync_cursor();
 }
 
 void
 tty_scroll_up()
 {
     size_t last_line = TTY_WIDTH * (TTY_HEIGHT - 1);
-    memcpy(buffer, buffer + TTY_WIDTH, last_line);
+    memcpy(tty_vga_buffer, tty_vga_buffer + TTY_WIDTH, last_line * 2);
     for (size_t i = 0; i < TTY_WIDTH; i++) {
-        *(buffer + i + last_line) = theme_color;
+        *(tty_vga_buffer + i + last_line) = tty_theme_color;
     }
-    tty_y = tty_y == 0 ? 0 : tty_y - 1;
+    tty_y = tty_y == 0 ? 0 : TTY_HEIGHT - 1;
 }
 
 void
 tty_clear()
 {
     for (uint32_t i = 0; i < TTY_WIDTH * TTY_HEIGHT; i++) {
-        *(buffer + i) = theme_color;
+        *(tty_vga_buffer + i) = tty_theme_color;
     }
     tty_x = 0;
     tty_y = 0;
+}
+
+void
+tty_clear_line(unsigned int y) {
+    for (size_t i = 0; i < TTY_WIDTH; i++)
+    {
+        *(tty_vga_buffer + i + y * TTY_WIDTH) = tty_theme_color;
+    }
+}
+
+void
+tty_set_cpos(unsigned int x, unsigned int y) {
+    tty_x = x % TTY_WIDTH;
+    tty_y = y % TTY_HEIGHT;
+}
+
+void
+tty_get_cpos(unsigned int* x, unsigned int* y) {
+    *x = tty_x;
+    *y = tty_y;
+}
+
+vga_attribute
+tty_get_theme() {
+    return tty_theme_color;
 }
\ No newline at end of file