xref: /illumos-gate/usr/src/boot/sys/sys/tem_impl.h (revision d863b4c1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
29 /*		All Rights Reserved	*/
30 
31 #ifndef	_SYS_TEM_H
32 #define	_SYS_TEM_H
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #include <sys/types.h>
39 #include <sys/visual_io.h>
40 #include <sys/font.h>
41 #include <sys/list.h>
42 #include <sys/tem.h>
43 #include <sys/rgb.h>
44 #include <bootstrap.h>
45 #include <stdbool.h>
46 
47 /*
48  * Definitions for ANSI x3.64 terminal control language parser.
49  * With UTF-8 support we use 32-bit value for Unicode codepoints.
50  *
51  * However, as we only need 21 bits for unicode char, we will use the
52  * rest of the bits for attributes, so we can save memory and
53  * have combined attribute+char in screen buffer. This will also allow us
54  * to keep better track about attributes and apply other optimizations.
55  *
56  *  Bits  Meaning
57  *  0-20  char
58  * 21-31  attributes
59  */
60 
61 #define	TEM_ATTR_MASK		0x7FF
62 #define	TEM_CHAR(c)		((c) & 0x1fffff)
63 #define	TEM_CHAR_ATTR(c)	(((c) >> 21) & TEM_ATTR_MASK)
64 #define	TEM_ATTR(c)		(((c) & TEM_ATTR_MASK) << 21)
65 #define	TEM_ATTR_ISSET(c, a)	((TEM_CHAR_ATTR(c) & (a)) == (a))
66 
67 #define	TEM_MAXPARAMS	32	/* maximum number of ANSI paramters */
68 #define	TEM_MAXFKEY	30	/* max length of function key with <ESC>Q */
69 #define	MAX_TEM		2	/* max number of loadable terminal emulators */
70 
71 #define	TEM_SCROLL_UP		0
72 #define	TEM_SCROLL_DOWN		1
73 #define	TEM_SHIFT_LEFT		0
74 #define	TEM_SHIFT_RIGHT		1
75 
76 /* Attributes 0-0x7ff */
77 #define	TEM_ATTR_NORMAL		0x0000
78 #define	TEM_ATTR_REVERSE	0x0001
79 #define	TEM_ATTR_BOLD		0x0002
80 #define	TEM_ATTR_BLINK		0x0004
81 #define	TEM_ATTR_UNDERLINE	0x0008
82 #define	TEM_ATTR_SCREEN_REVERSE	0x0010
83 #define	TEM_ATTR_BRIGHT_FG	0x0020
84 #define	TEM_ATTR_BRIGHT_BG	0x0040
85 #define	TEM_ATTR_TRANSPARENT	0x0080
86 #define	TEM_ATTR_IMAGE		0x0100
87 #define	TEM_ATTR_RGB_FG		0x0200
88 #define	TEM_ATTR_RGB_BG		0x0400
89 
90 #define	ANSI_COLOR_BLACK	0
91 #define	ANSI_COLOR_RED		1
92 #define	ANSI_COLOR_GREEN	2
93 #define	ANSI_COLOR_YELLOW	3
94 #define	ANSI_COLOR_BLUE		4
95 #define	ANSI_COLOR_MAGENTA	5
96 #define	ANSI_COLOR_CYAN		6
97 #define	ANSI_COLOR_WHITE	7
98 
99 #define	TEM_TEXT_WHITE		0
100 #define	TEM_TEXT_BLACK		1
101 #define	TEM_TEXT_BLACK24_RED	0x00
102 #define	TEM_TEXT_BLACK24_GREEN	0x00
103 #define	TEM_TEXT_BLACK24_BLUE	0x00
104 #define	TEM_TEXT_WHITE24_RED	0xff
105 #define	TEM_TEXT_WHITE24_GREEN	0xff
106 #define	TEM_TEXT_WHITE24_BLUE	0xff
107 
108 #define	A_STATE_START			0
109 #define	A_STATE_ESC			1
110 #define	A_STATE_CSI			2
111 #define	A_STATE_CSI_QMARK		3
112 #define	A_STATE_CSI_EQUAL		4
113 
114 /*
115  * Default number of rows and columns
116  */
117 #define	TEM_DEFAULT_ROWS	25
118 #define	TEM_DEFAULT_COLS	80
119 
120 /*
121  * Default foreground/background color
122  */
123 
124 #if !defined(DEFAULT_ANSI_FOREGROUND) && !defined(DEFAULT_ANSI_BACKGROUND)
125 #define	DEFAULT_ANSI_FOREGROUND	ANSI_COLOR_BLACK
126 #define	DEFAULT_ANSI_BACKGROUND	ANSI_COLOR_WHITE
127 #endif
128 
129 
130 #define	BUF_LEN		160 /* Two lines of data can be processed at a time */
131 
132 typedef uint32_t tem_char_t;	/* 32bit char to support UTF-8 */
133 typedef union {
134 	uint32_t n;
135 	struct bgra {
136 		uint8_t b;
137 		uint8_t g;
138 		uint8_t r;
139 		uint8_t a;
140 	} rgb;
141 } text_color_t;
142 typedef uint16_t text_attr_t;
143 
144 typedef struct tem_color {
145 	text_color_t	fg_color;
146 	text_color_t	bg_color;
147 	text_attr_t	a_flags;
148 } tem_color_t;
149 
150 struct tem_pix_pos {
151 	screen_pos_t	x;
152 	screen_pos_t	y;
153 };
154 
155 struct tem_char_pos {
156 	screen_pos_t	col;
157 	screen_pos_t	row;
158 };
159 
160 struct tem_size {
161 	screen_size_t	width;
162 	screen_size_t	height;
163 };
164 
165 /* Combined color and 32bit tem char */
166 typedef struct term_char {
167 	text_color_t	tc_fg_color;
168 	text_color_t	tc_bg_color;
169 	tem_char_t	tc_char;
170 } term_char_t;
171 
172 /*
173  * Values for tvs_stateflags.
174  */
175 #define	TVS_AUTOWRAP	0x00000001	/* autowrap is on by default */
176 #define	TVS_WRAPPED	0x00000002	/* line wrap in progress */
177 
178 /*
179  * State structure for each virtual terminal emulator
180  */
181 struct tem_vt_state {
182 	uint8_t		tvs_fbmode;	/* framebuffer mode */
183 	uint8_t		tvs_alpha;	/* rgb alpha channel */
184 	text_attr_t	tvs_flags;	/* flags for this x3.64 terminal */
185 	int		tvs_state;	/* state in output esc seq processing */
186 	uint_t		tvs_stateflags; /* state of some features */
187 	bool		tvs_gotparam;	/* does output esc seq have a param */
188 
189 	int	tvs_curparam;	/* current param # of output esc seq */
190 	int	tvs_paramval;	/* value of current param */
191 	int	tvs_params[TEM_MAXPARAMS];  /* parameters of output esc seq */
192 	screen_pos_t	*tvs_tabs;	/* tab stops */
193 	size_t	tvs_maxtab;		/* maximum number of tab stops */
194 	size_t	tvs_ntabs;		/* number of tabs used */
195 	int	tvs_nscroll;		/* number of lines to scroll */
196 
197 	struct tem_char_pos tvs_s_cursor;	/* start cursor position */
198 	struct tem_char_pos tvs_c_cursor;	/* current cursor position */
199 	struct tem_char_pos tvs_r_cursor;	/* remembered cursor position */
200 
201 	term_char_t	*tvs_outbuf;	/* place to keep incomplete lines */
202 	int		tvs_outindex;	/* index into a_outbuf */
203 	void		*tvs_pix_data;	/* pointer to tmp bitmap area */
204 	unsigned	tvs_pix_data_size;
205 
206 	text_color_t	tvs_fg_color;	/* console foreground */
207 	text_color_t	tvs_bg_color;	/* console background */
208 
209 	int		tvs_first_line;	/* kernel console output begins */
210 
211 	term_char_t	*tvs_screen_buf;	/* whole screen buffer */
212 	unsigned	tvs_utf8_left;		/* UTF-8 code points */
213 	tem_char_t	tvs_utf8_partial;	/* UTF-8 char being completed */
214 
215 	bool		tvs_isactive;
216 	bool		tvs_initialized;	/* initialization flag */
217 	bool		tvs_cursor_hidden;
218 
219 	list_node_t	tvs_list_node;
220 };
221 
222 typedef struct tem_callbacks {
223 	void (*tsc_display)(struct tem_vt_state *, term_char_t *, int,
224 	    screen_pos_t, screen_pos_t);
225 	void (*tsc_copy)(struct tem_vt_state *,
226 	    screen_pos_t, screen_pos_t, screen_pos_t, screen_pos_t,
227 	    screen_pos_t, screen_pos_t);
228 	void (*tsc_cursor)(struct tem_vt_state *, short);
229 	void (*tsc_bit2pix)(struct tem_vt_state *, term_char_t *);
230 	void (*tsc_cls)(struct tem_vt_state *, int, screen_pos_t, screen_pos_t);
231 } tem_callbacks_t;
232 
233 typedef struct __tem_modechg_cb_arg *tem_modechg_cb_arg_t;
234 typedef void (*tem_modechg_cb_t) (tem_modechg_cb_arg_t arg);
235 typedef	struct __tem_vt_state *tem_vt_state_t;
236 
237 /*
238  * common term soft state structure shared by all virtual terminal emulators
239  */
240 typedef struct tem_state {
241 	struct console	*ts_hdl;	/* Framework handle for dev */
242 	screen_size_t	ts_linebytes;	/* Layered on bytes per scan line */
243 
244 	int	ts_display_mode;	/* What mode we are in */
245 
246 	struct tem_size ts_c_dimension;	/* window dimensions in characters */
247 	struct tem_size ts_p_dimension;	/* screen dimensions in pixels */
248 	struct tem_pix_pos ts_p_offset;	/* pix offset to center the display */
249 
250 	unsigned ts_pix_data_size;	/* size of bitmap data areas */
251 	int	ts_pdepth;		/* pixel depth */
252 	struct font	ts_font;	/* font table */
253 	bool	update_font;		/* flag the font change */
254 
255 	tem_callbacks_t	*ts_callbacks;	/* internal output functions */
256 
257 	int	ts_initialized;		/* initialization flag */
258 
259 	tem_modechg_cb_t	ts_modechg_cb;
260 	tem_modechg_cb_arg_t	ts_modechg_arg;
261 
262 	color_map_fn_t	ts_color_map;
263 
264 	tem_color_t	ts_init_color; /* initial color and attributes */
265 
266 	struct tem_vt_state	*ts_active;
267 	list_t		ts_list;	/* chain of all tems */
268 } tem_state_t;
269 
270 extern tem_state_t tems;
271 /*
272  * tems_* fuctions mean that they just operate on the common soft state
273  * (tem_state_t), and tem_* functions mean that they operate on the
274  * per-tem structure (tem_vt_state). All "safe" interfaces are in tem_safe.c.
275  */
276 int	tems_cls(struct vis_consclear *);
277 void	tems_display(struct vis_consdisplay *);
278 void	tems_copy(struct vis_conscopy *);
279 void	tems_cursor(struct vis_conscursor *);
280 
281 int	tem_initialized(tem_vt_state_t);
282 
283 tem_vt_state_t tem_init(void);
284 
285 int	tem_info_init(struct console *);
286 void	tem_write(tem_vt_state_t, uint8_t *, ssize_t);
287 void	tem_get_size(uint16_t *, uint16_t *, uint16_t *, uint16_t *);
288 void	tem_save_state(void);
289 void	tem_register_modechg_cb(tem_modechg_cb_t, tem_modechg_cb_arg_t);
290 void	tem_activate(tem_vt_state_t, bool);
291 void	tem_get_colors(tem_vt_state_t, text_color_t *, text_color_t *);
292 void	tem_image_display(struct tem_vt_state *, screen_pos_t, screen_pos_t,
293 	screen_pos_t, screen_pos_t);
294 
295 #ifdef __cplusplus
296 }
297 #endif
298 
299 #endif /* _SYS_TEM_H */
300