cb4f1b4bc76cc69f8ea68a1e4ba2136bf0c0143a
[lunaix-os.git] / lunaix-os / kernel / tty / tty.c
1 #include <lunaix/tty/tty.h>
2 #include <stdint.h>
3
4 #define TTY_WIDTH 80
5 #define TTY_HEIGHT 25
6
7 vga_atrributes *buffer = 0xB8000;
8
9 vga_atrributes theme_color = VGA_COLOR_BLACK;
10
11 uint32_t TTY_COLUMN = 0;
12 uint16_t TTY_ROW = 0;
13
14 void tty_set_theme(vga_atrributes fg, vga_atrributes bg) {
15     theme_color = (bg << 4 | fg) << 8;
16 }
17
18 void tty_put_char(char chr) {
19     if (chr == '\n') {
20         TTY_COLUMN = 0;
21         TTY_ROW++;
22     }
23     else if (chr == '\r') {
24         TTY_COLUMN = 0;
25     }
26     else {
27         *(buffer + TTY_COLUMN + TTY_ROW * TTY_WIDTH) = (theme_color | chr);
28         TTY_COLUMN++;
29         if (TTY_COLUMN >= TTY_WIDTH) {
30             TTY_COLUMN = 0;
31             TTY_ROW++;
32         }
33     }
34
35     if (TTY_ROW >= TTY_HEIGHT) {
36         tty_scroll_up();
37         TTY_ROW--;
38     } 
39 }
40
41 void tty_put_str(char* str) {
42     while (*str != '\0') {
43         tty_put_char(*str);
44         str++;
45     }
46 }
47
48 void tty_scroll_up() {
49     // TODO use memcpy
50 }
51
52 void tty_clear() {
53     for (uint32_t x = 0; x < TTY_WIDTH; x++) {
54         for (uint32_t y = 0; y < TTY_HEIGHT; y++) {
55             *(buffer + x + y * TTY_WIDTH) = theme_color;
56         }
57     }
58 }