Terminal.h 12 KB

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