teken.h revision 303975
160484Sobrien/*-
2104834Sobrien * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
360484Sobrien * All rights reserved.
433965Sjdp *
533965Sjdp * Redistribution and use in source and binary forms, with or without
633965Sjdp * modification, are permitted provided that the following conditions
733965Sjdp * are met:
833965Sjdp * 1. Redistributions of source code must retain the above copyright
960484Sobrien *    notice, this list of conditions and the following disclaimer.
1033965Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1133965Sjdp *    notice, this list of conditions and the following disclaimer in the
1233965Sjdp *    documentation and/or other materials provided with the distribution.
1333965Sjdp *
1433965Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1533965Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1633965Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1733965Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1838889Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1938889Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2038889Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2133965Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2277298Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2333965Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2433965Sjdp * SUCH DAMAGE.
2533965Sjdp *
2633965Sjdp * $FreeBSD: releng/11.0/sys/teken/teken.h 259667 2013-12-20 21:31:50Z ed $
2789857Sobrien */
2833965Sjdp
2933965Sjdp#ifndef _TEKEN_H_
3033965Sjdp#define	_TEKEN_H_
3133965Sjdp
3233965Sjdp#include <sys/types.h>
3333965Sjdp
34107492Sobrien/*
3533965Sjdp * libteken: terminal emulation library.
3633965Sjdp *
3778828Sobrien * This library converts an UTF-8 stream of bytes to terminal drawing
3833965Sjdp * commands.
3933965Sjdp */
4033965Sjdp
4133965Sjdptypedef uint32_t teken_char_t;
4233965Sjdptypedef unsigned short teken_unit_t;
4333965Sjdptypedef unsigned char teken_format_t;
4433965Sjdp#define	TF_BOLD		0x01	/* Bold character. */
4533965Sjdp#define	TF_UNDERLINE	0x02	/* Underline character. */
4633965Sjdp#define	TF_BLINK	0x04	/* Blinking character. */
4733965Sjdp#define	TF_REVERSE	0x08	/* Reverse rendered character. */
4833965Sjdp#define	TF_CJK_RIGHT	0x10	/* Right-hand side of CJK character. */
4933965Sjdptypedef unsigned char teken_color_t;
5038889Sjdp#define	TC_BLACK	0
5138889Sjdp#define	TC_RED		1
5238889Sjdp#define	TC_GREEN	2
5333965Sjdp#define	TC_BROWN	3
5433965Sjdp#define	TC_BLUE		4
5538889Sjdp#define	TC_MAGENTA	5
5633965Sjdp#define	TC_CYAN		6
5777298Sobrien#define	TC_WHITE	7
5833965Sjdp#define	TC_NCOLORS	8
5933965Sjdp
6033965Sjdptypedef struct {
6133965Sjdp	teken_unit_t	tp_row;
6233965Sjdp	teken_unit_t	tp_col;
6333965Sjdp} teken_pos_t;
6433965Sjdptypedef struct {
6577298Sobrien	teken_pos_t	tr_begin;
6677298Sobrien	teken_pos_t	tr_end;
6733965Sjdp} teken_rect_t;
6833965Sjdptypedef struct {
6933965Sjdp	teken_format_t	ta_format;
7033965Sjdp	teken_color_t	ta_fgcolor;
7133965Sjdp	teken_color_t	ta_bgcolor;
7277298Sobrien} teken_attr_t;
7333965Sjdptypedef struct {
7433965Sjdp	teken_unit_t	ts_begin;
7533965Sjdp	teken_unit_t	ts_end;
7633965Sjdp} teken_span_t;
7733965Sjdp
7833965Sjdptypedef struct __teken teken_t;
7933965Sjdp
8033965Sjdptypedef void teken_state_t(teken_t *, teken_char_t);
8133965Sjdp
8289857Sobrien/*
8389857Sobrien * Drawing routines supplied by the user.
8489857Sobrien */
8533965Sjdp
8633965Sjdptypedef void tf_bell_t(void *);
8733965Sjdptypedef void tf_cursor_t(void *, const teken_pos_t *);
8833965Sjdptypedef void tf_putchar_t(void *, const teken_pos_t *, teken_char_t,
8933965Sjdp    const teken_attr_t *);
9033965Sjdptypedef void tf_fill_t(void *, const teken_rect_t *, teken_char_t,
9133965Sjdp    const teken_attr_t *);
9233965Sjdptypedef void tf_copy_t(void *, const teken_rect_t *, const teken_pos_t *);
9333965Sjdptypedef void tf_param_t(void *, int, unsigned int);
9433965Sjdp#define	TP_SHOWCURSOR	0
9533965Sjdp#define	TP_KEYPADAPP	1
9633965Sjdp#define	TP_AUTOREPEAT	2
9733965Sjdp#define	TP_SWITCHVT	3
9833965Sjdp#define	TP_132COLS	4
9933965Sjdp#define	TP_SETBELLPD	5
10033965Sjdp#define	TP_SETBELLPD_PITCH(pd)		((pd) >> 16)
10133965Sjdp#define	TP_SETBELLPD_DURATION(pd)	((pd) & 0xffff)
10233965Sjdp#define	TP_MOUSE	6
10360484Sobrientypedef void tf_respond_t(void *, const void *, size_t);
10460484Sobrien
10560484Sobrientypedef struct {
10660484Sobrien	tf_bell_t	*tf_bell;
10760484Sobrien	tf_cursor_t	*tf_cursor;
10860484Sobrien	tf_putchar_t	*tf_putchar;
10933965Sjdp	tf_fill_t	*tf_fill;
11060484Sobrien	tf_copy_t	*tf_copy;
11133965Sjdp	tf_param_t	*tf_param;
11233965Sjdp	tf_respond_t	*tf_respond;
11360484Sobrien} teken_funcs_t;
11433965Sjdp
11533965Sjdptypedef teken_char_t teken_scs_t(teken_t *, teken_char_t);
11660484Sobrien
11760484Sobrien/*
11860484Sobrien * Terminal state.
11960484Sobrien */
12060484Sobrien
12160484Sobrienstruct __teken {
12260484Sobrien	const teken_funcs_t *t_funcs;
12360484Sobrien	void		*t_softc;
12460484Sobrien
12560484Sobrien	teken_state_t	*t_nextstate;
12660484Sobrien	unsigned int	 t_stateflags;
12760484Sobrien
12860484Sobrien#define T_NUMSIZE	8
12960484Sobrien	unsigned int	 t_nums[T_NUMSIZE];
13060484Sobrien	unsigned int	 t_curnum;
13160484Sobrien
13260484Sobrien	teken_pos_t	 t_cursor;
13360484Sobrien	teken_attr_t	 t_curattr;
134107492Sobrien	teken_pos_t	 t_saved_cursor;
135107492Sobrien	teken_attr_t	 t_saved_curattr;
136107492Sobrien
137107492Sobrien	teken_attr_t	 t_defattr;
138107492Sobrien	teken_pos_t	 t_winsize;
139107492Sobrien
140107492Sobrien	/* For DECSTBM. */
141107492Sobrien	teken_span_t	 t_scrollreg;
142104834Sobrien	/* For DECOM. */
143107492Sobrien	teken_span_t	 t_originreg;
144107492Sobrien
145107492Sobrien#define	T_NUMCOL	160
146107492Sobrien	unsigned int	 t_tabstops[T_NUMCOL / (sizeof(unsigned int) * 8)];
147107492Sobrien
148107492Sobrien	unsigned int	 t_utf8_left;
149107492Sobrien	teken_char_t	 t_utf8_partial;
150107492Sobrien
151107492Sobrien	unsigned int	 t_curscs;
152107492Sobrien	teken_scs_t	*t_saved_curscs;
153107492Sobrien	teken_scs_t	*t_scs[2];
154107492Sobrien};
155107492Sobrien
156107492Sobrien/* Initialize teken structure. */
157107492Sobrienvoid	teken_init(teken_t *, const teken_funcs_t *, void *);
158107492Sobrien
159107492Sobrien/* Deliver character input. */
160107492Sobrienvoid	teken_input(teken_t *, const void *, size_t);
161107492Sobrien
162107492Sobrien/* Get/set teken attributes. */
163107492Sobrienconst teken_pos_t *teken_get_cursor(teken_t *);
164107492Sobrienconst teken_attr_t *teken_get_curattr(teken_t *);
165107492Sobrienconst teken_attr_t *teken_get_defattr(teken_t *);
166107492Sobrienvoid	teken_get_defattr_cons25(teken_t *, int *, int *);
167107492Sobrienconst teken_pos_t *teken_get_winsize(teken_t *);
168107492Sobrienvoid	teken_set_cursor(teken_t *, const teken_pos_t *);
169107492Sobrienvoid	teken_set_curattr(teken_t *, const teken_attr_t *);
170107492Sobrienvoid	teken_set_defattr(teken_t *, const teken_attr_t *);
171107492Sobrienvoid	teken_set_winsize(teken_t *, const teken_pos_t *);
172107492Sobrienvoid	teken_set_winsize_noreset(teken_t *, const teken_pos_t *);
173107492Sobrien
174107492Sobrien/* Key input escape sequences. */
175107492Sobrien#define	TKEY_UP		0x00
176107492Sobrien#define	TKEY_DOWN	0x01
177107492Sobrien#define	TKEY_LEFT	0x02
178107492Sobrien#define	TKEY_RIGHT	0x03
179107492Sobrien
180107492Sobrien#define	TKEY_HOME	0x04
181107492Sobrien#define	TKEY_END	0x05
182107492Sobrien#define	TKEY_INSERT	0x06
183107492Sobrien#define	TKEY_DELETE	0x07
184107492Sobrien#define	TKEY_PAGE_UP	0x08
185107492Sobrien#define	TKEY_PAGE_DOWN	0x09
186107492Sobrien
187107492Sobrien#define	TKEY_F1		0x0a
188107492Sobrien#define	TKEY_F2		0x0b
189107492Sobrien#define	TKEY_F3		0x0c
190107492Sobrien#define	TKEY_F4		0x0d
191107492Sobrien#define	TKEY_F5		0x0e
192107492Sobrien#define	TKEY_F6		0x0f
193107492Sobrien#define	TKEY_F7		0x10
194107492Sobrien#define	TKEY_F8		0x11
195107492Sobrien#define	TKEY_F9		0x12
196107492Sobrien#define	TKEY_F10	0x13
197107492Sobrien#define	TKEY_F11	0x14
198107492Sobrien#define	TKEY_F12	0x15
199107492Sobrienconst char *teken_get_sequence(teken_t *, unsigned int);
200107492Sobrien
201107492Sobrien/* Legacy features. */
202107492Sobrienvoid	teken_set_8bit(teken_t *);
203107492Sobrienvoid	teken_set_cons25(teken_t *);
204107492Sobrien
205107492Sobrien/* Color conversion. */
206107492Sobrienteken_color_t teken_256to8(teken_color_t);
207107492Sobrien
208107492Sobrien#endif /* !_TEKEN_H_ */
209107492Sobrien