From abbbf7ec154a28b8ce5a0bcb91e401a3e3a11b2a Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 5 Apr 2023 02:53:28 +0300 Subject: [PATCH] Kernel: Add tty to process and make termios modifiable --- kernel/include/kernel/Process.h | 6 +++++- kernel/include/kernel/Terminal/TTY.h | 9 +++++--- kernel/kernel/Process.cpp | 31 ++++++++++++++++++++-------- kernel/kernel/Terminal/TTY.cpp | 5 +++++ 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index 4762abb3..7713afaf 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -28,6 +29,7 @@ namespace Kernel void on_thread_exit(Thread&); BAN::ErrorOr init_stdio(); + BAN::ErrorOr set_termios(const termios&); pid_t pid() const { return m_pid; } @@ -50,7 +52,7 @@ namespace Kernel static BAN::RefPtr current() { return Thread::current().process(); } private: - Process(pid_t pid) : m_pid(pid) {} + Process(pid_t); BAN::ErrorOr absolute_path_of(BAN::StringView) const; @@ -75,6 +77,8 @@ namespace Kernel BAN::String m_working_directory; BAN::Vector m_threads; + TTY* m_tty { nullptr }; + friend class BAN::RefPtr; }; diff --git a/kernel/include/kernel/Terminal/TTY.h b/kernel/include/kernel/Terminal/TTY.h index 4958736c..6674b7ec 100644 --- a/kernel/include/kernel/Terminal/TTY.h +++ b/kernel/include/kernel/Terminal/TTY.h @@ -16,9 +16,8 @@ namespace Kernel { public: TTY(TerminalDriver*); - void clear(); - void putchar(uint8_t ch); - void set_cursor_position(uint32_t x, uint32_t y); + + void set_termios(const termios& termios) { m_termios = termios; } void set_font(const Kernel::Font&); uint32_t height() const { return m_height; } @@ -27,16 +26,20 @@ namespace Kernel // for kprint static void putchar_current(uint8_t ch); static bool is_initialized(); + static TTY* current(); virtual BAN::ErrorOr read(size_t, void*, size_t) override; virtual BAN::ErrorOr write(size_t, const void*, size_t) override; private: + void clear(); + void putchar(uint8_t ch); void reset_ansi(); void handle_ansi_csi(uint8_t ch); void handle_ansi_csi_color(); void putchar_at(uint32_t codepoint, uint32_t x, uint32_t y); void render_from_buffer(uint32_t x, uint32_t y); + void set_cursor_position(uint32_t x, uint32_t y); void on_key(Input::KeyEvent); void do_backspace(); diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 92372242..62f2ceee 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -18,15 +18,10 @@ namespace Kernel return process; } - BAN::ErrorOr Process::init_stdio() - { - if (!m_open_files.empty()) - return BAN::Error::from_c_string("Could not init stdio, process already has open files"); - TRY(open("/dev/tty1", O_RDONLY)); // stdin - TRY(open("/dev/tty1", O_WRONLY)); // stdout - TRY(open("/dev/tty1", O_WRONLY)); // stderr - return {}; - } + Process::Process(pid_t pid) + : m_pid(pid) + , m_tty(TTY::current()) + { } BAN::ErrorOr Process::add_thread(entry_t entry, void* data) { @@ -51,6 +46,24 @@ namespace Kernel m_threads.remove(i); } + BAN::ErrorOr Process::init_stdio() + { + if (!m_open_files.empty()) + return BAN::Error::from_c_string("Could not init stdio, process already has open files"); + TRY(open("/dev/tty1", O_RDONLY)); // stdin + TRY(open("/dev/tty1", O_WRONLY)); // stdout + TRY(open("/dev/tty1", O_WRONLY)); // stderr + return {}; + } + + BAN::ErrorOr Process::set_termios(const termios& termios) + { + if (m_tty == nullptr) + return BAN::Error::from_errno(ENOTTY); + m_tty->set_termios(termios); + return {}; + } + BAN::ErrorOr Process::open(BAN::StringView path, int flags) { if (flags & ~O_RDWR) diff --git a/kernel/kernel/Terminal/TTY.cpp b/kernel/kernel/Terminal/TTY.cpp index bd21d279..2f44e0f2 100644 --- a/kernel/kernel/Terminal/TTY.cpp +++ b/kernel/kernel/Terminal/TTY.cpp @@ -59,6 +59,11 @@ namespace Kernel ); } + TTY* TTY::current() + { + return s_tty; + } + void TTY::on_key(Input::KeyEvent event) { ASSERT(!m_lock.is_locked());