xref: /illumos-gate/usr/src/boot/sys/sys/tem_impl.h (revision d863b4c1)
19890ff83SToomas Soome /*
29890ff83SToomas Soome  * CDDL HEADER START
39890ff83SToomas Soome  *
49890ff83SToomas Soome  * The contents of this file are subject to the terms of the
59890ff83SToomas Soome  * Common Development and Distribution License (the "License").
69890ff83SToomas Soome  * You may not use this file except in compliance with the License.
79890ff83SToomas Soome  *
89890ff83SToomas Soome  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99890ff83SToomas Soome  * or http://www.opensolaris.org/os/licensing.
109890ff83SToomas Soome  * See the License for the specific language governing permissions
119890ff83SToomas Soome  * and limitations under the License.
129890ff83SToomas Soome  *
139890ff83SToomas Soome  * When distributing Covered Code, include this CDDL HEADER in each
149890ff83SToomas Soome  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159890ff83SToomas Soome  * If applicable, add the following below this CDDL HEADER, with the
169890ff83SToomas Soome  * fields enclosed by brackets "[]" replaced with your own identifying
179890ff83SToomas Soome  * information: Portions Copyright [yyyy] [name of copyright owner]
189890ff83SToomas Soome  *
199890ff83SToomas Soome  * CDDL HEADER END
209890ff83SToomas Soome  */
219890ff83SToomas Soome 
229890ff83SToomas Soome /*
239890ff83SToomas Soome  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
249890ff83SToomas Soome  * Use is subject to license terms.
259890ff83SToomas Soome  */
269890ff83SToomas Soome /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
279890ff83SToomas Soome 
289890ff83SToomas Soome /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
299890ff83SToomas Soome /*		All Rights Reserved	*/
309890ff83SToomas Soome 
319890ff83SToomas Soome #ifndef	_SYS_TEM_H
329890ff83SToomas Soome #define	_SYS_TEM_H
339890ff83SToomas Soome 
349890ff83SToomas Soome #ifdef __cplusplus
359890ff83SToomas Soome extern "C" {
369890ff83SToomas Soome #endif
379890ff83SToomas Soome 
389890ff83SToomas Soome #include <sys/types.h>
399890ff83SToomas Soome #include <sys/visual_io.h>
409890ff83SToomas Soome #include <sys/font.h>
419890ff83SToomas Soome #include <sys/list.h>
429890ff83SToomas Soome #include <sys/tem.h>
43cbc8e155SToomas Soome #include <sys/rgb.h>
449890ff83SToomas Soome #include <bootstrap.h>
459890ff83SToomas Soome #include <stdbool.h>
469890ff83SToomas Soome 
479890ff83SToomas Soome /*
489890ff83SToomas Soome  * Definitions for ANSI x3.64 terminal control language parser.
499890ff83SToomas Soome  * With UTF-8 support we use 32-bit value for Unicode codepoints.
509890ff83SToomas Soome  *
519890ff83SToomas Soome  * However, as we only need 21 bits for unicode char, we will use the
529890ff83SToomas Soome  * rest of the bits for attributes, so we can save memory and
539890ff83SToomas Soome  * have combined attribute+char in screen buffer. This will also allow us
549890ff83SToomas Soome  * to keep better track about attributes and apply other optimizations.
559890ff83SToomas Soome  *
569890ff83SToomas Soome  *  Bits  Meaning
579890ff83SToomas Soome  *  0-20  char
589890ff83SToomas Soome  * 21-31  attributes
599890ff83SToomas Soome  */
609890ff83SToomas Soome 
619890ff83SToomas Soome #define	TEM_ATTR_MASK		0x7FF
629890ff83SToomas Soome #define	TEM_CHAR(c)		((c) & 0x1fffff)
639890ff83SToomas Soome #define	TEM_CHAR_ATTR(c)	(((c) >> 21) & TEM_ATTR_MASK)
649890ff83SToomas Soome #define	TEM_ATTR(c)		(((c) & TEM_ATTR_MASK) << 21)
65e0721d5aSToomas Soome #define	TEM_ATTR_ISSET(c, a)	((TEM_CHAR_ATTR(c) & (a)) == (a))
669890ff83SToomas Soome 
675e897995SToomas Soome #define	TEM_MAXPARAMS	32	/* maximum number of ANSI paramters */
689890ff83SToomas Soome #define	TEM_MAXFKEY	30	/* max length of function key with <ESC>Q */
699890ff83SToomas Soome #define	MAX_TEM		2	/* max number of loadable terminal emulators */
709890ff83SToomas Soome 
719890ff83SToomas Soome #define	TEM_SCROLL_UP		0
729890ff83SToomas Soome #define	TEM_SCROLL_DOWN		1
739890ff83SToomas Soome #define	TEM_SHIFT_LEFT		0
749890ff83SToomas Soome #define	TEM_SHIFT_RIGHT		1
759890ff83SToomas Soome 
769890ff83SToomas Soome /* Attributes 0-0x7ff */
779890ff83SToomas Soome #define	TEM_ATTR_NORMAL		0x0000
789890ff83SToomas Soome #define	TEM_ATTR_REVERSE	0x0001
799890ff83SToomas Soome #define	TEM_ATTR_BOLD		0x0002
809890ff83SToomas Soome #define	TEM_ATTR_BLINK		0x0004
819890ff83SToomas Soome #define	TEM_ATTR_UNDERLINE	0x0008
829890ff83SToomas Soome #define	TEM_ATTR_SCREEN_REVERSE	0x0010
839890ff83SToomas Soome #define	TEM_ATTR_BRIGHT_FG	0x0020
849890ff83SToomas Soome #define	TEM_ATTR_BRIGHT_BG	0x0040
859890ff83SToomas Soome #define	TEM_ATTR_TRANSPARENT	0x0080
869890ff83SToomas Soome #define	TEM_ATTR_IMAGE		0x0100
87fa9eb222SToomas Soome #define	TEM_ATTR_RGB_FG		0x0200
88fa9eb222SToomas Soome #define	TEM_ATTR_RGB_BG		0x0400
899890ff83SToomas Soome 
909890ff83SToomas Soome #define	ANSI_COLOR_BLACK	0
919890ff83SToomas Soome #define	ANSI_COLOR_RED		1
929890ff83SToomas Soome #define	ANSI_COLOR_GREEN	2
939890ff83SToomas Soome #define	ANSI_COLOR_YELLOW	3
949890ff83SToomas Soome #define	ANSI_COLOR_BLUE		4
959890ff83SToomas Soome #define	ANSI_COLOR_MAGENTA	5
969890ff83SToomas Soome #define	ANSI_COLOR_CYAN		6
979890ff83SToomas Soome #define	ANSI_COLOR_WHITE	7
989890ff83SToomas Soome 
999890ff83SToomas Soome #define	TEM_TEXT_WHITE		0
1009890ff83SToomas Soome #define	TEM_TEXT_BLACK		1
1019890ff83SToomas Soome #define	TEM_TEXT_BLACK24_RED	0x00
1029890ff83SToomas Soome #define	TEM_TEXT_BLACK24_GREEN	0x00
1039890ff83SToomas Soome #define	TEM_TEXT_BLACK24_BLUE	0x00
1049890ff83SToomas Soome #define	TEM_TEXT_WHITE24_RED	0xff
1059890ff83SToomas Soome #define	TEM_TEXT_WHITE24_GREEN	0xff
1069890ff83SToomas Soome #define	TEM_TEXT_WHITE24_BLUE	0xff
1079890ff83SToomas Soome 
1089890ff83SToomas Soome #define	A_STATE_START			0
1099890ff83SToomas Soome #define	A_STATE_ESC			1
1109890ff83SToomas Soome #define	A_STATE_CSI			2
1119890ff83SToomas Soome #define	A_STATE_CSI_QMARK		3
1129890ff83SToomas Soome #define	A_STATE_CSI_EQUAL		4
1139890ff83SToomas Soome 
1149890ff83SToomas Soome /*
1159890ff83SToomas Soome  * Default number of rows and columns
1169890ff83SToomas Soome  */
1179890ff83SToomas Soome #define	TEM_DEFAULT_ROWS	25
1189890ff83SToomas Soome #define	TEM_DEFAULT_COLS	80
1199890ff83SToomas Soome 
1209890ff83SToomas Soome /*
1219890ff83SToomas Soome  * Default foreground/background color
1229890ff83SToomas Soome  */
1239890ff83SToomas Soome 
1243c562093SToomas Soome #if !defined(DEFAULT_ANSI_FOREGROUND) && !defined(DEFAULT_ANSI_BACKGROUND)
1259890ff83SToomas Soome #define	DEFAULT_ANSI_FOREGROUND	ANSI_COLOR_BLACK
1269890ff83SToomas Soome #define	DEFAULT_ANSI_BACKGROUND	ANSI_COLOR_WHITE
1273c562093SToomas Soome #endif
1289890ff83SToomas Soome 
1299890ff83SToomas Soome 
1309890ff83SToomas Soome #define	BUF_LEN		160 /* Two lines of data can be processed at a time */
1319890ff83SToomas Soome 
132fa9eb222SToomas Soome typedef uint32_t tem_char_t;	/* 32bit char to support UTF-8 */
133fa9eb222SToomas Soome typedef union {
134fa9eb222SToomas Soome 	uint32_t n;
135fa9eb222SToomas Soome 	struct bgra {
136fa9eb222SToomas Soome 		uint8_t b;
137fa9eb222SToomas Soome 		uint8_t g;
138fa9eb222SToomas Soome 		uint8_t r;
139fa9eb222SToomas Soome 		uint8_t a;
140fa9eb222SToomas Soome 	} rgb;
141fa9eb222SToomas Soome } text_color_t;
1429890ff83SToomas Soome typedef uint16_t text_attr_t;
1439890ff83SToomas Soome 
1449890ff83SToomas Soome typedef struct tem_color {
1459890ff83SToomas Soome 	text_color_t	fg_color;
1469890ff83SToomas Soome 	text_color_t	bg_color;
1479890ff83SToomas Soome 	text_attr_t	a_flags;
1489890ff83SToomas Soome } tem_color_t;
1499890ff83SToomas Soome 
1509890ff83SToomas Soome struct tem_pix_pos {
1519890ff83SToomas Soome 	screen_pos_t	x;
1529890ff83SToomas Soome 	screen_pos_t	y;
1539890ff83SToomas Soome };
1549890ff83SToomas Soome 
1559890ff83SToomas Soome struct tem_char_pos {
1569890ff83SToomas Soome 	screen_pos_t	col;
1579890ff83SToomas Soome 	screen_pos_t	row;
1589890ff83SToomas Soome };
1599890ff83SToomas Soome 
1609890ff83SToomas Soome struct tem_size {
1619890ff83SToomas Soome 	screen_size_t	width;
1629890ff83SToomas Soome 	screen_size_t	height;
1639890ff83SToomas Soome };
1649890ff83SToomas Soome 
1659890ff83SToomas Soome /* Combined color and 32bit tem char */
1669890ff83SToomas Soome typedef struct term_char {
1679890ff83SToomas Soome 	text_color_t	tc_fg_color;
1689890ff83SToomas Soome 	text_color_t	tc_bg_color;
1699890ff83SToomas Soome 	tem_char_t	tc_char;
1709890ff83SToomas Soome } term_char_t;
1719890ff83SToomas Soome 
172*d863b4c1SToomas Soome /*
173*d863b4c1SToomas Soome  * Values for tvs_stateflags.
174*d863b4c1SToomas Soome  */
175*d863b4c1SToomas Soome #define	TVS_AUTOWRAP	0x00000001	/* autowrap is on by default */
176*d863b4c1SToomas Soome #define	TVS_WRAPPED	0x00000002	/* line wrap in progress */
177*d863b4c1SToomas Soome 
1789890ff83SToomas Soome /*
1799890ff83SToomas Soome  * State structure for each virtual terminal emulator
1809890ff83SToomas Soome  */
1819890ff83SToomas Soome struct tem_vt_state {
1829890ff83SToomas Soome 	uint8_t		tvs_fbmode;	/* framebuffer mode */
183fa9eb222SToomas Soome 	uint8_t		tvs_alpha;	/* rgb alpha channel */
1849890ff83SToomas Soome 	text_attr_t	tvs_flags;	/* flags for this x3.64 terminal */
1859890ff83SToomas Soome 	int		tvs_state;	/* state in output esc seq processing */
186*d863b4c1SToomas Soome 	uint_t		tvs_stateflags; /* state of some features */
1879890ff83SToomas Soome 	bool		tvs_gotparam;	/* does output esc seq have a param */
1889890ff83SToomas Soome 
1899890ff83SToomas Soome 	int	tvs_curparam;	/* current param # of output esc seq */
1909890ff83SToomas Soome 	int	tvs_paramval;	/* value of current param */
1919890ff83SToomas Soome 	int	tvs_params[TEM_MAXPARAMS];  /* parameters of output esc seq */
192ba2848d4SToomas Soome 	screen_pos_t	*tvs_tabs;	/* tab stops */
193ba2848d4SToomas Soome 	size_t	tvs_maxtab;		/* maximum number of tab stops */
194ba2848d4SToomas Soome 	size_t	tvs_ntabs;		/* number of tabs used */
1959890ff83SToomas Soome 	int	tvs_nscroll;		/* number of lines to scroll */
1969890ff83SToomas Soome 
1979890ff83SToomas Soome 	struct tem_char_pos tvs_s_cursor;	/* start cursor position */
1989890ff83SToomas Soome 	struct tem_char_pos tvs_c_cursor;	/* current cursor position */
1999890ff83SToomas Soome 	struct tem_char_pos tvs_r_cursor;	/* remembered cursor position */
2009890ff83SToomas Soome 
2019890ff83SToomas Soome 	term_char_t	*tvs_outbuf;	/* place to keep incomplete lines */
2029890ff83SToomas Soome 	int		tvs_outindex;	/* index into a_outbuf */
2039890ff83SToomas Soome 	void		*tvs_pix_data;	/* pointer to tmp bitmap area */
2042d84dc94SToomas Soome 	unsigned	tvs_pix_data_size;
205fa9eb222SToomas Soome 
206fa9eb222SToomas Soome 	text_color_t	tvs_fg_color;	/* console foreground */
207fa9eb222SToomas Soome 	text_color_t	tvs_bg_color;	/* console background */
208fa9eb222SToomas Soome 
2099890ff83SToomas Soome 	int		tvs_first_line;	/* kernel console output begins */
2109890ff83SToomas Soome 
2119890ff83SToomas Soome 	term_char_t	*tvs_screen_buf;	/* whole screen buffer */
2129890ff83SToomas Soome 	unsigned	tvs_utf8_left;		/* UTF-8 code points */
2139890ff83SToomas Soome 	tem_char_t	tvs_utf8_partial;	/* UTF-8 char being completed */
2149890ff83SToomas Soome 
2159890ff83SToomas Soome 	bool		tvs_isactive;
2169890ff83SToomas Soome 	bool		tvs_initialized;	/* initialization flag */
2175e897995SToomas Soome 	bool		tvs_cursor_hidden;
2189890ff83SToomas Soome 
2199890ff83SToomas Soome 	list_node_t	tvs_list_node;
2209890ff83SToomas Soome };
2219890ff83SToomas Soome 
2229890ff83SToomas Soome typedef struct tem_callbacks {
2239890ff83SToomas Soome 	void (*tsc_display)(struct tem_vt_state *, term_char_t *, int,
2249890ff83SToomas Soome 	    screen_pos_t, screen_pos_t);
2259890ff83SToomas Soome 	void (*tsc_copy)(struct tem_vt_state *,
2269890ff83SToomas Soome 	    screen_pos_t, screen_pos_t, screen_pos_t, screen_pos_t,
2279890ff83SToomas Soome 	    screen_pos_t, screen_pos_t);
2289890ff83SToomas Soome 	void (*tsc_cursor)(struct tem_vt_state *, short);
229fa9eb222SToomas Soome 	void (*tsc_bit2pix)(struct tem_vt_state *, term_char_t *);
2309890ff83SToomas Soome 	void (*tsc_cls)(struct tem_vt_state *, int, screen_pos_t, screen_pos_t);
2319890ff83SToomas Soome } tem_callbacks_t;
2329890ff83SToomas Soome 
2339890ff83SToomas Soome typedef struct __tem_modechg_cb_arg *tem_modechg_cb_arg_t;
2349890ff83SToomas Soome typedef void (*tem_modechg_cb_t) (tem_modechg_cb_arg_t arg);
2359890ff83SToomas Soome typedef	struct __tem_vt_state *tem_vt_state_t;
2369890ff83SToomas Soome 
2379890ff83SToomas Soome /*
2389890ff83SToomas Soome  * common term soft state structure shared by all virtual terminal emulators
2399890ff83SToomas Soome  */
2409890ff83SToomas Soome typedef struct tem_state {
2419890ff83SToomas Soome 	struct console	*ts_hdl;	/* Framework handle for dev */
2429890ff83SToomas Soome 	screen_size_t	ts_linebytes;	/* Layered on bytes per scan line */
2439890ff83SToomas Soome 
2449890ff83SToomas Soome 	int	ts_display_mode;	/* What mode we are in */
2459890ff83SToomas Soome 
2469890ff83SToomas Soome 	struct tem_size ts_c_dimension;	/* window dimensions in characters */
2479890ff83SToomas Soome 	struct tem_size ts_p_dimension;	/* screen dimensions in pixels */
2489890ff83SToomas Soome 	struct tem_pix_pos ts_p_offset;	/* pix offset to center the display */
2499890ff83SToomas Soome 
2502d84dc94SToomas Soome 	unsigned ts_pix_data_size;	/* size of bitmap data areas */
2519890ff83SToomas Soome 	int	ts_pdepth;		/* pixel depth */
2529890ff83SToomas Soome 	struct font	ts_font;	/* font table */
2539890ff83SToomas Soome 	bool	update_font;		/* flag the font change */
2549890ff83SToomas Soome 
2559890ff83SToomas Soome 	tem_callbacks_t	*ts_callbacks;	/* internal output functions */
2569890ff83SToomas Soome 
2579890ff83SToomas Soome 	int	ts_initialized;		/* initialization flag */
2589890ff83SToomas Soome 
2599890ff83SToomas Soome 	tem_modechg_cb_t	ts_modechg_cb;
2609890ff83SToomas Soome 	tem_modechg_cb_arg_t	ts_modechg_arg;
2619890ff83SToomas Soome 
2629890ff83SToomas Soome 	color_map_fn_t	ts_color_map;
2639890ff83SToomas Soome 
2649890ff83SToomas Soome 	tem_color_t	ts_init_color; /* initial color and attributes */
2659890ff83SToomas Soome 
2669890ff83SToomas Soome 	struct tem_vt_state	*ts_active;
2679890ff83SToomas Soome 	list_t		ts_list;	/* chain of all tems */
2689890ff83SToomas Soome } tem_state_t;
2699890ff83SToomas Soome 
2709890ff83SToomas Soome extern tem_state_t tems;
2719890ff83SToomas Soome /*
2729890ff83SToomas Soome  * tems_* fuctions mean that they just operate on the common soft state
2739890ff83SToomas Soome  * (tem_state_t), and tem_* functions mean that they operate on the
2749890ff83SToomas Soome  * per-tem structure (tem_vt_state). All "safe" interfaces are in tem_safe.c.
2759890ff83SToomas Soome  */
2769890ff83SToomas Soome int	tems_cls(struct vis_consclear *);
2779890ff83SToomas Soome void	tems_display(struct vis_consdisplay *);
2789890ff83SToomas Soome void	tems_copy(struct vis_conscopy *);
2799890ff83SToomas Soome void	tems_cursor(struct vis_conscursor *);
2809890ff83SToomas Soome 
2819890ff83SToomas Soome int	tem_initialized(tem_vt_state_t);
2829890ff83SToomas Soome 
2839890ff83SToomas Soome tem_vt_state_t tem_init(void);
2849890ff83SToomas Soome 
2859890ff83SToomas Soome int	tem_info_init(struct console *);
2869890ff83SToomas Soome void	tem_write(tem_vt_state_t, uint8_t *, ssize_t);
2879890ff83SToomas Soome void	tem_get_size(uint16_t *, uint16_t *, uint16_t *, uint16_t *);
2889890ff83SToomas Soome void	tem_save_state(void);
2899890ff83SToomas Soome void	tem_register_modechg_cb(tem_modechg_cb_t, tem_modechg_cb_arg_t);
290ffd79eb6SToomas Soome void	tem_activate(tem_vt_state_t, bool);
2919890ff83SToomas Soome void	tem_get_colors(tem_vt_state_t, text_color_t *, text_color_t *);
2929890ff83SToomas Soome void	tem_image_display(struct tem_vt_state *, screen_pos_t, screen_pos_t,
2939890ff83SToomas Soome 	screen_pos_t, screen_pos_t);
2949890ff83SToomas Soome 
2959890ff83SToomas Soome #ifdef __cplusplus
2969890ff83SToomas Soome }
2979890ff83SToomas Soome #endif
2989890ff83SToomas Soome 
2999890ff83SToomas Soome #endif /* _SYS_TEM_H */
300