File: ftermopenbsd.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 (181 lines) | stat: -rw-r--r-- 5,932 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
/***********************************************************************
* ftermopenbsd.cpp - Contains the NetBSD/OpenBSD 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/output/tty/fterm.h"
#include "final/output/tty/ftermopenbsd.h"
#include "final/util/flog.h"
#include "final/util/fsystem.h"

#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(UNIT_TEST)

namespace finalcut
{

// static class attributes
kbd_t  FTermOpenBSD::bsd_keyboard_encoding{0};
bool   FTermOpenBSD::meta_sends_escape{true};


//----------------------------------------------------------------------
// class FTermOpenBSD
//----------------------------------------------------------------------

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

//----------------------------------------------------------------------
auto FTermOpenBSD::isBSDConsole() -> bool
{
  // Check if it's a NetBSD/OpenBSD workstation console

  static kbd_t kbdencoding{};
  static const auto& fsystem = FSystem::getInstance();
  return fsystem->ioctl(0, WSKBDIO_GETENCODING, &kbdencoding) == 0;
}

//----------------------------------------------------------------------
void FTermOpenBSD::init()
{
  // Initialize BSD workstation console

  if ( ! isBSDConsole() )
    return;

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

    // Alt key generate ESC prefix
    setBSDConsoleMetaEsc();
  }
}

//----------------------------------------------------------------------
void FTermOpenBSD::finish()
{
  if ( ! isBSDConsole() )
    return;

  if ( meta_sends_escape )
    resetBSDConsoleEncoding();
}

//----------------------------------------------------------------------
auto FTermOpenBSD::setBeep (int Hz, int ms) -> bool
{
  if ( ! isBSDConsole() )
    return false;

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

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

  wskbd_bell_data bell{};
  bell.which  = WSKBD_BELL_DOALL;
  bell.pitch  = uInt(Hz);
  bell.period = uInt(ms);
  bell.volume = 50;  // 50% volume
  static const auto& fsystem = FSystem::getInstance();
  return fsystem->ioctl(0, WSKBDIO_SETBELL, &bell) >= 0;
}

//----------------------------------------------------------------------
auto FTermOpenBSD::resetBeep() -> bool
{
  wskbd_bell_data default_bell{};
  static const auto& fsystem = FSystem::getInstance();

  // Gets the default setting for the bell
  if ( fsystem->ioctl(0, WSKBDIO_GETDEFAULTBELL, &default_bell) < 0 )
    return false;

  default_bell.which = WSKBD_BELL_DOALL;

  // Sets the bell settings
  return fsystem->ioctl(0, WSKBDIO_SETBELL, &default_bell) >= 0;
}


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

//----------------------------------------------------------------------
auto FTermOpenBSD::saveBSDConsoleEncoding() -> bool
{
  static kbd_t k_encoding{};
  int ret{-1};

  static const auto& fsystem = FSystem::getInstance();
  ret = fsystem->ioctl (0, WSKBDIO_GETENCODING, &k_encoding);

  if ( ret < 0 )
    return false;

  // Save current encoding
  bsd_keyboard_encoding = k_encoding;
  return true;
}

//----------------------------------------------------------------------
auto FTermOpenBSD::setBSDConsoleEncoding (kbd_t k_encoding) -> bool
{
  static const auto& fsystem = FSystem::getInstance();
  return fsystem->ioctl(0, WSKBDIO_SETENCODING, &k_encoding) >= 0;
}

//----------------------------------------------------------------------
auto FTermOpenBSD::setBSDConsoleMetaEsc() -> bool
{
  static constexpr kbd_t meta_esc = 0x20;  // Generate ESC prefix on ALT-key
  return setBSDConsoleEncoding (bsd_keyboard_encoding | meta_esc);
}

//----------------------------------------------------------------------
auto FTermOpenBSD::resetBSDConsoleEncoding() -> bool
{
  return setBSDConsoleEncoding (bsd_keyboard_encoding);
}

}  // namespace finalcut

#endif  // defined(__NetBSD__) || defined(__OpenBSD__) || defined(UNIT_TEST)