1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 18:27:35 +00:00

Libraries: Move to Userland/Libraries/

This commit is contained in:
Andreas Kling 2021-01-12 12:17:30 +01:00
parent dc28c07fa5
commit 13d7c09125
1857 changed files with 266 additions and 274 deletions

View file

@ -0,0 +1,8 @@
set(SOURCES
Line.cpp
Terminal.cpp
TerminalWidget.cpp
)
serenity_lib(LibVT vt)
target_link_libraries(LibVT LibC LibCore LibGUI LibGfx LibDesktop)

View file

@ -0,0 +1,127 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibVT/Line.h>
#include <string.h>
namespace VT {
Line::Line(u16 length)
{
set_length(length);
}
Line::~Line()
{
if (m_utf32)
delete[] m_code_points.as_u32;
else
delete[] m_code_points.as_u8;
delete[] m_attributes;
}
template<typename CodepointType>
static CodepointType* create_new_code_point_array(size_t new_length, const CodepointType* old_code_points, size_t old_length)
{
auto* new_code_points = new CodepointType[new_length];
for (size_t i = 0; i < new_length; ++i)
new_code_points[i] = ' ';
if (old_code_points) {
for (size_t i = 0; i < min(old_length, new_length); ++i) {
new_code_points[i] = old_code_points[i];
}
}
delete[] old_code_points;
return new_code_points;
}
void Line::set_length(u16 new_length)
{
if (m_length == new_length)
return;
if (m_utf32)
m_code_points.as_u32 = create_new_code_point_array<u32>(new_length, m_code_points.as_u32, m_length);
else
m_code_points.as_u8 = create_new_code_point_array<u8>(new_length, m_code_points.as_u8, m_length);
auto* new_attributes = new Attribute[new_length];
if (m_attributes) {
for (size_t i = 0; i < min(m_length, new_length); ++i)
new_attributes[i] = m_attributes[i];
}
delete[] m_attributes;
m_attributes = new_attributes;
m_length = new_length;
}
void Line::clear(Attribute attribute)
{
if (m_dirty) {
for (u16 i = 0; i < m_length; ++i) {
set_code_point(i, ' ');
m_attributes[i] = attribute;
}
return;
}
for (unsigned i = 0; i < m_length; ++i) {
if (code_point(i) != ' ')
m_dirty = true;
set_code_point(i, ' ');
}
for (unsigned i = 0; i < m_length; ++i) {
if (m_attributes[i] != attribute)
m_dirty = true;
m_attributes[i] = attribute;
}
}
bool Line::has_only_one_background_color() const
{
if (!m_length)
return true;
// FIXME: Cache this result?
auto color = m_attributes[0].effective_background_color();
for (size_t i = 1; i < m_length; ++i) {
if (m_attributes[i].effective_background_color() != color)
return false;
}
return true;
}
void Line::convert_to_utf32()
{
ASSERT(!m_utf32);
auto* new_code_points = new u32[m_length];
for (size_t i = 0; i < m_length; ++i) {
new_code_points[i] = m_code_points.as_u8[i];
}
delete m_code_points.as_u8;
m_code_points.as_u32 = new_code_points;
m_utf32 = true;
}
}

View file

