Implement (half) simple semaphore & mutex lock, 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 #include <hal/io.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 tty_init(void* vga_buf) {
18     tty_vga_buffer = (vga_attribute*)vga_buf;
19     tty_clear();
20
21     io_outb(0x3D4, 0x0A);
22         io_outb(0x3D5, (io_inb(0x3D5) & 0xC0) | 13);
23  
24         io_outb(0x3D4, 0x0B);
25         io_outb(0x3D5, (io_inb(0x3D5) & 0xE0) | 15);
26 }
27
28 void tty_set_buffer(void* vga_buf) {
29     tty_vga_buffer = (vga_attribute*)vga_buf;
30 }
31
32 void
33 tty_set_theme(vga_attribute fg, vga_attribute bg)
34 {
35     tty_theme_color = (bg << 4 | fg) << 8;
36 }
37
38 void
39 tty_put_char(char chr)
40 {
41     switch (chr) {
42         case '\t':
43             tty_x += 4;
44             break;
45         case '\n':
46             tty_y++;
47             // fall through
48         case '\r':
49             tty_x = 0;
50             break;
51         case '\x08':
52             tty_x = tty_x ? tty_x - 1 : 0;
53             *(tty_vga_buffer + tty_x + tty_y * TTY_WIDTH) = (tty_theme_color | 0x20);
54             break;
55         default:
56             *(tty_vga_buffer + tty_x + tty_y * TTY_WIDTH) = (tty_theme_color | chr);
57             tty_x++;
58             break;
59     }
60
61     if (tty_x >= TTY_WIDTH) {
62         tty_x = 0;
63         tty_y++;
64     }
65     if (tty_y >= TTY_HEIGHT) {
66         tty_scroll_up();
67     }
68 }
69
70 void tty_sync_cursor() {
71     tty_set_cursor(tty_x, tty_y);
72 }
73
74
75 void tty_set_cursor(uint8_t x, uint8_t y) {
76     if (x >= TTY_WIDTH || y >= TTY_HEIGHT) {
77         x = y = 0;
78     }
79     uint32_t pos = y * TTY_WIDTH + x;
80     io_outb(0x3D4, 14);
81     io_outb(0x3D5, pos / 256);
82     io_outb(0x3D4, 15);
83     io_outb(0x3D5, pos % 256);
84 }
85
86 void
87 tty_put_str(char* str)
88 {
89     while (*str != '\0') {
90         tty_put_char(*str);
91         str++;
92     }
93     tty_sync_cursor();
94 }
95
96 void
97 tty_scroll_up()
98 {
99     size_t last_line = TTY_WIDTH * (TTY_HEIGHT - 1);
100     memcpy(tty_vga_buffer, tty_vga_buffer + TTY_WIDTH, last_line * 2);
101     for (size_t i = 0; i < TTY_WIDTH; i++) {
102         *(tty_vga_buffer + i + last_line) = tty_theme_color;
103     }
104     tty_y = tty_y == 0 ? 0 : TTY_HEIGHT - 1;
105 }
106
107 void
108 tty_clear()
109 {
110     for (uint32_t i = 0; i < TTY_WIDTH * TTY_HEIGHT; i++) {
111         *(tty_vga_buffer + i) = tty_theme_color;
112     }
113     tty_x = 0;
114     tty_y = 0;
115 }
116
117 void
118 tty_clear_line(unsigned int y) {
119     for (size_t i = 0; i < TTY_WIDTH; i++)
120     {
121         *(tty_vga_buffer + i + y * TTY_WIDTH) = tty_theme_color;
122     }
123 }
124
125 void
126 tty_set_cpos(unsigned int x, unsigned int y) {
127     tty_x = x % TTY_WIDTH;
128     tty_y = y % TTY_HEIGHT;
129 }
130
131 void
132 tty_get_cpos(unsigned int* x, unsigned int* y) {
133     *x = tty_x;
134     *y = tty_y;
135 }
136
137 vga_attribute
138 tty_get_theme() {
139     return tty_theme_color;
140 }