Implement shift+<key> support, and ...
[lunaix-os.git] / lunaix-os / kernel / tty / tty.c
1 #include <klibc/string.h>
2 #include <lunaix/tty/tty.h>
3 #include <lunaix/common.h>
4 #include <stdint.h>
5
6 #define TTY_WIDTH 80
7 #define TTY_HEIGHT 25
8
9 static vga_attribute* tty_vga_buffer = (vga_attribute*)VGA_BUFFER_PADDR;
10
11 static vga_attribute tty_theme_color = VGA_COLOR_BLACK;
12
13 static uint32_t tty_x = 0;
14 static uint16_t tty_y = 0;
15
16 void tty_init(void* vga_buf) {
17     tty_vga_buffer = (vga_attribute*)vga_buf;
18     tty_clear();
19 }
20
21 void tty_set_buffer(void* vga_buf) {
22     tty_vga_buffer = (vga_attribute*)vga_buf;
23 }
24
25 void
26 tty_set_theme(vga_attribute fg, vga_attribute bg)
27 {
28     tty_theme_color = (bg << 4 | fg) << 8;
29 }
30
31 void
32 tty_put_char(char chr)
33 {
34     switch (chr) {
35         case '\t':
36             tty_x += 4;
37             break;
38         case '\n':
39             tty_y++;
40             // fall through
41         case '\r':
42             tty_x = 0;
43             break;
44         case '\x08':
45             tty_x = tty_x ? tty_x - 1 : 0;
46             *(tty_vga_buffer + tty_x + tty_y * TTY_WIDTH) = (tty_theme_color | 0x20);
47             break;
48         default:
49             *(tty_vga_buffer + tty_x + tty_y * TTY_WIDTH) = (tty_theme_color | chr);
50             tty_x++;
51             break;
52     }
53
54     if (tty_x >= TTY_WIDTH) {
55         tty_x = 0;
56         tty_y++;
57     }
58     if (tty_y >= TTY_HEIGHT) {
59         tty_scroll_up();
60     }
61 }
62
63 void
64 tty_put_str(char* str)
65 {
66     while (*str != '\0') {
67         tty_put_char(*str);
68         str++;
69     }
70 }
71
72 void
73 tty_scroll_up()
74 {
75     size_t last_line = TTY_WIDTH * (TTY_HEIGHT - 1);
76     memcpy(tty_vga_buffer, tty_vga_buffer + TTY_WIDTH, last_line * 2);
77     for (size_t i = 0; i < TTY_WIDTH; i++) {
78         *(tty_vga_buffer + i + last_line) = tty_theme_color;
79     }
80     tty_y = tty_y == 0 ? 0 : TTY_HEIGHT - 1;
81 }
82
83 void
84 tty_clear()
85 {
86     for (uint32_t i = 0; i < TTY_WIDTH * TTY_HEIGHT; i++) {
87         *(tty_vga_buffer + i) = tty_theme_color;
88     }
89     tty_x = 0;
90     tty_y = 0;
91 }
92
93 void
94 tty_clear_line(unsigned int y) {
95     for (size_t i = 0; i < TTY_WIDTH; i++)
96     {
97         *(tty_vga_buffer + i + y * TTY_WIDTH) = tty_theme_color;
98     }
99 }
100
101 void
102 tty_set_cpos(unsigned int x, unsigned int y) {
103     tty_x = x % TTY_WIDTH;
104     tty_y = y % TTY_HEIGHT;
105 }
106
107 void
108 tty_get_cpos(unsigned int* x, unsigned int* y) {
109     *x = tty_x;
110     *y = tty_y;
111 }
112
113 vga_attribute
114 tty_get_theme() {
115     return tty_theme_color;
116 }