Implemented simple IO for getting keyboard input.

This commit is contained in:
2024-01-20 01:13:29 +01:00
parent d711938821
commit f4bad14fcd
9 changed files with 584 additions and 306 deletions

View File

@@ -4,6 +4,7 @@
#if !defined(BAD_APPLE_OS_DRIVERS_PS2_HPP_INCLUDED)
#define BAD_APPLE_OS_DRIVERS_PS2_HPP_INCLUDED
#include <array>
#include "drivers/pic.hpp"
namespace baos::ps2
@@ -77,6 +78,7 @@ enum class Scancode
ENTER = 0x5A,
BRACKET_CLOSE = 0x5B,
BACKSLASH = 0x5D,
GRAVE = 0x61,
BACKSPACE = 0x66,
KP_1 = 0x69,
KP_4 = 0x6B,
@@ -98,46 +100,61 @@ enum class Scancode
SCROLLOCK = 0x7E,
F7 = 0x83,
MM_WWW_SEARCH = 0xE010,
RIGHT_ALT = 0xE011,
RIGHT_CONTROL = 0xE014,
MM_PREV_TRACK = 0xE015,
LEFT_GUI = 0xE01F,
MM_WWW_FAVORITES = 0xE020,
MM_VOLUME_DOWN = 0xE021,
MM_MUTE = 0xE023,
RIGHT_GUI = 0xE027,
MM_WWW_STOP = 0xE028,
MM_CALCULATOR = 0xE02B,
APPS = 0xE02F,
MM_WWW_FORWARD = 0xE030,
MM_VOLUME_UP = 0xE032,
MM_PLAY_PAUSE = 0xE034,
ACPI_POWER = 0xE037,
MM_WWW_BACK = 0xE038,
MM_WWW_HOME = 0xE03A,
MM_STOP = 0xE03B,
ACPI_SLEEP = 0xE03F,
MM_MY_COMPUTER = 0xE040,
MM_EMAIL = 0xE048,
KP_SLASH = 0xE04A,
MM_NEXT_TRACK = 0xE04D,
MM_MEDIA_SELECT = 0xE050,
KP_ENTER = 0xE05A,
ACPI_WAKE = 0xE05E,
END = 0xE069,
ARROW_LEFT = 0xE06B,
HOME = 0xE06C,
INSERT = 0xE070,
DELETE = 0xE071,
ARROW_DOWN = 0xE072,
ARROW_RIGHT = 0xE074,
ARROW_UP = 0xE075,
PAGE_DOWN = 0xE07A,
PAGE_UP = 0xE07D,
MM_WWW_SEARCH = 0x0110,
RIGHT_ALT = 0x0111,
RIGHT_CONTROL = 0x0114,
MM_PREV_TRACK = 0x0115,
LEFT_GUI = 0x011F,
MM_WWW_FAVORITES = 0x0120,
MM_VOLUME_DOWN = 0x0121,
MM_MUTE = 0x0123,
RIGHT_GUI = 0x0127,
MM_WWW_STOP = 0x0128,
MM_CALCULATOR = 0x012B,
APPS = 0x012F,
MM_WWW_FORWARD = 0x0130,
MM_VOLUME_UP = 0x0132,
MM_PLAY_PAUSE = 0x0134,
ACPI_POWER = 0x0137,
MM_WWW_BACK = 0x0138,
MM_WWW_HOME = 0x013A,
MM_STOP = 0x013B,
ACPI_SLEEP = 0x013F,
MM_MY_COMPUTER = 0x0140,
MM_EMAIL = 0x0148,
KP_SLASH = 0x014A,
MM_NEXT_TRACK = 0x014D,
MM_MEDIA_SELECT = 0x0150,
KP_ENTER = 0x015A,
ACPI_WAKE = 0x015E,
END = 0x0169,
ARROW_LEFT = 0x016B,
HOME = 0x016C,
INSERT = 0x0170,
DELETE = 0x0171,
ARROW_DOWN = 0x0172,
ARROW_RIGHT = 0x0174,
ARROW_UP = 0x0175,
PAGE_DOWN = 0x017A,
PAGE_UP = 0x017D,
PRINT_SCREEN = 0xF000,
PAUSE = 0xF001
PRINT_SCREEN = 0x017E,
PAUSE = 0x017F
};
inline constexpr std::uint16_t MAX_SCANCODE = static_cast<std::uint16_t>(Scancode::PAUSE) + 1;
struct Keymap
{
std::array<char, MAX_SCANCODE> regularKeys;
std::array<char, MAX_SCANCODE> shiftedKeys;
std::array<char, MAX_SCANCODE> altGredKeys;
constexpr void set(Scancode scancode, char regular, char shifted = '\0', char altgred = '\0') noexcept
{
regularKeys[static_cast<unsigned>(scancode)] = regular;
shiftedKeys[static_cast<unsigned>(scancode)] = shifted;
altGredKeys[static_cast<unsigned>(scancode)] = altgred;
}
};
struct KeyEvent
@@ -152,8 +169,11 @@ struct KeyEvent
__attribute__((interrupt))
void isrKeyboard(InterruptFrame* interruptFrame) noexcept;
[[nodiscard]] bool isKeyDown(Scancode scancode) noexcept;
[[nodiscard]] bool readKey(KeyEvent& outEvent) noexcept;
[[nodiscard]] bool readChar(char& outChar) noexcept;
[[nodiscard]] const char* keyName(Scancode scancode) noexcept;
[[nodiscard]] bool keyToChar(Scancode scancode, char& outChar) noexcept;
}
#endif // !defined(BAD_APPLE_OS_DRIVERS_PS2_HPP_INCLUDED)

