xref: /freebsd/sys/dev/syscons/scterm-sc.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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 		FG_BLACK,     FG_RED,          FG_GREEN,      FG_BROWN,
176 		FG_BLUE,      FG_MAGENTA,      FG_CYAN,       FG_LIGHTGREY,
177 		FG_DARKGREY,  FG_LIGHTRED,     FG_LIGHTGREEN, FG_YELLOW,
178 		FG_LIGHTBLUE, FG_LIGHTMAGENTA, FG_LIGHTCYAN,  FG_WHITE
179 	};
180 	sc_softc_t *sc;
181 	int i, n;
182 
183     i = n = 0;
184     sc = scp->sc;
185     if (tcp->esc == 1) {	/* seen ESC */
186 	switch (c) {
187 
188 	case '7':   /* Save cursor position */
189 	    tcp->saved_xpos = scp->xpos;
190 	    tcp->saved_ypos = scp->ypos;
191 	    break;
192 
193 	case '8':   /* Restore saved cursor position */
194 	    if (tcp->saved_xpos >= 0 && tcp->saved_ypos >= 0)
195 		sc_move_cursor(scp, tcp->saved_xpos, tcp->saved_ypos);
196 	    break;
197 
198 	case '[':   /* Start ESC [ sequence */
199 	    tcp->esc = 2;
200 	    tcp->last_param = -1;
201 	    for (i = tcp->num_param; i < MAX_ESC_PAR; i++)
202 		tcp->param[i] = 1;
203 	    tcp->num_param = 0;
204 	    return;
205 
206 	case 'M':   /* Move cursor up 1 line, scroll if at top */
207 	    sc_term_up_scroll(scp, 1, sc->scr_map[0x20], tcp->cur_attr, 0, 0);
208 	    break;
209 #if notyet
210 	case 'Q':
211 	    tcp->esc = 4;
212 	    return;
213 #endif
214 	case 'c':   /* Clear screen & home */
215 	    sc_clear_screen(scp);
216 	    break;
217 
218 	case '(':   /* iso-2022: designate 94 character set to G0 */
219 	    tcp->esc = 5;
220 	    return;
221 	}
222     }
223     else if (tcp->esc == 2) {	/* seen ESC [ */
224 	if (c >= '0' && c <= '9') {
225 	    if (tcp->num_param < MAX_ESC_PAR) {
226 		if (tcp->last_param != tcp->num_param) {
227 		    tcp->last_param = tcp->num_param;
228 		    tcp->param[tcp->num_param] = 0;
229 		} else {
230 		    tcp->param[tcp->num_param] *= 10;
231 		}
232 		tcp->param[tcp->num_param] += c - '0';
233 		return;
234 	    }
235 	}
236 	tcp->num_param = tcp->last_param + 1;
237 	switch (c) {
238 
239 	case ';':
240 	    if (tcp->num_param < MAX_ESC_PAR)
241 		return;
242 	    break;
243 
244 	case '=':
245 	    tcp->esc = 3;
246 	    tcp->last_param = -1;
247 	    for (i = tcp->num_param; i < MAX_ESC_PAR; i++)
248 		tcp->param[i] = 1;
249 	    tcp->num_param = 0;
250 	    return;
251 
252 	case 'A':   /* up n rows */
253 	    sc_term_up(scp, tcp->param[0], 0);
254 	    break;
255 
256 	case 'B':   /* down n rows */
257 	    sc_term_down(scp, tcp->param[0], 0);
258 	    break;
259 
260 	case 'C':   /* right n columns */
261 	    sc_term_right(scp, tcp->param[0]);
262 	    break;
263 
264 	case 'D':   /* left n columns */
265 	    sc_term_left(scp, tcp->param[0]);
266 	    break;
267 
268 	case 'E':   /* cursor to start of line n lines down */
269 	    n = tcp->param[0]; if (n < 1) n = 1;
270 	    sc_move_cursor(scp, 0, scp->ypos + n);
271 	    break;
272 
273 	case 'F':   /* cursor to start of line n lines up */
274 	    n = tcp->param[0]; if (n < 1) n = 1;
275 	    sc_move_cursor(scp, 0, scp->ypos - n);
276 	    break;
277 
278 	case 'f':   /* Cursor move */
279 	case 'H':
280 	    if (tcp->num_param == 0)
281 		sc_move_cursor(scp, 0, 0);
282 	    else if (tcp->num_param == 2)
283 		sc_move_cursor(scp, tcp->param[1] - 1, tcp->param[0] - 1);
284 	    break;
285 
286 	case 'J':   /* Clear all or part of display */
287 	    if (tcp->num_param == 0)
288 		n = 0;
289 	    else
290 		n = tcp->param[0];
291 	    sc_term_clr_eos(scp, n, sc->scr_map[0x20], tcp->cur_attr);
292 	    break;
293 
294 	case 'K':   /* Clear all or part of line */
295 	    if (tcp->num_param == 0)
296 		n = 0;
297 	    else
298 		n = tcp->param[0];
299 	    sc_term_clr_eol(scp, n, sc->scr_map[0x20], tcp->cur_attr);
300 	    break;
301 
302 	case 'L':   /* Insert n lines */
303 	    sc_term_ins_line(scp, scp->ypos, tcp->param[0],
304 			     sc->scr_map[0x20], tcp->cur_attr, 0);
305 	    break;
306 
307 	case 'M':   /* Delete n lines */
308 	    sc_term_del_line(scp, scp->ypos, tcp->param[0],
309 			     sc->scr_map[0x20], tcp->cur_attr, 0);
310 	    break;
311 
312 	case 'P':   /* Delete n chars */
313 	    sc_term_del_char(scp, tcp->param[0],
314 			     sc->scr_map[0x20], tcp->cur_attr);
315 	    break;
316 
317 	case '@':   /* Insert n chars */
318 	    sc_term_ins_char(scp, tcp->param[0],
319 			     sc->scr_map[0x20], tcp->cur_attr);
320 	    break;
321 
322 	case 'S':   /* scroll up n lines */
323 	    sc_term_del_line(scp, 0, tcp->param[0],
324 			     sc->scr_map[0x20], tcp->cur_attr, 0);
325 	    break;
326 
327 	case 'T':   /* scroll down n lines */
328 	    sc_term_ins_line(scp, 0, tcp->param[0],
329 			     sc->scr_map[0x20], tcp->cur_attr, 0);
330 	    break;
331 
332 	case 'X':   /* erase n characters in line */
333 	    n = tcp->param[0]; if (n < 1)  n = 1;
334 	    if (n > scp->xsize - scp->xpos)
335 		n = scp->xsize - scp->xpos;
336 	    sc_vtb_erase(&scp->vtb, scp->cursor_pos, n,
337 			 sc->scr_map[0x20], tcp->cur_attr);
338 	    mark_for_update(scp, scp->cursor_pos);
339 	    mark_for_update(scp, scp->cursor_pos + n - 1);
340 	    break;
341 
342 	case 'Z':   /* move n tabs backwards */
343 	    sc_term_backtab(scp, tcp->param[0]);
344 	    break;
345 
346 	case '`':   /* move cursor to column n */
347 	    sc_term_col(scp, tcp->param[0]);
348 	    break;
349 
350 	case 'a':   /* move cursor n columns to the right */
351 	    sc_term_right(scp, tcp->param[0]);
352 	    break;
353 
354 	case 'd':   /* move cursor to row n */
355 	    sc_term_row(scp, tcp->param[0]);
356 	    break;
357 
358 	case 'e':   /* move cursor n rows down */
359 	    sc_term_down(scp, tcp->param[0], 0);
360 	    break;
361 
362 	case 'm':   /* change attribute */
363 	    if (tcp->num_param == 0) {
364 		tcp->attr_mask = NORMAL_ATTR;
365 		tcp->cur_color = tcp->std_color;
366 		tcp->cur_attr = mask2attr(tcp);
367 		break;
368 	    }
369 	    for (i = 0; i < tcp->num_param; i++) {
370 		switch (n = tcp->param[i]) {
371 		case 0: /* back to normal */
372 		    tcp->attr_mask = NORMAL_ATTR;
373 		    tcp->cur_color = tcp->std_color;
374 		    tcp->cur_attr = mask2attr(tcp);
375 		    break;
376 		case 1: /* bold */
377 		    tcp->attr_mask |= BOLD_ATTR;
378 		    tcp->cur_attr = mask2attr(tcp);
379 		    break;
380 		case 4: /* underline */
381 		    tcp->attr_mask |= UNDERLINE_ATTR;
382 		    tcp->cur_attr = mask2attr(tcp);
383 		    break;
384 		case 5: /* blink */
385 		    tcp->attr_mask |= BLINK_ATTR;
386 		    tcp->cur_attr = mask2attr(tcp);
387 		    break;
388 		case 7: /* reverse video */
389 		    tcp->attr_mask |= REVERSE_ATTR;
390 		    tcp->cur_attr = mask2attr(tcp);
391 		    break;
392 		case 30: case 31: /* set fg color */
393 		case 32: case 33: case 34:
394 		case 35: case 36: case 37:
395 		    tcp->attr_mask |= FG_CHANGED;
396 		    tcp->cur_color.fg = ansi_col[n - 30];
397 		    tcp->cur_attr = mask2attr(tcp);
398 		    break;
399 		case 40: case 41: /* set bg color */
400 		case 42: case 43: case 44:
401 		case 45: case 46: case 47:
402 		    tcp->attr_mask |= BG_CHANGED;
403 		    tcp->cur_color.bg = ansi_col[n - 40];
404 		    tcp->cur_attr = mask2attr(tcp);
405 		    break;
406 		}
407 	    }
408 	    break;
409 
410 	case 's':   /* Save cursor position */
411 	    tcp->saved_xpos = scp->xpos;
412 	    tcp->saved_ypos = scp->ypos;
413 	    break;
414 
415 	case 'u':   /* Restore saved cursor position */
416 	    if (tcp->saved_xpos >= 0 && tcp->saved_ypos >= 0)
417 		sc_move_cursor(scp, tcp->saved_xpos, tcp->saved_ypos);
418 	    break;
419 
420 	case 'x':
421 	    if (tcp->num_param == 0)
422 		n = 0;
423 	    else
424 		n = tcp->param[0];
425 	    switch (n) {
426 	    case 0:     /* reset attributes */
427 		tcp->attr_mask = NORMAL_ATTR;
428 		tcp->cur_color = tcp->std_color = tcp->dflt_std_color;
429 		tcp->rev_color = tcp->dflt_rev_color;
430 		tcp->cur_attr = mask2attr(tcp);
431 		break;
432 	    case 1:     /* set ansi background */
433 		tcp->attr_mask &= ~BG_CHANGED;
434 		tcp->cur_color.bg = tcp->std_color.bg =
435 		    ansi_col[tcp->param[1] & 0x0f];
436 		tcp->cur_attr = mask2attr(tcp);
437 		break;
438 	    case 2:     /* set ansi foreground */
439 		tcp->attr_mask &= ~FG_CHANGED;
440 		tcp->cur_color.fg = tcp->std_color.fg =
441 		    ansi_col[tcp->param[1] & 0x0f];
442 		tcp->cur_attr = mask2attr(tcp);
443 		break;
444 	    case 3:     /* set ansi attribute directly */
445 		tcp->attr_mask &= ~(FG_CHANGED | BG_CHANGED);
446 		tcp->cur_color.fg = tcp->std_color.fg =
447 		    tcp->param[1] & 0x0f;
448 		tcp->cur_color.bg = tcp->std_color.bg =
449 		    (tcp->param[1] >> 4) & 0x0f;
450 		tcp->cur_attr = mask2attr(tcp);
451 		break;
452 	    case 5:     /* set ansi reverse video background */
453 		tcp->rev_color.bg = ansi_col[tcp->param[1] & 0x0f];
454 		tcp->cur_attr = mask2attr(tcp);
455 		break;
456 	    case 6:     /* set ansi reverse video foreground */
457 		tcp->rev_color.fg = ansi_col[tcp->param[1] & 0x0f];
458 		tcp->cur_attr = mask2attr(tcp);
459 		break;
460 	    case 7:     /* set ansi reverse video directly */
461 		tcp->rev_color.fg = tcp->param[1] & 0x0f;
462 		tcp->rev_color.bg = (tcp->param[1] >> 4) & 0x0f;
463 		tcp->cur_attr = mask2attr(tcp);
464 		break;
465 	    }
466 	    break;
467 
468 	case 'z':   /* switch to (virtual) console n */
469 	    if (tcp->num_param == 1)
470 		sc_switch_scr(sc, tcp->param[0]);
471 	    break;
472 	}
473     }
474     else if (tcp->esc == 3) {	/* seen ESC [0-9]+ = */
475 	if (c >= '0' && c <= '9') {
476 	    if (tcp->num_param < MAX_ESC_PAR) {
477 		if (tcp->last_param != tcp->num_param) {
478 		    tcp->last_param = tcp->num_param;
479 		    tcp->param[tcp->num_param] = 0;
480 		} else {
481 		    tcp->param[tcp->num_param] *= 10;
482 		}
483 		tcp->param[tcp->num_param] += c - '0';
484 		return;
485 	    }
486 	}
487 	tcp->num_param = tcp->last_param + 1;
488 	switch (c) {
489 
490 	case ';':
491 	    if (tcp->num_param < MAX_ESC_PAR)
492 		return;
493 	    break;
494 
495 	case 'A':   /* set display border color */
496 	    if (tcp->num_param == 1) {
497 		scp->border=tcp->param[0] & 0xff;
498 		if (scp == sc->cur_scp)
499 		    sc_set_border(scp, scp->border);
500             }
501 	    break;
502 
503 	case 'B':   /* set bell pitch and duration */
504 	    if (tcp->num_param == 2) {
505 		scp->bell_pitch = tcp->param[0];
506 		scp->bell_duration = tcp->param[1];
507 	    }
508 	    break;
509 
510 	case 'C':   /* set cursor type & shape */
511 	    i = spltty();
512 	    if (!ISGRAPHSC(sc->cur_scp))
513 		sc_remove_cursor_image(sc->cur_scp);
514 	    if (tcp->num_param == 1) {
515 		if (tcp->param[0] & 0x01)
516 		    sc->flags |= SC_BLINK_CURSOR;
517 		else
518 		    sc->flags &= ~SC_BLINK_CURSOR;
519 		if (tcp->param[0] & 0x02)
520 		    sc->flags |= SC_CHAR_CURSOR;
521 		else
522 		    sc->flags &= ~SC_CHAR_CURSOR;
523 	    } else if (tcp->num_param == 2) {
524 		sc->cursor_base = scp->font_size
525 					- (tcp->param[1] & 0x1F) - 1;
526 		sc->cursor_height = (tcp->param[1] & 0x1F)
527 					- (tcp->param[0] & 0x1F) + 1;
528 	    }
529 	    /*
530 	     * The cursor shape is global property; all virtual consoles
531 	     * are affected. Update the cursor in the current console...
532 	     */
533 	    if (!ISGRAPHSC(sc->cur_scp)) {
534 		sc_set_cursor_image(sc->cur_scp);
535 		sc_draw_cursor_image(sc->cur_scp);
536 	    }
537 	    splx(i);
538 	    break;
539 
540 	case 'F':   /* set ansi foreground */
541 	    if (tcp->num_param == 1) {
542 		tcp->attr_mask &= ~FG_CHANGED;
543 		tcp->cur_color.fg = tcp->std_color.fg = tcp->param[0] & 0x0f;
544 		tcp->cur_attr = mask2attr(tcp);
545 	    }
546 	    break;
547 
548 	case 'G':   /* set ansi background */
549 	    if (tcp->num_param == 1) {
550 		tcp->attr_mask &= ~BG_CHANGED;
551 		tcp->cur_color.bg = tcp->std_color.bg = tcp->param[0] & 0x0f;
552 		tcp->cur_attr = mask2attr(tcp);
553 	    }
554 	    break;
555 
556 	case 'H':   /* set ansi reverse video foreground */
557 	    if (tcp->num_param == 1) {
558 		tcp->rev_color.fg = tcp->param[0] & 0x0f;
559 		tcp->cur_attr = mask2attr(tcp);
560 	    }
561 	    break;
562 
563 	case 'I':   /* set ansi reverse video background */
564 	    if (tcp->num_param == 1) {
565 		tcp->rev_color.bg = tcp->param[0] & 0x0f;
566 		tcp->cur_attr = mask2attr(tcp);
567 	    }
568 	    break;
569 	}
570 
571 #if notyet
572 	} else if (tcp->esc == 4) {	/* seen ESC Q */
573 		/* to be filled */
574 #endif
575 	} else if (tcp->esc == 5) {	/* seen ESC ( */
576 		switch (c) {
577 		case 'B':   /* iso-2022: desginate ASCII into G0 */
578 			break;
579 			/* other items to be filled */
580 		default:
581 			break;
582 		}
583 	}
584 	tcp->esc = 0;
585 }
586 
587 static void
588 scterm_puts(scr_stat *scp, u_char *buf, int len)
589 {
590 	term_stat *tcp;
591 
592 	tcp = scp->ts;
593 outloop:
594 	scp->sc->write_in_progress++;
595 
596 	if (tcp->esc) {
597 		scterm_scan_esc(scp, tcp, *buf);
598 		buf++;
599 		len--;
600 	} else {
601 		switch (*buf) {
602 		case 0x1b:
603 			tcp->esc = 1;
604 			tcp->num_param = 0;
605 			buf++;
606 			len--;
607 			break;
608 		default:
609 			sc_term_gen_print(scp, &buf, &len, tcp->cur_attr);
610 			break;
611 		}
612 	}
613 
614 	sc_term_gen_scroll(scp, scp->sc->scr_map[0x20], tcp->cur_attr);
615 
616 	scp->sc->write_in_progress--;
617 	if (len)
618 		goto outloop;
619 }
620 
621 static int
622 scterm_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data,
623 	     int flag, struct proc *p)
624 {
625 	term_stat *tcp = scp->ts;
626 	vid_info_t *vi;
627 
628 	switch (cmd) {
629 	case GIO_ATTR:      	/* get current attributes */
630 		/* FIXME: */
631 		*(int*)data = (tcp->cur_attr >> 8) & 0xff;
632 		return 0;
633 	case CONS_GETINFO:  	/* get current (virtual) console info */
634 		vi = (vid_info_t *)data;
635 		if (vi->size != sizeof(struct vid_info))
636 			return EINVAL;
637 		vi->mv_norm.fore = tcp->std_color.fg;
638 		vi->mv_norm.back = tcp->std_color.bg;
639 		vi->mv_rev.fore = tcp->rev_color.fg;
640 		vi->mv_rev.back = tcp->rev_color.bg;
641 		/*
642 		 * The other fields are filled by the upper routine. XXX
643 		 */
644 		return ENOIOCTL;
645 	}
646 	return ENOIOCTL;
647 }
648 
649 static int
650 scterm_reset(scr_stat *scp, int code)
651 {
652 	/* FIXME */
653 	return 0;
654 }
655 
656 static void
657 scterm_default_attr(scr_stat *scp, int color, int rev_color)
658 {
659 	term_stat *tcp = scp->ts;
660 
661 	tcp->dflt_std_color.fg = color & 0x0f;
662 	tcp->dflt_std_color.bg = (color >> 4) & 0x0f;
663 	tcp->dflt_rev_color.fg = rev_color & 0x0f;
664 	tcp->dflt_rev_color.bg = (rev_color >> 4) & 0x0f;
665 	tcp->std_color = tcp->dflt_std_color;
666 	tcp->rev_color = tcp->dflt_rev_color;
667 	tcp->cur_color = tcp->std_color;
668 	tcp->cur_attr = mask2attr(tcp);
669 }
670 
671 static void
672 scterm_clear(scr_stat *scp)
673 {
674 	term_stat *tcp = scp->ts;
675 
676 	sc_move_cursor(scp, 0, 0);
677 	sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20], tcp->cur_attr);
678 	mark_all(scp);
679 }
680 
681 static void
682 scterm_notify(scr_stat *scp, int event)
683 {
684 	switch (event) {
685 	case SC_TE_NOTIFY_VTSWITCH_IN:
686 		break;
687 	case SC_TE_NOTIFY_VTSWITCH_OUT:
688 		break;
689 	}
690 }
691 
692 static int
693 scterm_input(scr_stat *scp, int c, struct tty *tp)
694 {
695 	return FALSE;
696 }
697 
698 /*
699  * Calculate hardware attributes word using logical attributes mask and
700  * hardware colors
701  */
702 
703 /* FIXME */
704 static int
705 mask2attr(term_stat *tcp)
706 {
707 	int attr, mask = tcp->attr_mask;
708 
709 	if (mask & REVERSE_ATTR) {
710 		attr = ((mask & FG_CHANGED) ?
711 			tcp->cur_color.bg : tcp->rev_color.fg) |
712 			(((mask & BG_CHANGED) ?
713 			tcp->cur_color.fg : tcp->rev_color.bg) << 4);
714 	} else
715 		attr = tcp->cur_color.fg | (tcp->cur_color.bg << 4);
716 
717 	/* XXX: underline mapping for Hercules adapter can be better */
718 	if (mask & (BOLD_ATTR | UNDERLINE_ATTR))
719 		attr ^= 0x08;
720 	if (mask & BLINK_ATTR)
721 		attr ^= 0x80;
722 
723 	return (attr << 8);
724 }
725 
726 #endif /* SC_DUMB_TERMINAL */
727