v2 initial commit
This commit is contained in:
parent
2c97dba9f7
commit
d2ef885e8d
118 changed files with 2420 additions and 9320 deletions
58
drivers/tty/tty.c
Normal file
58
drivers/tty/tty.c
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <drivers/serial.h>
|
||||
#include <kernel/device.h>
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <drivers/tty.h>
|
||||
|
||||
static mem_cache_t *tty_slab;
|
||||
|
||||
static struct tty_serial_ops arch_serial_ops = { .serial_getchar = arch_serial_read, .serial_putchar = arch_serial_write };
|
||||
|
||||
struct tty *tty_create(const char *name, dev_t serial_device)
|
||||
{
|
||||
struct tty *tty = slab_alloc(tty_slab);
|
||||
|
||||
dev_t tty_device = device_create(tty, DEV_CLASS_TTY, name);
|
||||
|
||||
tty->dev = tty_device;
|
||||
tty->serial_device = serial_device;
|
||||
tty->serial_device_private = device_private_info(serial_device);
|
||||
|
||||
switch (device_class(serial_device)) {
|
||||
case DEV_CLASS_NATIVE_SERIAL:
|
||||
tty->serial_ops = &arch_serial_ops;
|
||||
break;
|
||||
default:
|
||||
kwarn("unknown serial device: %s", name);
|
||||
break;
|
||||
}
|
||||
|
||||
return tty;
|
||||
}
|
||||
|
||||
void tty_putchar(struct tty *tty, char c)
|
||||
{
|
||||
tty->serial_ops->serial_putchar(tty, c);
|
||||
}
|
||||
|
||||
char tty_getchar(struct tty *tty)
|
||||
{
|
||||
return tty->serial_ops->serial_getchar(tty);
|
||||
}
|
||||
|
||||
void tty_write(struct tty *tty, const char *s)
|
||||
{
|
||||
char c;
|
||||
while ((c = *s++))
|
||||
tty_putchar(tty, c);
|
||||
}
|
||||
|
||||
void tty_layer_init()
|
||||
{
|
||||
tty_slab = slab_create(sizeof(struct tty), 256 * KB, 8);
|
||||
|
||||
if (!tty_slab) {
|
||||
panic("tty_slab alloc failed\n");
|
||||
}
|
||||
}
|
||||
87
drivers/tty/vt100.c
Normal file
87
drivers/tty/vt100.c
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/* 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");
|
||||
}
|
||||
Loading…
Reference in a new issue