Terminal.h 12 KB

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