FINAL CUT
ftermlinux.h
1 /***********************************************************************
2 * ftermlinux.h - Contains the Linux terminal functions *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2018-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  * ▕ FTermLinux ▏
28  * ▕▁▁▁▁▁▁▁▁▁▁▁▁▏
29  */
30 
31 #ifndef FTERMLINUX_H
32 #define FTERMLINUX_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 #if defined(__linux__)
39  #include <linux/fb.h> // Linux framebuffer console
40 
41  #if defined(__arm__) && defined(__GLIBC__) && defined(__GLIBC_PREREQ)
42  // ISA sysctl support on arm processors only up to glibc-2.29
43  #if !__GLIBC_PREREQ(2,30)
44  #define ARM_ISA_SYSCTL
45  #endif
46  #endif
47 
48  #if defined(__x86_64__) || defined(__i386) || defined(ARM_ISA_SYSCTL)
49  #define ISA_SYSCTL_SUPPORT
50  #include <sys/io.h>
51  #endif // defined(__x86_64__) || defined(__i386) || defined(ARM_ISA_SYSCTL)
52 
53  #include <sys/kd.h>
54 #else
55  // struct forward declaration
56  struct console_font_op;
57  struct unimapdesc;
58 #endif // defined(__linux__)
59 
60 #include <sys/ioctl.h>
61 #include <unistd.h>
62 
63 #include <bitset>
64 #include <cstdio> // need for sprintf
65 #include <cstring>
66 #include <unordered_map>
67 #include <utility>
68 #include <vector>
69 
70 #include "final/output/tty/ftermcap.h"
71 #include "final/output/tty/ftermdata.h"
72 #include "final/util/fstring.h"
73 
74 namespace finalcut
75 {
76 
77 //----------------------------------------------------------------------
78 // class FTermLinux
79 //----------------------------------------------------------------------
80 
81 class FTermLinux final
82 {
83  public:
84  // Using-declaration
85  using CursorStyle = LinuxConsoleCursorStyle;
86 
87  // Constructors
88  FTermLinux() = default;
89 
90  // Disable copy constructor
91  FTermLinux (const FTermLinux&) = delete;
92 
93  // Disable move constructor
94  FTermLinux (FTermLinux&&) noexcept = delete;
95 
96  // Destructor
97  ~FTermLinux();
98 
99  // Disable copy assignment operator (=)
100  auto operator = (const FTermLinux&) -> FTermLinux& = delete;
101 
102  // Disable move assignment operator (=)
103  auto operator = (FTermLinux&&) noexcept -> FTermLinux& = delete;
104 
105  // Accessors
106  auto getClassName() const -> FString;
107  static auto getInstance() -> FTermLinux&;
108  auto getCursorStyle() const -> CursorStyle;
109  auto getCursorStyleCtrl() const -> FTermcap::TermcapString;
110  auto getFramebufferBpp() const noexcept -> int;
111 
112  // Mutators
113  auto setCursorStyle (CursorStyle) -> bool;
114  auto setPalette (FColor, int, int, int) -> bool;
115  void setUTF8 (bool = true) const;
116 
117  // Predicates
118  static auto isLinuxConsole() -> bool;
119  auto isVGAFontUsed() const noexcept -> bool;
120  auto isNewFontUsed() const noexcept -> bool;
121 
122  // Methods
123  void init();
124  void initCharMap() const;
125  void finish() const;
126  auto loadVGAFont() -> bool;
127  auto loadNewFont() -> bool;
128  auto loadOldFont() -> bool;
129  auto saveColorMap() -> bool;
130  auto resetColorMap() -> bool;
131  void setBeep (int, int) const;
132  void resetBeep() const;
133  auto modifierKeyCorrection (const FKey&) -> FKey;
134 
135  private:
136  struct ModifierKey // bit field
137  {
138  uChar shift : 1; // 0..1
139  uChar alt_gr : 1; // 0..1
140  uChar ctrl : 1; // 0..1
141  uChar alt : 1; // 0..1
142  uChar : 4; // padding bits
143  };
144 
145  struct ModifierKeyHash
146  {
147  auto operator () (const ModifierKey& m_key) const noexcept -> std::size_t
148  {
149  uChar m{};
150  std::memcpy(&m, &m_key, sizeof(uChar));
151  return std::hash<uChar>{}(m);
152  }
153  };
154 
155  struct RGB
156  {
157  uChar red;
158  uChar green;
159  uChar blue;
160  };
161 
162  struct ColorMap
163  {
164  std::array<RGB, 16> color;
165  };
166 
167  struct Pair
168  {
169  ModifierKey modifier;
170  FKey key;
171  };
172 
173  struct PairHash
174  {
175  auto operator () (const Pair& pair) const noexcept -> std::size_t
176  {
177  size_t seed = 0;
178  const auto hash1 = ModifierKeyHash{}(pair.modifier);
179  const auto hash2 = EnumHash<FKey>{}(pair.key);
180  seed ^= hash1 + 0x9e3779b9 + (seed << 6u) + (seed >> 2u);
181  seed ^= hash2 + 0x9e3779b9 + (seed << 6u) + (seed >> 2u);
182  return seed;
183  }
184  };
185 
186  struct PairEqual
187  {
188  auto operator () (const Pair& lhs, const Pair& rhs) const noexcept -> bool
189  {
190  return std::memcmp(&lhs.modifier, &rhs.modifier, sizeof(ModifierKey)) == 0
191  && lhs.key == rhs.key;
192  }
193  };
194 
195  // Using-declaration
196  using KeyMap = std::unordered_map<Pair, FKey, PairHash, PairEqual>;
197  using FontData = std::vector<uChar>;
198  using UnicodeEntries = std::vector<struct unipair>;
199 
200  // Accessors
201  auto getFramebuffer_bpp() const -> int;
202  auto getScreenFont() -> bool;
203  auto getUnicodeMap () -> bool;
204  auto getModifierKey() & -> ModifierKey&;
205 
206  // Predicates
207  auto isLinuxTerm() const -> bool;
208 
209  // Mutators
210  auto setScreenFont ( const uChar[], uInt, uInt, uInt
211  , bool = false ) -> int;
212  auto setUnicodeMap (struct unimapdesc*) const -> int;
213  void setLinuxCursorStyle (LinuxConsoleCursorStyle) const;
214 
215  // Methods
216 #if defined(ISA_SYSCTL_SUPPORT)
217  auto getInputStatusRegisterOne() const -> uInt16;
218  auto readAttributeController (uChar) const -> uChar;
219  void writeAttributeController (uChar, uChar) const;
220  auto getAttributeMode() const -> uChar;
221  void setAttributeMode (uChar) const;
222  auto setBlinkAsIntensity (bool = true) const -> int;
223  auto has9BitCharacters() const -> bool;
224  void getVGAPalette();
225  void setVGADefaultPalette();
226  auto setVGAPalette (FColor, int, int, int) -> bool;
227  auto saveVGAPalette() -> bool;
228  auto resetVGAPalette() -> bool;
229 #endif // defined(ISA_SYSCTL_SUPPORT)
230  void initKeyMap();
231  void keyCorrection();
232  void shiftKeyCorrection();
233  void ctrlKeyCorrection();
234  void altKeyCorrection();
235  void shiftCtrlKeyCorrection();
236  void shiftAltKeyCorrection();
237  void ctrlAltKeyCorrection();
238  void shiftCtrlAltKeyCorrection();
239  void initSpecialCharacter() const;
240  auto getFontPos (wchar_t ucs) const -> sInt16;
241  void deleteFontData (console_font_op&);
242  void deleteUnicodeMapEntries (unimapdesc&);
243  void characterFallback (wchar_t, const std::vector<wchar_t>&) const;
244 
245  // Data members
246 #if defined(__linux__)
247  bool vga_font{};
248  bool new_font{};
249  bool has_saved_palette{};
250  int framebuffer_bpp{-1};
251  CursorStyle linux_console_cursor_style{};
252  console_font_op screen_font{};
253  unimapdesc screen_unicode_map{};
254  FontData font_data{};
255  FontData screen_font_data{};
256  UnicodeEntries unicode_entries{};
257  ColorMap saved_color_map{};
258  ColorMap cmap{};
259  KeyMap key_map{};
260  ModifierKey mod_key{};
261 #endif // defined(__linux__)
262 };
263 
264 
265 // FTermLinux inline functions
266 //----------------------------------------------------------------------
267 inline auto FTermLinux::getClassName() const -> FString
268 { return "FTermLinux"; }
269 
270 //----------------------------------------------------------------------
271 #if defined(__linux__)
272 inline auto FTermLinux::getFramebufferBpp() const noexcept -> int
273 { return framebuffer_bpp; }
274 
275 //----------------------------------------------------------------------
276 inline auto FTermLinux::isVGAFontUsed() const noexcept -> bool
277 { return vga_font; }
278 
279 //----------------------------------------------------------------------
280 inline auto FTermLinux::isNewFontUsed() const noexcept -> bool
281 { return new_font; }
282 #endif // defined(__linux__)
283 
284 } // namespace finalcut
285 
286 #endif // FTERMLINUX_H
Definition: ftermcap.h:72
Definition: ftermlinux.h:81
Definition: class_template.cpp:25
Definition: fstring.h:82
Definition: ftypes.h:161