Terminal.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/Position.h>
  32. #include <LibVT/XtermColors.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. struct Attribute {
  44. Attribute() { reset(); }
  45. static const u32 default_foreground_color = xterm_colors[7];
  46. static const u32 default_background_color = xterm_colors[0];
  47. void reset()
  48. {
  49. foreground_color = default_foreground_color;
  50. background_color = default_background_color;
  51. flags = Flags::NoAttributes;
  52. }
  53. u32 foreground_color;
  54. u32 background_color;
  55. String href;
  56. String href_id;
  57. enum Flags : u8 {
  58. NoAttributes = 0x00,
  59. Bold = 0x01,
  60. Italic = 0x02,
  61. Underline = 0x04,
  62. Negative = 0x08,
  63. Blink = 0x10,
  64. Touched = 0x20,
  65. };
  66. bool is_untouched() const { return !(flags & Touched); }
  67. // TODO: it would be really nice if we had a helper for enums that
  68. // exposed bit ops for class enums...
  69. u8 flags = Flags::NoAttributes;
  70. bool operator==(const Attribute& other) const
  71. {
  72. return foreground_color == other.foreground_color && background_color == other.background_color && flags == other.flags;
  73. }
  74. bool operator!=(const Attribute& other) const
  75. {
  76. return !(*this == other);
  77. }
  78. };
  79. class Terminal {
  80. public:
  81. explicit Terminal(TerminalClient&);
  82. ~Terminal();
  83. bool m_need_full_flush { false };
  84. void invalidate_cursor();
  85. void on_char(u8);
  86. void clear();
  87. void set_size(u16 columns, u16 rows);
  88. u16 columns() const { return m_columns; }
  89. u16 rows() const { return m_rows; }
  90. u16 cursor_column() const { return m_cursor_column; }
  91. u16 cursor_row() const { return m_cursor_row; }
  92. struct Line {
  93. AK_MAKE_NONCOPYABLE(Line);
  94. AK_MAKE_NONMOVABLE(Line);
  95. public:
  96. explicit Line(u16 columns);
  97. ~Line();
  98. void clear(Attribute);
  99. bool has_only_one_background_color() const;
  100. void set_length(u16);
  101. StringView text() const { return { characters, m_length }; }
  102. u8* characters { nullptr };
  103. Attribute* attributes { nullptr };
  104. bool dirty { false };
  105. u16 m_length { 0 };
  106. };
  107. size_t line_count() const
  108. {
  109. return m_history.size() + m_lines.size();
  110. }
  111. Line& line(size_t index)
  112. {
  113. if (index < m_history.size())
  114. return m_history[index];
  115. return m_lines[index - m_history.size()];
  116. }
  117. const Line& line(size_t index) const
  118. {
  119. if (index < m_history.size())
  120. return m_history[index];
  121. return m_lines[index - m_history.size()];
  122. }
  123. size_t max_history_size() const { return 500; }
  124. const NonnullOwnPtrVector<Line>& history() const { return m_history; }
  125. void inject_string(const StringView&);
  126. Attribute attribute_at(const Position&) const;
  127. private:
  128. typedef Vector<unsigned, 4> ParamVector;
  129. void scroll_up();
  130. void scroll_down();
  131. void newline();
  132. void set_cursor(unsigned row, unsigned column);
  133. void put_character_at(unsigned row, unsigned column, u8 ch);
  134. void set_window_title(const String&);
  135. void unimplemented_escape();
  136. void unimplemented_xterm_escape();
  137. void emit_string(const StringView&);
  138. void alter_mode(bool should_set, bool question_param, const ParamVector&);
  139. void CUU(const ParamVector&);
  140. void CUD(const ParamVector&);
  141. void CUF(const ParamVector&);
  142. void CUB(const ParamVector&);
  143. void CUP(const ParamVector&);
  144. void ED(const ParamVector&);
  145. void EL(const ParamVector&);
  146. void escape$M(const ParamVector&);
  147. void escape$P(const ParamVector&);
  148. void escape$G(const ParamVector&);
  149. void escape$X(const ParamVector&);
  150. void escape$b(const ParamVector&);
  151. void escape$d(const ParamVector&);
  152. void SGR(const ParamVector&);
  153. void escape$s(const ParamVector&);
  154. void escape$u(const ParamVector&);
  155. void escape$t(const ParamVector&);
  156. void DECSTBM(const ParamVector&);
  157. void escape$S(const ParamVector&);
  158. void escape$T(const ParamVector&);
  159. void escape$L(const ParamVector&);
  160. void RM(bool question_param, const ParamVector&);
  161. void SM(bool question_param, const ParamVector&);
  162. void DA(const ParamVector&);
  163. void HVP(const ParamVector&);
  164. void NEL();
  165. void IND();
  166. void RI();
  167. void DSR(const ParamVector&);
  168. TerminalClient& m_client;
  169. NonnullOwnPtrVector<Line> m_history;
  170. NonnullOwnPtrVector<Line> m_lines;
  171. size_t m_scroll_region_top { 0 };
  172. size_t m_scroll_region_bottom { 0 };
  173. u16 m_columns { 1 };
  174. u16 m_rows { 1 };
  175. u16 m_cursor_row { 0 };
  176. u16 m_cursor_column { 0 };
  177. u16 m_saved_cursor_row { 0 };
  178. u16 m_saved_cursor_column { 0 };
  179. bool m_swallow_current { false };
  180. bool m_stomp { false };
  181. Attribute m_current_attribute;
  182. u32 m_next_href_id { 0 };
  183. void execute_escape_sequence(u8 final);
  184. void execute_xterm_command();
  185. void execute_hashtag(u8);
  186. enum EscapeState {
  187. Normal,
  188. GotEscape,
  189. ExpectParameter,
  190. ExpectIntermediate,
  191. ExpectFinal,
  192. ExpectHashtagDigit,
  193. ExpectXtermParameter,
  194. ExpectStringTerminator,
  195. };
  196. EscapeState m_escape_state { Normal };
  197. Vector<u8> m_parameters;
  198. Vector<u8> m_intermediates;
  199. Vector<u8> m_xterm_parameters;
  200. Vector<bool> m_horizontal_tabs;
  201. u8 m_final { 0 };
  202. u8 m_last_char { 0 };
  203. };
  204. }