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