19b934d09SEd Schouten /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3fe267a55SPedro F. Giffuni * 49b934d09SEd Schouten * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org> 59b934d09SEd Schouten * All rights reserved. 69b934d09SEd Schouten * 79b934d09SEd Schouten * Redistribution and use in source and binary forms, with or without 89b934d09SEd Schouten * modification, are permitted provided that the following conditions 99b934d09SEd Schouten * are met: 109b934d09SEd Schouten * 1. Redistributions of source code must retain the above copyright 119b934d09SEd Schouten * notice, this list of conditions and the following disclaimer. 129b934d09SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright 139b934d09SEd Schouten * notice, this list of conditions and the following disclaimer in the 149b934d09SEd Schouten * documentation and/or other materials provided with the distribution. 159b934d09SEd Schouten * 169b934d09SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 179b934d09SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 189b934d09SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 199b934d09SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 209b934d09SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 219b934d09SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 229b934d09SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 239b934d09SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 249b934d09SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 259b934d09SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 269b934d09SEd Schouten * SUCH DAMAGE. 279b934d09SEd Schouten */ 289b934d09SEd Schouten 299b934d09SEd Schouten #ifndef _TEKEN_H_ 309b934d09SEd Schouten #define _TEKEN_H_ 319b934d09SEd Schouten 32ecc16c8dSEd Schouten #include <sys/types.h> 33ecc16c8dSEd Schouten 349b934d09SEd Schouten /* 359b934d09SEd Schouten * libteken: terminal emulation library. 369b934d09SEd Schouten * 379b934d09SEd Schouten * This library converts an UTF-8 stream of bytes to terminal drawing 389b934d09SEd Schouten * commands. 399b934d09SEd Schouten */ 409b934d09SEd Schouten 419b934d09SEd Schouten typedef uint32_t teken_char_t; 429b934d09SEd Schouten typedef unsigned short teken_unit_t; 439b934d09SEd Schouten typedef unsigned char teken_format_t; 44a6c26592SEd Schouten #define TF_BOLD 0x01 /* Bold character. */ 45a6c26592SEd Schouten #define TF_UNDERLINE 0x02 /* Underline character. */ 46a6c26592SEd Schouten #define TF_BLINK 0x04 /* Blinking character. */ 47a6c26592SEd Schouten #define TF_REVERSE 0x08 /* Reverse rendered character. */ 48a6c26592SEd Schouten #define TF_CJK_RIGHT 0x10 /* Right-hand side of CJK character. */ 493630506bSToomas Soome #define TF_IMAGE 0x20 /* This character space has image. */ 509b934d09SEd Schouten typedef unsigned char teken_color_t; 519b934d09SEd Schouten #define TC_BLACK 0 529b934d09SEd Schouten #define TC_RED 1 539b934d09SEd Schouten #define TC_GREEN 2 54cf8880d5SEd Maste #define TC_YELLOW 3 559b934d09SEd Schouten #define TC_BLUE 4 569b934d09SEd Schouten #define TC_MAGENTA 5 579b934d09SEd Schouten #define TC_CYAN 6 589b934d09SEd Schouten #define TC_WHITE 7 599b934d09SEd Schouten #define TC_NCOLORS 8 602610c9f2SBruce Evans #define TC_LIGHT 8 /* ORed with the others. */ 619b934d09SEd Schouten 629b934d09SEd Schouten typedef struct { 639b934d09SEd Schouten teken_unit_t tp_row; 649b934d09SEd Schouten teken_unit_t tp_col; 659b934d09SEd Schouten } teken_pos_t; 669b934d09SEd Schouten typedef struct { 679b934d09SEd Schouten teken_pos_t tr_begin; 689b934d09SEd Schouten teken_pos_t tr_end; 699b934d09SEd Schouten } teken_rect_t; 709b934d09SEd Schouten typedef struct { 719b934d09SEd Schouten teken_format_t ta_format; 729b934d09SEd Schouten teken_color_t ta_fgcolor; 739b934d09SEd Schouten teken_color_t ta_bgcolor; 749b934d09SEd Schouten } teken_attr_t; 759b934d09SEd Schouten typedef struct { 769b934d09SEd Schouten teken_unit_t ts_begin; 779b934d09SEd Schouten teken_unit_t ts_end; 789b934d09SEd Schouten } teken_span_t; 799b934d09SEd Schouten 809b934d09SEd Schouten typedef struct __teken teken_t; 819b934d09SEd Schouten 829b934d09SEd Schouten typedef void teken_state_t(teken_t *, teken_char_t); 839b934d09SEd Schouten 849b934d09SEd Schouten /* 859b934d09SEd Schouten * Drawing routines supplied by the user. 869b934d09SEd Schouten */ 879b934d09SEd Schouten 889b934d09SEd Schouten typedef void tf_bell_t(void *); 899b934d09SEd Schouten typedef void tf_cursor_t(void *, const teken_pos_t *); 909b934d09SEd Schouten typedef void tf_putchar_t(void *, const teken_pos_t *, teken_char_t, 919b934d09SEd Schouten const teken_attr_t *); 929b934d09SEd Schouten typedef void tf_fill_t(void *, const teken_rect_t *, teken_char_t, 939b934d09SEd Schouten const teken_attr_t *); 949b934d09SEd Schouten typedef void tf_copy_t(void *, const teken_rect_t *, const teken_pos_t *); 95547e74a8SJean-Sébastien Pédron typedef void tf_pre_input_t(void *); 96547e74a8SJean-Sébastien Pédron typedef void tf_post_input_t(void *); 979b934d09SEd Schouten typedef void tf_param_t(void *, int, unsigned int); 989b934d09SEd Schouten #define TP_SHOWCURSOR 0 993a8a07eaSEd Schouten #define TP_KEYPADAPP 1 1003a8a07eaSEd Schouten #define TP_AUTOREPEAT 2 1013a8a07eaSEd Schouten #define TP_SWITCHVT 3 1023a8a07eaSEd Schouten #define TP_132COLS 4 1033a8a07eaSEd Schouten #define TP_SETBELLPD 5 1049b934d09SEd Schouten #define TP_SETBELLPD_PITCH(pd) ((pd) >> 16) 1059b934d09SEd Schouten #define TP_SETBELLPD_DURATION(pd) ((pd) & 0xffff) 1063a8a07eaSEd Schouten #define TP_MOUSE 6 107dd833891SBruce Evans #define TP_SETBORDER 7 10815e0c651SBruce Evans #define TP_SETLOCALCURSOR 8 10915e0c651SBruce Evans #define TP_SETGLOBALCURSOR 9 1109b934d09SEd Schouten typedef void tf_respond_t(void *, const void *, size_t); 1119b934d09SEd Schouten 1129b934d09SEd Schouten typedef struct { 1139b934d09SEd Schouten tf_bell_t *tf_bell; 1149b934d09SEd Schouten tf_cursor_t *tf_cursor; 1159b934d09SEd Schouten tf_putchar_t *tf_putchar; 1169b934d09SEd Schouten tf_fill_t *tf_fill; 1179b934d09SEd Schouten tf_copy_t *tf_copy; 118547e74a8SJean-Sébastien Pédron tf_pre_input_t *tf_pre_input; 119547e74a8SJean-Sébastien Pédron tf_post_input_t *tf_post_input; 1209b934d09SEd Schouten tf_param_t *tf_param; 1219b934d09SEd Schouten tf_respond_t *tf_respond; 1229b934d09SEd Schouten } teken_funcs_t; 1239b934d09SEd Schouten 12492223bddSPoul-Henning Kamp typedef teken_char_t teken_scs_t(const teken_t *, teken_char_t); 1259b934d09SEd Schouten 1269b934d09SEd Schouten /* 1279b934d09SEd Schouten * Terminal state. 1289b934d09SEd Schouten */ 1299b934d09SEd Schouten 1309b934d09SEd Schouten struct __teken { 1319b934d09SEd Schouten const teken_funcs_t *t_funcs; 1329b934d09SEd Schouten void *t_softc; 1339b934d09SEd Schouten 1349b934d09SEd Schouten teken_state_t *t_nextstate; 1359b934d09SEd Schouten unsigned int t_stateflags; 1369b934d09SEd Schouten 1379b934d09SEd Schouten #define T_NUMSIZE 8 1389b934d09SEd Schouten unsigned int t_nums[T_NUMSIZE]; 1399b934d09SEd Schouten unsigned int t_curnum; 1409b934d09SEd Schouten 1419b934d09SEd Schouten teken_pos_t t_cursor; 1429b934d09SEd Schouten teken_attr_t t_curattr; 1439b934d09SEd Schouten teken_pos_t t_saved_cursor; 1449b934d09SEd Schouten teken_attr_t t_saved_curattr; 1459b934d09SEd Schouten 1469b934d09SEd Schouten teken_attr_t t_defattr; 1479b934d09SEd Schouten teken_pos_t t_winsize; 1489b934d09SEd Schouten 1499b934d09SEd Schouten /* For DECSTBM. */ 1509b934d09SEd Schouten teken_span_t t_scrollreg; 1519b934d09SEd Schouten /* For DECOM. */ 1529b934d09SEd Schouten teken_span_t t_originreg; 1539b934d09SEd Schouten 1549b934d09SEd Schouten #define T_NUMCOL 160 1559b934d09SEd Schouten unsigned int t_tabstops[T_NUMCOL / (sizeof(unsigned int) * 8)]; 1569b934d09SEd Schouten 157eba77f5cSEd Schouten unsigned int t_utf8_left; 1589b934d09SEd Schouten teken_char_t t_utf8_partial; 1593eb27bf0SPoul-Henning Kamp teken_char_t t_last; 1609b934d09SEd Schouten 1619b934d09SEd Schouten unsigned int t_curscs; 1629b934d09SEd Schouten teken_scs_t *t_saved_curscs; 1639b934d09SEd Schouten teken_scs_t *t_scs[2]; 1649b934d09SEd Schouten }; 1659b934d09SEd Schouten 1669b934d09SEd Schouten /* Initialize teken structure. */ 1679b934d09SEd Schouten void teken_init(teken_t *, const teken_funcs_t *, void *); 1689b934d09SEd Schouten 1699b934d09SEd Schouten /* Deliver character input. */ 1709b934d09SEd Schouten void teken_input(teken_t *, const void *, size_t); 1719b934d09SEd Schouten 1729b934d09SEd Schouten /* Get/set teken attributes. */ 17392223bddSPoul-Henning Kamp const teken_pos_t *teken_get_cursor(const teken_t *); 17492223bddSPoul-Henning Kamp const teken_attr_t *teken_get_curattr(const teken_t *); 17592223bddSPoul-Henning Kamp const teken_attr_t *teken_get_defattr(const teken_t *); 17692223bddSPoul-Henning Kamp void teken_get_defattr_cons25(const teken_t *, int *, int *); 17792223bddSPoul-Henning Kamp const teken_pos_t *teken_get_winsize(const teken_t *); 1789b934d09SEd Schouten void teken_set_cursor(teken_t *, const teken_pos_t *); 1799b934d09SEd Schouten void teken_set_curattr(teken_t *, const teken_attr_t *); 1809b934d09SEd Schouten void teken_set_defattr(teken_t *, const teken_attr_t *); 1819b934d09SEd Schouten void teken_set_winsize(teken_t *, const teken_pos_t *); 18227cf7d04SAleksandr Rybalko void teken_set_winsize_noreset(teken_t *, const teken_pos_t *); 1839b934d09SEd Schouten 1843a8a07eaSEd Schouten /* Key input escape sequences. */ 1853a8a07eaSEd Schouten #define TKEY_UP 0x00 1863a8a07eaSEd Schouten #define TKEY_DOWN 0x01 1873a8a07eaSEd Schouten #define TKEY_LEFT 0x02 1883a8a07eaSEd Schouten #define TKEY_RIGHT 0x03 1893a8a07eaSEd Schouten 190d80a1e6cSEd Schouten #define TKEY_HOME 0x04 191d80a1e6cSEd Schouten #define TKEY_END 0x05 192d80a1e6cSEd Schouten #define TKEY_INSERT 0x06 193d80a1e6cSEd Schouten #define TKEY_DELETE 0x07 1943a8a07eaSEd Schouten #define TKEY_PAGE_UP 0x08 1953a8a07eaSEd Schouten #define TKEY_PAGE_DOWN 0x09 1963a8a07eaSEd Schouten 1973a8a07eaSEd Schouten #define TKEY_F1 0x0a 1983a8a07eaSEd Schouten #define TKEY_F2 0x0b 1993a8a07eaSEd Schouten #define TKEY_F3 0x0c 2003a8a07eaSEd Schouten #define TKEY_F4 0x0d 2013a8a07eaSEd Schouten #define TKEY_F5 0x0e 2023a8a07eaSEd Schouten #define TKEY_F6 0x0f 2033a8a07eaSEd Schouten #define TKEY_F7 0x10 2043a8a07eaSEd Schouten #define TKEY_F8 0x11 2053a8a07eaSEd Schouten #define TKEY_F9 0x12 2063a8a07eaSEd Schouten #define TKEY_F10 0x13 2073a8a07eaSEd Schouten #define TKEY_F11 0x14 2083a8a07eaSEd Schouten #define TKEY_F12 0x15 20992223bddSPoul-Henning Kamp const char *teken_get_sequence(const teken_t *, unsigned int); 2103a8a07eaSEd Schouten 211e06d84fcSEd Schouten /* Legacy features. */ 212e06d84fcSEd Schouten void teken_set_8bit(teken_t *); 213eba77f5cSEd Schouten void teken_set_cons25(teken_t *); 2143a199184SBruce Evans void teken_set_cons25keys(teken_t *); 215e06d84fcSEd Schouten 21656a4365bSEd Schouten /* Color conversion. */ 2172610c9f2SBruce Evans teken_color_t teken_256to16(teken_color_t); 21856a4365bSEd Schouten teken_color_t teken_256to8(teken_color_t); 21956a4365bSEd Schouten 2209b934d09SEd Schouten #endif /* !_TEKEN_H_ */ 221