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