vbos/drivers/tty/vt100.c
2026-04-11 11:35:31 -04:00

87 lines
1.2 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <drivers/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");
}