| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include <ansi_colors.h> |
| 6 | #include <array> |
| 7 | #include <mos/io/io.hpp> |
| 8 | #include <mos/lib/structures/list.hpp> |
| 9 | #include <mos/lib/structures/ring_buffer.hpp> |
| 10 | #include <mos/lib/sync/spinlock.hpp> |
| 11 | #include <mos/mos_global.h> |
| 12 | #include <mos/tasks/wait.hpp> |
| 13 | #include <mos/types.hpp> |
| 14 | |
| 15 | enum ConsoleCapability |
| 16 | { |
| 17 | CONSOLE_CAP_COLOR = 1 << 0, |
| 18 | CONSOLE_CAP_CLEAR = 1 << 1, |
| 19 | CONSOLE_CAP_GET_SIZE = 1 << 2, |
| 20 | CONSOLE_CAP_CURSOR_HIDE = 1 << 3, |
| 21 | CONSOLE_CAP_CURSOR_MOVE = 1 << 4, |
| 22 | CONSOLE_CAP_READ = 1 << 6, ///< console supports read |
| 23 | }; |
| 24 | |
| 25 | MOS_ENUM_FLAGS(ConsoleCapability, ConsoleCapFlags); |
| 26 | |
| 27 | template<size_t buf_size> |
| 28 | struct Buffer |
| 29 | { |
| 30 | u8 buf[buf_size] __aligned(buf_size) = { 0 }; |
| 31 | const size_t size = buf_size; |
| 32 | }; |
| 33 | |
| 34 | struct Console : public IO |
| 35 | { |
| 36 | StandardColor fg, bg; |
| 37 | |
| 38 | public: |
| 39 | template<size_t buf_size> |
| 40 | Console(mos::string_view name, ConsoleCapFlags caps, Buffer<buf_size> *readBuf, StandardColor fg, StandardColor bg) : Console(name, caps, fg, bg) |
| 41 | { |
| 42 | this->reader.buf = readBuf->buf; |
| 43 | this->reader.size = readBuf->size; |
| 44 | ring_buffer_pos_init(pos: &reader.pos, capacity: reader.size); |
| 45 | } |
| 46 | |
| 47 | virtual ~Console() = default; |
| 48 | |
| 49 | void Register(); |
| 50 | |
| 51 | public: |
| 52 | size_t Write(const char *data, size_t size); |
| 53 | size_t WriteColored(const char *data, size_t size, StandardColor fg, StandardColor bg); |
| 54 | void putc(u8 c); |
| 55 | |
| 56 | private: |
| 57 | virtual bool clear() = 0; |
| 58 | virtual bool set_color(StandardColor fg, StandardColor bg) = 0; |
| 59 | virtual size_t do_write(const char *data, size_t size) = 0; |
| 60 | |
| 61 | public: |
| 62 | // IO interface |
| 63 | virtual size_t on_read(void *, size_t) override; |
| 64 | virtual size_t on_write(const void *, size_t) override; |
| 65 | virtual void on_closed() override; |
| 66 | |
| 67 | public: |
| 68 | // IO interface |
| 69 | virtual mos::string name() const override; |
| 70 | |
| 71 | private: |
| 72 | Console(mos::string_view name, ConsoleCapFlags caps, StandardColor default_fg, StandardColor default_bg); |
| 73 | |
| 74 | private: |
| 75 | const ConsoleCapFlags caps; |
| 76 | const StandardColor default_fg = White, default_bg = Black; |
| 77 | |
| 78 | struct reader |
| 79 | { |
| 80 | spinlock_t lock; |
| 81 | u8 *buf = nullptr; |
| 82 | ring_buffer_pos_t pos; |
| 83 | size_t size = 0; |
| 84 | } reader; |
| 85 | |
| 86 | struct |
| 87 | { |
| 88 | spinlock_t lock; |
| 89 | } writer; |
| 90 | |
| 91 | mos::string_view conName = "<unnamed>" ; |
| 92 | waitlist_t waitlist; // waitlist for readers |
| 93 | }; |
| 94 | |
| 95 | extern std::array<Console *, 128> consoles; |
| 96 | |
| 97 | std::optional<Console *> console_get(mos::string_view name); |
| 98 | std::optional<Console *> console_get_by_prefix(mos::string_view name); |
| 99 | |