formated code and fix include error in interrupts.c
[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_y++;
30         case '\r':
31             tty_x = 0;
32             break;
33         default:
34             *(buffer + tty_x + tty_y * TTY_WIDTH) = (theme_color | chr);
35             tty_x++;
36             break;
37     }
38
39     if (tty_x >= TTY_WIDTH) {
40         tty_x = 0;
41         tty_y++;
42     }
43     if (tty_y >= TTY_HEIGHT) {
44         tty_scroll_up();
45     }
46 }
47
48 void
49 tty_put_str(char* str)
50 {
51     while (*str != '\0') {
52         tty_put_char(*str);
53         str++;
54     }
55 }
56
57 void
58 tty_scroll_up()
59 {
60     size_t last_line = TTY_WIDTH * (TTY_HEIGHT - 1);
61     memcpy(buffer, buffer + TTY_WIDTH, last_line);
62     for (size_t i = 0; i < TTY_WIDTH; i++) {
63         *(buffer + i + last_line) = theme_color;
64     }
65     tty_y = tty_y == 0 ? 0 : tty_y - 1;
66 }
67
68 void
69 tty_clear()
70 {
71     for (uint32_t i = 0; i < TTY_WIDTH * TTY_HEIGHT; i++) {
72         *(buffer + i) = theme_color;
73     }
74     tty_x = 0;
75     tty_y = 0;
76 }