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