2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-02-15 19:58:18 +01:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2022-03-04 13:27:47 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-01-15 04:30:55 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2020-02-15 00:24:14 +01:00
|
|
|
#include <LibCore/ElapsedTimer.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/Notifier.h>
|
|
|
|
|
#include <LibCore/Timer.h>
|
2021-07-27 03:19:56 +08:00
|
|
|
#include <LibGUI/Clipboard.h>
|
2020-02-15 00:24:14 +01:00
|
|
|
#include <LibGUI/Frame.h>
|
2020-02-06 12:07:05 +01:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2020-02-06 12:04:00 +01:00
|
|
|
#include <LibGfx/Rect.h>
|
2021-05-27 19:19:30 +02:00
|
|
|
#include <LibVT/Color.h>
|
2020-12-28 02:24:37 +02:00
|
|
|
#include <LibVT/Range.h>
|
2019-08-12 17:32:16 +02:00
|
|
|
#include <LibVT/Terminal.h>
|
2019-01-15 04:30:55 +01:00
|
|
|
|
2021-02-27 17:49:08 +01:00
|
|
|
namespace VT {
|
|
|
|
|
|
2021-02-15 19:58:18 +01:00
|
|
|
class TerminalWidget final
|
|
|
|
|
: public GUI::Frame
|
2021-07-27 03:19:56 +08:00
|
|
|
, public VT::TerminalClient
|
|
|
|
|
, public GUI::Clipboard::ClipboardClient {
|
2021-02-15 19:58:18 +01:00
|
|
|
C_OBJECT(TerminalWidget);
|
|
|
|
|
|
2019-01-15 04:30:55 +01:00
|
|
|
public:
|
2022-03-04 13:27:47 -07:00
|
|
|
virtual ~TerminalWidget() override = default;
|
2019-01-15 04:30:55 +01:00
|
|
|
|
2019-10-22 21:57:53 +02:00
|
|
|
void set_pty_master_fd(int fd);
|
2021-11-11 00:55:02 +01:00
|
|
|
void inject_string(StringView string)
|
2019-10-22 22:14:36 +02:00
|
|
|
{
|
|
|
|
|
m_terminal.inject_string(string);
|
|
|
|
|
flush_dirty_lines();
|
|
|
|
|
}
|
2019-10-22 21:57:53 +02:00
|
|
|
|
2019-02-10 14:28:39 +01:00
|
|
|
void flush_dirty_lines();
|
2019-01-17 17:38:04 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void apply_size_increments_to_window(GUI::Window&);
|
2019-02-21 00:21:23 +01:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
void set_opacity(u8);
|
2023-07-07 22:48:11 -04:00
|
|
|
float opacity() { return m_opacity; }
|
2020-12-19 21:47:45 +00:00
|
|
|
|
2022-01-14 20:38:30 -08:00
|
|
|
void set_show_scrollbar(bool);
|
|
|
|
|
|
2020-12-19 21:47:45 +00:00
|
|
|
enum class BellMode {
|
|
|
|
|
Visible,
|
|
|
|
|
AudibleBeep,
|
|
|
|
|
Disabled
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BellMode bell_mode() { return m_bell_mode; }
|
2023-07-07 22:48:11 -04:00
|
|
|
void set_bell_mode(BellMode bm) { m_bell_mode = bm; }
|
2019-04-29 19:24:18 +02:00
|
|
|
|
2024-02-06 01:41:35 +03:30
|
|
|
enum class AutoMarkMode {
|
|
|
|
|
MarkNothing,
|
|
|
|
|
MarkInteractiveShellPrompt,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AutoMarkMode auto_mark_mode() { return m_auto_mark_mode; }
|
|
|
|
|
void set_auto_mark_mode(AutoMarkMode am) { m_auto_mark_mode = am; }
|
|
|
|
|
|
2019-06-23 09:18:17 +02:00
|
|
|
bool has_selection() const;
|
2019-08-13 12:48:54 +02:00
|
|
|
bool selection_contains(const VT::Position&) const;
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString selected_text() const;
|
2020-12-28 02:24:37 +02:00
|
|
|
VT::Range normalized_selection() const { return m_selection.normalized(); }
|
|
|
|
|
void set_selection(const VT::Range& selection);
|
2022-12-06 20:27:44 +00:00
|
|
|
VT::Position buffer_position_at(Gfx::IntPoint) const;
|
2019-06-23 09:18:17 +02:00
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
VT::Range find_next(StringView, const VT::Position& start = {}, bool case_sensitivity = false, bool should_wrap = false);
|
|
|
|
|
VT::Range find_previous(StringView, const VT::Position& start = {}, bool case_sensitivity = false, bool should_wrap = false);
|
2020-12-28 11:23:38 +02:00
|
|
|
|
2020-11-10 11:47:19 +01:00
|
|
|
void scroll_to_bottom();
|
2020-12-28 11:23:38 +02:00
|
|
|
void scroll_to_row(int);
|
2020-11-10 11:47:19 +01:00
|
|
|
|
2019-08-20 20:11:56 +02:00
|
|
|
bool is_scrollable() const;
|
2020-06-29 22:28:18 +02:00
|
|
|
int scroll_length() const;
|
2019-08-20 20:11:56 +02:00
|
|
|
|
2020-11-29 17:56:02 +03:30
|
|
|
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); }
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
GUI::Action& copy_action() { return *m_copy_action; }
|
|
|
|
|
GUI::Action& paste_action() { return *m_paste_action; }
|
2020-07-05 23:34:02 +02:00
|
|
|
GUI::Action& clear_including_history_action() { return *m_clear_including_history_action; }
|
2024-02-06 01:41:35 +03:30
|
|
|
GUI::Action& clear_to_previous_mark_action() { return *m_clear_to_previous_mark_action; }
|
2019-11-20 21:33:23 +01:00
|
|
|
|
|
|
|
|
void copy();
|
|
|
|
|
void paste();
|
2020-07-05 23:34:02 +02:00
|
|
|
void clear_including_history();
|
2024-02-06 01:41:35 +03:30
|
|
|
void clear_to_previous_mark();
|
|
|
|
|
|
|
|
|
|
void set_startup_process_id(pid_t pid) { m_startup_process_id = pid; }
|
2019-11-20 21:33:23 +01:00
|
|
|
|
2021-05-28 21:26:39 +02:00
|
|
|
const StringView color_scheme_name() const { return m_color_scheme_name; }
|
|
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
Function<void(StringView)> on_title_change;
|
2022-12-06 21:35:32 +00:00
|
|
|
Function<void(Gfx::IntSize)> on_terminal_size_change;
|
2019-10-22 21:57:53 +02:00
|
|
|
Function<void()> on_command_exit;
|
2019-10-21 22:07:59 +02:00
|
|
|
|
2020-12-30 13:42:16 +01:00
|
|
|
GUI::Menu& context_menu() { return *m_context_menu; }
|
|
|
|
|
|
2021-06-03 17:26:25 +02:00
|
|
|
constexpr Gfx::Color terminal_color_to_rgb(VT::Color) const;
|
2021-05-27 19:19:30 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void set_font_and_resize_to_fit(Gfx::Font const&);
|
2021-01-09 13:46:22 +01:00
|
|
|
|
2022-12-02 18:16:58 +02:00
|
|
|
void update_color_scheme();
|
2021-05-28 21:26:39 +02:00
|
|
|
|
2022-11-17 09:23:09 -05:00
|
|
|
void set_logical_focus(bool);
|
|
|
|
|
|
2022-05-12 22:52:14 +02:00
|
|
|
VT::CursorShape cursor_shape() { return m_cursor_shape; }
|
|
|
|
|
virtual void set_cursor_blinking(bool) override;
|
|
|
|
|
virtual void set_cursor_shape(CursorShape) override;
|
|
|
|
|
|
|
|
|
|
static Optional<VT::CursorShape> parse_cursor_shape(StringView);
|
2024-02-07 06:05:16 +00:00
|
|
|
static Optional<BellMode> parse_bell(StringView);
|
|
|
|
|
static Optional<AutoMarkMode> parse_automark_mode(StringView);
|
2023-12-16 17:49:34 +03:30
|
|
|
static ByteString stringify_cursor_shape(VT::CursorShape);
|
2024-02-07 06:05:16 +00:00
|
|
|
static ByteString stringify_bell(BellMode);
|
|
|
|
|
static ByteString stringify_automark_mode(AutoMarkMode);
|
2022-05-12 22:52:14 +02:00
|
|
|
|
2019-01-15 04:30:55 +01:00
|
|
|
private:
|
2021-10-31 23:38:04 +01:00
|
|
|
TerminalWidget(int ptm_fd, bool automatic_size_policy);
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
// ^GUI::Widget
|
2020-02-02 12:34:39 +01:00
|
|
|
virtual void event(Core::Event&) override;
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual void paint_event(GUI::PaintEvent&) override;
|
|
|
|
|
virtual void resize_event(GUI::ResizeEvent&) override;
|
|
|
|
|
virtual void keydown_event(GUI::KeyEvent&) override;
|
2020-03-05 22:34:31 -06:00
|
|
|
virtual void keyup_event(GUI::KeyEvent&) override;
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual void mousedown_event(GUI::MouseEvent&) override;
|
2020-05-10 15:57:45 +02:00
|
|
|
virtual void mouseup_event(GUI::MouseEvent&) override;
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual void mousemove_event(GUI::MouseEvent&) override;
|
|
|
|
|
virtual void mousewheel_event(GUI::MouseEvent&) override;
|
|
|
|
|
virtual void doubleclick_event(GUI::MouseEvent&) override;
|
2020-08-14 19:56:40 +02:00
|
|
|
virtual void focusin_event(GUI::FocusEvent&) override;
|
|
|
|
|
virtual void focusout_event(GUI::FocusEvent&) override;
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual void context_menu_event(GUI::ContextMenuEvent&) override;
|
2022-06-20 11:57:26 +02:00
|
|
|
virtual void drag_enter_event(GUI::DragEvent&) override;
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual void drop_event(GUI::DropEvent&) override;
|
2020-05-09 17:04:12 +02:00
|
|
|
virtual void leave_event(Core::Event&) override;
|
2020-01-08 21:09:43 +01:00
|
|
|
virtual void did_change_font() override;
|
2019-02-10 14:28:39 +01:00
|
|
|
|
2019-08-12 17:32:16 +02:00
|
|
|
// ^TerminalClient
|
|
|
|
|
virtual void beep() override;
|
2021-11-11 00:55:02 +01:00
|
|
|
virtual void set_window_title(StringView) override;
|
2020-05-30 22:11:35 +02:00
|
|
|
virtual void set_window_progress(int value, int max) override;
|
2019-08-12 17:32:16 +02:00
|
|
|
virtual void terminal_did_resize(u16 columns, u16 rows) override;
|
2021-06-05 16:46:33 +02:00
|
|
|
virtual void terminal_history_changed(int delta) override;
|
2024-02-06 01:41:35 +03:30
|
|
|
virtual void terminal_did_perform_possibly_partial_clear() override;
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual void emit(u8 const*, size_t) override;
|
2019-01-15 04:30:55 +01:00
|
|
|
|
2021-07-27 03:19:56 +08:00
|
|
|
// ^GUI::Clipboard::ClipboardClient
|
2023-12-16 17:49:34 +03:30
|
|
|
virtual void clipboard_content_did_change(ByteString const&) override { update_paste_action(); }
|
2021-07-27 03:19:56 +08:00
|
|
|
|
2021-11-11 01:06:34 +01:00
|
|
|
void send_non_user_input(ReadonlyBytes);
|
2021-05-24 12:01:59 +02:00
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntRect glyph_rect(u16 row, u16 column);
|
|
|
|
|
Gfx::IntRect row_rect(u16 row);
|
2019-05-29 21:10:08 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Gfx::IntSize widget_size_for_font(Gfx::Font const&) const;
|
2021-01-09 13:46:22 +01:00
|
|
|
|
2019-08-12 17:32:16 +02:00
|
|
|
void update_cursor();
|
|
|
|
|
void invalidate_cursor();
|
2019-01-15 04:30:55 +01:00
|
|
|
|
2022-12-06 21:35:32 +00:00
|
|
|
void relayout(Gfx::IntSize);
|
2020-01-08 21:09:43 +01:00
|
|
|
|
2020-12-06 20:56:23 +00:00
|
|
|
void update_copy_action();
|
|
|
|
|
void update_paste_action();
|
|
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntSize compute_base_size() const;
|
2019-08-25 11:20:32 -05:00
|
|
|
int first_selection_column_on_row(int row) const;
|
|
|
|
|
int last_selection_column_on_row(int row) const;
|
2019-08-19 19:12:34 +02:00
|
|
|
|
2020-12-28 11:23:38 +02:00
|
|
|
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;
|
|
|
|
|
|
2023-01-05 13:12:50 +01:00
|
|
|
void update_cached_font_metrics();
|
|
|
|
|
|
2024-02-06 01:41:35 +03:30
|
|
|
void handle_pty_owner_change(pid_t new_owner);
|
|
|
|
|
|
2019-08-12 17:32:16 +02:00
|
|
|
VT::Terminal m_terminal;
|
2019-01-15 04:30:55 +01:00
|
|
|
|
2020-12-28 02:24:37 +02:00
|
|
|
VT::Range m_selection;
|
2019-01-15 07:30:24 +01:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString m_hovered_href;
|
|
|
|
|
Optional<ByteString> m_hovered_href_id;
|
2020-05-09 16:16:16 +02:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString m_active_href;
|
|
|
|
|
Optional<ByteString> m_active_href_id;
|
2020-05-10 15:57:45 +02:00
|
|
|
|
2020-05-09 18:45:45 +02:00
|
|
|
// Snapshot of m_hovered_href when opening a context menu for a hyperlink.
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString m_context_menu_href;
|
2020-05-09 18:45:45 +02:00
|
|
|
|
2022-12-02 18:16:58 +02:00
|
|
|
Gfx::Color m_colors[256];
|
2021-05-28 21:26:39 +02:00
|
|
|
Gfx::Color m_default_foreground_color;
|
|
|
|
|
Gfx::Color m_default_background_color;
|
2021-06-03 17:26:25 +02:00
|
|
|
bool m_show_bold_text_as_bright { true };
|
2021-05-28 21:26:39 +02:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString m_color_scheme_name;
|
2021-05-28 21:26:39 +02:00
|
|
|
|
2024-02-06 01:41:35 +03:30
|
|
|
AutoMarkMode m_auto_mark_mode { AutoMarkMode::MarkInteractiveShellPrompt };
|
|
|
|
|
|
2020-12-19 21:47:45 +00:00
|
|
|
BellMode m_bell_mode { BellMode::Visible };
|
2020-03-05 22:34:31 -06:00
|
|
|
bool m_alt_key_held { false };
|
|
|
|
|
bool m_rectangle_selection { false };
|
2019-01-15 04:30:55 +01:00
|
|
|
|
|
|
|
|
int m_pixel_width { 0 };
|
|
|
|
|
int m_pixel_height { 0 };
|
2019-01-15 07:39:51 +01:00
|
|
|
|
|
|
|
|
int m_inset { 2 };
|
|
|
|
|
int m_line_spacing { 4 };
|
2019-01-17 16:19:49 +01:00
|
|
|
int m_line_height { 0 };
|
2023-01-05 13:33:35 +01:00
|
|
|
int m_cell_height { 0 };
|
2023-01-05 13:12:50 +01:00
|
|
|
int m_column_width { 0 };
|
2019-01-17 16:19:49 +01:00
|
|
|
|
2019-02-10 14:28:39 +01:00
|
|
|
int m_ptm_fd { -1 };
|
|
|
|
|
|
2019-10-21 20:14:51 +02:00
|
|
|
bool m_has_logical_focus { false };
|
2021-02-21 16:49:36 +01:00
|
|
|
bool m_in_relayout { false };
|
2019-01-17 17:38:04 +01:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
RefPtr<Core::Notifier> m_notifier;
|
2019-02-19 01:42:53 +01:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
u8 m_opacity { 255 };
|
2019-03-30 21:40:27 +01:00
|
|
|
bool m_cursor_blink_state { true };
|
2019-10-21 20:28:30 +02:00
|
|
|
bool m_automatic_size_policy { false };
|
2019-03-06 11:03:10 +01:00
|
|
|
|
2022-05-12 22:52:14 +02:00
|
|
|
VT::CursorShape m_cursor_shape { VT::CursorShape::Block };
|
|
|
|
|
bool m_cursor_is_blinking_set { true };
|
2021-05-24 09:36:41 +02:00
|
|
|
|
2020-12-03 22:18:28 +01:00
|
|
|
enum class AutoScrollDirection {
|
|
|
|
|
None,
|
|
|
|
|
Up,
|
|
|
|
|
Down
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-25 14:09:20 +01:00
|
|
|
void set_auto_scroll_direction(AutoScrollDirection);
|
|
|
|
|
|
2020-12-03 22:18:28 +01:00
|
|
|
AutoScrollDirection m_auto_scroll_direction { AutoScrollDirection::None };
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
RefPtr<Core::Timer> m_cursor_blink_timer;
|
|
|
|
|
RefPtr<Core::Timer> m_visual_beep_timer;
|
2020-12-03 22:18:28 +01:00
|
|
|
RefPtr<Core::Timer> m_auto_scroll_timer;
|
2019-08-19 19:12:34 +02:00
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
RefPtr<GUI::Scrollbar> m_scrollbar;
|
2019-11-15 23:48:58 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
RefPtr<GUI::Action> m_copy_action;
|
|
|
|
|
RefPtr<GUI::Action> m_paste_action;
|
2020-07-05 23:34:02 +02:00
|
|
|
RefPtr<GUI::Action> m_clear_including_history_action;
|
2024-02-06 01:41:35 +03:30
|
|
|
RefPtr<GUI::Action> m_clear_to_previous_mark_action;
|
2019-11-20 21:33:23 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
RefPtr<GUI::Menu> m_context_menu;
|
2020-05-09 16:42:42 +02:00
|
|
|
RefPtr<GUI::Menu> m_context_menu_for_hyperlink;
|
2019-11-20 21:39:26 +01:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
Core::ElapsedTimer m_triple_click_timer;
|
2020-05-10 17:39:11 +02:00
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntPoint m_left_mousedown_position;
|
2021-08-28 21:54:15 -04:00
|
|
|
VT::Position m_left_mousedown_position_buffer;
|
2024-02-06 01:41:35 +03:30
|
|
|
|
|
|
|
|
bool m_startup_process_owns_pty { false };
|
|
|
|
|
pid_t m_startup_process_id { -1 };
|
2019-01-15 04:30:55 +01:00
|
|
|
};
|
2021-02-27 17:49:08 +01:00
|
|
|
|
|
|
|
|
}
|