lib/Termbox.pm

filter: Statementstmt 87.5% 105 / 120 Branchbran 33.3% 4 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 16 / 20 Podpod n/a totaltotal 82.2% 125 / 152 CC 4 SCAR 9.8
CC
4
Cov
83%
__ANON__
12.0
_export
2.0
__ANON__
2.0
CRAP 2.7
↑ Summary
1 2 package Termbox {
2 2 ▶ use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = "2.00";
6 #
7 2 ▶ use File::ShareDir qw[dist_dir];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶ use File::Spec::Functions qw[catdir canonpath];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #
10 2 ▶ use FFI::CheckLib;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶ use FFI::Platypus 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶ use FFI::Platypus::Memory qw( malloc free );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 $ENV{FFI_PLATYPUS_DLERROR} = 1;
14 2 my $ffi = FFI::Platypus->new(
15 api => 1,
16 lang => 'C',
17 lib => find_lib_or_exit(
18 lib => 'termbox2',
19 recursive => 1,
20 libpath => [ qw[ . ./share/lib], canonpath( catdir( dist_dir(__PACKAGE__), 'lib' ) ) ]
21 )
22 );
23 #
24 2 ▶ use base qw[Exporter];
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶ use vars qw[@EXPORT_OK @EXPORT %EXPORT_TAGS];
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # Utility functions that should be at the end but we need it here
28 2 $ffi->attach( tb_has_truecolor => ['void'] => 'int' );
29 2 $ffi->attach( tb_has_egc => ['void'] => 'int' );
30 #
31 2 our $TRUECOLOR = tb_has_truecolor();
32 2 ▶ our $uintattr_t = $TRUECOLOR ? 'uint32_t' : 'uint16_t';
Branch: 1 branch
BranchTrueFalseTotal
$Termbox::TRUECOLOR ? : 2 0 2
33 #
34 $EXPORT_TAGS{api} = [
35 2 qw[
36 tb_init tb_init_file tb_init_fd tb_init_rwfd tb_shutdown
37 tb_width tb_height
38 tb_clear tb_set_clear_attrs
39 tb_present
40 tb_invalidate
41 tb_set_cursor tb_hide_cursor
42 tb_set_cell tb_set_cell_ex tb_extend_cell
43 tb_set_input_mode
44 tb_set_output_mode
45 tb_peek_event
46 tb_poll_event
47 tb_get_fds
48 tb_print
49 tb_send
50 tb_set_func
51 tb_utf8_char_length tb_utf8_char_to_unicode tb_utf8_unicode_to_char
52 tb_last_errno tb_strerror
53 tb_cell_buffer
54 tb_has_truecolor tb_has_egc
55 tb_version
56 ]
57 ];
58 #
59 12 ▶ sub _export ($$) {
Subroutine _export: called CC=2 SCAR=6.9
60 12 my ( $tag, $values ) = @_;
61 12 push @{ $EXPORT_TAGS{$tag} }, keys %$values;
62 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 12 for my $key ( keys %$values ) {
64 264 ▶ *{ __PACKAGE__ . '::' . $key } = sub () { $values->{$key} }
Subroutine __ANON__: called CC=1 SCAR=0.0
65 264 }
66 }
67 #
68 2 _export keys => {
69  
70 # Terminal-dependent key constants (tb_event.key) and terminfo capabilities
71 TB_KEY_F1 => ( 0xFFFF - 0 ),
72 TB_KEY_F2 => ( 0xFFFF - 1 ),
73 TB_KEY_F3 => ( 0xFFFF - 2 ),
74 TB_KEY_F4 => ( 0xFFFF - 3 ),
75 TB_KEY_F5 => ( 0xFFFF - 4 ),
76 TB_KEY_F6 => ( 0xFFFF - 5 ),
77 TB_KEY_F7 => ( 0xFFFF - 6 ),
78 TB_KEY_F8 => ( 0xFFFF - 7 ),
79 TB_KEY_F9 => ( 0xFFFF - 8 ),
80 TB_KEY_F10 => ( 0xFFFF - 9 ),
81 TB_KEY_F11 => ( 0xFFFF - 10 ),
82 TB_KEY_F12 => ( 0xFFFF - 11 ),
83 TB_KEY_INSERT => ( 0xFFFF - 12 ),
84 TB_KEY_DELETE => ( 0xFFFF - 13 ),
85 TB_KEY_HOME => ( 0xFFFF - 14 ),
86 TB_KEY_END => ( 0xFFFF - 15 ),
87 TB_KEY_PGUP => ( 0xFFFF - 16 ),
88 TB_KEY_PGDN => ( 0xFFFF - 17 ),
89 TB_KEY_ARROW_UP => ( 0xFFFF - 18 ),
90 TB_KEY_ARROW_DOWN => ( 0xFFFF - 19 ),
91 TB_KEY_ARROW_LEFT => ( 0xFFFF - 20 ),
92 TB_KEY_ARROW_RIGHT => ( 0xFFFF - 21 ),
93 TB_KEY_BACK_TAB => ( 0xffff - 22 ),
94 TB_KEY_MOUSE_LEFT => ( 0xffff - 23 ),
95 TB_KEY_MOUSE_RIGHT => ( 0xffff - 24 ),
96 TB_KEY_MOUSE_MIDDLE => ( 0xffff - 25 ),
97 TB_KEY_MOUSE_RELEASE => ( 0xffff - 26 ),
98 TB_KEY_MOUSE_WHEEL_UP => ( 0xffff - 27 ),
99 TB_KEY_MOUSE_WHEEL_DOWN => ( 0xffff - 28 ),
100 #
101 TB_CAP_F1 => 0,
102 TB_CAP_F2 => 1,
103 TB_CAP_F3 => 2,
104 TB_CAP_F4 => 3,
105 TB_CAP_F5 => 4,
106 TB_CAP_F6 => 5,
107 TB_CAP_F7 => 6,
108 TB_CAP_F8 => 7,
109 TB_CAP_F9 => 8,
110 TB_CAP_F10 => 9,
111 TB_CAP_F11 => 10,
112 TB_CAP_F12 => 11,
113 TB_CAP_INSERT => 12,
114 TB_CAP_DELETE => 13,
115 TB_CAP_HOME => 14,
116 TB_CAP_END => 15,
117 TB_CAP_PGUP => 16,
118 TB_CAP_PGDN => 17,
119 TB_CAP_ARROW_UP => 18,
120 TB_CAP_ARROW_DOWN => 19,
121 TB_CAP_ARROW_LEFT => 20,
122 TB_CAP_ARROW_RIGHT => 21,
123 TB_CAP_BACK_TAB => 22,
124 TB_CAP__COUNT_KEYS => 23,
125 TB_CAP_ENTER_CA => 23,
126 TB_CAP_EXIT_CA => 24,
127 TB_CAP_SHOW_CURSOR => 25,
128 TB_CAP_HIDE_CURSOR => 26,
129 TB_CAP_CLEAR_SCREEN => 27,
130 TB_CAP_SGR0 => 28,
131 TB_CAP_UNDERLINE => 29,
132 TB_CAP_BOLD => 30,
133 TB_CAP_BLINK => 31,
134 TB_CAP_ITALIC => 32,
135 TB_CAP_REVERSE => 33,
136 TB_CAP_ENTER_KEYPAD => 34,
137 TB_CAP_EXIT_KEYPAD => 35,
138 TB_CAP__COUNT => 36
139 };
140 2 ▶ _export colors => {
Branch: 1 branch
BranchTrueFalseTotal
$Termbox::TRUECOLOR ? : 2 0 2
141 TB_DEFAULT => 0x0000,
142 TB_BLACK => 0x0001,
143 TB_RED => 0x0002,
144 TB_GREEN => 0x0003,
145 TB_YELLOW => 0x0004,
146 TB_BLUE => 0x0005,
147 TB_MAGENTA => 0x0006,
148 TB_CYAN => 0x0007,
149 TB_WHITE => 0x0008,
150 TB_BOLD => 0x0100,
151 TB_UNDERLINE => 0x0200,
152 TB_REVERSE => 0x0400,
153 TB_ITALIC => 0x0800,
154 TB_BLINK => 0x1000,
155 TB_256_BLACK => 0x2000, (
156 $TRUECOLOR ? (
157 TB_TRUECOLOR_BOLD => 0x01000000,
158 TB_TRUECOLOR_UNDERLINE => 0x02000000,
159 TB_TRUECOLOR_REVERSE => 0x04000000,
160 TB_TRUECOLOR_ITALIC => 0x08000000,
161 TB_TRUECOLOR_BLINK => 0x10000000,
162 TB_TRUECOLOR_BLACK => 0x20000000,
163 ) :
164 ()
165 )
166 };
167 2 ▶ _export event => {
Branch: 1 branch
BranchTrueFalseTotal
$Termbox::TRUECOLOR ? : 2 0 2
168  
169 #~ Event types (tb_event.type)
170 TB_EVENT_KEY => 1,
171 TB_EVENT_RESIZE => 2,
172 TB_EVENT_MOUSE => 3,
173  
174 #~ Key modifiers (bitwise) (tb_event.mod)
175 TB_MOD_ALT => 1,
176 TB_MOD_CTRL => 2,
177 TB_MOD_SHIFT => 4,
178 TB_MOD_MOTION => 8,
179  
180 #~ Input modes (bitwise) (tb_set_input_mode)
181 TB_INPUT_CURRENT => 0,
182 TB_INPUT_ESC => 1,
183 TB_INPUT_ALT => 2,
184 TB_INPUT_MOUSE => 4,
185  
186 #~ Output modes (tb_set_output_mode)
187 TB_OUTPUT_CURRENT => 0,
188 TB_OUTPUT_NORMAL => 1,
189 TB_OUTPUT_256 => 2,
190 TB_OUTPUT_216 => 3,
191 TB_OUTPUT_GRAYSCALE => 4,
192 ( $TRUECOLOR ? ( TB_OUTPUT_TRUECOLOR => 5 ) : () )
193 };
194 2 _export return => {
195 TB_OK => 0,
196 TB_ERR => -1,
197 TB_ERR_NEED_MORE => -2,
198 TB_ERR_INIT_ALREADY => -3,
199 TB_ERR_INIT_OPEN => -4,
200 TB_ERR_MEM => -5,
201 TB_ERR_NO_EVENT => -6,
202 TB_ERR_NO_TERM => -7,
203 TB_ERR_NOT_INIT => -8,
204 TB_ERR_OUT_OF_BOUNDS => -9,
205 TB_ERR_READ => -10,
206 TB_ERR_RESIZE_IOCTL => -11,
207 TB_ERR_RESIZE_PIPE => -12,
208 TB_ERR_RESIZE_SIGACTION => -13,
209 TB_ERR_POLL => -14,
210 TB_ERR_TCGETATTR => -15,
211 TB_ERR_TCSETATTR => -16,
212 TB_ERR_UNSUPPORTED_TERM => -17,
213 TB_ERR_RESIZE_WRITE => -18,
214 TB_ERR_RESIZE_POLL => -19,
215 TB_ERR_RESIZE_READ => -20,
216 TB_ERR_RESIZE_SSCANF => -21,
217 TB_ERR_CAP_COLLISION => -22
218 };
219 2 _export return =>
220 { TB_ERR_SELECT => TB_ERR_POLL(), TB_ERR_RESIZE_SELECT => TB_ERR_RESIZE_POLL() };
221 2 _export func => { TB_FUNC_EXTRACT_PRE => 0, TB_FUNC_EXTRACT_POST => 1 };
222 #
223 2 @EXPORT_OK = sort map { @$_ = sort @$_; @$_ } values %EXPORT_TAGS;
224 2 $EXPORT_TAGS{'all'} = \@EXPORT_OK; # When you want to import everything
225  
226 #
227 package #
228 2 Termbox::Cell {
229 2 ▶ use FFI::Platypus::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
230 2 ▶ record_layout_1(
Branch: 1 branch
BranchTrueFalseTotal
Termbox::tb_has_egc() ? : 2 0 2
231 uint32_t => 'ch',
232 $Termbox::uintattr_t => 'fg',
233 $Termbox::uintattr_t => 'bg', (
234 Termbox::tb_has_egc() ? ( 'opaque' => 'ech', size_t => 'nech', size_t => 'cech' ) :
235 ()
236 )
237 );
238 };
239 2 $ffi->type('record(Termbox::Cell)');
240 #
241 package #
242 2 Termbox::Event {
243 2 ▶ use FFI::Platypus::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
244 2 record_layout_1(
245 qw[
246 uint8_t type
247 uint8_t mod
248 uint16_t key
249 uint32_t ch
250 int32_t w
251 int32_t h
252 int32_t x
253 int32_t y
254 ]
255 );
256 };
257 #
258 2 $ffi->type('record(Termbox::Event)');
259 #
260 2 $ffi->attach( tb_init => ['void'] => 'int' );
261 2 $ffi->attach( tb_init_file => ['string'] => 'int' );
262 2 $ffi->attach( tb_init_fd => ['int'] => 'int' );
263 2 $ffi->attach( tb_init_rwfd => [ 'int', 'int' ] => 'int' );
264 2 $ffi->attach( tb_shutdown => ['void'] => 'void' );
265 #
266 2 $ffi->attach( tb_width => ['void'] => 'int' );
267 2 $ffi->attach( tb_height => ['void'] => 'int' );
268 #
269 2 $ffi->attach( tb_clear => ['void'] => 'void' );
270 2 $ffi->attach( tb_set_clear_attrs => [ $uintattr_t, $uintattr_t ] => 'void' );
271 #
272 2 $ffi->attach( tb_present => ['void'] => 'void' );
273 #
274 2 $ffi->attach( tb_invalidate => ['void'] => 'void' );
275 #
276 2 $ffi->attach( tb_set_cursor => [ 'int', 'int' ] => 'void' );
277 2 $ffi->attach( tb_hide_cursor => ['void'] => 'void' );
278 #
279 $ffi->attach(
280 tb_set_cell => [ 'int', 'int', 'uint32_t', $uintattr_t, $uintattr_t ] => 'int',
281 78 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
282 78 my ( $xsub, $x, $y, $ch, $fg, $bg ) = @_;
283 78 $xsub->( $x, $y, ord $ch, $fg, $bg );
284 }
285 2 );
286 $ffi->attach(
287 tb_set_cell_ex => [ 'int', 'int', 'uint32_t', 'size_t', $uintattr_t, $uintattr_t ] => 'int',
288 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
289 0 my ( $xsub, $x, $y, $ch, $nch, $fg, $bg ) = @_;
290 0 $xsub->( $x, $y, ord $ch, $nch, $fg, $bg );
291 }
292 2 );
293 $ffi->attach(
294 tb_extend_cell => [ 'int', 'int', 'uint32_t' ] => 'int',
295 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
296 0 my ( $xsub, $x, $y, $ch ) = @_;
297 0 $xsub->( $x, $y, ord $ch );
298 }
299 2 );
300 #
301 2 $ffi->attach( tb_set_input_mode => ['int'] => 'int' );
302 #
303 2 $ffi->attach( tb_set_output_mode => ['int'] => 'int' );
304 #
305 2 $ffi->attach( tb_peek_event => [ 'record(Termbox::Event)*', 'int' ] => 'int' );
306 #
307 2 $ffi->attach( tb_poll_event => ['record(Termbox::Event)*'] => 'int' );
308 #
309 2 $ffi->attach( tb_get_fds => [ 'int*', 'int*' ] => 'int' );
310 #
311 2 $ffi->attach( tb_print => [ 'int', 'int', $uintattr_t, $uintattr_t, 'string' ] => 'int' );
312  
313 #~ int tb_printf(int x, int y, uintattr_t fg, uintattr_t bg, const char *fmt, ...);
314 #~ int tb_print_ex(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w, const char *str);
315 #~ int tb_printf_ex(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w, const char *fmt, ...);
316 #
317 2 $ffi->attach( tb_send => [ 'string', 'size_t' ] => 'int' );
318 #
319 2 $ffi->type( '(opaque, opaque)->int' => 'closure_t' ); # int (*fn)(struct tb_event *, size_t *)
320 $ffi->attach(
321 tb_set_func => [ 'int', 'closure_t' ] => 'int',
322 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
323 0 CORE::state $cache;
324 0 my ( $xsub, $fn_type, $func ) = @_;
325 0 ▶ $cache->{$fn_type}->unsticky if $cache->{$fn_type};
Branch: 1 branch
BranchTrueFalseTotal
if $cache->{$fn_type} 0 0 0
326 0 my $closure;
327 0 ▶ if ($func) {
Branch: 1 branch
BranchTrueFalseTotal
if ($func) 0 0 0
328 $closure = $ffi->closure(
329 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
330 0 my ( $event, $size ) = @_;
331 0 $func->(
332 $ffi->cast( 'opaque', 'record(Termbox::Event)*', $event ),
333 $ffi->cast( 'opaque', 'size_t*', $size )
334 );
335 }
336 0 );
337 0 $closure->sticky;
338 }
339 0 $cache->{$fn_type} = $closure;
340 0 $xsub->( $fn_type, $closure );
341 }
342 2 );
343 #
344 2 $ffi->attach( tb_utf8_char_length => ['char'] => 'int' );
345 2 $ffi->attach( tb_utf8_char_to_unicode => [ 'uint32_t*', 'string*' ] => 'int' );
346 2 $ffi->attach( tb_utf8_unicode_to_char => [ 'string', 'uint32_t' ] => 'int' );
347 2 $ffi->attach( tb_last_errno => ['void'] => 'int' );
348 2 $ffi->attach( tb_strerror => ['int'] => 'string' );
349 2 $ffi->attach( tb_cell_buffer => ['void'] => 'record(Termbox::Cell)*' );
350  
351 # tb_has_truecolor and tbs_has_egc are defined near the top
352 2 $ffi->attach( tb_version => ['void'] => 'string' );
353 }
354 2 1;
355 __END__
↑ Summary