xref: /freebsd/sys/teken/teken.h (revision 547e74a8be76e2d9fcaa1c4c9b31225044f70963)
19b934d09SEd Schouten /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
299b934d09SEd Schouten  */
309b934d09SEd Schouten 
319b934d09SEd Schouten #ifndef _TEKEN_H_
329b934d09SEd Schouten #define	_TEKEN_H_
339b934d09SEd Schouten 
34ecc16c8dSEd Schouten #include <sys/types.h>
35ecc16c8dSEd Schouten 
369b934d09SEd Schouten /*
379b934d09SEd Schouten  * libteken: terminal emulation library.
389b934d09SEd Schouten  *
399b934d09SEd Schouten  * This library converts an UTF-8 stream of bytes to terminal drawing
409b934d09SEd Schouten  * commands.
419b934d09SEd Schouten  */
429b934d09SEd Schouten 
439b934d09SEd Schouten typedef uint32_t teken_char_t;
449b934d09SEd Schouten typedef unsigned short teken_unit_t;
459b934d09SEd Schouten typedef unsigned char teken_format_t;
46a6c26592SEd Schouten #define	TF_BOLD		0x01	/* Bold character. */
47a6c26592SEd Schouten #define	TF_UNDERLINE	0x02	/* Underline character. */
48a6c26592SEd Schouten #define	TF_BLINK	0x04	/* Blinking character. */
49a6c26592SEd Schouten #define	TF_REVERSE	0x08	/* Reverse rendered character. */
50a6c26592SEd Schouten #define	TF_CJK_RIGHT	0x10	/* Right-hand side of CJK character. */
519b934d09SEd Schouten typedef unsigned char teken_color_t;
529b934d09SEd Schouten #define	TC_BLACK	0
539b934d09SEd Schouten #define	TC_RED		1
549b934d09SEd Schouten #define	TC_GREEN	2
559b934d09SEd Schouten #define	TC_BROWN	3
569b934d09SEd Schouten #define	TC_BLUE		4
579b934d09SEd Schouten #define	TC_MAGENTA	5
589b934d09SEd Schouten #define	TC_CYAN		6
599b934d09SEd Schouten #define	TC_WHITE	7
609b934d09SEd Schouten #define	TC_NCOLORS	8
612610c9f2SBruce Evans #define	TC_LIGHT	8	/* ORed with the others. */
629b934d09SEd Schouten 
639b934d09SEd Schouten typedef struct {
649b934d09SEd Schouten 	teken_unit_t	tp_row;
659b934d09SEd Schouten 	teken_unit_t	tp_col;
669b934d09SEd Schouten } teken_pos_t;
679b934d09SEd Schouten typedef struct {
689b934d09SEd Schouten 	teken_pos_t	tr_begin;
699b934d09SEd Schouten 	teken_pos_t	tr_end;
709b934d09SEd Schouten } teken_rect_t;
719b934d09SEd Schouten typedef struct {
729b934d09SEd Schouten 	teken_format_t	ta_format;
739b934d09SEd Schouten 	teken_color_t	ta_fgcolor;
749b934d09SEd Schouten 	teken_color_t	ta_bgcolor;
759b934d09SEd Schouten } teken_attr_t;
769b934d09SEd Schouten typedef struct {
779b934d09SEd Schouten 	teken_unit_t	ts_begin;
789b934d09SEd Schouten 	teken_unit_t	ts_end;
799b934d09SEd Schouten } teken_span_t;
809b934d09SEd Schouten 
819b934d09SEd Schouten typedef struct __teken teken_t;
829b934d09SEd Schouten 
839b934d09SEd Schouten typedef void teken_state_t(teken_t *, teken_char_t);
849b934d09SEd Schouten 
859b934d09SEd Schouten /*
869b934d09SEd Schouten  * Drawing routines supplied by the user.
879b934d09SEd Schouten  */
889b934d09SEd Schouten 
899b934d09SEd Schouten typedef void tf_bell_t(void *);
909b934d09SEd Schouten typedef void tf_cursor_t(void *, const teken_pos_t *);
919b934d09SEd Schouten typedef void tf_putchar_t(void *, const teken_pos_t *, teken_char_t,
929b934d09SEd Schouten     const teken_attr_t *);
939b934d09SEd Schouten typedef void tf_fill_t(void *, const teken_rect_t *, teken_char_t,
949b934d09SEd Schouten     const teken_attr_t *);
959b934d09SEd Schouten typedef void tf_copy_t(void *, const teken_rect_t *, const teken_pos_t *);
96*547e74a8SJean-Sébastien Pédron typedef void tf_pre_input_t(void *);
97*547e74a8SJean-Sébastien Pédron typedef void tf_post_input_t(void *);
989b934d09SEd Schouten typedef void tf_param_t(void *, int, unsigned int);
999b934d09SEd Schouten #define	TP_SHOWCURSOR	0
1003a8a07eaSEd Schouten #define	TP_KEYPADAPP	1
1013a8a07eaSEd Schouten #define	TP_AUTOREPEAT	2
1023a8a07eaSEd Schouten #define	TP_SWITCHVT	3
1033a8a07eaSEd Schouten #define	TP_132COLS	4
1043a8a07eaSEd Schouten #define	TP_SETBELLPD	5
1059b934d09SEd Schouten #define	TP_SETBELLPD_PITCH(pd)		((pd) >> 16)
1069b934d09SEd Schouten #define	TP_SETBELLPD_DURATION(pd)	((pd) & 0xffff)
1073a8a07eaSEd Schouten #define	TP_MOUSE	6
108dd833891SBruce Evans #define	TP_SETBORDER	7
10915e0c651SBruce Evans #define	TP_SETLOCALCURSOR	8
11015e0c651SBruce Evans #define	TP_SETGLOBALCURSOR	9
1119b934d09SEd Schouten typedef void tf_respond_t(void *, const void *, size_t);
1129b934d09SEd Schouten 
1139b934d09SEd Schouten typedef struct {
1149b934d09SEd Schouten 	tf_bell_t	*tf_bell;
1159b934d09SEd Schouten 	tf_cursor_t	*tf_cursor;
1169b934d09SEd Schouten 	tf_putchar_t	*tf_putchar;
1179b934d09SEd Schouten 	tf_fill_t	*tf_fill;
1189b934d09SEd Schouten 	tf_copy_t	*tf_copy;
119*547e74a8SJean-Sébastien Pédron 	tf_pre_input_t	*tf_pre_input;
120*547e74a8SJean-Sébastien Pédron 	tf_post_input_t	*tf_post_input;
1219b934d09SEd Schouten 	tf_param_t	*tf_param;
1229b934d09SEd Schouten 	tf_respond_t	*tf_respond;
1239b934d09SEd Schouten } teken_funcs_t;
1249b934d09SEd Schouten 
12592223bddSPoul-Henning Kamp typedef teken_char_t teken_scs_t(const teken_t *, teken_char_t);
1269b934d09SEd Schouten 
1279b934d09SEd Schouten /*
1289b934d09SEd Schouten  * Terminal state.
1299b934d09SEd Schouten  */
1309b934d09SEd Schouten 
1319b934d09SEd Schouten struct __teken {
1329b934d09SEd Schouten 	const teken_funcs_t *t_funcs;
1339b934d09SEd Schouten 	void		*t_softc;
1349b934d09SEd Schouten 
1359b934d09SEd Schouten 	teken_state_t	*t_nextstate;
1369b934d09SEd Schouten 	unsigned int	 t_stateflags;
1379b934d09SEd Schouten 
1389b934d09SEd Schouten #define T_NUMSIZE	8
1399b934d09SEd Schouten 	unsigned int	 t_nums[T_NUMSIZE];
1409b934d09SEd Schouten 	unsigned int	 t_curnum;
1419b934d09SEd Schouten 
1429b934d09SEd Schouten 	teken_pos_t	 t_cursor;
1439b934d09SEd Schouten 	teken_attr_t	 t_curattr;
1449b934d09SEd Schouten 	teken_pos_t	 t_saved_cursor;
1459b934d09SEd Schouten 	teken_attr_t	 t_saved_curattr;
1469b934d09SEd Schouten 
1479b934d09SEd Schouten 	teken_attr_t	 t_defattr;
1489b934d09SEd Schouten 	teken_pos_t	 t_winsize;
1499b934d09SEd Schouten 
1509b934d09SEd Schouten 	/* For DECSTBM. */
1519b934d09SEd Schouten 	teken_span_t	 t_scrollreg;
1529b934d09SEd Schouten 	/* For DECOM. */
1539b934d09SEd Schouten 	teken_span_t	 t_originreg;
1549b934d09SEd Schouten 
1559b934d09SEd Schouten #define	T_NUMCOL	160
1569b934d09SEd Schouten 	unsigned int	 t_tabstops[T_NUMCOL / (sizeof(unsigned int) * 8)];
1579b934d09SEd Schouten 
158eba77f5cSEd Schouten 	unsigned int	 t_utf8_left;
1599b934d09SEd Schouten 	teken_char_t	 t_utf8_partial;
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 *);
214e06d84fcSEd Schouten 
21556a4365bSEd Schouten /* Color conversion. */
2162610c9f2SBruce Evans teken_color_t teken_256to16(teken_color_t);
21756a4365bSEd Schouten teken_color_t teken_256to8(teken_color_t);
21856a4365bSEd Schouten 
2199b934d09SEd Schouten #endif /* !_TEKEN_H_ */
220