Terminal.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Noncopyable.h>
  28. #include <AK/NonnullOwnPtrVector.h>
  29. #include <AK/String.h>
  30. #include <AK/Vector.h>
  31. #include <Kernel/API/KeyCode.h>
  32. #include <LibVT/Line.h>
  33. #include <LibVT/Position.h>
  34. namespace VT {
  35. class TerminalClient {
  36. public:
  37. virtual ~TerminalClient() { }
  38. virtual void beep() = 0;
  39. virtual void set_window_title(const StringView&) = 0;
  40. virtual void set_window_progress(int value, int max) = 0;
  41. virtual void terminal_did_resize(u16 columns, u16 rows) = 0;
  42. virtual void terminal_history_changed() = 0;
  43. virtual void emit(const u8*, size_t) = 0;
  44. };
  45. class Terminal {
  46. public:
  47. explicit Terminal(TerminalClient&);
  48. ~Terminal();
  49. bool m_need_full_flush { false };
  50. void invalidate_cursor();
  51. void on_input(u8);
  52. void clear();
  53. void clear_including_history();
  54. void set_size(u16 columns, u16 rows);
  55. u16 columns() const { return m_columns; }
  56. u16 rows() const { return m_rows; }
  57. u16 cursor_column() const { return m_cursor_column; }
  58. u16 cursor_row() const { return m_cursor_row; }
  59. size_t line_count() const
  60. {
  61. return m_history.size() + m_lines.size();
  62. }
  63. Line& line(size_t index)
  64. {
  65. if (index < m_history.size())
  66. return m_history[(m_history_start + index) % m_history.size()];
  67. return m_lines[index - m_history.size()];
  68. }
  69. const Line& line(size_t index) const
  70. {
  71. return const_cast<Terminal*>(this)->line(index);
  72. }
  73. Line& visible_line(size_t index)
  74. {
  75. return m_lines[index];
  76. }
  77. const Line& visible_line(size_t index) const
  78. {
  79. return m_lines[index];
  80. }
  81. size_t max_history_size() const { return m_max_history_lines; }
  82. void set_max_history_size(size_t value)
  83. {
  84. if (value == 0) {
  85. m_max_history_lines = 0;
  86. m_history_start = 0;
  87. m_history.clear();
  88. m_client.terminal_history_changed();
  89. return;
  90. }
  91. if (m_max_history_lines > value) {
  92. NonnullOwnPtrVector<Line> new_history;
  93. new_history.ensure_capacity(value);
  94. auto existing_line_count = min(m_history.size(), value);
  95. for (size_t i = m_history.size() - existing_line_count; i < m_history.size(); ++i) {
  96. auto j = (m_history_start + i) % m_history.size();
  97. new_history.unchecked_append(move(static_cast<Vector<NonnullOwnPtr<Line>>&>(m_history).at(j)));
  98. }
  99. m_history = move(new_history);
  100. m_history_start = 0;
  101. m_client.terminal_history_changed();
  102. }
  103. m_max_history_lines = value;
  104. }
  105. size_t history_size() const { return m_history.size(); }
  106. void inject_string(const StringView&);
  107. void handle_key_press(KeyCode, u32, u8 flags);
  108. Attribute attribute_at(const Position&) const;
  109. private:
  110. typedef Vector<unsigned, 4> ParamVector;
  111. void on_code_point(u32);
  112. void scroll_up();
  113. void scroll_down();
  114. void newline();
  115. void set_cursor(unsigned row, unsigned column);
  116. void put_character_at(unsigned row, unsigned column, u32 ch);
  117. void set_window_title(const String&);
  118. void unimplemented_escape();
  119. void unimplemented_xterm_escape();
  120. void emit_string(const StringView&);
  121. void alter_mode(bool should_set, bool question_param, const ParamVector&);
  122. // CUU – Cursor Up
  123. void CUU(const ParamVector&);
  124. // CUD – Cursor Down
  125. void CUD(const ParamVector&);
  126. // CUF – Cursor Forward
  127. void CUF(const ParamVector&);
  128. // CUB – Cursor Backward
  129. void CUB(const ParamVector&);
  130. // CUP - Cursor Position
  131. void CUP(const ParamVector&);
  132. // ED - Erase in Display
  133. void ED(const ParamVector&);
  134. // EL - Erase in Line
  135. void EL(const ParamVector&);
  136. // SGR – Select Graphic Rendition
  137. void SGR(const ParamVector&);
  138. // Save Current Cursor Position
  139. void SCOSC(const ParamVector&);
  140. // Restore Saved Cursor Position
  141. void SCORC(const ParamVector&);
  142. // DECSTBM – Set Top and Bottom Margins ("Scrolling Region")
  143. void DECSTBM(const ParamVector&);
  144. // RM – Reset Mode
  145. void RM(bool question_param, const ParamVector&);
  146. // SM – Set Mode
  147. void SM(bool question_param, const ParamVector&);
  148. // DA - Device Attributes
  149. void DA(const ParamVector&);
  150. // HVP – Horizontal and Vertical Position
  151. void HVP(const ParamVector&);
  152. // NEL - Next Line
  153. void NEL();
  154. // IND - Index (move down)
  155. void IND();
  156. // RI - Reverse Index (move up)
  157. void RI();
  158. // DSR - Device Status Reports
  159. void DSR(const ParamVector&);
  160. // ICH - Insert Character
  161. void ICH(const ParamVector&);
  162. // SU - Scroll Up (called "Pan Down" in VT510)
  163. void SU(const ParamVector&);
  164. // SD - Scroll Down (called "Pan Up" in VT510)
  165. void SD(const ParamVector&);
  166. // IL - Insert Line
  167. void IL(const ParamVector&);
  168. // DCH - Delete Character
  169. void DCH(const ParamVector&);
  170. // DL - Delete Line
  171. void DL(const ParamVector&);
  172. // CHA - Cursor Horizontal Absolute
  173. void CHA(const ParamVector&);
  174. // REP - Repeat
  175. void REP(const ParamVector&);
  176. // VPA - Vertical Line Position Absolute
  177. void VPA(const ParamVector&);
  178. // ECH - Erase Character
  179. void ECH(const ParamVector&);
  180. // FIXME: Find the right names for these.
  181. void XTERM_WM(const ParamVector&);
  182. TerminalClient& m_client;
  183. size_t m_history_start = 0;
  184. NonnullOwnPtrVector<Line> m_history;
  185. void add_line_to_history(NonnullOwnPtr<Line>&& line)
  186. {
  187. if (max_history_size() == 0)
  188. return;
  189. if (m_history.size() < max_history_size()) {
  190. ASSERT(m_history_start == 0);
  191. m_history.append(move(line));
  192. return;
  193. }
  194. m_history.ptr_at(m_history_start) = move(line);
  195. m_history_start = (m_history_start + 1) % m_history.size();
  196. }
  197. NonnullOwnPtrVector<Line> m_lines;
  198. size_t m_scroll_region_top { 0 };
  199. size_t m_scroll_region_bottom { 0 };
  200. u16 m_columns { 1 };
  201. u16 m_rows { 1 };
  202. u16 m_cursor_row { 0 };
  203. u16 m_cursor_column { 0 };
  204. u16 m_saved_cursor_row { 0 };
  205. u16 m_saved_cursor_column { 0 };
  206. bool m_swallow_current { false };
  207. bool m_stomp { false };
  208. Attribute m_current_attribute;
  209. u32 m_next_href_id { 0 };
  210. void execute_escape_sequence(u8 final);
  211. void execute_xterm_command();
  212. void execute_hashtag(u8);
  213. enum ParserState {
  214. Normal,
  215. GotEscape,
  216. ExpectParameter,
  217. ExpectIntermediate,
  218. ExpectFinal,
  219. ExpectHashtagDigit,
  220. ExpectXtermParameter,
  221. ExpectStringTerminator,
  222. UTF8Needs3Bytes,
  223. UTF8Needs2Bytes,
  224. UTF8Needs1Byte,
  225. };
  226. ParserState m_parser_state { Normal };
  227. u32 m_parser_code_point { 0 };
  228. Vector<u8> m_parameters;
  229. Vector<u8> m_intermediates;
  230. Vector<u8> m_xterm_parameters;
  231. Vector<bool> m_horizontal_tabs;
  232. u8 m_final { 0 };
  233. u32 m_last_code_point { 0 };
  234. size_t m_max_history_lines { 1024 };
  235. };
  236. }