teken.h revision 188391
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
31590Srgrimes * All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes *
141590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241590Srgrimes * SUCH DAMAGE.
251590Srgrimes *
261590Srgrimes * $FreeBSD: head/sys/dev/syscons/teken/teken.h 188391 2009-02-09 15:55:21Z ed $
271590Srgrimes */
281590Srgrimes
291590Srgrimes#ifndef _TEKEN_H_
301590Srgrimes#define	_TEKEN_H_
311590Srgrimes
321590Srgrimes/*
331590Srgrimes * libteken: terminal emulation library.
341590Srgrimes *
351590Srgrimes * This library converts an UTF-8 stream of bytes to terminal drawing
361590Srgrimes * commands.
3774769Smikeh *
3888150Smikeh * Configuration switches:
3974769Smikeh * - TEKEN_UTF8: Enable/disable UTF-8 handling.
401590Srgrimes * - TEKEN_XTERM: Enable xterm-style emulation, instead of cons25.
4199112Sobrien */
4299112Sobrien
431590Srgrimes#if defined(__FreeBSD__) && defined(_KERNEL)
441590Srgrimes#include "opt_teken.h"
451590Srgrimes#endif /* __FreeBSD__ && _KERNEL */
461590Srgrimes
471590Srgrimes#ifdef TEKEN_UTF8
481590Srgrimestypedef uint32_t teken_char_t;
491590Srgrimes#else /* !TEKEN_UTF8 */
501590Srgrimestypedef unsigned char teken_char_t;
511590Srgrimes#endif /* TEKEN_UTF8 */
521590Srgrimestypedef unsigned short teken_unit_t;
531590Srgrimestypedef unsigned char teken_format_t;
54173439Sdds#define	TF_BOLD		0x01
551590Srgrimes#define	TF_UNDERLINE	0x02
5677274Smikeh#define	TF_BLINK	0x04
5777274Smikehtypedef unsigned char teken_color_t;
581590Srgrimes#define	TC_BLACK	0
59216564Scharnier#define	TC_RED		1
601590Srgrimes#define	TC_GREEN	2
6177274Smikeh#define	TC_BROWN	3
621590Srgrimes#define	TC_BLUE		4
6332189Sjoerg#define	TC_MAGENTA	5
6488150Smikeh#define	TC_CYAN		6
651590Srgrimes#define	TC_WHITE	7
661590Srgrimes#define	TC_NCOLORS	8
671590Srgrimes
681590Srgrimestypedef struct {
691590Srgrimes	teken_unit_t	tp_row;
701590Srgrimes	teken_unit_t	tp_col;
711590Srgrimes} teken_pos_t;
721590Srgrimestypedef struct {
7377274Smikeh	teken_pos_t	tr_begin;
741590Srgrimes	teken_pos_t	tr_end;
751590Srgrimes} teken_rect_t;
761590Srgrimestypedef struct {
771590Srgrimes	teken_format_t	ta_format;
781590Srgrimes	teken_color_t	ta_fgcolor;
791590Srgrimes	teken_color_t	ta_bgcolor;
801590Srgrimes} teken_attr_t;
811590Srgrimestypedef struct {
821590Srgrimes	teken_unit_t	ts_begin;
831590Srgrimes	teken_unit_t	ts_end;
8477274Smikeh} teken_span_t;
8577274Smikeh
8677274Smikehtypedef struct __teken teken_t;
8777274Smikeh
8877274Smikehtypedef void teken_state_t(teken_t *, teken_char_t);
8977274Smikeh
90126415Smikeh/*
911590Srgrimes * Drawing routines supplied by the user.
921590Srgrimes */
931590Srgrimes
941590Srgrimestypedef void tf_bell_t(void *);
951590Srgrimestypedef void tf_cursor_t(void *, const teken_pos_t *);
961590Srgrimestypedef void tf_putchar_t(void *, const teken_pos_t *, teken_char_t,
971590Srgrimes    const teken_attr_t *);
9874769Smikehtypedef void tf_fill_t(void *, const teken_rect_t *, teken_char_t,
9977274Smikeh    const teken_attr_t *);
10074769Smikehtypedef void tf_copy_t(void *, const teken_rect_t *, const teken_pos_t *);
10177274Smikehtypedef void tf_param_t(void *, int, int);
1021590Srgrimes#define	TP_SHOWCURSOR	0
1031590Srgrimes#define	TP_CURSORKEYS	1
1041590Srgrimes#define	TP_KEYPADAPP	2
1051590Srgrimes#define	TP_AUTOREPEAT	3
1061590Srgrimes#define	TP_SWITCHVT	4
1071590Srgrimes#define	TP_132COLS	5
10877274Smikehtypedef void tf_respond_t(void *, const void *, size_t);
1091590Srgrimes
1101590Srgrimestypedef struct {
1111590Srgrimes	tf_bell_t	*tf_bell;
1121590Srgrimes	tf_cursor_t	*tf_cursor;
1131590Srgrimes	tf_putchar_t	*tf_putchar;
1141590Srgrimes	tf_fill_t	*tf_fill;
1151590Srgrimes	tf_copy_t	*tf_copy;
1161590Srgrimes	tf_param_t	*tf_param;
1171590Srgrimes	tf_respond_t	*tf_respond;
1181590Srgrimes} teken_funcs_t;
1191590Srgrimes
120126415Smikeh#if defined(TEKEN_XTERM) && defined(TEKEN_UTF8)
121126415Smikehtypedef teken_char_t teken_scs_t(teken_char_t);
122126415Smikeh#endif /* TEKEN_XTERM && TEKEN_UTF8 */
123126415Smikeh
124126415Smikeh/*
125126415Smikeh * Terminal state.
126126415Smikeh */
127126415Smikeh
128126415Smikehstruct __teken {
129126415Smikeh	const teken_funcs_t *t_funcs;
130126415Smikeh	void		*t_softc;
131126415Smikeh
132126415Smikeh	teken_state_t	*t_nextstate;
133126415Smikeh	unsigned int	 t_stateflags;
134126415Smikeh
135126415Smikeh#define T_NUMSIZE	8
136126415Smikeh	unsigned int	 t_nums[T_NUMSIZE];
137126415Smikeh	unsigned int	 t_curnum;
138126415Smikeh
1391590Srgrimes	teken_pos_t	 t_cursor;
1401590Srgrimes	teken_attr_t	 t_curattr;
1411590Srgrimes	teken_pos_t	 t_saved_cursor;
1421590Srgrimes	teken_attr_t	 t_saved_curattr;
1431590Srgrimes
1441590Srgrimes	teken_attr_t	 t_defattr;
1451590Srgrimes	teken_pos_t	 t_winsize;
1461590Srgrimes
1471590Srgrimes	/* For DECSTBM. */
1481590Srgrimes	teken_span_t	 t_scrollreg;
1491590Srgrimes	/* For DECOM. */
1501590Srgrimes	teken_span_t	 t_originreg;
1511590Srgrimes
1521590Srgrimes#define	T_NUMCOL	160
1531590Srgrimes	unsigned int	 t_tabstops[T_NUMCOL / (sizeof(unsigned int) * 8)];
1541590Srgrimes
1551590Srgrimes#ifdef TEKEN_UTF8
15677274Smikeh	unsigned int	 t_utf8_left;
1571590Srgrimes	teken_char_t	 t_utf8_partial;
1581590Srgrimes#endif /* TEKEN_UTF8 */
1591590Srgrimes
1601590Srgrimes#if defined(TEKEN_XTERM) && defined(TEKEN_UTF8)
1611590Srgrimes	unsigned int	 t_curscs;
1621590Srgrimes	teken_scs_t	*t_saved_curscs;
1631590Srgrimes	teken_scs_t	*t_scs[2];
1641590Srgrimes#endif /* TEKEN_XTERM && TEKEN_UTF8 */
1651590Srgrimes};
1661590Srgrimes
1671590Srgrimes/* Initialize teken structure. */
1681590Srgrimesvoid	teken_init(teken_t *, const teken_funcs_t *, void *);
1691590Srgrimes
1701590Srgrimes/* Deliver character input. */
1711590Srgrimesvoid	teken_input(teken_t *, const void *, size_t);
1721590Srgrimes
1731590Srgrimes/* Get/set teken attributes. */
1741590Srgrimesconst teken_attr_t *teken_get_curattr(teken_t *);
1751590Srgrimesconst teken_attr_t *teken_get_defattr(teken_t *);
1761590Srgrimesvoid	teken_set_cursor(teken_t *, const teken_pos_t *);
1771590Srgrimesvoid	teken_set_defattr(teken_t *, const teken_attr_t *);
1781590Srgrimesvoid	teken_set_winsize(teken_t *, const teken_pos_t *);
1791590Srgrimes
1801590Srgrimes#endif /* !_TEKEN_H_ */
1811590Srgrimes