FINAL CUT
ftermdetection.h
1 /***********************************************************************
2 * ftermdetection.h - Detection of the terminal type *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2018-2023 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  * ▕ FTermDetection ▏
28  * ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
29  */
30 
31 #ifndef FTERMDETECTION_H
32 #define FTERMDETECTION_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 <cctype>
39 #include <cstdio>
40 #include <cstdlib>
41 #include <cstring>
42 
43 #include "final/fconfig.h" // Supplies F_HAVE_GETTTYNAM if available
44 #include "final/util/fstring.h"
45 
46 namespace finalcut
47 {
48 
49 //----------------------------------------------------------------------
50 // class FTermDetection
51 //----------------------------------------------------------------------
52 
53 class FTermDetection final
54 {
55  public:
56  // Constructors
57  FTermDetection() = default;
58 
59  // Accessors
60  auto getClassName() const -> FString;
61  static auto getInstance() -> FTermDetection&;
62  auto getTermType() const & -> const FString&;
63 
64 #if DEBUG
65  auto getAnswerbackString() const & -> const FString&;
66  auto getSecDAString() const & -> const FString&;
67  auto getTermType_256color() const & -> const FString&;
68  auto getTermType_Answerback() const & -> const FString&;
69  auto getTermType_SecDA() const & -> const FString&;
70 #endif
71 
72  // Inquiries
73  auto canDisplay256Colors() const noexcept -> bool;
74  auto hasTerminalDetection() const noexcept -> bool;
75  auto hasSetCursorStyleSupport() const noexcept -> bool;
76 
77  // Mutators
78  void setTerminalDetection (bool = true) noexcept;
79  void setTtyTypeFileName (const FString&);
80 
81  // Methods
82  void detect();
83 
84  private:
85  struct colorEnv
86  {
87  FString string1{};
88  FString string2{};
89  FString string3{};
90  FString string4{};
91  FString string5{};
92  FString string6{};
93  FString string7{};
94  FString string8{};
95  };
96 
97  struct secondaryDA
98  {
99  int terminal_id_type{-1};
100  int terminal_id_version{-1};
101  int terminal_id_hardware{-1};
102  };
103 
104  // Using-declaration
105  using TermTypeMap = std::vector<std::pair<std::wstring, FTermType>>;
106 
107  // Methods
108  void getSystemTermType();
109  auto getTTYtype() -> bool;
110 #if F_HAVE_GETTTYNAM
111  auto getTTYSFileEntry() -> bool;
112 #endif
113  auto getTermBasename() const -> const char*;
114  template<typename StringT>
115  auto startsWithTermType (StringT&&) const -> bool;
116  void termtypeAnalysis();
117  auto findMatchingTerm (const TermTypeMap&) -> TermTypeMap::const_iterator;
118  auto isTerminalWithoutDetection() const -> bool;
119  void handleScreenAndTmux() const;
120  void detectTerminal();
121  auto init_256colorTerminal() -> FString;
122  auto get256colorEnvString() -> bool;
123  auto termtype_256color_quirks() -> FString;
124  auto determineMaxColor (const FString&) -> FString;
125  auto getXTermColorName (FColor) const -> FString;
126  auto parseAnswerbackMsg (const FString&) -> FString;
127  auto getAnswerbackMsg() const -> FString;
128  auto parseSecDA (const FString&) -> FString;
129  auto str2int (const FString&) const -> int;
130  auto getSecDA() const -> FString;
131  auto secDA_Analysis (const FString&) -> FString;
132  auto secDA_Analysis_0 (const FString&) const -> FString;
133  auto secDA_Analysis_1 (const FString&) -> FString;
134  auto secDA_Analysis_24 (const FString&) -> FString;
135  auto secDA_Analysis_32 () const -> FString;
136  auto secDA_Analysis_65 (const FString&) -> FString;
137  auto secDA_Analysis_67 () const -> FString;
138  auto secDA_Analysis_77 () -> FString;
139  auto secDA_Analysis_82 () const -> FString;
140  auto secDA_Analysis_83 (const FString&) const -> FString;
141  auto secDA_Analysis_84 (const FString&) const -> FString;
142  auto secDA_Analysis_85 () const -> FString;
143  auto secDA_Analysis_vte (const FString&) -> FString;
144  auto secDA_Analysis_kitty (const FString&) -> FString;
145  void correctFalseAssumptions (int) const;
146 
147  // Data members
148 #if DEBUG
149  FString termtype_256color{};
150  FString termtype_Answerback{};
151  FString termtype_SecDA{};
152 #endif
153  FString termtype{};
154  FString ttytypename{"/etc/ttytype"}; // Default ttytype file
155  bool decscusr_support{false}; // Preset to false
156  bool terminal_detection{true}; // Preset to true
157  bool color256{};
158  FString answer_back{};
159  FString sec_da{};
160  colorEnv color_env{};
161  secondaryDA secondary_da{};
162 };
163 
164 
165 // FTermDetection inline functions
166 //----------------------------------------------------------------------
167 inline auto FTermDetection::getClassName() const -> FString
168 { return "FTermDetection"; }
169 
170 //----------------------------------------------------------------------
171 inline auto FTermDetection::getTermType() const & -> const FString&
172 { return termtype; }
173 
174 #if DEBUG
175 //----------------------------------------------------------------------
176 inline auto FTermDetection::getTermType_256color() const & -> const FString&
177 { return termtype_256color; }
178 
179 //----------------------------------------------------------------------
180 inline auto FTermDetection::getTermType_Answerback() const & -> const FString&
181 { return termtype_Answerback; }
182 
183 //----------------------------------------------------------------------
184 inline auto FTermDetection::getTermType_SecDA() const & -> const FString&
185 { return termtype_SecDA; }
186 #endif
187 
188 //----------------------------------------------------------------------
189 inline auto FTermDetection::canDisplay256Colors() const noexcept -> bool
190 { return color256; }
191 
192 //----------------------------------------------------------------------
193 inline auto FTermDetection::hasSetCursorStyleSupport() const noexcept -> bool
194 { return decscusr_support; }
195 
196 //----------------------------------------------------------------------
197 inline auto FTermDetection::hasTerminalDetection() const noexcept -> bool
198 { return terminal_detection; }
199 
200 //----------------------------------------------------------------------
201 inline void FTermDetection::setTerminalDetection (bool enable) noexcept
202 { terminal_detection = enable; }
203 
204 //----------------------------------------------------------------------
205 template<typename StringT>
206 inline auto FTermDetection::startsWithTermType (StringT&& prefix) const -> bool
207 {
208  return termtype.toWString().find(std::forward<StringT>(prefix)) == 0;
209 }
210 
211 } // namespace finalcut
212 
213 #endif // FTERMDETECTION_H
Definition: class_template.cpp:25
Definition: fstring.h:79
Definition: ftermdetection.h:53