Kernel: Remove TTY keyboard thread

This was really hacky as it had no idea when the keyboard had events
and the blocking was just one millisecond sleeps :D

Now keyboard device checks if current tty is receiving input and if so
it forwards the events to the TTY.
This commit is contained in:
2026-05-20 04:17:03 +03:00
parent 32206069bc
commit a19e6938eb
3 changed files with 55 additions and 33 deletions

View File

@@ -61,6 +61,7 @@ namespace Kernel
{ {
public: public:
static BAN::ErrorOr<BAN::RefPtr<KeyboardDevice>> create(mode_t mode, uid_t uid, gid_t gid); static BAN::ErrorOr<BAN::RefPtr<KeyboardDevice>> create(mode_t mode, uid_t uid, gid_t gid);
static BAN::ErrorOr<void> initialize_tty_thread();
void notify(); void notify();

View File

@@ -1,7 +1,10 @@
#include <BAN/CircularQueue.h>
#include <kernel/Device/DeviceNumbers.h> #include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Input/InputDevice.h> #include <kernel/Input/InputDevice.h>
#include <kernel/Lock/SpinLockAsMutex.h> #include <kernel/Lock/SpinLockAsMutex.h>
#include <kernel/Terminal/TTY.h>
#include <LibInput/Joystick.h> #include <LibInput/Joystick.h>
#include <LibInput/KeyEvent.h> #include <LibInput/KeyEvent.h>
@@ -24,6 +27,10 @@ namespace Kernel
static SpinLock s_joystick_lock; static SpinLock s_joystick_lock;
static BAN::Vector<BAN::WeakPtr<InputDevice>> s_joysticks; static BAN::Vector<BAN::WeakPtr<InputDevice>> s_joysticks;
static SpinLock s_tty_keyboard_event_lock;
static ThreadBlocker s_tty_keyboard_event_blocker;
static BAN::CircularQueue<LibInput::RawKeyEvent, 128> s_tty_keyboard_events;
static const char* get_name_format(InputDevice::Type type) static const char* get_name_format(InputDevice::Type type)
{ {
switch (type) switch (type)
@@ -201,6 +208,15 @@ namespace Kernel
break; break;
} }
} }
if (TTY::current()->should_receive_input())
{
SpinLockGuard _(s_tty_keyboard_event_lock);
if (!s_tty_keyboard_events.full())
s_tty_keyboard_events.push(key_event);
s_tty_keyboard_event_blocker.unblock();
return;
}
} }
if (m_event_count == m_max_event_count) if (m_event_count == m_max_event_count)
@@ -261,6 +277,41 @@ namespace Kernel
} }
static void tty_keyboard_thread(void*)
{
static BAN::Atomic<bool> initialized = false;
[[maybe_unused]] bool old_initialized = initialized.exchange(true);
ASSERT(old_initialized == false);
for (;;)
{
LibInput::RawKeyEvent event;
{
SpinLockGuard guard(s_tty_keyboard_event_lock);
if (s_tty_keyboard_events.empty())
{
SpinLockGuardAsMutex smutex(guard);
s_tty_keyboard_event_blocker.block_indefinite(&smutex);
continue;
}
event = s_tty_keyboard_events.front();
s_tty_keyboard_events.pop();
}
TTY::current()->on_key_event(event);
}
}
BAN::ErrorOr<void> KeyboardDevice::initialize_tty_thread()
{
auto* thread = TRY(Thread::create_kernel(tty_keyboard_thread, nullptr));
ASSERT(thread);
TRY(Processor::scheduler().add_thread(thread));
return {};
}
BAN::ErrorOr<BAN::RefPtr<KeyboardDevice>> KeyboardDevice::create(mode_t mode, uid_t uid, gid_t gid) BAN::ErrorOr<BAN::RefPtr<KeyboardDevice>> KeyboardDevice::create(mode_t mode, uid_t uid, gid_t gid)
{ {

View File

@@ -5,6 +5,7 @@
#include <kernel/Device/DeviceNumbers.h> #include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/FS/VirtualFileSystem.h> #include <kernel/FS/VirtualFileSystem.h>
#include <kernel/Input/InputDevice.h>
#include <kernel/Lock/LockGuard.h> #include <kernel/Lock/LockGuard.h>
#include <kernel/Lock/SpinLockAsMutex.h> #include <kernel/Lock/SpinLockAsMutex.h>
#include <kernel/Process.h> #include <kernel/Process.h>
@@ -117,40 +118,9 @@ namespace Kernel
return {}; return {};
} }
void TTY::keyboard_task(void*)
{
BAN::RefPtr<Inode> keyboard_inode;
if (auto ret = DevFileSystem::get().root_inode()->find_inode("keyboard"_sv); !ret.is_error())
keyboard_inode = ret.release_value();
else
{
dprintln("could not open keyboard device: {}", ret.error());
return;
}
while (true)
{
while (TTY::current()->m_tty_ctrl.receive_input)
{
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() void TTY::initialize_devices()
{ {
auto* thread = MUST(Thread::create_kernel(&TTY::keyboard_task, nullptr)); MUST(KeyboardDevice::initialize_tty_thread());
MUST(Processor::scheduler().add_thread(thread));
DevFileSystem::get().add_inode("tty", MUST(DevTTY::create(0666, 0, 0))); DevFileSystem::get().add_inode("tty", MUST(DevTTY::create(0666, 0, 0)));
} }
@@ -268,7 +238,7 @@ namespace Kernel
if (ch == _POSIX_VDISABLE) if (ch == _POSIX_VDISABLE)
return; return;
LockGuard _0(m_mutex); LockGuard _(m_mutex);
const auto termios = get_termios(); const auto termios = get_termios();