kernel: Add ioctls() for termios stuff to ConsoleDevice

Only handles echoing for now.
This commit is contained in:
apio 2023-04-09 11:23:57 +02:00
parent 76eb8cd129
commit fee33e7a14
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 57 additions and 1 deletions

View File

@ -1,12 +1,15 @@
#include "fs/devices/ConsoleDevice.h"
#include "arch/Keyboard.h"
#include "memory/MemoryManager.h"
#include "video/TextConsole.h"
#include <bits/termios.h>
#include <luna/Buffer.h>
#include <luna/CString.h>
#include <luna/Vector.h>
static Buffer g_console_input;
static bool g_eof { false };
static bool g_echo { true };
Result<SharedPtr<Device>> 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<u64> 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);
}
}

View File

@ -15,5 +15,7 @@ class ConsoleDevice : public Device
bool blocking() const override;
Result<u64> ioctl(int request, void* arg) override;
virtual ~ConsoleDevice() = default;
};

View File

@ -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