remove build folder
[lunaix-os.git] / lunaix-os / kernel / tty / tty.c
1 #include <libc/string.h>
2 #include <lunaix/tty/tty.h>
3 #include <stdint.h>
4
5 #define TTY_WIDTH 80
6 #define TTY_HEIGHT 25
7
8 vga_attribute* buffer = (vga_attribute*)0xB8000;
9
10 vga_attribute theme_color = VGA_COLOR_BLACK;
11
12 uint32_t tty_x = 0;
13 uint16_t tty_y = 0;
14
15 void
16 tty_set_theme(vga_attribute fg, vga_attribute bg)
17 {
18     theme_color = (bg << 4 | fg) << 8;
19 }
20
21 void
22 tty_put_char(char chr)
23 {
24     switch (chr) {
25         case '\t':
26             tty_x += 4;
27             break;
28         case '\n':
29             tty_x = 0;
30             tty_y++;
31             break;
32         case '\r':
33             tty_x = 0;
34             break;
35         default:
36             *(buffer + tty_x + tty_y * TTY_WIDTH) = (theme_color | chr);
37             tty_x++;
38             break;
39     }
40
41     if (tty_x >= TTY_WIDTH) {
42         tty_x = 0;
43         tty_y++;
44     }
45     if (tty_y >= TTY_HEIGHT) {
46         tty_scroll_up();
47     }
48 }
49
50 void
51 tty_put_str(char* str)
52 {
53     while (*str != '\0') {
54         tty_put_char(*str);
55         str++;
56     }
57 }
58
59 void
60 tty_scroll_up()
61 {
62     size_t last_line = TTY_WIDTH * (TTY_HEIGHT - 1);
63     memcpy(buffer, buffer + TTY_WIDTH, last_line);
64     for (size_t i = 0; i < TTY_WIDTH; i++) {
65         *(buffer + i + last_line) = theme_color;
66     }
67     tty_y = tty_y == 0 ? 0 : tty_y - 1;
68 }
69
70 void
71 tty_clear()
72 {
73     for (uint32_t i = 0; i < TTY_WIDTH * TTY_HEIGHT; i++) {
74         *(buffer + i) = theme_color;
75     }
76 }