💾 Archived View for gmi.noulin.net › gitRepositories › heartbeat › file › shpPackages › termbox › te… captured on 2024-06-20 at 11:56:06. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

heartbeat

Log

Files

Refs

README

termbox.h (13476B)

     1 #pragma once
     2 
     3 #include <stdint.h>
     4 
     5 /* for shared objects */
     6 #if __GNUC__ >= 4
     7  #define SO_IMPORT __attribute__((visibility("default")))
     8 #else
     9  #define SO_IMPORT
    10 #endif
    11 
    12 #ifdef __cplusplus
    13 extern "C" {
    14 #endif
    15 
    16 /* Key constants. See also struct tb_event's key field.
    17  *
    18  * These are a safe subset of terminfo keys, which exist on all popular
    19  * terminals. Termbox uses only them to stay truly portable.
    20  */
    21 #define TB_KEY_F1               (0xFFFF-0)
    22 #define TB_KEY_F2               (0xFFFF-1)
    23 #define TB_KEY_F3               (0xFFFF-2)
    24 #define TB_KEY_F4               (0xFFFF-3)
    25 #define TB_KEY_F5               (0xFFFF-4)
    26 #define TB_KEY_F6               (0xFFFF-5)
    27 #define TB_KEY_F7               (0xFFFF-6)
    28 #define TB_KEY_F8               (0xFFFF-7)
    29 #define TB_KEY_F9               (0xFFFF-8)
    30 #define TB_KEY_F10              (0xFFFF-9)
    31 #define TB_KEY_F11              (0xFFFF-10)
    32 #define TB_KEY_F12              (0xFFFF-11)
    33 #define TB_KEY_INSERT           (0xFFFF-12)
    34 #define TB_KEY_DELETE           (0xFFFF-13)
    35 #define TB_KEY_HOME             (0xFFFF-14)
    36 #define TB_KEY_END              (0xFFFF-15)
    37 #define TB_KEY_PGUP             (0xFFFF-16)
    38 #define TB_KEY_PGDN             (0xFFFF-17)
    39 #define TB_KEY_ARROW_UP         (0xFFFF-18)
    40 #define TB_KEY_ARROW_DOWN       (0xFFFF-19)
    41 #define TB_KEY_ARROW_LEFT       (0xFFFF-20)
    42 #define TB_KEY_ARROW_RIGHT      (0xFFFF-21)
    43 #define TB_KEY_MOUSE_LEFT       (0xFFFF-22)
    44 #define TB_KEY_MOUSE_RIGHT      (0xFFFF-23)
    45 #define TB_KEY_MOUSE_MIDDLE     (0xFFFF-24)
    46 #define TB_KEY_MOUSE_RELEASE    (0xFFFF-25)
    47 #define TB_KEY_MOUSE_WHEEL_UP   (0xFFFF-26)
    48 #define TB_KEY_MOUSE_WHEEL_DOWN (0xFFFF-27)
    49 
    50 /* These are all ASCII code points below SPACE character and a BACKSPACE key. */
    51 #define TB_KEY_CTRL_TILDE       0x00
    52 #define TB_KEY_CTRL_2           0x00 /* clash with 'CTRL_TILDE' */
    53 #define TB_KEY_CTRL_A           0x01
    54 #define TB_KEY_CTRL_B           0x02
    55 #define TB_KEY_CTRL_C           0x03
    56 #define TB_KEY_CTRL_D           0x04
    57 #define TB_KEY_CTRL_E           0x05
    58 #define TB_KEY_CTRL_F           0x06
    59 #define TB_KEY_CTRL_G           0x07
    60 #define TB_KEY_BACKSPACE        0x08
    61 #define TB_KEY_CTRL_H           0x08 /* clash with 'CTRL_BACKSPACE' */
    62 #define TB_KEY_TAB              0x09
    63 #define TB_KEY_CTRL_I           0x09 /* clash with 'TAB' */
    64 #define TB_KEY_CTRL_J           0x0A
    65 #define TB_KEY_CTRL_K           0x0B
    66 #define TB_KEY_CTRL_L           0x0C
    67 #define TB_KEY_ENTER            0x0D
    68 #define TB_KEY_CTRL_M           0x0D /* clash with 'ENTER' */
    69 #define TB_KEY_CTRL_N           0x0E
    70 #define TB_KEY_CTRL_O           0x0F
    71 #define TB_KEY_CTRL_P           0x10
    72 #define TB_KEY_CTRL_Q           0x11
    73 #define TB_KEY_CTRL_R           0x12
    74 #define TB_KEY_CTRL_S           0x13
    75 #define TB_KEY_CTRL_T           0x14
    76 #define TB_KEY_CTRL_U           0x15
    77 #define TB_KEY_CTRL_V           0x16
    78 #define TB_KEY_CTRL_W           0x17
    79 #define TB_KEY_CTRL_X           0x18
    80 #define TB_KEY_CTRL_Y           0x19
    81 #define TB_KEY_CTRL_Z           0x1A
    82 #define TB_KEY_ESC              0x1B
    83 #define TB_KEY_CTRL_LSQ_BRACKET 0x1B /* clash with 'ESC' */
    84 #define TB_KEY_CTRL_3           0x1B /* clash with 'ESC' */
    85 #define TB_KEY_CTRL_4           0x1C
    86 #define TB_KEY_CTRL_BACKSLASH   0x1C /* clash with 'CTRL_4' */
    87 #define TB_KEY_CTRL_5           0x1D
    88 #define TB_KEY_CTRL_RSQ_BRACKET 0x1D /* clash with 'CTRL_5' */
    89 #define TB_KEY_CTRL_6           0x1E
    90 #define TB_KEY_CTRL_7           0x1F
    91 #define TB_KEY_CTRL_SLASH       0x1F /* clash with 'CTRL_7' */
    92 #define TB_KEY_CTRL_UNDERSCORE  0x1F /* clash with 'CTRL_7' */
    93 #define TB_KEY_SPACE            0x20
    94 #define TB_KEY_BACKSPACE2       0x7F
    95 #define TB_KEY_CTRL_8           0x7F /* clash with 'BACKSPACE2' */
    96 
    97 /* These are non-existing ones.
    98  *
    99  * #define TB_KEY_CTRL_1 clash with '1'
   100  * #define TB_KEY_CTRL_9 clash with '9'
   101  * #define TB_KEY_CTRL_0 clash with '0'
   102  */
   103 
   104 /*
   105  * Alt modifier constant, see tb_event.mod field and tb_select_input_mode function.
   106  * Mouse-motion modifier
   107  */
   108 #define TB_MOD_ALT    0x01
   109 #define TB_MOD_MOTION 0x02
   110 
   111 /* Colors (see struct tb_cell's fg and bg fields). */
   112 #define TB_PALETTE 0x10000000
   113 #define TB_DEFAULT 0x08000000
   114 #define TB_BLACK         (0x00 | TB_PALETTE)
   115 #define TB_RED           (0x01 | TB_PALETTE)
   116 #define TB_GREEN         (0x02 | TB_PALETTE)
   117 #define TB_YELLOW        (0x03 | TB_PALETTE)
   118 #define TB_BLUE          (0x04 | TB_PALETTE)
   119 #define TB_MAGENTA       (0x05 | TB_PALETTE)
   120 #define TB_CYAN          (0x06 | TB_PALETTE)
   121 #define TB_WHITE         (0x07 | TB_PALETTE)
   122 #define TB_LIGHT_BLACK   (0x08 | TB_PALETTE)
   123 #define TB_LIGHT_RED     (0x09 | TB_PALETTE)
   124 #define TB_LIGHT_GREEN   (0x0A | TB_PALETTE)
   125 #define TB_LIGHT_YELLOW  (0x0B | TB_PALETTE)
   126 #define TB_LIGHT_BLUE    (0x0C | TB_PALETTE)
   127 #define TB_LIGHT_MAGENTA (0x0D | TB_PALETTE)
   128 #define TB_LIGHT_CYAN    (0x0E | TB_PALETTE)
   129 #define TB_LIGHT_WHITE   (0x0F | TB_PALETTE)
   130 
   131 extern uint32_t tb_palette[16];
   132 
   133 /* Attributes, it is possible to use multiple attributes by combining them
   134  * using bitwise OR ('|'). Although, colors cannot be combined. But you can
   135  * combine attributes and a single color. See also struct tb_cell's fg and bg
   136  * fields.
   137  */
   138 #define TB_BOLD      0x01000000
   139 #define TB_UNDERLINE 0x02000000
   140 #define TB_REVERSE   0x04000000
   141 // TB_DEFAULT        0x08000000
   142 // TB_PALETTE        0x10000000
   143 // TB_RESERVED       0x20000000
   144 #define TB_FAINT     0x40000000
   145 #define TB_ITALIC    0x80000000
   146 // bg effects: set TB_BLINK in bg color
   147 #define TB_BLINK     0x01000000
   148 #define TB_HIDDEN    0x02000000
   149 #define TB_CROSSED   0x04000000
   150 
   151 /* A cell, single conceptual entity on the terminal screen. The terminal screen
   152  * is basically a 2d array of cells. It has the following fields:
   153  *  - 'ch' is a unicode character
   154  *  - 'fg' foreground color and attributes
   155  *  - 'bg' background color and attributes
   156  */
   157 struct tb_cell {
   158         uint32_t ch;
   159         uint32_t fg;
   160         uint32_t bg;
   161 };
   162 
   163 #define TB_EVENT_KEY    1
   164 #define TB_EVENT_RESIZE 2
   165 #define TB_EVENT_MOUSE  3
   166 #define TB_EVENT_SOCKET 4
   167 
   168 /* An event, single interaction from the user. The 'mod' and 'ch' fields are
   169  * valid if 'type' is TB_EVENT_KEY. The 'w' and 'h' fields are valid if 'type'
   170  * is TB_EVENT_RESIZE. The 'x' and 'y' fields are valid if 'type' is
   171  * TB_EVENT_MOUSE. The 'key' field is valid if 'type' is either TB_EVENT_KEY
   172  * or TB_EVENT_MOUSE. The fields 'key' and 'ch' are mutually exclusive; only
   173  * one of them can be non-zero at a time.
   174  */
   175 struct tb_event {
   176         uint8_t type;
   177         uint8_t mod; /* modifiers to either 'key' or 'ch' below */
   178         uint16_t key; /* one of the TB_KEY_* constants */
   179         uint32_t ch; /* unicode character */
   180         int32_t w;
   181         int32_t h;
   182         int32_t x;
   183         int32_t y;
   184 };
   185 
   186 /* Error codes returned by tb_init(). All of them are self-explanatory, except
   187  * the pipe trap error. Termbox uses unix pipes in order to deliver a message
   188  * from a signal handler (SIGWINCH) to the main event reading loop. Honestly in
   189  * most cases you should just check the returned code as < 0.
   190  */
   191 #define TB_EUNSUPPORTED_TERMINAL -1
   192 #define TB_EFAILED_TO_OPEN_TTY   -2
   193 #define TB_EPIPE_TRAP_ERROR      -3
   194 
   195 /* Initializes the termbox library. This function should be called before any
   196  * other functions. Function tb_init is same as tb_init_file("/dev/tty").
   197  * After successful initialization, the library must be
   198  * finalized using the tb_shutdown() function.
   199  */
   200 SO_IMPORT int tb_init(void);
   201 SO_IMPORT int tb_init_file(const char* name);
   202 SO_IMPORT int tb_init_fd(int inout);
   203 SO_IMPORT void tb_shutdown(void);
   204 
   205 /* Returns the size of the internal back buffer (which is the same as
   206  * terminal's window size in characters). The internal buffer can be resized
   207  * after tb_clear() or tb_present() function calls. Both dimensions have an
   208  * unspecified negative value when called before tb_init() or after
   209  * tb_shutdown().
   210  */
   211 SO_IMPORT int tb_width(void);
   212 SO_IMPORT int tb_height(void);
   213 
   214 /* Clears the internal back buffer using TB_DEFAULT color or the
   215  * color/attributes set by tb_set_clear_attributes() function.
   216  */
   217 SO_IMPORT void tb_clear(void);
   218 SO_IMPORT void tb_set_clear_attributes(uint32_t fg, uint32_t bg);
   219 
   220 /* Synchronizes the internal back buffer with the terminal. */
   221 SO_IMPORT void tb_present(void);
   222 
   223 #define TB_HIDE_CURSOR -1
   224 
   225 /* Sets the position of the cursor. Upper-left character is (0, 0). If you pass
   226  * TB_HIDE_CURSOR as both coordinates, then the cursor will be hidden. Cursor
   227  * is hidden by default.
   228  */
   229 SO_IMPORT void tb_set_cursor(int cx, int cy);
   230 
   231 /* Changes cell's parameters in the internal back buffer at the specified
   232  * position.
   233  */
   234 SO_IMPORT void tb_put_cell(int x, int y, const struct tb_cell *cell);
   235 SO_IMPORT void tb_change_cell(int x, int y, uint32_t ch, uint32_t fg, uint32_t bg);
   236 
   237 /* Copies the buffer from 'cells' at the specified position, assuming the
   238  * buffer is a two-dimensional array of size ('w' x 'h'), represented as a
   239  * one-dimensional buffer containing lines of cells starting from the top.
   240  *
   241  * (DEPRECATED: use tb_cell_buffer() instead and copy memory on your own)
   242  */
   243 SO_IMPORT void tb_blit(int x, int y, int w, int h, const struct tb_cell *cells);
   244 
   245 /* Returns a pointer to internal cell back buffer. You can get its dimensions
   246  * using tb_width() and tb_height() functions. The pointer stays valid as long
   247  * as no tb_clear() and tb_present() calls are made. The buffer is
   248  * one-dimensional buffer containing lines of cells starting from the top.
   249  */
   250 SO_IMPORT struct tb_cell *tb_cell_buffer(void);
   251 
   252 #define TB_INPUT_CURRENT 0 /* 000 */
   253 #define TB_INPUT_ESC     1 /* 001 */
   254 #define TB_INPUT_ALT     2 /* 010 */
   255 #define TB_INPUT_MOUSE   4 /* 100 */
   256 
   257 /* Sets the termbox input mode. Termbox has two input modes:
   258  * 1. Esc input mode.
   259  *    When ESC sequence is in the buffer and it doesn't match any known
   260  *    ESC sequence => ESC means TB_KEY_ESC.
   261  * 2. Alt input mode.
   262  *    When ESC sequence is in the buffer and it doesn't match any known
   263  *    sequence => ESC enables TB_MOD_ALT modifier for the next keyboard event.
   264  *
   265  * You can also apply TB_INPUT_MOUSE via bitwise OR operation to either of the
   266  * modes (e.g. TB_INPUT_ESC | TB_INPUT_MOUSE). If none of the main two modes
   267  * were set, but the mouse mode was, TB_INPUT_ESC mode is used. If for some
   268  * reason you've decided to use (TB_INPUT_ESC | TB_INPUT_ALT) combination, it
   269  * will behave as if only TB_INPUT_ESC was selected.
   270  *
   271  * If 'mode' is TB_INPUT_CURRENT, it returns the current input mode.
   272  *
   273  * Default termbox input mode is TB_INPUT_ESC.
   274  */
   275 SO_IMPORT int tb_select_input_mode(int mode);
   276 
   277 #define TB_OUTPUT_CURRENT   0
   278 #define TB_OUTPUT_NORMAL    1
   279 #define TB_OUTPUT_256       2
   280 #define TB_OUTPUT_216       3
   281 #define TB_OUTPUT_GRAYSCALE 4
   282 #define TB_OUTPUT_TRUECOLOR 5
   283 
   284 /* Sets the termbox output mode. Termbox has three output options:
   285  * 1. TB_OUTPUT_NORMAL     => [1..8]
   286  *    This mode provides 8 different colors:
   287  *      black, red, green, yellow, blue, magenta, cyan, white
   288  *    Shortcut: TB_BLACK, TB_RED, ...
   289  *    Attributes: TB_BOLD, TB_UNDERLINE, TB_REVERSE
   290  *
   291  *    Example usage:
   292  *        tb_change_cell(x, y, '@', TB_BLACK | TB_BOLD, TB_RED);
   293  *
   294  * 2. TB_OUTPUT_256        => [0..256]
   295  *    In this mode you can leverage the 256 terminal mode:
   296  *    0x00 - 0x07: the 8 colors as in TB_OUTPUT_NORMAL
   297  *    0x08 - 0x0f: TB_* | TB_BOLD
   298  *    0x10 - 0xe7: 216 different colors
   299  *    0xe8 - 0xff: 24 different shades of grey
   300  *
   301  *    Example usage:
   302  *        tb_change_cell(x, y, '@', 184, 240);
   303  *        tb_change_cell(x, y, '@', 0xb8, 0xf0);
   304  *
   305  * 3. TB_OUTPUT_216        => [0..216]
   306  *    This mode supports the 3rd range of the 256 mode only.
   307  *    But you don't need to provide an offset.
   308  *
   309  * 4. TB_OUTPUT_GRAYSCALE  => [0..23]
   310  *    This mode supports the 4th range of the 256 mode only.
   311  *    But you dont need to provide an offset.
   312  *
   313  * 5. TB_OUTPUT_TRUECOLOR  => [0x000000..0xFFFFFF]
   314  *    This mode supports 24-bit true color. Format is 0xRRGGBB.
   315  *
   316  * Execute build/src/demo/output to see its impact on your terminal.
   317  *
   318  * If 'mode' is TB_OUTPUT_CURRENT, it returns the current output mode.
   319  *
   320  * Default termbox output mode is TB_OUTPUT_NORMAL.
   321  */
   322 SO_IMPORT int tb_select_output_mode(int mode);
   323 
   324 /* Wait for an event up to 'timeout' milliseconds and fill the 'event'
   325  * structure with it, when the event is available. Returns the type of the
   326  * event (one of TB_EVENT_* constants) or -1 if there was an error or 0 in case
   327  * there were no event during 'timeout' period.
   328  */
   329 SO_IMPORT int tb_peek_event(struct tb_event *event, int timeout);
   330 
   331 /* Wait for an event forever and fill the 'event' structure with it, when the
   332  * event is available. Returns the type of the event (one of TB_EVENT_*
   333  * constants) or -1 if there was an error.
   334  */
   335 SO_IMPORT int tb_poll_event(struct tb_event *event, int sock);
   336 
   337 /* Utility utf8 functions. */
   338 #define TB_EOF -1
   339 SO_IMPORT int tb_utf8_char_length(char c);
   340 SO_IMPORT int tb_utf8_char_to_unicode(uint32_t *out, const char *c);
   341 SO_IMPORT int tb_utf8_unicode_to_char(char *out, uint32_t c);
   342 SO_IMPORT int tb_unicode_is_char_wide(uint32_t cp);
   343 
   344 /* Set string starting at specific position */
   345 SO_IMPORT int tb_string(int x, int y, uint32_t fg, uint32_t bg, const char * str);
   346 
   347 SO_IMPORT int tb_string_with_limit(int x, int y, uint32_t fg, uint32_t bg, int limit, const char * str);
   348 
   349 /* Same as above but with format and arguments (printf-style) */
   350 SO_IMPORT int tb_stringf(int x, int y, uint32_t fg, uint32_t bg, const char * fmt, ...);
   351 
   352 SO_IMPORT int tb_stringf_with_limit(int x, int y, uint32_t fg, uint32_t bg, int limit, const char * fmt, ...);
   353 
   354 #ifdef __cplusplus
   355 }
   356 #endif