tty: tty abstraction initial implementation

This commit is contained in:
Matheus
2024-04-27 13:42:57 -03:00
parent d8e3ce178e
commit 1fc3791bc9
5 changed files with 388 additions and 192 deletions
+19 -191
View File
@@ -8,15 +8,11 @@
#include <kernel/keyboard.h>
#include <termios.h>
#include <kernel/poll.h>
#include <kernel/tty.h>
#define BUFFER_SIZE 4096
static mutex_t writemutex;
static mutex_t readmutex;
static semaphore_t readsemaphore;
static ringbuffer_t readbuffer;
static thread_t *thread;
static volatile termios_t termios;
static pollheader_t pollheader;
static tty_t *tty;
void console_write(char *str, size_t count) {
MUTEX_ACQUIRE(&writemutex, false);
@@ -30,8 +26,6 @@ void console_putc(char c) {
console_write(&c, 1);
}
#define THREAD_BUFFER_SIZE 128
// taken from https://en.wikipedia.org/wiki/ANSI_escape_code#Terminal_input_sequences
#define HOME_STR "\e[1~"
@@ -47,34 +41,25 @@ void console_putc(char c) {
#define LEFT_STR "\e[D"
static void consolethread() {
char buffer[THREAD_BUFFER_SIZE + 1];
uintmax_t buffpos = 0;
for (;;) {
kbpacket_t packet;
keyboard_wait(keyboard_console, &packet);
if (packet.flags & KBPACKET_FLAGS_RELEASED)
continue;
char asciibuffer[2] = {0, 0};
char *target = asciibuffer;
if (packet.ascii) {
if (packet.ascii == '\r' && (termios.c_iflag & IGNCR))
continue;
if (packet.ascii == '\r' && (termios.c_iflag & ICRNL))
packet.ascii = '\n';
else if (packet.ascii == '\n' && (termios.c_iflag & INLCR))
packet.ascii = '\r';
// control characters
if (packet.flags & (KBPACKET_FLAGS_LEFTCTRL | KBPACKET_FLAGS_RIGHTCTRL)) {
if (!((packet.ascii > 'a' && packet.ascii < 'z') || (packet.ascii > 'A' && packet.ascii < 'Z')))
continue;
packet.ascii = packet.ascii > 'a' ? packet.ascii - 0x60 : packet.ascii - 0x40;
}
tty_process(tty, packet.ascii);
} else {
// no ascii for key but we can send in a escape sequence for it
char *target = NULL;
switch (packet.keycode) {
case KEYCODE_HOME:
target = HOME_STR;
@@ -109,185 +94,28 @@ static void consolethread() {
default:
continue;
}
// TODO SWITCH OF SPECIAL KEYS
}
asciibuffer[0] = packet.ascii;
size_t targetlen = strlen(target);
if (targetlen == 0)
continue;
if (termios.c_lflag & ICANON) {
bool flush = false;
if (packet.ascii == '\b') {
if (buffpos == 0)
continue;
--buffpos;
buffer[buffpos] = '\0';
if (termios.c_lflag & ECHO)
console_write("\b \b", 3);
continue;
} else if (packet.ascii == '\n') {
flush = true;
}
if (termios.c_lflag & ECHO)
console_write(target, targetlen);
for (size_t i = 0; i < targetlen; ++i) {
buffer[buffpos++] = target[i];
if (buffpos == THREAD_BUFFER_SIZE) {
flush = true;
break;
}
}
if (flush) {
MUTEX_ACQUIRE(&readmutex, false);
ringbuffer_write(&readbuffer, buffer, buffpos);
buffpos = 0;
size_t datacount = RINGBUFFER_DATACOUNT(&readbuffer);
if (datacount) {
if (datacount >= termios.c_cc[VMIN])
poll_event(&pollheader, POLLIN);
semaphore_signal(&readsemaphore);
}
MUTEX_RELEASE(&readmutex);
}
} else {
if (termios.c_lflag & ECHO)
console_write(target, targetlen);
MUTEX_ACQUIRE(&readmutex, false);
ringbuffer_write(&readbuffer, target, targetlen);
size_t datacount = RINGBUFFER_DATACOUNT(&readbuffer);
if (datacount) {
if (datacount >= termios.c_cc[VMIN])
poll_event(&pollheader, POLLIN);
semaphore_signal(&readsemaphore);
}
MUTEX_RELEASE(&readmutex);
size_t len = strlen(target);
for (int i = 0; i < len; ++i)
tty_process(tty, target[i]);
}
}
}
static int read(int minor, void *buffer, size_t size, uintmax_t offset, int flags, size_t *readc) {
size_t total = 0;
for (;;) {
if (semaphore_test(&readsemaphore) == false) {
*readc = 0;
if (termios.c_cc[VMIN] == 0)
return 0;
if (flags & V_FFLAGS_NONBLOCKING)
return EAGAIN;
int i = semaphore_wait(&readsemaphore, true);
if (i)
return i;
}
MUTEX_ACQUIRE(&readmutex, false);
size_t read = ringbuffer_read(&readbuffer, buffer, size);
size_t left = readbuffer.write - readbuffer.read;
MUTEX_RELEASE(&readmutex);
size -= read;
buffer = (void *)((uintptr_t)buffer + read);
total += read;
if (size == 0 || total >= termios.c_cc[VMIN]) {
if (left > 0)
semaphore_signal(&readsemaphore);
*readc = total;
return 0;
}
}
}
static int write(int minor, void *buffer, size_t size, uintmax_t offset, int flags, size_t *writec) {
*writec = size;
console_write(buffer, size);
return 0;
}
static int poll(int minor, polldata_t *data, int events) {
int revents = 0;
if (events & POLLOUT)
events |= POLLOUT;
MUTEX_ACQUIRE(&readmutex, false);
size_t datacount = RINGBUFFER_DATACOUNT(&readbuffer);
if ((events & POLLIN) && datacount >= termios.c_cc[VMIN])
revents |= POLLIN;
MUTEX_RELEASE(&readmutex);
if (revents == 0 && data)
poll_add(&pollheader, data, events);
return revents;
}
static int ioctl(int minor, unsigned long req, void *arg, int *result) {
switch (req) {
case TIOCGWINSZ:
size_t x, y, fbx, fby;
term_getsize(&x, &y, &fbx, &fby);
winsize_t *w = arg;
w->ws_col = x;
w->ws_row = y;
w->ws_xpixel = fbx;
w->ws_ypixel = fby;
break;
case TCGETS:
*(termios_t *)arg = termios;
break;
case TCSETS:
termios = *(termios_t *)arg;
break;
default:
return ENOTTY;
}
return 0;
}
static int isatty(int minor) {
return 0;
}
static devops_t devops = {
.write = write,
.read = read,
.isatty = isatty,
.poll = poll,
.ioctl = ioctl
};
void console_init() {
SEMAPHORE_INIT(&readsemaphore, 0);
MUTEX_INIT(&writemutex);
MUTEX_INIT(&readmutex);
__assert(ringbuffer_init(&readbuffer, BUFFER_SIZE) == 0);
__assert(devfs_register(&devops, "console", V_TYPE_CHDEV, DEV_MAJOR_CONSOLE, 0, 0644) == 0);
thread = sched_newthread(consolethread, PAGE_SIZE * 16, 0, NULL, NULL);
__assert(thread);
sched_queue(thread);
termios.c_iflag = ICRNL;
termios.c_lflag = ECHO | ICANON | ISIG;
termios.c_cc[VINTR] = 0x03;
termios.ibaud = 38400;
termios.obaud = 38400;
termios.c_cc[VMIN] = 1;
tty = tty_create("console", console_write);
__assert(tty);
POLL_INITHEADER(&pollheader);
size_t x, y, fbx, fby;
term_getsize(&x, &y, &fbx, &fby);
tty->winsize.ws_col = x;
tty->winsize.ws_row = y;
tty->winsize.ws_xpixel = fbx;
tty->winsize.ws_ypixel = fby;
}