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