From d59463d11b7355ad1ea86ee443e4dbc8611349a8 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 22 Sep 2024 17:13:10 +0300 Subject: [PATCH] Kernel: Fix TTY reading one keyevent after disabling input handling --- kernel/include/kernel/FS/Inode.h | 1 + kernel/include/kernel/Terminal/TTY.h | 1 + kernel/kernel/Terminal/TTY.cpp | 61 +++++++++++++++++----------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/kernel/include/kernel/FS/Inode.h b/kernel/include/kernel/FS/Inode.h index fdc6840cc7..ae2c906331 100644 --- a/kernel/include/kernel/FS/Inode.h +++ b/kernel/include/kernel/FS/Inode.h @@ -160,6 +160,7 @@ namespace Kernel BAN::WeakPtr m_shared_region; friend class FileBackedRegion; friend class SharedFileData; + friend class TTY; }; } diff --git a/kernel/include/kernel/Terminal/TTY.h b/kernel/include/kernel/Terminal/TTY.h index d9bf403773..102a44b246 100644 --- a/kernel/include/kernel/Terminal/TTY.h +++ b/kernel/include/kernel/Terminal/TTY.h @@ -28,6 +28,7 @@ namespace Kernel static BAN::RefPtr current(); void set_as_current(); + static void keyboard_task(void*); static void initialize_devices(); void on_key_event(LibInput::KeyEvent); void handle_input_byte(uint8_t); diff --git a/kernel/kernel/Terminal/TTY.cpp b/kernel/kernel/Terminal/TTY.cpp index b6024ef266..f2d339da96 100644 --- a/kernel/kernel/Terminal/TTY.cpp +++ b/kernel/kernel/Terminal/TTY.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -95,35 +96,47 @@ namespace Kernel return {}; } + void TTY::keyboard_task(void*) + { + BAN::RefPtr keyboard_inode; + if (auto ret = VirtualFileSystem::get().file_from_absolute_path({ 0, 0, 0, 0 }, "/dev/keyboard"_sv, O_RDONLY); !ret.is_error()) + keyboard_inode = ret.value().inode; + else + { + dprintln("could not open keyboard device: {}", ret.error()); + return; + } + + while (true) + { + while (!TTY::current()->m_tty_ctrl.receive_input) + TTY::current()->m_tty_ctrl.thread_blocker.block_indefinite(); + + while (TTY::current()->m_tty_ctrl.receive_input) + { + LockGuard _(keyboard_inode->m_mutex); + if (!keyboard_inode->can_read()) + { + SystemTimer::get().sleep_ms(1); + continue; + } + + LibInput::RawKeyEvent event; + [[maybe_unused]] const size_t read = MUST(keyboard_inode->read(0, BAN::ByteSpan::from(event))); + ASSERT(read == sizeof(event)); + + TTY::current()->on_key_event(LibInput::KeyboardLayout::get().key_event_from_raw(event)); + } + } + } + void TTY::initialize_devices() { static bool initialized = false; ASSERT(!initialized); - Process::create_kernel( - [](void*) - { - auto file_or_error = VirtualFileSystem::get().file_from_absolute_path({ 0, 0, 0, 0 }, "/dev/keyboard"_sv, O_RDONLY); - if (file_or_error.is_error()) - { - dprintln("no keyboard found"); - return; - } - - auto inode = file_or_error.value().inode; - while (true) - { - while (!TTY::current()->m_tty_ctrl.receive_input) - TTY::current()->m_tty_ctrl.thread_blocker.block_indefinite(); - - LibInput::RawKeyEvent event; - size_t read = MUST(inode->read(0, BAN::ByteSpan::from(event))); - ASSERT(read == sizeof(event)); - - TTY::current()->on_key_event(LibInput::KeyboardLayout::get().key_event_from_raw(event)); - } - }, nullptr - ); + auto* thread = MUST(Thread::create_kernel(&TTY::keyboard_task, nullptr, nullptr)); + MUST(Processor::scheduler().add_thread(thread)); initialized = true; }