FINAL CUT
fterm.h
1 /***********************************************************************
2 * fterm.h - Base class for terminal control *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2012-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 /* Base class
24  * ══════════
25  *
26  * ▕▔▔▔▔▔▔▔▏1 1▕▔▔▔▔▔▔▔▔▔▔▏
27  * ▕ FTerm ▏-┬- - - -▕ FTermios ▏
28  * ▕▁▁▁▁▁▁▁▏ : ▕▁▁▁▁▁▁▁▁▁▁▏
29  * :
30  * : 1▕▔▔▔▔▔▔▔▔▔▔▏
31  * :- - - -▕ FTermcap ▏
32  * : ▕▁▁▁▁▁▁▁▁▁▁▏
33  * :
34  * : 1▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
35  * :- - - -▕ FTermcapQuirks ▏
36  * : ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
37  * :
38  * : 1▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
39  * :- - - -▕ FTermDetection ▏
40  * : ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
41  * :
42  * : 1▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
43  * :- - - -▕ FTermXTerminal ▏
44  * : ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
45  * :
46  * : 1▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
47  * :- - - -▕ FTermLinux ▏
48  * : ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
49  * :
50  * : 1▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
51  * :- - - -▕ FTermFreeBSD ▏
52  * : ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
53  * :
54  * : 1▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
55  * :- - - -▕ FTermOpenBSD ▏
56  * : ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
57  * :
58  * : 1▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
59  * :- - - -▕ FColorPalette ▏
60  * : ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
61  * :
62  * : 1▕▔▔▔▔▔▔▔▔▔▔▔▏
63  * :- - - -▕ FOptiAttr ▏
64  * : ▕▁▁▁▁▁▁▁▁▁▁▁▏
65  * :
66  * : 1▕▔▔▔▔▔▔▔▔▔▔▔▏
67  * :- - - -▕ FOptiMove ▏
68  * : ▕▁▁▁▁▁▁▁▁▁▁▁▏
69  * :
70  * : 1▕▔▔▔▔▔▔▔▔▔▔▔▏
71  * :- - - -▕ FKeyboard ▏
72  * : ▕▁▁▁▁▁▁▁▁▁▁▁▏
73  * :
74  * : 1▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
75  * :- - - -▕ FMouseControl ▏
76  * : ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
77  * :
78  * : *▕▔▔▔▔▔▔▔▔▔▏
79  * :- - - -▕ FString ▏
80  * : ▕▁▁▁▁▁▁▁▁▁▏
81  * :
82  * : *▕▔▔▔▔▔▔▔▔▏
83  * :- - - -▕ FPoint ▏
84  * : ▕▁▁▁▁▁▁▁▁▏
85  * :
86  * : *▕▔▔▔▔▔▔▔▏
87  * └- - - -▕ FRect ▏
88  * ▕▁▁▁▁▁▁▁▏
89  */
90 
91 #ifndef FTERM_H
92 #define FTERM_H
93 
94 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
95  #error "Only <final/final.h> can be included directly."
96 #endif
97 
98 #include "final/fconfig.h"
99 
100 #include <sys/ioctl.h>
101 #include <sys/stat.h>
102 
103 #include <fcntl.h>
104 #include <langinfo.h>
105 
106 #if defined(F_HAVE_GETTTYNAM) && defined(F_HAVE_TTYENT_H)
107  #include <ttyent.h>
108 #endif
109 
110 #include <clocale>
111 #include <cmath>
112 #include <csignal>
113 
114 #include <functional>
115 #include <memory>
116 #include <string>
117 #include <utility>
118 #include <vector>
119 
120 #include "final/fc.h"
121 #include "final/output/fcolorpalette.h"
122 #include "final/output/tty/ftermdata.h"
123 #include "final/output/tty/fterm_functions.h"
124 #include "final/util/fstring.h"
125 #include "final/util/fsystem.h"
126 
127 namespace finalcut
128 {
129 
130 // class forward declaration
131 class FColorPalette;
132 class FPoint;
133 class FStartOptions;
134 class FSize;
135 class FString;
136 class FVTermBuffer;
137 
138 
139 //----------------------------------------------------------------------
140 // class FTerm
141 //----------------------------------------------------------------------
142 
143 class FTerm final
144 {
145  public:
146  // Using-declaration
147  using FSetPalette = FColorPalette::FSetPalette;
148 
149  // Constructor
150  FTerm();
151 
152  // Disable copy constructor
153  FTerm (const FTerm&) = delete;
154 
155  // Disable move constructor
156  FTerm (FTerm&&) noexcept = delete;
157 
158  // Destructor
159  ~FTerm();
160 
161  // Disable copy assignment operator (=)
162  auto operator = (const FTerm&) -> FTerm& = delete;
163 
164  // Disable move assignment operator (=)
165  auto operator = (FTerm&&) noexcept -> FTerm& = delete;
166 
167  // Accessors
168  static auto getClassName() -> FString;
169  static auto getLineNumber() -> std::size_t;
170  static auto getColumnNumber() -> std::size_t;
171  static auto getKeyName (FKey) -> FString;
172  auto getCharSubstitutionMap() & -> FCharSubstitution&;
173  static auto getTTYFileDescriptor() -> int;
174  static auto getTermType() -> std::string;
175  static auto getTermFileName() -> std::string;
176  static auto getTabstop() -> int;
177  static auto getMaxColor() -> int;
178 
179  // Inquiries
180  static auto isRaw() -> bool;
181  static auto hasUTF8() -> bool;
182  static auto isMonochron() -> bool;
183  static auto isNewFont() -> bool;
184  static auto isInitialized() -> bool;
185  static auto isCursorHideable() -> bool;
186  static auto isEncodable (const wchar_t&) -> bool;
187  static auto hasChangedTermSize() -> bool;
188  static auto hasShadowCharacter() -> bool;
189  static auto hasHalfBlockCharacter() -> bool;
190  static auto hasAlternateScreen() -> bool;
191  static auto canChangeColorPalette() -> bool;
192 
193  // Mutators
194  static void setFSystem (std::unique_ptr<FSystem>&);
195  static void setTermType (const std::string&);
196  static void setInsertCursor (bool = true);
197  static void unsetInsertCursor();
198  static void redefineDefaultColors (bool = true);
199  static void setDblclickInterval (const uInt64);
200  static void useAlternateScreen (bool = true);
201  static void setUTF8 (bool = true);
202  static void unsetUTF8();
203 
204  // Methods
205  static auto setVGAFont() -> bool;
206  static auto setNewFont() -> bool;
207  static auto resetFont() -> bool;
208  static auto openConsole() -> int;
209  static auto closeConsole() -> int;
210  static auto moveCursorString (int, int, int, int) -> std::string;
211  static auto cursorsVisibilityString (bool = true) -> std::string;
212  static void detectTermSize();
213  static void setTermSize (const FSize&);
214  static void setTermTitle (const FString&);
215  static void setKDECursor (KdeKonsoleCursorShape);
216  static void saveColorMap();
217  static void resetColorMap();
218  static void clearTerminalAttributes();
219  static void setPalette (FColor, int, int, int);
220  static void setBeep (int, int);
221  static void resetBeep();
222  static void beep();
223 
224  static void setEncoding (Encoding);
225  static auto getEncodingString() -> std::string;
226  static auto charEncode (const wchar_t&) -> wchar_t;
227  static auto charEncode (const wchar_t&, const Encoding&) -> wchar_t;
228 
229  static auto scrollTermForward() -> bool;
230  static auto scrollTermReverse() -> bool;
231 
232  template <typename... Args>
233  static void paddingPrintf (Args&&...);
234  static void paddingPrint (const std::string&, int = 1);
235  static void stringPrint (const std::string&);
236 
237  void initTerminal();
238  static void initScreenSettings();
239  static void changeTermSizeFinished();
240 
241  private:
242  // Methods
243  static auto getStartOptions() -> FStartOptions&;
244  static void init_global_values();
245  static void init_terminal_device_path();
246  static void oscPrefix();
247  static void oscPostfix();
248  static void init_alt_charset();
249  static void init_pc_charset();
250  static void init_cygwin_charmap();
251  static void updatePCEncodingForCygwin();
252  static void updateCharMappingForCygwin();
253  static void init_teraterm_charmap();
254  static void init_fixed_max_color();
255  static void init_termcap();
256  static void init_quirks();
257  static void init_optiMove();
258  static void init_optiAttr();
259  static auto init_font() -> bool;
260  static void init_locale();
261  static auto init_xterm_locale (const char*) -> const char*;
262  static auto init_tera_term_locale (const char*) -> const char*;
263  static auto init_kterm_locale (const char*) -> const char*;
264  static auto init_sun_locale (const char*) -> const char*;
265  static auto init_locale_if_not_found (const char*) -> const char*;
266  static void init_encoding();
267  static void init_encoding_set();
268  static void init_term_encoding();
269  static void init_individual_term_encoding();
270  static void init_force_vt100_encoding();
271  static void init_utf8_without_alt_charset();
272  static void init_tab_quirks();
273  static void init_captureFontAndTitle();
274  static auto hasNoFontSettingOption() -> bool;
275  static auto canSetTerminalFont() -> bool;
276  static void setInsertCursorStyle();
277  static void setOverwriteCursorStyle();
278  static auto enableCursorString() -> std::string;
279  static auto disableCursorString() -> std::string;
280  static void enableMouse();
281  static void disableMouse();
282  static void enableApplicationEscKey();
283  static void disableApplicationEscKey();
284  static void enableKeypad();
285  static void disableKeypad();
286  static void enableAlternateCharset();
287  static void useAlternateScreenBuffer();
288  static void useNormalScreenBuffer();
289  void init();
290  auto init_terminal() const -> bool;
291  void initOSspecifics() const;
292  void initTermspecifics() const;
293  void initBaudRate() const;
294  void finish() const;
295  void finishOSspecifics() const;
296  void finish_encoding() const;
297  static void printExitMessage();
298  static void terminalSizeChange();
299  [[noreturn]] static void processTermination (int);
300  static void setSignalHandler();
301  static void resetSignalHandler();
302  static void signal_handler (int);
303 };
304 
305 
306 // FTerm inline functions
307 //----------------------------------------------------------------------
308 inline auto FTerm::getClassName() -> FString
309 { return "FTerm"; }
310 
311 //----------------------------------------------------------------------
312 inline void FTerm::setFSystem (std::unique_ptr<FSystem>& fsystem)
313 {
314  FSystem::getInstance().swap(fsystem);
315 }
316 
317 //----------------------------------------------------------------------
318 inline void FTerm::unsetInsertCursor()
319 { setInsertCursor(false); }
320 
321 //----------------------------------------------------------------------
322 inline void FTerm::unsetUTF8()
323 { setUTF8(false); }
324 
325 //----------------------------------------------------------------------
326 template <typename... Args>
327 inline void FTerm::paddingPrintf (Args&&... args)
328 {
329  const int size = std::snprintf (nullptr, 0, args...);
330 
331  if ( size <= 0 )
332  return;
333 
334  std::string buffer{};
335  auto buffer_size = std::size_t(size) + 1;
336  buffer.resize(buffer_size);
337  std::snprintf ( &*buffer.begin(), buffer_size
338  , std::forward<Args>(args)... );
339  buffer_size--;
340  buffer.resize(buffer_size);
341  paddingPrint (buffer, 1);
342 }
343 
344 //----------------------------------------------------------------------
345 inline void FTerm::initTerminal()
346 { init(); }
347 
348 } // namespace finalcut
349 
350 //----------------------------------------------------------------------
351 inline auto operator << (std::ostream& os, finalcut::UniChar c) -> std::ostream&
352 {
353  static const auto& data = finalcut::FTermData::getInstance();
354 
355  if ( data.getTerminalEncoding() == finalcut::Encoding::UTF8 )
356  return os << finalcut::unicode_to_utf8(wchar_t(c));
357 
358  return os << static_cast<char>(uChar(c));
359 }
360 
361 //----------------------------------------------------------------------
362 inline auto operator << (std::wostream& os, finalcut::UniChar c) -> std::wostream&
363 {
364  return os << static_cast<wchar_t>(c);
365 }
366 
367 #endif // FTERM_H
Definition: ftermdata.h:58
Definition: class_template.cpp:25
Definition: fsize.h:55
Definition: fstartoptions.h:52
Definition: fstring.h:79
Definition: fterm.h:143