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