Terminal.h 6.4 KB

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