File: ftermfreebsd.cpp

package info (click to toggle)
finalcut 0.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,832 kB
  • sloc: cpp: 90,264; makefile: 546; sh: 412
file content (256 lines) | stat: -rw-r--r-- 7,771 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/***********************************************************************
* ftermfreebsd.cpp - Contains the FreeBSD terminal functions           *
*                                                                      *
* This file is part of the FINAL CUT widget toolkit                    *
*                                                                      *
* Copyright 2018-2023 Markus Gans                                      *
*                                                                      *
* FINAL CUT is free software; you can redistribute it and/or modify    *
* it under the terms of the GNU Lesser General Public License as       *
* published by the Free Software Foundation; either version 3 of       *
* the License, or (at your option) any later version.                  *
*                                                                      *
* FINAL CUT is distributed in the hope that it will be useful, but     *
* WITHOUT ANY WARRANTY; without even the implied warranty of           *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
* GNU Lesser General Public License for more details.                  *
*                                                                      *
* You should have received a copy of the GNU Lesser General Public     *
* License along with this program.  If not, see                        *
* <http://www.gnu.org/licenses/>.                                      *
***********************************************************************/

#include <memory>

#include "final/fapplication.h"
#include "final/ftypes.h"
#include "final/output/tty/fcharmap.h"
#include "final/output/tty/ftermdata.h"
#include "final/output/tty/ftermfreebsd.h"
#include "final/output/tty/fterm.h"
#include "final/util/flog.h"
#include "final/util/fsystem.h"

#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(UNIT_TEST)

