Branch data Line data Source code
1 : : #ifndef bqtTTYterminalHH 2 : : #define bqtTTYterminalHH 3 : : /** @file tty/terminal.hh 4 : : * @brief Defines TerminalWindow, the bulk of the terminal emulator. Its main purpose is to render anything that is printed by the subprocess. 5 : : */ 6 : : 7 : : #include <deque> 8 : : #include <string> 9 : : #include <vector> 10 : : #include <array> 11 : : 12 : : #include "window.hh" 13 : : 14 : : /** A terminal emulator. It is always attached to some instance of Window. */ 15 : : class TerminalWindow 16 : : { 17 : : private: 18 : : Window& wnd; ///< The reference to Window instance. 19 : : 20 : : public: 21 : : std::deque<char32_t> OutBuffer; ///< Outgoing symbols (some ANSI codes cause input to be generated) 22 : : 23 : : private: 24 : : std::size_t top; ///< Current top row 25 : : std::size_t bottom; ///< Current bottom row 26 : : 27 : 398 : struct backup ///< The backup handled by SaveCur() and RestoreCur() 28 : : { 29 : : std::size_t cx; ///< Cursor X position 30 : : std::size_t cy; ///< Cursor Y position 31 : : Cell attr; ///< Attributes 32 : : } backup; 33 : : 34 : : std::array<unsigned char,4> gset; ///< Graphics set attributes: Changed by SCS, i.e. "esc(" and "esc)" 35 : : unsigned char activeset; ///< Identifies current index into gset to use for character set translation 36 : : unsigned char utfmode; ///< UTF-8 mode flag (set by "esc%@" and "esc%8", does not do anything for now) 37 : : unsigned char scs; ///< Index into gset to be changed in SCS commands. 38 : : 39 : : char32_t lastch = U' '; ///< Last printed character. Required by CSI b. 40 : : 41 : : std::vector<unsigned> p; ///< Parameters from the current escape mode 42 : : 43 : : unsigned state=0; ///< State index used in ANSI code parsing 44 : : bool edgeflag=false; ///< Flag that is set when something is printed at rightmost column 45 : : std::u32string string; ///< String that is collected in DCS, OSC, PM, APC and SOS commands 46 : : 47 : : private: 48 : : void ResetAttr(); ///< Resets the Window's attributes into defaults, but keeps protect flag. 49 : : /** Resets all attributes and states. If @param full is true, also clears screen and repositions cursor. */ 50 : : void Reset(bool full = true); 51 : : 52 : : /** Performs down-scrolling within a section of screen. 53 : : * @param y1 First line (inclusive) 54 : : * @param y2 Last line (inclusive) 55 : : * @param amount Number of lines to scroll. Non-positive values are ignored. 56 : : * The top of the window is filled with @amount lines of blank using Window.FillBox. 57 : : */ 58 : : void YScrollDown(unsigned y1, unsigned y2, int amount) const; 59 : : 60 : : /** Performs top-scrolling within a section of screen. 61 : : * @param y1 First line (inclusive) 62 : : * @param y2 Last line (inclusive) 63 : : * @param amount Number of lines to scroll. Non-positive values are ignored. 64 : : * The bottom of the window is filled with @amount lines of blank using Window.FillBox. 65 : : */ 66 : : void YScrollUp(unsigned y1, unsigned y2, int amount) const; 67 : : 68 : : void SaveCur(); ///< Implements the DECSC/ANSI_SC command 69 : : void RestoreCur(); ///< Implements the DECRC/ANSI_RC command 70 : : void EchoBack(std::u32string_view buffer); ///< Appends content into OutBuffer 71 : : public: 72 : : /** Processes output (input from the terminal's perspective) from the subprocess. 73 : : * Interprets ANSI codes. 74 : : * @param s String to parse. Internal state allows ANSI codes to be split between 75 : : * successive calls, i.e. 76 : : * Write("abc") works identically to Write("a");Write("b");Write("c"). 77 : : */ 78 : : void Write(std::u32string_view s); 79 : : 80 : : /** Initializes the terminal window with reference to a @param w Window. */ 81 : 398 : TerminalWindow(Window& w): wnd(w) 82 : : { 83 : 398 : ResetAttr(); 84 : 398 : Reset(); 85 : 398 : SaveCur(); 86 : 398 : } 87 : : 88 : : /** Resizes the terminal window to a different size. */ 89 : : void Resize(std::size_t newsx, std::size_t newsy); 90 : : }; 91 : : 92 : : #endif