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