=head1 NAME

Termbox::Go - Pure Perl termbox implementation

=head1 DESCRIPTION

This document describes the Termbox library for Perl, for the use of terminal 
applications.

The advantage of the Termbox library is the use of an standard. Termbox
contains a few functions with which console applications can be 
developed with high portability and interoperability. 

=head1 SUBROUTINES

=head2 CellBuffer

 my \@arrayref = CellBuffer();

Returns a slice into the termbox's back buffer. You can get its dimensions
using L</Size> function. The slice remains valid as long as no L</Clear> or
L</Flush> function calls were made after call to this function.


=head2 Clear

 my $errno = Clear($fg, $bg);

Clears the internal back buffer.


=head2 Close

 my $errno = Close();

Finalizes termbox library, should be called after successful initialization
when termbox's functionality isn't required anymore.


=head2 Flush

 my $errno = Flush();

Synchronizes the internal back buffer with the terminal.


=head2 GetCell

 my \%Cell = GetCell($x, $y);

Returns the specified cell from the internal back buffer.


=head2 HideCursor

 my $errno = HideCursor();

The shortcut for L<SetCursor(-1, -1)|/SetCursor>.


=head2 Init

 my $errno = Init();

Initializes termbox library. This function should be called before any other
functions. After successful initialization, the library must be finalized
using L</Close> function.

Example usage:

 my $err = Init();
 if ($err != 0) {
   die "Error: $err"
 }
 Close();



=head2 Interrupt

 my $errno = Interrupt();

Interrupt an in-progress call to L</PollEvent> by causing it to return
EventInterrupt.  Note that this function will block until the L</PollEvent>
function has successfully been interrupted.


=head2 ParseEvent

 my \%event = ParseEvent($data);

After getting a raw event from PollRawEvent function call, you can parse it
again into an ordinary one using termbox logic. That is parse an event as
termbox would do it. Returned event in addition to usual Event struct fields
sets N field to the amount of bytes used within C<data> slice. If the length
of C<data> slice is zero or event cannot be parsed for some other reason, the
function will return a special event type: EventNone.

B<IMPORTANT>: EventNone may contain a non-zero N, which means you should skip
these bytes, because termbox cannot recognize them.

B<NOTE>: This API is experimental and may change in future.


=head2 PollEvent

 my \%Event = PollEvent();

Wait for an event and return it. This is a blocking function call.


=head2 PollRawEvent

 my \%event = PollRawEvent($data);

Wait for an event and return it. This is a blocking function call. Instead
of EventKey and EventMouse it returns EventRaw events. Raw event is written
into C<data> slice and Event's N field is set to the amount of bytes written.
The minimum required length of the C<data> slice is 1. This requirement may
vary on different platforms.

B<NOTE>: This API is experimental and may change in future.


=head2 SetBg

 my $errno = SetBg($x, $y, $bg);

Changes cell's background attributes in the internal back buffer at
the specified position.


=head2 SetCell

 my $errno = SetCell($x, $y, $ch, $fg, $bg);

Changes cell's parameters in the internal back buffer at the specified
position.


=head2 SetChar

 my $errno = SetChar($x, $y, $ch);

Changes cell's character (utf8) in the internal back buffer at
the specified position.


=head2 SetCursor

 my $errno = SetCursor($x, $y);

Sets the position of the cursor. See also L</HideCursor>.


=head2 SetFg

 my $errno = SetFg($x, $y, $fg);

Changes cell's foreground attributes in the internal back buffer at
the specified position.


=head2 SetInputMode

 my $current = SetInputMode($mode);

Sets termbox input mode. Termbox has two input modes:

1. Esc input mode. When ESC sequence is in the buffer and it doesn't match
any known sequence. ESC means 'KeyEsc'. This is the default input mode.

2. Alt input mode. When ESC sequence is in the buffer and it doesn't match
any known sequence. ESC enables 'ModAlt' modifier for the next keyboard event.

Both input modes can be OR'ed with Mouse mode. Setting Mouse mode bit up will
enable mouse button press/release and drag events.

If I<$mode> is 'InputCurrent', returns the current input mode. See also
'Input*' constants.


=head2 SetOutputMode

 my $current = SetOutputMode($mode);

Sets the termbox output mode. Termbox has four output options:

1. OutputNormal => [1..8]
   This mode provides 8 different colors:
       black, red, green, yellow, blue, magenta, cyan, white
   Shortcut: ColorBlack, ColorRed, ...
   Attributes: AttrBold, AttrUnderline, AttrReverse

   Example usage:
       SetCell($x, $y, '@', ColorBlack | AttrBold, ColorRed);

2. Output256 => [1..256]
   In this mode you can leverage the 256 terminal mode:
   0x01 - 0x08: the 8 colors as in OutputNormal
   0x09 - 0x10: Color* | AttrBold
   0x11 - 0xe8: 216 different colors
   0xe9 - 0x1ff: 24 different shades of grey

   Example usage:
       SetCell($x, $y, '@', 184, 240);
       SetCell($x, $y, '@', 0xb8, 0xf0);

3. Output216 => [1..216]
   This mode supports the 3rd range of the 256 mode only.
   But you don't need to provide an offset.

4. OutputGrayscale => [1..26]
   This mode supports the 4th range of the 256 mode
   and black and white colors from 3th range of the 256 mode
   But you don't need to provide an offset.

In all modes, 0x00 represents the default color.

C<perl examples/output.pl> to see its impact on your terminal.

If I<$mode> is 'OutputCurrent', it returns the current output mode.

Note that this may return a different OutputMode than the one requested,
as the requested mode may not be available on the target platform.


=head2 Size

 my ($x, $y) = Size();

Returns the size of the internal back buffer (which is mostly the same as
terminal's window size in characters). But it doesn't always match the size
of the terminal window, after the terminal size has changed, the internal back
buffer will get in sync only after L</Clear> or L</Flush> function calls.


=head2 Sync

 my $errno = Sync();

Sync comes handy when something causes desync between termbox's understanding
of a terminal buffer and the reality. Such as a third party process. Sync
forces a complete resync between the termbox and a terminal, it may not be
visually pretty though.



=head1 SEE ALSO

L<Termbox>

L<Go termbox implementation|http://godoc.org/github.com/nsf/termbox-go>

=cut

=head1 COPYRIGHT AND LICENCE

 This file is part of the port of Termbox.
 
 Copyright (C) 2012 by termbox-go authors
 
 This library content was taken from the termbox-go implementation of Termbox
 which is licensed under MIT licence.
 
 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
 to deal in the Software without restriction, including without limitation
 the rights to use, copy, modify, merge, publish, distribute, sublicense,
 and/or sell copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

=head1 AUTHORS

=over

=item * 2024,2026 by J. Schneider L<https://github.com/brickpool/>

=back

=head1 DISCLAIMER OF WARRANTIES
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 DEALINGS IN THE SOFTWARE.

=cut
