« blib/lib/Term/Fabulous/Terminal/Memory.pm
↑ Summary
blib/lib/Term/Fabulous/Terminal/Termbox/Cells.pm »
| 1 | package Term::Fabulous::Terminal::Termbox; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | use Object::Pad 0.825; | ||
| 11 | |||
| 12 | # Loaded first: dies with a clear message when termbox2 lacks truecolor, | ||
| 13 | # before the TB_OUTPUT_TRUECOLOR import below could fail obscurely. | ||
| 14 | use Term::Fabulous::Render::Attr (); | ||
| 15 | |||
| 16 | use Term::Fabulous::Role::Terminal; | ||
| 17 | use Term::Fabulous::Terminal::Termbox::Cells; | ||
| 18 | |||
| 19 | class Term::Fabulous::Terminal::Termbox :does(Term::Fabulous::Role::Terminal) :strict(params) { | ||
| 20 | use Feature::Compat::Try; | ||
| 21 | use POSIX qw(EINTR); | ||
| 22 | use Scalar::Util qw(openhandle); | ||
| 23 | use Term::Fabulous::Termbox qw( | ||
| 24 | tb_init tb_init_rwfd tf_init_inline tf_init_inline_rwfd tb_shutdown tb_width tb_height tb_hide_cursor tb_clear | ||
| 25 | tb_set_input_mode tb_set_output_mode tb_get_fds tb_peek_event tb_send | ||
| 26 | tb_last_errno tb_strerror tf_install_input_parser tf_input_ended | ||
| 27 | tf_cursor_position tf_reset_attrs tf_kitty_keyboard_query tf_sixel_query | ||
| 28 | TB_OK TB_ERR TB_ERR_NEED_MORE TB_ERR_NO_EVENT TB_ERR_POLL TB_ERR_READ | ||
| 29 | TB_INPUT_ESC TB_INPUT_MOUSE TB_OUTPUT_TRUECOLOR | ||
| 30 | ); | ||
| 31 | use Term::Fabulous::Termbox::Event; | ||
| 32 | use Term::Fabulous::Unicode qw(terminal_is_utf8); | ||
| 33 | |||
| 34 | # termbox2 sends UTF-8. A terminal shows those bytes in the character | ||
| 35 | # set of the locale; the Windows console is switched to UTF-8 by | ||
| 36 | # termbox2 itself, whatever the locale says. | ||
| 37 | use constant LOCALE_SETS_THE_CHARACTER_SET => $^O ne 'MSWin32'; | ||
| 38 | |||
| 39 | # A signal interrupts select on POSIX systems; on Windows no wait is | ||
| 40 | # interrupted, and tb_last_errno holds a Win32 error code, not an errno. | ||
| 41 | use constant POLL_CAN_BE_INTERRUPTED => $^O ne 'MSWin32'; | ||
| 42 | |||
| 43 | # Mouse mode 1003 (any-event tracking) reports the pointer moving with | ||
| 44 | # no button held; termbox2 asks only for buttons, drags and the wheel. | ||
| 45 | use constant REPORT_MOUSE_MOTION => "\x1b[?1003h"; | ||
| 46 | use constant STOP_MOUSE_MOTION_REPORT => "\x1b[?1003l"; | ||
| 47 | |||
| 48 | # The kitty keyboard protocol with its flags 1 (disambiguate escape | ||
| 49 | # codes) and 4 (report alternate keys), pushed onto the terminal's | ||
| 50 | # stack of flags and popped off again. | ||
| 51 | use constant PUSH_KITTY_KEYBOARD => "\x1b[>5u"; | ||
| 52 | use constant POP_KITTY_KEYBOARD => "\x1b[<u"; | ||
| 53 | use constant KITTY_KEYBOARD_QUERY_TIMEOUT_MS => 500; | ||
| 54 | |||
| 55 | use constant CURSOR_REPORT_TIMEOUT_MS => 1000; | ||
| 56 | use constant SIXEL_QUERY_TIMEOUT_MS => 500; | ||
| 57 | use constant ERASE_BELOW => "\x1b[J"; | ||
| 58 | |||
| 59 | use constant OPEN_OPTIONS => qw(inline mouse kitty_keyboard); | ||
| 60 | |||
| 61 | # Handles of another terminal to use instead of the controlling one. | ||
| 62 | field $input :param = undef; | ||
| 63 | field $output :param = undef; | ||
| 64 | |||
| 65 | field $cell_target :reader = Term::Fabulous::Terminal::Termbox::Cells->new; | ||
| 66 | field $is_open :reader = 0; | ||
| 67 | field $kitty_keyboard_active :reader = 0; | ||
| 68 | field $inline; # the rows asked for, or undef for the full screen | ||
| 69 | field $mouse = 0; | ||
| 70 | field $_shows_sixel = 0; | ||
| 71 | field @_size; # the layout's [columns, rows] | ||
| 72 | field $_input_fd; # termbox's terminal input descriptor | ||
| 73 | field @_read_handles; # duplicates of termbox's input descriptors | ||
| 74 | |||
| 75 | ADJUST { | ||
| 76 | die "Term::Fabulous::Terminal::Termbox: input and output go together; pass both or neither" | ||
| 77 | if defined $input != defined $output; | ||
| 78 | foreach my $handle ( grep { defined } $input, $output ) { | ||
| 79 | die "Term::Fabulous::Terminal::Termbox: input and output must be open file handles" unless openhandle($handle); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | method open (%options) { | ||
| 84 | die "Term::Fabulous::Terminal::Termbox: the terminal is open already" if $is_open; | ||
| 85 | my %known = map { $_ => 1 } OPEN_OPTIONS; | ||
| 86 | my @unknown = grep { !$known{$_} } sort keys %options; | ||
| 87 | die "Term::Fabulous::Terminal::Termbox: open does not accept @unknown (known options: " . join( ', ', OPEN_OPTIONS ) . ")" if @unknown; | ||
| 88 | die "Term::Fabulous::Terminal::Termbox: inline must be a whole number of rows of at least 1, got '$options{inline}'" | ||
| 89 | if defined $options{inline} && $options{inline} !~ /\A[1-9][0-9]*\z/; | ||
| 90 | warn "Term::Fabulous::Terminal::Termbox: the locale's character set is not UTF-8; wide characters will be misaligned\n" | ||
| 91 | if LOCALE_SETS_THE_CHARACTER_SET && !terminal_is_utf8(); | ||
| 92 | |||
| 93 | my ( $init, $rc ) = $self->_init( defined $options{inline} ); | ||
| 94 | die "Term::Fabulous::Terminal::Termbox: $init failed: " . tb_strerror($rc) . "\n" unless $rc == TB_OK; | ||
| 95 | ( $is_open, $inline, $mouse ) = ( 1, $options{inline}, $options{mouse} ? 1 : 0 ); | ||
| 96 | |||
| 97 | try { | ||
| 98 | $self->_prepare( $options{kitty_keyboard} ); | ||
| 99 | } | ||
| 100 | catch ($error) { | ||
| 101 | $self->close; | ||
| 102 | die $error; | ||
| 103 | } | ||
| 104 | return; | ||
| 105 | } | ||
| 106 | |||
| 107 | method _init ($is_inline) { | ||
| 108 | return $is_inline ? ( 'tf_init_inline', tf_init_inline() ) : ( 'tb_init', tb_init() ) unless defined $input; | ||
| 109 | my @fds = ( fileno $input, fileno $output ); | ||
| 110 | return $is_inline ? ( 'tf_init_inline_rwfd', tf_init_inline_rwfd(@fds) ) : ( 'tb_init_rwfd', tb_init_rwfd(@fds) ); | ||
| 111 | } | ||
| 112 | |||
| 113 | method _prepare ($kitty_keyboard) { | ||
| 114 | _check_termbox( 'tb_set_output_mode', tb_set_output_mode(TB_OUTPUT_TRUECOLOR) ); | ||
| 115 | _check_termbox( 'tb_set_input_mode', tb_set_input_mode( TB_INPUT_ESC | ( $mouse ? TB_INPUT_MOUSE : 0 ) ) ); | ||
| 116 | _check_termbox( 'tf_install_input_parser', tf_install_input_parser() ); | ||
| 117 | _check_termbox( 'tb_send', tb_send(REPORT_MOUSE_MOTION) ) if $mouse; | ||
| 118 | $self->_use_kitty_keyboard if $kitty_keyboard; | ||
| 119 | _check_termbox( 'tb_hide_cursor', tb_hide_cursor() ); | ||
| 120 | |||
| 121 | my ( $width, $height ) = ( tb_width(), tb_height() ); | ||
| 122 | die "Term::Fabulous::Terminal::Termbox: the terminal reports an unusable size of ${width}x${height}\n" | ||
| 123 | if $width < 1 || $height < 1; | ||
| 124 | @_size = ( $width, defined $inline ? $self->_anchor_inline_region($height) : $height ); | ||
| 125 | $self->_detect_sixel; | ||
| 126 | |||
| 127 | _check_termbox( 'tb_get_fds', tb_get_fds( \my $tty_fd, \my $resize_fd ) ); | ||
| 128 | $_input_fd = $tty_fd; | ||
| 129 | @_read_handles = map { _duplicate_for_reading($_) } grep { $_ >= 0 } $tty_fd, $resize_fd; # Windows has no resize descriptor | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | method close () { | ||
| 134 | return unless $is_open; | ||
| 135 | $is_open = 0; | ||
| 136 | tb_send(STOP_MOUSE_MOTION_REPORT) if $mouse; # termbox2 switches off only the modes it switched on | ||
| 137 | tb_send(POP_KITTY_KEYBOARD) if $kitty_keyboard_active; # before termbox2 leaves the alternate screen, which has a stack of its own | ||
| 138 | $kitty_keyboard_active = 0; | ||
| 139 | $_shows_sixel = 0; | ||
| 140 | $cell_target->set_sixel_cell_size; | ||
| 141 | $cell_target->forget_sixels; | ||
| 142 | $self->_leave_inline_region if defined $cell_target->region_top; | ||
| 143 | CORE::close($_) foreach @_read_handles; | ||
| 144 | @_read_handles = (); | ||
| 145 | $_input_fd = undef; | ||
| 146 | tb_shutdown(); | ||
| 147 | return; | ||
| 148 | } | ||
| 149 | |||
| 150 | # The safety net for a terminal that is dropped while open, by a program | ||
| 151 | # that dies outside Term::Fabulous's run (step leaves the terminal open) | ||
| 152 | # or never closes it: the shell gets its screen, modes and input back. | ||
| 153 | # No ${^GLOBAL_PHASE} guard, because global destruction is when a | ||
| 154 | # file-scoped object goes, and close needs only XS calls and fields. | ||
| 155 | method DESTROY { | ||
| 156 | $self->close if $is_open; | ||
| 157 | return; | ||
| 158 | } | ||
| 159 | |||
| 160 | method size () { | ||
| 161 | die "Term::Fabulous::Terminal::Termbox: the terminal is not open" unless $is_open; | ||
| 162 | return @_size; | ||
| 163 | } | ||
| 164 | |||
| 165 | method apply_resize ( $width, $height ) { | ||
| 166 | die "Term::Fabulous::Terminal::Termbox: the terminal is not open" unless $is_open; | ||
| 167 | @_size = ( $width, defined $inline ? $self->_anchor_inline_region($height) : $height ); | ||
| 168 | $self->_detect_sixel if $_shows_sixel; # a new font size changes the cell size | ||
| 169 | return @_size; | ||
| 170 | } | ||
| 171 | |||
| 172 | method read_handles () { | ||
| 173 | return @_read_handles; | ||
| 174 | } | ||
| 175 | |||
| 176 | method next_event () { | ||
| 177 | my $event = Term::Fabulous::Termbox::Event->new; | ||
| 178 | my $rc = tb_peek_event( $event, 0 ); | ||
| 179 | return $event if $rc == TB_OK; | ||
| 180 | |||
| 181 | # Nothing buffered, or only the start of a key sequence whose | ||
| 182 | # remaining bytes make the descriptor readable again; an | ||
| 183 | # interrupted poll leaves it readable, too. | ||
| 184 | return undef if $rc == TB_ERR_NO_EVENT || $rc == TB_ERR || $rc == TB_ERR_NEED_MORE; | ||
| 185 | return undef if $rc == TB_ERR_POLL && POLL_CAN_BE_INTERRUPTED && tb_last_errno() == EINTR; | ||
| 186 | die "Term::Fabulous::Terminal::Termbox: reading terminal input failed: " . tb_strerror($rc) . "\n"; | ||
| 187 | } | ||
| 188 | |||
| 189 | # After termbox has read all it could, a descriptor that still reports | ||
| 190 | # readable with no byte to read is at end of file: the terminal is gone. | ||
| 191 | # termbox reports that only as "no event", again and again. | ||
| 192 | method input_ended () { | ||
| 193 | return 0 unless defined $_input_fd; | ||
| 194 | return tf_input_ended($_input_fd); | ||
| 195 | } | ||
| 196 | |||
| 197 | # A terminal that does not answer the query does not speak the | ||
| 198 | # protocol; the input parser reads the legacy encodings as before. | ||
| 199 | method _use_kitty_keyboard () { | ||
| 200 | my $rc = tf_kitty_keyboard_query( KITTY_KEYBOARD_QUERY_TIMEOUT_MS, \my $supported ); | ||
| 201 | return if $rc == TB_ERR_NO_EVENT; | ||
| 202 | _check_termbox( 'tf_kitty_keyboard_query', $rc ); | ||
| 203 | return unless $supported; | ||
| 204 | _check_termbox( 'tb_send', tb_send(PUSH_KITTY_KEYBOARD) ); | ||
| 205 | $kitty_keyboard_active = 1; | ||
| 206 | return; | ||
| 207 | } | ||
| 208 | |||
| 209 | # A terminal that does not answer shows no sixel; one that does not | ||
| 210 | # tell the size of its cells shows none Term::Fabulous could place. | ||
| 211 | # Input that cannot be read is no answer either: next_event reports it. | ||
| 212 | method _detect_sixel () { | ||
| 213 | my $rc = tf_sixel_query( SIXEL_QUERY_TIMEOUT_MS, \my $supported, \my $cell_width, \my $cell_height ); | ||
| 214 | _check_termbox( 'tf_sixel_query', $rc ) unless $rc == TB_ERR_NO_EVENT || $rc == TB_ERR_READ; | ||
| 215 | $_shows_sixel = $rc == TB_OK && $supported ? 1 : 0; | ||
| 216 | $cell_target->set_sixel_cell_size( $_shows_sixel && $cell_width > 0 ? ( $cell_width, $cell_height ) : () ); | ||
| 217 | return; | ||
| 218 | } | ||
| 219 | |||
| 220 | # The region starts on the cursor's row, or on the next one when text | ||
| 221 | # precedes the cursor there. When it would reach below the screen, | ||
| 222 | # the terminal scrolls up first. Its rows are erased, since termbox | ||
| 223 | # takes the rows it has not drawn yet for blank. | ||
| 224 | method _anchor_inline_region ($screen_height) { | ||
| 225 | my $rc = tf_cursor_position( CURSOR_REPORT_TIMEOUT_MS, \my $column, \my $row ); | ||
| 226 | die "Term::Fabulous::Terminal::Termbox: the terminal did not report its cursor position; inline mode needs a terminal that answers ESC [ 6 n\n" | ||
| 227 | if $rc == TB_ERR_NO_EVENT; | ||
| 228 | _check_termbox( 'tf_cursor_position', $rc ); | ||
| 229 | |||
| 230 | my $rows = $inline < $screen_height ? $inline : $screen_height; | ||
| 231 | my $top = $column == 0 ? $row : $row + 1; | ||
| 232 | my $overflow = $top + $rows - $screen_height; | ||
| 233 | _check_termbox( 'tf_reset_attrs', tf_reset_attrs() ); # scrolled-in and erased rows take the current background | ||
| 234 | if ( $overflow > 0 ) { | ||
| 235 | _check_termbox( 'tb_send', tb_send( _cursor_to_row( $screen_height - 1 ) . "\n" x $overflow ) ); | ||
| 236 | $top -= $overflow; | ||
| 237 | } | ||
| 238 | _check_termbox( 'tb_send', tb_send( _cursor_to_row($top) . ERASE_BELOW ) ); | ||
| 239 | _check_termbox( 'tb_clear', tb_clear() ); # cells of an earlier region must not come back at their old rows | ||
| 240 | $cell_target->place_region( $top, $rows ); | ||
| 241 | return $rows; | ||
| 242 | } | ||
| 243 | |||
| 244 | # The last frame stays where it is; the shell goes on below it. | ||
| 245 | method _leave_inline_region () { | ||
| 246 | my $last_row = $cell_target->region_top + $cell_target->region_rows - 1; | ||
| 247 | tf_reset_attrs(); | ||
| 248 | tb_send( _cursor_to_row($last_row) . "\n" ); | ||
| 249 | $cell_target->place_region( undef, undef ); | ||
| 250 | return; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub _cursor_to_row ($row) { | ||
| 254 | return "\x1b[" . ( $row + 1 ) . ";1H"; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub _check_termbox ( $function, $rc ) { | ||
| 258 | die "Term::Fabulous::Terminal::Termbox: $function failed: " . tb_strerror($rc) . "\n" unless $rc == TB_OK; | ||
| 259 | return; | ||
| 260 | } | ||
| 261 | |||
| 262 | # A watcher may drop its handle; a duplicate keeps termbox's own | ||
| 263 | # descriptor open until tb_shutdown(). | ||
| 264 | sub _duplicate_for_reading ($fd) { | ||
| 265 | CORE::open( my $handle, '<&', $fd ) or die "Term::Fabulous::Terminal::Termbox: cannot duplicate file descriptor $fd: $!\n"; | ||
| 266 | return $handle; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | 1; | ||
| 271 | |||
| 272 | __END__ |