Fixed the terminal not fully functional error
[utio.git] / ti.h
blob3b59957b836045a252ae1fb48fb8cc4284e5ca02
1 // This file is part of the utio library, a terminal I/O library.
2 //
3 // Copyright (C) 2004 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
5 //
6 // ti.h
7 //
9 #ifndef TI_H_2B8E70FA3501DD2B50EF36C24E2DC717
10 #define TI_H_2B8E70FA3501DD2B50EF36C24E2DC717
12 #include "ticonst.h"
13 #include "gdt.h"
15 namespace utio {
17 /// \class CTerminfo ti.h utio/ti.h
18 ///
19 /// \brief An interface to a terminfo entry.
20 ///
21 /// This object reads and interprets terminfo files and can be used to set
22 /// colors and attributes, draw ACS characters, and decode keystrings, taking
23 /// a lot of headache from console programming.
24 ///
25 class CTerminfo {
26 public:
27 typedef int16_t number_t; ///< Type of numeric terminfo values.
28 typedef const char* capout_t; ///< Type of values returned by all capability output functions.
29 typedef const string& strout_t;
30 typedef string& rstrbuf_t;
31 typedef string keystrings_t; ///< List of key strings corresponding to EKeyDataValue enum.
32 typedef gdt::coord_t coord_t;
33 typedef gdt::dim_t dim_t;
34 public:
35 CTerminfo (void);
36 void Load (const char* termname = NULL);
37 void LoadEntry (memblock& buf, const char* termname = NULL) const;
38 strout_t MoveTo (coord_t x, coord_t y) const;
39 strout_t Color (EColor fg, EColor bg = color_Preserve) const;
40 capout_t Clear (void) const;
41 capout_t AttrOn (EAttribute a) const;
42 capout_t AttrOff (EAttribute a) const;
43 strout_t Attrs (uint16_t a) const;
44 capout_t AllAttrsOff (void) const;
45 inline capout_t HideCursor (void) const;
46 inline capout_t ShowCursor (void) const;
47 strout_t Image (coord_t x, coord_t y, dim_t w, dim_t h, const CCharCell* data) const;
48 strout_t Box (coord_t x, coord_t y, dim_t w, dim_t h) const;
49 strout_t Bar (coord_t x, coord_t y, dim_t w, dim_t h, char c = ' ') const;
50 strout_t HLine (coord_t x, coord_t y, dim_t w) const;
51 strout_t VLine (coord_t x, coord_t y, dim_t h) const;
52 capout_t Reset (void) const;
53 void ResetState (void) const;
54 inline strout_t Name (void) const { return (m_Name); }
55 inline dim_t Width (void) const { return (m_nColumns); }
56 inline dim_t Height (void) const { return (m_nRows); }
57 inline size_t Colors (void) const { return (m_nColors); }
58 inline size_t ColorPairs (void) const { return (m_nPairs); }
59 inline char AcsChar (EGraphicChar c) const { return (m_AcsMap[c]); }
60 bool GetBool (ti::EBooleans i) const;
61 number_t GetNumber (ti::ENumbers i) const;
62 capout_t GetString (ti::EStrings i) const;
63 wchar_t SubstituteChar (wchar_t c) const;
64 void LoadKeystrings (keystrings_t& ksv) const;
65 void Update (void);
66 void read (istream& is);
67 void write (ostream& os) const;
68 size_t stream_size (void) const;
69 private:
70 typedef vector<int8_t> boolvec_t;
71 typedef vector<number_t> numvec_t;
72 typedef uint16_t stroffset_t;
73 typedef vector<stroffset_t> stroffvec_t;
74 typedef string strtable_t;
75 typedef unsigned long progvalue_t;
76 typedef vector<progvalue_t> progstack_t;
77 typedef tuple<acs_Last,char> acsmap_t;
78 typedef tuple<attr_Last,number_t> progargs_t;
79 /// Structure for describing alternate character set values.
80 struct DLL_LOCAL SAcscInfo {
81 char m_vt100Code; ///< vt100 code for this character.
82 char m_Default; ///< Default value, if the terminfo does not specify.
83 uint16_t m_Unicode; ///< Unicode equivalent character value.
85 private:
86 static const SAcscInfo c_AcscInfo [acs_Last]; ///< Codes for all ACS characters.
87 static const int16_t c_KeyToStringMap [kv_nKeys];
88 /// Current terminal state.
89 class CContext {
90 public:
91 CContext (void);
92 public:
93 string m_Output; ///< Output string buffer.
94 progstack_t m_ProgStack; ///< Stack for running ti programs.
95 gdt::Point2d m_Pos; ///< Current cursor position.
96 uint16_t m_Attrs; ///< Text attributes.
97 uint8_t m_FgColor; ///< Foreground (text) color.
98 uint8_t m_BgColor; ///< Background color.
100 public:
101 static inline wchar_t AcsUnicodeValue (EGraphicChar c) { return (c_AcscInfo[c].m_Unicode); }
102 private:
103 void CacheFrequentValues (void);
104 void ObtainTerminalParameters (void);
105 void NormalizeColor (EColor& fg, EColor& bg, uint16_t& attrs) const;
106 void NColor (EColor fg, EColor bg, rstrbuf_t s) const;
107 void MoveTo (coord_t x, coord_t y, rstrbuf_t s) const;
108 void Color (EColor fg, EColor bg, rstrbuf_t s) const;
109 void Attrs (uint16_t a, rstrbuf_t s) const;
110 void RunStringProgram (const char* program, string& result, progargs_t args) const;
111 progvalue_t PSPop (void) const;
112 inline progvalue_t PSPopNonzero (void) const { const progvalue_t v (PSPop()); return (v ? v : 1); }
113 void PSPush (progvalue_t v) const;
114 private:
115 string m_Name; ///< Name of the terminfo entry.
116 boolvec_t m_Booleans; ///< Boolean caps.
117 numvec_t m_Numbers; ///< Numeric caps.
118 stroffvec_t m_StringOffsets; ///< String caps (offsets into m_StringTable)
119 strtable_t m_StringTable; ///< Actual string caps values.
120 acsmap_t m_AcsMap; ///< Decoded ACS characters.
121 mutable CContext m_Ctx; ///< Current state of the terminal.
122 uint16_t m_nColors; ///< Number of available colors.
123 uint16_t m_nPairs; ///< Number of available color pairs (unused).
124 dim_t m_nColumns; ///< Number of display columns.
125 dim_t m_nRows; ///< Number of display rows.
128 //----------------------------------------------------------------------
130 /// Makes the cursor invisible.
131 inline CTerminfo::capout_t CTerminfo::HideCursor (void) const
133 return (GetString (ti::cursor_invisible));
136 /// Makes the cursor visible.
137 inline CTerminfo::capout_t CTerminfo::ShowCursor (void) const
139 return (GetString (ti::cursor_normal));
142 //----------------------------------------------------------------------
144 } // namespace utio
146 STD_STREAMABLE(utio::CTerminfo);
148 #endif