summaryrefslogtreecommitdiff
path: root/src/init/driver
diff options
context:
space:
mode:
authordzwdz2022-03-31 15:40:48 +0200
committerdzwdz2022-03-31 15:40:48 +0200
commita39baf2d6e6dd759fb8c4ce4bab6779aac586126 (patch)
treef8d9ed72c0f938cd9c487b60f3eed08c232382e0 /src/init/driver
parent6b7cbc2842354eabb7c3345390c5891a6f881f0c (diff)
init: move ansiterm to `driver/`
Diffstat (limited to 'src/init/driver')
-rw-r--r--src/init/driver/ansiterm.c106
-rw-r--r--src/init/driver/driver.h3
2 files changed, 109 insertions, 0 deletions
diff --git a/src/init/driver/ansiterm.c b/src/init/driver/ansiterm.c
new file mode 100644
index 0000000..5b05af1
--- /dev/null
+++ b/src/init/driver/ansiterm.c
@@ -0,0 +1,106 @@
+#include <init/driver/driver.h>
+#include <shared/syscalls.h>
+#include <stdbool.h>
+
+struct vga_cell {
+ unsigned char c;
+ unsigned char style;
+} __attribute__((__packed__));
+static struct vga_cell vga[80 * 25];
+
+static handle_t tty_fd;
+static handle_t vga_fd;
+
+static struct {int x, y;} cursor = {0};
+static bool dirty = false;
+static bool pendingFlush = false;
+
+/* TODO unflushed writes mix with the kernel's vga driver */
+
+static void flush(void) {
+ _syscall_write(vga_fd, vga, sizeof vga, 0);
+ dirty = false;
+ pendingFlush = false;
+}
+
+static void scroll(void) {
+ for (size_t i = 0; i < 80 * 24; i++)
+ vga[i] = vga[i + 80];
+ for (size_t i = 80 * 24; i < 80 * 25; i++)
+ vga[i].c = ' ';
+ cursor.y--;
+ pendingFlush = true;
+}
+
+static void in_char(char c) {
+ switch (c) {
+ case '\n':
+ cursor.x = 0;
+ cursor.y++;
+ pendingFlush = true;
+ break;
+ case '\b':
+ if (--cursor.x < 0) cursor.x = 0;
+ break;
+ default:
+ vga[cursor.y * 80 + cursor.x++].c = c;
+ }
+
+ if (cursor.x >= 80) {
+ cursor.x = 0;
+ cursor.y++;
+ }
+ while (cursor.y >= 25) scroll();
+ dirty = true;
+}
+
+void ansiterm_drv(void) {
+ tty_fd = _syscall_open("/tty", 4);
+ _syscall_write(tty_fd, "ansiterm...\n", 12, 0);
+
+ vga_fd = _syscall_open("/vga", 4);
+ _syscall_read(vga_fd, vga, sizeof vga, 0);
+
+ // find first empty line
+ for (cursor.y = 0; cursor.y < 25; cursor.y++) {
+ for (cursor.x = 0; cursor.x < 80; cursor.x++) {
+ char c = vga[cursor.y * 80 + cursor.x].c;
+ if (c != ' ' && c != '\0') break;
+ }
+ if (cursor.x == 80) break;
+ }
+ cursor.x = 0;
+
+ static char buf[512];
+ struct fs_wait_response res;
+ while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
+ switch (res.op) {
+ case VFSOP_OPEN:
+ // TODO check path
+ _syscall_fs_respond(NULL, 1);
+ break;
+
+ case VFSOP_WRITE:
+ _syscall_write(tty_fd, buf, res.len, 0);
+ for (int i = 0; i < res.len; i++)
+ in_char(buf[i]);
+ if (pendingFlush) flush();
+ _syscall_fs_respond(NULL, res.len);
+ break;
+
+ case VFSOP_READ:
+ if (res.capacity > sizeof buf)
+ res.capacity = sizeof buf;
+ if (dirty) flush();
+ size_t len = _syscall_read(tty_fd, buf, res.capacity, 0);
+ _syscall_fs_respond(buf, len);
+ break;
+
+ default:
+ _syscall_fs_respond(NULL, -1);
+ break;
+ }
+ }
+
+ _syscall_exit(1);
+}
diff --git a/src/init/driver/driver.h b/src/init/driver/driver.h
new file mode 100644
index 0000000..e854c65
--- /dev/null
+++ b/src/init/driver/driver.h
@@ -0,0 +1,3 @@
+#pragma once
+
+void ansiterm_drv(void);