diff --git a/kernel/include/fs/devices/Console.h b/kernel/include/fs/devices/Console.h index d7ce5d96..aef42b14 100644 --- a/kernel/include/fs/devices/Console.h +++ b/kernel/include/fs/devices/Console.h @@ -6,4 +6,10 @@ namespace ConsoleDevice VFS::Node* create_new(const char* devname); ssize_t write(VFS::Node* node, size_t offset, size_t size, const char* buffer); + + ssize_t read(VFS::Node* node, size_t offset, size_t size, char* buffer); + + int would_block(VFS::Node* node); + + void append(char c); } \ No newline at end of file diff --git a/kernel/src/fs/devices/Console.cpp b/kernel/src/fs/devices/Console.cpp index 8ba7f978..2eaeafc2 100644 --- a/kernel/src/fs/devices/Console.cpp +++ b/kernel/src/fs/devices/Console.cpp @@ -5,20 +5,30 @@ #include "std/stdlib.h" #include "std/string.h" -extern uint64_t clock_boot(); +char* conin_buffer = nullptr; +uint64_t conin_bufsize = 0; + +int ConsoleDevice::would_block(VFS::Node*) +{ + return conin_bufsize == 0; +} + +extern uint64_t clock_now(); VFS::Node* ConsoleDevice::create_new(const char* devname) { VFS::Node* dev = new VFS::Node; dev->write_func = ConsoleDevice::write; + dev->read_func = ConsoleDevice::read; + dev->block_func = ConsoleDevice::would_block; dev->inode = 0; dev->length = 0; dev->type = VFS_DEVICE; dev->flags = 0; dev->tty = 1; dev->uid = dev->gid = 0; - dev->mode = 0222; - dev->atime = dev->ctime = dev->mtime = clock_boot(); + dev->mode = 0666; + dev->atime = dev->ctime = dev->mtime = clock_now(); strncpy(dev->name, devname, sizeof(dev->name)); return dev; } @@ -28,4 +38,24 @@ ssize_t ConsoleDevice::write(VFS::Node* node, size_t, size_t size, const char* b if (!node) return -1; TextRenderer::write(buffer, size); return (ssize_t)size; +} + +ssize_t ConsoleDevice::read(VFS::Node* node, size_t, size_t size, char* buffer) +{ + if (!node) return -1; + if (!conin_buffer) return 0; + if (size > conin_bufsize) size = conin_bufsize; + memcpy(buffer, conin_buffer, size); + memmove(conin_buffer, conin_buffer + size, conin_bufsize - size); + conin_bufsize -= size; + conin_buffer = (char*)krealloc(conin_buffer, conin_bufsize); + return (ssize_t)size; +} + +void ConsoleDevice::append(char c) +{ + conin_bufsize++; + conin_buffer = (char*)krealloc( + conin_buffer, conin_bufsize); // FIXME: We should probably not be calling realloc every time a key is pressed. + conin_buffer[conin_bufsize - 1] = c; } \ No newline at end of file diff --git a/kernel/src/interrupts/IRQ.cpp b/kernel/src/interrupts/IRQ.cpp index 22142735..438fa83f 100644 --- a/kernel/src/interrupts/IRQ.cpp +++ b/kernel/src/interrupts/IRQ.cpp @@ -2,6 +2,7 @@ #include "interrupts/IRQ.h" #include "fs/devices/Keyboard.h" +#include "fs/devices/Console.h" #include "io/IO.h" #include "io/PIC.h" #include "log/Log.h" @@ -21,10 +22,11 @@ void IRQ::interrupt_handler(Context* context) break; case 1: { unsigned char scancode = IO::inb(0x60); + KeyboardDevice::append((char)scancode); bool ignore = false; char key = translate_scancode(scancode, &ignore); if (ignore) break; - KeyboardDevice::append(key); + ConsoleDevice::append(key); break; } default: kwarnln("Unhandled IRQ: %ld", context->irq_number); break; diff --git a/libs/libc/src/init.cpp b/libs/libc/src/init.cpp index 5c740b7b..a54dc248 100644 --- a/libs/libc/src/init.cpp +++ b/libs/libc/src/init.cpp @@ -58,7 +58,7 @@ extern char* program_invocation_name; extern "C" void initialize_libc(int, char** argv) { - check_for_file(STDIN_FILENO, &stdin, "/dev/kbd", "r"); + check_for_file(STDIN_FILENO, &stdin, "/dev/console", "r"); check_for_file(STDOUT_FILENO, &stdout, "/dev/console", "a"); check_for_file(STDERR_FILENO, &stderr, "/dev/console", "a");