teken.h revision 189617
1206917Smarius/*-
2206917Smarius * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
3206917Smarius * All rights reserved.
4206917Smarius *
5206917Smarius * Redistribution and use in source and binary forms, with or without
6206917Smarius * modification, are permitted provided that the following conditions
7206917Smarius * are met:
8206917Smarius * 1. Redistributions of source code must retain the above copyright
9206917Smarius *    notice, this list of conditions and the following disclaimer.
10206917Smarius * 2. Redistributions in binary form must reproduce the above copyright
11206917Smarius *    notice, this list of conditions and the following disclaimer in the
12206917Smarius *    documentation and/or other materials provided with the distribution.
13206917Smarius *
14206917Smarius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15206917Smarius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16206917Smarius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17206917Smarius * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18206917Smarius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19206917Smarius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20206917Smarius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21206917Smarius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22206917Smarius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23206917Smarius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24206917Smarius * SUCH DAMAGE.
25206917Smarius *
26206917Smarius * $FreeBSD: head/sys/dev/syscons/teken/teken.h 189617 2009-03-10 11:28:54Z ed $
27206917Smarius */
28206917Smarius
29206917Smarius#ifndef _TEKEN_H_
30206917Smarius#define	_TEKEN_H_
31206917Smarius
32206917Smarius/*
33206917Smarius * libteken: terminal emulation library.
34206917Smarius *
35206917Smarius * This library converts an UTF-8 stream of bytes to terminal drawing
36206917Smarius * commands.
37206917Smarius *
38206917Smarius * Configuration switches:
39206917Smarius * - TEKEN_UTF8: Enable/disable UTF-8 handling.
40206917Smarius * - TEKEN_XTERM: Enable xterm-style emulation, instead of cons25.
41206917Smarius */
42206917Smarius
43206917Smarius#if defined(__FreeBSD__) && defined(_KERNEL)
44206917Smarius#include "opt_teken.h"
45206917Smarius#endif /* __FreeBSD__ && _KERNEL */
46206917Smarius
47206917Smarius#ifdef TEKEN_UTF8
48206917Smariustypedef uint32_t teken_char_t;
49206917Smarius#else /* !TEKEN_UTF8 */
50206917Smariustypedef unsigned char teken_char_t;
51206917Smarius#endif /* TEKEN_UTF8 */
52206917Smariustypedef unsigned short teken_unit_t;
53206917Smariustypedef unsigned char teken_format_t;
54206917Smarius#define	TF_BOLD		0x01
55206917Smarius#define	TF_UNDERLINE	0x02
56206917Smarius#define	TF_BLINK	0x04
57206917Smariustypedef unsigned char teken_color_t;
58206917Smarius#define	TC_BLACK	0
59206917Smarius#define	TC_RED		1
60206917Smarius#define	TC_GREEN	2
61206917Smarius#define	TC_BROWN	3
62206917Smarius#define	TC_BLUE		4
63206917Smarius#define	TC_MAGENTA	5
64206917Smarius#define	TC_CYAN		6
65206917Smarius#define	TC_WHITE	7
66206917Smarius#define	TC_NCOLORS	8
67206917Smarius
68206917Smariustypedef struct {
69206917Smarius	teken_unit_t	tp_row;
70206917Smarius	teken_unit_t	tp_col;
71206917Smarius} teken_pos_t;
72206917Smariustypedef struct {
73206917Smarius	teken_pos_t	tr_begin;
74206917Smarius	teken_pos_t	tr_end;
75206917Smarius} teken_rect_t;
76206917Smariustypedef struct {
77206917Smarius	teken_format_t	ta_format;
78206917Smarius	teken_color_t	ta_fgcolor;
79206917Smarius	teken_color_t	ta_bgcolor;
80206917Smarius} teken_attr_t;
81206917Smariustypedef struct {
82206917Smarius	teken_unit_t	ts_begin;
83206917Smarius	teken_unit_t	ts_end;
84206917Smarius} teken_span_t;
85206917Smarius
86206917Smariustypedef struct __teken teken_t;
87206917Smarius
88206917Smariustypedef void teken_state_t(teken_t *, teken_char_t);
89206917Smarius
90206917Smarius/*
91206917Smarius * Drawing routines supplied by the user.
92206917Smarius */
93206917Smarius
94206917Smariustypedef void tf_bell_t(void *);
95206917Smariustypedef void tf_cursor_t(void *, const teken_pos_t *);
96206917Smariustypedef void tf_putchar_t(void *, const teken_pos_t *, teken_char_t,
97206917Smarius    const teken_attr_t *);
98206917Smariustypedef void tf_fill_t(void *, const teken_rect_t *, teken_char_t,
99206917Smarius    const teken_attr_t *);
100206917Smariustypedef void tf_copy_t(void *, const teken_rect_t *, const teken_pos_t *);
101206917Smariustypedef void tf_param_t(void *, int, int);
102206917Smarius#define	TP_SHOWCURSOR	0
103206917Smarius#define	TP_CURSORKEYS	1
104206917Smarius#define	TP_KEYPADAPP	2
105206917Smarius#define	TP_AUTOREPEAT	3
106206917Smarius#define	TP_SWITCHVT	4
107206917Smarius#define	TP_132COLS	5
108206917Smariustypedef void tf_respond_t(void *, const void *, size_t);
109206917Smarius
110206917Smariustypedef struct {
111206917Smarius	tf_bell_t	*tf_bell;
112206917Smarius	tf_cursor_t	*tf_cursor;
113206917Smarius	tf_putchar_t	*tf_putchar;
114206917Smarius	tf_fill_t	*tf_fill;
115206917Smarius	tf_copy_t	*tf_copy;
116206917Smarius	tf_param_t	*tf_param;
117206917Smarius	tf_respond_t	*tf_respond;
118206917Smarius} teken_funcs_t;
119206917Smarius
120206917Smarius#if defined(TEKEN_XTERM) && defined(TEKEN_UTF8)
121206917Smariustypedef teken_char_t teken_scs_t(teken_char_t);
122206917Smarius#endif /* TEKEN_XTERM && TEKEN_UTF8 */
123206917Smarius
124206917Smarius/*
125206917Smarius * Terminal state.
126206917Smarius */
127206917Smarius
128206917Smariusstruct __teken {
129206917Smarius	const teken_funcs_t *t_funcs;
130206917Smarius	void		*t_softc;
131206917Smarius
132206917Smarius	teken_state_t	*t_nextstate;
133206917Smarius	unsigned int	 t_stateflags;
134206917Smarius
135206917Smarius#define T_NUMSIZE	8
136206917Smarius	unsigned int	 t_nums[T_NUMSIZE];
137206917Smarius	unsigned int	 t_curnum;
138206917Smarius
139206917Smarius	teken_pos_t	 t_cursor;
140206917Smarius	teken_attr_t	 t_curattr;
141206917Smarius	teken_pos_t	 t_saved_cursor;
142206917Smarius	teken_attr_t	 t_saved_curattr;
143206917Smarius
144206917Smarius	teken_attr_t	 t_defattr;
145206917Smarius	teken_pos_t	 t_winsize;
146206917Smarius
147206917Smarius	/* For DECSTBM. */
148206917Smarius	teken_span_t	 t_scrollreg;
149206917Smarius	/* For DECOM. */
150206917Smarius	teken_span_t	 t_originreg;
151206917Smarius
152206917Smarius#define	T_NUMCOL	160
153206917Smarius	unsigned int	 t_tabstops[T_NUMCOL / (sizeof(unsigned int) * 8)];
154206917Smarius
155206917Smarius#ifdef TEKEN_UTF8
156206917Smarius	unsigned int	 t_utf8_left;
157206917Smarius	teken_char_t	 t_utf8_partial;
158206917Smarius#endif /* TEKEN_UTF8 */
159206917Smarius
160206917Smarius#if defined(TEKEN_XTERM) && defined(TEKEN_UTF8)
161206917Smarius	unsigned int	 t_curscs;
162206917Smarius	teken_scs_t	*t_saved_curscs;
163206917Smarius	teken_scs_t	*t_scs[2];
164206917Smarius#endif /* TEKEN_XTERM && TEKEN_UTF8 */
165206917Smarius};
166206917Smarius
167206917Smarius/* Initialize teken structure. */
168206917Smariusvoid	teken_init(teken_t *, const teken_funcs_t *, void *);
169206917Smarius
170206917Smarius/* Deliver character input. */
171206917Smariusvoid	teken_input(teken_t *, const void *, size_t);
172206917Smarius
173206917Smarius/* Get/set teken attributes. */
174206917Smariusconst teken_attr_t *teken_get_curattr(teken_t *);
175206917Smariusconst teken_attr_t *teken_get_defattr(teken_t *);
176206917Smariusvoid	teken_set_cursor(teken_t *, const teken_pos_t *);
177206917Smariusvoid	teken_set_curattr(teken_t *, const teken_attr_t *);
178206917Smariusvoid	teken_set_defattr(teken_t *, const teken_attr_t *);
179206917Smariusvoid	teken_set_winsize(teken_t *, const teken_pos_t *);
180206917Smarius
181206917Smarius#endif /* !_TEKEN_H_ */
182206917Smarius