xref: /illumos-gate/usr/src/uts/common/sys/tem_impl.h (revision dd72704bd9e794056c558153663c739e2012d721)
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 
27 /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
28 
29 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
30 /*	  All Rights Reserved	*/
31 
32 /*
33  * Copyright 2021 RackTop Systems, Inc.
34  */
35 
36 #ifndef	_SYS_TEM_IMPL_H
37 #define	_SYS_TEM_IMPL_H
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 #include <sys/types.h>
44 #include <sys/font.h>
45 #include <sys/rgb.h>
46 #if !defined(_BOOT)
47 #include <sys/sunddi.h>
48 #include <sys/sunldi.h>
49 #include <sys/visual_io.h>
50 #include <sys/list.h>
51 #include <sys/tem.h>
52 #include <sys/note.h>
53 #endif
54 
55 /*
56  * Definitions for ANSI x3.64 terminal control language parser.
57  * With UTF-8 support we use 32-bit value for Unicode codepoints.
58  *
59  * However, as we only need 21 bits for unicode char, we will use the
60  * rest of the bits for attributes, so we can save memory and
61  * have combined attribute+char in screen buffer. This will also allow us
62  * to keep better track about attributes and apply other optimizations.
63  *
64  * This setup will give us 11 bits for attributes (mask 0x7FF).
65  *  Bits  Meaning
66  *  0-20  char
67  * 21-31  attributes
68  *
69  * The current implementation is building the screen buffer in three parts,
70  * tvs_screen_buf is implementing the character array and the foreground
71  * and the background colors have tvs_fg_color and tvs_bg_color arrays.
72  * The character and color arrays are currently only used to restore the
73  * screen from tem switch (vt switch, or switch from Xorg session).
74  * To implement the console history, this buffering needs to be reviewed.
75  */
76 
77 #define	TEM_ATTR_MASK		0x7FF
78 #define	TEM_CHAR(c)		((c) & 0x1fffff)
79 #define	TEM_CHAR_ATTR(c)	(((c) >> 21) & TEM_ATTR_MASK)
80 #define	TEM_ATTR(c)		(((c) & TEM_ATTR_MASK) << 21)
81 #define	TEM_ATTR_ISSET(c, a)	((TEM_CHAR_ATTR(c) & (a)) == (a))
82 
83 #define	TEM_MAXPARAMS	32	/* maximum number of ANSI paramters */
84 #define	TEM_MAXFKEY	30	/* max length of function key with <ESC>Q */
85 
86 #define	TEM_SCROLL_UP		0
87 #define	TEM_SCROLL_DOWN		1
88 #define	TEM_SHIFT_LEFT		0
89 #define	TEM_SHIFT_RIGHT		1
90 
91 /* Attributes 0-0x7ff */
92 #define	TEM_ATTR_NORMAL		0x0000
93 #define	TEM_ATTR_REVERSE	0x0001
94 #define	TEM_ATTR_BOLD		0x0002
95 #define	TEM_ATTR_BLINK		0x0004
96 #define	TEM_ATTR_UNDERLINE	0x0008
97 #define	TEM_ATTR_SCREEN_REVERSE	0x0010
98 #define	TEM_ATTR_BRIGHT_FG	0x0020
99 #define	TEM_ATTR_BRIGHT_BG	0x0040
100 #define	TEM_ATTR_TRANSPARENT	0x0080
101 #define	TEM_ATTR_IMAGE		0x0100
102 #define	TEM_ATTR_RGB_FG		0x0200
103 #define	TEM_ATTR_RGB_BG		0x0400
104 
105 #define	ANSI_COLOR_BLACK	0
106 #define	ANSI_COLOR_RED		1
107 #define	ANSI_COLOR_GREEN	2
108 #define	ANSI_COLOR_BROWN	3
109 #define	ANSI_COLOR_BLUE		4
110 #define	ANSI_COLOR_MAGENTA	5
111 #define	ANSI_COLOR_CYAN		6
112 #define	ANSI_COLOR_WHITE	7
113 
114 #define	TEM_TEXT_WHITE		0
115 #define	TEM_TEXT_BLACK		1
116 #define	TEM_TEXT_BLACK24_RED	0x00
117 #define	TEM_TEXT_BLACK24_GREEN	0x00
118 #define	TEM_TEXT_BLACK24_BLUE	0x00
119 #define	TEM_TEXT_WHITE24_RED	0xff
120 #define	TEM_TEXT_WHITE24_GREEN	0xff
121 #define	TEM_TEXT_WHITE24_BLUE	0xff
122 
123 #define	A_STATE_START			0
124 #define	A_STATE_ESC			1
125 #define	A_STATE_CSI			2
126 #define	A_STATE_CSI_QMARK		3
127 #define	A_STATE_CSI_EQUAL		4
128 
129 /*
130  * Default number of rows and columns
131  */
132 #ifdef _HAVE_TEM_FIRMWARE
133 #define	TEM_DEFAULT_ROWS	34
134 #define	TEM_DEFAULT_COLS	80
135 #else
136 #define	TEM_DEFAULT_ROWS	25
137 #define	TEM_DEFAULT_COLS	80
138 #endif
139 
140 /*
141  * Default foreground/background color
142  */
143 
144 #define	DEFAULT_ANSI_FOREGROUND	ANSI_COLOR_BLACK
145 #define	DEFAULT_ANSI_BACKGROUND	ANSI_COLOR_WHITE
146 
147 typedef uint32_t tem_char_t;	/* 32bit char to support UTF-8 */
148 typedef union {
149 	uint32_t n;
150 	struct bgra {
151 		uint8_t b;
152 		uint8_t g;
153 		uint8_t r;
154 		uint8_t a;
155 	} rgb;
156 } text_color_t;
157 typedef uint16_t text_attr_t;
158 
159 #if !defined(_BOOT)
160 typedef struct tem_color {
161 	text_color_t	fg_color;
162 	text_color_t	bg_color;
163 	text_attr_t	a_flags;
164 } tem_color_t;
165 
166 enum called_from { CALLED_FROM_NORMAL, CALLED_FROM_STANDALONE };
167 
168 struct tem_pix_pos {
169 	screen_pos_t	x;
170 	screen_pos_t	y;
171 };
172 
173 struct tem_char_pos {
174 	screen_pos_t	col;
175 	screen_pos_t	row;
176 };
177 
178 struct tem_size {
179 	screen_size_t	width;
180 	screen_size_t	height;
181 };
182 
183 /* Combined color and 32bit tem char */
184 typedef struct term_char {
185 	text_color_t	tc_fg_color;
186 	text_color_t	tc_bg_color;
187 	tem_char_t	tc_char;
188 } term_char_t;
189 
190 /*
191  * State structure for each virtual terminal emulator
192  */
193 struct tem_vt_state {
194 	queue_t		*tvs_queue;	/* read queue for console */
195 	kmutex_t	tvs_lock;
196 	uchar_t		tvs_fbmode;	/* framebuffer mode */
197 	uchar_t		tvs_alpha;	/* rgb alpha channel */
198 	text_attr_t	tvs_flags;	/* flags for this x3.64 terminal */
199 	int		tvs_state;	/* state in output esc seq processing */
200 	boolean_t	tvs_gotparam;	/* does output esc seq have a param */
201 
202 	int	tvs_curparam;	/* current param # of output esc seq */
203 	int	tvs_paramval;	/* value of current param */
204 	int	tvs_params[TEM_MAXPARAMS];  /* parameters of output esc seq */
205 	screen_pos_t	*tvs_tabs;	/* tab stops */
206 	size_t	tvs_maxtab;		/* maximum number of tab stops */
207 	size_t	tvs_ntabs;		/* number of tabs used */
208 	int	tvs_nscroll;		/* number of lines to scroll */
209 
210 	struct tem_char_pos tvs_s_cursor;	/* start cursor position */
211 	struct tem_char_pos tvs_c_cursor;	/* current cursor position */
212 	struct tem_char_pos tvs_r_cursor;	/* remembered cursor position */
213 
214 	term_char_t	*tvs_outbuf;	/* place to keep incomplete lines */
215 	size_t		tvs_outbuf_size;
216 	size_t		tvs_outindex;	/* index into a_outbuf */
217 	void		*tvs_pix_data;	/* pointer to tmp bitmap area */
218 	size_t		tvs_pix_data_size;
219 
220 	text_color_t	tvs_fg_color;	/* console foreground */
221 	text_color_t	tvs_bg_color;	/* console background */
222 
223 	int		tvs_first_line;	/* kernel console output begins */
224 
225 	term_char_t	*tvs_screen_buf;	/* whole screen buffer */
226 	term_char_t	**tvs_screen_rows;	/* screen buffer rows */
227 	size_t		tvs_screen_buf_size;
228 	size_t		tvs_screen_history_size;
229 
230 	unsigned	tvs_utf8_left;		/* UTF-8 code points */
231 	tem_char_t	tvs_utf8_partial;	/* UTF-8 char being completed */
232 
233 	boolean_t	tvs_isactive;
234 	boolean_t	tvs_initialized;	/* initialization flag */
235 	boolean_t	tvs_cursor_hidden;
236 
237 	list_node_t	tvs_list_node;
238 };
239 _NOTE(MUTEX_PROTECTS_DATA(tem_vt_state::tvs_lock, tem_vt_state))
240 
241 typedef struct tem_safe_callbacks {
242 	void (*tsc_display)(struct tem_vt_state *, term_char_t *, int,
243 	    screen_pos_t, screen_pos_t, cred_t *, enum called_from);
244 	void (*tsc_copy)(struct tem_vt_state *,
245 	    screen_pos_t, screen_pos_t, screen_pos_t, screen_pos_t,
246 	    screen_pos_t, screen_pos_t, cred_t *, enum called_from);
247 	void (*tsc_cursor)(struct tem_vt_state *, short, cred_t *,
248 	    enum called_from);
249 	void (*tsc_bit2pix)(struct tem_vt_state *, term_char_t *);
250 	void (*tsc_cls)(struct tem_vt_state *, int,
251 	    screen_pos_t, screen_pos_t, cred_t *, enum called_from);
252 } tem_safe_callbacks_t;
253 
254 /*
255  * common term soft state structure shared by all virtual terminal emulators
256  */
257 typedef struct tem_state {
258 	ldi_handle_t	ts_hdl;	/* Framework handle for layered on dev */
259 	screen_size_t	ts_linebytes;	/* Layered on bytes per scan line */
260 
261 	int	ts_display_mode;	/* What mode we are in */
262 	struct	vis_polledio	*ts_fb_polledio;
263 
264 	struct tem_size ts_c_dimension;	/* window dimensions in characters */
265 	struct tem_size ts_p_dimension;	/* screen dimensions in pixels */
266 	struct tem_pix_pos ts_p_offset;	/* pix offset to center the display */
267 
268 	int	ts_pix_data_size;	/* size of bitmap data areas */
269 	int	ts_pdepth;		/* pixel depth */
270 	struct font	ts_font;	/* font table */
271 
272 	term_char_t	*ts_blank_line;	/* a blank line for scrolling */
273 	tem_safe_callbacks_t	*ts_callbacks;	/* internal output functions */
274 
275 	int	ts_initialized;		/* initialization flag */
276 
277 	tem_modechg_cb_t	ts_modechg_cb;
278 	tem_modechg_cb_arg_t	ts_modechg_arg;
279 
280 	color_map_fn_t	ts_color_map;
281 
282 	tem_color_t	ts_init_color; /* initial color and attributes */
283 
284 	struct tem_vt_state	*ts_active;
285 	kmutex_t	ts_lock;
286 	list_t		ts_list;	/* chain of all tems */
287 } tem_state_t;
288 
289 extern tem_state_t tems;
290 extern tem_safe_callbacks_t tem_safe_text_callbacks;
291 extern tem_safe_callbacks_t tem_safe_pix_callbacks;
292 
293 
294 /*
295  * tems_* fuctions mean that they just operate on the common soft state
296  * (tem_state_t), and tem_* functions mean that they operate on the
297  * per-tem structure (tem_vt_state). All "safe" interfaces are in tem_safe.c.
298  */
299 int	tems_cls_layered(struct vis_consclear *, cred_t *);
300 void	tems_display_layered(struct vis_consdisplay *, cred_t *);
301 void	tems_copy_layered(struct vis_conscopy *, cred_t *);
302 void	tems_cursor_layered(struct vis_conscursor *, cred_t *);
303 void	tems_safe_copy(struct vis_conscopy *, cred_t *, enum called_from);
304 
305 void	tem_align(struct tem_vt_state *, cred_t *, enum called_from);
306 void	tem_safe_check_first_time(struct tem_vt_state *tem, cred_t *,
307 	    enum called_from);
308 void	tem_safe_reset_display(struct tem_vt_state *, cred_t *,
309 	    enum called_from, boolean_t, boolean_t);
310 void	tem_safe_terminal_emulate(struct tem_vt_state *, uchar_t *, int,
311 	    cred_t *, enum called_from);
312 void	tem_safe_text_display(struct tem_vt_state *, term_char_t *,
313 	    int, screen_pos_t, screen_pos_t, cred_t *, enum called_from);
314 void	tem_safe_text_copy(struct tem_vt_state *,
315 	    screen_pos_t, screen_pos_t,
316 	    screen_pos_t, screen_pos_t,
317 	    screen_pos_t, screen_pos_t,
318 	    cred_t *, enum called_from);
319 void	tem_safe_text_cursor(struct tem_vt_state *, short, cred_t *,
320 	    enum called_from);
321 void	tem_safe_text_cls(struct tem_vt_state *,
322 	    int count, screen_pos_t row, screen_pos_t col,
323 	    cred_t *credp, enum called_from called_from);
324 void	tem_safe_pix_display(struct tem_vt_state *, term_char_t *,
325 	    int, screen_pos_t, screen_pos_t, cred_t *, enum called_from);
326 void	tem_safe_pix_copy(struct tem_vt_state *,
327 	    screen_pos_t, screen_pos_t,
328 	    screen_pos_t, screen_pos_t,
329 	    screen_pos_t, screen_pos_t,
330 	    cred_t *, enum called_from);
331 void	tem_safe_pix_cursor(struct tem_vt_state *, short, cred_t *,
332 	    enum called_from);
333 void	tem_safe_pix_bit2pix(struct tem_vt_state *, term_char_t *);
334 void	tem_safe_pix_cls(struct tem_vt_state *, int, screen_pos_t, screen_pos_t,
335 	    cred_t *, enum called_from);
336 void	tem_safe_pix_cls_range(struct tem_vt_state *,
337 	    screen_pos_t, int, int,
338 	    screen_pos_t, int, int,
339 	    boolean_t, cred_t *, enum called_from);
340 
341 void	tem_safe_pix_clear_entire_screen(struct tem_vt_state *,
342 	    cred_t *, enum called_from);
343 
344 void	tem_safe_get_attr(struct tem_vt_state *, text_color_t *,
345 	    text_color_t *, text_attr_t *, uint8_t);
346 
347 void	tem_safe_blank_screen(struct tem_vt_state *, cred_t *,
348 	    enum called_from);
349 void	tem_safe_unblank_screen(struct tem_vt_state *, cred_t *,
350 	    enum called_from);
351 #endif	/* _BOOT */
352 
353 #ifdef __cplusplus
354 }
355 #endif
356 
357 #endif /* _SYS_TEM_IMPL_H */
358