6db2417a7d81c5524e2c66f0c3460c3aeed4ab11
[lunaix-os.git] / lunaix-os / kernel / tty / tty.c
1 #include <hal/io.h>
2 #include <klibc/string.h>
3 #include <lunaix/common.h>
4 #include <lunaix/spike.h>
5 #include <lunaix/tty/console.h>
6 #include <lunaix/tty/tty.h>
7 #include <stdint.h>
8
9 vga_attribute* tty_vga_buffer = (vga_attribute*)VGA_BUFFER_PADDR;
10
11 vga_attribute tty_theme_color = VGA_COLOR_BLACK;
12
13 inline void
14 tty_clear()
15 {
16     asm volatile("rep stosw" ::"D"(tty_vga_buffer),
17                  "c"(TTY_HEIGHT * TTY_WIDTH),
18                  "a"(tty_theme_color)
19                  : "memory");
20 }
21
22 void
23 tty_init(void* vga_buf)
24 {
25     tty_vga_buffer = (vga_attribute*)vga_buf;
26
27     tty_clear();
28
29     io_outb(0x3D4, 0x0A);
30     io_outb(0x3D5, (io_inb(0x3D5) & 0xC0) | 13);
31
32     io_outb(0x3D4, 0x0B);
33     io_outb(0x3D5, (io_inb(0x3D5) & 0xE0) | 15);
34 }
35
36 void
37 tty_set_theme(vga_attribute fg, vga_attribute bg)
38 {
39     tty_theme_color = (bg << 4 | fg) << 8;
40 }
41
42 void
43 tty_flush_buffer(struct fifo_buf* buf)
44 {
45     int x = 0, y = 0;
46
47     // Clear screen
48     tty_clear();
49
50     char chr;
51     int state = 0;
52     int g[2] = { 0, 0 };
53     vga_attribute current_theme = tty_theme_color;
54     while (fifo_readone_async(buf, &chr)) {
55         if (state == 0 && chr == '\033') {
56             state = 1;
57         } else if (state == 1 && chr == '[') {
58             state = 2;
59         } else if (state > 1) {
60             if ('0' <= chr && chr <= '9') {
61                 g[state - 2] = (chr - '0') + g[state - 2] * 10;
62             } else if (chr == ';' && state == 2) {
63                 state = 3;
64             } else {
65                 if (g[0] == 39 && g[1] == 49) {
66                     current_theme = tty_theme_color;
67                 } else {
68                     current_theme = (g[1] << 4 | g[0]) << 8;
69                 }
70                 g[0] = 0;
71                 g[1] = 0;
72                 state = 0;
73             }
74         } else {
75             state = 0;
76             switch (chr) {
77                 case '\t':
78                     x += 4;
79                     break;
80                 case '\n':
81                     y++;
82                     // fall through
83                 case '\r':
84                     x = 0;
85                     break;
86                 case '\x08':
87                     x = x ? x - 1 : 0;
88                     *(tty_vga_buffer + x + y * TTY_WIDTH) =
89                       (current_theme | 0x20);
90                     break;
91                 default:
92                     *(tty_vga_buffer + x + y * TTY_WIDTH) =
93                       (current_theme | chr);
94                     (x)++;
95                     break;
96             }
97
98             if (x >= TTY_WIDTH) {
99                 x = 0;
100                 y++;
101             }
102             if (y >= TTY_HEIGHT) {
103                 y--;
104                 break;
105             }
106         }
107     }
108     tty_set_cursor(x, y);
109 }
110
111 void
112 tty_set_cursor(uint8_t x, uint8_t y)
113 {
114     if (x >= TTY_WIDTH || y >= TTY_HEIGHT) {
115         x = y = 0;
116     }
117     uint32_t pos = y * TTY_WIDTH + x;
118     io_outb(0x3D4, 14);
119     io_outb(0x3D5, pos / 256);
120     io_outb(0x3D4, 15);
121     io_outb(0x3D5, pos % 256);
122 }
123
124 void
125 tty_clear_line(int line_num)
126 {
127     asm volatile("rep stosw" ::"D"(tty_vga_buffer + line_num * TTY_WIDTH),
128                  "c"(TTY_WIDTH),
129                  "a"(tty_theme_color)
130                  : "memory");
131 }
132
133 void
134 tty_put_str_at(char* str, int x, int y)
135 {
136     char c;
137     while ((c = (*str)) && y < TTY_HEIGHT) {
138         *(tty_vga_buffer + x + y * TTY_WIDTH) = c | tty_theme_color;
139         x++;
140         if (x >= TTY_WIDTH) {
141             y++;
142             x = 0;
143         }
144         str++;
145     }
146 }