Terminal.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <LibVT/Line.h>
  32. #include <LibVT/Position.h>
  33. namespace VT {
  34. class TerminalClient {
  35. public:
  36. virtual ~TerminalClient() { }
  37. virtual void beep() = 0;
  38. virtual void set_window_title(const StringView&) = 0;
  39. virtual void terminal_did_resize(u16 columns, u16 rows) = 0;
  40. virtual void terminal_history_changed() = 0;
  41. virtual void emit(const u8*, size_t) = 0;
  42. };
  43. class Terminal {
  44. public:
  45. explicit Terminal(TerminalClient&);
  46. ~Terminal();
  47. bool m_need_full_flush { false };
  48. void invalidate_cursor();
  49. void on_input(u8);
  50. void clear();
  51. void set_size(u16 columns, u16 rows);
  52. u16 columns() const { return m_columns; }
  53. u16 rows() const { return m_rows; }
  54. u16 cursor_column() const { return m_cursor_column; }
  55. u16 cursor_row() const { return m_cursor_row; }
  56. size_t line_count() const
  57. {
  58. return m_history.size() + m_lines.size();
  59. }
  60. Line& line(size_t index)
  61. {
  62. if (index < m_history.size())
  63. return m_history[index];
  64. return m_lines[index - m_history.size()];
  65. }
  66. const Line& line(size_t index) const
  67. {
  68. if (index < m_history.size())
  69. return m_history[index];
  70. return m_lines[index - m_history.size()];
  71. }
  72. Line& visible_line(size_t index)
  73. {
  74. return m_lines[index];
  75. }
  76. const Line& visible_line(size_t index) const
  77. {
  78. return m_lines[index];
  79. }
  80. size_t max_history_size() const { return 500; }
  81. const NonnullOwnPtrVector<Line>& history() const { return m_history; }
  82. void inject_string(const StringView&);
  83. Attribute attribute_at(const Position&) const;
  84. private:
  85. typedef Vector<unsigned, 4> ParamVector;
  86. void on_codepoint(u32);
  87. void scroll_up();
  88. void scroll_down();
  89. void newline();
  90. void set_cursor(unsigned row, unsigned column);
  91. void put_character_at(unsigned row, unsigned column, u32 ch);
  92. void set_window_title(const String&);
  93. void unimplemented_escape();
  94. void unimplemented_xterm_escape();
  95. void emit_string(const StringView&);
  96. void alter_mode(bool should_set, bool question_param, const ParamVector&);
  97. void CUU(const ParamVector&);
  98. void CUD(const ParamVector&);
  99. void CUF(const ParamVector&);
  100. void CUB(const ParamVector&);
  101. void CUP(const ParamVector&);
  102. void ED(const ParamVector&);
  103. void EL(const ParamVector&);
  104. void escape$M(const ParamVector&);
  105. void escape$P(const ParamVector&);
  106. void escape$G(const ParamVector&);
  107. void escape$X(const ParamVector&);
  108. void escape$b(const ParamVector&);
  109. void escape$d(const ParamVector&);
  110. void SGR(const ParamVector&);
  111. void escape$s(const ParamVector&);
  112. void escape$u(const ParamVector&);
  113. void escape$t(const ParamVector&);
  114. void DECSTBM(const ParamVector&);
  115. void escape$S(const ParamVector&);
  116. void escape$T(const ParamVector&);
  117. void escape$L(const ParamVector&);
  118. void RM(bool question_param, const ParamVector&);
  119. void SM(bool question_param, const ParamVector&);
  120. void DA(const ParamVector&);
  121. void HVP(const ParamVector&);
  122. void NEL();
  123. void IND();
  124. void RI();
  125. void DSR(const ParamVector&);
  126. TerminalClient& m_client;
  127. NonnullOwnPtrVector<Line> m_history;
  128. NonnullOwnPtrVector<Line> m_lines;
  129. size_t m_scroll_region_top { 0 };
  130. size_t m_scroll_region_bottom { 0 };
  131. u16 m_columns { 1 };
  132. u16 m_rows { 1 };
  133. u16 m_cursor_row { 0 };
  134. u16 m_cursor_column { 0 };
  135. u16 m_saved_cursor_row { 0 };
  136. u16 m_saved_cursor_column { 0 };
  137. bool m_swallow_current { false };
  138. bool m_stomp { false };
  139. Attribute m_current_attribute;
  140. u32 m_next_href_id { 0 };
  141. void execute_escape_sequence(u8 final);
  142. void execute_xterm_command();
  143. void execute_hashtag(u8);
  144. enum ParserState {
  145. Normal,
  146. GotEscape,
  147. ExpectParameter,
  148. ExpectIntermediate,
  149. ExpectFinal,
  150. ExpectHashtagDigit,
  151. ExpectXtermParameter,
  152. ExpectStringTerminator,
  153. UTF8Needs3Bytes,
  154. UTF8Needs2Bytes,
  155. UTF8Needs1Byte,
  156. };
  157. ParserState m_parser_state { Normal };
  158. u32 m_parser_codepoint { 0 };
  159. Vector<u8> m_parameters;
  160. Vector<u8> m_intermediates;
  161. Vector<u8> m_xterm_parameters;
  162. Vector<bool> m_horizontal_tabs;
  163. u8 m_final { 0 };
  164. u32 m_last_codepoint { 0 };
  165. };
  166. }