2024-06-03 18:04:33 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <LibFont/Font.h>
|
|
|
|
|
|
|
|
#include <LibGUI/Window.h>
|
|
|
|
|
2024-09-15 03:09:10 +03:00
|
|
|
struct Rectangle
|
|
|
|
{
|
|
|
|
uint32_t x { 0 };
|
|
|
|
uint32_t y { 0 };
|
|
|
|
uint32_t width { 0 };
|
|
|
|
uint32_t height { 0 };
|
|
|
|
|
|
|
|
Rectangle get_bounding_box(Rectangle other) const
|
|
|
|
{
|
|
|
|
const auto min_x = BAN::Math::min(x, other.x);
|
|
|
|
const auto min_y = BAN::Math::min(y, other.y);
|
|
|
|
const auto max_x = BAN::Math::max(x + width, other.x + other.width);
|
|
|
|
const auto max_y = BAN::Math::max(y + height, other.y + other.height);
|
|
|
|
return Rectangle {
|
|
|
|
.x = min_x,
|
|
|
|
.y = min_y,
|
|
|
|
.width = max_x - min_x,
|
|
|
|
.height = max_y - min_y,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-03 18:04:33 +03:00
|
|
|
class Terminal
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void run();
|
|
|
|
|
|
|
|
uint32_t cols() const { return m_window->width() / m_font.width(); }
|
|
|
|
uint32_t rows() const { return m_window->height() / m_font.height(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void handle_sgr();
|
2024-09-26 15:05:11 +03:00
|
|
|
Rectangle handle_csi(char ch);
|
2024-09-15 03:09:10 +03:00
|
|
|
Rectangle putchar(uint8_t ch);
|
2024-08-11 01:01:05 +03:00
|
|
|
bool read_shell();
|
2024-06-03 18:04:33 +03:00
|
|
|
|
2024-08-12 00:50:06 +03:00
|
|
|
void hide_cursor();
|
|
|
|
void show_cursor();
|
|
|
|
|
2024-10-17 01:36:59 +03:00
|
|
|
void on_key_event(LibGUI::EventPacket::KeyEvent::event_t);
|
2024-06-03 18:04:33 +03:00
|
|
|
|
|
|
|
void start_shell();
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Cursor
|
|
|
|
{
|
|
|
|
uint32_t x;
|
|
|
|
uint32_t y;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ShellInfo
|
|
|
|
{
|
2024-08-11 01:01:05 +03:00
|
|
|
int pts_master;
|
2024-06-03 18:04:33 +03:00
|
|
|
pid_t pid;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class State
|
|
|
|
{
|
|
|
|
Normal,
|
|
|
|
ESC,
|
|
|
|
CSI,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CSIInfo
|
|
|
|
{
|
|
|
|
int32_t fields[2];
|
|
|
|
size_t index;
|
2024-08-12 00:50:06 +03:00
|
|
|
bool question;
|
2024-06-03 18:04:33 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
BAN::UniqPtr<LibGUI::Window> m_window;
|
|
|
|
LibFont::Font m_font;
|
|
|
|
ShellInfo m_shell_info;
|
|
|
|
State m_state { State::Normal };
|
|
|
|
CSIInfo m_csi_info;
|
|
|
|
|
2024-08-12 00:50:06 +03:00
|
|
|
bool m_cursor_shown { true };
|
2024-08-12 14:24:17 +03:00
|
|
|
bool m_cursor_blink_shown { true };
|
|
|
|
uint64_t m_cursor_blink_ms { 0 };
|
2024-08-12 00:50:06 +03:00
|
|
|
Cursor m_cursor { 0, 0 };
|
|
|
|
BAN::Vector<uint32_t> m_cursor_buffer;
|
2024-08-12 14:24:17 +03:00
|
|
|
bool m_got_key_event { false };
|
2024-08-12 00:50:06 +03:00
|
|
|
|
2024-08-11 00:54:51 +03:00
|
|
|
uint8_t m_utf8_index { 0 };
|
|
|
|
uint8_t m_utf8_bytes[4] { };
|
|
|
|
|
2024-06-03 18:04:33 +03:00
|
|
|
Cursor m_saved_cursor { 0, 0 };
|
2024-09-12 19:34:25 +03:00
|
|
|
uint32_t m_fg_color { 0 };
|
|
|
|
uint32_t m_bg_color { 0 };
|
2024-06-03 18:04:33 +03:00
|
|
|
};
|