mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:08:10 +00:00

Now that we're bringing back the in-kernel virtual console, we should move towards having a single implementation of terminal emulation. This patch rips out the emulation code from the Terminal application and turns it into the beginnings of LibVT. The basic design idea is that users of VT::Terminal will implement and provide a VT::TerminalClient subclass to handle presentation-specific things. We'll need to iterate on this, but it's a start. :^)
224 lines
5.5 KiB
C++
224 lines
5.5 KiB
C++
#pragma once
|
|
|
|
#include <AK/AKString.h>
|
|
#include <AK/NonnullOwnPtrVector.h>
|
|
#include <AK/Vector.h>
|
|
|
|
namespace VT {
|
|
|
|
class TerminalClient {
|
|
public:
|
|
virtual ~TerminalClient() {}
|
|
|
|
virtual void beep() = 0;
|
|
virtual void set_window_title(const StringView&) = 0;
|
|
virtual void terminal_did_resize(u16 columns, u16 rows) = 0;
|
|
};
|
|
|
|
struct Attribute {
|
|
Attribute() { reset(); }
|
|
|
|
static u8 default_foreground_color;
|
|
static u8 default_background_color;
|
|
|
|
void reset()
|
|
{
|
|
foreground_color = default_foreground_color;
|
|
background_color = default_background_color;
|
|
flags = Flags::NoAttributes;
|
|
}
|
|
u8 foreground_color;
|
|
u8 background_color;
|
|
|
|
enum Flags {
|
|
NoAttributes = 0x00,
|
|
Bold = 0x01,
|
|
Italic = 0x02,
|
|
Underline = 0x04,
|
|
Negative = 0x08,
|
|
Blink = 0x10,
|
|
Touched = 0x20,
|
|
};
|
|
|
|
bool is_untouched() const { return !(flags & Touched); }
|
|
|
|
// TODO: it would be really nice if we had a helper for enums that
|
|
// exposed bit ops for class enums...
|
|
int flags = Flags::NoAttributes;
|
|
|
|
bool operator==(const Attribute& other) const
|
|
{
|
|
return foreground_color == other.foreground_color && background_color == other.background_color && flags == other.flags;
|
|
}
|
|
bool operator!=(const Attribute& other) const
|
|
{
|
|
return !(*this == other);
|
|
}
|
|
};
|
|
|
|
class BufferPosition {
|
|
public:
|
|
BufferPosition() {}
|
|
BufferPosition(int row, int column)
|
|
: m_row(row)
|
|
, m_column(column)
|
|
{
|
|
}
|
|
|
|
bool is_valid() const { return m_row >= 0 && m_column >= 0; }
|
|
int row() const { return m_row; }
|
|
int column() const { return m_column; }
|
|
|
|
bool operator<(const BufferPosition& other) const
|
|
{
|
|
return m_row < other.m_row || (m_row == other.m_row && m_column < other.m_column);
|
|
}
|
|
|
|
bool operator<=(const BufferPosition& other) const
|
|
{
|
|
return *this < other || *this == other;
|
|
}
|
|
|
|
bool operator>=(const BufferPosition& other) const
|
|
{
|
|
return !(*this < other);
|
|
}
|
|
|
|
bool operator==(const BufferPosition& other) const
|
|
{
|
|
return m_row == other.m_row && m_column == other.m_column;
|
|
}
|
|
|
|
bool operator!=(const BufferPosition& other) const
|
|
{
|
|
return !(*this == other);
|
|
}
|
|
|
|
private:
|
|
int m_row { -1 };
|
|
int m_column { -1 };
|
|
};
|
|
|
|
class Terminal {
|
|
public:
|
|
explicit Terminal(TerminalClient&);
|
|
~Terminal();
|
|
|
|
bool m_need_full_flush { false };
|
|
|
|
void invalidate_cursor();
|
|
void on_char(u8);
|
|
|
|
void clear();
|
|
void set_size(u16 columns, u16 rows);
|
|
u16 columns() const { return m_columns; }
|
|
u16 rows() const { return m_rows; }
|
|
|
|
u16 cursor_column() const { return m_cursor_column; }
|
|
u16 cursor_row() const { return m_cursor_row; }
|
|
|
|
struct Line {
|
|
explicit Line(u16 columns);
|
|
~Line();
|
|
void clear(Attribute);
|
|
bool has_only_one_background_color() const;
|
|
void set_length(u16);
|
|
u8* characters { nullptr };
|
|
Attribute* attributes { nullptr };
|
|
bool dirty { false };
|
|
u16 m_length { 0 };
|
|
};
|
|
|
|
Line& line(size_t index)
|
|
{
|
|
ASSERT(index < m_rows);
|
|
return m_lines[index];
|
|
}
|
|
const Line& line(size_t index) const
|
|
{
|
|
ASSERT(index < m_rows);
|
|
return m_lines[index];
|
|
}
|
|
|
|
private:
|
|
typedef Vector<unsigned, 4> ParamVector;
|
|
|
|
void scroll_up();
|
|
void scroll_down();
|
|
void newline();
|
|
void set_cursor(unsigned row, unsigned column);
|
|
void put_character_at(unsigned row, unsigned column, u8 ch);
|
|
void set_window_title(const String&);
|
|
|
|
void inject_string(const String&);
|
|
void unimplemented_escape();
|
|
void unimplemented_xterm_escape();
|
|
|
|
void escape$A(const ParamVector&);
|
|
void escape$B(const ParamVector&);
|
|
void escape$C(const ParamVector&);
|
|
void escape$D(const ParamVector&);
|
|
void escape$H(const ParamVector&);
|
|
void escape$J(const ParamVector&);
|
|
void escape$K(const ParamVector&);
|
|
void escape$M(const ParamVector&);
|
|
void escape$P(const ParamVector&);
|
|
void escape$G(const ParamVector&);
|
|
void escape$X(const ParamVector&);
|
|
void escape$b(const ParamVector&);
|
|
void escape$d(const ParamVector&);
|
|
void escape$m(const ParamVector&);
|
|
void escape$s(const ParamVector&);
|
|
void escape$u(const ParamVector&);
|
|
void escape$t(const ParamVector&);
|
|
void escape$r(const ParamVector&);
|
|
void escape$S(const ParamVector&);
|
|
void escape$T(const ParamVector&);
|
|
void escape$L(const ParamVector&);
|
|
void escape$h_l(bool, bool, const ParamVector&);
|
|
|
|
TerminalClient& m_client;
|
|
|
|
NonnullOwnPtrVector<Line> m_lines;
|
|
|
|
int m_scroll_region_top { 0 };
|
|
int m_scroll_region_bottom { 0 };
|
|
|
|
u16 m_columns { 0 };
|
|
u16 m_rows { 0 };
|
|
|
|
u16 m_cursor_row { 0 };
|
|
u16 m_cursor_column { 0 };
|
|
u16 m_saved_cursor_row { 0 };
|
|
u16 m_saved_cursor_column { 0 };
|
|
bool m_swallow_current { false };
|
|
bool m_stomp { false };
|
|
|
|
Attribute m_current_attribute;
|
|
|
|
void execute_escape_sequence(u8 final);
|
|
void execute_xterm_command();
|
|
|
|
enum EscapeState {
|
|
Normal,
|
|
ExpectBracket,
|
|
ExpectParameter,
|
|
ExpectIntermediate,
|
|
ExpectFinal,
|
|
|
|
ExpectXtermParameter1,
|
|
ExpectXtermParameter2,
|
|
ExpectXtermFinal,
|
|
};
|
|
EscapeState m_escape_state { Normal };
|
|
Vector<u8> m_parameters;
|
|
Vector<u8> m_intermediates;
|
|
Vector<u8> m_xterm_param1;
|
|
Vector<u8> m_xterm_param2;
|
|
Vector<bool> m_horizontal_tabs;
|
|
u8 m_final { 0 };
|
|
|
|
u8 m_last_char { 0 };
|
|
};
|
|
|
|
}
|