Terminal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Noncopyable.h>
  10. #include <AK/Vector.h>
  11. #include <Kernel/API/KeyCode.h>
  12. #include <LibVT/CharacterSet.h>
  13. #include <LibVT/EscapeSequenceParser.h>
  14. #include <LibVT/Position.h>
  15. #ifndef KERNEL
  16. # include <AK/ByteString.h>
  17. # include <AK/HashTable.h>
  18. # include <LibVT/Attribute.h>
  19. # include <LibVT/Line.h>
  20. #else
  21. namespace Kernel {
  22. class VirtualConsole;
  23. }
  24. # include <LibVT/Attribute.h>
  25. #endif
  26. namespace VT {
  27. enum class CursorShape {
  28. None,
  29. Block,
  30. Underline,
  31. Bar,
  32. };
  33. enum CursorKeysMode {
  34. Application,
  35. Cursor,
  36. };
  37. class TerminalClient {
  38. public:
  39. virtual ~TerminalClient() = default;
  40. virtual void beep() = 0;
  41. virtual void set_window_title(StringView) = 0;
  42. virtual void set_window_progress(int value, int max) = 0;
  43. virtual void terminal_did_resize(u16 columns, u16 rows) = 0;
  44. virtual void terminal_history_changed(int delta) = 0;
  45. virtual void terminal_did_perform_possibly_partial_clear() = 0;
  46. virtual void emit(u8 const*, size_t) = 0;
  47. virtual void set_cursor_shape(CursorShape) = 0;
  48. virtual void set_cursor_blinking(bool) = 0;
  49. };
  50. class Terminal : public EscapeSequenceExecutor {
  51. public:
  52. #ifndef KERNEL
  53. explicit Terminal(TerminalClient&);
  54. #else
  55. explicit Terminal(Kernel::VirtualConsole&);
  56. #endif
  57. virtual ~Terminal()
  58. {
  59. }
  60. bool m_need_full_flush { false };
  61. #ifndef KERNEL
  62. void invalidate_cursor();
  63. #else
  64. virtual void invalidate_cursor() = 0;
  65. #endif
  66. void on_input(u8);
  67. void set_cursor(unsigned row, unsigned column, bool skip_debug = false);
  68. void clear_including_history()
  69. {
  70. clear_history();
  71. clear();
  72. }
  73. #ifndef KERNEL
  74. void mark_cursor();
  75. OrderedHashTable<Mark> const& marks() const { return m_valid_marks; }
  76. void clear();
  77. void clear_history();
  78. void clear_to_mark(Mark);
  79. #else
  80. virtual void clear() = 0;
  81. virtual void clear_history() = 0;
  82. #endif
  83. #ifndef KERNEL
  84. void set_size(u16 columns, u16 rows);
  85. #else
  86. virtual void set_size(u16 columns, u16 rows) = 0;
  87. #endif
  88. u16 columns() const
  89. {
  90. return m_columns;
  91. }
  92. u16 rows() const { return m_rows; }
  93. u16 cursor_column() const { return m_current_state.cursor.column; }
  94. u16 cursor_row() const { return m_current_state.cursor.row; }
  95. #ifndef KERNEL
  96. size_t line_count() const
  97. {
  98. if (m_use_alternate_screen_buffer)
  99. return m_alternate_screen_buffer.size();
  100. else
  101. return m_history.size() + m_normal_screen_buffer.size();
  102. }
  103. Line& line(size_t index)
  104. {
  105. if (m_use_alternate_screen_buffer) {
  106. return *m_alternate_screen_buffer[index];
  107. } else {
  108. if (index < m_history.size())
  109. return *m_history[(m_history_start + index) % m_history.size()];
  110. return *m_normal_screen_buffer[index - m_history.size()];
  111. }
  112. }
  113. Line const& line(size_t index) const
  114. {
  115. return const_cast<Terminal*>(this)->line(index);
  116. }
  117. Line& visible_line(size_t index)
  118. {
  119. return *active_buffer()[index];
  120. }
  121. Line const& visible_line(size_t index) const
  122. {
  123. return *active_buffer()[index];
  124. }
  125. size_t max_history_size() const { return m_max_history_lines; }
  126. void set_max_history_size(size_t value)
  127. {
  128. if (value == 0) {
  129. auto previous_size = m_history.size();
  130. m_max_history_lines = 0;
  131. m_history_start = 0;
  132. m_history.clear();
  133. m_client.terminal_history_changed(-previous_size);
  134. return;
  135. }
  136. if (m_max_history_lines > value) {
  137. Vector<NonnullOwnPtr<Line>> new_history;
  138. new_history.ensure_capacity(value);
  139. auto existing_line_count = min(m_history.size(), value);
  140. for (size_t i = m_history.size() - existing_line_count; i < m_history.size(); ++i) {
  141. auto j = (m_history_start + i) % m_history.size();
  142. new_history.unchecked_append(move(static_cast<Vector<NonnullOwnPtr<Line>>&>(m_history).at(j)));
  143. }
  144. m_history = move(new_history);
  145. m_history_start = 0;
  146. m_client.terminal_history_changed(value - existing_line_count);
  147. }
  148. m_max_history_lines = value;
  149. }
  150. size_t history_size() const { return m_use_alternate_screen_buffer ? 0 : m_history.size(); }
  151. #endif
  152. void inject_string(StringView);
  153. void handle_key_press(KeyCode, u32, u8 flags);
  154. #ifndef KERNEL
  155. Attribute attribute_at(Position const&) const;
  156. #endif
  157. bool needs_bracketed_paste() const
  158. {
  159. return m_needs_bracketed_paste;
  160. }
  161. bool is_within_scroll_region(u16 line) const
  162. {
  163. return line >= m_scroll_region_top && line <= m_scroll_region_bottom;
  164. }
  165. protected:
  166. // ^EscapeSequenceExecutor
  167. virtual void emit_code_point(u32) override;
  168. virtual void execute_control_code(u8) override;
  169. virtual void execute_escape_sequence(Intermediates intermediates, bool ignore, u8 last_byte) override;
  170. virtual void execute_csi_sequence(Parameters parameters, Intermediates intermediates, bool ignore, u8 last_byte) override;
  171. virtual void execute_osc_sequence(OscParameters parameters, u8 last_byte) override;
  172. virtual void dcs_hook(Parameters parameters, Intermediates intermediates, bool ignore, u8 last_byte) override;
  173. virtual void receive_dcs_char(u8 byte) override;
  174. virtual void execute_dcs_sequence() override;
  175. struct BufferState {
  176. Attribute attribute;
  177. CursorPosition cursor;
  178. };
  179. void carriage_return();
  180. inline void scroll_up(size_t count = 1);
  181. inline void scroll_down(size_t count = 1);
  182. void linefeed();
  183. #ifndef KERNEL
  184. void scroll_up(u16 region_top, u16 region_bottom, size_t count);
  185. void scroll_down(u16 region_top, u16 region_bottom, size_t count);
  186. void scroll_left(u16 row, u16 column, size_t count);
  187. void scroll_right(u16 row, u16 column, size_t count);
  188. void put_character_at(unsigned row, unsigned column, u32 ch);
  189. void clear_in_line(u16 row, u16 first_column, u16 last_column);
  190. #else
  191. virtual void scroll_up(u16 region_top, u16 region_bottom, size_t count) = 0;
  192. virtual void scroll_down(u16 region_top, u16 region_bottom, size_t count) = 0;
  193. virtual void scroll_left(u16 row, u16 column, size_t count) = 0;
  194. virtual void scroll_right(u16 row, u16 column, size_t count) = 0;
  195. virtual void put_character_at(unsigned row, unsigned column, u32 ch) = 0;
  196. virtual void clear_in_line(u16 row, u16 first_column, u16 last_column) = 0;
  197. #endif
  198. void unimplemented_control_code(u8);
  199. void unimplemented_escape_sequence(Intermediates, u8 last_byte);
  200. void unimplemented_csi_sequence(Parameters, Intermediates, u8 last_byte);
  201. void unimplemented_osc_sequence(OscParameters, u8 last_byte);
  202. void emit_string(StringView);
  203. void alter_ansi_mode(bool should_set, Parameters);
  204. void alter_private_mode(bool should_set, Parameters);
  205. // CUU – Cursor Up
  206. void CUU(Parameters);
  207. // CUD – Cursor Down
  208. void CUD(Parameters);
  209. // CUF – Cursor Forward
  210. void CUF(Parameters);
  211. // CUB – Cursor Backward
  212. void CUB(Parameters);
  213. // CNL - Cursor Next Line
  214. void CNL(Parameters);
  215. // CPL - Cursor Previous Line
  216. void CPL(Parameters);
  217. // CUP - Cursor Position
  218. void CUP(Parameters);
  219. // ED - Erase in Display
  220. void ED(Parameters);
  221. // EL - Erase in Line
  222. void EL(Parameters);
  223. // SGR – Select Graphic Rendition
  224. void SGR(Parameters);
  225. // Save Current Cursor Position
  226. void SCOSC();
  227. // Restore Saved Cursor Position
  228. void SCORC();
  229. // Save Cursor (and other attributes)
  230. void DECSC();
  231. // Restore Cursor (and other attributes)
  232. void DECRC();
  233. // DECSTBM – Set Top and Bottom Margins ("Scrolling Region")
  234. void DECSTBM(Parameters);
  235. // RM – Reset Mode
  236. void RM(Parameters);
  237. // DECRST - DEC Private Mode Reset
  238. void DECRST(Parameters);
  239. // SM – Set Mode
  240. void SM(Parameters);
  241. // DECSET - Dec Private Mode Set
  242. void DECSET(Parameters);
  243. // DA - Device Attributes
  244. void DA(Parameters);
  245. // HVP – Horizontal and Vertical Position
  246. void HVP(Parameters);
  247. // NEL - Next Line
  248. void NEL();
  249. // IND - Index (move down)
  250. void IND();
  251. // RI - Reverse Index (move up)
  252. void RI();
  253. // DECBI - Back Index
  254. void DECBI();
  255. // DECFI - Forward Index
  256. void DECFI();
  257. // DSR - Device Status Reports
  258. void DSR(Parameters);
  259. // DECSCUSR - Set Cursor Style
  260. void DECSCUSR(Parameters);
  261. // ICH - Insert Character
  262. void ICH(Parameters);
  263. // SU - Scroll Up (called "Pan Down" in VT510)
  264. void SU(Parameters);
  265. // SD - Scroll Down (called "Pan Up" in VT510)
  266. void SD(Parameters);
  267. // IL - Insert Line
  268. void IL(Parameters);
  269. // DCH - Delete Character
  270. void DCH(Parameters);
  271. // DL - Delete Line
  272. void DL(Parameters);
  273. // CHA - Cursor Horizontal Absolute
  274. void CHA(Parameters);
  275. // REP - Repeat
  276. void REP(Parameters);
  277. // VPA - Line Position Absolute
  278. void VPA(Parameters);
  279. // VPR - Line Position Relative
  280. void VPR(Parameters);
  281. // HPA - Character Position Absolute
  282. void HPA(Parameters);
  283. // HPR - Character Position Relative
  284. void HPR(Parameters);
  285. // ECH - Erase Character
  286. void ECH(Parameters);
  287. // FIXME: Find the right names for these.
  288. void XTERM_WM(Parameters);
  289. // DECIC - Insert Column
  290. void DECIC(Parameters);
  291. // DECDC - Delete Column
  292. void DECDC(Parameters);
  293. // DECPNM - Set numeric keypad mode
  294. void DECKPNM();
  295. // DECPAM - Set application keypad mode
  296. void DECKPAM();
  297. #ifndef KERNEL
  298. TerminalClient& m_client;
  299. #else
  300. Kernel::VirtualConsole& m_client;
  301. #endif
  302. EscapeSequenceParser m_parser;
  303. #ifndef KERNEL
  304. size_t m_history_start = 0;
  305. Vector<NonnullOwnPtr<Line>> m_history;
  306. void add_line_to_history(NonnullOwnPtr<Line>&& line)
  307. {
  308. if (max_history_size() == 0)
  309. return;
  310. // If m_history can expand, add the new line to the end of the list.
  311. // If there is an overflow wrap, the end is at the index before the start.
  312. if (m_history.size() < max_history_size()) {
  313. if (m_history_start == 0)
  314. m_history.append(move(line));
  315. else
  316. m_history.insert(m_history_start - 1, move(line));
  317. return;
  318. }
  319. m_history[m_history_start] = move(line);
  320. m_history_start = (m_history_start + 1) % m_history.size();
  321. }
  322. Vector<NonnullOwnPtr<Line>>& active_buffer() { return m_use_alternate_screen_buffer ? m_alternate_screen_buffer : m_normal_screen_buffer; }
  323. Vector<NonnullOwnPtr<Line>> const& active_buffer() const { return m_use_alternate_screen_buffer ? m_alternate_screen_buffer : m_normal_screen_buffer; }
  324. Vector<NonnullOwnPtr<Line>> m_normal_screen_buffer;
  325. Vector<NonnullOwnPtr<Line>> m_alternate_screen_buffer;
  326. #endif
  327. bool m_use_alternate_screen_buffer { false };
  328. size_t m_scroll_region_top { 0 };
  329. size_t m_scroll_region_bottom { 0 };
  330. u16 m_columns { 1 };
  331. u16 m_rows { 1 };
  332. BufferState m_current_state;
  333. BufferState m_normal_saved_state;
  334. BufferState m_alternate_saved_state;
  335. // Separate from *_saved_state: some escape sequences only save/restore the cursor position,
  336. // while others impact the text attributes and other state too.
  337. CursorPosition m_saved_cursor_position;
  338. bool m_swallow_current { false };
  339. bool m_stomp { false };
  340. bool m_in_application_keypad_mode { false };
  341. CursorShape m_cursor_shape { VT::CursorShape::Block };
  342. CursorShape m_saved_cursor_shape { VT::CursorShape::Block };
  343. bool m_cursor_is_blinking_set { true };
  344. bool m_needs_bracketed_paste { false };
  345. Attribute m_current_attribute;
  346. Attribute m_saved_attribute;
  347. #ifndef KERNEL
  348. ByteString m_current_window_title;
  349. Vector<ByteString> m_title_stack;
  350. OrderedHashTable<Mark> m_valid_marks;
  351. #endif
  352. #ifndef KERNEL
  353. u32 m_next_href_id { 0 };
  354. #endif
  355. Vector<bool> m_horizontal_tabs;
  356. u32 m_last_code_point { 0 };
  357. size_t m_max_history_lines { 1024 };
  358. Optional<u16> m_column_before_carriage_return;
  359. bool m_controls_are_logically_generated { false };
  360. CursorKeysMode m_cursor_keys_mode { Cursor };
  361. CharacterSetTranslator m_character_set_translator {};
  362. size_t m_active_working_set_index { 0 };
  363. CharacterSet m_working_sets[4] { Iso_8859_1 };
  364. };
  365. }