xref: /freebsd-13.1/sys/dev/syscons/syscons.c (revision 92927338)
1 /*-
2  * Copyright (c) 1992-1998 S�ren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include "sc.h"
32 #include "splash.h"
33 #include "opt_syscons.h"
34 #include "opt_ddb.h"
35 #ifdef __i386__
36 #include "apm.h"
37 #endif
38 
39 #if NSC > 0
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/eventhandler.h>
43 #include <sys/reboot.h>
44 #include <sys/conf.h>
45 #include <sys/proc.h>
46 #include <sys/signalvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/tty.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/cons.h>
52 
53 #include <machine/clock.h>
54 #include <machine/console.h>
55 #include <machine/psl.h>
56 #include <machine/pc/display.h>
57 #ifdef __i386__
58 #include <machine/apm_bios.h>
59 #include <machine/frame.h>
60 #include <machine/random.h>
61 #endif
62 
63 #include <dev/kbd/kbdreg.h>
64 #include <dev/fb/fbreg.h>
65 #include <dev/fb/splashreg.h>
66 #include <dev/syscons/syscons.h>
67 
68 #define COLD 0
69 #define WARM 1
70 
71 #define DEFAULT_BLANKTIME	(5*60)		/* 5 minutes */
72 #define MAX_BLANKTIME		(7*24*60*60)	/* 7 days!? */
73 
74 #define KEYCODE_BS		0x0e		/* "<-- Backspace" key, XXX */
75 
76 static default_attr user_default = {
77     SC_NORM_ATTR << 8,
78     SC_NORM_REV_ATTR << 8,
79 };
80 
81 static default_attr kernel_default = {
82     SC_KERNEL_CONS_ATTR << 8,
83     SC_KERNEL_CONS_REV_ATTR << 8,
84 };
85 
86 static	int		sc_console_unit = -1;
87 static  scr_stat    	*sc_console;
88 static	struct tty	*sc_console_tty;
89 #ifndef SC_NO_SYSMOUSE
90 static	struct tty	*sc_mouse_tty;
91 #endif
92 static  term_stat   	kernel_console;
93 static  default_attr    *current_default;
94 
95 static  char        	init_done = COLD;
96 static  char		shutdown_in_progress = FALSE;
97 static	char		sc_malloc = FALSE;
98 
99 static	int		saver_mode = CONS_LKM_SAVER; /* LKM/user saver */
100 static	int		run_scrn_saver = FALSE;	/* should run the saver? */
101 static	long        	scrn_blank_time = 0;    /* screen saver timeout value */
102 #if NSPLASH > 0
103 static	int     	scrn_blanked;		/* # of blanked screen */
104 static	int		sticky_splash = FALSE;
105 
106 static	void		none_saver(sc_softc_t *sc, int blank) { }
107 static	void		(*current_saver)(sc_softc_t *, int) = none_saver;
108 #endif
109 
110 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
111 #include "font.h"
112 #endif
113 
114 	d_ioctl_t	*sc_user_ioctl;
115 
116 static	bios_values_t	bios_value;
117 
118 static	int		enable_panic_key;
119 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
120 	   0, "");
121 
122 #define SC_MOUSE 	128
123 #define SC_CONSOLECTL	255
124 
125 #define VIRTUAL_TTY(sc, x) (SC_DEV((sc), (x))->si_tty)
126 
127 static	int		debugger;
128 
129 /* prototypes */
130 static int scvidprobe(int unit, int flags, int cons);
131 static int sckbdprobe(int unit, int flags, int cons);
132 static void scmeminit(void *arg);
133 static int scdevtounit(dev_t dev);
134 static kbd_callback_func_t sckbdevent;
135 static int scparam(struct tty *tp, struct termios *t);
136 static void scstart(struct tty *tp);
137 static void scmousestart(struct tty *tp);
138 static void scinit(int unit, int flags);
139 #if __i386__
140 static void scterm(int unit, int flags);
141 #endif
142 static void scshutdown(void *arg, int howto);
143 static u_int scgetc(sc_softc_t *sc, u_int flags);
144 #define SCGETC_CN	1
145 #define SCGETC_NONBLOCK	2
146 static int sccngetch(int flags);
147 static void sccnupdate(scr_stat *scp);
148 static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
149 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
150 static timeout_t scrn_timer;
151 static int and_region(int *s1, int *e1, int s2, int e2);
152 static void scrn_update(scr_stat *scp, int show_cursor);
153 
154 #if NSPLASH > 0
155 static int scsplash_callback(int event, void *arg);
156 static void scsplash_saver(sc_softc_t *sc, int show);
157 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
158 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
159 static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
160 static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
161 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
162 static int wait_scrn_saver_stop(sc_softc_t *sc);
163 #define scsplash_stick(stick)		(sticky_splash = (stick))
164 #else /* !NSPLASH */
165 #define scsplash_stick(stick)
166 #endif /* NSPLASH */
167 
168 static int switch_scr(sc_softc_t *sc, u_int next_scr);
169 static int do_switch_scr(sc_softc_t *sc, int s);
170 static int vt_proc_alive(scr_stat *scp);
171 static int signal_vt_rel(scr_stat *scp);
172 static int signal_vt_acq(scr_stat *scp);
173 static void exchange_scr(sc_softc_t *sc);
174 static void scan_esc(scr_stat *scp, u_char c);
175 static void ansi_put(scr_stat *scp, u_char *buf, int len);
176 static void draw_cursor_image(scr_stat *scp);
177 static void remove_cursor_image(scr_stat *scp);
178 static void update_cursor_image(scr_stat *scp);
179 static void move_crsr(scr_stat *scp, int x, int y);
180 static int mask2attr(struct term_stat *term);
181 static int save_kbd_state(scr_stat *scp);
182 static int update_kbd_state(scr_stat *scp, int state, int mask);
183 static int update_kbd_leds(scr_stat *scp, int which);
184 static void do_bell(scr_stat *scp, int pitch, int duration);
185 static timeout_t blink_screen;
186 
187 #define	CDEV_MAJOR	12
188 
189 static cn_probe_t	sccnprobe;
190 static cn_init_t	sccninit;
191 static cn_getc_t	sccngetc;
192 static cn_checkc_t	sccncheckc;
193 static cn_putc_t	sccnputc;
194 static cn_dbctl_t	sccndbctl;
195 static cn_term_t	sccnterm;
196 
197 #if __alpha__
198 void sccnattach(void);
199 #endif
200 
201 CONS_DRIVER(sc, sccnprobe, sccninit, sccnterm, sccngetc, sccncheckc, sccnputc,
202 	    sccndbctl);
203 
204 static	d_open_t	scopen;
205 static	d_close_t	scclose;
206 static	d_read_t	scread;
207 static	d_ioctl_t	scioctl;
208 static	d_mmap_t	scmmap;
209 
210 static struct cdevsw sc_cdevsw = {
211 	/* open */	scopen,
212 	/* close */	scclose,
213 	/* read */	scread,
214 	/* write */	ttywrite,
215 	/* ioctl */	scioctl,
216 	/* poll */	ttypoll,
217 	/* mmap */	scmmap,
218 	/* strategy */	nostrategy,
219 	/* name */	"sc",
220 	/* maj */	CDEV_MAJOR,
221 	/* dump */	nodump,
222 	/* psize */	nopsize,
223 	/* flags */	D_TTY,
224 	/* bmaj */	-1
225 };
226 
227 int
228 sc_probe_unit(int unit, int flags)
229 {
230     if (!scvidprobe(unit, flags, FALSE)) {
231 	if (bootverbose)
232 	    printf("sc%d: no video adapter is found.\n", unit);
233 	return ENXIO;
234     }
235 
236     /* syscons will be attached even when there is no keyboard */
237     sckbdprobe(unit, flags, FALSE);
238 
239     return 0;
240 }
241 
242 /* probe video adapters, return TRUE if found */
243 static int
244 scvidprobe(int unit, int flags, int cons)
245 {
246     /*
247      * Access the video adapter driver through the back door!
248      * Video adapter drivers need to be configured before syscons.
249      * However, when syscons is being probed as the low-level console,
250      * they have not been initialized yet.  We force them to initialize
251      * themselves here. XXX
252      */
253     vid_configure(cons ? VIO_PROBE_ONLY : 0);
254 
255     return (vid_find_adapter("*", unit) >= 0);
256 }
257 
258 /* probe the keyboard, return TRUE if found */
259 static int
260 sckbdprobe(int unit, int flags, int cons)
261 {
262     /* access the keyboard driver through the backdoor! */
263     kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
264 
265     return (kbd_find_keyboard("*", unit) >= 0);
266 }
267 
268 static char
269 *adapter_name(video_adapter_t *adp)
270 {
271     static struct {
272 	int type;
273 	char *name[2];
274     } names[] = {
275 	{ KD_MONO,	{ "MDA",	"MDA" } },
276 	{ KD_HERCULES,	{ "Hercules",	"Hercules" } },
277 	{ KD_CGA,	{ "CGA",	"CGA" } },
278 	{ KD_EGA,	{ "EGA",	"EGA (mono)" } },
279 	{ KD_VGA,	{ "VGA",	"VGA (mono)" } },
280 	{ KD_PC98,	{ "PC-98x1",	"PC-98x1" } },
281 	{ KD_TGA,	{ "TGA",	"TGA" } },
282 	{ -1,		{ "Unknown",	"Unknown" } },
283     };
284     int i;
285 
286     for (i = 0; names[i].type != -1; ++i)
287 	if (names[i].type == adp->va_type)
288 	    break;
289     return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
290 }
291 
292 int
293 sc_attach_unit(int unit, int flags)
294 {
295     sc_softc_t *sc;
296     scr_stat *scp;
297 #ifdef SC_PIXEL_MODE
298     video_info_t info;
299 #endif
300     int vc;
301     dev_t dev;
302 
303     scmeminit(NULL);		/* XXX */
304 
305     flags &= ~SC_KERNEL_CONSOLE;
306     if (sc_console_unit == unit)
307 	flags |= SC_KERNEL_CONSOLE;
308     scinit(unit, flags);
309     sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
310     sc->config = flags;
311     scp = SC_STAT(sc->dev[0]);
312     if (sc_console == NULL)	/* sc_console_unit < 0 */
313 	sc_console = scp;
314 
315 #ifdef SC_PIXEL_MODE
316     if ((sc->config & SC_VESA800X600)
317 	&& ((*vidsw[sc->adapter]->get_info)(sc->adp, M_VESA_800x600, &info) == 0)) {
318 #if NSPLASH > 0
319 	if (sc->flags & SC_SPLASH_SCRN)
320 	    splash_term(sc->adp);
321 #endif
322 	sc_set_graphics_mode(scp, NULL, M_VESA_800x600);
323 	sc_set_pixel_mode(scp, NULL, COL, ROW, 16);
324 	sc->initial_mode = M_VESA_800x600;
325 #if NSPLASH > 0
326 	/* put up the splash again! */
327 	if (sc->flags & SC_SPLASH_SCRN)
328     	    splash_init(sc->adp, scsplash_callback, sc);
329 #endif
330     }
331 #endif /* SC_PIXEL_MODE */
332 
333     /* initialize cursor */
334     if (!ISGRAPHSC(scp))
335     	update_cursor_image(scp);
336 
337     /* get screen update going */
338     scrn_timer(sc);
339 
340     /* set up the keyboard */
341     kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
342     update_kbd_state(scp, scp->status, LOCK_MASK);
343 
344     printf("sc%d: %s <%d virtual consoles, flags=0x%x>\n",
345 	   unit, adapter_name(sc->adp), sc->vtys, sc->config);
346     if (bootverbose) {
347 	printf("sc%d:", unit);
348     	if (sc->adapter >= 0)
349 	    printf(" fb%d", sc->adapter);
350 	if (sc->keyboard >= 0)
351 	    printf(" kbd%d", sc->keyboard);
352 	printf("\n");
353     }
354 
355     /* register a shutdown callback for the kernel console */
356     if (sc_console_unit == unit)
357 	EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown,
358 			      (void *)(uintptr_t)unit, SHUTDOWN_PRI_DEFAULT);
359 
360     for (vc = 0; vc < sc->vtys; vc++) {
361 	dev = make_dev(&sc_cdevsw, vc + unit * MAXCONS,
362 	    UID_ROOT, GID_WHEEL, 0600, "ttyv%r", vc + unit * MAXCONS);
363 	sc->dev[vc] = dev;
364 	/*
365 	 * The first vty already has struct tty and scr_stat initialized
366 	 * in scinit().  The other vtys will have these structs when
367 	 * first opened.
368 	 */
369     }
370 
371 #ifndef SC_NO_SYSMOUSE
372     dev = make_dev(&sc_cdevsw, SC_MOUSE,
373 		   UID_ROOT, GID_WHEEL, 0600, "sysmouse");
374     dev->si_tty = sc_mouse_tty = ttymalloc(sc_mouse_tty);
375     /* sysmouse doesn't have scr_stat */
376 #endif /* SC_NO_SYSMOUSE */
377     dev = make_dev(&sc_cdevsw, SC_CONSOLECTL,
378 		   UID_ROOT, GID_WHEEL, 0600, "consolectl");
379     dev->si_tty = sc_console_tty = ttymalloc(sc_console_tty);
380     SC_STAT(dev) = sc_console;
381 
382     return 0;
383 }
384 
385 static void
386 scmeminit(void *arg)
387 {
388     if (sc_malloc)
389 	return;
390     sc_malloc = TRUE;
391 
392     /*
393      * As soon as malloc() becomes functional, we had better allocate
394      * various buffers for the kernel console.
395      */
396 
397     if (sc_console_unit < 0)
398 	return;
399 
400     /* copy the temporary buffer to the final buffer */
401     sc_alloc_scr_buffer(sc_console, FALSE, FALSE);
402 
403 #ifndef SC_NO_CUTPASTE
404     /* cut buffer is available only when the mouse pointer is used */
405     if (ISMOUSEAVAIL(sc_console->sc->adp->va_flags))
406 	sc_alloc_cut_buffer(sc_console, FALSE);
407 #endif
408 
409 #ifndef SC_NO_HISTORY
410     /* initialize history buffer & pointers */
411     sc_alloc_history_buffer(sc_console, 0, 0, FALSE);
412 #endif
413 }
414 
415 /* XXX */
416 SYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL);
417 
418 int
419 sc_resume_unit(int unit)
420 {
421     /* XXX should be moved to the keyboard driver? */
422     sc_softc_t *sc;
423 
424     sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
425     if (sc->kbd != NULL)
426 	kbd_clear_state(sc->kbd);
427     return 0;
428 }
429 
430 static int
431 scdevtounit(dev_t dev)
432 {
433     int vty = SC_VTY(dev);
434 
435     if (vty == SC_CONSOLECTL)
436 	return ((sc_console != NULL) ? sc_console->sc->unit : -1);
437     else if (vty == SC_MOUSE)
438 	return -1;
439     else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
440 	return -1;
441     else
442 	return vty/MAXCONS;
443 }
444 
445 int
446 scopen(dev_t dev, int flag, int mode, struct proc *p)
447 {
448     int unit = scdevtounit(dev);
449     sc_softc_t *sc;
450     struct tty *tp;
451     scr_stat *scp;
452     keyarg_t key;
453     int error;
454 
455     DPRINTF(5, ("scopen: dev:%d,%d, unit:%d, vty:%d\n",
456 		major(dev), minor(dev), unit, SC_VTY(dev)));
457 
458     /* sc == NULL, if SC_VTY(dev) == SC_MOUSE */
459     sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
460 #ifndef SC_NO_SYSMOUSE
461     if ((SC_VTY(dev) != SC_MOUSE) && (sc == NULL))
462 #else
463     if (sc == NULL)
464 #endif
465 	return ENXIO;
466 
467     tp = dev->si_tty = ttymalloc(dev->si_tty);
468     tp->t_oproc = (SC_VTY(dev) == SC_MOUSE) ? scmousestart : scstart;
469     tp->t_param = scparam;
470     tp->t_stop = nottystop;
471     tp->t_dev = dev;
472     if (!(tp->t_state & TS_ISOPEN)) {
473 	ttychars(tp);
474         /* Use the current setting of the <-- key as default VERASE. */
475         /* If the Delete key is preferable, an stty is necessary     */
476 	if (sc != NULL) {
477 	    key.keynum = KEYCODE_BS;
478 	    kbd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
479             tp->t_cc[VERASE] = key.key.map[0];
480 	}
481 	tp->t_iflag = TTYDEF_IFLAG;
482 	tp->t_oflag = TTYDEF_OFLAG;
483 	tp->t_cflag = TTYDEF_CFLAG;
484 	tp->t_lflag = TTYDEF_LFLAG;
485 	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
486 	scparam(tp, &tp->t_termios);
487 	(*linesw[tp->t_line].l_modem)(tp, 1);
488 #ifndef SC_NO_SYSMOUSE
489     	if (SC_VTY(dev) == SC_MOUSE)
490 	    sc_mouse_set_level(0);	/* XXX */
491 #endif
492     }
493     else
494 	if (tp->t_state & TS_XCLUDE && suser(p))
495 	    return(EBUSY);
496 
497     error = (*linesw[tp->t_line].l_open)(dev, tp);
498 
499     if (SC_VTY(dev) != SC_MOUSE) {
500 	/* assert(sc != NULL) */
501 	scp = SC_STAT(dev);
502 	if (scp == NULL) {
503 	    scp = SC_STAT(dev) = alloc_scp(sc, SC_VTY(dev));
504 	    if (ISGRAPHSC(scp))
505 		sc_set_pixel_mode(scp, NULL, COL, ROW, 16);
506 	}
507 	if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
508 	    tp->t_winsize.ws_col = scp->xsize;
509 	    tp->t_winsize.ws_row = scp->ysize;
510 	}
511     }
512     return error;
513 }
514 
515 int
516 scclose(dev_t dev, int flag, int mode, struct proc *p)
517 {
518     struct tty *tp = dev->si_tty;
519     struct scr_stat *scp;
520     int s;
521 
522     if ((SC_VTY(dev) != SC_CONSOLECTL) && (SC_VTY(dev) != SC_MOUSE)) {
523 	scp = SC_STAT(tp->t_dev);
524 	/* were we in the middle of the VT switching process? */
525 	DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
526 	s = spltty();
527 	if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
528 	    cons_unavail = FALSE;
529 	if (scp->status & SWITCH_WAIT_REL) {
530 	    /* assert(scp == scp->sc->cur_scp) */
531 	    DPRINTF(5, ("reset WAIT_REL, "));
532 	    scp->status &= ~SWITCH_WAIT_REL;
533 	    do_switch_scr(scp->sc, s);
534 	}
535 	if (scp->status & SWITCH_WAIT_ACQ) {
536 	    /* assert(scp == scp->sc->cur_scp) */
537 	    DPRINTF(5, ("reset WAIT_ACQ, "));
538 	    scp->status &= ~SWITCH_WAIT_ACQ;
539 	    scp->sc->switch_in_progress = 0;
540 	}
541 #if not_yet_done
542 	if (scp == &main_console) {
543 	    scp->pid = 0;
544 	    scp->proc = NULL;
545 	    scp->smode.mode = VT_AUTO;
546 	}
547 	else {
548 	    sc_vtb_destroy(&scp->vtb);
549 	    sc_vtb_destroy(&scp->scr);
550 	    sc_free_history_buffer(scp, scp->ysize);
551 	    free(scp, M_DEVBUF);
552 	}
553 #else
554 	scp->pid = 0;
555 	scp->proc = NULL;
556 	scp->smode.mode = VT_AUTO;
557 #endif
558 	scp->kbd_mode = K_XLATE;
559 	if (scp == scp->sc->cur_scp)
560 	    kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
561 	DPRINTF(5, ("done.\n"));
562     }
563     spltty();
564     (*linesw[tp->t_line].l_close)(tp, flag);
565     ttyclose(tp);
566     spl0();
567     return(0);
568 }
569 
570 int
571 scread(dev_t dev, struct uio *uio, int flag)
572 {
573 
574     sc_touch_scrn_saver();
575     return(ttyread(dev, uio, flag));
576 }
577 
578 static int
579 sckbdevent(keyboard_t *thiskbd, int event, void *arg)
580 {
581     sc_softc_t *sc;
582     struct tty *cur_tty;
583     int c;
584     size_t len;
585     u_char *cp;
586 
587     sc = (sc_softc_t *)arg;
588     /* assert(thiskbd == sc->kbd) */
589 
590     switch (event) {
591     case KBDIO_KEYINPUT:
592 	break;
593     case KBDIO_UNLOADING:
594 	sc->kbd = NULL;
595 	sc->keyboard = -1;
596 	kbd_release(thiskbd, (void *)&sc->keyboard);
597 	return 0;
598     default:
599 	return EINVAL;
600     }
601 
602     /*
603      * Loop while there is still input to get from the keyboard.
604      * I don't think this is nessesary, and it doesn't fix
605      * the Xaccel-2.1 keyboard hang, but it can't hurt.		XXX
606      */
607     while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
608 
609 	cur_tty = VIRTUAL_TTY(sc, sc->cur_scp->index);
610 	/* XXX */
611 	if (!(cur_tty->t_state & TS_ISOPEN))
612 	    if (((cur_tty = sc_console_tty) == NULL)
613 	    	|| !(cur_tty->t_state & TS_ISOPEN))
614 		continue;
615 
616 	switch (KEYFLAGS(c)) {
617 	case 0x0000: /* normal key */
618 	    (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
619 	    break;
620 	case FKEY:  /* function key, return string */
621 	    cp = kbd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
622 	    if (cp != NULL) {
623 	    	while (len-- >  0)
624 		    (*linesw[cur_tty->t_line].l_rint)(*cp++, cur_tty);
625 	    }
626 	    break;
627 	case MKEY:  /* meta is active, prepend ESC */
628 	    (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
629 	    (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
630 	    break;
631 	case BKEY:  /* backtab fixed sequence (esc [ Z) */
632 	    (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
633 	    (*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
634 	    (*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
635 	    break;
636 	}
637     }
638 
639 #ifndef SC_NO_CUTPASTE
640     if (sc->cur_scp->status & MOUSE_VISIBLE) {
641 	sc_remove_mouse_image(sc->cur_scp);
642 	sc->cur_scp->status &= ~MOUSE_VISIBLE;
643     }
644 #endif /* SC_NO_CUTPASTE */
645 
646     return 0;
647 }
648 
649 static int
650 scparam(struct tty *tp, struct termios *t)
651 {
652     tp->t_ispeed = t->c_ispeed;
653     tp->t_ospeed = t->c_ospeed;
654     tp->t_cflag = t->c_cflag;
655     return 0;
656 }
657 
658 int
659 scioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
660 {
661     int error;
662     int i;
663     struct tty *tp;
664     sc_softc_t *sc;
665     scr_stat *scp;
666     int s;
667 
668     tp = dev->si_tty;
669 
670     /* If there is a user_ioctl function call that first */
671     if (sc_user_ioctl) {
672 	error = (*sc_user_ioctl)(dev, cmd, data, flag, p);
673 	if (error != ENOIOCTL)
674 	    return error;
675     }
676 
677     error = sc_vid_ioctl(tp, cmd, data, flag, p);
678     if (error != ENOIOCTL)
679 	return error;
680 
681 #ifndef SC_NO_HISTORY
682     error = sc_hist_ioctl(tp, cmd, data, flag, p);
683     if (error != ENOIOCTL)
684 	return error;
685 #endif
686 
687 #ifndef SC_NO_SYSMOUSE
688     error = sc_mouse_ioctl(tp, cmd, data, flag, p);
689     if (error != ENOIOCTL)
690 	return error;
691     if (SC_VTY(dev) == SC_MOUSE) {
692 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
693 	if (error != ENOIOCTL)
694 	    return error;
695 	error = ttioctl(tp, cmd, data, flag);
696 	if (error != ENOIOCTL)
697 	    return error;
698 	return ENOTTY;
699     }
700 #endif
701 
702     scp = SC_STAT(tp->t_dev);
703     /* assert(scp != NULL) */
704     /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
705     sc = scp->sc;
706 
707     switch (cmd) {  		/* process console hardware related ioctl's */
708 
709     case GIO_ATTR:      	/* get current attributes */
710 	*(int*)data = (scp->term.cur_attr >> 8) & 0xFF;
711 	return 0;
712 
713     case GIO_COLOR:     	/* is this a color console ? */
714 	*(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
715 	return 0;
716 
717     case CONS_BLANKTIME:    	/* set screen saver timeout (0 = no saver) */
718 	if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
719             return EINVAL;
720 	s = spltty();
721 	scrn_blank_time = *(int *)data;
722 	run_scrn_saver = (scrn_blank_time != 0);
723 	splx(s);
724 	return 0;
725 
726     case CONS_CURSORTYPE:   	/* set cursor type blink/noblink */
727 	if (!ISGRAPHSC(sc->cur_scp))
728 	    remove_cursor_image(sc->cur_scp);
729 	if ((*(int*)data) & 0x01)
730 	    sc->flags |= SC_BLINK_CURSOR;
731 	else
732 	    sc->flags &= ~SC_BLINK_CURSOR;
733 	if ((*(int*)data) & 0x02) {
734 	    sc->flags |= SC_CHAR_CURSOR;
735 	} else
736 	    sc->flags &= ~SC_CHAR_CURSOR;
737 	/*
738 	 * The cursor shape is global property; all virtual consoles
739 	 * are affected. Update the cursor in the current console...
740 	 */
741 	if (!ISGRAPHSC(sc->cur_scp)) {
742 	    s = spltty();
743 	    sc_set_cursor_image(sc->cur_scp);
744 	    draw_cursor_image(sc->cur_scp);
745 	    splx(s);
746 	}
747 	return 0;
748 
749     case CONS_BELLTYPE: 	/* set bell type sound/visual */
750 	if ((*(int *)data) & 0x01)
751 	    sc->flags |= SC_VISUAL_BELL;
752 	else
753 	    sc->flags &= ~SC_VISUAL_BELL;
754 	if ((*(int *)data) & 0x02)
755 	    sc->flags |= SC_QUIET_BELL;
756 	else
757 	    sc->flags &= ~SC_QUIET_BELL;
758 	return 0;
759 
760     case CONS_GETINFO:  	/* get current (virtual) console info */
761     {
762 	vid_info_t *ptr = (vid_info_t*)data;
763 	if (ptr->size == sizeof(struct vid_info)) {
764 	    ptr->m_num = sc->cur_scp->index;
765 	    ptr->mv_col = scp->xpos;
766 	    ptr->mv_row = scp->ypos;
767 	    ptr->mv_csz = scp->xsize;
768 	    ptr->mv_rsz = scp->ysize;
769 	    ptr->mv_norm.fore = (scp->term.std_color & 0x0f00)>>8;
770 	    ptr->mv_norm.back = (scp->term.std_color & 0xf000)>>12;
771 	    ptr->mv_rev.fore = (scp->term.rev_color & 0x0f00)>>8;
772 	    ptr->mv_rev.back = (scp->term.rev_color & 0xf000)>>12;
773 	    ptr->mv_grfc.fore = 0;      /* not supported */
774 	    ptr->mv_grfc.back = 0;      /* not supported */
775 	    ptr->mv_ovscan = scp->border;
776 	    if (scp == sc->cur_scp)
777 		save_kbd_state(scp);
778 	    ptr->mk_keylock = scp->status & LOCK_MASK;
779 	    return 0;
780 	}
781 	return EINVAL;
782     }
783 
784     case CONS_GETVERS:  	/* get version number */
785 	*(int*)data = 0x200;    /* version 2.0 */
786 	return 0;
787 
788     case CONS_IDLE:		/* see if the screen has been idle */
789 	/*
790 	 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
791 	 * the user process may have been writing something on the
792 	 * screen and syscons is not aware of it. Declare the screen
793 	 * is NOT idle if it is in one of these modes. But there is
794 	 * an exception to it; if a screen saver is running in the
795 	 * graphics mode in the current screen, we should say that the
796 	 * screen has been idle.
797 	 */
798 	*(int *)data = (sc->flags & SC_SCRN_IDLE)
799 		       && (!ISGRAPHSC(sc->cur_scp)
800 			   || (sc->cur_scp->status & SAVER_RUNNING));
801 	return 0;
802 
803     case CONS_SAVERMODE:	/* set saver mode */
804 	switch(*(int *)data) {
805 	case CONS_USR_SAVER:
806 	    /* if a LKM screen saver is running, stop it first. */
807 	    scsplash_stick(FALSE);
808 	    saver_mode = *(int *)data;
809 	    s = spltty();
810 #if NSPLASH > 0
811 	    if ((error = wait_scrn_saver_stop(NULL))) {
812 		splx(s);
813 		return error;
814 	    }
815 #endif /* NSPLASH */
816 	    run_scrn_saver = TRUE;
817 	    scp->status |= SAVER_RUNNING;
818 	    scsplash_stick(TRUE);
819 	    splx(s);
820 	    break;
821 	case CONS_LKM_SAVER:
822 	    s = spltty();
823 	    if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
824 		scp->status &= ~SAVER_RUNNING;
825 	    saver_mode = *(int *)data;
826 	    splx(s);
827 	    break;
828 	default:
829 	    return EINVAL;
830 	}
831 	return 0;
832 
833     case CONS_SAVERSTART:	/* immediately start/stop the screen saver */
834 	/*
835 	 * Note that this ioctl does not guarantee the screen saver
836 	 * actually starts or stops. It merely attempts to do so...
837 	 */
838 	s = spltty();
839 	run_scrn_saver = (*(int *)data != 0);
840 	if (run_scrn_saver)
841 	    sc->scrn_time_stamp -= scrn_blank_time;
842 	splx(s);
843 	return 0;
844 
845     case VT_SETMODE:    	/* set screen switcher mode */
846     {
847 	struct vt_mode *mode;
848 
849 	mode = (struct vt_mode *)data;
850 	DPRINTF(5, ("sc%d: VT_SETMODE ", sc->unit));
851 	if (scp->smode.mode == VT_PROCESS) {
852     	    if (scp->proc == pfind(scp->pid) && scp->proc != p) {
853 		DPRINTF(5, ("error EPERM\n"));
854 		return EPERM;
855 	    }
856 	}
857 	s = spltty();
858 	if (mode->mode == VT_AUTO) {
859 	    scp->smode.mode = VT_AUTO;
860 	    scp->proc = NULL;
861 	    scp->pid = 0;
862 	    DPRINTF(5, ("VT_AUTO, "));
863 	    if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
864 		cons_unavail = FALSE;
865 	    /* were we in the middle of the vty switching process? */
866 	    if (scp->status & SWITCH_WAIT_REL) {
867 		/* assert(scp == scp->sc->cur_scp) */
868 		DPRINTF(5, ("reset WAIT_REL, "));
869 		scp->status &= ~SWITCH_WAIT_REL;
870 		s = do_switch_scr(sc, s);
871 	    }
872 	    if (scp->status & SWITCH_WAIT_ACQ) {
873 		/* assert(scp == scp->sc->cur_scp) */
874 		DPRINTF(5, ("reset WAIT_ACQ, "));
875 		scp->status &= ~SWITCH_WAIT_ACQ;
876 		sc->switch_in_progress = 0;
877 	    }
878 	} else {
879 	    if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
880 		|| !ISSIGVALID(mode->frsig)) {
881 		splx(s);
882 		DPRINTF(5, ("error EINVAL\n"));
883 		return EINVAL;
884 	    }
885 	    DPRINTF(5, ("VT_PROCESS %d, ", p->p_pid));
886 	    bcopy(data, &scp->smode, sizeof(struct vt_mode));
887 	    scp->proc = p;
888 	    scp->pid = scp->proc->p_pid;
889 	    if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
890 		cons_unavail = TRUE;
891 	}
892 	splx(s);
893 	DPRINTF(5, ("\n"));
894 	return 0;
895     }
896 
897     case VT_GETMODE:    	/* get screen switcher mode */
898 	bcopy(&scp->smode, data, sizeof(struct vt_mode));
899 	return 0;
900 
901     case VT_RELDISP:    	/* screen switcher ioctl */
902 	s = spltty();
903 	/*
904 	 * This must be the current vty which is in the VT_PROCESS
905 	 * switching mode...
906 	 */
907 	if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
908 	    splx(s);
909 	    return EINVAL;
910 	}
911 	/* ...and this process is controlling it. */
912 	if (scp->proc != p) {
913 	    splx(s);
914 	    return EPERM;
915 	}
916 	error = EINVAL;
917 	switch(*(int *)data) {
918 	case VT_FALSE:  	/* user refuses to release screen, abort */
919 	    if ((scp == sc->old_scp) && (scp->status & SWITCH_WAIT_REL)) {
920 		sc->old_scp->status &= ~SWITCH_WAIT_REL;
921 		sc->switch_in_progress = 0;
922 		DPRINTF(5, ("sc%d: VT_FALSE\n", sc->unit));
923 		error = 0;
924 	    }
925 	    break;
926 
927 	case VT_TRUE:   	/* user has released screen, go on */
928 	    if ((scp == sc->old_scp) && (scp->status & SWITCH_WAIT_REL)) {
929 		scp->status &= ~SWITCH_WAIT_REL;
930 		s = do_switch_scr(sc, s);
931 		DPRINTF(5, ("sc%d: VT_TRUE\n", sc->unit));
932 		error = 0;
933 	    }
934 	    break;
935 
936 	case VT_ACKACQ: 	/* acquire acknowledged, switch completed */
937 	    if ((scp == sc->new_scp) && (scp->status & SWITCH_WAIT_ACQ)) {
938 		scp->status &= ~SWITCH_WAIT_ACQ;
939 		sc->switch_in_progress = 0;
940 		DPRINTF(5, ("sc%d: VT_ACKACQ\n", sc->unit));
941 		error = 0;
942 	    }
943 	    break;
944 
945 	default:
946 	    break;
947 	}
948 	splx(s);
949 	return error;
950 
951     case VT_OPENQRY:    	/* return free virtual console */
952 	for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
953 	    tp = VIRTUAL_TTY(sc, i);
954 	    if ((tp == NULL) || !(tp->t_state & TS_ISOPEN)) {
955 		*(int *)data = i + 1;
956 		return 0;
957 	    }
958 	}
959 	return EINVAL;
960 
961     case VT_ACTIVATE:   	/* switch to screen *data */
962 	s = spltty();
963 	sc_clean_up(sc->cur_scp);
964 	splx(s);
965 	return switch_scr(sc, *(int *)data - 1);
966 
967     case VT_WAITACTIVE: 	/* wait for switch to occur */
968 	if ((*(int *)data >= sc->first_vty + sc->vtys)
969 		|| (*(int *)data < sc->first_vty))
970 	    return EINVAL;
971 	s = spltty();
972 	error = sc_clean_up(sc->cur_scp);
973 	splx(s);
974 	if (error)
975 	    return error;
976 	if (*(int *)data != 0)
977 	    scp = SC_STAT(SC_DEV(sc, *(int *)data - 1));
978 	if (scp == scp->sc->cur_scp)
979 	    return 0;
980 	while ((error=tsleep((caddr_t)&scp->smode, PZERO|PCATCH,
981 			     "waitvt", 0)) == ERESTART) ;
982 	return error;
983 
984     case VT_GETACTIVE:		/* get active vty # */
985 	*(int *)data = sc->cur_scp->index + 1;
986 	return 0;
987 
988     case VT_GETINDEX:		/* get this vty # */
989 	*(int *)data = scp->index + 1;
990 	return 0;
991 
992     case KDENABIO:      	/* allow io operations */
993 	error = suser(p);
994 	if (error != 0)
995 	    return error;
996 	if (securelevel > 0)
997 	    return EPERM;
998 #ifdef __i386__
999 	p->p_md.md_regs->tf_eflags |= PSL_IOPL;
1000 #endif
1001 	return 0;
1002 
1003     case KDDISABIO:     	/* disallow io operations (default) */
1004 #ifdef __i386__
1005 	p->p_md.md_regs->tf_eflags &= ~PSL_IOPL;
1006 #endif
1007 	return 0;
1008 
1009     case KDSKBSTATE:    	/* set keyboard state (locks) */
1010 	if (*(int *)data & ~LOCK_MASK)
1011 	    return EINVAL;
1012 	scp->status &= ~LOCK_MASK;
1013 	scp->status |= *(int *)data;
1014 	if (scp == sc->cur_scp)
1015 	    update_kbd_state(scp, scp->status, LOCK_MASK);
1016 	return 0;
1017 
1018     case KDGKBSTATE:    	/* get keyboard state (locks) */
1019 	if (scp == sc->cur_scp)
1020 	    save_kbd_state(scp);
1021 	*(int *)data = scp->status & LOCK_MASK;
1022 	return 0;
1023 
1024     case KDGETREPEAT:      	/* get keyboard repeat & delay rates */
1025     case KDSETREPEAT:      	/* set keyboard repeat & delay rates (new) */
1026 	error = kbd_ioctl(sc->kbd, cmd, data);
1027 	if (error == ENOIOCTL)
1028 	    error = ENODEV;
1029 	return error;
1030 
1031     case KDSETRAD:      	/* set keyboard repeat & delay rates (old) */
1032 	if (*(int *)data & ~0x7f)
1033 	    return EINVAL;
1034 	error = kbd_ioctl(sc->kbd, cmd, data);
1035 	if (error == ENOIOCTL)
1036 	    error = ENODEV;
1037 	return error;
1038 
1039     case KDSKBMODE:     	/* set keyboard mode */
1040 	switch (*(int *)data) {
1041 	case K_XLATE:   	/* switch to XLT ascii mode */
1042 	case K_RAW: 		/* switch to RAW scancode mode */
1043 	case K_CODE: 		/* switch to CODE mode */
1044 	    scp->kbd_mode = *(int *)data;
1045 	    if (scp == sc->cur_scp)
1046 		kbd_ioctl(sc->kbd, cmd, data);
1047 	    return 0;
1048 	default:
1049 	    return EINVAL;
1050 	}
1051 	/* NOT REACHED */
1052 
1053     case KDGKBMODE:     	/* get keyboard mode */
1054 	*(int *)data = scp->kbd_mode;
1055 	return 0;
1056 
1057     case KDGKBINFO:
1058 	error = kbd_ioctl(sc->kbd, cmd, data);
1059 	if (error == ENOIOCTL)
1060 	    error = ENODEV;
1061 	return error;
1062 
1063     case KDMKTONE:      	/* sound the bell */
1064 	if (*(int*)data)
1065 	    do_bell(scp, (*(int*)data)&0xffff,
1066 		    (((*(int*)data)>>16)&0xffff)*hz/1000);
1067 	else
1068 	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
1069 	return 0;
1070 
1071     case KIOCSOUND:     	/* make tone (*data) hz */
1072 	if (scp == sc->cur_scp) {
1073 	    if (*(int *)data)
1074 		return sc_tone(*(int *)data);
1075 	    else
1076 		return sc_tone(0);
1077 	}
1078 	return 0;
1079 
1080     case KDGKBTYPE:     	/* get keyboard type */
1081 	error = kbd_ioctl(sc->kbd, cmd, data);
1082 	if (error == ENOIOCTL) {
1083 	    /* always return something? XXX */
1084 	    *(int *)data = 0;
1085 	}
1086 	return 0;
1087 
1088     case KDSETLED:      	/* set keyboard LED status */
1089 	if (*(int *)data & ~LED_MASK)	/* FIXME: LOCK_MASK? */
1090 	    return EINVAL;
1091 	scp->status &= ~LED_MASK;
1092 	scp->status |= *(int *)data;
1093 	if (scp == sc->cur_scp)
1094 	    update_kbd_leds(scp, scp->status);
1095 	return 0;
1096 
1097     case KDGETLED:      	/* get keyboard LED status */
1098 	if (scp == sc->cur_scp)
1099 	    save_kbd_state(scp);
1100 	*(int *)data = scp->status & LED_MASK;
1101 	return 0;
1102 
1103     case CONS_SETKBD: 		/* set the new keyboard */
1104 	{
1105 	    keyboard_t *newkbd;
1106 
1107 	    s = spltty();
1108 	    newkbd = kbd_get_keyboard(*(int *)data);
1109 	    if (newkbd == NULL) {
1110 		splx(s);
1111 		return EINVAL;
1112 	    }
1113 	    error = 0;
1114 	    if (sc->kbd != newkbd) {
1115 		i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1116 				 (void *)&sc->keyboard, sckbdevent, sc);
1117 		/* i == newkbd->kb_index */
1118 		if (i >= 0) {
1119 		    if (sc->kbd != NULL) {
1120 			save_kbd_state(sc->cur_scp);
1121 			kbd_release(sc->kbd, (void *)&sc->keyboard);
1122 		    }
1123 		    sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1124 		    sc->keyboard = i;
1125 		    kbd_ioctl(sc->kbd, KDSKBMODE,
1126 			      (caddr_t)&sc->cur_scp->kbd_mode);
1127 		    update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1128 				     LOCK_MASK);
1129 		} else {
1130 		    error = EPERM;	/* XXX */
1131 		}
1132 	    }
1133 	    splx(s);
1134 	    return error;
1135 	}
1136 
1137     case CONS_RELKBD: 		/* release the current keyboard */
1138 	s = spltty();
1139 	error = 0;
1140 	if (sc->kbd != NULL) {
1141 	    save_kbd_state(sc->cur_scp);
1142 	    error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1143 	    if (error == 0) {
1144 		sc->kbd = NULL;
1145 		sc->keyboard = -1;
1146 	    }
1147 	}
1148 	splx(s);
1149 	return error;
1150 
1151     case GIO_SCRNMAP:   	/* get output translation table */
1152 	bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1153 	return 0;
1154 
1155     case PIO_SCRNMAP:   	/* set output translation table */
1156 	bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1157 	for (i=0; i<sizeof(sc->scr_map); i++) {
1158 	    sc->scr_rmap[sc->scr_map[i]] = i;
1159 	}
1160 	return 0;
1161 
1162     case GIO_KEYMAP:		/* get keyboard translation table */
1163     case PIO_KEYMAP:		/* set keyboard translation table */
1164     case GIO_DEADKEYMAP:	/* get accent key translation table */
1165     case PIO_DEADKEYMAP:	/* set accent key translation table */
1166     case GETFKEY:		/* get function key string */
1167     case SETFKEY:		/* set function key string */
1168 	error = kbd_ioctl(sc->kbd, cmd, data);
1169 	if (error == ENOIOCTL)
1170 	    error = ENODEV;
1171 	return error;
1172 
1173 #ifndef SC_NO_FONT_LOADING
1174 
1175     case PIO_FONT8x8:   	/* set 8x8 dot font */
1176 	if (!ISFONTAVAIL(sc->adp->va_flags))
1177 	    return ENXIO;
1178 	bcopy(data, sc->font_8, 8*256);
1179 	sc->fonts_loaded |= FONT_8;
1180 	/*
1181 	 * FONT KLUDGE
1182 	 * Always use the font page #0. XXX
1183 	 * Don't load if the current font size is not 8x8.
1184 	 */
1185 	if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1186 	    copy_font(sc->cur_scp, LOAD, 8, sc->font_8);
1187 	return 0;
1188 
1189     case GIO_FONT8x8:   	/* get 8x8 dot font */
1190 	if (!ISFONTAVAIL(sc->adp->va_flags))
1191 	    return ENXIO;
1192 	if (sc->fonts_loaded & FONT_8) {
1193 	    bcopy(sc->font_8, data, 8*256);
1194 	    return 0;
1195 	}
1196 	else
1197 	    return ENXIO;
1198 
1199     case PIO_FONT8x14:  	/* set 8x14 dot font */
1200 	if (!ISFONTAVAIL(sc->adp->va_flags))
1201 	    return ENXIO;
1202 	bcopy(data, sc->font_14, 14*256);
1203 	sc->fonts_loaded |= FONT_14;
1204 	/*
1205 	 * FONT KLUDGE
1206 	 * Always use the font page #0. XXX
1207 	 * Don't load if the current font size is not 8x14.
1208 	 */
1209 	if (ISTEXTSC(sc->cur_scp)
1210 	    && (sc->cur_scp->font_size >= 14)
1211 	    && (sc->cur_scp->font_size < 16))
1212 	    copy_font(sc->cur_scp, LOAD, 14, sc->font_14);
1213 	return 0;
1214 
1215     case GIO_FONT8x14:  	/* get 8x14 dot font */
1216 	if (!ISFONTAVAIL(sc->adp->va_flags))
1217 	    return ENXIO;
1218 	if (sc->fonts_loaded & FONT_14) {
1219 	    bcopy(sc->font_14, data, 14*256);
1220 	    return 0;
1221 	}
1222 	else
1223 	    return ENXIO;
1224 
1225     case PIO_FONT8x16:  	/* set 8x16 dot font */
1226 	if (!ISFONTAVAIL(sc->adp->va_flags))
1227 	    return ENXIO;
1228 	bcopy(data, sc->font_16, 16*256);
1229 	sc->fonts_loaded |= FONT_16;
1230 	/*
1231 	 * FONT KLUDGE
1232 	 * Always use the font page #0. XXX
1233 	 * Don't load if the current font size is not 8x16.
1234 	 */
1235 	if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1236 	    copy_font(sc->cur_scp, LOAD, 16, sc->font_16);
1237 	return 0;
1238 
1239     case GIO_FONT8x16:  	/* get 8x16 dot font */
1240 	if (!ISFONTAVAIL(sc->adp->va_flags))
1241 	    return ENXIO;
1242 	if (sc->fonts_loaded & FONT_16) {
1243 	    bcopy(sc->font_16, data, 16*256);
1244 	    return 0;
1245 	}
1246 	else
1247 	    return ENXIO;
1248 
1249 #endif /* SC_NO_FONT_LOADING */
1250 
1251     default:
1252 	break;
1253     }
1254 
1255     error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1256     if (error != ENOIOCTL)
1257 	return(error);
1258     error = ttioctl(tp, cmd, data, flag);
1259     if (error != ENOIOCTL)
1260 	return(error);
1261     return(ENOTTY);
1262 }
1263 
1264 static void
1265 scstart(struct tty *tp)
1266 {
1267     struct clist *rbp;
1268     int s, len;
1269     u_char buf[PCBURST];
1270     scr_stat *scp = SC_STAT(tp->t_dev);
1271 
1272     if (scp->status & SLKED || scp->sc->blink_in_progress)
1273 	return;
1274     s = spltty();
1275     if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1276 	tp->t_state |= TS_BUSY;
1277 	rbp = &tp->t_outq;
1278 	while (rbp->c_cc) {
1279 	    len = q_to_b(rbp, buf, PCBURST);
1280 	    splx(s);
1281 	    ansi_put(scp, buf, len);
1282 	    s = spltty();
1283 	}
1284 	tp->t_state &= ~TS_BUSY;
1285 	ttwwakeup(tp);
1286     }
1287     splx(s);
1288 }
1289 
1290 static void
1291 scmousestart(struct tty *tp)
1292 {
1293     struct clist *rbp;
1294     int s;
1295     u_char buf[PCBURST];
1296 
1297     s = spltty();
1298     if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1299 	tp->t_state |= TS_BUSY;
1300 	rbp = &tp->t_outq;
1301 	while (rbp->c_cc) {
1302 	    q_to_b(rbp, buf, PCBURST);
1303 	}
1304 	tp->t_state &= ~TS_BUSY;
1305 	ttwwakeup(tp);
1306     }
1307     splx(s);
1308 }
1309 
1310 static void
1311 sccnprobe(struct consdev *cp)
1312 {
1313 #if __i386__
1314     int unit;
1315     int flags;
1316 
1317     cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1318 
1319     /* a video card is always required */
1320     if (!scvidprobe(unit, flags, TRUE))
1321 	cp->cn_pri = CN_DEAD;
1322 
1323     /* syscons will become console even when there is no keyboard */
1324     sckbdprobe(unit, flags, TRUE);
1325 
1326     if (cp->cn_pri == CN_DEAD)
1327 	return;
1328 
1329     /* initialize required fields */
1330     cp->cn_dev = makedev(CDEV_MAJOR, SC_CONSOLECTL);
1331 #endif /* __i386__ */
1332 
1333 #if __alpha__
1334     /*
1335      * alpha use sccnattach() rather than cnprobe()/cninit()/cnterm()
1336      * interface to install the console.  Always return CN_DEAD from
1337      * here.
1338      */
1339     cp->cn_pri = CN_DEAD;
1340 #endif /* __alpha__ */
1341 }
1342 
1343 static void
1344 sccninit(struct consdev *cp)
1345 {
1346 #if __i386__
1347     int unit;
1348     int flags;
1349 
1350     sc_get_cons_priority(&unit, &flags);
1351     scinit(unit, flags | SC_KERNEL_CONSOLE);
1352     sc_console_unit = unit;
1353     sc_console = SC_STAT(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1354 #endif /* __i386__ */
1355 
1356 #if __alpha__
1357     /* SHOULDN'T REACH HERE */
1358 #endif /* __alpha__ */
1359 }
1360 
1361 static void
1362 sccnterm(struct consdev *cp)
1363 {
1364     /* we are not the kernel console any more, release everything */
1365 
1366     if (sc_console_unit < 0)
1367 	return;			/* shouldn't happen */
1368 
1369 #if __i386__
1370 #if 0 /* XXX */
1371     sc_clear_screen(sc_console);
1372     sccnupdate(sc_console);
1373 #endif
1374     scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1375     sc_console_unit = -1;
1376     sc_console = NULL;
1377 #endif /* __i386__ */
1378 
1379 #if __alpha__
1380     /* do nothing XXX */
1381 #endif /* __alpha__ */
1382 }
1383 
1384 #ifdef __alpha__
1385 
1386 void
1387 sccnattach(void)
1388 {
1389     static struct consdev consdev;
1390     int unit;
1391     int flags;
1392 
1393     bcopy(&sc_consdev, &consdev, sizeof(sc_consdev));
1394     consdev.cn_pri = sc_get_cons_priority(&unit, &flags);
1395 
1396     /* a video card is always required */
1397     if (!scvidprobe(unit, flags, TRUE))
1398 	consdev.cn_pri = CN_DEAD;
1399 
1400     /* alpha doesn't allow the console being without a keyboard... Why? */
1401     if (!sckbdprobe(unit, flags, TRUE))
1402 	consdev.cn_pri = CN_DEAD;
1403 
1404     if (consdev.cn_pri == CN_DEAD)
1405 	return;
1406 
1407     scinit(unit, flags | SC_KERNEL_CONSOLE);
1408     sc_console_unit = unit;
1409     sc_console = SC_STAT(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1410     consdev.cn_dev = makedev(CDEV_MAJOR, 0);
1411     cn_tab = &consdev;
1412 }
1413 
1414 #endif /* __alpha__ */
1415 
1416 static void
1417 sccnputc(dev_t dev, int c)
1418 {
1419     u_char buf[1];
1420     scr_stat *scp = sc_console;
1421     term_stat save = scp->term;
1422 #ifndef SC_NO_HISTORY
1423     struct tty *tp;
1424 #endif /* !SC_NO_HISTORY */
1425     int s;
1426 
1427     /* assert(sc_console != NULL) */
1428 
1429 #ifndef SC_NO_HISTORY
1430     if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1431 	scp->status &= ~SLKED;
1432 	update_kbd_state(scp, scp->status, SLKED);
1433 	if (scp->status & BUFFER_SAVED) {
1434 	    if (!sc_hist_restore(scp))
1435 		sc_remove_cutmarking(scp);
1436 	    scp->status &= ~BUFFER_SAVED;
1437 	    scp->status |= CURSOR_ENABLED;
1438 	    draw_cursor_image(scp);
1439 	}
1440 	tp = VIRTUAL_TTY(scp->sc, scp->index);
1441 	if (tp->t_state & TS_ISOPEN)
1442 	    scstart(tp);
1443     }
1444 #endif /* !SC_NO_HISTORY */
1445 
1446     scp->term = kernel_console;
1447     current_default = &kernel_default;
1448     buf[0] = c;
1449     ansi_put(scp, buf, 1);
1450     kernel_console = scp->term;
1451     current_default = &user_default;
1452     scp->term = save;
1453 
1454     s = spltty();	/* block sckbdevent and scrn_timer */
1455     sccnupdate(scp);
1456     splx(s);
1457 }
1458 
1459 static int
1460 sccngetc(dev_t dev)
1461 {
1462     return sccngetch(0);
1463 }
1464 
1465 static int
1466 sccncheckc(dev_t dev)
1467 {
1468     return sccngetch(SCGETC_NONBLOCK);
1469 }
1470 
1471 static void
1472 sccndbctl(dev_t dev, int on)
1473 {
1474     /* try to switch to the kernel console screen */
1475     if (on && debugger == 0) {
1476 	/*
1477 	 * TRY to make sure the screen saver is stopped,
1478 	 * and the screen is updated before switching to
1479 	 * the vty0.
1480 	 */
1481 	scrn_timer(NULL);
1482 	if (!cold
1483 	    && sc_console->sc->cur_scp->smode.mode == VT_AUTO
1484 	    && sc_console->smode.mode == VT_AUTO)
1485 	    switch_scr(sc_console->sc, sc_console->index);
1486     }
1487     if (on)
1488 	++debugger;
1489     else
1490 	--debugger;
1491 }
1492 
1493 static int
1494 sccngetch(int flags)
1495 {
1496     static struct fkeytab fkey;
1497     static int fkeycp;
1498     scr_stat *scp;
1499     u_char *p;
1500     int cur_mode;
1501     int s = spltty();	/* block sckbdevent and scrn_timer while we poll */
1502     int c;
1503 
1504     /* assert(sc_console != NULL) */
1505 
1506     /*
1507      * Stop the screen saver and update the screen if necessary.
1508      * What if we have been running in the screen saver code... XXX
1509      */
1510     sc_touch_scrn_saver();
1511     scp = sc_console->sc->cur_scp;	/* XXX */
1512     sccnupdate(scp);
1513 
1514     if (fkeycp < fkey.len) {
1515 	splx(s);
1516 	return fkey.str[fkeycp++];
1517     }
1518 
1519     if (scp->sc->kbd == NULL) {
1520 	splx(s);
1521 	return -1;
1522     }
1523 
1524     /*
1525      * Make sure the keyboard is accessible even when the kbd device
1526      * driver is disabled.
1527      */
1528     kbd_enable(scp->sc->kbd);
1529 
1530     /* we shall always use the keyboard in the XLATE mode here */
1531     cur_mode = scp->kbd_mode;
1532     scp->kbd_mode = K_XLATE;
1533     kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1534 
1535     kbd_poll(scp->sc->kbd, TRUE);
1536     c = scgetc(scp->sc, SCGETC_CN | flags);
1537     kbd_poll(scp->sc->kbd, FALSE);
1538 
1539     scp->kbd_mode = cur_mode;
1540     kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1541     kbd_disable(scp->sc->kbd);
1542     splx(s);
1543 
1544     switch (KEYFLAGS(c)) {
1545     case 0:	/* normal char */
1546 	return KEYCHAR(c);
1547     case FKEY:	/* function key */
1548 	p = kbd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
1549 	fkey.len = fkeycp;
1550 	if ((p != NULL) && (fkey.len > 0)) {
1551 	    bcopy(p, fkey.str, fkey.len);
1552 	    fkeycp = 1;
1553 	    return fkey.str[0];
1554 	}
1555 	return c;	/* XXX */
1556     case NOKEY:
1557     case ERRKEY:
1558     default:
1559 	return -1;
1560     }
1561     /* NOT REACHED */
1562 }
1563 
1564 static void
1565 sccnupdate(scr_stat *scp)
1566 {
1567     /* this is a cut-down version of scrn_timer()... */
1568 
1569     if (scp->sc->font_loading_in_progress || scp->sc->videoio_in_progress)
1570 	return;
1571 
1572     if (debugger > 0 || panicstr || shutdown_in_progress) {
1573 	sc_touch_scrn_saver();
1574     } else if (scp != scp->sc->cur_scp) {
1575 	return;
1576     }
1577 
1578     if (!run_scrn_saver)
1579 	scp->sc->flags &= ~SC_SCRN_IDLE;
1580 #if NSPLASH > 0
1581     if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
1582 	if (scp->sc->flags & SC_SCRN_BLANKED)
1583             stop_scrn_saver(scp->sc, current_saver);
1584 #endif /* NSPLASH */
1585 
1586     if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
1587 	|| scp->sc->switch_in_progress)
1588 	return;
1589     /*
1590      * FIXME: unlike scrn_timer(), we call scrn_update() from here even
1591      * when write_in_progress is non-zero.  XXX
1592      */
1593 
1594     if (!ISGRAPHSC(scp) && !(scp->status & SAVER_RUNNING))
1595 	scrn_update(scp, TRUE);
1596 }
1597 
1598 static void
1599 scrn_timer(void *arg)
1600 {
1601     static int kbd_interval = 0;
1602     struct timeval tv;
1603     sc_softc_t *sc;
1604     scr_stat *scp;
1605     int again;
1606     int s;
1607 
1608     again = (arg != NULL);
1609     if (arg != NULL)
1610 	sc = (sc_softc_t *)arg;
1611     else if (sc_console != NULL)
1612 	sc = sc_console->sc;
1613     else
1614 	return;
1615 
1616     /* don't do anything when we are performing some I/O operations */
1617     if (sc->font_loading_in_progress || sc->videoio_in_progress) {
1618 	if (again)
1619 	    timeout(scrn_timer, sc, hz / 10);
1620 	return;
1621     }
1622     s = spltty();
1623 
1624     if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
1625 	/* try to allocate a keyboard automatically */
1626 	if (++kbd_interval >= 25) {
1627 	    sc->keyboard = kbd_allocate("*", -1, (void *)&sc->keyboard,
1628 					sckbdevent, sc);
1629 	    if (sc->keyboard >= 0) {
1630 		sc->kbd = kbd_get_keyboard(sc->keyboard);
1631 		kbd_ioctl(sc->kbd, KDSKBMODE,
1632 			  (caddr_t)&sc->cur_scp->kbd_mode);
1633 		update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1634 				 LOCK_MASK);
1635 	    }
1636 	    kbd_interval = 0;
1637 	}
1638     }
1639 
1640     /* find the vty to update */
1641     scp = sc->cur_scp;
1642 
1643     /* should we stop the screen saver? */
1644     getmicrouptime(&tv);
1645     if (debugger > 0 || panicstr || shutdown_in_progress)
1646 	sc_touch_scrn_saver();
1647     if (run_scrn_saver) {
1648 	if (tv.tv_sec > sc->scrn_time_stamp + scrn_blank_time)
1649 	    sc->flags |= SC_SCRN_IDLE;
1650 	else
1651 	    sc->flags &= ~SC_SCRN_IDLE;
1652     } else {
1653 	sc->scrn_time_stamp = tv.tv_sec;
1654 	sc->flags &= ~SC_SCRN_IDLE;
1655 	if (scrn_blank_time > 0)
1656 	    run_scrn_saver = TRUE;
1657     }
1658 #if NSPLASH > 0
1659     if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
1660 	if (sc->flags & SC_SCRN_BLANKED)
1661             stop_scrn_saver(sc, current_saver);
1662 #endif /* NSPLASH */
1663 
1664     /* should we just return ? */
1665     if (sc->blink_in_progress || sc->switch_in_progress
1666 	|| sc->write_in_progress) {
1667 	if (again)
1668 	    timeout(scrn_timer, sc, hz / 10);
1669 	splx(s);
1670 	return;
1671     }
1672 
1673     /* Update the screen */
1674     scp = sc->cur_scp;		/* cur_scp may have changed... */
1675     if (!ISGRAPHSC(scp) && !(scp->status & SAVER_RUNNING))
1676 	scrn_update(scp, TRUE);
1677 
1678 #if NSPLASH > 0
1679     /* should we activate the screen saver? */
1680     if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
1681 	if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
1682 	    (*current_saver)(sc, TRUE);
1683 #endif /* NSPLASH */
1684 
1685     if (again)
1686 	timeout(scrn_timer, sc, hz / 25);
1687     splx(s);
1688 }
1689 
1690 static int
1691 and_region(int *s1, int *e1, int s2, int e2)
1692 {
1693     if (*e1 < s2 || e2 < *s1)
1694 	return FALSE;
1695     *s1 = imax(*s1, s2);
1696     *e1 = imin(*e1, e2);
1697     return TRUE;
1698 }
1699 
1700 static void
1701 scrn_update(scr_stat *scp, int show_cursor)
1702 {
1703     int start;
1704     int end;
1705     int s;
1706     int e;
1707 
1708     /* assert(scp == scp->sc->cur_scp) */
1709 
1710     ++scp->sc->videoio_in_progress;
1711 
1712 #ifndef SC_NO_CUTPASTE
1713     /* remove the previous mouse pointer image if necessary */
1714     if ((scp->status & (MOUSE_VISIBLE | MOUSE_MOVED))
1715 	== (MOUSE_VISIBLE | MOUSE_MOVED)) {
1716 	/* FIXME: I don't like this... XXX */
1717 	sc_remove_mouse_image(scp);
1718         if (scp->end >= scp->xsize*scp->ysize)
1719 	    scp->end = scp->xsize*scp->ysize - 1;
1720     }
1721 #endif /* !SC_NO_CUTPASTE */
1722 
1723 #if 1
1724     /* debug: XXX */
1725     if (scp->end >= scp->xsize*scp->ysize) {
1726 	printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
1727 	scp->end = scp->xsize*scp->ysize - 1;
1728     }
1729     if (scp->start < 0) {
1730 	printf("scrn_update(): scp->start %d < 0\n", scp->start);
1731 	scp->start = 0;
1732     }
1733 #endif
1734 
1735     /* update screen image */
1736     if (scp->start <= scp->end)  {
1737 	if (scp->mouse_cut_end >= 0) {
1738 	    /* there is a marked region for cut & paste */
1739 	    if (scp->mouse_cut_start <= scp->mouse_cut_end) {
1740 		start = scp->mouse_cut_start;
1741 		end = scp->mouse_cut_end;
1742 	    } else {
1743 		start = scp->mouse_cut_end;
1744 		end = scp->mouse_cut_start - 1;
1745 	    }
1746 	    s = start;
1747 	    e = end;
1748 	    /* does the cut-mark region overlap with the update region? */
1749 	    if (and_region(&s, &e, scp->start, scp->end)) {
1750 		(*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
1751 		s = 0;
1752 		e = start - 1;
1753 		if (and_region(&s, &e, scp->start, scp->end))
1754 		    (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1755 		s = end + 1;
1756 		e = scp->xsize*scp->ysize - 1;
1757 		if (and_region(&s, &e, scp->start, scp->end))
1758 		    (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1759 	    } else {
1760 		(*scp->rndr->draw)(scp, scp->start,
1761 				   scp->end - scp->start + 1, FALSE);
1762 	    }
1763 	} else {
1764 	    (*scp->rndr->draw)(scp, scp->start,
1765 			       scp->end - scp->start + 1, FALSE);
1766 	}
1767     }
1768 
1769     /* we are not to show the cursor and the mouse pointer... */
1770     if (!show_cursor) {
1771         scp->end = 0;
1772         scp->start = scp->xsize*scp->ysize - 1;
1773 	--scp->sc->videoio_in_progress;
1774 	return;
1775     }
1776 
1777     /* update cursor image */
1778     if (scp->status & CURSOR_ENABLED) {
1779         /* did cursor move since last time ? */
1780         if (scp->cursor_pos != scp->cursor_oldpos) {
1781             /* do we need to remove old cursor image ? */
1782             if (scp->cursor_oldpos < scp->start ||
1783                 scp->cursor_oldpos > scp->end) {
1784                 remove_cursor_image(scp);
1785             }
1786             scp->cursor_oldpos = scp->cursor_pos;
1787             draw_cursor_image(scp);
1788         }
1789         else {
1790             /* cursor didn't move, has it been overwritten ? */
1791             if (scp->cursor_pos >= scp->start && scp->cursor_pos <= scp->end) {
1792                 draw_cursor_image(scp);
1793             } else {
1794                 /* if its a blinking cursor, we may have to update it */
1795 		if (scp->sc->flags & SC_BLINK_CURSOR)
1796                     (*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
1797 					       sc_inside_cutmark(scp,
1798 							scp->cursor_pos));
1799             }
1800         }
1801     }
1802 
1803 #ifndef SC_NO_CUTPASTE
1804     /* update "pseudo" mouse pointer image */
1805     if (scp->status & MOUSE_VISIBLE) {
1806         /* did mouse move since last time ? */
1807         if (scp->status & MOUSE_MOVED) {
1808             /* the previous pointer image has been removed, see above */
1809             scp->status &= ~MOUSE_MOVED;
1810             sc_draw_mouse_image(scp);
1811         } else {
1812             /* mouse didn't move, has it been overwritten ? */
1813             if (scp->mouse_pos + scp->xsize + 1 >= scp->start &&
1814                 scp->mouse_pos <= scp->end) {
1815                 sc_draw_mouse_image(scp);
1816             } else if (scp->cursor_pos == scp->mouse_pos ||
1817             	scp->cursor_pos == scp->mouse_pos + 1 ||
1818             	scp->cursor_pos == scp->mouse_pos + scp->xsize ||
1819             	scp->cursor_pos == scp->mouse_pos + scp->xsize + 1) {
1820                 sc_draw_mouse_image(scp);
1821 	    }
1822         }
1823     }
1824 #endif /* SC_NO_CUTPASTE */
1825 
1826     scp->end = 0;
1827     scp->start = scp->xsize*scp->ysize - 1;
1828 
1829     --scp->sc->videoio_in_progress;
1830 }
1831 
1832 #if NSPLASH > 0
1833 static int
1834 scsplash_callback(int event, void *arg)
1835 {
1836     sc_softc_t *sc;
1837     int error;
1838 
1839     sc = (sc_softc_t *)arg;
1840 
1841     switch (event) {
1842     case SPLASH_INIT:
1843 	if (add_scrn_saver(scsplash_saver) == 0) {
1844 	    sc->flags &= ~SC_SAVER_FAILED;
1845 	    run_scrn_saver = TRUE;
1846 	    if (cold && !(boothowto & (RB_VERBOSE | RB_CONFIG))) {
1847 		scsplash_stick(TRUE);
1848 		(*current_saver)(sc, TRUE);
1849 	    }
1850 	}
1851 	return 0;
1852 
1853     case SPLASH_TERM:
1854 	if (current_saver == scsplash_saver) {
1855 	    scsplash_stick(FALSE);
1856 	    error = remove_scrn_saver(scsplash_saver);
1857 	    if (error)
1858 		return error;
1859 	}
1860 	return 0;
1861 
1862     default:
1863 	return EINVAL;
1864     }
1865 }
1866 
1867 static void
1868 scsplash_saver(sc_softc_t *sc, int show)
1869 {
1870     static int busy = FALSE;
1871     scr_stat *scp;
1872 
1873     if (busy)
1874 	return;
1875     busy = TRUE;
1876 
1877     scp = sc->cur_scp;
1878     if (show) {
1879 	if (!(sc->flags & SC_SAVER_FAILED)) {
1880 	    if (!(sc->flags & SC_SCRN_BLANKED))
1881 		set_scrn_saver_mode(scp, -1, NULL, 0);
1882 	    switch (splash(sc->adp, TRUE)) {
1883 	    case 0:		/* succeeded */
1884 		break;
1885 	    case EAGAIN:	/* try later */
1886 		restore_scrn_saver_mode(scp, FALSE);
1887 		sc_touch_scrn_saver();		/* XXX */
1888 		break;
1889 	    default:
1890 		sc->flags |= SC_SAVER_FAILED;
1891 		scsplash_stick(FALSE);
1892 		restore_scrn_saver_mode(scp, TRUE);
1893 		printf("scsplash_saver(): failed to put up the image\n");
1894 		break;
1895 	    }
1896 	}
1897     } else if (!sticky_splash) {
1898 	if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
1899 	    restore_scrn_saver_mode(scp, TRUE);
1900     }
1901     busy = FALSE;
1902 }
1903 
1904 static int
1905 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
1906 {
1907 #if 0
1908     int error;
1909 
1910     if (current_saver != none_saver) {
1911 	error = remove_scrn_saver(current_saver);
1912 	if (error)
1913 	    return error;
1914     }
1915 #endif
1916     if (current_saver != none_saver)
1917 	return EBUSY;
1918 
1919     run_scrn_saver = FALSE;
1920     saver_mode = CONS_LKM_SAVER;
1921     current_saver = this_saver;
1922     return 0;
1923 }
1924 
1925 static int
1926 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
1927 {
1928     if (current_saver != this_saver)
1929 	return EINVAL;
1930 
1931 #if 0
1932     /*
1933      * In order to prevent `current_saver' from being called by
1934      * the timeout routine `scrn_timer()' while we manipulate
1935      * the saver list, we shall set `current_saver' to `none_saver'
1936      * before stopping the current saver, rather than blocking by `splXX()'.
1937      */
1938     current_saver = none_saver;
1939     if (scrn_blanked)
1940         stop_scrn_saver(this_saver);
1941 #endif
1942 
1943     /* unblank all blanked screens */
1944     wait_scrn_saver_stop(NULL);
1945     if (scrn_blanked)
1946 	return EBUSY;
1947 
1948     current_saver = none_saver;
1949     return 0;
1950 }
1951 
1952 static int
1953 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
1954 {
1955     int s;
1956 
1957     /* assert(scp == scp->sc->cur_scp) */
1958     s = spltty();
1959     if (!ISGRAPHSC(scp))
1960 	remove_cursor_image(scp);
1961     scp->splash_save_mode = scp->mode;
1962     scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
1963     scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
1964     scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
1965     scp->sc->flags |= SC_SCRN_BLANKED;
1966     ++scrn_blanked;
1967     splx(s);
1968     if (mode < 0)
1969 	return 0;
1970     scp->mode = mode;
1971     if (set_mode(scp) == 0) {
1972 	if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
1973 	    scp->status |= GRAPHICS_MODE;
1974 #ifndef SC_NO_PALETTE_LOADING
1975 	if (pal != NULL)
1976 	    load_palette(scp->sc->adp, pal);
1977 #endif
1978 	set_border(scp, border);
1979 	return 0;
1980     } else {
1981 	s = spltty();
1982 	scp->mode = scp->splash_save_mode;
1983 	scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
1984 	scp->status |= scp->splash_save_status;
1985 	splx(s);
1986 	return 1;
1987     }
1988 }
1989 
1990 static int
1991 restore_scrn_saver_mode(scr_stat *scp, int changemode)
1992 {
1993     int mode;
1994     int status;
1995     int s;
1996 
1997     /* assert(scp == scp->sc->cur_scp) */
1998     s = spltty();
1999     mode = scp->mode;
2000     status = scp->status;
2001     scp->mode = scp->splash_save_mode;
2002     scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2003     scp->status |= scp->splash_save_status;
2004     scp->sc->flags &= ~SC_SCRN_BLANKED;
2005     if (!changemode) {
2006 	if (!ISGRAPHSC(scp))
2007 	    draw_cursor_image(scp);
2008 	--scrn_blanked;
2009 	splx(s);
2010 	return 0;
2011     }
2012     if (set_mode(scp) == 0) {
2013 #ifndef SC_NO_PALETTE_LOADING
2014 	load_palette(scp->sc->adp, scp->sc->palette);
2015 #endif
2016 	--scrn_blanked;
2017 	splx(s);
2018 	return 0;
2019     } else {
2020 	scp->mode = mode;
2021 	scp->status = status;
2022 	splx(s);
2023 	return 1;
2024     }
2025 }
2026 
2027 static void
2028 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2029 {
2030     (*saver)(sc, FALSE);
2031     run_scrn_saver = FALSE;
2032     /* the screen saver may have chosen not to stop after all... */
2033     if (sc->flags & SC_SCRN_BLANKED)
2034 	return;
2035 
2036     mark_all(sc->cur_scp);
2037     if (sc->delayed_next_scr)
2038 	switch_scr(sc, sc->delayed_next_scr - 1);
2039     wakeup((caddr_t)&scrn_blanked);
2040 }
2041 
2042 static int
2043 wait_scrn_saver_stop(sc_softc_t *sc)
2044 {
2045     int error = 0;
2046 
2047     while (scrn_blanked > 0) {
2048 	run_scrn_saver = FALSE;
2049 	if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2050 	    error = 0;
2051 	    break;
2052 	}
2053 	error = tsleep((caddr_t)&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
2054 	if ((error != 0) && (error != ERESTART))
2055 	    break;
2056     }
2057     run_scrn_saver = FALSE;
2058     return error;
2059 }
2060 #endif /* NSPLASH */
2061 
2062 void
2063 sc_touch_scrn_saver(void)
2064 {
2065     scsplash_stick(FALSE);
2066     run_scrn_saver = FALSE;
2067 }
2068 
2069 void
2070 sc_clear_screen(scr_stat *scp)
2071 {
2072     move_crsr(scp, 0, 0);
2073     scp->cursor_oldpos = scp->cursor_pos;
2074     sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20], scp->term.cur_color);
2075     mark_all(scp);
2076     sc_remove_cutmarking(scp);
2077 }
2078 
2079 static int
2080 switch_scr(sc_softc_t *sc, u_int next_scr)
2081 {
2082     struct tty *tp;
2083     int s;
2084 
2085     DPRINTF(5, ("sc0: switch_scr() %d ", next_scr + 1));
2086 
2087     /* delay switch if the screen is blanked or being updated */
2088     if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2089 	|| sc->blink_in_progress || sc->videoio_in_progress) {
2090 	sc->delayed_next_scr = next_scr + 1;
2091 	sc_touch_scrn_saver();
2092 	DPRINTF(5, ("switch delayed\n"));
2093 	return 0;
2094     }
2095 
2096     s = spltty();
2097 
2098     /* we are in the middle of the vty switching process... */
2099     if (sc->switch_in_progress
2100 	&& (sc->cur_scp->smode.mode == VT_PROCESS)
2101 	&& sc->cur_scp->proc) {
2102 	if (sc->cur_scp->proc != pfind(sc->cur_scp->pid)) {
2103 	    /*
2104 	     * The controlling process has died!!.  Do some clean up.
2105 	     * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2106 	     * are not reset here yet; they will be cleared later.
2107 	     */
2108 	    DPRINTF(5, ("cur_scp controlling process %d died, ",
2109 	       sc->cur_scp->pid));
2110 	    if (sc->cur_scp->status & SWITCH_WAIT_REL) {
2111 		/*
2112 		 * Force the previous switch to finish, but return now
2113 		 * with error.
2114 		 */
2115 		DPRINTF(5, ("reset WAIT_REL, "));
2116 		sc->cur_scp->status &= ~SWITCH_WAIT_REL;
2117 		s = do_switch_scr(sc, s);
2118 		splx(s);
2119 		DPRINTF(5, ("finishing previous switch\n"));
2120 		return EINVAL;
2121 	    } else if (sc->cur_scp->status & SWITCH_WAIT_ACQ) {
2122 		/* let's assume screen switch has been completed. */
2123 		DPRINTF(5, ("reset WAIT_ACQ, "));
2124 		sc->cur_scp->status &= ~SWITCH_WAIT_ACQ;
2125 		sc->switch_in_progress = 0;
2126 	    } else {
2127 		/*
2128 	 	 * We are in between screen release and acquisition, and
2129 		 * reached here via scgetc() or scrn_timer() which has
2130 		 * interrupted exchange_scr(). Don't do anything stupid.
2131 		 */
2132 		DPRINTF(5, ("waiting nothing, "));
2133 	    }
2134 	} else {
2135 	    /*
2136 	     * The controlling process is alive, but not responding...
2137 	     * It is either buggy or it may be just taking time.
2138 	     * The following code is a gross kludge to cope with this
2139 	     * problem for which there is no clean solution. XXX
2140 	     */
2141 	    if (sc->cur_scp->status & SWITCH_WAIT_REL) {
2142 		switch (sc->switch_in_progress++) {
2143 		case 1:
2144 		    break;
2145 		case 2:
2146 		    DPRINTF(5, ("sending relsig again, "));
2147 		    signal_vt_rel(sc->cur_scp);
2148 		    break;
2149 		case 3:
2150 		    break;
2151 		case 4:
2152 		default:
2153 		    /*
2154 		     * Act as if the controlling program returned
2155 		     * VT_FALSE.
2156 		     */
2157 		    DPRINTF(5, ("force reset WAIT_REL, "));
2158 		    sc->cur_scp->status &= ~SWITCH_WAIT_REL;
2159 		    sc->switch_in_progress = 0;
2160 		    splx(s);
2161 		    DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2162 		    return EINVAL;
2163 		}
2164 	    } else if (sc->cur_scp->status & SWITCH_WAIT_ACQ) {
2165 		switch (sc->switch_in_progress++) {
2166 		case 1:
2167 		    break;
2168 		case 2:
2169 		    DPRINTF(5, ("sending acqsig again, "));
2170 		    signal_vt_acq(sc->cur_scp);
2171 		    break;
2172 		case 3:
2173 		    break;
2174 		case 4:
2175 		default:
2176 		     /* clear the flag and finish the previous switch */
2177 		    DPRINTF(5, ("force reset WAIT_ACQ, "));
2178 		    sc->cur_scp->status &= ~SWITCH_WAIT_ACQ;
2179 		    sc->switch_in_progress = 0;
2180 		    break;
2181 		}
2182 	    }
2183 	}
2184     }
2185 
2186     /*
2187      * Return error if an invalid argument is given, or vty switch
2188      * is still in progress.
2189      */
2190     if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2191 	|| sc->switch_in_progress) {
2192 	splx(s);
2193 	do_bell(sc->cur_scp, bios_value.bell_pitch, BELL_DURATION);
2194 	DPRINTF(5, ("error 1\n"));
2195 	return EINVAL;
2196     }
2197 
2198     /*
2199      * Don't allow switching away from the graphics mode vty
2200      * if the switch mode is VT_AUTO, unless the next vty is the same
2201      * as the current or the current vty has been closed (but showing).
2202      */
2203     tp = VIRTUAL_TTY(sc, sc->cur_scp->index);
2204     if ((sc->cur_scp->index != next_scr)
2205 	&& (tp->t_state & TS_ISOPEN)
2206 	&& (sc->cur_scp->smode.mode == VT_AUTO)
2207 	&& ISGRAPHSC(sc->cur_scp)) {
2208 	splx(s);
2209 	do_bell(sc->cur_scp, bios_value.bell_pitch, BELL_DURATION);
2210 	DPRINTF(5, ("error, graphics mode\n"));
2211 	return EINVAL;
2212     }
2213 
2214     /*
2215      * Is the wanted vty open? Don't allow switching to a closed vty.
2216      * Note that we always allow the user to switch to the kernel
2217      * console even if it is closed.
2218      */
2219     if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2220 	tp = VIRTUAL_TTY(sc, next_scr);
2221 	if ((tp == NULL) || !(tp->t_state & TS_ISOPEN)) {
2222 	    splx(s);
2223 	    do_bell(sc->cur_scp, bios_value.bell_pitch, BELL_DURATION);
2224 	    DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2225 	    return EINVAL;
2226 	}
2227     }
2228 
2229     /* this is the start of vty switching process... */
2230     ++sc->switch_in_progress;
2231     sc->delayed_next_scr = 0;
2232     sc->old_scp = sc->cur_scp;
2233     sc->new_scp = SC_STAT(SC_DEV(sc, next_scr));
2234     if (sc->new_scp == sc->old_scp) {
2235 	sc->switch_in_progress = 0;
2236 	wakeup((caddr_t)&sc->new_scp->smode);
2237 	splx(s);
2238 	DPRINTF(5, ("switch done (new == old)\n"));
2239 	return 0;
2240     }
2241 
2242     /* has controlling process died? */
2243     vt_proc_alive(sc->old_scp);
2244     vt_proc_alive(sc->new_scp);
2245 
2246     /* wait for the controlling process to release the screen, if necessary */
2247     if (signal_vt_rel(sc->old_scp)) {
2248 	splx(s);
2249 	return 0;
2250     }
2251 
2252     /* go set up the new vty screen */
2253     splx(s);
2254     exchange_scr(sc);
2255     s = spltty();
2256 
2257     /* wake up processes waiting for this vty */
2258     wakeup((caddr_t)&sc->cur_scp->smode);
2259 
2260     /* wait for the controlling process to acknowledge, if necessary */
2261     if (signal_vt_acq(sc->cur_scp)) {
2262 	splx(s);
2263 	return 0;
2264     }
2265 
2266     sc->switch_in_progress = 0;
2267     if (sc->unit == sc_console_unit)
2268 	cons_unavail = FALSE;
2269     splx(s);
2270     DPRINTF(5, ("switch done\n"));
2271 
2272     return 0;
2273 }
2274 
2275 static int
2276 do_switch_scr(sc_softc_t *sc, int s)
2277 {
2278     vt_proc_alive(sc->new_scp);
2279 
2280     splx(s);
2281     exchange_scr(sc);
2282     s = spltty();
2283     /* sc->cur_scp == sc->new_scp */
2284     wakeup((caddr_t)&sc->cur_scp->smode);
2285 
2286     /* wait for the controlling process to acknowledge, if necessary */
2287     if (!signal_vt_acq(sc->cur_scp)) {
2288 	sc->switch_in_progress = 0;
2289 	if (sc->unit == sc_console_unit)
2290 	    cons_unavail = FALSE;
2291     }
2292 
2293     return s;
2294 }
2295 
2296 static int
2297 vt_proc_alive(scr_stat *scp)
2298 {
2299     if (scp->proc) {
2300 	if (scp->proc == pfind(scp->pid))
2301 	    return TRUE;
2302 	scp->proc = NULL;
2303 	scp->smode.mode = VT_AUTO;
2304 	DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2305     }
2306     return FALSE;
2307 }
2308 
2309 static int
2310 signal_vt_rel(scr_stat *scp)
2311 {
2312     if (scp->smode.mode != VT_PROCESS)
2313 	return FALSE;
2314     scp->status |= SWITCH_WAIT_REL;
2315     psignal(scp->proc, scp->smode.relsig);
2316     DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2317     return TRUE;
2318 }
2319 
2320 static int
2321 signal_vt_acq(scr_stat *scp)
2322 {
2323     if (scp->smode.mode != VT_PROCESS)
2324 	return FALSE;
2325     if (scp->sc->unit == sc_console_unit)
2326 	cons_unavail = TRUE;
2327     scp->status |= SWITCH_WAIT_ACQ;
2328     psignal(scp->proc, scp->smode.acqsig);
2329     DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2330     return TRUE;
2331 }
2332 
2333 static void
2334 exchange_scr(sc_softc_t *sc)
2335 {
2336     scr_stat *scp;
2337 
2338     /* save the current state of video and keyboard */
2339     move_crsr(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2340     if (sc->old_scp->kbd_mode == K_XLATE)
2341 	save_kbd_state(sc->old_scp);
2342 
2343     /* set up the video for the new screen */
2344     scp = sc->cur_scp = sc->new_scp;
2345     if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2346 	set_mode(scp);
2347     else
2348 	sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2349 		    (void *)sc->adp->va_window, FALSE);
2350     move_crsr(scp, scp->xpos, scp->ypos);
2351     if (!ISGRAPHSC(scp))
2352 	sc_set_cursor_image(scp);
2353 #ifndef SC_NO_PALETTE_LOADING
2354     if (ISGRAPHSC(sc->old_scp))
2355 	load_palette(sc->adp, sc->palette);
2356 #endif
2357     set_border(scp, scp->border);
2358 
2359     /* set up the keyboard for the new screen */
2360     if (sc->old_scp->kbd_mode != scp->kbd_mode)
2361 	kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2362     update_kbd_state(scp, scp->status, LOCK_MASK);
2363 
2364     mark_all(scp);
2365 }
2366 
2367 static void
2368 scan_esc(scr_stat *scp, u_char c)
2369 {
2370     static u_char ansi_col[16] =
2371 	{0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15};
2372     sc_softc_t *sc;
2373     int i, n;
2374     int count;
2375 
2376     i = n = 0;
2377     sc = scp->sc;
2378     if (scp->term.esc == 1) {	/* seen ESC */
2379 	switch (c) {
2380 
2381 	case '7':   /* Save cursor position */
2382 	    scp->saved_xpos = scp->xpos;
2383 	    scp->saved_ypos = scp->ypos;
2384 	    break;
2385 
2386 	case '8':   /* Restore saved cursor position */
2387 	    if (scp->saved_xpos >= 0 && scp->saved_ypos >= 0)
2388 		move_crsr(scp, scp->saved_xpos, scp->saved_ypos);
2389 	    break;
2390 
2391 	case '[':   /* Start ESC [ sequence */
2392 	    scp->term.esc = 2;
2393 	    scp->term.last_param = -1;
2394 	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
2395 		scp->term.param[i] = 1;
2396 	    scp->term.num_param = 0;
2397 	    return;
2398 
2399 	case 'M':   /* Move cursor up 1 line, scroll if at top */
2400 	    if (scp->ypos > 0)
2401 		move_crsr(scp, scp->xpos, scp->ypos - 1);
2402 	    else {
2403 		sc_vtb_ins(&scp->vtb, 0, scp->xsize,
2404 			   sc->scr_map[0x20], scp->term.cur_color);
2405     		mark_all(scp);
2406 	    }
2407 	    break;
2408 #if notyet
2409 	case 'Q':
2410 	    scp->term.esc = 4;
2411 	    return;
2412 #endif
2413 	case 'c':   /* Clear screen & home */
2414 	    sc_clear_screen(scp);
2415 	    break;
2416 
2417 	case '(':   /* iso-2022: designate 94 character set to G0 */
2418 	    scp->term.esc = 5;
2419 	    return;
2420 	}
2421     }
2422     else if (scp->term.esc == 2) {	/* seen ESC [ */
2423 	if (c >= '0' && c <= '9') {
2424 	    if (scp->term.num_param < MAX_ESC_PAR) {
2425 	    if (scp->term.last_param != scp->term.num_param) {
2426 		scp->term.last_param = scp->term.num_param;
2427 		scp->term.param[scp->term.num_param] = 0;
2428 	    }
2429 	    else
2430 		scp->term.param[scp->term.num_param] *= 10;
2431 	    scp->term.param[scp->term.num_param] += c - '0';
2432 	    return;
2433 	    }
2434 	}
2435 	scp->term.num_param = scp->term.last_param + 1;
2436 	switch (c) {
2437 
2438 	case ';':
2439 	    if (scp->term.num_param < MAX_ESC_PAR)
2440 		return;
2441 	    break;
2442 
2443 	case '=':
2444 	    scp->term.esc = 3;
2445 	    scp->term.last_param = -1;
2446 	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
2447 		scp->term.param[i] = 1;
2448 	    scp->term.num_param = 0;
2449 	    return;
2450 
2451 	case 'A':   /* up n rows */
2452 	    n = scp->term.param[0]; if (n < 1) n = 1;
2453 	    move_crsr(scp, scp->xpos, scp->ypos - n);
2454 	    break;
2455 
2456 	case 'B':   /* down n rows */
2457 	    n = scp->term.param[0]; if (n < 1) n = 1;
2458 	    move_crsr(scp, scp->xpos, scp->ypos + n);
2459 	    break;
2460 
2461 	case 'C':   /* right n columns */
2462 	    n = scp->term.param[0]; if (n < 1) n = 1;
2463 	    move_crsr(scp, scp->xpos + n, scp->ypos);
2464 	    break;
2465 
2466 	case 'D':   /* left n columns */
2467 	    n = scp->term.param[0]; if (n < 1) n = 1;
2468 	    move_crsr(scp, scp->xpos - n, scp->ypos);
2469 	    break;
2470 
2471 	case 'E':   /* cursor to start of line n lines down */
2472 	    n = scp->term.param[0]; if (n < 1) n = 1;
2473 	    move_crsr(scp, 0, scp->ypos + n);
2474 	    break;
2475 
2476 	case 'F':   /* cursor to start of line n lines up */
2477 	    n = scp->term.param[0]; if (n < 1) n = 1;
2478 	    move_crsr(scp, 0, scp->ypos - n);
2479 	    break;
2480 
2481 	case 'f':   /* Cursor move */
2482 	case 'H':
2483 	    if (scp->term.num_param == 0)
2484 		move_crsr(scp, 0, 0);
2485 	    else if (scp->term.num_param == 2)
2486 		move_crsr(scp, scp->term.param[1] - 1, scp->term.param[0] - 1);
2487 	    break;
2488 
2489 	case 'J':   /* Clear all or part of display */
2490 	    if (scp->term.num_param == 0)
2491 		n = 0;
2492 	    else
2493 		n = scp->term.param[0];
2494 	    switch (n) {
2495 	    case 0: /* clear form cursor to end of display */
2496 		sc_vtb_erase(&scp->vtb, scp->cursor_pos,
2497 			     scp->xsize * scp->ysize - scp->cursor_pos,
2498 			     sc->scr_map[0x20], scp->term.cur_color);
2499 		mark_for_update(scp, scp->cursor_pos);
2500     		mark_for_update(scp, scp->xsize * scp->ysize - 1);
2501 		sc_remove_cutmarking(scp);
2502 		break;
2503 	    case 1: /* clear from beginning of display to cursor */
2504 		sc_vtb_erase(&scp->vtb, 0, scp->cursor_pos,
2505 			     sc->scr_map[0x20], scp->term.cur_color);
2506 		mark_for_update(scp, 0);
2507 		mark_for_update(scp, scp->cursor_pos);
2508 		sc_remove_cutmarking(scp);
2509 		break;
2510 	    case 2: /* clear entire display */
2511 		sc_vtb_clear(&scp->vtb, sc->scr_map[0x20], scp->term.cur_color);
2512 		mark_all(scp);
2513 		sc_remove_cutmarking(scp);
2514 		break;
2515 	    }
2516 	    break;
2517 
2518 	case 'K':   /* Clear all or part of line */
2519 	    if (scp->term.num_param == 0)
2520 		n = 0;
2521 	    else
2522 		n = scp->term.param[0];
2523 	    switch (n) {
2524 	    case 0: /* clear form cursor to end of line */
2525 		sc_vtb_erase(&scp->vtb, scp->cursor_pos,
2526 			     scp->xsize - scp->xpos,
2527 			     sc->scr_map[0x20], scp->term.cur_color);
2528     		mark_for_update(scp, scp->cursor_pos);
2529     		mark_for_update(scp, scp->cursor_pos +
2530 				scp->xsize - 1 - scp->xpos);
2531 		break;
2532 	    case 1: /* clear from beginning of line to cursor */
2533 		sc_vtb_erase(&scp->vtb, scp->cursor_pos - scp->xpos,
2534 			     scp->xpos + 1,
2535 			     sc->scr_map[0x20], scp->term.cur_color);
2536     		mark_for_update(scp, scp->ypos * scp->xsize);
2537     		mark_for_update(scp, scp->cursor_pos);
2538 		break;
2539 	    case 2: /* clear entire line */
2540 		sc_vtb_erase(&scp->vtb, scp->cursor_pos - scp->xpos,
2541 			     scp->xsize,
2542 			     sc->scr_map[0x20], scp->term.cur_color);
2543     		mark_for_update(scp, scp->ypos * scp->xsize);
2544     		mark_for_update(scp, (scp->ypos + 1) * scp->xsize - 1);
2545 		break;
2546 	    }
2547 	    break;
2548 
2549 	case 'L':   /* Insert n lines */
2550 	    n = scp->term.param[0]; if (n < 1) n = 1;
2551 	    if (n > scp->ysize - scp->ypos)
2552 		n = scp->ysize - scp->ypos;
2553 	    sc_vtb_ins(&scp->vtb, scp->ypos * scp->xsize, n * scp->xsize,
2554 		       sc->scr_map[0x20], scp->term.cur_color);
2555 	    mark_for_update(scp, scp->ypos * scp->xsize);
2556 	    mark_for_update(scp, scp->xsize * scp->ysize - 1);
2557 	    break;
2558 
2559 	case 'M':   /* Delete n lines */
2560 	    n = scp->term.param[0]; if (n < 1) n = 1;
2561 	    if (n > scp->ysize - scp->ypos)
2562 		n = scp->ysize - scp->ypos;
2563 	    sc_vtb_delete(&scp->vtb, scp->ypos * scp->xsize, n * scp->xsize,
2564 			  sc->scr_map[0x20], scp->term.cur_color);
2565 	    mark_for_update(scp, scp->ypos * scp->xsize);
2566 	    mark_for_update(scp, scp->xsize * scp->ysize - 1);
2567 	    break;
2568 
2569 	case 'P':   /* Delete n chars */
2570 	    n = scp->term.param[0]; if (n < 1) n = 1;
2571 	    if (n > scp->xsize - scp->xpos)
2572 		n = scp->xsize - scp->xpos;
2573 	    count = scp->xsize - (scp->xpos + n);
2574 	    sc_vtb_move(&scp->vtb, scp->cursor_pos + n, scp->cursor_pos, count);
2575 	    sc_vtb_erase(&scp->vtb, scp->cursor_pos + count, n,
2576 			 sc->scr_map[0x20], scp->term.cur_color);
2577 	    mark_for_update(scp, scp->cursor_pos);
2578 	    mark_for_update(scp, scp->cursor_pos + n + count - 1);
2579 	    break;
2580 
2581 	case '@':   /* Insert n chars */
2582 	    n = scp->term.param[0]; if (n < 1) n = 1;
2583 	    if (n > scp->xsize - scp->xpos)
2584 		n = scp->xsize - scp->xpos;
2585 	    count = scp->xsize - (scp->xpos + n);
2586 	    sc_vtb_move(&scp->vtb, scp->cursor_pos, scp->cursor_pos + n, count);
2587 	    sc_vtb_erase(&scp->vtb, scp->cursor_pos, n,
2588 			 sc->scr_map[0x20], scp->term.cur_color);
2589 	    mark_for_update(scp, scp->cursor_pos);
2590 	    mark_for_update(scp, scp->cursor_pos + n + count - 1);
2591 	    break;
2592 
2593 	case 'S':   /* scroll up n lines */
2594 	    n = scp->term.param[0]; if (n < 1)  n = 1;
2595 	    if (n > scp->ysize)
2596 		n = scp->ysize;
2597 	    sc_vtb_delete(&scp->vtb, 0, n * scp->xsize,
2598 			  sc->scr_map[0x20], scp->term.cur_color);
2599     	    mark_all(scp);
2600 	    break;
2601 
2602 	case 'T':   /* scroll down n lines */
2603 	    n = scp->term.param[0]; if (n < 1)  n = 1;
2604 	    if (n > scp->ysize)
2605 		n = scp->ysize;
2606 	    sc_vtb_ins(&scp->vtb, 0, n * scp->xsize,
2607 		       sc->scr_map[0x20], scp->term.cur_color);
2608     	    mark_all(scp);
2609 	    break;
2610 
2611 	case 'X':   /* erase n characters in line */
2612 	    n = scp->term.param[0]; if (n < 1)  n = 1;
2613 	    if (n > scp->xsize - scp->xpos)
2614 		n = scp->xsize - scp->xpos;
2615 	    sc_vtb_erase(&scp->vtb, scp->cursor_pos, n,
2616 			 sc->scr_map[0x20], scp->term.cur_color);
2617 	    mark_for_update(scp, scp->cursor_pos);
2618 	    mark_for_update(scp, scp->cursor_pos + n - 1);
2619 	    break;
2620 
2621 	case 'Z':   /* move n tabs backwards */
2622 	    n = scp->term.param[0]; if (n < 1)  n = 1;
2623 	    if ((i = scp->xpos & 0xf8) == scp->xpos)
2624 		i -= 8*n;
2625 	    else
2626 		i -= 8*(n-1);
2627 	    if (i < 0)
2628 		i = 0;
2629 	    move_crsr(scp, i, scp->ypos);
2630 	    break;
2631 
2632 	case '`':   /* move cursor to column n */
2633 	    n = scp->term.param[0]; if (n < 1)  n = 1;
2634 	    move_crsr(scp, n - 1, scp->ypos);
2635 	    break;
2636 
2637 	case 'a':   /* move cursor n columns to the right */
2638 	    n = scp->term.param[0]; if (n < 1)  n = 1;
2639 	    move_crsr(scp, scp->xpos + n, scp->ypos);
2640 	    break;
2641 
2642 	case 'd':   /* move cursor to row n */
2643 	    n = scp->term.param[0]; if (n < 1)  n = 1;
2644 	    move_crsr(scp, scp->xpos, n - 1);
2645 	    break;
2646 
2647 	case 'e':   /* move cursor n rows down */
2648 	    n = scp->term.param[0]; if (n < 1)  n = 1;
2649 	    move_crsr(scp, scp->xpos, scp->ypos + n);
2650 	    break;
2651 
2652 	case 'm':   /* change attribute */
2653 	    if (scp->term.num_param == 0) {
2654 		scp->term.attr_mask = NORMAL_ATTR;
2655 		scp->term.cur_attr =
2656 		    scp->term.cur_color = scp->term.std_color;
2657 		break;
2658 	    }
2659 	    for (i = 0; i < scp->term.num_param; i++) {
2660 		switch (n = scp->term.param[i]) {
2661 		case 0: /* back to normal */
2662 		    scp->term.attr_mask = NORMAL_ATTR;
2663 		    scp->term.cur_attr =
2664 			scp->term.cur_color = scp->term.std_color;
2665 		    break;
2666 		case 1: /* bold */
2667 		    scp->term.attr_mask |= BOLD_ATTR;
2668 		    scp->term.cur_attr = mask2attr(&scp->term);
2669 		    break;
2670 		case 4: /* underline */
2671 		    scp->term.attr_mask |= UNDERLINE_ATTR;
2672 		    scp->term.cur_attr = mask2attr(&scp->term);
2673 		    break;
2674 		case 5: /* blink */
2675 		    scp->term.attr_mask |= BLINK_ATTR;
2676 		    scp->term.cur_attr = mask2attr(&scp->term);
2677 		    break;
2678 		case 7: /* reverse video */
2679 		    scp->term.attr_mask |= REVERSE_ATTR;
2680 		    scp->term.cur_attr = mask2attr(&scp->term);
2681 		    break;
2682 		case 30: case 31: /* set fg color */
2683 		case 32: case 33: case 34:
2684 		case 35: case 36: case 37:
2685 		    scp->term.attr_mask |= FOREGROUND_CHANGED;
2686 		    scp->term.cur_color =
2687 			(scp->term.cur_color&0xF000) | (ansi_col[(n-30)&7]<<8);
2688 		    scp->term.cur_attr = mask2attr(&scp->term);
2689 		    break;
2690 		case 40: case 41: /* set bg color */
2691 		case 42: case 43: case 44:
2692 		case 45: case 46: case 47:
2693 		    scp->term.attr_mask |= BACKGROUND_CHANGED;
2694 		    scp->term.cur_color =
2695 			(scp->term.cur_color&0x0F00) | (ansi_col[(n-40)&7]<<12);
2696 		    scp->term.cur_attr = mask2attr(&scp->term);
2697 		    break;
2698 		}
2699 	    }
2700 	    break;
2701 
2702 	case 's':   /* Save cursor position */
2703 	    scp->saved_xpos = scp->xpos;
2704 	    scp->saved_ypos = scp->ypos;
2705 	    break;
2706 
2707 	case 'u':   /* Restore saved cursor position */
2708 	    if (scp->saved_xpos >= 0 && scp->saved_ypos >= 0)
2709 		move_crsr(scp, scp->saved_xpos, scp->saved_ypos);
2710 	    break;
2711 
2712 	case 'x':
2713 	    if (scp->term.num_param == 0)
2714 		n = 0;
2715 	    else
2716 		n = scp->term.param[0];
2717 	    switch (n) {
2718 	    case 0:     /* reset attributes */
2719 		scp->term.attr_mask = NORMAL_ATTR;
2720 		scp->term.cur_attr =
2721 		    scp->term.cur_color = scp->term.std_color =
2722 		    current_default->std_color;
2723 		scp->term.rev_color = current_default->rev_color;
2724 		break;
2725 	    case 1:     /* set ansi background */
2726 		scp->term.attr_mask &= ~BACKGROUND_CHANGED;
2727 		scp->term.cur_color = scp->term.std_color =
2728 		    (scp->term.std_color & 0x0F00) |
2729 		    (ansi_col[(scp->term.param[1])&0x0F]<<12);
2730 		scp->term.cur_attr = mask2attr(&scp->term);
2731 		break;
2732 	    case 2:     /* set ansi foreground */
2733 		scp->term.attr_mask &= ~FOREGROUND_CHANGED;
2734 		scp->term.cur_color = scp->term.std_color =
2735 		    (scp->term.std_color & 0xF000) |
2736 		    (ansi_col[(scp->term.param[1])&0x0F]<<8);
2737 		scp->term.cur_attr = mask2attr(&scp->term);
2738 		break;
2739 	    case 3:     /* set ansi attribute directly */
2740 		scp->term.attr_mask &= ~(FOREGROUND_CHANGED|BACKGROUND_CHANGED);
2741 		scp->term.cur_color = scp->term.std_color =
2742 		    (scp->term.param[1]&0xFF)<<8;
2743 		scp->term.cur_attr = mask2attr(&scp->term);
2744 		break;
2745 	    case 5:     /* set ansi reverse video background */
2746 		scp->term.rev_color =
2747 		    (scp->term.rev_color & 0x0F00) |
2748 		    (ansi_col[(scp->term.param[1])&0x0F]<<12);
2749 		scp->term.cur_attr = mask2attr(&scp->term);
2750 		break;
2751 	    case 6:     /* set ansi reverse video foreground */
2752 		scp->term.rev_color =
2753 		    (scp->term.rev_color & 0xF000) |
2754 		    (ansi_col[(scp->term.param[1])&0x0F]<<8);
2755 		scp->term.cur_attr = mask2attr(&scp->term);
2756 		break;
2757 	    case 7:     /* set ansi reverse video directly */
2758 		scp->term.rev_color =
2759 		    (scp->term.param[1]&0xFF)<<8;
2760 		scp->term.cur_attr = mask2attr(&scp->term);
2761 		break;
2762 	    }
2763 	    break;
2764 
2765 	case 'z':   /* switch to (virtual) console n */
2766 	    if (scp->term.num_param == 1)
2767 		switch_scr(sc, scp->term.param[0]);
2768 	    break;
2769 	}
2770     }
2771     else if (scp->term.esc == 3) {	/* seen ESC [0-9]+ = */
2772 	if (c >= '0' && c <= '9') {
2773 	    if (scp->term.num_param < MAX_ESC_PAR) {
2774 	    if (scp->term.last_param != scp->term.num_param) {
2775 		scp->term.last_param = scp->term.num_param;
2776 		scp->term.param[scp->term.num_param] = 0;
2777 	    }
2778 	    else
2779 		scp->term.param[scp->term.num_param] *= 10;
2780 	    scp->term.param[scp->term.num_param] += c - '0';
2781 	    return;
2782 	    }
2783 	}
2784 	scp->term.num_param = scp->term.last_param + 1;
2785 	switch (c) {
2786 
2787 	case ';':
2788 	    if (scp->term.num_param < MAX_ESC_PAR)
2789 		return;
2790 	    break;
2791 
2792 	case 'A':   /* set display border color */
2793 	    if (scp->term.num_param == 1) {
2794 		scp->border=scp->term.param[0] & 0xff;
2795 		if (scp == sc->cur_scp)
2796 		    set_border(scp, scp->border);
2797             }
2798 	    break;
2799 
2800 	case 'B':   /* set bell pitch and duration */
2801 	    if (scp->term.num_param == 2) {
2802 		scp->bell_pitch = scp->term.param[0];
2803 		scp->bell_duration = scp->term.param[1];
2804 	    }
2805 	    break;
2806 
2807 	case 'C':   /* set cursor type & shape */
2808 	    if (!ISGRAPHSC(sc->cur_scp))
2809 		remove_cursor_image(sc->cur_scp);
2810 	    if (scp->term.num_param == 1) {
2811 		if (scp->term.param[0] & 0x01)
2812 		    sc->flags |= SC_BLINK_CURSOR;
2813 		else
2814 		    sc->flags &= ~SC_BLINK_CURSOR;
2815 		if (scp->term.param[0] & 0x02)
2816 		    sc->flags |= SC_CHAR_CURSOR;
2817 		else
2818 		    sc->flags &= ~SC_CHAR_CURSOR;
2819 	    }
2820 	    else if (scp->term.num_param == 2) {
2821 		sc->cursor_base = scp->font_size
2822 					- (scp->term.param[1] & 0x1F) - 1;
2823 		sc->cursor_height = (scp->term.param[1] & 0x1F)
2824 					- (scp->term.param[0] & 0x1F) + 1;
2825 	    }
2826 	    /*
2827 	     * The cursor shape is global property; all virtual consoles
2828 	     * are affected. Update the cursor in the current console...
2829 	     */
2830 	    if (!ISGRAPHSC(sc->cur_scp)) {
2831 		i = spltty();
2832 		sc_set_cursor_image(sc->cur_scp);
2833 		draw_cursor_image(sc->cur_scp);
2834 		splx(i);
2835 	    }
2836 	    break;
2837 
2838 	case 'F':   /* set ansi foreground */
2839 	    if (scp->term.num_param == 1) {
2840 		scp->term.attr_mask &= ~FOREGROUND_CHANGED;
2841 		scp->term.cur_color = scp->term.std_color =
2842 		    (scp->term.std_color & 0xF000)
2843 		    | ((scp->term.param[0] & 0x0F) << 8);
2844 		scp->term.cur_attr = mask2attr(&scp->term);
2845 	    }
2846 	    break;
2847 
2848 	case 'G':   /* set ansi background */
2849 	    if (scp->term.num_param == 1) {
2850 		scp->term.attr_mask &= ~BACKGROUND_CHANGED;
2851 		scp->term.cur_color = scp->term.std_color =
2852 		    (scp->term.std_color & 0x0F00)
2853 		    | ((scp->term.param[0] & 0x0F) << 12);
2854 		scp->term.cur_attr = mask2attr(&scp->term);
2855 	    }
2856 	    break;
2857 
2858 	case 'H':   /* set ansi reverse video foreground */
2859 	    if (scp->term.num_param == 1) {
2860 		scp->term.rev_color =
2861 		    (scp->term.rev_color & 0xF000)
2862 		    | ((scp->term.param[0] & 0x0F) << 8);
2863 		scp->term.cur_attr = mask2attr(&scp->term);
2864 	    }
2865 	    break;
2866 
2867 	case 'I':   /* set ansi reverse video background */
2868 	    if (scp->term.num_param == 1) {
2869 		scp->term.rev_color =
2870 		    (scp->term.rev_color & 0x0F00)
2871 		    | ((scp->term.param[0] & 0x0F) << 12);
2872 		scp->term.cur_attr = mask2attr(&scp->term);
2873 	    }
2874 	    break;
2875 	}
2876     }
2877 #if notyet
2878     else if (scp->term.esc == 4) {	/* seen ESC Q */
2879 	/* to be filled */
2880     }
2881 #endif
2882     else if (scp->term.esc == 5) {	/* seen ESC ( */
2883 	switch (c) {
2884 	case 'B':   /* iso-2022: desginate ASCII into G0 */
2885 	    break;
2886 	/* other items to be filled */
2887 	default:
2888 	    break;
2889 	}
2890     }
2891     scp->term.esc = 0;
2892 }
2893 
2894 static void
2895 ansi_put(scr_stat *scp, u_char *buf, int len)
2896 {
2897     u_char *ptr = buf;
2898 
2899 #if NSPLASH > 0
2900     /* make screensaver happy */
2901     if (!sticky_splash && scp == scp->sc->cur_scp)
2902 	run_scrn_saver = FALSE;
2903 #endif
2904 
2905 outloop:
2906     scp->sc->write_in_progress++;
2907     if (scp->term.esc) {
2908 	scan_esc(scp, *ptr++);
2909 	len--;
2910     }
2911     else if (PRINTABLE(*ptr)) {     /* Print only printables */
2912 	vm_offset_t p;
2913 	u_char *map;
2914  	int cnt;
2915 	int attr;
2916 	int i;
2917 
2918 	p = sc_vtb_pointer(&scp->vtb, scp->cursor_pos);
2919 	map = scp->sc->scr_map;
2920 	attr = scp->term.cur_attr;
2921 
2922 	cnt = (len <= scp->xsize - scp->xpos) ? len : (scp->xsize - scp->xpos);
2923 	i = cnt;
2924 	do {
2925 	    /*
2926 	     * gcc-2.6.3 generates poor (un)sign extension code.  Casting the
2927 	     * pointers in the following to volatile should have no effect,
2928 	     * but in fact speeds up this inner loop from 26 to 18 cycles
2929 	     * (+ cache misses) on i486's.
2930 	     */
2931 #define	UCVP(ucp)	((u_char volatile *)(ucp))
2932 	    p = sc_vtb_putchar(&scp->vtb, p, UCVP(map)[*UCVP(ptr)], attr);
2933 	    ++ptr;
2934 	    --i;
2935 	} while (i > 0 && PRINTABLE(*ptr));
2936 
2937 	len -= cnt - i;
2938 	mark_for_update(scp, scp->cursor_pos);
2939 	scp->cursor_pos += cnt - i;
2940 	mark_for_update(scp, scp->cursor_pos - 1);
2941 	scp->xpos += cnt - i;
2942 
2943 	if (scp->xpos >= scp->xsize) {
2944 	    scp->xpos = 0;
2945 	    scp->ypos++;
2946 	}
2947     }
2948     else  {
2949 	switch(*ptr) {
2950 	case 0x07:
2951 	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
2952 	    break;
2953 
2954 	case 0x08:      /* non-destructive backspace */
2955 	    if (scp->cursor_pos > 0) {
2956 		mark_for_update(scp, scp->cursor_pos);
2957 		scp->cursor_pos--;
2958 		mark_for_update(scp, scp->cursor_pos);
2959 		if (scp->xpos > 0)
2960 		    scp->xpos--;
2961 		else {
2962 		    scp->xpos += scp->xsize - 1;
2963 		    scp->ypos--;
2964 		}
2965 	    }
2966 	    break;
2967 
2968 	case 0x09:  /* non-destructive tab */
2969 	    mark_for_update(scp, scp->cursor_pos);
2970 	    scp->cursor_pos += (8 - scp->xpos % 8u);
2971 	    if ((scp->xpos += (8 - scp->xpos % 8u)) >= scp->xsize) {
2972 	        scp->xpos = 0;
2973 	        scp->ypos++;
2974 		scp->cursor_pos = scp->xsize * scp->ypos;
2975 	    }
2976 	    mark_for_update(scp, scp->cursor_pos);
2977 	    break;
2978 
2979 	case 0x0a:  /* newline, same pos */
2980 	    mark_for_update(scp, scp->cursor_pos);
2981 	    scp->cursor_pos += scp->xsize;
2982 	    mark_for_update(scp, scp->cursor_pos);
2983 	    scp->ypos++;
2984 	    break;
2985 
2986 	case 0x0c:  /* form feed, clears screen */
2987 	    sc_clear_screen(scp);
2988 	    break;
2989 
2990 	case 0x0d:  /* return, return to pos 0 */
2991 	    mark_for_update(scp, scp->cursor_pos);
2992 	    scp->cursor_pos -= scp->xpos;
2993 	    mark_for_update(scp, scp->cursor_pos);
2994 	    scp->xpos = 0;
2995 	    break;
2996 
2997 	case 0x1b:  /* start escape sequence */
2998 	    scp->term.esc = 1;
2999 	    scp->term.num_param = 0;
3000 	    break;
3001 	}
3002 	ptr++; len--;
3003     }
3004     /* do we have to scroll ?? */
3005     if (scp->cursor_pos >= scp->ysize * scp->xsize) {
3006 	sc_remove_cutmarking(scp);
3007 #ifndef SC_NO_HISTORY
3008 	if (scp->history != NULL)
3009 	    sc_hist_save_one_line(scp, 0);
3010 #endif
3011 	sc_vtb_delete(&scp->vtb, 0, scp->xsize,
3012 		      scp->sc->scr_map[0x20], scp->term.cur_color);
3013 	scp->cursor_pos -= scp->xsize;
3014 	scp->ypos--;
3015     	mark_all(scp);
3016     }
3017     scp->sc->write_in_progress--;
3018     if (len)
3019 	goto outloop;
3020     if (scp->sc->delayed_next_scr)
3021 	switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3022 }
3023 
3024 static void
3025 draw_cursor_image(scr_stat *scp)
3026 {
3027     /* assert(scp == scp->sc->cur_scp); */
3028     ++scp->sc->videoio_in_progress;
3029     (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
3030 			      scp->sc->flags & SC_BLINK_CURSOR, TRUE,
3031 			      sc_inside_cutmark(scp, scp->cursor_pos));
3032     --scp->sc->videoio_in_progress;
3033 }
3034 
3035 static void
3036 remove_cursor_image(scr_stat *scp)
3037 {
3038     /* assert(scp == scp->sc->cur_scp); */
3039     ++scp->sc->videoio_in_progress;
3040     (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
3041 			      scp->sc->flags & SC_BLINK_CURSOR, FALSE,
3042 			      sc_inside_cutmark(scp, scp->cursor_oldpos));
3043     --scp->sc->videoio_in_progress;
3044 }
3045 
3046 static void
3047 update_cursor_image(scr_stat *scp)
3048 {
3049     int blink;
3050 
3051     if (scp->sc->flags & SC_CHAR_CURSOR) {
3052 	scp->cursor_base = scp->sc->cursor_base;
3053 	scp->cursor_height = imin(scp->sc->cursor_height, scp->font_size);
3054     } else {
3055 	scp->cursor_base = 0;
3056 	scp->cursor_height = scp->font_size;
3057     }
3058     blink = scp->sc->flags & SC_BLINK_CURSOR;
3059 
3060     /* assert(scp == scp->sc->cur_scp); */
3061     ++scp->sc->videoio_in_progress;
3062     (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos, blink, FALSE,
3063 			      sc_inside_cutmark(scp, scp->cursor_pos));
3064     (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height, blink);
3065     (*scp->rndr->draw_cursor)(scp, scp->cursor_pos, blink, TRUE,
3066 			      sc_inside_cutmark(scp, scp->cursor_pos));
3067     --scp->sc->videoio_in_progress;
3068 }
3069 
3070 void
3071 sc_set_cursor_image(scr_stat *scp)
3072 {
3073     if (scp->sc->flags & SC_CHAR_CURSOR) {
3074 	scp->cursor_base = scp->sc->cursor_base;
3075 	scp->cursor_height = imin(scp->sc->cursor_height, scp->font_size);
3076     } else {
3077 	scp->cursor_base = 0;
3078 	scp->cursor_height = scp->font_size;
3079     }
3080 
3081     /* assert(scp == scp->sc->cur_scp); */
3082     ++scp->sc->videoio_in_progress;
3083     (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height,
3084 			     scp->sc->flags & SC_BLINK_CURSOR);
3085     --scp->sc->videoio_in_progress;
3086 }
3087 
3088 static void
3089 move_crsr(scr_stat *scp, int x, int y)
3090 {
3091     if (x < 0)
3092 	x = 0;
3093     if (y < 0)
3094 	y = 0;
3095     if (x >= scp->xsize)
3096 	x = scp->xsize-1;
3097     if (y >= scp->ysize)
3098 	y = scp->ysize-1;
3099     scp->xpos = x;
3100     scp->ypos = y;
3101     scp->cursor_pos = scp->ypos * scp->xsize + scp->xpos;
3102 }
3103 
3104 static void
3105 scinit(int unit, int flags)
3106 {
3107     /*
3108      * When syscons is being initialized as the kernel console, malloc()
3109      * is not yet functional, because various kernel structures has not been
3110      * fully initialized yet.  Therefore, we need to declare the following
3111      * static buffers for the console.  This is less than ideal,
3112      * but is necessry evil for the time being.  XXX
3113      */
3114     static scr_stat main_console;
3115     static dev_t main_devs[MAXCONS];
3116     static struct tty main_tty;
3117     static u_short sc_buffer[ROW*COL];	/* XXX */
3118 #ifndef SC_NO_FONT_LOADING
3119     static u_char font_8[256*8];
3120     static u_char font_14[256*14];
3121     static u_char font_16[256*16];
3122 #endif
3123 
3124     sc_softc_t *sc;
3125     scr_stat *scp;
3126     video_adapter_t *adp;
3127     int col;
3128     int row;
3129     int i;
3130 
3131     /* one time initialization */
3132     if (init_done == COLD) {
3133 	sc_get_bios_values(&bios_value);
3134 	current_default = &user_default;
3135 	/* kernel console attributes */
3136 	kernel_console.esc = 0;
3137 	kernel_console.attr_mask = NORMAL_ATTR;
3138 	kernel_console.cur_attr =
3139 	    kernel_console.cur_color = kernel_console.std_color =
3140 	    kernel_default.std_color;
3141 	kernel_console.rev_color = kernel_default.rev_color;
3142     }
3143     init_done = WARM;
3144 
3145     /*
3146      * Allocate resources.  Even if we are being called for the second
3147      * time, we must allocate them again, because they might have
3148      * disappeared...
3149      */
3150     sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3151     adp = NULL;
3152     if (sc->adapter >= 0) {
3153 	vid_release(sc->adp, (void *)&sc->adapter);
3154 	adp = sc->adp;
3155 	sc->adp = NULL;
3156     }
3157     if (sc->keyboard >= 0) {
3158 	DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
3159 	i = kbd_release(sc->kbd, (void *)&sc->keyboard);
3160 	DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
3161 	if (sc->kbd != NULL) {
3162 	    DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, minor:%d, flags:0x%x\n",
3163 		unit, sc->kbd->kb_index, sc->kbd->kb_minor, sc->kbd->kb_flags));
3164 	}
3165 	sc->kbd = NULL;
3166     }
3167     sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
3168     sc->adp = vid_get_adapter(sc->adapter);
3169     /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
3170     sc->keyboard = kbd_allocate("*", unit, (void *)&sc->keyboard,
3171 				sckbdevent, sc);
3172     DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
3173     sc->kbd = kbd_get_keyboard(sc->keyboard);
3174     if (sc->kbd != NULL) {
3175 	DPRINTF(1, ("sc%d: kbd index:%d, minor:%d, flags:0x%x\n",
3176 		unit, sc->kbd->kb_index, sc->kbd->kb_minor, sc->kbd->kb_flags));
3177     }
3178 
3179     if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
3180 
3181 	sc->initial_mode = sc->adp->va_initial_mode;
3182 
3183 #ifndef SC_NO_FONT_LOADING
3184 	if (flags & SC_KERNEL_CONSOLE) {
3185 	    sc->font_8 = font_8;
3186 	    sc->font_14 = font_14;
3187 	    sc->font_16 = font_16;
3188 	} else if (sc->font_8 == NULL) {
3189 	    /* assert(sc_malloc) */
3190 	    sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
3191 	    sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
3192 	    sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
3193 	}
3194 #endif
3195 
3196 	/* extract the hardware cursor location and hide the cursor for now */
3197 	(*vidsw[sc->adapter]->read_hw_cursor)(sc->adp, &col, &row);
3198 	(*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, -1, -1);
3199 
3200 	/* set up the first console */
3201 	sc->first_vty = unit*MAXCONS;
3202 	sc->vtys = MAXCONS;
3203 	if (flags & SC_KERNEL_CONSOLE) {
3204 	    sc->dev = main_devs;
3205 	    sc->dev[0] = makedev(CDEV_MAJOR, unit*MAXCONS);
3206 	    sc->dev[0]->si_tty = &main_tty;
3207 	    ttyregister(&main_tty);
3208 	    scp = &main_console;
3209 	    init_scp(sc, sc->first_vty, scp);
3210 	    sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
3211 			(void *)sc_buffer, FALSE);
3212 	} else {
3213 	    /* assert(sc_malloc) */
3214 	    sc->dev = malloc(sizeof(dev_t)*sc->vtys, M_DEVBUF, M_WAITOK);
3215 	    bzero(sc->dev, sizeof(dev_t)*sc->vtys);
3216 	    sc->dev[0] = makedev(CDEV_MAJOR, unit*MAXCONS);
3217 	    sc->dev[0]->si_tty = ttymalloc(sc->dev[0]->si_tty);
3218 	    scp = alloc_scp(sc, sc->first_vty);
3219 	}
3220 	SC_STAT(sc->dev[0]) = scp;
3221 	sc->cur_scp = scp;
3222 
3223 	/* copy screen to temporary buffer */
3224 	sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3225 		    (void *)scp->sc->adp->va_window, FALSE);
3226 	if (ISTEXTSC(scp))
3227 	    sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
3228 
3229 	/* move cursors to the initial positions */
3230 	scp->mouse_pos = scp->mouse_oldpos = 0;
3231 	if (col >= scp->xsize)
3232 	    col = 0;
3233 	if (row >= scp->ysize)
3234 	    row = scp->ysize - 1;
3235 	scp->xpos = col;
3236 	scp->ypos = row;
3237 	scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
3238 	if (bios_value.cursor_end < scp->font_size)
3239 	    sc->cursor_base = scp->font_size - bios_value.cursor_end - 1;
3240 	else
3241 	    sc->cursor_base = 0;
3242 	i = bios_value.cursor_end - bios_value.cursor_start + 1;
3243 	sc->cursor_height = imin(i, scp->font_size);
3244 	if (!ISGRAPHSC(scp)) {
3245     	    sc_set_cursor_image(scp);
3246     	    draw_cursor_image(scp);
3247 	}
3248 
3249 	/* save font and palette */
3250 #ifndef SC_NO_FONT_LOADING
3251 	sc->fonts_loaded = 0;
3252 	if (ISFONTAVAIL(sc->adp->va_flags)) {
3253 #ifdef SC_DFLT_FONT
3254 	    bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
3255 	    bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
3256 	    bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
3257 	    sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
3258 	    if (scp->font_size < 14) {
3259 		copy_font(scp, LOAD, 8, sc->font_8);
3260 	    } else if (scp->font_size >= 16) {
3261 		copy_font(scp, LOAD, 16, sc->font_16);
3262 	    } else {
3263 		copy_font(scp, LOAD, 14, sc->font_14);
3264 	    }
3265 #else /* !SC_DFLT_FONT */
3266 	    if (scp->font_size < 14) {
3267 		copy_font(scp, SAVE, 8, sc->font_8);
3268 		sc->fonts_loaded = FONT_8;
3269 	    } else if (scp->font_size >= 16) {
3270 		copy_font(scp, SAVE, 16, sc->font_16);
3271 		sc->fonts_loaded = FONT_16;
3272 	    } else {
3273 		copy_font(scp, SAVE, 14, sc->font_14);
3274 		sc->fonts_loaded = FONT_14;
3275 	    }
3276 #endif /* SC_DFLT_FONT */
3277 	    /* FONT KLUDGE: always use the font page #0. XXX */
3278 	    (*vidsw[sc->adapter]->show_font)(sc->adp, 0);
3279 	}
3280 #endif /* !SC_NO_FONT_LOADING */
3281 
3282 #ifndef SC_NO_PALETTE_LOADING
3283 	save_palette(sc->adp, sc->palette);
3284 #endif
3285 
3286 #if NSPLASH > 0
3287 	if (!(sc->flags & SC_SPLASH_SCRN) && (flags & SC_KERNEL_CONSOLE)) {
3288 	    /* we are ready to put up the splash image! */
3289 	    splash_init(sc->adp, scsplash_callback, sc);
3290 	    sc->flags |= SC_SPLASH_SCRN;
3291 	}
3292 #endif /* NSPLASH */
3293     }
3294 
3295     /* the rest is not necessary, if we have done it once */
3296     if (sc->flags & SC_INIT_DONE)
3297 	return;
3298 
3299     /* initialize mapscrn arrays to a one to one map */
3300     for (i = 0; i < sizeof(sc->scr_map); i++)
3301 	sc->scr_map[i] = sc->scr_rmap[i] = i;
3302 
3303     sc->flags |= SC_INIT_DONE;
3304 }
3305 
3306 #if __i386__
3307 static void
3308 scterm(int unit, int flags)
3309 {
3310     sc_softc_t *sc;
3311 
3312     sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3313     if (sc == NULL)
3314 	return;			/* shouldn't happen */
3315 
3316 #if NSPLASH > 0
3317     /* this console is no longer available for the splash screen */
3318     if (sc->flags & SC_SPLASH_SCRN) {
3319 	splash_term(sc->adp);
3320 	sc->flags &= ~SC_SPLASH_SCRN;
3321     }
3322 #endif /* NSPLASH */
3323 
3324 #if 0 /* XXX */
3325     /* move the hardware cursor to the upper-left corner */
3326     (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, 0, 0);
3327 #endif
3328 
3329     /* release the keyboard and the video card */
3330     if (sc->keyboard >= 0)
3331 	kbd_release(sc->kbd, &sc->keyboard);
3332     if (sc->adapter >= 0)
3333 	vid_release(sc->adp, &sc->adapter);
3334 
3335     /* clear the structure */
3336     if (!(flags & SC_KERNEL_CONSOLE)) {
3337 	/* XXX: We need delete_dev() for this */
3338 	free(sc->dev, M_DEVBUF);
3339 #if 0
3340 	/* XXX: We need a ttyunregister for this */
3341 	free(sc->tty, M_DEVBUF);
3342 #endif
3343 #ifndef SC_NO_FONT_LOADING
3344 	free(sc->font_8, M_DEVBUF);
3345 	free(sc->font_14, M_DEVBUF);
3346 	free(sc->font_16, M_DEVBUF);
3347 #endif
3348 	/* XXX vtb, history */
3349     }
3350     bzero(sc, sizeof(*sc));
3351     sc->keyboard = -1;
3352     sc->adapter = -1;
3353 }
3354 #endif
3355 
3356 static void
3357 scshutdown(void *arg, int howto)
3358 {
3359     /* assert(sc_console != NULL) */
3360 
3361     sc_touch_scrn_saver();
3362     if (!cold && sc_console
3363 	&& sc_console->sc->cur_scp->smode.mode == VT_AUTO
3364 	&& sc_console->smode.mode == VT_AUTO)
3365 	switch_scr(sc_console->sc, sc_console->index);
3366     shutdown_in_progress = TRUE;
3367 }
3368 
3369 int
3370 sc_clean_up(scr_stat *scp)
3371 {
3372 #if NSPLASH > 0
3373     int error;
3374 #endif /* NSPLASH */
3375 
3376     sc_touch_scrn_saver();
3377 #if NSPLASH > 0
3378     if ((error = wait_scrn_saver_stop(scp->sc)))
3379 	return error;
3380 #endif /* NSPLASH */
3381     scp->status &= ~MOUSE_VISIBLE;
3382     sc_remove_cutmarking(scp);
3383     return 0;
3384 }
3385 
3386 void
3387 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
3388 {
3389     sc_vtb_t new;
3390     sc_vtb_t old;
3391     int s;
3392 
3393     old = scp->vtb;
3394     sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
3395     if (!discard && (old.vtb_flags & VTB_VALID)) {
3396 	/* retain the current cursor position and buffer contants */
3397 	scp->cursor_oldpos = scp->cursor_pos;
3398 	/*
3399 	 * This works only if the old buffer has the same size as or larger
3400 	 * than the new one. XXX
3401 	 */
3402 	sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
3403 	scp->vtb = new;
3404     } else {
3405         /* clear the screen and move the text cursor to the top-left position */
3406 	s = splhigh();
3407 	scp->vtb = new;
3408 	sc_clear_screen(scp);
3409 	splx(s);
3410 	sc_vtb_destroy(&old);
3411     }
3412 
3413 #ifndef SC_NO_SYSMOUSE
3414     /* move the mouse cursor at the center of the screen */
3415     sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
3416 #endif
3417 }
3418 
3419 static scr_stat
3420 *alloc_scp(sc_softc_t *sc, int vty)
3421 {
3422     scr_stat *scp;
3423 
3424     /* assert(sc_malloc) */
3425 
3426     scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
3427     init_scp(sc, vty, scp);
3428 
3429     sc_alloc_scr_buffer(scp, TRUE, TRUE);
3430 
3431 #ifndef SC_NO_SYSMOUSE
3432     if (ISMOUSEAVAIL(sc->adp->va_flags))
3433 	sc_alloc_cut_buffer(scp, TRUE);
3434 #endif
3435 
3436 #ifndef SC_NO_HISTORY
3437     sc_alloc_history_buffer(scp, 0, 0, TRUE);
3438 #endif
3439 
3440     sc_clear_screen(scp);
3441     return scp;
3442 }
3443 
3444 static void
3445 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3446 {
3447     video_info_t info;
3448 
3449     scp->index = vty;
3450     scp->sc = sc;
3451     scp->status = 0;
3452     scp->mode = sc->initial_mode;
3453     (*vidsw[sc->adapter]->get_info)(sc->adp, scp->mode, &info);
3454     if (info.vi_flags & V_INFO_GRAPHICS) {
3455 	scp->status |= GRAPHICS_MODE;
3456 	scp->xpixel = info.vi_width;
3457 	scp->ypixel = info.vi_height;
3458 	scp->xsize = info.vi_width/8;
3459 	scp->ysize = info.vi_height/info.vi_cheight;
3460 	scp->font_size = FONT_NONE;
3461 	scp->font = NULL;
3462     } else {
3463 	scp->xsize = info.vi_width;
3464 	scp->ysize = info.vi_height;
3465 	scp->xpixel = scp->xsize*8;
3466 	scp->ypixel = scp->ysize*info.vi_cheight;
3467 	if (info.vi_cheight < 14) {
3468 	    scp->font_size = 8;
3469 #ifndef SC_NO_FONT_LOADING
3470 	    scp->font = sc->font_8;
3471 #else
3472 	    scp->font = NULL;
3473 #endif
3474 	} else if (info.vi_cheight >= 16) {
3475 	    scp->font_size = 16;
3476 #ifndef SC_NO_FONT_LOADING
3477 	    scp->font = sc->font_16;
3478 #else
3479 	    scp->font = NULL;
3480 #endif
3481 	} else {
3482 	    scp->font_size = 14;
3483 #ifndef SC_NO_FONT_LOADING
3484 	    scp->font = sc->font_14;
3485 #else
3486 	    scp->font = NULL;
3487 #endif
3488 	}
3489     }
3490     sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3491     sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3492     scp->xoff = scp->yoff = 0;
3493     scp->xpos = scp->ypos = 0;
3494     scp->saved_xpos = scp->saved_ypos = -1;
3495     scp->start = scp->xsize * scp->ysize - 1;
3496     scp->end = 0;
3497     scp->term.esc = 0;
3498     scp->term.attr_mask = NORMAL_ATTR;
3499     scp->term.cur_attr =
3500 	scp->term.cur_color = scp->term.std_color =
3501 	current_default->std_color;
3502     scp->term.rev_color = current_default->rev_color;
3503     scp->border = BG_BLACK;
3504     scp->cursor_base = sc->cursor_base;
3505     scp->cursor_height = imin(sc->cursor_height, scp->font_size);
3506     scp->mouse_xpos = scp->xoff*8 + scp->xsize*8/2;
3507     scp->mouse_ypos = (scp->ysize + scp->yoff)*scp->font_size/2;
3508     scp->mouse_cut_start = scp->xsize*scp->ysize;
3509     scp->mouse_cut_end = -1;
3510     scp->mouse_signal = 0;
3511     scp->mouse_pid = 0;
3512     scp->mouse_proc = NULL;
3513     scp->kbd_mode = K_XLATE;
3514     scp->bell_pitch = bios_value.bell_pitch;
3515     scp->bell_duration = BELL_DURATION;
3516     scp->status |= (bios_value.shift_state & NLKED);
3517     scp->status |= CURSOR_ENABLED;
3518     scp->pid = 0;
3519     scp->proc = NULL;
3520     scp->smode.mode = VT_AUTO;
3521     scp->history = NULL;
3522     scp->history_pos = 0;
3523     scp->history_size = 0;
3524 
3525     /* what if the following call fails... XXX */
3526     scp->rndr = sc_render_match(scp, scp->sc->adp,
3527 				scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3528 }
3529 
3530 /*
3531  * scgetc(flags) - get character from keyboard.
3532  * If flags & SCGETC_CN, then avoid harmful side effects.
3533  * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3534  * return NOKEY if there is nothing there.
3535  */
3536 static u_int
3537 scgetc(sc_softc_t *sc, u_int flags)
3538 {
3539     scr_stat *scp;
3540 #ifndef SC_NO_HISTORY
3541     struct tty *tp;
3542 #endif
3543     u_int c;
3544     int this_scr;
3545     int f;
3546     int i;
3547 
3548     if (sc->kbd == NULL)
3549 	return NOKEY;
3550 
3551 next_code:
3552 #if 1
3553     /* I don't like this, but... XXX */
3554     if (flags & SCGETC_CN)
3555 	sccnupdate(sc->cur_scp);
3556 #endif
3557     scp = sc->cur_scp;
3558     /* first see if there is something in the keyboard port */
3559     for (;;) {
3560 	c = kbd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3561 	if (c == ERRKEY) {
3562 	    if (!(flags & SCGETC_CN))
3563 		do_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3564 	} else if (c == NOKEY)
3565 	    return c;
3566 	else
3567 	    break;
3568     }
3569 
3570     /* make screensaver happy */
3571     if (!(c & RELKEY))
3572 	sc_touch_scrn_saver();
3573 
3574 #ifdef __i386__
3575     if (!(flags & SCGETC_CN))
3576 	/* do the /dev/random device a favour */
3577 	add_keyboard_randomness(c);
3578 #endif
3579 
3580     if (scp->kbd_mode != K_XLATE)
3581 	return KEYCHAR(c);
3582 
3583     /* if scroll-lock pressed allow history browsing */
3584     if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3585 
3586 	scp->status &= ~CURSOR_ENABLED;
3587 	remove_cursor_image(scp);
3588 
3589 #ifndef SC_NO_HISTORY
3590 	if (!(scp->status & BUFFER_SAVED)) {
3591 	    scp->status |= BUFFER_SAVED;
3592 	    sc_hist_save(scp);
3593 	}
3594 	switch (c) {
3595 	/* FIXME: key codes */
3596 	case SPCLKEY | FKEY | F(49):  /* home key */
3597 	    sc_remove_cutmarking(scp);
3598 	    sc_hist_home(scp);
3599 	    goto next_code;
3600 
3601 	case SPCLKEY | FKEY | F(57):  /* end key */
3602 	    sc_remove_cutmarking(scp);
3603 	    sc_hist_end(scp);
3604 	    goto next_code;
3605 
3606 	case SPCLKEY | FKEY | F(50):  /* up arrow key */
3607 	    sc_remove_cutmarking(scp);
3608 	    if (sc_hist_up_line(scp))
3609 		if (!(flags & SCGETC_CN))
3610 		    do_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3611 	    goto next_code;
3612 
3613 	case SPCLKEY | FKEY | F(58):  /* down arrow key */
3614 	    sc_remove_cutmarking(scp);
3615 	    if (sc_hist_down_line(scp))
3616 		if (!(flags & SCGETC_CN))
3617 		    do_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3618 	    goto next_code;
3619 
3620 	case SPCLKEY | FKEY | F(51):  /* page up key */
3621 	    sc_remove_cutmarking(scp);
3622 	    for (i=0; i<scp->ysize; i++)
3623 	    if (sc_hist_up_line(scp)) {
3624 		if (!(flags & SCGETC_CN))
3625 		    do_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3626 		break;
3627 	    }
3628 	    goto next_code;
3629 
3630 	case SPCLKEY | FKEY | F(59):  /* page down key */
3631 	    sc_remove_cutmarking(scp);
3632 	    for (i=0; i<scp->ysize; i++)
3633 	    if (sc_hist_down_line(scp)) {
3634 		if (!(flags & SCGETC_CN))
3635 		    do_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3636 		break;
3637 	    }
3638 	    goto next_code;
3639 	}
3640 #endif /* SC_NO_HISTORY */
3641     }
3642 
3643     /*
3644      * Process and consume special keys here.  Return a plain char code
3645      * or a char code with the META flag or a function key code.
3646      */
3647     if (c & RELKEY) {
3648 	/* key released */
3649 	/* goto next_code */
3650     } else {
3651 	/* key pressed */
3652 	if (c & SPCLKEY) {
3653 	    c &= ~SPCLKEY;
3654 	    switch (KEYCHAR(c)) {
3655 	    /* LOCKING KEYS */
3656 	    case NLK: case CLK: case ALK:
3657 		break;
3658 	    case SLK:
3659 		kbd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3660 		if (f & SLKED) {
3661 		    scp->status |= SLKED;
3662 		} else {
3663 		    if (scp->status & SLKED) {
3664 			scp->status &= ~SLKED;
3665 #ifndef SC_NO_HISTORY
3666 			if (scp->status & BUFFER_SAVED) {
3667 			    if (!sc_hist_restore(scp))
3668 				sc_remove_cutmarking(scp);
3669 			    scp->status &= ~BUFFER_SAVED;
3670 			    scp->status |= CURSOR_ENABLED;
3671 			    draw_cursor_image(scp);
3672 			}
3673 			tp = VIRTUAL_TTY(sc, scp->index);
3674 			if (tp->t_state & TS_ISOPEN)
3675 			    scstart(tp);
3676 #endif
3677 		    }
3678 		}
3679 		break;
3680 
3681 	    /* NON-LOCKING KEYS */
3682 	    case NOP:
3683 	    case LSH:  case RSH:  case LCTR: case RCTR:
3684 	    case LALT: case RALT: case ASH:  case META:
3685 		break;
3686 
3687 	    case BTAB:
3688 		if (!(sc->flags & SC_SCRN_BLANKED))
3689 		    return c;
3690 		break;
3691 
3692 	    case SPSC:
3693 #if NSPLASH > 0
3694 		/* force activatation/deactivation of the screen saver */
3695 		if (!(sc->flags & SC_SCRN_BLANKED)) {
3696 		    run_scrn_saver = TRUE;
3697 		    sc->scrn_time_stamp -= scrn_blank_time;
3698 		}
3699 		if (cold) {
3700 		    /*
3701 		     * While devices are being probed, the screen saver need
3702 		     * to be invoked explictly. XXX
3703 		     */
3704 		    if (sc->flags & SC_SCRN_BLANKED) {
3705 			scsplash_stick(FALSE);
3706 			stop_scrn_saver(sc, current_saver);
3707 		    } else {
3708 			if (!ISGRAPHSC(scp)) {
3709 			    scsplash_stick(TRUE);
3710 			    (*current_saver)(sc, TRUE);
3711 			}
3712 		    }
3713 		}
3714 #endif /* NSPLASH */
3715 		break;
3716 
3717 	    case RBT:
3718 #ifndef SC_DISABLE_REBOOT
3719 		shutdown_nice();
3720 #endif
3721 		break;
3722 
3723 #if NAPM > 0
3724 	    case SUSP:
3725 		apm_suspend(PMST_SUSPEND);
3726 		break;
3727 	    case STBY:
3728 		apm_suspend(PMST_STANDBY);
3729 		break;
3730 #else
3731 	    case SUSP:
3732 	    case STBY:
3733 		break;
3734 #endif
3735 
3736 	    case DBG:
3737 #ifndef SC_DISABLE_DDBKEY
3738 #ifdef DDB
3739 		Debugger("manual escape to debugger");
3740 #else
3741 		printf("No debugger in kernel\n");
3742 #endif
3743 #else /* SC_DISABLE_DDBKEY */
3744 		/* do nothing */
3745 #endif /* SC_DISABLE_DDBKEY */
3746 		break;
3747 
3748 	    case PNC:
3749 		if (enable_panic_key)
3750 			panic("Forced by the panic key");
3751 		break;
3752 
3753 	    case NEXT:
3754 		this_scr = scp->index;
3755 		for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3756 			sc->first_vty + i != this_scr;
3757 			i = (i + 1)%sc->vtys) {
3758 		    struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3759 		    if (tp && tp->t_state & TS_ISOPEN) {
3760 			switch_scr(scp->sc, sc->first_vty + i);
3761 			break;
3762 		    }
3763 		}
3764 		break;
3765 
3766 	    case PREV:
3767 		this_scr = scp->index;
3768 		for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3769 			sc->first_vty + i != this_scr;
3770 			i = (i + sc->vtys - 1)%sc->vtys) {
3771 		    struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3772 		    if (tp && tp->t_state & TS_ISOPEN) {
3773 			switch_scr(scp->sc, sc->first_vty + i);
3774 			break;
3775 		    }
3776 		}
3777 		break;
3778 
3779 	    default:
3780 		if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3781 		    switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3782 		    break;
3783 		}
3784 		/* assert(c & FKEY) */
3785 		if (!(sc->flags & SC_SCRN_BLANKED))
3786 		    return c;
3787 		break;
3788 	    }
3789 	    /* goto next_code */
3790 	} else {
3791 	    /* regular keys (maybe MKEY is set) */
3792 	    if (!(sc->flags & SC_SCRN_BLANKED))
3793 		return c;
3794 	}
3795     }
3796 
3797     goto next_code;
3798 }
3799 
3800 int
3801 scmmap(dev_t dev, vm_offset_t offset, int nprot)
3802 {
3803     scr_stat *scp;
3804 
3805     if (SC_VTY(dev) == SC_MOUSE)
3806 	return -1;
3807     scp = SC_STAT(dev);
3808     if (scp != scp->sc->cur_scp)
3809 	return -1;
3810     return (*vidsw[scp->sc->adapter]->mmap)(scp->sc->adp, offset, nprot);
3811 }
3812 
3813 /*
3814  * Calculate hardware attributes word using logical attributes mask and
3815  * hardware colors
3816  */
3817 
3818 static int
3819 mask2attr(struct term_stat *term)
3820 {
3821     int attr, mask = term->attr_mask;
3822 
3823     if (mask & REVERSE_ATTR) {
3824 	attr = ((mask & FOREGROUND_CHANGED) ?
3825 		((term->cur_color & 0xF000) >> 4) :
3826 		(term->rev_color & 0x0F00)) |
3827 	       ((mask & BACKGROUND_CHANGED) ?
3828 		((term->cur_color & 0x0F00) << 4) :
3829 		(term->rev_color & 0xF000));
3830     } else
3831 	attr = term->cur_color;
3832 
3833     /* XXX: underline mapping for Hercules adapter can be better */
3834     if (mask & (BOLD_ATTR | UNDERLINE_ATTR))
3835 	attr ^= 0x0800;
3836     if (mask & BLINK_ATTR)
3837 	attr ^= 0x8000;
3838 
3839     return attr;
3840 }
3841 
3842 static int
3843 save_kbd_state(scr_stat *scp)
3844 {
3845     int state;
3846     int error;
3847 
3848     error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3849     if (error == ENOIOCTL)
3850 	error = ENODEV;
3851     if (error == 0) {
3852 	scp->status &= ~LOCK_MASK;
3853 	scp->status |= state;
3854     }
3855     return error;
3856 }
3857 
3858 static int
3859 update_kbd_state(scr_stat *scp, int new_bits, int mask)
3860 {
3861     int state;
3862     int error;
3863 
3864     if (mask != LOCK_MASK) {
3865 	error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3866 	if (error == ENOIOCTL)
3867 	    error = ENODEV;
3868 	if (error)
3869 	    return error;
3870 	state &= ~mask;
3871 	state |= new_bits & mask;
3872     } else {
3873 	state = new_bits & LOCK_MASK;
3874     }
3875     error = kbd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3876     if (error == ENOIOCTL)
3877 	error = ENODEV;
3878     return error;
3879 }
3880 
3881 static int
3882 update_kbd_leds(scr_stat *scp, int which)
3883 {
3884     int error;
3885 
3886     which &= LOCK_MASK;
3887     error = kbd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3888     if (error == ENOIOCTL)
3889 	error = ENODEV;
3890     return error;
3891 }
3892 
3893 int
3894 set_mode(scr_stat *scp)
3895 {
3896     video_info_t info;
3897 
3898     /* reject unsupported mode */
3899     if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info))
3900 	return 1;
3901 
3902     /* if this vty is not currently showing, do nothing */
3903     if (scp != scp->sc->cur_scp)
3904 	return 0;
3905 
3906     /* setup video hardware for the given mode */
3907     (*vidsw[scp->sc->adapter]->set_mode)(scp->sc->adp, scp->mode);
3908     sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3909 		(void *)scp->sc->adp->va_window, FALSE);
3910 
3911 #ifndef SC_NO_FONT_LOADING
3912     /* load appropriate font */
3913     if (!(scp->status & GRAPHICS_MODE)) {
3914 	if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3915 	    if (scp->font_size < 14) {
3916 		if (scp->sc->fonts_loaded & FONT_8)
3917 		    copy_font(scp, LOAD, 8, scp->sc->font_8);
3918 	    } else if (scp->font_size >= 16) {
3919 		if (scp->sc->fonts_loaded & FONT_16)
3920 		    copy_font(scp, LOAD, 16, scp->sc->font_16);
3921 	    } else {
3922 		if (scp->sc->fonts_loaded & FONT_14)
3923 		    copy_font(scp, LOAD, 14, scp->sc->font_14);
3924 	    }
3925 	    /*
3926 	     * FONT KLUDGE:
3927 	     * This is an interim kludge to display correct font.
3928 	     * Always use the font page #0 on the video plane 2.
3929 	     * Somehow we cannot show the font in other font pages on
3930 	     * some video cards... XXX
3931 	     */
3932 	    (*vidsw[scp->sc->adapter]->show_font)(scp->sc->adp, 0);
3933 	}
3934 	mark_all(scp);
3935     }
3936 #endif /* !SC_NO_FONT_LOADING */
3937 
3938     set_border(scp, scp->border);
3939     sc_set_cursor_image(scp);
3940 
3941     return 0;
3942 }
3943 
3944 void
3945 set_border(scr_stat *scp, int color)
3946 {
3947     ++scp->sc->videoio_in_progress;
3948     (*scp->rndr->draw_border)(scp, color);
3949     --scp->sc->videoio_in_progress;
3950 }
3951 
3952 #ifndef SC_NO_FONT_LOADING
3953 void
3954 copy_font(scr_stat *scp, int operation, int font_size, u_char *buf)
3955 {
3956     /*
3957      * FONT KLUDGE:
3958      * This is an interim kludge to display correct font.
3959      * Always use the font page #0 on the video plane 2.
3960      * Somehow we cannot show the font in other font pages on
3961      * some video cards... XXX
3962      */
3963     scp->sc->font_loading_in_progress = TRUE;
3964     if (operation == LOAD) {
3965 	(*vidsw[scp->sc->adapter]->load_font)(scp->sc->adp, 0, font_size,
3966 					      buf, 0, 256);
3967     } else if (operation == SAVE) {
3968 	(*vidsw[scp->sc->adapter]->save_font)(scp->sc->adp, 0, font_size,
3969 					      buf, 0, 256);
3970     }
3971     scp->sc->font_loading_in_progress = FALSE;
3972 }
3973 #endif /* !SC_NO_FONT_LOADING */
3974 
3975 #ifndef SC_NO_SYSMOUSE
3976 struct tty
3977 *sc_get_mouse_tty(void)
3978 {
3979     return sc_mouse_tty;
3980 }
3981 #endif /* !SC_NO_SYSMOUSE */
3982 
3983 #ifndef SC_NO_CUTPASTE
3984 void
3985 sc_paste(scr_stat *scp, u_char *p, int count)
3986 {
3987     struct tty *tp;
3988     u_char *rmap;
3989 
3990     if (scp->status & MOUSE_VISIBLE) {
3991 	tp = VIRTUAL_TTY(scp->sc, scp->sc->cur_scp->index);
3992 	if (!(tp->t_state & TS_ISOPEN))
3993 	    return;
3994 	rmap = scp->sc->scr_rmap;
3995 	for (; count > 0; --count)
3996 	    (*linesw[tp->t_line].l_rint)(rmap[*p++], tp);
3997     }
3998 }
3999 #endif /* SC_NO_CUTPASTE */
4000 
4001 static void
4002 do_bell(scr_stat *scp, int pitch, int duration)
4003 {
4004     if (cold || shutdown_in_progress)
4005 	return;
4006 
4007     if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
4008 	return;
4009 
4010     if (scp->sc->flags & SC_VISUAL_BELL) {
4011 	if (scp->sc->blink_in_progress)
4012 	    return;
4013 	scp->sc->blink_in_progress = 3;
4014 	if (scp != scp->sc->cur_scp)
4015 	    scp->sc->blink_in_progress += 2;
4016 	blink_screen(scp->sc->cur_scp);
4017     } else {
4018 	if (scp != scp->sc->cur_scp)
4019 	    pitch *= 2;
4020 	sysbeep(pitch, duration);
4021     }
4022 }
4023 
4024 static void
4025 blink_screen(void *arg)
4026 {
4027     scr_stat *scp = arg;
4028     struct tty *tp;
4029 
4030     if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
4031 	scp->sc->blink_in_progress = 0;
4032     	mark_all(scp);
4033 	tp = VIRTUAL_TTY(scp->sc, scp->index);
4034 	if (tp->t_state & TS_ISOPEN)
4035 	    scstart(tp);
4036 	if (scp->sc->delayed_next_scr)
4037 	    switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
4038     }
4039     else {
4040 	(*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
4041 			   scp->sc->blink_in_progress & 1);
4042 	scp->sc->blink_in_progress--;
4043 	timeout(blink_screen, scp, hz / 10);
4044     }
4045 }
4046 
4047 #endif /* NSC */
4048