2 #include <klibc/string.h>
3 #include <lunaix/common.h>
4 #include <lunaix/tty/tty.h>
10 static vga_attribute* tty_vga_buffer = (vga_attribute*)VGA_BUFFER_PADDR;
12 static vga_attribute tty_theme_color = VGA_COLOR_BLACK;
14 static uint32_t tty_x = 0;
15 static uint32_t tty_y = 0;
18 tty_init(void* vga_buf)
20 tty_vga_buffer = (vga_attribute*)vga_buf;
24 io_outb(0x3D5, (io_inb(0x3D5) & 0xC0) | 13);
27 io_outb(0x3D5, (io_inb(0x3D5) & 0xE0) | 15);
31 tty_set_buffer(void* vga_buf)
33 tty_vga_buffer = (vga_attribute*)vga_buf;
37 tty_set_theme(vga_attribute fg, vga_attribute bg)
39 tty_theme_color = (bg << 4 | fg) << 8;
43 tty_put_char(char chr)
56 tty_x = tty_x ? tty_x - 1 : 0;
57 *(tty_vga_buffer + tty_x + tty_y * TTY_WIDTH) =
58 (tty_theme_color | 0x20);
61 *(tty_vga_buffer + tty_x + tty_y * TTY_WIDTH) =
62 (tty_theme_color | chr);
67 if (tty_x >= TTY_WIDTH) {
71 if (tty_y >= TTY_HEIGHT) {
79 tty_set_cursor(tty_x, tty_y);
83 tty_set_cursor(uint8_t x, uint8_t y)
85 if (x >= TTY_WIDTH || y >= TTY_HEIGHT) {
88 uint32_t pos = y * TTY_WIDTH + x;
90 io_outb(0x3D5, pos / 256);
92 io_outb(0x3D5, pos % 256);
96 tty_put_str(char* str)
98 while (*str != '\0') {
102 // FIXME: This does not work in user mode.
104 // 1. (Easy) Define an IO Permission bitmap in TSS
105 // 2. (More effort) Mount onto file system. (/dev/tty)
106 // tty_sync_cursor();
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;
117 tty_y = tty_y == 0 ? 0 : TTY_HEIGHT - 1;
123 for (uint32_t i = 0; i < TTY_WIDTH * TTY_HEIGHT; i++) {
124 *(tty_vga_buffer + i) = tty_theme_color;
131 tty_clear_line(unsigned int y)
133 for (size_t i = 0; i < TTY_WIDTH; i++) {
134 *(tty_vga_buffer + i + y * TTY_WIDTH) = tty_theme_color;
139 tty_set_cpos(unsigned int x, unsigned int y)
141 tty_x = x % TTY_WIDTH;
142 tty_y = y % TTY_HEIGHT;
146 tty_get_cpos(unsigned int* x, unsigned int* y)
155 return tty_theme_color;