@ -0,0 +1,135 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Noncopyable.h>
#include <AK/String.h>
#include <LibVT/XtermColors.h>
namespace VT {
struct Attribute {
Attribute() { reset(); }
static const u32 default_foreground_color = xterm_colors[7];
static const u32 default_background_color = xterm_colors[0];
void reset()
{
foreground_color = default_foreground_color;
background_color = default_background_color;
flags = Flags::NoAttributes;
}
u32 foreground_color;
u32 background_color;
u32 effective_background_color() const { return flags & Negative ? foreground_color : background_color; }
u32 effective_foreground_color() const { return flags & Negative ? background_color : foreground_color; }
String href;
String href_id;
enum Flags : u8 {
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...
u8 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 Line {
AK_MAKE_NONCOPYABLE(Line);
AK_MAKE_NONMOVABLE(Line);
public:
explicit Line(u16 columns);
~Line();
void clear(Attribute);
bool has_only_one_background_color() const;
void set_length(u16);
u16 length() const { return m_length; }
u32 code_point(size_t index) const
{
if (m_utf32)
return m_code_points.as_u32[index];
return m_code_points.as_u8[index];
}
void set_code_point(size_t index, u32 code_point)
{
if (!m_utf32 && code_point & 0xffffff80u)
convert_to_utf32();
if (m_utf32)
m_code_points.as_u32[index] = code_point;
else
m_code_points.as_u8[index] = code_point;
}
bool is_dirty() const { return m_dirty; }
void set_dirty(bool b) { m_dirty = b; }
const Attribute* attributes() const { return m_attributes; }
Attribute* attributes() { return m_attributes; }
void convert_to_utf32();
bool is_utf32() const { return m_utf32; }
private:
union {
u8* as_u8;
u32* as_u32;
} m_code_points { nullptr };
Attribute* m_attributes { nullptr };
bool m_dirty { false };
bool m_utf32 { false };
u16 m_length { 0 };
};
}

View file

@ -0,0 +1,74 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
namespace VT {
class Position {
public:
Position() { }
Position(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 Position& other) const
{
return m_row < other.m_row || (m_row == other.m_row && m_column < other.m_column);
}
bool operator<=(const Position& other) const
{
return *this < other || *this == other;
}
bool operator>=(const Position& other) const
{
return !(*this < other);
}
bool operator==(const Position& other) const
{
return m_row == other.m_row && m_column == other.m_column;
}
bool operator!=(const Position& other) const
{
return !(*this == other);
}
private:
int m_row { -1 };
int m_column { -1 };
};
}

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibVT/Position.h>
namespace VT {
class Range {
public:
Range() { }
Range(const VT::Position& start, const VT::Position& end)
: m_start(start)
, m_end(end)
{
}
bool is_valid() const { return m_start.is_valid() && m_end.is_valid(); }
void clear()
{
m_start = {};
m_end = {};
}
VT::Position& start() { return m_start; }
VT::Position& end() { return m_end; }
const VT::Position& start() const { return m_start; }
const VT::Position& end() const { return m_end; }
Range normalized() const { return Range(normalized_start(), normalized_end()); }
void set_start(const VT::Position& position) { m_start = position; }
void set_end(const VT::Position& position) { m_end = position; }
void set(const VT::Position& start, const VT::Position& end)
{
m_start = start;
m_end = end;
}
bool operator==(const Range& other) const
{
return m_start == other.m_start && m_end == other.m_end;
}
bool contains(const VT::Position& position) const
{
if (!(position.row() > m_start.row() || (position.row() == m_start.row() && position.column() >= m_start.column())))
return false;
if (!(position.row() < m_end.row() || (position.row() == m_end.row() && position.column() <= m_end.column())))
return false;
return true;
}
private:
VT::Position normalized_start() const { return m_start < m_end ? m_start : m_end; }
VT::Position normalized_end() const { return m_start < m_end ? m_end : m_start; }
VT::Position m_start;
VT::Position m_end;
};
};

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,244 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Noncopyable.h>
#include <AK/NonnullOwnPtrVector.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <Kernel/API/KeyCode.h>
#include <LibVT/Line.h>
#include <LibVT/Position.h>
namespace VT {
class TerminalClient {
public:
virtual ~TerminalClient() { }
virtual void beep() = 0;
virtual void set_window_title(const StringView&) = 0;
virtual void set_window_progress(int value, int max) = 0;
virtual void terminal_did_resize(u16 columns, u16 rows) = 0;
virtual void terminal_history_changed() = 0;
virtual void emit(const u8*, size_t) = 0;
};
class Terminal {
public:
explicit Terminal(TerminalClient&);
~Terminal();
bool m_need_full_flush { false };
void invalidate_cursor();
void on_input(u8);
void clear();
void clear_including_history();
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; }
size_t line_count() const
{
return m_history.size() + m_lines.size();
}
Line& line(size_t index)
{
if (index < m_history.size())
return m_history[(m_history_start + index) % m_history.size()];
return m_lines[index - m_history.size()];
}
const Line& line(size_t index) const
{
return const_cast<Terminal*>(this)->line(index);
}
Line& visible_line(size_t index)
{
return m_lines[index];
}
const Line& visible_line(size_t index) const
{
return m_lines[index];
}
size_t max_history_size() const { return m_max_history_lines; }
void set_max_history_size(size_t value)
{
if (value == 0) {
m_max_history_lines = 0;
m_history_start = 0;
m_history.clear();
m_client.terminal_history_changed();
return;
}
if (m_max_history_lines > value) {
NonnullOwnPtrVector<Line> new_history;
new_history.ensure_capacity(value);
auto existing_line_count = min(m_history.size(), value);
for (size_t i = m_history.size() - existing_line_count; i < m_history.size(); ++i) {
auto j = (m_history_start + i) % m_history.size();
new_history.unchecked_append(move(static_cast<Vector<NonnullOwnPtr<Line>>&>(m_history).at(j)));
}
m_history = move(new_history);
m_history_start = 0;
m_client.terminal_history_changed();
}
m_max_history_lines = value;
}
size_t history_size() const { return m_history.size(); }
void inject_string(const StringView&);
void handle_key_press(KeyCode, u32, u8 flags);
Attribute attribute_at(const Position&) const;
private:
typedef Vector<unsigned, 4> ParamVector;
void on_code_point(u32);
void scroll_up();
void scroll_down();
void newline();
void set_cursor(unsigned row, unsigned column);
void put_character_at(unsigned row, unsigned column, u32 ch);
void set_window_title(const String&);
void unimplemented_escape();
void unimplemented_xterm_escape();
void emit_string(const StringView&);
void alter_mode(bool should_set, bool question_param, const ParamVector&);
void CUU(const ParamVector&);
void CUD(const ParamVector&);
void CUF(const ParamVector&);
void CUB(const ParamVector&);
void CUP(const ParamVector&);
void ED(const ParamVector&);
void EL(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 SGR(const ParamVector&);
void escape$s(const ParamVector&);
void escape$u(const ParamVector&);
void escape$t(const ParamVector&);
void DECSTBM(const ParamVector&);
void escape$S(const ParamVector&);
void escape$T(const ParamVector&);
void escape$L(const ParamVector&);
void RM(bool question_param, const ParamVector&);
void SM(bool question_param, const ParamVector&);
void DA(const ParamVector&);
void HVP(const ParamVector&);
void NEL();
void IND();
void RI();
void DSR(const ParamVector&);
void ICH(const ParamVector&);
TerminalClient& m_client;
size_t m_history_start = 0;
NonnullOwnPtrVector<Line> m_history;
void add_line_to_history(NonnullOwnPtr<Line>&& line)
{
if (max_history_size() == 0)
return;
if (m_history.size() < max_history_size()) {
ASSERT(m_history_start == 0);
m_history.append(move(line));
return;
}
m_history.ptr_at(m_history_start) = move(line);
m_history_start = (m_history_start + 1) % m_history.size();
}
NonnullOwnPtrVector<Line> m_lines;
size_t m_scroll_region_top { 0 };
size_t m_scroll_region_bottom { 0 };
u16 m_columns { 1 };
u16 m_rows { 1 };
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;
u32 m_next_href_id { 0 };
void execute_escape_sequence(u8 final);
void execute_xterm_command();
void execute_hashtag(u8);
enum ParserState {
Normal,
GotEscape,
ExpectParameter,
ExpectIntermediate,
ExpectFinal,
ExpectHashtagDigit,
ExpectXtermParameter,
ExpectStringTerminator,
UTF8Needs3Bytes,
UTF8Needs2Bytes,
UTF8Needs1Byte,
};
ParserState m_parser_state { Normal };
u32 m_parser_code_point { 0 };
Vector<u8> m_parameters;
Vector<u8> m_intermediates;
Vector<u8> m_xterm_parameters;
Vector<bool> m_horizontal_tabs;
u8 m_final { 0 };
u32 m_last_code_point { 0 };
size_t m_max_history_lines { 1024 };
};
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,226 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/String.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/ElapsedTimer.h>
#include <LibCore/Notifier.h>
#include <LibCore/Timer.h>
#include <LibGUI/Frame.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Rect.h>
#include <LibVT/Range.h>
#include <LibVT/Terminal.h>
class TerminalWidget final : public GUI::Frame
, public VT::TerminalClient {
C_OBJECT(TerminalWidget)
public:
TerminalWidget(int ptm_fd, bool automatic_size_policy, RefPtr<Core::ConfigFile> config);
virtual ~TerminalWidget() override;
void set_pty_master_fd(int fd);
void inject_string(const StringView& string)
{
m_terminal.inject_string(string);
flush_dirty_lines();
}
void create_window();
void flush_dirty_lines();
void force_repaint();
void apply_size_increments_to_window(GUI::Window&);
const Gfx::Font& bold_font() const { return *m_bold_font; }
void set_opacity(u8);
float opacity() { return m_opacity; };
enum class BellMode {
Visible,
AudibleBeep,
Disabled
};
BellMode bell_mode() { return m_bell_mode; }
void set_bell_mode(BellMode bm) { m_bell_mode = bm; };
RefPtr<Core::ConfigFile> config() const { return m_config; }
bool has_selection() const;
bool selection_contains(const VT::Position&) const;
String selected_text() const;
VT::Range normalized_selection() const { return m_selection.normalized(); }
void set_selection(const VT::Range& selection);
VT::Position buffer_position_at(const Gfx::IntPoint&) const;
VT::Range find_next(const StringView&, const VT::Position& start = {}, bool case_sensitivity = false, bool should_wrap = false);
VT::Range find_previous(const StringView&, const VT::Position& start = {}, bool case_sensitivity = false, bool should_wrap = false);
void scroll_to_bottom();
void scroll_to_row(int);
bool is_scrollable() const;
int scroll_length() const;
size_t max_history_size() const { return m_terminal.max_history_size(); }
void set_max_history_size(size_t value) { m_terminal.set_max_history_size(value); }
GUI::Action& copy_action() { return *m_copy_action; }
GUI::Action& paste_action() { return *m_paste_action; }
GUI::Action& clear_including_history_action() { return *m_clear_including_history_action; }
void copy();
void paste();
void clear_including_history();
Function<void(const StringView&)> on_title_change;
Function<void()> on_command_exit;
GUI::Menu& context_menu() { return *m_context_menu; }
void set_font_and_resize_to_fit(const Gfx::Font&);
private:
// ^GUI::Widget
virtual void event(Core::Event&) override;
virtual void paint_event(GUI::PaintEvent&) override;
virtual void resize_event(GUI::ResizeEvent&) override;
virtual void keydown_event(GUI::KeyEvent&) override;
virtual void keyup_event(GUI::KeyEvent&) override;
virtual void mousedown_event(GUI::MouseEvent&) override;
virtual void mouseup_event(GUI::MouseEvent&) override;
virtual void mousemove_event(GUI::MouseEvent&) override;
virtual void mousewheel_event(GUI::MouseEvent&) override;
virtual void doubleclick_event(GUI::MouseEvent&) override;
virtual void focusin_event(GUI::FocusEvent&) override;
virtual void focusout_event(GUI::FocusEvent&) override;
virtual void context_menu_event(GUI::ContextMenuEvent&) override;
virtual void drop_event(GUI::DropEvent&) override;
virtual void leave_event(Core::Event&) override;
virtual void did_change_font() override;
// ^TerminalClient
virtual void beep() override;
virtual void set_window_title(const StringView&) override;
virtual void set_window_progress(int value, int max) override;
virtual void terminal_did_resize(u16 columns, u16 rows) override;
virtual void terminal_history_changed() override;
virtual void emit(const u8*, size_t) override;
void set_logical_focus(bool);
Gfx::IntRect glyph_rect(u16 row, u16 column);
Gfx::IntRect row_rect(u16 row);
Gfx::IntSize widget_size_for_font(const Gfx::Font&) const;
void update_cursor();
void invalidate_cursor();
void relayout(const Gfx::IntSize&);
void update_copy_action();
void update_paste_action();
Gfx::IntSize compute_base_size() const;
int first_selection_column_on_row(int row) const;
int last_selection_column_on_row(int row) const;
u32 code_point_at(const VT::Position&) const;
VT::Position next_position_after(const VT::Position&, bool should_wrap) const;
VT::Position previous_position_before(const VT::Position&, bool should_wrap) const;
VT::Terminal m_terminal;
VT::Range m_selection;
String m_hovered_href;
String m_hovered_href_id;
String m_active_href;
String m_active_href_id;
// Snapshot of m_hovered_href when opening a context menu for a hyperlink.
String m_context_menu_href;
BellMode m_bell_mode { BellMode::Visible };
bool m_belling { false };
bool m_alt_key_held { false };
bool m_rectangle_selection { false };
int m_pixel_width { 0 };
int m_pixel_height { 0 };
int m_inset { 2 };
int m_line_spacing { 4 };
int m_line_height { 0 };
int m_ptm_fd { -1 };
bool m_has_logical_focus { false };
RefPtr<Core::Notifier> m_notifier;
u8 m_opacity { 255 };
bool m_needs_background_fill { true };
bool m_cursor_blink_state { true };
bool m_automatic_size_policy { false };
RefPtr<Gfx::Font> m_bold_font;
int m_glyph_width { 0 };
enum class AutoScrollDirection {
None,
Up,
Down
};
AutoScrollDirection m_auto_scroll_direction { AutoScrollDirection::None };
RefPtr<Core::Timer> m_cursor_blink_timer;
RefPtr<Core::Timer> m_visual_beep_timer;
RefPtr<Core::Timer> m_auto_scroll_timer;
RefPtr<Core::ConfigFile> m_config;
RefPtr<GUI::ScrollBar> m_scrollbar;
RefPtr<GUI::Action> m_copy_action;
RefPtr<GUI::Action> m_paste_action;
RefPtr<GUI::Action> m_clear_including_history_action;
RefPtr<GUI::Menu> m_context_menu;
RefPtr<GUI::Menu> m_context_menu_for_hyperlink;
Core::ElapsedTimer m_triple_click_timer;
Gfx::IntPoint m_left_mousedown_position;
};

View file

@ -0,0 +1,286 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
static constexpr unsigned xterm_colors[256] = {
0x000000,
0xcc0000,
0x3e9a06,
0xc4a000,
0x3465a4,
0x75507b,
0x06989a,
0xeeeeec,
0x555753,
0xef2929,
0x8ae234,
0xfce94f,
0x729fcf,
0xad7fa8,
0x34e2e2,
0xFFFFFF,
0x000000,
0x00005f,
0x000087,
0x0000af,
0x0000d7,
0x0000ff,
0x005f00,
0x005f5f,
0x005f87,
0x005faf,
0x005fd7,
0x005fff,
0x008700,
0x00875f,
0x008787,
0x0087af,
0x0087d7,
0x0087ff,
0x00af00,
0x00af5f,
0x00af87,
0x00afaf,
0x00afd7,
0x00afff,
0x00d700,
0x00d75f,
0x00d787,
0x00d7af,
0x00d7d7,
0x00d7ff,
0x00ff00,
0x00ff5f,
0x00ff87,
0x00ffaf,
0x00ffd7,
0x00ffff,
0x5f0000,
0x5f005f,
0x5f0087,
0x5f00af,
0x5f00d7,
0x5f00ff,
0x5f5f00,
0x5f5f5f,
0x5f5f87,
0x5f5faf,
0x5f5fd7,
0x5f5fff,
0x5f8700,
0x5f875f,
0x5f8787,
0x5f87af,
0x5f87d7,
0x5f87ff,
0x5faf00,
0x5faf5f,
0x5faf87,
0x5fafaf,
0x5fafd7,
0x5fafff,
0x5fd700,
0x5fd75f,
0x5fd787,
0x5fd7af,
0x5fd7d7,
0x5fd7ff,
0x5fff00,
0x5fff5f,
0x5fff87,
0x5fffaf,
0x5fffd7,
0x5fffff,
0x870000,
0x87005f,
0x870087,
0x8700af,
0x8700d7,
0x8700ff,
0x875f00,
0x875f5f,
0x875f87,
0x875faf,
0x875fd7,
0x875fff,
0x878700,
0x87875f,
0x878787,
0x8787af,
0x8787d7,
0x8787ff,
0x87af00,
0x87af5f,
0x87af87,
0x87afaf,
0x87afd7,
0x87afff,
0x87d700,
0x87d75f,
0x87d787,
0x87d7af,
0x87d7d7,
0x87d7ff,
0x87ff00,
0x87ff5f,
0x87ff87,
0x87ffaf,
0x87ffd7,
0x87ffff,
0xaf0000,
0xaf005f,
0xaf0087,
0xaf00af,
0xaf00d7,
0xaf00ff,
0xaf5f00,
0xaf5f5f,
0xaf5f87,
0xaf5faf,
0xaf5fd7,
0xaf5fff,
0xaf8700,
0xaf875f,
0xaf8787,
0xaf87af,
0xaf87d7,
0xaf87ff,
0xafaf00,
0xafaf5f,
0xafaf87,
0xafafaf,
0xafafd7,
0xafafff,
0xafd700,
0xafd75f,
0xafd787,
0xafd7af,
0xafd7d7,
0xafd7ff,
0xafff00,
0xafff5f,
0xafff87,
0xafffaf,
0xafffd7,
0xafffff,
0xd70000,
0xd7005f,
0xd70087,
0xd700af,
0xd700d7,
0xd700ff,
0xd75f00,
0xd75f5f,
0xd75f87,
0xd75faf,
0xd75fd7,
0xd75fff,
0xd78700,
0xd7875f,
0xd78787,
0xd787af,
0xd787d7,
0xd787ff,
0xd7af00,
0xd7af5f,
0xd7af87,
0xd7afaf,
0xd7afd7,
0xd7afff,
0xd7d700,
0xd7d75f,
0xd7d787,
0xd7d7af,
0xd7d7d7,
0xd7d7ff,
0xd7ff00,
0xd7ff5f,
0xd7ff87,
0xd7ffaf,
0xd7ffd7,
0xd7ffff,
0xff0000,
0xff005f,
0xff0087,
0xff00af,
0xff00d7,
0xff00ff,
0xff5f00,
0xff5f5f,
0xff5f87,
0xff5faf,
0xff5fd7,
0xff5fff,
0xff8700,
0xff875f,
0xff8787,
0xff87af,
0xff87d7,
0xff87ff,
0xffaf00,
0xffaf5f,
0xffaf87,
0xffafaf,
0xffafd7,
0xffafff,
0xffd700,
0xffd75f,
0xffd787,
0xffd7af,
0xffd7d7,
0xffd7ff,
0xffff00,
0xffff5f,
0xffff87,
0xffffaf,
0xffffd7,
0xffffff,
0x080808,
0x121212,
0x1c1c1c,
0x262626,
0x303030,
0x3a3a3a,
0x444444,
0x4e4e4e,
0x585858,
0x626262,
0x6c6c6c,
0x767676,
0x808080,
0x8a8a8a,
0x949494,
0x9e9e9e,
0xa8a8a8,
0xb2b2b2,
0xbcbcbc,
0xc6c6c6,
0xd0d0d0,
0xdadada,
0xe4e4e4,
0xeeeeee,
};