namespace finalcut
{

// static class attributes
uInt                      FTermFreeBSD::bsd_alt_keymap{0};
FTermFreeBSD::CursorStyle FTermFreeBSD::cursor_style{FreeBSDConsoleCursorStyle::Normal};
bool                      FTermFreeBSD::change_cursorstyle{true};
bool                      FTermFreeBSD::meta_sends_escape{true};


//----------------------------------------------------------------------
// class FTermFreeBSD
//----------------------------------------------------------------------

// public methods of FTermFreeBSD
//----------------------------------------------------------------------
auto FTermFreeBSD::getInstance() -> FTermFreeBSD&
{
  static const auto& freebsd_console = std::make_unique<FTermFreeBSD>();
  return *freebsd_console;
}

//----------------------------------------------------------------------
auto FTermFreeBSD::getCursorStyle() -> CursorStyle
{
  return cursor_style;
}

//----------------------------------------------------------------------
auto FTermFreeBSD::setCursorStyle (CursorStyle style) -> bool
{
  // Set cursor style in a BSD console

  if ( ! isFreeBSDConsole() || ! change_cursorstyle )
    return false;

  cursor_style = style;
  static auto& fterm_data = FTermData::getInstance();

  if ( fterm_data.isCursorHidden() )
    return false;

  return setFreeBSDCursorStyle(style);
}

//----------------------------------------------------------------------
auto FTermFreeBSD::isFreeBSDConsole() -> bool
{
  // Check if it's a FreeBSD console

  keymap_t keymap{};
  static const auto& fsystem = FSystem::getInstance();
  return fsystem->ioctl(0, GIO_KEYMAP, &keymap) == 0;
}

//----------------------------------------------------------------------
void FTermFreeBSD::setBeep (int Hz, int ms)
{
  static auto& fterm_data = FTermData::getInstance();

  if ( ! fterm_data.isTermType(FTermType::freebsd_con) )
    return;

  // Range for frequency: 21-32766
  if ( Hz < 21 || Hz > 32766 )
    return;

  // Range for duration:  0-1999
  if ( ms < 0 || ms > 1999 )
    return;

  constexpr int timer_frequency = 1193182;
  int period = timer_frequency / Hz;
  ms /= 10;
  FTerm::paddingPrintf (CSI "=%d;%dB", period, ms);
  std::fflush(stdout);
}

//----------------------------------------------------------------------
void FTermFreeBSD::resetBeep()
{
  static auto& fterm_data = FTermData::getInstance();

  if ( ! fterm_data.isTermType(FTermType::freebsd_con) )
    return;

  // Default frequency: 1491 Hz
  // Default duration:  50 ms
  FTerm::paddingPrint (CSI "=800;5B");
  std::fflush(stdout);
}

//----------------------------------------------------------------------
void FTermFreeBSD::init()
{
  // Initialize BSD console

  if ( ! isFreeBSDConsole() )
    return;

  if ( meta_sends_escape )
  {
    // Save current left alt key mapping
    saveFreeBSDAltKey();

    // Map meta key to left alt key
    setFreeBSDAlt2Meta();
  }

  if ( change_cursorstyle )
  {
    // Initialize FreeBSD console cursor
    setCursorStyle (FreeBSDConsoleCursorStyle::Destructive);
  }
}

//----------------------------------------------------------------------
void FTermFreeBSD::initCharMap()
{
  // A FreeBSD console can't show ASCII codes from 0x00 to 0x1b

  if ( ! isFreeBSDConsole() )
    return;

  for (auto&& entry : FCharMap::getCharEncodeMap())
    if ( entry.pc < 0x1c )
      entry.pc = entry.ascii;
}

//----------------------------------------------------------------------
void FTermFreeBSD::finish()
{
  // Resetting the FreeBSD console settings

  if ( ! isFreeBSDConsole() )
    return;

  if ( meta_sends_escape )
    resetFreeBSDAlt2Meta();

  setFreeBSDCursorStyle (FreeBSDConsoleCursorStyle::Normal);
}


// private methods of FTermFreeBSD
//----------------------------------------------------------------------
void FTermFreeBSD::warnNotInitialized()
{
  std::clog << FLog::LogLevel::Warn
            << "The FTermFreeBSD object has "
            << "not yet been initialized! "
            << "Please call the init() method first."
            << std::endl;
}

//----------------------------------------------------------------------
auto FTermFreeBSD::saveFreeBSDAltKey() -> bool
{
  // Saving the current mapping for the alt key

  static constexpr int left_alt = 0x38;
  int ret{-1};
  keymap_t keymap{};
  static const auto& fsystem = FSystem::getInstance();
  ret = fsystem->ioctl (0, GIO_KEYMAP, &keymap);

  if ( ret < 0 )
    return false;

  // Save current mapping
  bsd_alt_keymap = uInt(keymap.key[left_alt].map[0]);
  return true;
}

//----------------------------------------------------------------------
auto FTermFreeBSD::setFreeBSDAltKey (uInt key) -> bool
{
  // Remapping the alt key

  static constexpr int left_alt = 0x38;
  int ret{-1};
  keymap_t keymap{};
  static const auto& fsystem = FSystem::getInstance();
  ret = fsystem->ioctl (0, GIO_KEYMAP, &keymap);

  if ( ret < 0 )
    return false;

  // Mapping "key" on the left alt key
  keymap.key[left_alt].map[0] = int(key);
  return ( keymap.n_keys > 0 )
         ? fsystem->ioctl(0, PIO_KEYMAP, &keymap) >= 0
         : false;
}

//----------------------------------------------------------------------
auto FTermFreeBSD::setFreeBSDAlt2Meta() -> bool
{
  // Use the meta key when pressing the Alt key

  return setFreeBSDAltKey (META);
}

//----------------------------------------------------------------------
auto FTermFreeBSD::resetFreeBSDAlt2Meta() -> bool
{
  // Restore the alt key mapping

  return setFreeBSDAltKey (bsd_alt_keymap);
}

//----------------------------------------------------------------------
auto FTermFreeBSD::setFreeBSDCursorStyle (CursorStyle style) -> bool
{
  static const auto& fsystem = FSystem::getInstance();
  return fsystem->ioctl(0, CONS_CURSORTYPE, &style) == 0;
}

}  // namespace finalcut

#endif  // defined(__FreeBSD__) || defined(__DragonFly__) || defined(UNIT_TEST)