sled

Једноставни уређивач текста
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување 643743fb708fe29a4d017197f468d41d2658ad6d
родитељ eef7414de7638b48508da88a475ea36fe4a2cc4a
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Tue, 21 Jun 2022 16:15:50 +0200

termbox.h: Update from upstream

Signed-off-by: Страхиња Радић <contact@strahinja.org>

Diffstat:
Mtermbox.h | 320+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
измењених датотека: 1, додавања: 182(+), брисања: 138(-)

diff --git a/termbox.h b/termbox.h @@ -26,30 +26,34 @@ SOFTWARE. #ifndef __TERMBOX_H #define __TERMBOX_H +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE +#endif + +#ifndef _DEFAULT_SOURCE +#define _DEFAULT_SOURCE +#endif + #include <errno.h> #include <fcntl.h> +#include <limits.h> +#include <signal.h> #include <stdarg.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/ioctl.h> +#include <sys/select.h> #include <sys/stat.h> #include <sys/time.h> #include <sys/types.h> -#if !TB_OPT_SELECT -#include <poll.h> -#else -#include <sys/select.h> -#endif -#include <limits.h> -#include <signal.h> #include <termios.h> #include <unistd.h> #include <wchar.h> #ifdef __cplusplus -extern "C" { // __ffi_strip +extern "C" { #endif /* ASCII key constants (tb_event.key) */ @@ -102,7 +106,7 @@ extern "C" { // __ffi_strip #define tb_key_i(i) 0xffff - (i) /* Terminal-dependent key constants (tb_event.key) and terminfo capabilities */ /* BEGIN codegen h */ -/* Produced by ./codegen.sh on Fri, 18 Feb 2022 13:51:38 +0000 */ +/* Produced by ./codegen.sh on Sun, 19 Sep 2021 01:02:02 +0000 */ #define TB_KEY_F1 (0xffff - 0) #define TB_KEY_F2 (0xffff - 1) #define TB_KEY_F3 (0xffff - 2) @@ -191,30 +195,38 @@ extern "C" { // __ffi_strip #define TB_UNDERLINE 0x0200 #define TB_REVERSE 0x0400 #define TB_ITALIC 0x0800 +#define TB_BLINK 0x1000 +#ifdef TB_OPT_TRUECOLOR +#define TB_TRUECOLOR_BOLD 0x01000000 +#define TB_TRUECOLOR_UNDERLINE 0x02000000 +#define TB_TRUECOLOR_REVERSE 0x04000000 +#define TB_TRUECOLOR_ITALIC 0x08000000 +#define TB_TRUECOLOR_BLINK 0x10000000 +#endif /* Event types (tb_event.type) */ -#define TB_EVENT_KEY 1 -#define TB_EVENT_RESIZE 2 -#define TB_EVENT_MOUSE 3 +#define TB_EVENT_KEY 1 +#define TB_EVENT_RESIZE 2 +#define TB_EVENT_MOUSE 3 /* Key modifiers (bitwise) (tb_event.mod) */ -#define TB_MOD_ALT 1 -#define TB_MOD_CTRL 2 -#define TB_MOD_SHIFT 4 -#define TB_MOD_MOTION 8 +#define TB_MOD_ALT 1 +#define TB_MOD_CTRL 2 +#define TB_MOD_SHIFT 4 +#define TB_MOD_MOTION 8 /* Input modes (bitwise) (tb_set_input_mode) */ -#define TB_INPUT_CURRENT 0 -#define TB_INPUT_ESC 1 -#define TB_INPUT_ALT 2 -#define TB_INPUT_MOUSE 4 +#define TB_INPUT_CURRENT 0 +#define TB_INPUT_ESC 1 +#define TB_INPUT_ALT 2 +#define TB_INPUT_MOUSE 4 /* Output modes (tb_set_output_mode) */ -#define TB_OUTPUT_CURRENT 0 -#define TB_OUTPUT_NORMAL 1 -#define TB_OUTPUT_256 2 -#define TB_OUTPUT_216 3 -#define TB_OUTPUT_GRAYSCALE 4 +#define TB_OUTPUT_CURRENT 0 +#define TB_OUTPUT_NORMAL 1 +#define TB_OUTPUT_256 2 +#define TB_OUTPUT_216 3 +#define TB_OUTPUT_GRAYSCALE 4 #ifdef TB_OPT_TRUECOLOR #define TB_OUTPUT_TRUECOLOR 5 #endif @@ -281,10 +293,12 @@ extern "C" { // __ffi_strip #define tb_free free #endif +// __ffi_start + #ifdef TB_OPT_TRUECOLOR typedef uint32_t uintattr_t; #else -typedef uint16_t uintattr_t; // __ffi_strip +typedef uint16_t uintattr_t; #endif /* The terminal screen is represented as 2d array of cells. The structure is @@ -328,7 +342,7 @@ struct tb_cell { * TB_MOD_CTRL and TB_MOD_SHIFT are only set as * modifiers to TB_KEY_ARROW_*. * - * when TB_EVENT_RESIZE: w, h + * when TB_EVENT_RESIZE: w, h * * when TB_EVENT_MOUSE: key (TB_KEY_MOUSE_*), x, y */ @@ -396,10 +410,11 @@ int tb_extend_cell(int x, int y, uint32_t ch); * sequence, the next keyboard event is returned with a TB_MOD_ALT modifier. * * You can also apply TB_INPUT_MOUSE via bitwise OR operation to either of the - * modes (e.g. TB_INPUT_ESC | TB_INPUT_MOUSE). If none of the main two modes - * were set, but the mouse mode was, TB_INPUT_ESC mode is used. If for some - * reason you've decided to use (TB_INPUT_ESC | TB_INPUT_ALT) combination, it - * will behave as if only TB_INPUT_ESC was selected. + * modes (e.g., TB_INPUT_ESC | TB_INPUT_MOUSE) to receive TB_EVENT_MOUSE events. + * If none of the main two modes were set, but the mouse mode was, TB_INPUT_ESC + * mode is used. If for some reason you've decided to use + * (TB_INPUT_ESC | TB_INPUT_ALT) combination, it will behave as if only + * TB_INPUT_ESC was selected. * * If mode is TB_INPUT_CURRENT, the function returns the current input mode. * @@ -413,7 +428,11 @@ int tb_set_input_mode(int mode); * TB_BLACK, TB_RED, TB_GREEN, TB_YELLOW, * TB_BLUE, TB_MAGENTA, TB_CYAN, TB_WHITE * Colors may be bitwise OR'd with attributes: - * TB_BOLD, TB_UNDERLINE, TB_REVERSE, TB_ITALIC + * TB_BOLD, TB_UNDERLINE, TB_REVERSE, TB_ITALIC, TB_BLINK + * + * Some notes: TB_REVERSE can be applied as either fg or bg attributes for + * the same effect. TB_BOLD, TB_UNDERLINE, TB_ITALIC, TB_BLINK apply as fg + * attributes only, and are ignored as bg attributes. * * Example usage: * tb_set_cell(x, y, '@', TB_BLACK | TB_BOLD, TB_RED); @@ -421,7 +440,7 @@ int tb_set_input_mode(int mode); * 2. TB_OUTPUT_256 => [0..255] * In this mode you get 256 distinct colors: * 0x00 - 0x07: the 8 colors as in TB_OUTPUT_NORMAL - * 0x08 - 0x0f: TB_<color> | TB_BOLD + * 0x08 - 0x0f: bright versions of the above * 0x10 - 0xe7: 216 different colors * 0xe8 - 0xff: 24 different shades of grey * @@ -429,17 +448,17 @@ int tb_set_input_mode(int mode); * tb_set_cell(x, y, '@', 184, 240); * tb_set_cell(x, y, '@', 0xb8, 0xf0); * - * 3. TB_OUTPUT_216 => [0..216] - * This mode supports the 3rd range of TB_OUTPUT_256 only, but you don't need - * to provide an offset. + * 3. TB_OUTPUT_216 => [0..215] + * This mode supports the 3rd range of TB_OUTPUT_256 only, but you don't + * need to provide an offset. * * 4. TB_OUTPUT_GRAYSCALE => [0..23] - * This mode supports the 4th range of TB_OUTPUT_256 only, but you dont need - * to provide an offset. + * This mode supports the 4th range of TB_OUTPUT_256 only, but you don't + * need to provide an offset. * * 5. TB_OUTPUT_TRUECOLOR => [0x000000..0xffffff] * This mode provides 24-bit color on supported terminals. The format is - * 0xRRGGBB. + * 0xRRGGBB. Colors may be bitwise OR'd with `TB_TRUECOLOR_*` attributes. * * If mode is TB_OUTPUT_CURRENT, the function returns the current output mode. * @@ -449,7 +468,7 @@ int tb_set_output_mode(int mode); /* Wait for an event up to timeout_ms milliseconds and fill the event structure * with it. If no event is available within the timeout period, TB_ERR_NO_EVENT - * is returned. On a resize event, the underlying poll(2) call may be + * is returned. On a resize event, the underlying select(2) call may be * interrupted, yielding a return code of TB_ERR_POLL. In this case, you may * check errno via tb_last_errno(). If it's EINTR, you can safely ignore that * and call tb_peek_event() again. @@ -496,21 +515,18 @@ int tb_utf8_char_to_unicode(uint32_t *out, const char *c); int tb_utf8_unicode_to_char(char *out, uint32_t c); int tb_last_errno(); const char *tb_strerror(int err); - struct tb_cell *tb_cell_buffer(); +int tb_has_truecolor(); +int tb_has_egc(); #ifdef __cplusplus -} // __ffi_strip +} #endif #endif /* __TERMBOX_H */ #ifdef TB_IMPL -#ifndef TB_OPT_TRUECOLOR -#define TB_OUTPUT_TRUECOLOR (-1) -#endif - #define if_err_return(rv, expr) \ if (((rv) = (expr)) != TB_OK) \ return (rv) @@ -598,10 +614,10 @@ struct tb_global_t { int (*fn_extract_esc_post)(struct tb_event *, size_t *); }; -struct tb_global_t global = {0}; +static struct tb_global_t global = {0}; /* BEGIN codegen c */ -/* Produced by ./codegen.sh on Fri, 18 Feb 2022 13:51:39 +0000 */ +/* Produced by ./codegen.sh on Sun, 19 Sep 2021 01:02:03 +0000 */ static const int16_t terminfo_cap_indexes[] = { 66, // kf1 (TB_CAP_F1) @@ -706,7 +722,7 @@ static const char *linux_caps[] = { "\033[B", // kcud1 (TB_CAP_ARROW_DOWN) "\033[D", // kcub1 (TB_CAP_ARROW_LEFT) "\033[C", // kcuf1 (TB_CAP_ARROW_RIGHT) - "\033\011", // kcbt (TB_CAP_BACK_TAB) + "\033[Z", // kcbt (TB_CAP_BACK_TAB) "", // smcup (TB_CAP_ENTER_CA) "", // rmcup (TB_CAP_EXIT_CA) "\033[?25h\033[?0c", // cnorm (TB_CAP_SHOW_CURSOR) @@ -804,42 +820,42 @@ static const char *rxvt_256color_caps[] = { // rxvt-unicode static const char *rxvt_unicode_caps[] = { - "", // kf1 (TB_CAP_F1) - "", // kf2 (TB_CAP_F2) - "", // kf3 (TB_CAP_F3) - "", // kf4 (TB_CAP_F4) - "", // kf5 (TB_CAP_F5) - "", // kf6 (TB_CAP_F6) - "", // kf7 (TB_CAP_F7) - "", // kf8 (TB_CAP_F8) - "", // kf9 (TB_CAP_F9) - "", // kf10 (TB_CAP_F10) - "", // kf11 (TB_CAP_F11) - "", // kf12 (TB_CAP_F12) - "", // kich1 (TB_CAP_INSERT) - "", // kdch1 (TB_CAP_DELETE) - "", // khome (TB_CAP_HOME) - "", // kend (TB_CAP_END) - "", // kpp (TB_CAP_PGUP) - "", // knp (TB_CAP_PGDN) - "", // kcuu1 (TB_CAP_ARROW_UP) - "", // kcud1 (TB_CAP_ARROW_DOWN) - "", // kcub1 (TB_CAP_ARROW_LEFT) - "", // kcuf1 (TB_CAP_ARROW_RIGHT) - "", // kcbt (TB_CAP_BACK_TAB) - "", // smcup (TB_CAP_ENTER_CA) - "", // rmcup (TB_CAP_EXIT_CA) - "", // cnorm (TB_CAP_SHOW_CURSOR) - "", // civis (TB_CAP_HIDE_CURSOR) - "", // clear (TB_CAP_CLEAR_SCREEN) - "", // sgr0 (TB_CAP_SGR0) - "", // smul (TB_CAP_UNDERLINE) - "", // bold (TB_CAP_BOLD) - "", // blink (TB_CAP_BLINK) - "", // sitm (TB_CAP_ITALIC) - "", // rev (TB_CAP_REVERSE) - "", // smkx (TB_CAP_ENTER_KEYPAD) - "", // rmkx (TB_CAP_EXIT_KEYPAD) + "\033[11~", // kf1 (TB_CAP_F1) + "\033[12~", // kf2 (TB_CAP_F2) + "\033[13~", // kf3 (TB_CAP_F3) + "\033[14~", // kf4 (TB_CAP_F4) + "\033[15~", // kf5 (TB_CAP_F5) + "\033[17~", // kf6 (TB_CAP_F6) + "\033[18~", // kf7 (TB_CAP_F7) + "\033[19~", // kf8 (TB_CAP_F8) + "\033[20~", // kf9 (TB_CAP_F9) + "\033[21~", // kf10 (TB_CAP_F10) + "\033[23~", // kf11 (TB_CAP_F11) + "\033[24~", // kf12 (TB_CAP_F12) + "\033[2~", // kich1 (TB_CAP_INSERT) + "\033[3~", // kdch1 (TB_CAP_DELETE) + "\033[7~", // khome (TB_CAP_HOME) + "\033[8~", // kend (TB_CAP_END) + "\033[5~", // kpp (TB_CAP_PGUP) + "\033[6~", // knp (TB_CAP_PGDN) + "\033[A", // kcuu1 (TB_CAP_ARROW_UP) + "\033[B", // kcud1 (TB_CAP_ARROW_DOWN) + "\033[D", // kcub1 (TB_CAP_ARROW_LEFT) + "\033[C", // kcuf1 (TB_CAP_ARROW_RIGHT) + "\033[Z", // kcbt (TB_CAP_BACK_TAB) + "\033[?1049h", // smcup (TB_CAP_ENTER_CA) + "\033[r\033[?1049l", // rmcup (TB_CAP_EXIT_CA) + "\033[?12l\033[?25h", // cnorm (TB_CAP_SHOW_CURSOR) + "\033[?25l", // civis (TB_CAP_HIDE_CURSOR) + "\033[H\033[2J", // clear (TB_CAP_CLEAR_SCREEN) + "\033[m\033(B", // sgr0 (TB_CAP_SGR0) + "\033[4m", // smul (TB_CAP_UNDERLINE) + "\033[1m", // bold (TB_CAP_BOLD) + "\033[5m", // blink (TB_CAP_BLINK) + "\033[3m", // sitm (TB_CAP_ITALIC) + "\033[7m", // rev (TB_CAP_REVERSE) + "\033=", // smkx (TB_CAP_ENTER_KEYPAD) + "\033>", // rmkx (TB_CAP_EXIT_KEYPAD) }; // Eterm @@ -1470,7 +1486,8 @@ int tb_present() { struct tb_cell *front_wide; if_err_return(rv, cellbuf_get(&global.front, x + i, y, &front_wide)); - cell_set(front_wide, 0, 1, back->fg, back->bg); + if_err_return(rv, + cell_set(front_wide, 0, 1, back->fg, back->bg)); } } } @@ -1585,11 +1602,20 @@ int tb_set_input_mode(int mode) { int tb_set_output_mode(int mode) { if_not_init_return(); - if (mode == TB_OUTPUT_CURRENT) { - return global.output_mode; + switch (mode) { + case TB_OUTPUT_CURRENT: + return global.output_mode; + case TB_OUTPUT_NORMAL: + case TB_OUTPUT_256: + case TB_OUTPUT_216: + case TB_OUTPUT_GRAYSCALE: +#ifdef TB_OPT_TRUECOLOR + case TB_OUTPUT_TRUECOLOR: +#endif + global.output_mode = mode; + return TB_OK; } - global.output_mode = mode; - return TB_OK; + return TB_ERR; } int tb_peek_event(struct tb_event *event, int timeout_ms) { @@ -1799,6 +1825,22 @@ const char *tb_strerror(int err) { } } +int tb_has_truecolor() { +#ifdef TB_OPT_TRUECOLOR + return 1; +#else + return 0; +#endif +} + +int tb_has_egc() { +#ifdef TB_OPT_EGC + return 1; +#else + return 0; +#endif +} + static int tb_reset() { int ttyfd_open = global.ttyfd_open; memset(&global, 0, sizeof(global)); @@ -2055,10 +2097,6 @@ static int update_term_size_via_esc() { return TB_ERR_RESIZE_WRITE; } -#if !TB_OPT_SELECT - struct pollfd fd = {.fd = global.rfd, .events = POLLIN}; - int poll_rv = poll(&fd, 1, TB_RESIZE_FALLBACK_MS); -#else fd_set fds; FD_ZERO(&fds); FD_SET(global.rfd, &fds); @@ -2067,10 +2105,9 @@ static int update_term_size_via_esc() { timeout.tv_sec = 0; timeout.tv_usec = TB_RESIZE_FALLBACK_MS * 1000; - int poll_rv = select(global.rfd + 1, &fds, NULL, NULL, &timeout); -#endif + int select_rv = select(global.rfd + 1, &fds, NULL, NULL, &timeout); - if (poll_rv != 1) { + if (select_rv != 1) { global.last_errno = errno; return TB_ERR_RESIZE_POLL; } @@ -2361,22 +2398,12 @@ static int wait_event(struct tb_event *event, int timeout) { memset(event, 0, sizeof(*event)); if_ok_return(rv, extract_event(event)); -#if !TB_OPT_SELECT - struct pollfd fds[2] = { - {.fd = global.rfd, .events = POLLIN}, - {.fd = global.resize_pipefd[0], .events = POLLIN} - }; -#else fd_set fds; struct timeval tv; tv.tv_sec = timeout / 1000; tv.tv_usec = (timeout - (tv.tv_sec * 1000)) * 1000; -#endif do { -#if !TB_OPT_SELECT - int poll_rv = poll(fds, 2, timeout); -#else FD_ZERO(&fds); FD_SET(global.rfd, &fds); FD_SET(global.resize_pipefd[0], &fds); @@ -2385,25 +2412,19 @@ static int wait_event(struct tb_event *event, int timeout) { ? global.resize_pipefd[0] : global.rfd; - int poll_rv = + int select_rv = select(maxfd + 1, &fds, NULL, NULL, (timeout < 0) ? NULL : &tv); -#endif - if (poll_rv < 0) { + if (select_rv < 0) { // Let EINTR/EAGAIN bubble up global.last_errno = errno; return TB_ERR_POLL; - } else if (poll_rv == 0) { + } else if (select_rv == 0) { return TB_ERR_NO_EVENT; } -#if !TB_OPT_SELECT - int tty_has_events = (fds[0].revents & POLLIN); - int resize_has_events = (fds[1].revents & POLLIN); -#else int tty_has_events = (FD_ISSET(global.rfd, &fds)); int resize_has_events = (FD_ISSET(global.resize_pipefd[0], &fds)); -#endif if (tty_has_events) { ssize_t read_rv = read(global.rfd, buf, sizeof(buf)); @@ -2789,33 +2810,49 @@ static int send_attr(uintattr_t fg, uintattr_t bg) { cbg += 0xe8; break; +#ifdef TB_OPT_TRUECOLOR case TB_OUTPUT_TRUECOLOR: cfg = fg; cbg = bg; break; +#endif } - if (global.output_mode != TB_OUTPUT_TRUECOLOR) { - if (fg & TB_BOLD) - if_err_return(rv, - bytebuf_puts(&global.out, global.caps[TB_CAP_BOLD])); + uintattr_t attr_bold, attr_blink, attr_italic, attr_underline, attr_reverse; +#ifdef TB_OPT_TRUECOLOR + if (global.output_mode == TB_OUTPUT_TRUECOLOR) { + attr_bold = TB_TRUECOLOR_BOLD; + attr_blink = TB_TRUECOLOR_BLINK; + attr_italic = TB_TRUECOLOR_ITALIC; + attr_underline = TB_TRUECOLOR_UNDERLINE; + attr_reverse = TB_TRUECOLOR_REVERSE; + } else +#endif + { + attr_bold = TB_BOLD; + attr_blink = TB_BLINK; + attr_italic = TB_ITALIC; + attr_underline = TB_UNDERLINE; + attr_reverse = TB_REVERSE; + } - if (bg & TB_BOLD) - if_err_return(rv, - bytebuf_puts(&global.out, global.caps[TB_CAP_BLINK])); + if (fg & attr_bold) + if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_BOLD])); - if (fg & TB_UNDERLINE) - if_err_return(rv, - bytebuf_puts(&global.out, global.caps[TB_CAP_UNDERLINE])); + if (fg & attr_blink) + if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_BLINK])); - if (fg & TB_ITALIC) - if_err_return(rv, - bytebuf_puts(&global.out, global.caps[TB_CAP_ITALIC])); + if (fg & attr_underline) + if_err_return(rv, + bytebuf_puts(&global.out, global.caps[TB_CAP_UNDERLINE])); - if ((fg & TB_REVERSE) || (bg & TB_REVERSE)) - if_err_return(rv, - bytebuf_puts(&global.out, global.caps[TB_CAP_REVERSE])); - } + if (fg & attr_italic) + if_err_return(rv, + bytebuf_puts(&global.out, global.caps[TB_CAP_ITALIC])); + + if ((fg & attr_reverse) || (bg & attr_reverse)) + if_err_return(rv, + bytebuf_puts(&global.out, global.caps[TB_CAP_REVERSE])); if_err_return(rv, send_sgr(cfg, cbg)); @@ -2829,8 +2866,11 @@ static int send_sgr(uintattr_t fg, uintattr_t bg) { int rv; char nbuf[32]; - if (global.output_mode != TB_OUTPUT_TRUECOLOR && fg == TB_DEFAULT && - bg == TB_DEFAULT) + if ( +#ifdef TB_OPT_TRUECOLOR + global.output_mode != TB_OUTPUT_TRUECOLOR && +#endif + fg == TB_DEFAULT && bg == TB_DEFAULT) { return TB_OK; } @@ -2871,6 +2911,7 @@ static int send_sgr(uintattr_t fg, uintattr_t bg) { send_literal(rv, "m"); break; +#ifdef TB_OPT_TRUECOLOR case TB_OUTPUT_TRUECOLOR: send_literal(rv, "\x1b[38;2;"); send_num(rv, nbuf, (fg >> 16) & 0xff); @@ -2886,6 +2927,7 @@ static int send_sgr(uintattr_t fg, uintattr_t bg) { send_num(rv, nbuf, bg & 0xff); send_literal(rv, "m"); break; +#endif } return TB_OK; } @@ -2978,7 +3020,8 @@ static int cell_set(struct tb_cell *cell, uint32_t *ch, size_t nch, if (nch <= 1) { cell->nech = 0; } else { - cell_reserve_ech(cell, nch + 1); + int rv; + if_err_return(rv, cell_reserve_ech(cell, nch + 1)); memcpy(cell->ech, ch, nch); cell->ech[nch] = '\0'; cell->nech = nch; @@ -3041,10 +3084,11 @@ static int cellbuf_free(struct cellbuf_t *c) { } static int cellbuf_clear(struct cellbuf_t *c) { - int i; + int rv, i; uint32_t space = (uint32_t)' '; for (i = 0; i < c->width * c->height; i++) { - cell_set(&c->cells[i], &space, 1, global.fg, global.bg); + if_err_return(rv, + cell_set(&c->cells[i], &space, 1, global.fg, global.bg)); } return TB_OK; }