kernel: add tty
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
fa34746870
commit
f46281b305
4 changed files with 176 additions and 0 deletions
87
kernel/tty/vt100.c
Normal file
87
kernel/tty/vt100.c
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/tty.h>
|
||||
#include <lib/stdio.h>
|
||||
#include <lib/string.h>
|
||||
|
||||
void tty_send_dsr(struct tty *tty)
|
||||
{
|
||||
tty_write(tty, "\033[6n");
|
||||
}
|
||||
|
||||
void tty_set_origin_mode(struct tty *tty)
|
||||
{
|
||||
tty_write(tty, "\033[?6h");
|
||||
}
|
||||
|
||||
void tty_clear_screen(struct tty *tty)
|
||||
{
|
||||
tty_write(tty, "\033[2J");
|
||||
}
|
||||
|
||||
void tty_set_cursor_pos(struct tty *tty, int r, int c)
|
||||
{
|
||||
if (r > 999)
|
||||
r = 999;
|
||||
if (c > 999)
|
||||
c = 999;
|
||||
|
||||
char str[12] = { 0 };
|
||||
snprintf(str, 11, "\033[%d;%dH", r, c);
|
||||
tty_write(tty, str);
|
||||
}
|
||||
|
||||
void tty_get_cursor_pos(struct tty *tty, int *row, int *col)
|
||||
{
|
||||
tty_send_dsr(tty);
|
||||
|
||||
char c;
|
||||
int n = 0;
|
||||
|
||||
char ro[128] = { 0 };
|
||||
char co[128] = { 0 };
|
||||
|
||||
bool sem = false;
|
||||
bool begin = false;
|
||||
|
||||
while ((c = tty_getchar(tty))) {
|
||||
if (c == '\033')
|
||||
continue;
|
||||
|
||||
if (c == ';') {
|
||||
sem = true;
|
||||
n = 0;
|
||||
}
|
||||
|
||||
if (!begin) {
|
||||
if (c == '[')
|
||||
begin = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c >= '0' && c <= '9') {
|
||||
if (!sem)
|
||||
ro[n] = c;
|
||||
else
|
||||
co[n] = c;
|
||||
|
||||
n++;
|
||||
}
|
||||
|
||||
if (c == 'R')
|
||||
break;
|
||||
}
|
||||
|
||||
*row = atoi(ro);
|
||||
*col = atoi(co);
|
||||
}
|
||||
|
||||
void tty_get_size(struct tty *tty, int *rows, int *cols)
|
||||
{
|
||||
tty_set_cursor_pos(tty, 999, 999);
|
||||
tty_get_cursor_pos(tty, rows, cols);
|
||||
}
|
||||
|
||||
void tty_setlf(struct tty *tty)
|
||||
{
|
||||
tty_write(tty, "\033[20l");
|
||||
}
|
||||
Loading…
Reference in a new issue