From fee33e7a14c1fb156c1e1902e05ceca92ef84956 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 9 Apr 2023 11:23:57 +0200 Subject: [PATCH] kernel: Add ioctls() for termios stuff to ConsoleDevice Only handles echoing for now. --- kernel/src/fs/devices/ConsoleDevice.cpp | 35 ++++++++++++++++++++++++- kernel/src/fs/devices/ConsoleDevice.h | 2 ++ libc/include/bits/termios.h | 21 +++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 libc/include/bits/termios.h diff --git a/kernel/src/fs/devices/ConsoleDevice.cpp b/kernel/src/fs/devices/ConsoleDevice.cpp index cffb29f1..c37143e9 100644 --- a/kernel/src/fs/devices/ConsoleDevice.cpp +++ b/kernel/src/fs/devices/ConsoleDevice.cpp @@ -1,12 +1,15 @@ #include "fs/devices/ConsoleDevice.h" #include "arch/Keyboard.h" +#include "memory/MemoryManager.h" #include "video/TextConsole.h" +#include #include #include #include static Buffer g_console_input; static bool g_eof { false }; +static bool g_echo { true }; Result> ConsoleDevice::create() { @@ -45,7 +48,10 @@ void ConsoleDevice::did_press_key(char key) { if (key == '\b') { - if (g_temp_input.try_pop().has_value()) TextConsole::putwchar(L'\b'); + if (g_temp_input.try_pop().has_value()) + { + if (g_echo) TextConsole::putwchar(L'\b'); + } return; } @@ -65,5 +71,32 @@ void ConsoleDevice::did_press_key(char key) g_temp_input.clear(); } + if (!g_echo) return; TextConsole::putchar(key); } + +Result ConsoleDevice::ioctl(int request, void* arg) +{ + switch (request) + { + case TCGETS: { + struct termios tc + { + .c_lflag = 0 + }; + + if (g_echo) tc.c_lflag |= ECHO; + return MemoryManager::copy_to_user_typed((struct termios*)arg, &tc) ? 0 : err(EFAULT); + } + case TCSETS: { + struct termios tc; + if (!MemoryManager::copy_from_user_typed((const struct termios*)arg, &tc)) return err(EFAULT); + if (tc.c_lflag & ECHO) g_echo = true; + else + g_echo = false; + + return 0; + } + default: return err(EINVAL); + } +} diff --git a/kernel/src/fs/devices/ConsoleDevice.h b/kernel/src/fs/devices/ConsoleDevice.h index 224f394c..d8cb8ce5 100644 --- a/kernel/src/fs/devices/ConsoleDevice.h +++ b/kernel/src/fs/devices/ConsoleDevice.h @@ -15,5 +15,7 @@ class ConsoleDevice : public Device bool blocking() const override; + Result ioctl(int request, void* arg) override; + virtual ~ConsoleDevice() = default; }; diff --git a/libc/include/bits/termios.h b/libc/include/bits/termios.h new file mode 100644 index 00000000..e4049293 --- /dev/null +++ b/libc/include/bits/termios.h @@ -0,0 +1,21 @@ +/* bits/termios.h: Terminal control definitions. */ + +#ifndef _BITS_TERMIOS_H +#define _BITS_TERMIOS_H + +typedef long tcflag_t; + +// VERY incomplete termios structure. +struct termios +{ + tcflag_t c_lflag; +}; + +// Values for c_lflag. +#define ECHO 1 + +// termios ioctl() requests. +#define TCGETS 0 +#define TCSETS 1 + +#endif