View File

@@ -0,0 +1,44 @@
#pragma once
#if !defined(BAD_APPLE_OS_OS_TOOLS_RINGBUFFER_HPP_INCLUDED)
#define BAD_APPLE_OS_OS_TOOLS_RINGBUFFER_HPP_INCLUDED
#include <array>
#include <cstddef>
namespace baos
{
template<typename T, std::size_t SIZE>
class RingBuffer
{
private:
std::array<T, SIZE> mElements;
std::size_t mBufferedElements = 0;
std::size_t mPosition = 0;
public:
[[nodiscard]] bool full() const noexcept { return mBufferedElements == SIZE; }
[[nodiscard]] bool empty() const noexcept { return mBufferedElements == 0; }
bool append(T element) noexcept
{
if (full()) {
return false;
}
mElements[mPosition] = std::move(element);
++mBufferedElements;
mPosition = (mPosition + 1) % SIZE;
return true;
}
bool next(T& outElement) noexcept
{
if (empty()) {
return false;
}
outElement = std::move(mElements[(mPosition + SIZE - mBufferedElements) % SIZE]);
--mBufferedElements;
return true;
}
};
}
#endif // !defined(BAD_APPLE_OS_OS_TOOLS_RINGBUFFER_HPP_INCLUDED)

View File

@@ -8,8 +8,16 @@
#include <stddef.h>
#include "./detail/common.h"
#define stdin __stdin
BA_EXTERN_C_BEGIN
typedef struct __file FILE;
extern FILE* __stdin;
static int EOF = -1;
int putchar(int chr) BA_CXX_NOEXCEPT;
int puts(const char* str) BA_CXX_NOEXCEPT;
int printf(const char* format, ...) BA_CXX_NOEXCEPT __attribute__((format(printf, 1, 2)));
@@ -17,6 +25,11 @@ int vprintf(const char* format, va_list vlist) BA_CXX_NOEXCEPT;
int snprintf(char* buffer, size_t bufferSize, const char* format, ...) BA_CXX_NOEXCEPT __attribute__((format(printf, 3, 4)));
int vsnprintf(char* buffer, size_t bufferSize, const char* format, va_list vlist) BA_CXX_NOEXCEPT;
int fgetc(FILE* stream) BA_CXX_NOEXCEPT;
char* fgets(char* buffer, int count, FILE* stream) BA_CXX_NOEXCEPT;
inline int getc(FILE* stream) BA_CXX_NOEXCEPT { return fgetc(stream); }
inline int getchar() BA_CXX_NOEXCEPT { return fgetc(stdin); }
BA_EXTERN_C_END
#endif // !defined(BAD_APPLE_OS_STDIO_H_INCLUDED)