xref: /freebsd/sys/dev/syscons/scterm-sc.c (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * Copyright (c) 1992-1998 Søren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer as
11  *    the first lines of this file unmodified.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include "opt_syscons.h"
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/consio.h>
36 
37 #if defined(__arm__) || defined(__powerpc__)
38 #include <machine/sc_machdep.h>
39 #else
40 #include <machine/pc/display.h>
41 #endif
42 
43 #include <dev/syscons/syscons.h>
44 #include <dev/syscons/sctermvar.h>
45 
46 #define MAX_ESC_PAR	5
47 
48 /* attribute flags */
49 typedef struct {
50 	u_short		fg;			/* foreground color */
51 	u_short		bg;			/* background color */
52 } color_t;
53 
54 typedef struct {
55 	int		flags;
56 #define SCTERM_BUSY	(1 << 0)
57 	int		esc;
58 	int		num_param;
59 	int		last_param;
60 	int		param[MAX_ESC_PAR];
61 	int		saved_xpos;
62 	int		saved_ypos;
63 	int		attr_mask;		/* current logical attr mask */
64 #define NORMAL_ATTR	0x00
65 #define BLINK_ATTR	0x01
66 #define BOLD_ATTR	0x02
67 #define UNDERLINE_ATTR	0x04
68 #define REVERSE_ATTR	0x08
69 #define FG_CHANGED	0x10
70 #define BG_CHANGED	0x20
71 	int		cur_attr;		/* current hardware attr word */
72 	color_t		cur_color;		/* current hardware color */
73 	color_t		std_color;		/* normal hardware color */
74 	color_t		rev_color;		/* reverse hardware color */
75 	color_t		dflt_std_color;		/* default normal color */
76 	color_t		dflt_rev_color;		/* default reverse color */
77 } term_stat;
78 
79 static sc_term_init_t	scterm_init;
80 static sc_term_term_t	scterm_term;
81 static sc_term_puts_t	scterm_puts;
82 static sc_term_ioctl_t	scterm_ioctl;
83 static sc_term_reset_t	scterm_reset;
84 static sc_term_default_attr_t	scterm_default_attr;
85 static sc_term_clear_t	scterm_clear;
86 static sc_term_notify_t	scterm_notify;
87 static sc_term_input_t	scterm_input;
88 static sc_term_fkeystr_t	scterm_fkeystr;
89 static sc_term_sync_t	scterm_sync;
90 
91 static sc_term_sw_t sc_term_sc = {
92 	{ NULL, NULL },
93 	"sc",					/* emulator name */
94 	"syscons terminal",			/* description */
95 	"*",					/* matching renderer, any :-) */
96 	sizeof(term_stat),			/* softc size */
97 	0,
98 	scterm_init,
99 	scterm_term,
100 	scterm_puts,
101 	scterm_ioctl,
102 	scterm_reset,
103 	scterm_default_attr,
104 	scterm_clear,
105 	scterm_notify,
106 	scterm_input,
107 	scterm_fkeystr,
108 	scterm_sync,
109 };
110 
111 SCTERM_MODULE(sc, sc_term_sc);
112 
113 static term_stat	reserved_term_stat;
114 static void		scterm_scan_esc(scr_stat *scp, term_stat *tcp,
115 					u_char c);
116 static int		mask2attr(term_stat *tcp);
117 
118 static int
119 scterm_init(scr_stat *scp, void **softc, int code)
120 {
121 	term_stat *tcp;
122 
123 	if (*softc == NULL) {
124 		if (reserved_term_stat.flags & SCTERM_BUSY)
125 			return EINVAL;
126 		*softc = &reserved_term_stat;
127 	}
128 	tcp = *softc;
129 
130 	switch (code) {
131 	case SC_TE_COLD_INIT:
132 		bzero(tcp, sizeof(*tcp));
133 		tcp->flags = SCTERM_BUSY;
134 		tcp->esc = 0;
135 		tcp->saved_xpos = -1;
136 		tcp->saved_ypos = -1;
137 		tcp->attr_mask = NORMAL_ATTR;
138 		/* XXX */
139 		tcp->dflt_std_color.fg = SC_NORM_ATTR & 0x0f;
140 		tcp->dflt_std_color.bg = (SC_NORM_ATTR >> 4) & 0x0f;
141 		tcp->dflt_rev_color.fg = SC_NORM_REV_ATTR & 0x0f;
142 		tcp->dflt_rev_color.bg = (SC_NORM_REV_ATTR >> 4) & 0x0f;
143 		tcp->std_color = tcp->dflt_std_color;
144 		tcp->rev_color = tcp->dflt_rev_color;
145 		tcp->cur_color = tcp->std_color;
146 		tcp->cur_attr = mask2attr(tcp);
147 		++sc_term_sc.te_refcount;
148 		break;
149 
150 	case SC_TE_WARM_INIT:
151 		tcp->esc = 0;
152 		tcp->saved_xpos = -1;
153 		tcp->saved_ypos = -1;
154 #if 0
155 		tcp->std_color = tcp->dflt_std_color;
156 		tcp->rev_color = tcp->dflt_rev_color;
157 #endif
158 		tcp->cur_color = tcp->std_color;
159 		tcp->cur_attr = mask2attr(tcp);
160 		break;
161 	}
162 
163 	return 0;
164 }
165 
166 static int
167 scterm_term(scr_stat *scp, void **softc)
168 {
169 	if (*softc == &reserved_term_stat) {
170 		*softc = NULL;
171 		bzero(&reserved_term_stat, sizeof(reserved_term_stat));
172 	}
173 	--sc_term_sc.te_refcount;
174 	return 0;
175 }
176 
177 static void
178 scterm_scan_esc(scr_stat *scp, term_stat *tcp, u_char c)
179 {
180 	static u_char ansi_col[16] = {
181 		FG_BLACK,     FG_RED,          FG_GREEN,      FG_BROWN,
182 		FG_BLUE,      FG_MAGENTA,      FG_CYAN,       FG_LIGHTGREY,
183 		FG_DARKGREY,  FG_LIGHTRED,     FG_LIGHTGREEN, FG_YELLOW,
184 		FG_LIGHTBLUE, FG_LIGHTMAGENTA, FG_LIGHTCYAN,  FG_WHITE
185 	};
186 	static int cattrs[] = {
187 		0,					/* block */
188 		CONS_BLINK_CURSOR,			/* blinking block */
189 		CONS_CHAR_CURSOR,			/* underline */
190 		CONS_CHAR_CURSOR | CONS_BLINK_CURSOR,	/* blinking underline */
191 		CONS_RESET_CURSOR,			/* reset to default */
192 		CONS_HIDDEN_CURSOR,			/* hide cursor */
193 	};
194 	static int tcattrs[] = {
195 		CONS_RESET_CURSOR | CONS_LOCAL_CURSOR,	/* normal */
196 		CONS_HIDDEN_CURSOR | CONS_LOCAL_CURSOR,	/* invisible */
197 		CONS_BLINK_CURSOR | CONS_LOCAL_CURSOR,	/* very visible */
198 	};
199 	sc_softc_t *sc;
200 	int v0, v1, v2;
201 	int i, n;
202 
203 	i = n = 0;
204 	sc = scp->sc;
205 	if (tcp->esc == 1) {	/* seen ESC */
206 		switch (c) {
207 		case '7':	/* Save cursor position */
208 			tcp->saved_xpos = scp->xpos;
209 			tcp->saved_ypos = scp->ypos;
210 			break;
211 
212 		case '8':	/* Restore saved cursor position */
213 			if (tcp->saved_xpos >= 0 && tcp->saved_ypos >= 0)
214 				sc_move_cursor(scp, tcp->saved_xpos,
215 					       tcp->saved_ypos);
216 			break;
217 
218 		case '[':	/* Start ESC [ sequence */
219 			tcp->esc = 2;
220 			tcp->last_param = -1;
221 			for (i = tcp->num_param; i < MAX_ESC_PAR; i++)
222 				tcp->param[i] = 1;
223 			tcp->num_param = 0;
224 			return;
225 
226 		case 'M':	/* Move cursor up 1 line, scroll if at top */
227 			sc_term_up_scroll(scp, 1, sc->scr_map[0x20],
228 					  tcp->cur_attr, 0, 0);
229 			break;
230 #ifdef notyet
231 		case 'Q':
232 			tcp->esc = 4;
233 			return;
234 #endif
235 		case 'c':       /* reset */
236 			tcp->attr_mask = NORMAL_ATTR;
237 			tcp->cur_color = tcp->std_color
238 				       = tcp->dflt_std_color;
239 			tcp->rev_color = tcp->dflt_rev_color;
240 			tcp->cur_attr = mask2attr(tcp);
241 			sc_change_cursor_shape(scp,
242 			    CONS_RESET_CURSOR | CONS_LOCAL_CURSOR, -1, -1);
243 			sc_clear_screen(scp);
244 			break;
245 
246 		case '(':	/* iso-2022: designate 94 character set to G0 */
247 			tcp->esc = 5;
248 			return;
249 		}
250 	} else if (tcp->esc == 2) {	/* seen ESC [ */
251 		if (c >= '0' && c <= '9') {
252 			if (tcp->num_param < MAX_ESC_PAR) {
253 				if (tcp->last_param != tcp->num_param) {
254 					tcp->last_param = tcp->num_param;
255 					tcp->param[tcp->num_param] = 0;
256 				} else {
257 					tcp->param[tcp->num_param] *= 10;
258 				}
259 				tcp->param[tcp->num_param] += c - '0';
260 				return;
261 			}
262 		}
263 		tcp->num_param = tcp->last_param + 1;
264 		switch (c) {
265 		case ';':
266 			if (tcp->num_param < MAX_ESC_PAR)
267 				return;
268 			break;
269 
270 		case '=':
271 			tcp->esc = 3;
272 			tcp->last_param = -1;
273 			for (i = tcp->num_param; i < MAX_ESC_PAR; i++)
274 				tcp->param[i] = 1;
275 			tcp->num_param = 0;
276 			return;
277 
278 		case 'A':	/* up n rows */
279 			sc_term_up(scp, tcp->param[0], 0);
280 			break;
281 
282 		case 'B':	/* down n rows */
283 			sc_term_down(scp, tcp->param[0], 0);
284 			break;
285 
286 		case 'C':	/* right n columns */
287 			sc_term_right(scp, tcp->param[0]);
288 			break;
289 
290 		case 'D':	/* left n columns */
291 			sc_term_left(scp, tcp->param[0]);
292 			break;
293 
294 		case 'E':	/* cursor to start of line n lines down */
295 			n = tcp->param[0];
296 			if (n < 1)
297 				n = 1;
298 			sc_move_cursor(scp, 0, scp->ypos + n);
299 			break;
300 
301 		case 'F':	/* cursor to start of line n lines up */
302 			n = tcp->param[0];
303 			if (n < 1)
304 				n = 1;
305 			sc_move_cursor(scp, 0, scp->ypos - n);
306 			break;
307 
308 		case 'f':	/* Cursor move */
309 		case 'H':
310 			if (tcp->num_param == 0)
311 				sc_move_cursor(scp, 0, 0);
312 			else if (tcp->num_param == 2)
313 				sc_move_cursor(scp, tcp->param[1] - 1,
314 					       tcp->param[0] - 1);
315 			break;
316 
317 		case 'J':	/* Clear all or part of display */
318 			if (tcp->num_param == 0)
319 				n = 0;
320 			else
321 				n = tcp->param[0];
322 			sc_term_clr_eos(scp, n, sc->scr_map[0x20],
323 					tcp->cur_attr);
324 			break;
325 
326 		case 'K':	/* Clear all or part of line */
327 			if (tcp->num_param == 0)
328 				n = 0;
329 			else
330 				n = tcp->param[0];
331 			sc_term_clr_eol(scp, n, sc->scr_map[0x20],
332 					tcp->cur_attr);
333 			break;
334 
335 		case 'L':	/* Insert n lines */
336 			sc_term_ins_line(scp, scp->ypos, tcp->param[0],
337 					 sc->scr_map[0x20], tcp->cur_attr, 0);
338 			break;
339 
340 		case 'M':	/* Delete n lines */
341 			sc_term_del_line(scp, scp->ypos, tcp->param[0],
342 					 sc->scr_map[0x20], tcp->cur_attr, 0);
343 			break;
344 
345 		case 'P':	/* Delete n chars */
346 			sc_term_del_char(scp, tcp->param[0],
347 					 sc->scr_map[0x20], tcp->cur_attr);
348 			break;
349 
350 		case '@':	/* Insert n chars */
351 			sc_term_ins_char(scp, tcp->param[0],
352 					 sc->scr_map[0x20], tcp->cur_attr);
353 			break;
354 
355 		case 'S':	/* scroll up n lines */
356 			sc_term_del_line(scp, 0, tcp->param[0],
357 					 sc->scr_map[0x20], tcp->cur_attr, 0);
358 			break;
359 
360 		case 'T':	/* scroll down n lines */
361 			sc_term_ins_line(scp, 0, tcp->param[0],
362 					 sc->scr_map[0x20], tcp->cur_attr, 0);
363 			break;
364 
365 		case 'X':	/* erase n characters in line */
366 			n = tcp->param[0];
367 			if (n < 1)
368 				n = 1;
369 			if (n > scp->xsize - scp->xpos)
370 				n = scp->xsize - scp->xpos;
371 			sc_vtb_erase(&scp->vtb, scp->cursor_pos, n,
372 				     sc->scr_map[0x20], tcp->cur_attr);
373 			mark_for_update(scp, scp->cursor_pos);
374 			mark_for_update(scp, scp->cursor_pos + n - 1);
375 			break;
376 
377 		case 'Z':	/* move n tabs backwards */
378 			sc_term_backtab(scp, tcp->param[0]);
379 			break;
380 
381 		case '`':	/* move cursor to column n */
382 			sc_term_col(scp, tcp->param[0]);
383 			break;
384 
385 		case 'a':	/* move cursor n columns to the right */
386 			sc_term_right(scp, tcp->param[0]);
387 			break;
388 
389 		case 'd':	/* move cursor to row n */
390 			sc_term_row(scp, tcp->param[0]);
391 			break;
392 
393 		case 'e':	/* move cursor n rows down */
394 			sc_term_down(scp, tcp->param[0], 0);
395 			break;
396 
397 		case 'm':	/* change attribute */
398 			if (tcp->num_param == 0) {
399 				tcp->attr_mask = NORMAL_ATTR;
400 				tcp->cur_color = tcp->std_color;
401 				tcp->cur_attr = mask2attr(tcp);
402 				break;
403 			}
404 			for (i = 0; i < tcp->num_param; i++) {
405 				switch (n = tcp->param[i]) {
406 				case 0:	/* back to normal */
407 					tcp->attr_mask = NORMAL_ATTR;
408 					tcp->cur_color = tcp->std_color;
409 					tcp->cur_attr = mask2attr(tcp);
410 					break;
411 				case 1:	/* bold */
412 					tcp->attr_mask |= BOLD_ATTR;
413 					tcp->cur_attr = mask2attr(tcp);
414 					break;
415 				case 4:	/* underline */
416 					tcp->attr_mask |= UNDERLINE_ATTR;
417 					tcp->cur_attr = mask2attr(tcp);
418 					break;
419 				case 5:	/* blink */
420 					tcp->attr_mask |= BLINK_ATTR;
421 					tcp->cur_attr = mask2attr(tcp);
422 					break;
423 				case 7: /* reverse */
424 					tcp->attr_mask |= REVERSE_ATTR;
425 					tcp->cur_attr = mask2attr(tcp);
426 					break;
427 				case 22: /* remove bold (or dim) */
428 					tcp->attr_mask &= ~BOLD_ATTR;
429 					tcp->cur_attr = mask2attr(tcp);
430 					break;
431 				case 24: /* remove underline */
432 					tcp->attr_mask &= ~UNDERLINE_ATTR;
433 					tcp->cur_attr = mask2attr(tcp);
434 					break;
435 				case 25: /* remove blink */
436 					tcp->attr_mask &= ~BLINK_ATTR;
437 					tcp->cur_attr = mask2attr(tcp);
438 					break;
439 				case 27: /* remove reverse */
440 					tcp->attr_mask &= ~REVERSE_ATTR;
441 					tcp->cur_attr = mask2attr(tcp);
442 					break;
443 				case 30: case 31: /* set ansi fg color */
444 				case 32: case 33: case 34:
445 				case 35: case 36: case 37:
446 					tcp->attr_mask |= FG_CHANGED;
447 					tcp->cur_color.fg = ansi_col[n - 30];
448 					tcp->cur_attr = mask2attr(tcp);
449 					break;
450 				case 39: /* restore fg color back to normal */
451 					tcp->attr_mask &= ~(FG_CHANGED|BOLD_ATTR);
452 					tcp->cur_color.fg = tcp->std_color.fg;
453 					tcp->cur_attr = mask2attr(tcp);
454 					break;
455 				case 40: case 41: /* set ansi bg color */
456 				case 42: case 43: case 44:
457 				case 45: case 46: case 47:
458 					tcp->attr_mask |= BG_CHANGED;
459 					tcp->cur_color.bg = ansi_col[n - 40];
460 					tcp->cur_attr = mask2attr(tcp);
461 		    			break;
462 				case 49: /* restore bg color back to normal */
463 					tcp->attr_mask &= ~BG_CHANGED;
464 					tcp->cur_color.bg = tcp->std_color.bg;
465 					tcp->cur_attr = mask2attr(tcp);
466 					break;
467 				}
468 			}
469 			break;
470 
471 		case 's':	/* Save cursor position */
472 			tcp->saved_xpos = scp->xpos;
473 			tcp->saved_ypos = scp->ypos;
474 			break;
475 
476 		case 'u':	/* Restore saved cursor position */
477 			if (tcp->saved_xpos >= 0 && tcp->saved_ypos >= 0)
478 				sc_move_cursor(scp, tcp->saved_xpos,
479 					       tcp->saved_ypos);
480 			break;
481 
482 		case 'x':
483 			if (tcp->num_param == 0)
484 				n = 0;
485 			else
486 				n = tcp->param[0];
487 			switch (n) {
488 			case 0: /* reset colors and attributes back to normal */
489 				tcp->attr_mask = NORMAL_ATTR;
490 				tcp->cur_color = tcp->std_color
491 					       = tcp->dflt_std_color;
492 				tcp->rev_color = tcp->dflt_rev_color;
493 				tcp->cur_attr = mask2attr(tcp);
494 				break;
495 			case 1:	/* set ansi background */
496 				tcp->attr_mask &= ~BG_CHANGED;
497 				tcp->cur_color.bg = tcp->std_color.bg
498 						  = ansi_col[tcp->param[1] & 0x0f];
499 				tcp->cur_attr = mask2attr(tcp);
500 				break;
501 			case 2:	/* set ansi foreground */
502 				tcp->attr_mask &= ~FG_CHANGED;
503 				tcp->cur_color.fg = tcp->std_color.fg
504 						  = ansi_col[tcp->param[1] & 0x0f];
505 				tcp->cur_attr = mask2attr(tcp);
506 				break;
507 			case 3: /* set adapter attribute directly */
508 				tcp->attr_mask &= ~(FG_CHANGED | BG_CHANGED);
509 				tcp->cur_color.fg = tcp->std_color.fg
510 						  = tcp->param[1] & 0x0f;
511 				tcp->cur_color.bg = tcp->std_color.bg
512 						  = (tcp->param[1] >> 4) & 0x0f;
513 				tcp->cur_attr = mask2attr(tcp);
514 				break;
515 			case 5: /* set ansi reverse background */
516 				tcp->rev_color.bg = ansi_col[tcp->param[1] & 0x0f];
517 				tcp->cur_attr = mask2attr(tcp);
518 				break;
519 			case 6: /* set ansi reverse foreground */
520 				tcp->rev_color.fg = ansi_col[tcp->param[1] & 0x0f];
521 				tcp->cur_attr = mask2attr(tcp);
522 				break;
523 			case 7: /* set adapter reverse attribute directly */
524 				tcp->rev_color.fg = tcp->param[1] & 0x0f;
525 				tcp->rev_color.bg = (tcp->param[1] >> 4) & 0x0f;
526 				tcp->cur_attr = mask2attr(tcp);
527 				break;
528 			}
529 			break;
530 
531 		case 'z':	/* switch to (virtual) console n */
532 			if (tcp->num_param == 1)
533 				sc_switch_scr(sc, tcp->param[0]);
534 			break;
535 		}
536 	} else if (tcp->esc == 3) {	/* seen ESC [0-9]+ = */
537 		if (c >= '0' && c <= '9') {
538 			if (tcp->num_param < MAX_ESC_PAR) {
539 				if (tcp->last_param != tcp->num_param) {
540 					tcp->last_param = tcp->num_param;
541 					tcp->param[tcp->num_param] = 0;
542 				} else {
543 					tcp->param[tcp->num_param] *= 10;
544 				}
545 				tcp->param[tcp->num_param] += c - '0';
546 				return;
547 			}
548 		}
549 		tcp->num_param = tcp->last_param + 1;
550 		switch (c) {
551 		case ';':
552 			if (tcp->num_param < MAX_ESC_PAR)
553 				return;
554 			break;
555 
556 		case 'A':   /* set display border color */
557 			if (tcp->num_param == 1) {
558 				scp->border=tcp->param[0] & 0xff;
559 				if (scp == sc->cur_scp)
560 					sc_set_border(scp, scp->border);
561 			}
562 			break;
563 
564 		case 'B':   /* set bell pitch and duration */
565 			if (tcp->num_param == 2) {
566 				scp->bell_pitch = tcp->param[0];
567 				scp->bell_duration =
568 				    (tcp->param[1] * hz + 99) / 100;
569 			}
570 			break;
571 
572 		case 'C':   /* set global/parmanent cursor type & shape */
573 			i = spltty();
574 			n = tcp->num_param;
575 			v0 = tcp->param[0];
576 			v1 = tcp->param[1];
577 			v2 = tcp->param[2];
578 			switch (n) {
579 			case 1:	/* flags only */
580 				if (v0 < sizeof(cattrs)/sizeof(cattrs[0]))
581 					v0 = cattrs[v0];
582 				else	/* backward compatibility */
583 					v0 = cattrs[v0 & 0x3];
584 				sc_change_cursor_shape(scp, v0, -1, -1);
585 				break;
586 			case 2:
587 				v2 = 0;
588 				v0 &= 0x1f;	/* backward compatibility */
589 				v1 &= 0x1f;
590 				/* FALL THROUGH */
591 			case 3:	/* base and height */
592 				if (v2 == 0)	/* count from top */
593 					sc_change_cursor_shape(scp, -1,
594 					    scp->font_size - v1 - 1,
595 					    v1 - v0 + 1);
596 				else if (v2 == 1) /* count from bottom */
597 					sc_change_cursor_shape(scp, -1,
598 					    v0, v1 - v0 + 1);
599 				break;
600 			}
601 			splx(i);
602 			break;
603 
604 		case 'F':   /* set adapter foreground */
605 			if (tcp->num_param == 1) {
606 				tcp->attr_mask &= ~FG_CHANGED;
607 				tcp->cur_color.fg = tcp->std_color.fg
608 						  = tcp->param[0] & 0x0f;
609 				tcp->cur_attr = mask2attr(tcp);
610 			}
611 			break;
612 
613 		case 'G':   /* set adapter background */
614 			if (tcp->num_param == 1) {
615 				tcp->attr_mask &= ~BG_CHANGED;
616 				tcp->cur_color.bg = tcp->std_color.bg
617 						  = tcp->param[0] & 0x0f;
618 				tcp->cur_attr = mask2attr(tcp);
619 			}
620 			break;
621 
622 		case 'H':   /* set adapter reverse foreground */
623 			if (tcp->num_param == 1) {
624 				tcp->rev_color.fg = tcp->param[0] & 0x0f;
625 				tcp->cur_attr = mask2attr(tcp);
626 			}
627 			break;
628 
629 		case 'I':   /* set adapter reverse background */
630 			if (tcp->num_param == 1) {
631 				tcp->rev_color.bg = tcp->param[0] & 0x0f;
632 				tcp->cur_attr = mask2attr(tcp);
633 			}
634 			break;
635 
636 		case 'S':   /* set local/temporary cursor type & shape */
637 			i = spltty();
638 			n = tcp->num_param;
639 			v0 = tcp->param[0];
640 			switch (n) {
641 			case 0:
642 				v0 = 0;
643 				/* FALL THROUGH */
644 			case 1:
645 				if (v0 < sizeof(tcattrs)/sizeof(tcattrs[0]))
646 					sc_change_cursor_shape(scp,
647 					    tcattrs[v0], -1, -1);
648 				break;
649 			}
650 			splx(i);
651 			break;
652 		}
653 #ifdef notyet
654 	} else if (tcp->esc == 4) {	/* seen ESC Q */
655 		/* to be filled */
656 #endif
657 	} else if (tcp->esc == 5) {	/* seen ESC ( */
658 		switch (c) {
659 		case 'B':   /* iso-2022: desginate ASCII into G0 */
660 			break;
661 		/* other items to be filled */
662 		default:
663 			break;
664 		}
665 	}
666 	tcp->esc = 0;
667 }
668 
669 static void
670 scterm_puts(scr_stat *scp, u_char *buf, int len)
671 {
672 	term_stat *tcp;
673 
674 	tcp = scp->ts;
675 outloop:
676 	scp->sc->write_in_progress++;
677 
678 	if (tcp->esc) {
679 		scterm_scan_esc(scp, tcp, *buf);
680 		buf++;
681 		len--;
682 	} else {
683 		switch (*buf) {
684 		case 0x1b:
685 			tcp->esc = 1;
686 			tcp->num_param = 0;
687 			buf++;
688 			len--;
689 			break;
690 		default:
691 			sc_term_gen_print(scp, &buf, &len, tcp->cur_attr);
692 			break;
693 		}
694 	}
695 
696 	sc_term_gen_scroll(scp, scp->sc->scr_map[0x20], tcp->cur_attr);
697 
698 	scp->sc->write_in_progress--;
699 	if (len)
700 		goto outloop;
701 }
702 
703 static int
704 scterm_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data,
705 	     struct thread *td)
706 {
707 	term_stat *tcp = scp->ts;
708 	vid_info_t *vi;
709 
710 	switch (cmd) {
711 	case GIO_ATTR:      	/* get current attributes */
712 		/* FIXME: */
713 		*(int*)data = (tcp->cur_attr >> 8) & 0xff;
714 		return 0;
715 	case CONS_GETINFO:  	/* get current (virtual) console info */
716 		vi = (vid_info_t *)data;
717 		if (vi->size != sizeof(struct vid_info))
718 			return EINVAL;
719 		vi->mv_norm.fore = tcp->std_color.fg;
720 		vi->mv_norm.back = tcp->std_color.bg;
721 		vi->mv_rev.fore = tcp->rev_color.fg;
722 		vi->mv_rev.back = tcp->rev_color.bg;
723 		/*
724 		 * The other fields are filled by the upper routine. XXX
725 		 */
726 		return ENOIOCTL;
727 	}
728 	return ENOIOCTL;
729 }
730 
731 static int
732 scterm_reset(scr_stat *scp, int code)
733 {
734 	/* FIXME */
735 	return 0;
736 }
737 
738 static void
739 scterm_default_attr(scr_stat *scp, int color, int rev_color)
740 {
741 	term_stat *tcp = scp->ts;
742 
743 	tcp->dflt_std_color.fg = color & 0x0f;
744 	tcp->dflt_std_color.bg = (color >> 4) & 0x0f;
745 	tcp->dflt_rev_color.fg = rev_color & 0x0f;
746 	tcp->dflt_rev_color.bg = (rev_color >> 4) & 0x0f;
747 	tcp->std_color = tcp->dflt_std_color;
748 	tcp->rev_color = tcp->dflt_rev_color;
749 	tcp->cur_color = tcp->std_color;
750 	tcp->cur_attr = mask2attr(tcp);
751 }
752 
753 static void
754 scterm_clear(scr_stat *scp)
755 {
756 	term_stat *tcp = scp->ts;
757 
758 	sc_move_cursor(scp, 0, 0);
759 	sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20], tcp->cur_attr);
760 	mark_all(scp);
761 }
762 
763 static void
764 scterm_notify(scr_stat *scp, int event)
765 {
766 	switch (event) {
767 	case SC_TE_NOTIFY_VTSWITCH_IN:
768 		break;
769 	case SC_TE_NOTIFY_VTSWITCH_OUT:
770 		break;
771 	}
772 }
773 
774 static int
775 scterm_input(scr_stat *scp, int c, struct tty *tp)
776 {
777 	return FALSE;
778 }
779 
780 static const char *
781 scterm_fkeystr(scr_stat *scp, int c)
782 {
783 	return (NULL);
784 }
785 
786 static void
787 scterm_sync(scr_stat *scp)
788 {
789 }
790 
791 /*
792  * Calculate hardware attributes word using logical attributes mask and
793  * hardware colors
794  */
795 
796 /* FIXME */
797 static int
798 mask2attr(term_stat *tcp)
799 {
800 	int attr, mask = tcp->attr_mask;
801 
802 	if (mask & REVERSE_ATTR) {
803 		attr = ((mask & FG_CHANGED) ?
804 			tcp->cur_color.bg : tcp->rev_color.fg) |
805 			(((mask & BG_CHANGED) ?
806 			tcp->cur_color.fg : tcp->rev_color.bg) << 4);
807 	} else
808 		attr = tcp->cur_color.fg | (tcp->cur_color.bg << 4);
809 
810 	/* XXX: underline mapping for Hercules adapter can be better */
811 	if (mask & (BOLD_ATTR | UNDERLINE_ATTR))
812 		attr ^= 0x08;
813 	if (mask & BLINK_ATTR)
814 		attr ^= 0x80;
815 
816 	return (attr << 8);
817 }
818