Terminal.h 12 KB

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