FINAL CUT
ftermcap.h
1 /***********************************************************************
2 * ftermcap.h - Provides access to terminal capabilities *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2016-2024 Markus Gans *
7 * *
8 * FINAL CUT is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation; either version 3 of *
11 * the License, or (at your option) any later version. *
12 * *
13 * FINAL CUT is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this program. If not, see *
20 * <http://www.gnu.org/licenses/>. *
21 ***********************************************************************/
22 
23 /* Standalone class
24  * ════════════════
25  *
26  * ▕▔▔▔▔▔▔▔▔▔▔▏
27  * ▕ FTermcap ▏
28  * ▕▁▁▁▁▁▁▁▁▁▁▏
29  */
30 
31 #ifndef FTERMCAP_H
32 #define FTERMCAP_H
33 
34 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
35  #error "Only <final/final.h> can be included directly."
36 #endif
37 
38 #include <algorithm>
39 #include <array>
40 #include <string>
41 #include <thread>
42 #include <type_traits>
43 #include <utility>
44 #include <vector>
45 
46 #include "final/ftypes.h"
47 #include "final/util/fstring.h"
48 
49 // FTermcap string macro
50 #define TCAP(...) FTermcap::strings[int(Termcap::__VA_ARGS__)].string
51 
52 namespace finalcut
53 {
54 
55 //----------------------------------------------------------------------
56 // class FTermcap
57 //----------------------------------------------------------------------
58 
59 class FTermcap final
60 {
61  public:
62  // Enumeration
63  enum class Status
64  {
65  Error = -1,
66  OK = 0
67  };
68 
69  static constexpr std::size_t tname_min_size = 4u;
70  static constexpr std::size_t tname_size = std::max(alignof(void*), tname_min_size);
71 
72  struct TCapMap
73  {
74  const char* string;
75  std::array<char, tname_size> tname;
76  };
77 
78  // Using-declaration
79  using TCapMapType = std::array<TCapMap, 86>;
80  using PutCharFunc = std::decay_t<int(int)>;
81  using PutStringFunc = std::decay_t<int(const std::string&)>;
82 
83  // Constructors
84  FTermcap() = default;
85 
86  // Accessors
87  auto getClassName() const -> FString;
88  static auto getFlag (const std::string&) -> bool;
89  static auto getNumber (const std::string&) -> int;
90  static auto getString (const std::string&) -> char*;
91  static auto encodeMotionParameter (const std::string&, int, int) -> std::string;
92  template <typename... Args>
93  static auto encodeParameter (const std::string&, Args&&...) -> std::string;
94  static auto paddingPrint (const std::string&, int) -> Status;
95  static auto stringPrint (const std::string&) -> Status;
96 
97  // Inquiry
98  static auto isInitialized() -> bool;
99 
100  // Mutator
101  template<typename PutChar>
102  static void setPutCharFunction (const PutChar&);
103  static void setDefaultPutCharFunction();
104  template<typename PutString>
105  static void setPutStringFunction (const PutString&);
106  static void setDefaultPutStringFunction();
107  static void setBaudrate (int);
108 
109  // Methods
110  static void init();
111 
112  // Data members
113  static bool background_color_erase;
114  static bool can_change_color_palette;
115  static bool automatic_left_margin;
116  static bool automatic_right_margin;
117  static bool eat_nl_glitch;
118  static bool has_ansi_escape_sequences;
119  static bool ansi_default_color;
120  static bool osc_support;
121  static bool no_utf8_acs_chars;
122  static bool no_padding_char;
123  static bool xon_xoff_flow_control;
124  static int max_color;
125  static int tabstop;
126  static int padding_baudrate;
127  static int attr_without_color;
128  static TCapMapType strings;
129 
130  private:
131  // Using-declaration
132  using string_iterator = std::string::const_iterator;
133 
134  // Methods
135  static void termcap();
136  static void termcapError (int);
137  static void termcapVariables();
138  static void termcapBoleans();
139  static void termcapNumerics();
140  static void termcapStrings();
141  static void termcapKeys();
142  static auto encodeParams ( const std::string&
143  , const std::array<int, 9>& ) -> std::string;
144  static auto hasDelay (const std::string&) -> bool;
145  static void delayOutput (int);
146  static auto readNumber (string_iterator&, int, bool&) -> int;
147  static void readDigits (string_iterator&, int&);
148  static void decimalPoint (string_iterator&, int&);
149  static void asteriskSlash (string_iterator&, int&, int, bool&);
150 
151  // Data member
152  static bool initialized;
153  static int baudrate;
154  static char PC;
155  static char* buffer;
156  static char** buffer_addr;
157  static PutCharFunc outc;
158  static PutStringFunc outs;
159 };
160 
161 // FTermcap inline functions
162 //----------------------------------------------------------------------
163 inline auto FTermcap::getClassName() const -> FString
164 { return "FTermcap"; }
165 
166 //----------------------------------------------------------------------
167 template <typename... Args>
168 auto FTermcap::encodeParameter (const std::string& cap, Args&&... args) -> std::string
169 {
170  std::array<int, 9> attr {{static_cast<int>(args)...}};
171  std::fill(attr.begin() + sizeof...(args), attr.end(), 0);
172  return encodeParams(cap, attr);
173 }
174 
175 //----------------------------------------------------------------------
176 inline auto FTermcap::isInitialized() -> bool
177 {
178  // FTermcap is fully initialized when the termcap database has been
179  // read and the function pointers outc and outs are set
180  return initialized && outc && outs;
181 }
182 
183 //----------------------------------------------------------------------
184 template<typename PutChar>
185 inline void FTermcap::setPutCharFunction (const PutChar& put_char)
186 { outc = put_char; }
187 
188 //----------------------------------------------------------------------
189 template<typename PutString>
190 inline void FTermcap::setPutStringFunction (const PutString& put_string)
191 { outs = put_string; }
192 
193 //----------------------------------------------------------------------
194 inline void FTermcap::setBaudrate (int baud)
195 {
196  baudrate = baud;
197 }
198 
199 //----------------------------------------------------------------------
200 inline void FTermcap::delayOutput (int ms)
201 {
202  if ( no_padding_char )
203  {
204  std::this_thread::sleep_for(std::chrono::milliseconds(ms));
205  }
206  else
207  {
208  static constexpr int baudbyte = 9; // = 7 bit + 1 parity + 1 stop
209 
210  for ( int pad_char_count = (ms * baudrate) / (baudbyte * 1000);
211  pad_char_count > 0;
212  pad_char_count-- )
213  {
214  outc(int(PC));
215  }
216 
217  std::fflush(stdout);
218  }
219 }
220 
221 } // namespace finalcut
222 
223 
224 #endif // FTERMCAP_H
Definition: ftermcap.h:59
Definition: class_template.cpp:25
Definition: ftermcap.h:72
Definition: fstring.h:79