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-2026 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 : sInt8
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 
73  {
74  const char* data;
75  uInt32 length{};
76  };
77 
78  struct TCapMap
79  {
80  TermcapString string;
81  std::array<char, tname_size> tname;
82  };
83 
84  // Using-declaration
85  using TCapMapType = std::array<TCapMap, 86>;
86  using PutCharFunc = std::decay_t<int(int)>;
87  using PutStringFunc = std::decay_t<int(const char*, uInt32)>;
88 
89  // Constructors
90  FTermcap() = default;
91 
92  // Accessors
93  auto getClassName() const -> FString;
94  static auto getFlag (const std::string&) -> bool;
95  static auto getNumber (const std::string&) -> int;
96  static auto getString (const std::string&) -> char*;
97  static auto encodeMotionParameter (const TermcapString&, int, int) -> TermcapString;
98  template <typename... Args>
99  static auto encodeParameter (const TermcapString&, Args&&...) -> TermcapString;
100  static auto paddingPrint (const char*, uInt32, int) -> Status;
101  static auto stringPrint (const char*, uInt32) -> Status;
102 
103  // Predicate
104  static auto isInitialized() noexcept -> bool;
105 
106  // Mutator
107  template<typename PutChar>
108  static void setPutCharFunction (const PutChar&) noexcept;
109  static void setDefaultPutCharFunction();
110  template<typename PutString>
111  static void setPutStringFunction (const PutString&) noexcept;
112  static void setDefaultPutStringFunction();
113  static void setBaudrate (int) noexcept;
114  static void clearMotionCache();
115 
116  // Methods
117  static void init();
118 
119  // Data members
120  static bool background_color_erase;
121  static bool can_change_color_palette;
122  static bool automatic_left_margin;
123  static bool automatic_right_margin;
124  static bool eat_nl_glitch;
125  static bool has_ansi_escape_sequences;
126  static bool ansi_default_color;
127  static bool osc_support;
128  static bool no_utf8_acs_chars;
129  static bool no_padding_char;
130  static bool xon_xoff_flow_control;
131  static int max_color;
132  static int tabstop;
133  static int padding_baudrate;
134  static int attr_without_color;
135  static TCapMapType strings;
136 
137  private:
138  // Using-declaration
139  using string_iterator = std::string::const_iterator;
140 
141  // Methods
142  static void termcap();
143  static void termcapError (int);
144  static void termcapVariables();
145  static void termcapBoleans();
146  static void termcapNumerics();
147  static void termcapStrings();
148  static void termcapKeys();
149  static auto encodeParams ( const TermcapString&
150  , const std::array<int, 9>& ) -> TermcapString;
151  static auto hasDelay (const std::string&) noexcept -> bool;
152  static void delayOutput (int) noexcept;
153  static auto readNumber (const char*&, int, bool&) noexcept -> int;
154  static void readDigits (const char*&, int&) noexcept;
155  static void decimalPoint (const char*&, int&) noexcept;
156  static void asteriskSlash (const char*&, int&, int, bool&) noexcept;
157 
158  // Data member
159  static bool initialized;
160  static int baudrate;
161  static char PC;
162  static char* buffer;
163  static char** buffer_addr;
164  static PutCharFunc outc;
165  static PutStringFunc outs;
166 };
167 
168 // FTermcap inline functions
169 //----------------------------------------------------------------------
170 inline auto FTermcap::getClassName() const -> FString
171 { return "FTermcap"; }
172 
173 //----------------------------------------------------------------------
174 template <typename... Args>
175 auto FTermcap::encodeParameter (const TermcapString& cap, Args&&... args) -> TermcapString
176 {
177  std::array<int, 9> attr {{static_cast<int>(args)...}};
178  return encodeParams(cap, attr);
179 }
180 
181 //----------------------------------------------------------------------
182 inline auto FTermcap::isInitialized() noexcept -> bool
183 {
184  // FTermcap is fully initialized when the termcap database has been
185  // read and the function pointers outc and outs are set
186  return initialized && outc && outs;
187 }
188 
189 //----------------------------------------------------------------------
190 template<typename PutChar>
191 inline void FTermcap::setPutCharFunction (const PutChar& put_char) noexcept
192 { outc = put_char; }
193 
194 //----------------------------------------------------------------------
195 template<typename PutString>
196 inline void FTermcap::setPutStringFunction (const PutString& put_string) noexcept
197 { outs = put_string; }
198 
199 //----------------------------------------------------------------------
200 inline void FTermcap::setBaudrate (int baud) noexcept
201 {
202  baudrate = baud;
203 }
204 
205 //----------------------------------------------------------------------
206 inline void FTermcap::delayOutput (int ms) noexcept
207 {
208  if ( no_padding_char )
209  {
210  std::this_thread::sleep_for(std::chrono::milliseconds(ms));
211  }
212  else
213  {
214  static constexpr int baudbyte = 9; // = 7 bit + 1 parity + 1 stop
215 
216  for ( int pad_char_count = (ms * baudrate) / (baudbyte * 1000);
217  pad_char_count > 0;
218  pad_char_count-- )
219  {
220  outc(int(PC));
221  }
222 
223  std::fflush(stdout);
224  }
225 }
226 
227 } // namespace finalcut
228 
229 
230 #endif // FTERMCAP_H
Definition: ftermcap.h:72
Definition: ftermcap.h:59
Definition: class_template.cpp:25
Definition: ftermcap.h:78
Definition: fstring.h:82