lib/TUI/Drivers/HardwareInfo/Termbox.pm

filter: Statementstmt 32.9% 182 / 553 Branchbran 4.5% 12 / 262 Conditioncond 9.6% 15 / 155 MC/DCmcdc 0.0% 0 / 97 Subroutinesub 62.1% 41 / 66 Podpod 0.0% 0 / 27 totaltotal 21.5% 250 / 1160 CC 184 SCAR 47.1
CC
184
Cov
22%
getColorCount
420.0
setScreenMode
306.0
__ANON__
272.0
CRAP 111.4
« lib/TUI/Views/Frame/Line.pm ↑ Summary lib/TUI/Views/View/Write.pm »
1 package TUI::Drivers::HardwareInfo::Termbox;
2 # ABSTRACT: Termbox driver for TUI::Drivers::HardwareInfo
3
4 # -------------------------------------------------------------------------
5 # Boilerplate
6 # -------------------------------------------------------------------------
7
8 102 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 102 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 102 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 102 our $VERSION = '2.000002';
13 102 $VERSION =~ tr/_//d;
14 102 our $AUTHORITY = 'cpan:BRICKPOOL';
15
16 # -------------------------------------------------------------------------
17 # Import modules
18 # -------------------------------------------------------------------------
19
20 102 ▶use constant PERL_ONLY => ( exists $ENV{PERL_ONLY} && $ENV{PERL_ONLY} )
Subroutine BEGIN: called CC=3 SCAR=14.2
21 0 ▶ || not eval { require Termbox; Termbox->VERSION(2); 1 };
Condition33%2/6
exists $ENV{'PERL_ONLY'} && $ENV{'PERL_ONLY'} || !eval { do { require Termbox; 'Termbox'->VERSION(2); 1 } }
l!l&&r!l&&!r
01020
exists $ENV{"PERL_ONLY"} and $ENV{"PERL_ONLY"}
!ll&&!rl&&r
10200
MC/DC0%0/3
exists $ENV{'PERL_ONLY'} && $ENV{'PERL_ONLY'} || !eval { do { require Termbox; 'Termbox'->VERSION(2); 1 } }
exists $ENV{"PERL_ONLY"} $ENV{"PERL_ONLY"} !eval { do { require Termbox; 'Termbox'->VERSION(2); 1 } }
Truth table20%1/5
A and B || not(C)
A= exists $ENV{"PERL_ONLY"}B= $ENV{"PERL_ONLY"}C= !eval { do { require Termbox; 'Termbox'->VERSION(2); 1 } }
ABCresult
11X1
0X11
1011
0X00
1000
22
23 102 ▶use PerlX::Assert::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 102 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 102 ▶use Errno qw( EINTR );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 102 ▶use List::Util qw( min max );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 102 ▶use Scalar::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
28 blessed
29 looks_like_number
30 readonly
31 102 );
32 102 ▶use if PERL_ONLY, 'Termbox::PP';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 102 ▶use Termbox qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
34 102 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
35 102 ▶use TUI::toolkit::boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
36
37 102 ▶use TUI::Drivers::Const qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
38 evCommand
39 evKeyDown
40 :smXXXX
41 :kbXXXX
42 :meXXXX
43 :mbXXXX
44 102 );
45 102 ▶use TUI::Drivers::ScreenCharacter;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 102 ▶use TUI::Drivers::ColorAttr;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 102 ▶use TUI::Drivers::Colors qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
48 BIOStoXTerm16
49 RGBtoXTerm16
50 RGBtoXTerm256
51 XTerm256toXTerm16
52 102 );
53 102 ▶use TUI::Drivers::ScreenCell;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 102 ▶use TUI::Memory qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
55 disposeCache
56 newCache
57 102 );
58 102 ▶use TUI::Views::Const qw( cmScreenChanged );
Subroutine BEGIN: called CC=1 SCAR=0.0
59
60 # -------------------------------------------------------------------------
61 # Define constants
62 # -------------------------------------------------------------------------
63
64 102 ▶use constant TB_NONE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
65
66 # Do not delay too much on ESC key presses, as the Alt modifier works well
67 # in most modern terminals. Still, this delay helps Termbox::PP distinguish
68 # special key sequences.
69 102 ▶use constant ESCDELAY => exists $ENV{ESCDELAY} ? 0+ $ENV{ESCDELAY} : 100;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'ESCDELAY'} ? : 0 102 102
70
71 # The Termbox FFI version doesn't define all keys and attributes,
72 # so we define it here for use in the translation tables.
73 102 ▶use if !PERL_ONLY, constant => {
Subroutine BEGIN: called CC=1 SCAR=0.0
74 TB_KEY_CTRL_A => 0x01,
75 TB_KEY_CTRL_B => 0x02,
76 TB_KEY_CTRL_C => 0x03,
77 TB_KEY_CTRL_D => 0x04,
78 TB_KEY_CTRL_E => 0x05,
79 TB_KEY_CTRL_F => 0x06,
80 TB_KEY_CTRL_G => 0x07,
81 TB_KEY_BACKSPACE => 0x08,
82 TB_KEY_CTRL_H => 0x08,
83 TB_KEY_TAB => 0x09,
84 TB_KEY_CTRL_I => 0x09,
85 TB_KEY_CTRL_J => 0x0a,
86 TB_KEY_CTRL_K => 0x0b,
87 TB_KEY_CTRL_L => 0x0c,
88 TB_KEY_ENTER => 0x0d,
89 TB_KEY_CTRL_M => 0x0d,
90 TB_KEY_CTRL_N => 0x0e,
91 TB_KEY_CTRL_O => 0x0f,
92 TB_KEY_CTRL_P => 0x10,
93 TB_KEY_CTRL_Q => 0x11,
94 TB_KEY_CTRL_R => 0x12,
95 TB_KEY_CTRL_S => 0x13,
96 TB_KEY_CTRL_T => 0x14,
97 TB_KEY_CTRL_U => 0x15,
98 TB_KEY_CTRL_V => 0x16,
99 TB_KEY_CTRL_W => 0x17,
100 TB_KEY_CTRL_X => 0x18,
101 TB_KEY_CTRL_Y => 0x19,
102 TB_KEY_CTRL_Z => 0x1a,
103 TB_KEY_ESC => 0x1b,
104 TB_KEY_SPACE => 0x20,
105 TB_KEY_BACKSPACE2 => 0x7f,
106 # That's strange, the following attributes do not rely on truecolor
107 TB_HI_BLACK => 0x2000,
108 TB_BRIGHT => 0x4000,
109 102 };
110
111 102 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
112 *TB_OUTPUT_TRUECOLOR = sub (){ 5 }
113 102 ▶ unless defined &TB_OUTPUT_TRUECOLOR;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &TUI::Drivers::HardwareInfo::Termbox::TB_OUTPUT_TRUECOLOR 102 0 102
114 }
115
116 # -------------------------------------------------------------------------
117 # Declare global variables
118 # -------------------------------------------------------------------------
119
120 102 our $insertState = true;
121 102 our $platform = '';
122 102 our $pendingEvent = 0;
123
124 # Track mouse button state.
125 102 our $lastButtons = 0;
126
127 # -------------------------------------------------------------------------
128 # Import global variables
129 # -------------------------------------------------------------------------
130
131 102 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
132 $ctrlBreakHit
133 102 );
134 {
135 102 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
136 102 *ctrlBreakHit = \$TUI::Drivers::SystemError::ctrlBreakHit;
137 }
138
139 # -------------------------------------------------------------------------
140 # Local variables
141 # -------------------------------------------------------------------------
142
143 102 my $screenMode = 0;
144 102 my $outputMode = 0;
145 102 my $initialized = false;
146 102 my $cursorSize = 15;
147 102 my @lastSize = ( 0, 0 );
148 102 my $tb_event = Termbox::Event->new();
149
150 # Codepage 437 to Unicode translation map.
151 102 my @CP437_TO_UTF8 = (
152 ord "\x{2007}", ord "\x{263a}", ord "\x{263b}", ord "\x{2665}",
153 ord "\x{2666}", ord "\x{2663}", ord "\x{2660}", ord "\x{2022}",
154 ord "\x{25d8}", ord "\x{25cb}", ord "\x{25d9}", ord "\x{2642}",
155 ord "\x{2640}", ord "\x{266a}", ord "\x{266b}", ord "\x{263c}",
156 # Termius has 25b6 and 25c0 here, which are better unicode equivalents.
157 # ord "\x{25ba}", ord "\x{25c4}", ord "\x{2195}", ord "\x{203c}",
158 ord "\x{25b6}", ord "\x{25c0}", ord "\x{2195}", ord "\x{203c}",
159 ord "\x{00b6}", ord "\x{00a7}", ord "\x{25ac}", ord "\x{21a8}",
160 ord "\x{2191}", ord "\x{2193}", ord "\x{2192}", ord "\x{2190}",
161 ord "\x{221f}", ord "\x{2194}", ord "\x{25b2}", ord "\x{25bc}",
162 ord "\x{0020}", ord "\x{0021}", ord "\x{0022}", ord "\x{0023}",
163 ord "\x{0024}", ord "\x{0025}", ord "\x{0026}", ord "\x{0027}",
164 ord "\x{0028}", ord "\x{0029}", ord "\x{002a}", ord "\x{002b}",
165 ord "\x{002c}", ord "\x{002d}", ord "\x{002e}", ord "\x{002f}",
166 ord "\x{0030}", ord "\x{0031}", ord "\x{0032}", ord "\x{0033}",
167 ord "\x{0034}", ord "\x{0035}", ord "\x{0036}", ord "\x{0037}",
168 ord "\x{0038}", ord "\x{0039}", ord "\x{003a}", ord "\x{003b}",
169 ord "\x{003c}", ord "\x{003d}", ord "\x{003e}", ord "\x{003f}",
170 ord "\x{0040}", ord "\x{0041}", ord "\x{0042}", ord "\x{0043}",
171 ord "\x{0044}", ord "\x{0045}", ord "\x{0046}", ord "\x{0047}",
172 ord "\x{0048}", ord "\x{0049}", ord "\x{004a}", ord "\x{004b}",
173 ord "\x{004c}", ord "\x{004d}", ord "\x{004e}", ord "\x{004f}",
174 ord "\x{0050}", ord "\x{0051}", ord "\x{0052}", ord "\x{0053}",
175 ord "\x{0054}", ord "\x{0055}", ord "\x{0056}", ord "\x{0057}",
176 ord "\x{0058}", ord "\x{0059}", ord "\x{005a}", ord "\x{005b}",
177 ord "\x{005c}", ord "\x{005d}", ord "\x{005e}", ord "\x{005f}",
178 ord "\x{0060}", ord "\x{0061}", ord "\x{0062}", ord "\x{0063}",
179 ord "\x{0064}", ord "\x{0065}", ord "\x{0066}", ord "\x{0067}",
180 ord "\x{0068}", ord "\x{0069}", ord "\x{006a}", ord "\x{006b}",
181 ord "\x{006c}", ord "\x{006d}", ord "\x{006e}", ord "\x{006f}",
182 ord "\x{0070}", ord "\x{0071}", ord "\x{0072}", ord "\x{0073}",
183 ord "\x{0074}", ord "\x{0075}", ord "\x{0076}", ord "\x{0077}",
184 ord "\x{0078}", ord "\x{0079}", ord "\x{007a}", ord "\x{007b}",
185 ord "\x{007c}", ord "\x{007d}", ord "\x{007e}", ord "\x{2302}",
186 ord "\x{00c7}", ord "\x{00fc}", ord "\x{00e9}", ord "\x{00e2}",
187 ord "\x{00e4}", ord "\x{00e0}", ord "\x{00e5}", ord "\x{00e7}",
188 ord "\x{00ea}", ord "\x{00eb}", ord "\x{00e8}", ord "\x{00ef}",
189 ord "\x{00ee}", ord "\x{00ec}", ord "\x{00c4}", ord "\x{00c5}",
190 ord "\x{00c9}", ord "\x{00e6}", ord "\x{00c6}", ord "\x{00f4}",
191 ord "\x{00f6}", ord "\x{00f2}", ord "\x{00fb}", ord "\x{00f9}",
192 ord "\x{00ff}", ord "\x{00d6}", ord "\x{00dc}", ord "\x{00a2}",
193 ord "\x{00a3}", ord "\x{00a5}", ord "\x{20a7}", ord "\x{0192}",
194 ord "\x{00e1}", ord "\x{00ed}", ord "\x{00f3}", ord "\x{00fa}",
195 ord "\x{00f1}", ord "\x{00d1}", ord "\x{00aa}", ord "\x{00ba}",
196 ord "\x{00bf}", ord "\x{2310}", ord "\x{00ac}", ord "\x{00bd}",
197 ord "\x{00bc}", ord "\x{00a1}", ord "\x{00ab}", ord "\x{00bb}",
198 ord "\x{2591}", ord "\x{2592}", ord "\x{2593}", ord "\x{2502}",
199 ord "\x{2524}", ord "\x{2561}", ord "\x{2562}", ord "\x{2556}",
200 ord "\x{2555}", ord "\x{2563}", ord "\x{2551}", ord "\x{2557}",
201 ord "\x{255d}", ord "\x{255c}", ord "\x{255b}", ord "\x{2510}",
202 ord "\x{2514}", ord "\x{2534}", ord "\x{252c}", ord "\x{251c}",
203 ord "\x{2500}", ord "\x{253c}", ord "\x{255e}", ord "\x{255f}",
204 ord "\x{255a}", ord "\x{2554}", ord "\x{2569}", ord "\x{2566}",
205 ord "\x{2560}", ord "\x{2550}", ord "\x{256c}", ord "\x{2567}",
206 ord "\x{2568}", ord "\x{2564}", ord "\x{2565}", ord "\x{2559}",
207 ord "\x{2558}", ord "\x{2552}", ord "\x{2553}", ord "\x{256b}",
208 ord "\x{256a}", ord "\x{2518}", ord "\x{250c}", ord "\x{2588}",
209 ord "\x{2584}", ord "\x{258c}", ord "\x{2590}", ord "\x{2580}",
210 ord "\x{03b1}", ord "\x{00df}", ord "\x{0393}", ord "\x{03c0}",
211 ord "\x{03a3}", ord "\x{03c3}", ord "\x{00b5}", ord "\x{03c4}",
212 ord "\x{03a6}", ord "\x{0398}", ord "\x{03a9}", ord "\x{03b4}",
213 ord "\x{221e}", ord "\x{03c6}", ord "\x{03b5}", ord "\x{2229}",
214 ord "\x{2261}", ord "\x{00b1}", ord "\x{2265}", ord "\x{2264}",
215 ord "\x{2320}", ord "\x{2321}", ord "\x{00f7}", ord "\x{2248}",
216 ord "\x{00b0}", ord "\x{2219}", ord "\x{00b7}", ord "\x{221a}",
217 ord "\x{207f}", ord "\x{00b2}", ord "\x{25a0}", ord "\x{00a0}",
218 );
219 102 my @UTF8_TO_CP437 = map { chr } @CP437_TO_UTF8;
220
221 # Key Event conversion
222 my (
223 102 $convertCharCode,
224 $convertKeyCode,
225 );
226
227 # Attribute conversion
228 102 my $TB_ATTR;
229 my (
230 102 $convertColor,
231 $convertNoColor,
232 $convertIndexed8,
233 $convertIndexed16,
234 $convertIndexed256,
235 $convertDirect,
236 );
237
238 # -------------------------------------------------------------------------
239 # Initialization and cleanup
240 # -------------------------------------------------------------------------
241
242 102 ▶INIT {
Subroutine INIT: called CC=4 SCAR=15.9
243 102 ▶ if ( eval { require Perl::OSType; 1 } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { require Perl::OSType; 1 } }) { } 102 0 102
elsif ($TUI::Drivers::HardwareInfo::Termbox::OSNAME eq 'MSWin32') { } 0 0 0
244 102 $platform = Perl::OSType::os_type( $OSNAME );
245 } elsif ( $OSNAME eq 'MSWin32' ) {
246 0 $platform = 'Windows';
247 }
248 102 ▶ $platform ||= 'Unix';
Condition50%1/2
$TUI::Drivers::HardwareInfo::Termbox::platform ||= "Unix"
l!l
1020
MC/DC0%0/1
$TUI::Drivers::HardwareInfo::Termbox::platform ||= "Unix"
$TUI::Drivers::HardwareInfo::Termbox::platform
Truth table50%1/2
A
A= $TUI::Drivers::HardwareInfo::Termbox::platform
Aresult
11
00
249 102 __PACKAGE__->resume();
250 }
251
252 102 ▶sub resume { # void ($class)
Subroutine resume: called CC=12 SCAR=44.2
Pod undocumented
253 102 my ( $class ) = @_;
254 102 assert ( @_ == 1 );
255 102 ▶ assert ( $class and !ref $class );
Condition33%1/3
$class && !ref($class)
!ll&&!rl&&r
00102
MC/DC0%0/2
$class && !ref($class)
$class !ref($class)
Truth table33%1/3
A && not(B)
A= $classB= !ref($class)
ABresult
0X0
100
111
256
257 102 ▶ return if $initialized;
Branch: 1 branch
BranchTrueFalseTotal
if $initialized 0 102 102
258 102 newCache( $TB_ATTR, 256 );
259
260 # https://github.com/neovim/neovim/issues/36635
261 102 ▶ $ENV{TERM} //= 'xterm-256color' if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 102 102
Condition0%0/2
$ENV{"TERM"} //= "xterm-256color"
l!l
00
MC/DC0%0/1
$ENV{"TERM"} //= "xterm-256color"
$ENV{"TERM"}
Truth table0%0/2
A
A= $ENV{"TERM"}
Aresult
11
00
262
263 # Initialize Termbox and set the input/output modes.
264 102 my $err = tb_init();
265 102 ▶ return if $err != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $err != 0 102 0 102
266 0 $err = tb_set_input_mode( TB_INPUT_ALT | TB_INPUT_MOUSE );
267 0 ▶ return if $err != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $err != 0 0 0 0
268 0 ▶ $outputMode = $class->getColorCount() >= 16
Branch: 1 branch
BranchTrueFalseTotal
$class->getColorCount >= 16 ? : 0 0 0
269 ? TB_OUTPUT_256
270 : TB_OUTPUT_NORMAL;
271 0 $err = tb_set_output_mode( $outputMode );
272 0 ▶ return if $err != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $err != 0 0 0 0
273 0 my $cols = tb_width();
274 0 ▶ return if $cols <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cols <= 0 0 0 0
275 0 my $rows = tb_height();
276 0 ▶ return if $rows <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rows <= 0 0 0 0
277
278 0 @lastSize = ( $cols, $rows );
279
280 # NOTE: The following workaround for detecting a single TB_KEY_ESC relies
281 # on deprecated function tb_set_func() and Termbox::PP internals.
282 # The tb_set_func() API itself is backend-independent, but the access to
283 # raw input buffer is currently only available in the PP backend.
284 0 if ( PERL_ONLY ) {
285 102 ▶ no warnings 'deprecated';
Subroutine BEGIN: called CC=1 SCAR=0.0
286 0 ▶ $err = tb_set_func( TB_FUNC_EXTRACT_PRE, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
287 0 my ( $event, $consumed_ref ) = @_;
288
289 0 state $esc_seen_at;
290 0 ▶ if ( $Termbox::global->{inbuf} eq "\e" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Termbox::global->{"inbuf"} eq "\e") 0 0 0
291 0 my $now = int(( time() - $BASETIME ) * 1000);
292 0 ▶ $esc_seen_at //= $now;
Condition0%0/3
$esc_seen_at //= $now
l!l&&r!l&&!r
000
MC/DC0%0/2
$esc_seen_at //= $now
$esc_seen_at $now
Truth table0%0/3
A //= B
A= $esc_seen_atB= $now
ABresult
1X1
011
000
293 0 my $elapsed = $now - $esc_seen_at;
294
295 0 ▶ return TB_ERR_NEED_MORE
Branch: 1 branch
BranchTrueFalseTotal
if $elapsed < 100 0 0 0
296 if $elapsed < ESCDELAY;
297
298 0 $$consumed_ref = 1;
299
300 0 $event->{type} = TB_EVENT_KEY;
301 0 $event->{mod} = TB_NONE;
302 0 $event->{key} = TB_KEY_ESC;
303 0 $event->{ch} = 0;
304
305 0 $esc_seen_at = undef;
306 0 return TB_OK;
307 }
308 0 $esc_seen_at = undef;
309 0 return TB_ERR;
310 0 });
311 0 ▶ return tb_strerror( $err ) if $err != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $err != 0 0 0 0
312 }
313
314 0 $initialized = true;
315 0 return;
316 }
317
318 102 ▶END {
Subroutine END: called CC=1 SCAR=0.0
319 102 __PACKAGE__->suspend();
320 }
321
322 102 ▶sub suspend { # void ($class)
Subroutine suspend: called CC=6 SCAR=31.7
Pod undocumented
323 102 assert ( @_ == 1 );
324 102 ▶ assert ( $_[0] and !ref $_[0] );
Condition33%1/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
00102
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table33%1/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
325
326 102 ▶ return unless $initialized;
Branch: 1 branch
BranchTrueFalseTotal
unless $initialized 102 0 102
327
328 # Restore the cursor style on exit
329 # https://unix.stackexchange.com/q/697650
330 {
331 0 ▶ my $term = $ENV{TERM} // '';
Condition0%0/2
$ENV{"TERM"} // ""
l!l
00
MC/DC0%0/1
$ENV{"TERM"} // ""
$ENV{"TERM"}
Truth table0%0/2
A
A= $ENV{"TERM"}
Aresult
11
00
332 0 ▶ if ( $term eq 'linux' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($term eq 'linux') { } 0 0 0
elsif ($term) { } 0 0 0
333 0 tb_send( $_ = "\x1B[?0c", bytes::length( $_ ) );
334 } elsif ( $term ) {
335 0 tb_send( $_ = "\x1B[0 q", bytes::length( $_ ) );
336 }
337 }
338 0 tb_set_func( TB_FUNC_EXTRACT_PRE, undef ) if PERL_ONLY;
339 0 tb_shutdown();
340
341 0 disposeCache( $TB_ATTR );
342 0 $initialized = false;
343 0 return;
344 }
345
346 # -------------------------------------------------------------------------
347 # General system functions
348 # -------------------------------------------------------------------------
349
350 1 ▶sub getTickCount { # $ticks ($class)
Subroutine getTickCount: called CC=2 SCAR=7.6
Pod undocumented
351 1 assert ( @_ == 1 );
352 1 ▶ assert ( $_[0] and !ref $_[0] );
Condition33%1/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
001
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table33%1/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
353 # Return Turbo Vision compatible clock ticks (~18.2 Hz).
354 1 return int( ( time() - $BASETIME ) * 1000 / 55 );
355 }
356
357 1 ▶sub getPlatform { # $osname ($class)
Subroutine getPlatform: called CC=2 SCAR=7.6
Pod undocumented
358 1 assert ( @_ == 1 );
359 1 ▶ assert ( $_[0] and !ref $_[0] );
Condition33%1/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
001
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table33%1/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
360 1 return $platform;
361 }
362
363 # -------------------------------------------------------------------------
364 # Caret functions
365 # -------------------------------------------------------------------------
366
367 2 ▶sub setCaretSize { # void ($class, $size)
Subroutine setCaretSize: called CC=9 SCAR=40.6
Pod undocumented
368 2 my ( $class, $size ) = @_;
369 2 assert ( @_ == 2 );
370 2 ▶ assert ( $class and !ref $class );
Condition33%1/3
$class && !ref($class)
!ll&&!rl&&r
002
MC/DC0%0/2
$class && !ref($class)
$class !ref($class)
Truth table33%1/3
A && not(B)
A= $classB= !ref($class)
ABresult
0X0
100
111
371 2 assert ( looks_like_number $size );
372 2 assert ( $initialized );
373 0 ▶ if ( $size <= 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size <= 0) { } 0 0 0
elsif ($size != $cursorSize) { } 0 0 0
374 0 $cursorSize = 0; # hidden
375 0 tb_hide_cursor();
376 }
377 elsif ( $size != $cursorSize ) {
378 0 $cursorSize = max( 0, min( $size, 100 ) );
379
380 0 ▶ my $term = $ENV{TERM} // '';
Condition0%0/2
$ENV{"TERM"} // ""
l!l
00
MC/DC0%0/1
$ENV{"TERM"} // ""
$ENV{"TERM"}
Truth table0%0/2
A
A= $ENV{"TERM"}
Aresult
11
00
381 0 ▶ if ( $term eq 'linux' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($term eq 'linux') { } 0 0 0
elsif ($term) { } 0 0 0
382 # Linux VGA console cursor style
383 # https://unix.stackexchange.com/a/92743
384 0 my $raw = sprintf(
385 "\x1B[?%dc",
386 2 + int( ( $cursorSize - 1 ) * 4 / 99 + 0.5 )
387 );
388 0 tb_send( $raw, bytes::length( $raw ) );
389 }
390 elsif ( $term ) {
391 # DECSCUSR (DEC Set Cursor Style)
392 # https://unix.stackexchange.com/a/597558
393 0 my $raw;
394 0 ▶ if ( $cursorSize < 50 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cursorSize < 50) { } 0 0 0
elsif ($cursorSize < 100) { } 0 0 0
395 0 $raw = "\x1B[3 q"; # blinking underline
396 }
397 elsif ( $cursorSize < 100 ) {
398 0 $raw = "\x1B[5 q"; # blinking bar
399 }
400 else {
401 0 $raw = "\x1B[1 q"; # blinking block
402 }
403 0 $raw .= "\x1B[?25h";
404 0 tb_send( $raw, bytes::length( $raw ) );
405 }
406 else {
407 # Show cursor; (-1,-1) is clamped to (0,0).
408 0 tb_set_cursor( -1, -1 );
409 }
410 }
411 0 return;
412 }
413
414 2 ▶sub getCaretSize { # $size ($class)
Subroutine getCaretSize: called CC=2 SCAR=7.6
Pod undocumented
415 2 assert ( @_ == 1 );
416 2 ▶ assert ( $_[0] and !ref $_[0] );
Condition33%1/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
002
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table33%1/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
417 2 return min( max( $cursorSize, 0 ), 100 );
418 }
419
420 0 ▶sub setCaretPosition { # void ($class, $x, $y)
Subroutine setCaretPosition: not called CC=2 SCAR=17.9
Pod undocumented
421 0 my ( $class, $x, $y ) = @_;
422 0 assert ( @_ == 3 );
423 0 ▶ assert ( $class and !ref $class );
Condition0%0/3
$class && !ref($class)
!ll&&!rl&&r
000
MC/DC0%0/2
$class && !ref($class)
$class !ref($class)
Truth table0%0/3
A && not(B)
A= $classB= !ref($class)
ABresult
0X0
100
111
424 0 assert ( looks_like_number $x );
425 0 assert ( looks_like_number $y );
426 0 assert ( $initialized );
427 0 tb_set_cursor( $x, $y );
428 0 return;
429 }
430
431 1 ▶sub isCaretVisible { # $visible ($class)
Subroutine isCaretVisible: called CC=2 SCAR=7.6
Pod undocumented
432 1 assert ( @_ == 1 );
433 1 ▶ assert ( $_[0] and !ref $_[0] );
Condition33%1/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
001
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table33%1/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
434 1 return $cursorSize > 0;
435 }
436
437 # -------------------------------------------------------------------------
438 # Screen functions
439 # -------------------------------------------------------------------------
440
441 1 ▶sub getScreenRows { # $rows ($class)
Subroutine getScreenRows: called CC=4 SCAR=22.3
Pod undocumented
442 1 assert ( @_ == 1 );
443 1 ▶ assert ( $_[0] and !ref $_[0] );
Condition33%1/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
001
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table33%1/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
444 1 assert ( $initialized );
445 0 my $rows = tb_height();
446 0 ▶ return 25
Branch: 1 branch
BranchTrueFalseTotal
if $rows == 0 0 0 0
447 if $rows == 0; # Borland's compatibility DOS default (only for rows)
448 0 ▶ return $rows > 0 ? $rows : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rows > 0 ? : 0 0 0
449 }
450
451 0 ▶sub getScreenCols { # $cols ($class)
Subroutine getScreenCols: not called CC=3 SCAR=24.8
Pod undocumented
452 0 assert ( @_ == 1 );
453 0 ▶ assert ( $_[0] and !ref $_[0] );
Condition0%0/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table0%0/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
454 0 assert ( $initialized );
455 0 my $cols = tb_width();
456 0 ▶ return $cols > 0 ? $cols : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cols > 0 ? : 0 0 0
457 }
458
459 95 ▶sub getScreenMode { # $mode ($class)
Subroutine getScreenMode: called CC=7 SCAR=35.8
Pod undocumented
460 95 assert ( @_ == 1 );
461 95 ▶ assert ( $_[0] and !ref $_[0] );
Condition33%1/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
0095
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table33%1/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
462 95 assert ( $initialized );
463
464 0 my $rows = tb_height();
465 0 ▶ return 0 unless $rows > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $rows > 0 0 0 0
466
467 # Invalid or unspecified mode.
468 0 ▶ unless ( $screenMode ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($screenMode) 0 0 0
469 # https://no-color.org/
470 $screenMode = exists $ENV{NO_COLOR} && $ENV{NO_COLOR}
471 0 ▶ ? smMono
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'NO_COLOR'} && $ENV{'NO_COLOR'} ? : 0 0 0
Condition0%0/3
exists $ENV{"NO_COLOR"} and $ENV{"NO_COLOR"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ENV{"NO_COLOR"} and $ENV{"NO_COLOR"}
exists $ENV{"NO_COLOR"} $ENV{"NO_COLOR"}
Truth table0%0/3
A and B
A= exists $ENV{"NO_COLOR"}B= $ENV{"NO_COLOR"}
ABresult
0X0
100
111
472 : smCO80;
473 }
474
475 0 my $mode = $screenMode;
476 0 $mode &= ~smFont8x8;
477 0 ▶ $mode |= smFont8x8 if $rows > 25;
Branch: 1 branch
BranchTrueFalseTotal
if $rows > 25 0 0 0
478 0 return $mode;
479 }
480
481 0 ▶sub setScreenMode { # void ($class, $mode)
Subroutine setScreenMode: not called CC=17 SCAR=57.2
Pod undocumented
482 0 my ( $class, $mode ) = @_;
483 0 assert ( @_ == 2 );
484 0 ▶ assert ( $class and !ref $class );
Condition0%0/3
$class && !ref($class)
!ll&&!rl&&r
000
MC/DC0%0/2
$class && !ref($class)
$class !ref($class)
Truth table0%0/3
A && not(B)
A= $classB= !ref($class)
ABresult
0X0
100
111
485 0 assert ( looks_like_number $mode );
486 0 assert ( $initialized );
487
488 0 ▶ return unless tb_height() > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless tb_height() > 0 0 0 0
489
490 # Fix the requested mode to a valid output mode.
491 0 my $base = $mode & 0xff;
492 0 ▶ if ( $base != smCO80
Branch: 1 branch
BranchTrueFalseTotal
if ($base != 3 and $base != 2 and $base != 7) 0 0 0
Condition0%0/6
$base != 3 and $base != 2 and $base != 7
!ll&&!rl&&r
000
$base != 3 and $base != 2
!ll&&!rl&&r
000
MC/DC0%0/3
$base != 3 and $base != 2 and $base != 7
$base != 3 $base != 2 $base != 7
Truth table0%0/4
A and B and C
A= $base != 3B= $base != 2C= $base != 7
ABCresult
0XX0
10X0
1100
1111
493 && $base != smBW80
494 && $base != smMono
495 ) {
496 0 $mode = ( $mode & 0xff00 ) | smCO80;
497 }
498 0 $mode &= ~smUpdate;
499 0 ▶ $mode &= ~smColorHigh
Branch: 1 branch
BranchTrueFalseTotal
if $mode & 1024 and $class->getColorCount < 16777216 0 0 0
Condition0%0/3
$mode & 1024 and $class->getColorCount < 16777216
!ll&&!rl&&r
000
MC/DC0%0/2
$mode & 1024 and $class->getColorCount < 16777216
$mode & 1024 $class->getColorCount < 16777216
Truth table0%0/3
A and B
A= $mode & 1024B= $class->getColorCount < 16777216
ABresult
0X0
100
111
500 if ( $mode & smColorHigh )
501 && $class->getColorCount() < 256*256*256;
502 0 ▶ $mode &= ~smColor256
Branch: 1 branch
BranchTrueFalseTotal
if $mode & 512 and $class->getColorCount < 256 0 0 0
Condition0%0/3
$mode & 512 and $class->getColorCount < 256
!ll&&!rl&&r
000
MC/DC0%0/2
$mode & 512 and $class->getColorCount < 256
$mode & 512 $class->getColorCount < 256
Truth table0%0/3
A and B
A= $mode & 512B= $class->getColorCount < 256
ABresult
0X0
100
111
503 if ( $mode & smColor256 )
504 && $class->getColorCount() < 256;
505
506 # Set the appropriate output mode based on the requested screen mode.
507 0 ▶ if ( $mode & smColorHigh ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode & 1024) { } 0 0 0
elsif ($mode & 512) { } 0 0 0
elsif ($class->getColorCount >= 16) { } 0 0 0
508 0 ▶ tb_set_output_mode( TB_OUTPUT_TRUECOLOR )
Branch: 1 branch
BranchTrueFalseTotal
if $outputMode != 5 0 0 0
509 if $outputMode != TB_OUTPUT_TRUECOLOR
510 }
511 elsif ( $mode & smColor256 ) {
512 0 ▶ tb_set_output_mode( TB_OUTPUT_256 )
Branch: 1 branch
BranchTrueFalseTotal
if $outputMode != 2 0 0 0
513 if $outputMode != TB_OUTPUT_256;
514 }
515 elsif ( $class->getColorCount() >= 16 ) {
516 0 ▶ tb_set_output_mode( TB_OUTPUT_256 )
Branch: 1 branch
BranchTrueFalseTotal
if $outputMode != 2 0 0 0
517 if $outputMode != TB_OUTPUT_256;
518 }
519 else {
520 0 ▶ tb_set_output_mode( TB_OUTPUT_NORMAL )
Branch: 1 branch
BranchTrueFalseTotal
if $outputMode != 1 0 0 0
521 if $outputMode != TB_OUTPUT_NORMAL;
522 }
523
524 # Clear the attribute cache, since the color mapping has changed.
525 0 %$TB_ATTR = ();
526
527 # Save the current output state
528 0 $screenMode = $mode;
529 0 $outputMode = tb_set_output_mode( TB_OUTPUT_CURRENT );
530
531 0 return;
532 }
533
534 0 ▶sub clearScreen { # void ($class, $w, $h)
Subroutine clearScreen: not called CC=2 SCAR=17.9
Pod undocumented
535 0 my ( $class, $w, $h ) = @_;
536 0 assert ( @_ == 3 );
537 0 ▶ assert ( $class and !ref $class );
Condition0%0/3
$class && !ref($class)
!ll&&!rl&&r
000
MC/DC0%0/2
$class && !ref($class)
$class !ref($class)
Truth table0%0/3
A && not(B)
A= $classB= !ref($class)
ABresult
0X0
100
111
538 0 assert ( looks_like_number $w );
539 0 assert ( looks_like_number $h );
540 0 assert ( $initialized );
541 0 tb_clear();
542 0 return;
543 }
544
545 0 ▶sub screenWrite { # void ($class, $x, $y, $buf, $len)
Subroutine screenWrite: not called CC=4 SCAR=30.0
Pod undocumented
546 0 my ( $class, $x, $y, $buf, $len ) = @_;
547 0 assert ( @_ == 5 );
548 0 ▶ assert ( $class and !ref $class );
Condition0%0/3
$class && !ref($class)
!ll&&!rl&&r
000
MC/DC0%0/2
$class && !ref($class)
$class !ref($class)
Truth table0%0/3
A && not(B)
A= $classB= !ref($class)
ABresult
0X0
100
111
549 0 assert ( looks_like_number $x );
550 0 assert ( looks_like_number $y );
551 0 assert ( ref $buf );
552 0 assert ( looks_like_number $len );
553 0 assert ( $initialized );
554
555 0 ▶ for ( my $i = 0 ; $i < $len ; ++$i, ++$x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 0 0 0
556 0 my $cell = $buf->[$i];
557 0 my $character = $cell->[0];
558 0 my $attribute = $cell->[1];
559
560 # Fast path equivalent for the code below:
561 # my $ch = Encode::decode( cp437 => $character->getText() );
562 0 my $ch = $UTF8_TO_CP437[ ord( $$character ) & 0xff ];
563
564 # Cached attribute conversion for the code below:
565 # my $attr = [
566 # $attribute->getForeground->$convertColor( true ),
567 # $attribute->getBackground->$convertColor( false ),
568 # ];
569 0 my $attr = $TB_ATTR->{$$attribute};
570 0 ▶ unless ( $attr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attr) 0 0 0
571 0 $attr = [
572 $attribute->getForeground->$convertColor( true ),
573 $attribute->getBackground->$convertColor( false ),
574 ];
575 0 $TB_ATTR->{$$attribute} = $attr;
576 }
577
578 0 tb_set_cell( $x, $y, $ch, @$attr );
579 }
580
581 0 tb_present();
582 0 return;
583 }
584
585 0 ▶sub allocateScreenBuffer { # \@buffer ($class)
Subroutine allocateScreenBuffer: not called CC=6 SCAR=37.4
Pod undocumented
586 0 my ( $class ) = @_;
587 0 assert ( @_ == 1 );
588 0 ▶ assert ( $class and !ref $class );
Condition0%0/3
$class && !ref($class)
!ll&&!rl&&r
000
MC/DC0%0/2
$class && !ref($class)
$class !ref($class)
Truth table0%0/3
A && not(B)
A= $classB= !ref($class)
ABresult
0X0
100
111
589 0 assert ( $initialized );
590 0 $class->reloadScreenInfo();
591
592 0 my $cols = tb_width();
593 0 my $rows = tb_height();
594
595 0 ▶ return []
Branch: 1 branch
BranchTrueFalseTotal
if $cols <= 0 or $rows <= 0 0 0 0
Condition0%0/3
$cols <= 0 or $rows <= 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$cols <= 0 or $rows <= 0
$cols <= 0 $rows <= 0
Truth table0%0/3
A or B
A= $cols <= 0B= $rows <= 0
ABresult
1X1
011
000
596 if $cols <= 0 || $rows <= 0;
597
598 # Make sure we allocate at least enough for a 80x50 screen.
599 0 ▶ $cols = 80 if $cols < 80;
Branch: 1 branch
BranchTrueFalseTotal
if $cols < 80 0 0 0
600 0 ▶ $rows = 50 if $rows < 50;
Branch: 1 branch
BranchTrueFalseTotal
if $rows < 50 0 0 0
601
602 0 return [ map { TScreenCell->new() } 1 .. $cols * $rows ];
603 }
604
605 0 ▶sub freeScreenBuffer { # void ($class, \@buffer)
Subroutine freeScreenBuffer: not called CC=3 SCAR=24.8
Pod undocumented
606 0 assert ( @_ == 2 );
607 0 ▶ assert ( $_[0] and !ref $_[0] );
Condition0%0/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table0%0/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
608 0 ▶ assert ( ref $_[1] and !readonly @{ $_[1] } );
Condition0%0/3
ref $_[1] && !&readonly(scalar @{$_[1];})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[1] && !&readonly(scalar @{$_[1];})
ref $_[1] !&readonly(scalar @{$_[1];})
Truth table0%0/3
A && not(B)
A= ref $_[1]B= !&readonly(scalar @{$_[1];})
ABresult
0X0
100
111
609 0 $_[1] = [];
610 0 return;
611 }
612
613 # -------------------------------------------------------------------------
614 # Mouse functions
615 # -------------------------------------------------------------------------
616
617 96 ▶sub getButtonCount { # $num ($class)
Subroutine getButtonCount: called CC=3 SCAR=12.5
Pod undocumented
618 96 assert ( @_ == 1 );
619 96 ▶ assert ( $_[0] and !ref $_[0] );
Condition33%1/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
0096
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table33%1/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
620
621 # Termbox reports mouse events, but not a physical button count.
622 # Return a practical compatibility value.
623 96 ▶ return $initialized ? 3 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$initialized ? : 0 96 96
624 }
625
626 96 ▶sub cursorOn { # void ($class)
Subroutine cursorOn: called CC=2 SCAR=9.9
Pod undocumented
627 96 assert ( @_ == 1 );
628 96 ▶ assert ( $_[0] and !ref $_[0] );
Condition33%1/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
0096
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table33%1/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
629 96 assert ( $initialized );
630 0 my $mode = tb_set_input_mode( TB_INPUT_CURRENT );
631 0 tb_set_input_mode( $mode | TB_INPUT_MOUSE );
632 0 return;
633 }
634
635 133 ▶sub cursorOff { # void ($class)
Subroutine cursorOff: called CC=2 SCAR=9.9
Pod undocumented
636 133 assert ( @_ == 1 );
637 133 ▶ assert ( $_[0] and !ref $_[0] );
Condition33%1/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
00133
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table33%1/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
638 133 assert ( $initialized );
639 0 my $mode = tb_set_input_mode( TB_INPUT_CURRENT );
640 0 tb_set_input_mode( $mode & ~TB_INPUT_MOUSE );
641 0 return;
642 }
643
644 # -------------------------------------------------------------------------
645 # Event functions
646 # -------------------------------------------------------------------------
647
648 0 ▶sub clearPendingEvent { # void ($class)
Subroutine clearPendingEvent: not called CC=2 SCAR=17.9
Pod undocumented
649 0 assert ( @_ == 1 );
650 0 ▶ assert ( $_[0] and !ref $_[0] );
Condition0%0/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table0%0/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
651 0 $pendingEvent = 0;
652 0 return;
653 }
654
655 0 ▶sub getMouseEvent { # $bool ($class, $event)
Subroutine getMouseEvent: not called CC=11 SCAR=48.8
Pod undocumented
656 0 my ( $class, $event ) = @_;
657 0 assert ( @_ == 2 );
658 0 ▶ assert ( $class and !ref $class );
Condition0%0/3
$class && !ref($class)
!ll&&!rl&&r
000
MC/DC0%0/2
$class && !ref($class)
$class !ref($class)
Truth table0%0/3
A && not(B)
A= $classB= !ref($class)
ABresult
0X0
100
111
659 0 assert ( blessed $event );
660 0 assert ( $initialized );
661
662 # Check for pending events
663 0 ▶ unless ( $pendingEvent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($TUI::Drivers::HardwareInfo::Termbox::pendingEvent) 0 0 0
664 0 my $rv = tb_peek_event( $tb_event, 0 );
665 0 ▶ $pendingEvent = 1 if $rv == TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 0 0 0
666 }
667
668 # Return false if there are no pending events
669 0 ▶ return false
Branch: 1 branch
BranchTrueFalseTotal
unless $TUI::Drivers::HardwareInfo::Termbox::pendingEvent 0 0 0
670 unless $pendingEvent;
671
672 # Return if the event is not a mouse event
673 0 ▶ return false
Branch: 1 branch
BranchTrueFalseTotal
if $tb_event->type != 3 0 0 0
674 if $tb_event->type != TB_EVENT_MOUSE;
675
676 # Track the state of mouse buttons
677 0 my $buttons = $lastButtons;
678 0 ▶ if ( $tb_event->key == TB_KEY_MOUSE_LEFT ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tb_event->key == 65512) { } 0 0 0
elsif ($tb_event->key == 65511) { } 0 0 0
elsif ($tb_event->key == 65509) { } 0 0 0
679 0 $buttons |= mbLeftButton;
680 }
681 elsif ( $tb_event->key == TB_KEY_MOUSE_RIGHT ) {
682 0 $buttons |= mbRightButton;
683 }
684 elsif ( $tb_event->key == TB_KEY_MOUSE_RELEASE ) {
685 0 $buttons = 0;
686 }
687
688 # Mouse position
689 0 my @where = ( $tb_event->x, $tb_event->y );
690 0 $event->{where}{x} = $where[0];
691 0 $event->{where}{y} = $where[1];
692
693 # Button state
694 0 $event->{buttons} = $buttons;
695
696 # Event flags. Double-clicks are detected by TEventQueue.
697 0 $event->{eventFlags} = 0;
698 0 ▶ $event->{eventFlags} |= meMouseMoved
Branch: 1 branch
BranchTrueFalseTotal
if $tb_event->mod & 8 0 0 0
699 if $tb_event->mod & TB_MOD_MOTION;
700
701 # Mouse modifier state.
702 0 ▶ $event->{controlKeyState} = $insertState ? kbInsState : 0;
Branch: 1 branch
BranchTrueFalseTotal
$TUI::Drivers::HardwareInfo::Termbox::insertState ? : 0 0 0
703
704 # Save the last button state
705 0 $lastButtons = $buttons;
706
707 # Clear the pending event flag because we have consumed the event
708 0 $pendingEvent = 0;
709 0 return true;
710 }
711
712 0 ▶sub getKeyEvent { # $bool ($class, $event)
Subroutine getKeyEvent: not called CC=15 SCAR=54.8
Pod undocumented
713 0 my ( $class, $event ) = @_;
714 0 assert ( @_ == 2 );
715 0 ▶ assert ( $class and !ref $class );
Condition0%0/3
$class && !ref($class)
!ll&&!rl&&r
000
MC/DC0%0/2
$class && !ref($class)
$class !ref($class)
Truth table0%0/3
A && not(B)
A= $classB= !ref($class)
ABresult
0X0
100
111
716 0 assert ( blessed $event );
717 0 assert ( $initialized );
718
719 # Check for pending events
720 0 ▶ unless ( $pendingEvent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($TUI::Drivers::HardwareInfo::Termbox::pendingEvent) 0 0 0
721 0 my $rv = tb_peek_event( $tb_event, 0 );
722 0 ▶ $pendingEvent = 1 if $rv == TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 0 0 0
723 }
724
725 # Return false if there are no pending events
726 0 ▶ return false
Branch: 1 branch
BranchTrueFalseTotal
unless $TUI::Drivers::HardwareInfo::Termbox::pendingEvent 0 0 0
727 unless $pendingEvent;
728
729 # Handle resize events immediately
730 0 ▶ if ( $tb_event->type == TB_EVENT_RESIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tb_event->type == 2) 0 0 0
731 0 $pendingEvent = 0;
732 0 ▶ if ( $class->screenChanged() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->screenChanged) 0 0 0
733 0 $event->{what} = evCommand;
734 0 $event->{message}{command} = cmScreenChanged;
735 0 $event->{message}{infoPtr} = undef;
736 0 return true;
737 }
738 0 return false;
739 }
740
741 # Return if the event is not a key event
742 0 ▶ return false
Branch: 1 branch
BranchTrueFalseTotal
if $tb_event->type != 1 0 0 0
743 if $tb_event->type != TB_EVENT_KEY;
744
745 0 $event->{what} = evKeyDown;
746
747 # Set the key code and character code in the event structure.
748 0 my $keyCode = $tb_event->$convertKeyCode();
749 0 $event->{keyDown}{keyCode} = $keyCode;
750 0 ▶ if ( $keyCode == kbNoKey ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($keyCode == 0) { } 0 0 0
elsif ($keyCode == 20992) { } 0 0 0
751 0 my $charCode = $tb_event->$convertCharCode();
752 0 $event->{keyDown}{charScan}{charCode} = $charCode;
753 }
754 elsif ( $keyCode == kbIns ) {
755 0 $insertState = !$insertState;
756 }
757
758 # Update the shift state and control key state in the event structure
759 0 my $shiftState = 0;
760 0 ▶ $shiftState |= kbAltShift
Branch: 1 branch
BranchTrueFalseTotal
if $tb_event->mod & 1 0 0 0
761 if $tb_event->mod & TB_MOD_ALT;
762 0 ▶ $shiftState |= kbCtrlShift
Branch: 1 branch
BranchTrueFalseTotal
if $tb_event->mod & 2 0 0 0
763 if $tb_event->mod & TB_MOD_CTRL;
764 0 ▶ $shiftState |= kbShift
Branch: 1 branch
BranchTrueFalseTotal
if $tb_event->mod & 4 0 0 0
765 if $tb_event->mod & TB_MOD_SHIFT;
766 0 ▶ $shiftState |= kbInsState
Branch: 1 branch
BranchTrueFalseTotal
if $TUI::Drivers::HardwareInfo::Termbox::insertState 0 0 0
767 if $insertState;
768 0 $event->{keyDown}{controlKeyState} = $shiftState;
769
770 # Set the Ctrl-Break flag if Ctrl-C was pressed
771 0 ▶ $ctrlBreakHit ||= $event->{keyDown}{keyCode} == kbCtrlC;
Condition0%0/3
$TUI::Drivers::HardwareInfo::Termbox::ctrlBreakHit ||= $event->{"keyDown"}{"keyCode"} == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
$TUI::Drivers::HardwareInfo::Termbox::ctrlBreakHit ||= $event->{"keyDown"}{"keyCode"} == 3
$TUI::Drivers::HardwareInfo::Termbox::ctrlBreakHit $event->{"keyDown"}{"keyCode"} == 3
Truth table0%0/3
A ||= B
A= $TUI::Drivers::HardwareInfo::Termbox::ctrlBreakHitB= $event->{"keyDown"}{"keyCode"} == 3
ABresult
1X1
011
000
772
773 0 $pendingEvent = 0;
774 0 return true;
775 }
776
777 # -------------------------------------------------------------------------
778 # System functions
779 # -------------------------------------------------------------------------
780
781 0 ▶sub setCtrlBrkHandler { # $success ($class, $install)
Subroutine setCtrlBrkHandler: not called CC=3 SCAR=24.8
Pod undocumented
782 0 my ( $class, $install ) = @_;
783 0 assert ( @_ == 2 );
784 0 ▶ assert ( $class and !ref $class );
Condition0%0/3
$class && !ref($class)
!ll&&!rl&&r
000
MC/DC0%0/2
$class && !ref($class)
$class !ref($class)
Truth table0%0/3
A && not(B)
A= $classB= !ref($class)
ABresult
0X0
100
111
785 0 ▶ assert ( !defined $install or !ref $install );
Condition0%0/3
defined $install && ref $install
!ll&&!rl&&r
000
MC/DC0%0/2
defined $install && ref $install
defined $install ref $install
Truth table0%0/3
A && B
A= defined $installB= ref $install
ABresult
0X0
100
111
786 # Termbox handles terminal input itself. No direct Ctrl-Break handler.
787 0 return true;
788 }
789
790 0 ▶sub setCritErrorHandler { # $bool ($class, $install)
Subroutine setCritErrorHandler: not called CC=3 SCAR=24.8
Pod undocumented
791 0 assert ( @_ == 2 );
792 0 ▶ assert ( $_[0] and !ref $_[0] );
Condition0%0/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table0%0/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
793 0 ▶ assert ( !defined $_[1] or !ref $_[1] );
Condition0%0/3
defined $_[1] && ref $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[1] && ref $_[1]
defined $_[1] ref $_[1]
Truth table0%0/3
A && B
A= defined $_[1]B= ref $_[1]
ABresult
0X0
100
111
794 # Not applicable for Termbox.
795 0 return true;
796 }
797
798 # -------------------------------------------------------------------------
799 # Additional functions (not part of the original Borland interface)
800 # -------------------------------------------------------------------------
801
802 0 ▶sub getColorCount { # $count ($class)
Subroutine getColorCount: not called CC=20 SCAR=60.4
Pod undocumented
803 0 assert ( @_ == 1 );
804 0 ▶ assert ( $_[0] and !ref $_[0] );
Condition0%0/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table0%0/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
805
806 0 state $COLORS;
807 0 ▶ return $COLORS if defined $COLORS;
Branch: 1 branch
BranchTrueFalseTotal
if defined $COLORS 0 0 0
808
809 # Is Windows 10 or later? (build 10586)
810 0 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 0 0
811 # Simple OS version test for very old systems
812 0 require Win32;
813 0 ▶ return $COLORS = 16
Branch: 1 branch
BranchTrueFalseTotal
if (Win32::GetOSVersion())[1] < 6 0 0 0
814 if ( Win32::GetOSVersion() )[1] < 6;
815
816 # Check if we can enable Virtual Terminal processing.
817 0 require Win32API::File;
818 0 require Win32::Console;
819 0 my $ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
820
821 0 my $console = Win32::Console->new( Win32API::File::STD_OUTPUT_HANDLE() );
822 0 ▶ return $COLORS = 16 unless $console;
Branch: 1 branch
BranchTrueFalseTotal
unless $console 0 0 0
823
824 0 $^E = 0;
825 0 my $mode = $console->Mode();
826 0 ▶ return $COLORS = 16 if $^E;
Branch: 1 branch
BranchTrueFalseTotal
if $^E 0 0 0
827
828 0 $console->Mode($mode | $ENABLE_VIRTUAL_TERMINAL_PROCESSING);
829 0 ▶ my $isVT = !$^E
Condition0%0/5
!$^E && ($console->Mode // 0) & $ENABLE_VIRTUAL_TERMINAL_PROCESSING
!ll&&!rl&&r
000
$console->Mode // 0
l!l
00
MC/DC0%0/2
!$^E && ($console->Mode // 0) & $ENABLE_VIRTUAL_TERMINAL_PROCESSING
!$^E $console->Mode
Truth table0%0/3
not(A) && B
A= !$^EB= $console->Mode
ABresult
0X0
100
111
830 && ( $console->Mode() // 0 ) & $ENABLE_VIRTUAL_TERMINAL_PROCESSING;
831
832 0 $console->Mode( $mode );
833
834 # VT processing implies modern console host.
835 # Only modern VT hosts support 24-bit colors.
836 0 ▶ return $COLORS = !$isVT ? 16
Branch: 2 branches
BranchTrueFalseTotal
!$isVT ? : 0 0 0
!tb_has_truecolor() ? : 0 0 0
837 : !tb_has_truecolor() ? 256
838 : 256*256*256;
839 }
840
841 # First check COLORTERM environment
842 0 ▶ if ( tb_has_truecolor() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (tb_has_truecolor()) 0 0 0
843 0 ▶ my $colorterm = $ENV{COLORTERM} // '';
Condition0%0/2
$ENV{"COLORTERM"} // ""
l!l
00
MC/DC0%0/1
$ENV{"COLORTERM"} // ""
$ENV{"COLORTERM"}
Truth table0%0/2
A
A= $ENV{"COLORTERM"}
Aresult
11
00
844 0 ▶ return $COLORS = 256*256*256
Branch: 1 branch
BranchTrueFalseTotal
if $colorterm =~ /\A(?:truecolor|24bit)\z/i 0 0 0
845 if $colorterm =~ /\A(?:truecolor|24bit)\z/i;
846 }
847
848 # Next check terminfo database
849 0 my $colors = eval {
850 0 require Term::Cap;
851 0 ▶ local $SIG{__WARN__} = sub { }; # Suppress warnings from Tgetent
Subroutine __ANON__: not called CC=1 SCAR=6.9
852 0 Term::Cap->Tgetent()->{_Co};
853 };
854 0 ▶ return $COLORS = $colors
Branch: 1 branch
BranchTrueFalseTotal
if not $@ and defined $colors and $colors >= 8 0 0 0
Condition0%0/6
not $@ and defined $colors and $colors >= 8
!ll&&!rl&&r
000
not $@ and defined $colors
!ll&&!rl&&r
000
MC/DC0%0/3
not $@ and defined $colors and $colors >= 8
not $@ defined $colors $colors >= 8
Truth table0%0/4
not(A) and B and C
A= not $@B= defined $colorsC= $colors >= 8
ABCresult
0XX0
10X0
1100
1111
855 if !$@ && defined $colors && $colors >=8;
856
857 # Otherwise check for common TERM values that indicate color support.
858 0 ▶ my $term = $ENV{TERM} // '';
Condition0%0/2
$ENV{"TERM"} // ""
l!l
00
MC/DC0%0/1
$ENV{"TERM"} // ""
$ENV{"TERM"}
Truth table0%0/2
A
A= $ENV{"TERM"}
Aresult
11
00
859 0 ▶ return $COLORS = 256 if $term =~ /256color/i;
Branch: 1 branch
BranchTrueFalseTotal
if $term =~ /256color/i 0 0 0
860
861 # Let's assume all terminals disguising themselves as 'xterm'
862 # support at least 16 colors.
863 0 ▶ return $COLORS = 16 if $term =~ /xterm/;
Branch: 1 branch
BranchTrueFalseTotal
if $term =~ /xterm/ 0 0 0
864
865 # Fall back to a conservative default of 8 colors.
866 0 return $COLORS = 8;
867 } #/ sub getColorCount
868
869 0 ▶sub reloadScreenInfo { # void ($class)
Subroutine reloadScreenInfo: not called CC=2 SCAR=17.9
Pod undocumented
870 0 assert ( @_ == 1 );
871 0 ▶ assert ( $_[0] and !ref $_[0] );
Condition0%0/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table0%0/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
872 0 tb_invalidate();
873 0 return;
874 }
875
876 0 ▶sub screenChanged { # $bool ($class)
Subroutine screenChanged: not called CC=4 SCAR=30.0
Pod undocumented
877 0 assert ( @_ == 1 );
878 0 ▶ assert ( $_[0] and !ref $_[0] );
Condition0%0/3
$_[0] && !ref($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] && !ref($_[0])
$_[0] !ref($_[0])
Truth table0%0/3
A && not(B)
A= $_[0]B= !ref($_[0])
ABresult
0X0
100
111
879 0 my $cols = tb_width();
880 0 my $rows = tb_height();
881 0 ▶ if ( $cols != $lastSize[0] || $rows != $lastSize[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols != $lastSize[0] or $rows != $lastSize[1]) 0 0 0
Condition0%0/3
$cols != $lastSize[0] or $rows != $lastSize[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$cols != $lastSize[0] or $rows != $lastSize[1]
$cols != $lastSize[0] $rows != $lastSize[1]
Truth table0%0/3
A or B
A= $cols != $lastSize[0]B= $rows != $lastSize[1]
ABresult
1X1
011
000
882 0 @lastSize = ( $cols, $rows );
883 0 return true;
884 }
885 0 return false;
886 }
887
888 # -------------------------------------------------------------------------
889 # Key Event conversion
890 # -------------------------------------------------------------------------
891
892 102 my %TV_TO_TB = (
893 # Control keys
894 kbCtrlA() => [ TB_MOD_CTRL, TB_KEY_CTRL_A, 0 ],
895 kbCtrlB() => [ TB_MOD_CTRL, TB_KEY_CTRL_B, 0 ],
896 kbCtrlC() => [ TB_MOD_CTRL, TB_KEY_CTRL_C, 0 ],
897 kbCtrlD() => [ TB_MOD_CTRL, TB_KEY_CTRL_D, 0 ],
898 kbCtrlE() => [ TB_MOD_CTRL, TB_KEY_CTRL_E, 0 ],
899 kbCtrlF() => [ TB_MOD_CTRL, TB_KEY_CTRL_F, 0 ],
900 kbCtrlG() => [ TB_MOD_CTRL, TB_KEY_CTRL_G, 0 ],
901 kbCtrlH() => [ TB_MOD_CTRL, TB_KEY_CTRL_H, 0 ],
902 kbCtrlI() => [ TB_MOD_CTRL, TB_KEY_CTRL_I, 0 ],
903 kbCtrlJ() => [ TB_MOD_CTRL, TB_KEY_CTRL_J, 0 ],
904 kbCtrlK() => [ TB_MOD_CTRL, TB_KEY_CTRL_K, 0 ],
905 kbCtrlL() => [ TB_MOD_CTRL, TB_KEY_CTRL_L, 0 ],
906 kbCtrlM() => [ TB_MOD_CTRL, TB_KEY_CTRL_M, 0 ],
907 kbCtrlN() => [ TB_MOD_CTRL, TB_KEY_CTRL_N, 0 ],
908 kbCtrlO() => [ TB_MOD_CTRL, TB_KEY_CTRL_O, 0 ],
909 kbCtrlP() => [ TB_MOD_CTRL, TB_KEY_CTRL_P, 0 ],
910 kbCtrlQ() => [ TB_MOD_CTRL, TB_KEY_CTRL_Q, 0 ],
911 kbCtrlR() => [ TB_MOD_CTRL, TB_KEY_CTRL_R, 0 ],
912 kbCtrlS() => [ TB_MOD_CTRL, TB_KEY_CTRL_S, 0 ],
913 kbCtrlT() => [ TB_MOD_CTRL, TB_KEY_CTRL_T, 0 ],
914 kbCtrlU() => [ TB_MOD_CTRL, TB_KEY_CTRL_U, 0 ],
915 kbCtrlV() => [ TB_MOD_CTRL, TB_KEY_CTRL_V, 0 ],
916 kbCtrlW() => [ TB_MOD_CTRL, TB_KEY_CTRL_W, 0 ],
917 kbCtrlX() => [ TB_MOD_CTRL, TB_KEY_CTRL_X, 0 ],
918 kbCtrlY() => [ TB_MOD_CTRL, TB_KEY_CTRL_Y, 0 ],
919 kbCtrlZ() => [ TB_MOD_CTRL, TB_KEY_CTRL_Z, 0 ],
920
921 # Extended key codes
922 kbEsc() => [ TB_NONE, TB_KEY_ESC, 0 ],
923 kbAltSpace() => [ TB_MOD_ALT, TB_KEY_SPACE, 0 ],
924
925 kbCtrlIns() => [ TB_MOD_CTRL, TB_KEY_INSERT, 0 ],
926 kbShiftIns() => [ TB_MOD_SHIFT, TB_KEY_INSERT, 0 ],
927
928 kbCtrlDel() => [ TB_MOD_CTRL, TB_KEY_DELETE, 0 ],
929 kbShiftDel() => [ TB_MOD_SHIFT, TB_KEY_DELETE, 0 ],
930
931 kbBack() => [ TB_MOD_CTRL, TB_KEY_BACKSPACE2, 0 ],
932 kbCtrlBack() => [ TB_MOD_CTRL, TB_KEY_BACKSPACE, 0 ],
933
934 kbShiftTab() => [ TB_MOD_SHIFT, TB_KEY_BACK_TAB, 0 ],
935 kbTab() => [ TB_NONE, TB_KEY_TAB, 0 ],
936
937 kbCtrlEnter() => [ TB_MOD_CTRL, TB_KEY_CTRL_J, 0 ],
938 kbEnter() => [ TB_MOD_CTRL, TB_KEY_ENTER, 0 ],
939
940 # Alt letters
941 kbAltQ() => [ TB_MOD_ALT, 0, ord('q') ],
942 kbAltW() => [ TB_MOD_ALT, 0, ord('w') ],
943 kbAltE() => [ TB_MOD_ALT, 0, ord('e') ],
944 kbAltR() => [ TB_MOD_ALT, 0, ord('r') ],
945 kbAltT() => [ TB_MOD_ALT, 0, ord('t') ],
946 kbAltY() => [ TB_MOD_ALT, 0, ord('y') ],
947 kbAltU() => [ TB_MOD_ALT, 0, ord('u') ],
948 kbAltI() => [ TB_MOD_ALT, 0, ord('i') ],
949 kbAltO() => [ TB_MOD_ALT, 0, ord('o') ],
950 kbAltP() => [ TB_MOD_ALT, 0, ord('p') ],
951
952 kbAltA() => [ TB_MOD_ALT, 0, ord('a') ],
953 kbAltS() => [ TB_MOD_ALT, 0, ord('s') ],
954 kbAltD() => [ TB_MOD_ALT, 0, ord('d') ],
955 kbAltF() => [ TB_MOD_ALT, 0, ord('f') ],
956 kbAltG() => [ TB_MOD_ALT, 0, ord('g') ],
957 kbAltH() => [ TB_MOD_ALT, 0, ord('h') ],
958 kbAltJ() => [ TB_MOD_ALT, 0, ord('j') ],
959 kbAltK() => [ TB_MOD_ALT, 0, ord('k') ],
960 kbAltL() => [ TB_MOD_ALT, 0, ord('l') ],
961
962 kbAltZ() => [ TB_MOD_ALT, 0, ord('z') ],
963 kbAltX() => [ TB_MOD_ALT, 0, ord('x') ],
964 kbAltC() => [ TB_MOD_ALT, 0, ord('c') ],
965 kbAltV() => [ TB_MOD_ALT, 0, ord('v') ],
966 kbAltB() => [ TB_MOD_ALT, 0, ord('b') ],
967 kbAltN() => [ TB_MOD_ALT, 0, ord('n') ],
968 kbAltM() => [ TB_MOD_ALT, 0, ord('m') ],
969
970 # Function keys
971 kbF1() => [ TB_NONE, TB_KEY_F1, 0 ],
972 kbF2() => [ TB_NONE, TB_KEY_F2, 0 ],
973 kbF3() => [ TB_NONE, TB_KEY_F3, 0 ],
974 kbF4() => [ TB_NONE, TB_KEY_F4, 0 ],
975 kbF5() => [ TB_NONE, TB_KEY_F5, 0 ],
976 kbF6() => [ TB_NONE, TB_KEY_F6, 0 ],
977 kbF7() => [ TB_NONE, TB_KEY_F7, 0 ],
978 kbF8() => [ TB_NONE, TB_KEY_F8, 0 ],
979 kbF9() => [ TB_NONE, TB_KEY_F9, 0 ],
980 kbF10() => [ TB_NONE, TB_KEY_F10, 0 ],
981 kbF11() => [ TB_NONE, TB_KEY_F11, 0 ],
982 kbF12() => [ TB_NONE, TB_KEY_F12, 0 ],
983
984 kbShiftF1() => [ TB_MOD_SHIFT, TB_KEY_F1, 0 ],
985 kbShiftF2() => [ TB_MOD_SHIFT, TB_KEY_F2, 0 ],
986 kbShiftF3() => [ TB_MOD_SHIFT, TB_KEY_F3, 0 ],
987 kbShiftF4() => [ TB_MOD_SHIFT, TB_KEY_F4, 0 ],
988 kbShiftF5() => [ TB_MOD_SHIFT, TB_KEY_F5, 0 ],
989 kbShiftF6() => [ TB_MOD_SHIFT, TB_KEY_F6, 0 ],
990 kbShiftF7() => [ TB_MOD_SHIFT, TB_KEY_F7, 0 ],
991 kbShiftF8() => [ TB_MOD_SHIFT, TB_KEY_F8, 0 ],
992 kbShiftF9() => [ TB_MOD_SHIFT, TB_KEY_F9, 0 ],
993 kbShiftF10() => [ TB_MOD_SHIFT, TB_KEY_F10, 0 ],
994 kbShiftF11() => [ TB_MOD_SHIFT, TB_KEY_F11, 0 ],
995 kbShiftF12() => [ TB_MOD_SHIFT, TB_KEY_F12, 0 ],
996
997 kbCtrlF1() => [ TB_MOD_CTRL, TB_KEY_F1, 0 ],
998 kbCtrlF2() => [ TB_MOD_CTRL, TB_KEY_F2, 0 ],
999 kbCtrlF3() => [ TB_MOD_CTRL, TB_KEY_F3, 0 ],
1000 kbCtrlF4() => [ TB_MOD_CTRL, TB_KEY_F4, 0 ],
1001 kbCtrlF5() => [ TB_MOD_CTRL, TB_KEY_F5, 0 ],
1002 kbCtrlF6() => [ TB_MOD_CTRL, TB_KEY_F6, 0 ],
1003 kbCtrlF7() => [ TB_MOD_CTRL, TB_KEY_F7, 0 ],
1004 kbCtrlF8() => [ TB_MOD_CTRL, TB_KEY_F8, 0 ],
1005 kbCtrlF9() => [ TB_MOD_CTRL, TB_KEY_F9, 0 ],
1006 kbCtrlF10() => [ TB_MOD_CTRL, TB_KEY_F10, 0 ],
1007 kbCtrlF11() => [ TB_MOD_CTRL, TB_KEY_F11, 0 ],
1008 kbCtrlF12() => [ TB_MOD_CTRL, TB_KEY_F12, 0 ],
1009
1010 kbAltF1() => [ TB_MOD_ALT, TB_KEY_F1, 0 ],
1011 kbAltF2() => [ TB_MOD_ALT, TB_KEY_F2, 0 ],
1012 kbAltF3() => [ TB_MOD_ALT, TB_KEY_F3, 0 ],
1013 kbAltF4() => [ TB_MOD_ALT, TB_KEY_F4, 0 ],
1014 kbAltF5() => [ TB_MOD_ALT, TB_KEY_F5, 0 ],
1015 kbAltF6() => [ TB_MOD_ALT, TB_KEY_F6, 0 ],
1016 kbAltF7() => [ TB_MOD_ALT, TB_KEY_F7, 0 ],
1017 kbAltF8() => [ TB_MOD_ALT, TB_KEY_F8, 0 ],
1018 kbAltF9() => [ TB_MOD_ALT, TB_KEY_F9, 0 ],
1019 kbAltF10() => [ TB_MOD_ALT, TB_KEY_F10, 0 ],
1020 kbAltF11() => [ TB_MOD_ALT, TB_KEY_F11, 0 ],
1021 kbAltF12() => [ TB_MOD_ALT, TB_KEY_F12, 0 ],
1022
1023 # Navigation
1024 kbHome() => [ TB_NONE, TB_KEY_HOME, 0 ],
1025 kbEnd() => [ TB_NONE, TB_KEY_END, 0 ],
1026
1027 kbPgUp() => [ TB_NONE, TB_KEY_PGUP, 0 ],
1028 kbPgDn() => [ TB_NONE, TB_KEY_PGDN, 0 ],
1029
1030 kbUp() => [ TB_NONE, TB_KEY_ARROW_UP, 0 ],
1031 kbDown() => [ TB_NONE, TB_KEY_ARROW_DOWN, 0 ],
1032 kbLeft() => [ TB_NONE, TB_KEY_ARROW_LEFT, 0 ],
1033 kbRight() => [ TB_NONE, TB_KEY_ARROW_RIGHT, 0 ],
1034
1035 kbCtrlLeft() => [ TB_MOD_CTRL, TB_KEY_ARROW_LEFT, 0 ],
1036 kbCtrlRight() => [ TB_MOD_CTRL, TB_KEY_ARROW_RIGHT, 0 ],
1037
1038 kbCtrlHome() => [ TB_MOD_CTRL, TB_KEY_HOME, 0 ],
1039 kbCtrlEnd() => [ TB_MOD_CTRL, TB_KEY_END, 0 ],
1040
1041 kbCtrlPgUp() => [ TB_MOD_CTRL, TB_KEY_PGUP, 0 ],
1042 kbCtrlPgDn() => [ TB_MOD_CTRL, TB_KEY_PGDN, 0 ],
1043
1044 kbIns() => [ TB_NONE, TB_KEY_INSERT, 0 ],
1045 kbDel() => [ TB_NONE, TB_KEY_DELETE, 0 ],
1046
1047 # Alt digits
1048 kbAlt1() => [ TB_MOD_ALT, 0, ord('1') ],
1049 kbAlt2() => [ TB_MOD_ALT, 0, ord('2') ],
1050 kbAlt3() => [ TB_MOD_ALT, 0, ord('3') ],
1051 kbAlt4() => [ TB_MOD_ALT, 0, ord('4') ],
1052 kbAlt5() => [ TB_MOD_ALT, 0, ord('5') ],
1053 kbAlt6() => [ TB_MOD_ALT, 0, ord('6') ],
1054 kbAlt7() => [ TB_MOD_ALT, 0, ord('7') ],
1055 kbAlt8() => [ TB_MOD_ALT, 0, ord('8') ],
1056 kbAlt9() => [ TB_MOD_ALT, 0, ord('9') ],
1057 kbAlt0() => [ TB_MOD_ALT, 0, ord('0') ],
1058
1059 kbAltMinus() => [ TB_MOD_ALT, 0, ord('-') ],
1060 kbAltEqual() => [ TB_MOD_ALT, 0, ord('=') ],
1061
1062 kbAltBack() => [
1063 TB_MOD_ALT | TB_MOD_CTRL,
1064 TB_KEY_BACKSPACE2,
1065 0
1066 ],
1067
1068 kbNoKey() => [ TB_NONE, 0, 0 ],
1069
1070 # kbGrayMinus() => collides with '-'
1071 # kbGrayPlus() => collides with '+'
1072 # kbCtrlPrtSc() => no event generated (effectively collides with kbNoKey())
1073 );
1074 102 my %TB_TO_TV; {
1075 102 ▶ while ( my ( $tv_key, $tb_key ) = each %TV_TO_TB ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %TV_TO_TB) 14280 102 14382
1076 14280 my ( $mod, $key, $ch ) = @$tb_key;
1077
1078 # Default: first mapping wins.
1079 # This avoids accidental replacement caused by hash iteration order.
1080 14280 my $id = join( ':', ( $mod, $key, $ch ) );
1081 $TB_TO_TV{$id} = $tv_key
1082 14280 ▶ unless exists $TB_TO_TV{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $TB_TO_TV{$id} 13974 306 14280
1083 }
1084
1085 # VT control-code collisions.
1086 #
1087 # VT stream TV keys Preferred
1088 # ------------------------------------------------
1089 # 0x08 Ctrl-H, Ctrl-Back Ctrl-Back
1090 # 0x09 Ctrl-I, Tab Tab
1091 # 0x0A Ctrl-J, Ctrl-Enter Ctrl-Enter
1092 # 0x0D Ctrl-M, Enter Enter
1093 102 $TB_TO_TV{ join( ':', TB_MOD_CTRL, TB_KEY_BACKSPACE, 0 ) }
1094 = kbCtrlBack();
1095 102 $TB_TO_TV{ join( ':', TB_MOD_CTRL, TB_KEY_TAB, 0 ) }
1096 = kbTab();
1097 102 $TB_TO_TV{ join( ':', TB_MOD_CTRL, TB_KEY_CTRL_J, 0 ) }
1098 = kbCtrlEnter();
1099 102 $TB_TO_TV{ join( ':', TB_MOD_CTRL, TB_KEY_ENTER, 0 ) }
1100 = kbEnter();
1101 }
1102 102 my %CP437 = map { $CP437_TO_UTF8[$_] => $_ } 0..255;
1103
1104 0 ▶$convertCharCode = sub { # $charCode ($event)
Subroutine __ANON__: not called CC=7 SCAR=40.3
1105 0 my ( $event ) = @_;
1106
1107 0 my $cp = $event->ch;
1108
1109 # Support the official Unicode values instead only the Termius equivalents
1110 0 ▶ return 0x10 if $cp == ord "\x{25ba}";
Branch: 1 branch
BranchTrueFalseTotal
if $cp == 9658 0 0 0
1111 0 ▶ return 0x11 if $cp == ord "\x{25c4}";
Branch: 1 branch
BranchTrueFalseTotal
if $cp == 9668 0 0 0
1112
1113 return $CP437{$cp}
1114 0 ▶ if $cp && exists $CP437{$cp};
Branch: 1 branch
BranchTrueFalseTotal
if $cp and exists $CP437{$cp} 0 0 0
Condition0%0/3
$cp and exists $CP437{$cp}
!ll&&!rl&&r
000
MC/DC0%0/2
$cp and exists $CP437{$cp}
$cp exists $CP437{$cp}
Truth table0%0/3
A and B
A= $cpB= exists $CP437{$cp}
ABresult
0X0
100
111
1115
1116 0 my $key = $event->key;
1117
1118 0 ▶ return $key
Branch: 1 branch
BranchTrueFalseTotal
if $key <= 31 or $key == 127 0 0 0
Condition0%0/3
$key <= 31 or $key == 127
l!l&&r!l&&!r
000
MC/DC0%0/2
$key <= 31 or $key == 127
$key <= 31 $key == 127
Truth table0%0/3
A or B
A= $key <= 31B= $key == 127
ABresult
1X1
011
000
1119 if $key <= 0x1f || $key == 0x7f;
1120
1121 0 return 0;
1122 102 };
1123
1124 0 ▶$convertKeyCode = sub { # $keyCode ($event)
Subroutine __ANON__: not called CC=16 SCAR=56.1
1125 0 my ( $event ) = @_;
1126
1127 0 my $mod = $event->mod;
1128 0 my $key = $event->key;
1129 0 my $ch = $event->ch;
1130
1131 # Keep only keyboard modifier bits relevant for the key mapping.
1132 0 $mod &= TB_MOD_ALT | TB_MOD_CTRL | TB_MOD_SHIFT;
1133
1134 0 my $id = join( ':', $mod, $key, $ch );
1135 return $TB_TO_TV{$id}
1136 0 ▶ if exists $TB_TO_TV{$id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $TB_TO_TV{$id} 0 0 0
1137
1138 # Some Alt/Ctrl key combinations have no dedicated Borland key code.
1139 # Map them to the corresponding unmodified key.
1140 0 ▶ if ( $mod & ( TB_MOD_CTRL | TB_MOD_ALT ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod & 3) 0 0 0
1141 0 ▶ return kbUp if $key == TB_KEY_ARROW_UP;
Branch: 1 branch
BranchTrueFalseTotal
if $key == 65517 0 0 0
1142 0 ▶ return kbDown if $key == TB_KEY_ARROW_DOWN;
Branch: 1 branch
BranchTrueFalseTotal
if $key == 65516 0 0 0
1143 0 ▶ return kbIns if $key == TB_KEY_INSERT;
Branch: 1 branch
BranchTrueFalseTotal
if $key == 65523 0 0 0
1144 0 ▶ return kbDel if $key == TB_KEY_DELETE;
Branch: 1 branch
BranchTrueFalseTotal
if $key == 65522 0 0 0
1145 0 ▶ return kbTab if $key == TB_KEY_TAB;
Branch: 1 branch
BranchTrueFalseTotal
if $key == 9 0 0 0
1146 0 ▶ return kbEnter if $key == TB_KEY_ENTER;
Branch: 1 branch
BranchTrueFalseTotal
if $key == 13 0 0 0
1147 }
1148 0 ▶ if ( $mod & TB_MOD_ALT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod & 1) 0 0 0
1149 0 ▶ return kbLeft if $key == TB_KEY_ARROW_LEFT;
Branch: 1 branch
BranchTrueFalseTotal
if $key == 65515 0 0 0
1150 0 ▶ return kbRight if $key == TB_KEY_ARROW_RIGHT;
Branch: 1 branch
BranchTrueFalseTotal
if $key == 65514 0 0 0
1151 0 ▶ return kbHome if $key == TB_KEY_HOME;
Branch: 1 branch
BranchTrueFalseTotal
if $key == 65521 0 0 0
1152 0 ▶ return kbEnd if $key == TB_KEY_END;
Branch: 1 branch
BranchTrueFalseTotal
if $key == 65520 0 0 0
1153 0 ▶ return kbPgUp if $key == TB_KEY_PGUP;
Branch: 1 branch
BranchTrueFalseTotal
if $key == 65519 0 0 0
1154 0 ▶ return kbPgDn if $key == TB_KEY_PGDN;
Branch: 1 branch
BranchTrueFalseTotal
if $key == 65518 0 0 0
1155 }
1156
1157 0 return kbNoKey();
1158 102 };
1159
1160 # -------------------------------------------------------------------------
1161 # Attribute conversion
1162 # -------------------------------------------------------------------------
1163
1164 # The following content is inspired by the framework
1165 # "A modern port of Turbo Vision 2.0", which is licensed under MIT licence.
1166 #
1167 # Copyright 2019-2026 by magiblot <magiblot@hotmail.com>
1168 #
1169 # I<ansiwrit.cpp>
1170
1171 0 ▶$convertColor = sub { # $tb_color ($color, $isFg)
Subroutine __ANON__: not called CC=7 SCAR=40.3
1172 0 my ( $color, $isFg ) = @_;
1173 0 assert ( @_ == 2 );
1174 0 assert ( blessed $color );
1175 0 assert ( !ref $isFg );
1176 0 ▶ return $color->$convertNoColor( $isFg )
Branch: 1 branch
BranchTrueFalseTotal
if ($screenMode & 255) == 7 0 0 0
1177 if ( $screenMode & 0xff ) == smMono;
1178
1179 0 ▶ return TB_DEFAULT
Branch: 1 branch
BranchTrueFalseTotal
if ($screenMode & 255) != 2 and ($screenMode & 255) != 3 0 0 0
Condition0%0/3
($screenMode & 255) != 2 and ($screenMode & 255) != 3
!ll&&!rl&&r
000
MC/DC0%0/2
($screenMode & 255) != 2 and ($screenMode & 255) != 3
($screenMode & 255) != 2 ($screenMode & 255) != 3
Truth table0%0/3
A and B
A= ($screenMode & 255) != 2B= ($screenMode & 255) != 3
ABresult
0X0
100
111
1180 if ( $screenMode & 0xff ) != smBW80
1181 && ( $screenMode & 0xff ) != smCO80;
1182
1183 0 ▶ return $color->$convertDirect( $isFg )
Branch: 1 branch
BranchTrueFalseTotal
if $screenMode & 1024 0 0 0
1184 if $screenMode & smColorHigh;
1185
1186 0 ▶ return $color->$convertIndexed256( $isFg )
Branch: 1 branch
BranchTrueFalseTotal
if $screenMode & 512 0 0 0
1187 if $screenMode & smColor256;
1188
1189 0 ▶ return $color->$convertIndexed16( $isFg )
Branch: 1 branch
BranchTrueFalseTotal
if $outputMode > 1 0 0 0
1190 if $outputMode > TB_OUTPUT_NORMAL;
1191
1192 0 return $color->$convertIndexed8( $isFg );
1193 102 };
1194
1195 0 ▶$convertNoColor = sub { # $tb_color ($color, $isFg)
Subroutine __ANON__: not called CC=6 SCAR=37.4
1196 0 my ( $color, $isFg ) = @_;
1197 0 assert ( @_ == 2 );
1198 0 assert ( blessed $color );
1199 0 assert ( !ref $isFg );
1200 0 my $c = TB_DEFAULT;
1201 0 ▶ if ( $color->isBIOS() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color->isBIOS) 0 0 0
1202 0 my $bios = $color->asBIOS();
1203 0 ▶ if ( $isFg ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($isFg) { } 0 0 0
elsif (($bios & 7) == 7) { } 0 0 0
1204 0 ▶ if ( $bios & 0x8 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bios & 8) { } 0 0 0
elsif ($bios == 1) { } 0 0 0
1205 0 $c |= TB_BOLD;
1206 }
1207 elsif ( $bios == 0x1 ) {
1208 0 $c |= TB_UNDERLINE;
1209 }
1210 }
1211 elsif ( ( $bios & 0x7 ) == 0x7 ) {
1212 0 $c |= TB_REVERSE;
1213 }
1214 }
1215 0 return $c;
1216 102 };
1217
1218 0 ▶$convertIndexed8 = sub { # $tb_color ($color, $isFg)
Subroutine __ANON__: not called CC=5 SCAR=34.0
1219 0 my ( $color, $isFg ) = @_;
1220 0 assert ( @_ == 2 );
1221 0 assert ( blessed $color );
1222 0 assert ( !ref $isFg );
1223 0 my $idx = $color->$convertIndexed16( $isFg );
1224 0 my $c = ( $idx & 0x7 ) + 1;
1225 0 ▶ if ( $idx & 0x8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx & 8) 0 0 0
1226 0 ▶ $c |= $isFg ? TB_BOLD : TB_BLINK;
Branch: 1 branch
BranchTrueFalseTotal
$isFg ? : 0 0 0
1227 }
1228 0 ▶ $c = TB_BLACK if $idx & TB_HI_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if $idx & 8192 0 0 0
1229 0 ▶ $c = TB_DEFAULT if $idx == TB_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
if $idx == 0 0 0 0
1230 0 return $c;
1231 102 };
1232
1233 0 ▶$convertIndexed16 = sub { # $tb_color ($color, $isFg)
Subroutine __ANON__: not called CC=8 SCAR=42.8
1234 0 my ( $color, $isFg ) = @_;
1235 0 assert ( @_ == 2 );
1236 0 assert ( blessed $color );
1237 0 assert ( !ref $isFg );
1238 0 ▶ if ( $color->isBIOS() ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($color->isBIOS) { } 0 0 0
elsif ($color->isXTerm) { } 0 0 0
elsif ($color->isRGB) { } 0 0 0
1239 0 my $idx = BIOStoXTerm16( $color->asBIOS() );
1240 0 ▶ return $idx == TB_DEFAULT ? TB_HI_BLACK : $idx;
Branch: 1 branch
BranchTrueFalseTotal
$idx == 0 ? : 0 0 0
1241 }
1242 elsif ( $color->isXTerm() ) {
1243 0 my $idx = $color->asXTerm();
1244 0 ▶ $idx = XTerm256toXTerm16( $idx )
Branch: 1 branch
BranchTrueFalseTotal
if $idx >= 16 0 0 0
1245 if $idx >= 16;
1246 0 ▶ return $idx == TB_DEFAULT ? TB_HI_BLACK : $idx;
Branch: 1 branch
BranchTrueFalseTotal
$idx == 0 ? : 0 0 0
1247 }
1248 elsif ( $color->isRGB() ) {
1249 0 my $idx = RGBtoXTerm16( $color->asRGB() );
1250 0 ▶ return $idx == TB_DEFAULT ? TB_HI_BLACK : $idx;
Branch: 1 branch
BranchTrueFalseTotal
$idx == 0 ? : 0 0 0
1251 }
1252 0 return TB_DEFAULT;
1253 102 };
1254
1255 0 ▶$convertIndexed256 = sub { # $tb_color ($color, $isFg)
Subroutine __ANON__: not called CC=5 SCAR=34.0
1256 0 my ( $color, $isFg ) = @_;
1257 0 assert ( @_ == 2 );
1258 0 assert ( blessed $color );
1259 0 assert ( !ref $isFg );
1260 0 ▶ if ( $color->isXTerm() ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($color->isXTerm) { } 0 0 0
elsif ($color->isRGB) { } 0 0 0
1261 0 my $idx = $color->asXTerm();
1262 0 ▶ return $idx == TB_DEFAULT ? TB_HI_BLACK : $idx;
Branch: 1 branch
BranchTrueFalseTotal
$idx == 0 ? : 0 0 0
1263 }
1264 elsif ( $color->isRGB() ) {
1265 0 my $idx = RGBtoXTerm256( $color->asRGB() );
1266 0 ▶ return $idx == TB_DEFAULT ? TB_HI_BLACK : $idx;
Branch: 1 branch
BranchTrueFalseTotal
$idx == 0 ? : 0 0 0
1267 }
1268 0 return $color->$convertIndexed16( $isFg );
1269 102 };
1270
1271 102 my @XTERM256 = (
1272 # 0-15: system colors
1273 0x000000, 0x800000, 0x008000, 0x808000,
1274 0x000080, 0x800080, 0x008080, 0xc0c0c0,
1275 0x808080, 0xff0000, 0x00ff00, 0xffff00,
1276 0x0000ff, 0xff00ff, 0x00ffff, 0xffffff,
1277 ); {
1278 102 my @level = ( 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff );
1279
1280 # 16-231: color cubes
1281 102 for my $r ( @level ) {
1282 612 for my $g ( @level ) {
1283 3672 for my $b ( @level ) {
1284 22032 push @XTERM256, ( $r << 16 ) | ( $g << 8 ) | $b;
1285 }
1286 }
1287 }
1288
1289 # 232-255: gray scale
1290 102 for my $i ( 0 .. 23 ) {
1291 2448 my $v = 8 + $i * 10;
1292 2448 push @XTERM256, ( $v << 16 ) | ( $v << 8 ) | $v;
1293 }
1294 }
1295
1296 0 ▶$convertDirect = sub { # $tb_color ($color, $isFg)
Subroutine __ANON__: not called CC=4 SCAR=30.0
1297 0 my ( $color, $isFg ) = @_;
1298 0 assert ( @_ == 2 );
1299 0 assert ( blessed $color );
1300 0 assert ( !ref $isFg );
1301 0 ▶ if ( $color->isRGB() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color->isRGB) 0 0 0
1302 0 my $idx = $color->asRGB();
1303 0 ▶ return $idx == TB_DEFAULT ? TB_HI_BLACK : $idx;
Branch: 1 branch
BranchTrueFalseTotal
$idx == 0 ? : 0 0 0
1304 }
1305 0 my $idx = $color->$convertIndexed256( $isFg );
1306 0 my $c = $XTERM256[ $idx & 0xff ];
1307 0 ▶ $c = TB_HI_BLACK if $idx & TB_HI_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if $idx & 8192 0 0 0
1308 0 return $c;
1309 102 };
1310
1311 102 1;
1312
1313 __END__
« lib/TUI/Views/Frame/Line.pm ↑ Summary lib/TUI/Views/View/Write.pm »