Merge branch 'signal-dev'
[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/tty/tty.h>
5 #include <stdint.h>
6
7 #define TTY_WIDTH 80
8 #define TTY_HEIGHT 25
9
10 static vga_attribute* tty_vga_buffer = (vga_attribute*)VGA_BUFFER_PADDR;
11
12 static vga_attribute tty_theme_color = VGA_COLOR_BLACK;
13
14 static uint32_t tty_x = 0;
15 static uint32_t tty_y = 0;
16
17 void
18 tty_init(void* vga_buf)
19 {
20     tty_vga_buffer = (vga_attribute*)vga_buf;
21     tty_clear();
22
23     io_outb(0x3D4, 0x0A);
24     io_outb(0x3D5, (io_inb(0x3D5) & 0xC0) | 13);
25
26     io_outb(0x3D4, 0x0B);
27     io_outb(0x3D5, (io_inb(0x3D5) & 0xE0) | 15);
28 }
29
30 void
31 tty_set_buffer(void* vga_buf)
32 {
33     tty_vga_buffer = (vga_attribute*)vga_buf;
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_put_char(char chr)
44 {
45     switch (chr) {
46         case '\t':
47             tty_x += 4;
48             break;
49         case '\n':
50             tty_y++;
51             // fall through
52         case '\r':
53             tty_x = 0;
54             break;
55         case '\x08':
56             tty_x = tty_x ? tty_x - 1 : 0;
57             *(tty_vga_buffer + tty_x + tty_y * TTY_WIDTH) =
58               (tty_theme_color | 0x20);
59             break;
60         default:
61             *(tty_vga_buffer + tty_x + tty_y * TTY_WIDTH) =
62               (tty_theme_color | chr);
63             tty_x++;
64             break;
65     }
66
67     if (tty_x >= TTY_WIDTH) {
68         tty_x = 0;
69         tty_y++;
70     }
71     if (tty_y >= TTY_HEIGHT) {
72         tty_scroll_up();
73     }
74 }
75
76 void
77 tty_sync_cursor()
78 {
79     tty_set_cursor(tty_x, tty_y);
80 }
81
82 void
83 tty_set_cursor(uint8_t x, uint8_t y)
84 {
85     if (x >= TTY_WIDTH || y >= TTY_HEIGHT) {
86         x = y = 0;
87     }
88     uint32_t pos = y * TTY_WIDTH + x;
89     io_outb(0x3D4, 14);
90     io_outb(0x3D5, pos / 256);
91     io_outb(0x3D4, 15);
92     io_outb(0x3D5, pos % 256);
93 }
94
95 void
96 tty_put_str(char* str)
97 {
98     while (*str != '\0') {
99         tty_put_char(*str);
100         str++;
101     }
102     // FIXME: This does not work in user mode.
103     // Work around:
104     //  1. (Easy) Define an IO Permission bitmap in TSS
105     //  2. (More effort) Mount onto file system. (/dev/tty)
106     // tty_sync_cursor();
107 }
108
109 void
110 tty_scroll_up()
111 {
112     size_t last_line = TTY_WIDTH * (TTY_HEIGHT - 1);
113     memcpy(tty_vga_buffer, tty_vga_buffer + TTY_WIDTH, last_line * 2);
114     for (size_t i = 0; i < TTY_WIDTH; i++) {
115         *(tty_vga_buffer + i + last_line) = tty_theme_color;
116     }
117     tty_y = tty_y == 0 ? 0 : TTY_HEIGHT - 1;
118 }
119
120 void
121 tty_clear()
122 {
123     for (uint32_t i = 0; i < TTY_WIDTH * TTY_HEIGHT; i++) {
124         *(tty_vga_buffer + i) = tty_theme_color;
125     }
126     tty_x = 0;
127     tty_y = 0;
128 }
129
130 void
131 tty_clear_line(unsigned int y)
132 {
133     for (size_t i = 0; i < TTY_WIDTH; i++) {
134         *(tty_vga_buffer + i + y * TTY_WIDTH) = tty_theme_color;
135     }
136 }
137
138 void
139 tty_set_cpos(unsigned int x, unsigned int y)
140 {
141     tty_x = x % TTY_WIDTH;
142     tty_y = y % TTY_HEIGHT;
143 }
144
145 void
146 tty_get_cpos(unsigned int* x, unsigned int* y)
147 {
148     *x = tty_x;
149     *y = tty_y;
150 }
151
152 vga_attribute
153 tty_get_theme()
154 {
155     return tty_theme_color;
156 }