xref: /freebsd-14.2/sys/dev/syscons/syscons.c (revision ef5d438e)
1 /*-
2  * Copyright (c) 1992-1995 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  *    in this position and unchanged.
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 withough 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  *  $Id: syscons.c,v 1.141 1996/02/08 06:30:31 pst Exp $
29  */
30 
31 #include "sc.h"
32 #include "apm.h"
33 #include "opt_ddb.h"
34 
35 #if NSC > 0
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/conf.h>
39 #include <sys/ioctl.h>
40 #include <sys/proc.h>
41 #include <sys/signalvar.h>
42 #include <sys/tty.h>
43 #include <sys/uio.h>
44 #include <sys/callout.h>
45 #include <sys/kernel.h>
46 #include <sys/syslog.h>
47 #include <sys/errno.h>
48 #include <sys/malloc.h>
49 #include <sys/devconf.h>
50 #ifdef	DEVFS
51 #include <sys/devfsext.h>
52 #endif
53 
54 #include <machine/clock.h>
55 #include <machine/cons.h>
56 #include <machine/console.h>
57 #include <machine/psl.h>
58 #include <machine/frame.h>
59 #include <machine/pc/display.h>
60 #include <machine/apm_bios.h>
61 #include <machine/random.h>
62 
63 #include <vm/vm.h>
64 #include <vm/vm_param.h>
65 #include <vm/pmap.h>
66 
67 #include <i386/isa/isa.h>
68 #include <i386/isa/isa_device.h>
69 #include <i386/isa/timerreg.h>
70 #include <i386/isa/kbdtables.h>
71 #include <i386/isa/syscons.h>
72 
73 #if !defined(MAXCONS)
74 #define MAXCONS 16
75 #endif
76 
77 
78 /* this may break on older VGA's but is usefull on real 32 bit systems */
79 #define bcopyw  bcopy
80 
81 static default_attr user_default = {
82     (FG_LIGHTGREY | BG_BLACK) << 8,
83     (FG_BLACK | BG_LIGHTGREY) << 8
84 };
85 
86 static default_attr kernel_default = {
87     (FG_WHITE | BG_BLACK) << 8,
88     (FG_BLACK | BG_LIGHTGREY) << 8
89 };
90 
91 static  scr_stat    	main_console;
92 static  scr_stat    	*console[MAXCONS];
93 static	void		*sc_devfs_token[MAXCONS];
94 	scr_stat    	*cur_console;
95 static  scr_stat    	*new_scp, *old_scp;
96 static  term_stat   	kernel_console;
97 static  default_attr    *current_default;
98 static  char        	init_done = FALSE;
99 static  int     	configuration = 0;
100 static  char        	switch_in_progress = FALSE;
101 static  char        	blink_in_progress = FALSE;
102 static  char        	write_in_progress = FALSE;
103 	u_int       	crtc_addr = MONO_BASE;
104 static  char        	crtc_vga = FALSE;
105 static  u_char      	shfts = 0, ctls = 0, alts = 0, agrs = 0, metas = 0;
106 static  u_char      	nlkcnt = 0, clkcnt = 0, slkcnt = 0, alkcnt = 0;
107 static  char        	*font_8 = NULL, *font_14 = NULL, *font_16 = NULL;
108 static  int     	fonts_loaded = 0;
109 	char        	palette[3*256];
110 static  const u_int     n_fkey_tab = sizeof(fkey_tab) / sizeof(*fkey_tab);
111 static  int     	delayed_next_scr = FALSE;
112 static  long        	scrn_blank_time = 0;    /* screen saver timeout value */
113 	int     	scrn_blanked = FALSE;   /* screen saver active flag */
114 static  long       	scrn_time_stamp;
115 	u_char      	scr_map[256];
116 static  char        	*video_mode_ptr = NULL;
117 #if ASYNCH
118 static  u_char      	kbd_reply = 0;
119 #endif
120 
121 static  u_short mouse_and_mask[16] = {
122 	0xc000, 0xe000, 0xf000, 0xf800, 0xfc00, 0xfe00, 0xff00, 0xff80,
123 	0xfe00, 0x1e00, 0x1f00, 0x0f00, 0x0f00, 0x0000, 0x0000, 0x0000
124 };
125 static  u_short mouse_or_mask[16] = {
126 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7c00, 0x7e00, 0x6800,
127 	0x0c00, 0x0c00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000
128 };
129 
130 static void    none_saver(int blank) { }
131 
132 void    (*current_saver) __P((int blank)) = none_saver;
133 
134 static int scattach(struct isa_device *dev);
135 static int scparam(struct tty *tp, struct termios *t);
136 static int scprobe(struct isa_device *dev);
137 static void scstart(struct tty *tp);
138 
139 /* OS specific stuff */
140 #ifdef not_yet_done
141 #define VIRTUAL_TTY(x)  (sccons[x] = ttymalloc(sccons[x]))
142 struct  CONSOLE_TTY 	(sccons[MAXCONS] = ttymalloc(sccons[MAXCONS]))
143 struct  tty         	*sccons[MAXCONS+1];
144 #else
145 #define VIRTUAL_TTY(x)  &sccons[x]
146 #define CONSOLE_TTY 	&sccons[MAXCONS]
147 static struct tty     	sccons[MAXCONS+1];
148 #endif
149 #define MONO_BUF    	pa_to_va(0xB0000)
150 #define CGA_BUF     	pa_to_va(0xB8000)
151 u_short         	*Crtat;
152 
153 #define WRAPHIST(scp, pointer, offset)\
154     ((scp->history) + ((((pointer) - (scp->history)) + (scp->history_size)\
155     + (offset)) % (scp->history_size)))
156 
157 struct  isa_driver scdriver = {
158     scprobe, scattach, "sc", 1
159 };
160 
161 static	d_open_t	scopen;
162 static	d_close_t	scclose;
163 static	d_read_t	scread;
164 static	d_write_t	scwrite;
165 static	d_ioctl_t	scioctl;
166 static	d_devtotty_t	scdevtotty;
167 static	d_mmap_t	scmmap;
168 
169 #define CDEV_MAJOR 12
170 static	struct cdevsw	scdevsw = {
171 	scopen,		scclose,	scread,		scwrite,
172 	scioctl,	nullstop,	noreset,	scdevtotty,
173 	ttselect,	scmmap,		nostrategy,	"sc",	NULL,	-1 };
174 
175 /*
176  * Calculate hardware attributes word using logical attributes mask and
177  * hardware colors
178  */
179 
180 static int
181 mask2attr(struct term_stat *term)
182 {
183     int attr, mask = term->attr_mask;
184 
185     if (mask & REVERSE_ATTR) {
186 	attr = ((mask & FOREGROUND_CHANGED) ?
187 		((term->cur_color & 0xF000) >> 4) :
188 		(term->rev_color & 0x0F00)) |
189 	       ((mask & BACKGROUND_CHANGED) ?
190 		((term->cur_color & 0x0F00) << 4) :
191 		(term->rev_color & 0xF000));
192     } else
193 	attr = term->cur_color;
194 
195     /* XXX: underline mapping for Hercules adapter can be better */
196     if (mask & (BOLD_ATTR | UNDERLINE_ATTR))
197 	attr ^= 0x0800;
198     if (mask & BLINK_ATTR)
199 	attr ^= 0x8000;
200 
201     return attr;
202 }
203 
204 static int
205 scprobe(struct isa_device *dev)
206 {
207     int i, retries = 5;
208     unsigned char val;
209 
210     /* Enable interrupts and keyboard controller */
211     kbd_wait();
212     outb(KB_STAT, KB_WRITE);
213     kbd_wait();
214     outb(KB_DATA, KB_MODE);
215 
216     /* flush any noise in the buffer */
217     while (inb(KB_STAT) & KB_BUF_FULL) {
218 	DELAY(10);
219 	(void) inb(KB_DATA);
220     }
221 
222     /* Reset keyboard hardware */
223     while (retries--) {
224 	kbd_wait();
225 	outb(KB_DATA, KB_RESET);
226 	for (i=0; i<100000; i++) {
227 	    DELAY(10);
228 	    val = inb(KB_DATA);
229 	    if (val == KB_ACK || val == KB_ECHO)
230 		goto gotres;
231 	    if (val == KB_RESEND)
232 		break;
233 	}
234     }
235 gotres:
236     if (!retries)
237 	printf("scprobe: keyboard won't accept RESET command\n");
238     else {
239 gotack:
240 	DELAY(10);
241 	while ((inb(KB_STAT) & KB_BUF_FULL) == 0) DELAY(10);
242 	DELAY(10);
243 	val = inb(KB_DATA);
244 	if (val == KB_ACK)
245 	    goto gotack;
246 	if (val != KB_RESET_DONE)
247 	    printf("scprobe: keyboard RESET failed %02x\n", val);
248     }
249 #ifdef XT_KEYBOARD
250     kbd_wait();
251     outb(KB_DATA, 0xF0);
252     kbd_wait();
253     outb(KB_DATA, 1);
254     kbd_wait();
255 #endif /* XT_KEYBOARD */
256     return (IO_KBDSIZE);
257 }
258 
259 static struct kern_devconf kdc_sc[NSC] = {
260     0, 0, 0,        		/* filled in by dev_attach */
261     "sc", 0, { MDDT_ISA, 0, "tty" },
262     isa_generic_externalize, 0, 0, ISA_EXTERNALLEN,
263     &kdc_isa0,      		/* parent */
264     0,          		/* parentdata */
265     DC_BUSY,        		/* the console is almost always busy */
266     "Graphics console",
267     DC_CLS_DISPLAY		/* class */
268 };
269 
270 static inline void
271 sc_registerdev(struct isa_device *id)
272 {
273     if(id->id_unit)
274 	kdc_sc[id->id_unit] = kdc_sc[0];
275     kdc_sc[id->id_unit].kdc_unit = id->id_unit;
276     kdc_sc[id->id_unit].kdc_isa = id;
277     dev_attach(&kdc_sc[id->id_unit]);
278 }
279 
280 #if NAPM > 0
281 static int
282 scresume(void *dummy)
283 {
284 	shfts = 0;
285 	ctls = 0;
286 	alts = 0;
287 	agrs = 0;
288 	metas = 0;
289 	return 0;
290 }
291 #endif
292 
293 static int
294 scattach(struct isa_device *dev)
295 {
296     scr_stat	*scp;
297     int		vc;
298 
299     scinit();
300     configuration = dev->id_flags;
301 
302     scp = console[0];
303 
304     if (crtc_vga) {
305 	font_8 = (char *)malloc(8*256, M_DEVBUF, M_NOWAIT);
306 	font_14 = (char *)malloc(14*256, M_DEVBUF, M_NOWAIT);
307 	font_16 = (char *)malloc(16*256, M_DEVBUF, M_NOWAIT);
308 	copy_font(SAVE, FONT_16, font_16);
309 	fonts_loaded = FONT_16;
310 	scp->font = FONT_16;
311 	save_palette();
312     }
313 
314     scp->scr_buf = (u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
315 				     M_DEVBUF, M_NOWAIT);
316     /* copy screen to buffer */
317     bcopyw(Crtat, scp->scr_buf, scp->xsize * scp->ysize * sizeof(u_short));
318     scp->cursor_pos = scp->scr_buf + scp->xpos + scp->ypos * scp->xsize;
319     scp->mouse_pos = scp->scr_buf;
320 
321     /* initialize history buffer & pointers */
322     scp->history_head = scp->history_pos = scp->history =
323 	(u_short *)malloc(scp->history_size*sizeof(u_short),
324 			  M_DEVBUF, M_NOWAIT);
325     bzero(scp->history_head, scp->history_size*sizeof(u_short));
326 
327     /* initialize cursor stuff */
328     draw_cursor(scp, TRUE);
329     if (crtc_vga && (configuration & CHAR_CURSOR))
330 	set_destructive_cursor(scp, TRUE);
331 
332     /* get screen update going */
333     scrn_timer();
334 
335     update_leds(scp->status);
336     sc_registerdev(dev);
337 
338     printf("sc%d: ", dev->id_unit);
339     if (crtc_vga)
340 	if (crtc_addr == MONO_BASE)
341 	    printf("VGA mono");
342 	else
343 	    printf("VGA color");
344     else
345 	if (crtc_addr == MONO_BASE)
346 	    printf("MDA/hercules");
347 	else
348 	    printf("CGA/EGA");
349     printf(" <%d virtual consoles, flags=0x%x>\n", MAXCONS, configuration);
350 
351 #if NAPM > 0
352     scp->r_hook.ah_fun = scresume;
353     scp->r_hook.ah_arg = NULL;
354     scp->r_hook.ah_name = "system keyboard";
355     scp->r_hook.ah_order = APM_MID_ORDER;
356     apm_hook_establish(APM_HOOK_RESUME , &scp->r_hook);
357 #endif
358 
359 #ifdef DEVFS
360     for ( vc = 0 ; vc < MAXCONS; vc++) {
361         sc_devfs_token[vc] = devfs_add_devswf(&scdevsw, vc,
362 					DV_CHR, 0, 0, 0600, "ttyv%n", vc );
363     }
364 #endif
365     {
366     dev_t dev = makedev(CDEV_MAJOR, 0);
367 
368     cdevsw_add(&dev, &scdevsw, NULL);
369     }
370 
371     return 0;
372 }
373 
374 struct tty
375 *scdevtotty(dev_t dev)
376 {
377     int unit = minor(dev);
378 
379     if (!init_done)
380 	return(NULL);
381     if (unit > MAXCONS || unit < 0)
382 	return(NULL);
383     if (unit == MAXCONS)
384 	return CONSOLE_TTY;
385     return VIRTUAL_TTY(unit);
386 }
387 
388 static scr_stat
389 *get_scr_stat(dev_t dev)
390 {
391     int unit = minor(dev);
392 
393     if (unit > MAXCONS || unit < 0)
394 	return(NULL);
395     if (unit == MAXCONS)
396 	return console[0];
397     return console[unit];
398 }
399 
400 static int
401 get_scr_num()
402 {
403     int i = 0;
404 
405     while ((i < MAXCONS) && (cur_console != console[i]))
406 	i++;
407     return i < MAXCONS ? i : 0;
408 }
409 
410 int
411 scopen(dev_t dev, int flag, int mode, struct proc *p)
412 {
413     struct tty *tp = scdevtotty(dev);
414 
415     if (!tp)
416 	return(ENXIO);
417 
418     tp->t_oproc = scstart;
419     tp->t_param = scparam;
420     tp->t_dev = dev;
421     if (!(tp->t_state & TS_ISOPEN)) {
422 	ttychars(tp);
423 	tp->t_iflag = TTYDEF_IFLAG;
424 	tp->t_oflag = TTYDEF_OFLAG;
425 	tp->t_cflag = TTYDEF_CFLAG;
426 	tp->t_lflag = TTYDEF_LFLAG;
427 	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
428 	scparam(tp, &tp->t_termios);
429 	ttsetwater(tp);
430 	(*linesw[tp->t_line].l_modem)(tp, 1);
431     }
432     else
433 	if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
434 	    return(EBUSY);
435     if (!console[minor(dev)])
436 	console[minor(dev)] = alloc_scp();
437     return((*linesw[tp->t_line].l_open)(dev, tp));
438 }
439 
440 int
441 scclose(dev_t dev, int flag, int mode, struct proc *p)
442 {
443     struct tty *tp = scdevtotty(dev);
444     struct scr_stat *scp;
445 
446     if (!tp)
447 	return(ENXIO);
448     if (minor(dev) < MAXCONS) {
449 	scp = get_scr_stat(tp->t_dev);
450 	if (scp->status & SWITCH_WAIT_ACQ)
451 	    wakeup((caddr_t)&scp->smode);
452 #if not_yet_done
453 	if (scp == &main_console) {
454 	    scp->pid = 0;
455 	    scp->proc = NULL;
456 	    scp->smode.mode = VT_AUTO;
457 	}
458 	else {
459 	    free(scp->scr_buf, M_DEVBUF);
460 	    free(scp->history, M_DEVBUF);
461 	    free(scp, M_DEVBUF);
462 	    console[minor(dev)] = NULL;
463 	}
464 #else
465 	scp->pid = 0;
466 	scp->proc = NULL;
467 	scp->smode.mode = VT_AUTO;
468 #endif
469     }
470     (*linesw[tp->t_line].l_close)(tp, flag);
471     ttyclose(tp);
472     return(0);
473 }
474 
475 int
476 scread(dev_t dev, struct uio *uio, int flag)
477 {
478     struct tty *tp = scdevtotty(dev);
479 
480     if (!tp)
481 	return(ENXIO);
482     return((*linesw[tp->t_line].l_read)(tp, uio, flag));
483 }
484 
485 int
486 scwrite(dev_t dev, struct uio *uio, int flag)
487 {
488     struct tty *tp = scdevtotty(dev);
489 
490     if (!tp)
491 	return(ENXIO);
492     return((*linesw[tp->t_line].l_write)(tp, uio, flag));
493 }
494 
495 void
496 scintr(int unit)
497 {
498     static struct tty *cur_tty;
499     int c, len;
500     u_char *cp;
501 
502     /* make screensaver happy */
503     scrn_time_stamp = time.tv_sec;
504     if (scrn_blanked) {
505 	(*current_saver)(FALSE);
506 	cur_console->start = 0;
507 	cur_console->end = cur_console->xsize * cur_console->ysize;
508     }
509 
510     c = scgetc(1);
511 
512     cur_tty = VIRTUAL_TTY(get_scr_num());
513     if (!(cur_tty->t_state & TS_ISOPEN))
514 	if (!((cur_tty = CONSOLE_TTY)->t_state & TS_ISOPEN))
515 	    return;
516 
517     switch (c & 0xff00) {
518     case 0x0000: /* normal key */
519 	(*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
520 	break;
521     case NOKEY: /* nothing there */
522 	break;
523     case FKEY:  /* function key, return string */
524 	if (cp = get_fstr((u_int)c, (u_int *)&len)) {
525 	    while (len-- >  0)
526 		(*linesw[cur_tty->t_line].l_rint)(*cp++ & 0xFF, cur_tty);
527 	}
528 	break;
529     case MKEY:  /* meta is active, prepend ESC */
530 	(*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
531 	(*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
532 	break;
533     case BKEY:  /* backtab fixed sequence (esc [ Z) */
534 	(*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
535 	(*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
536 	(*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
537 	break;
538     }
539 }
540 
541 static int
542 scparam(struct tty *tp, struct termios *t)
543 {
544     tp->t_ispeed = t->c_ispeed;
545     tp->t_ospeed = t->c_ospeed;
546     tp->t_cflag = t->c_cflag;
547     return 0;
548 }
549 
550 int
551 scioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
552 {
553     int i, error;
554     struct tty *tp;
555     struct trapframe *fp;
556     scr_stat *scp;
557 
558     tp = scdevtotty(dev);
559     if (!tp)
560 	return ENXIO;
561     scp = get_scr_stat(tp->t_dev);
562 
563     switch (cmd) {  		/* process console hardware related ioctl's */
564 
565     case GIO_ATTR:      	/* get current attributes */
566 	*(int*)data = scp->term.cur_attr;
567 	return 0;
568 
569     case GIO_COLOR:     	/* is this a color console ? */
570 	if (crtc_addr == COLOR_BASE)
571 	    *(int*)data = 1;
572 	else
573 	    *(int*)data = 0;
574 	return 0;
575 
576     case CONS_CURRENT:  	/* get current adapter type */
577 	if (crtc_vga)
578 	    *(int*)data = KD_VGA;
579 	else
580 	    if (crtc_addr == MONO_BASE)
581 		*(int*)data = KD_MONO;
582 	    else
583 		*(int*)data = KD_CGA;
584 	return 0;
585 
586     case CONS_GET:      	/* get current video mode */
587 	*(int*)data = scp->mode;
588 	return 0;
589 
590     case CONS_BLANKTIME:    	/* set screen saver timeout (0 = no saver) */
591 	scrn_blank_time = *(int*)data;
592 	return 0;
593 
594     case CONS_CURSORTYPE:   	/* set cursor type blink/noblink */
595 	if ((*(int*)data) & 0x01)
596 	    configuration |= BLINK_CURSOR;
597 	else
598 	    configuration &= ~BLINK_CURSOR;
599 	if ((*(int*)data) & 0x02) {
600 	    configuration |= CHAR_CURSOR;
601 	    set_destructive_cursor(scp, TRUE);
602 	} else
603 	    configuration &= ~CHAR_CURSOR;
604 	return 0;
605 
606     case CONS_BELLTYPE: 	/* set bell type sound/visual */
607 	if (*data)
608 	    configuration |= VISUAL_BELL;
609 	else
610 	    configuration &= ~VISUAL_BELL;
611 	return 0;
612 
613     case CONS_HISTORY:  	/* set history size */
614 	if (*data) {
615 	    free(scp->history, M_DEVBUF);
616 	    scp->history_size = *(int*)data;
617 	    if (scp->history_size < scp->ysize)
618 		scp->history = NULL;
619 	    else {
620 		scp->history_size *= scp->xsize;
621 		scp->history_head = scp->history_pos = scp->history =
622 		    (u_short *)malloc(scp->history_size*sizeof(u_short),
623 				      M_DEVBUF, M_WAITOK);
624 		bzero(scp->history_head, scp->history_size*sizeof(u_short));
625 	    }
626 	    return 0;
627 	}
628 	else
629 	    return EINVAL;
630 
631     case CONS_MOUSECTL:		/* control mouse arrow */
632     {
633 	mouse_info_t *mouse = (mouse_info_t*)data;
634 	int fontsize;
635 
636 	switch (scp->font) {
637 	default:
638 	case FONT_8:
639 	    fontsize = 8; break;
640 	case FONT_14:
641 	    fontsize = 14; break;
642 	case FONT_16:
643 	    fontsize = 16; break;
644 	}
645 	switch (mouse->operation) {
646 	case MOUSE_SHOW:
647 	    if (!(scp->status & MOUSE_ENABLED)) {
648 		scp->mouse_oldpos = Crtat + (scp->mouse_pos - scp->scr_buf);
649 		scp->status |= (UPDATE_MOUSE | MOUSE_ENABLED);
650 	    }
651 	    else
652 		return EINVAL;
653 	    break;
654 
655 	case MOUSE_HIDE:
656 	    if (scp->status & MOUSE_ENABLED) {
657 		scp->status &= ~MOUSE_ENABLED;
658 		scp->status |= UPDATE_MOUSE;
659 	    }
660 	    else
661 		return EINVAL;
662 	    break;
663 
664 	case MOUSE_MOVEABS:
665 	    scp->mouse_xpos = mouse->x;
666 	    scp->mouse_ypos = mouse->y;
667 	    goto set_mouse_pos;
668 
669 	case MOUSE_MOVEREL:
670 	    scp->mouse_xpos += mouse->x;
671 	    scp->mouse_ypos += mouse->y;
672 set_mouse_pos:
673 	    if (scp->mouse_xpos < 0)
674 		scp->mouse_xpos = 0;
675 	    if (scp->mouse_ypos < 0)
676 		scp->mouse_ypos = 0;
677 	    if (scp->mouse_xpos >= scp->xsize*8)
678 		scp->mouse_xpos = (scp->xsize*8)-1;
679 	    if (scp->mouse_ypos >= scp->ysize*fontsize)
680 		scp->mouse_ypos = (scp->ysize*fontsize)-1;
681 	    scp->mouse_pos = scp->scr_buf +
682 		(scp->mouse_ypos/fontsize)*scp->xsize + scp->mouse_xpos/8;
683 	    if (scp->status & MOUSE_ENABLED)
684 		scp->status |= UPDATE_MOUSE;
685 	    break;
686 
687 	case MOUSE_GETPOS:
688 	    mouse->x = scp->mouse_xpos;
689 	    mouse->y = scp->mouse_ypos;
690 	    return 0;
691 
692 	default:
693 	    return EINVAL;
694 	}
695 	/* make screensaver happy */
696 	if (scp == cur_console) {
697 	    scrn_time_stamp = time.tv_sec;
698 	    if (scrn_blanked) {
699 		(*current_saver)(FALSE);
700 		cur_console->start = 0;
701 		cur_console->end = cur_console->xsize * cur_console->ysize;
702 	    }
703 	}
704 	return 0;
705     }
706 
707     case CONS_GETINFO:  	/* get current (virtual) console info */
708     {
709 	vid_info_t *ptr = (vid_info_t*)data;
710 	if (ptr->size == sizeof(struct vid_info)) {
711 	    ptr->m_num = get_scr_num();
712 	    ptr->mv_col = scp->xpos;
713 	    ptr->mv_row = scp->ypos;
714 	    ptr->mv_csz = scp->xsize;
715 	    ptr->mv_rsz = scp->ysize;
716 	    ptr->mv_norm.fore = (scp->term.std_color & 0x0f00)>>8;
717 	    ptr->mv_norm.back = (scp->term.std_color & 0xf000)>>12;
718 	    ptr->mv_rev.fore = (scp->term.rev_color & 0x0f00)>>8;
719 	    ptr->mv_rev.back = (scp->term.rev_color & 0xf000)>>12;
720 	    ptr->mv_grfc.fore = 0;      /* not supported */
721 	    ptr->mv_grfc.back = 0;      /* not supported */
722 	    ptr->mv_ovscan = scp->border;
723 	    ptr->mk_keylock = scp->status & LOCK_KEY_MASK;
724 	    return 0;
725 	}
726 	return EINVAL;
727     }
728 
729     case CONS_GETVERS:  	/* get version number */
730 	*(int*)data = 0x200;    /* version 2.0 */
731 	return 0;
732 
733     /* VGA TEXT MODES */
734     case SW_VGA_C40x25:
735     case SW_VGA_C80x25: case SW_VGA_M80x25:
736     case SW_VGA_C80x30: case SW_VGA_M80x30:
737     case SW_VGA_C80x50: case SW_VGA_M80x50:
738     case SW_VGA_C80x60: case SW_VGA_M80x60:
739     case SW_B40x25:     case SW_C40x25:
740     case SW_B80x25:     case SW_C80x25:
741     case SW_ENH_B40x25: case SW_ENH_C40x25:
742     case SW_ENH_B80x25: case SW_ENH_C80x25:
743     case SW_ENH_B80x43: case SW_ENH_C80x43:
744 
745 	if (!crtc_vga || video_mode_ptr == NULL)
746 	    return ENXIO;
747 	switch (cmd & 0xff) {
748 	case M_VGA_C80x60: case M_VGA_M80x60:
749 	    if (!(fonts_loaded & FONT_8))
750 		return EINVAL;
751 	    scp->xsize = 80;
752 	    scp->ysize = 60;
753 	    break;
754 	case M_VGA_C80x50: case M_VGA_M80x50:
755 	    if (!(fonts_loaded & FONT_8))
756 		return EINVAL;
757 	    scp->xsize = 80;
758 	    scp->ysize = 50;
759 	    break;
760 	case M_ENH_B80x43: case M_ENH_C80x43:
761 	    if (!(fonts_loaded & FONT_8))
762 		return EINVAL;
763 	    scp->xsize = 80;
764 	    scp->ysize = 43;
765 	    break;
766 	case M_VGA_C80x30: case M_VGA_M80x30:
767 	    scp->xsize = 80;
768 	    scp->ysize = 30;
769 	    break;
770 	default:
771 	    if ((cmd & 0xff) > M_VGA_CG320)
772 		return EINVAL;
773 	    else
774 		scp->xsize = *(video_mode_ptr+((cmd&0xff)*64));
775 		scp->ysize = *(video_mode_ptr+((cmd&0xff)*64)+1)+1;
776 	    break;
777 	}
778 	scp->mode = cmd & 0xff;
779 	scp->status &= ~UNKNOWN_MODE;   /* text mode */
780 	free(scp->scr_buf, M_DEVBUF);
781 	scp->scr_buf = (u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
782 					 M_DEVBUF, M_WAITOK);
783 	if (scp == cur_console)
784 	    set_mode(scp);
785 	clear_screen(scp);
786 	if (tp->t_winsize.ws_col != scp->xsize
787 	    || tp->t_winsize.ws_row != scp->ysize) {
788 	    tp->t_winsize.ws_col = scp->xsize;
789 	    tp->t_winsize.ws_row = scp->ysize;
790 	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
791 	}
792 	return 0;
793 
794     /* GRAPHICS MODES */
795     case SW_BG320:     case SW_BG640:
796     case SW_CG320:     case SW_CG320_D:   case SW_CG640_E:
797     case SW_CG640x350: case SW_ENH_CG640:
798     case SW_BG640x480: case SW_CG640x480: case SW_VGA_CG320:
799 
800 	if (!crtc_vga || video_mode_ptr == NULL)
801 	    return ENXIO;
802 	scp->mode = cmd & 0xFF;
803 	scp->status |= UNKNOWN_MODE;    /* graphics mode */
804 	scp->xsize = (*(video_mode_ptr + (scp->mode*64))) * 8;
805 	scp->ysize = (*(video_mode_ptr + (scp->mode*64) + 1) + 1) *
806 		     (*(video_mode_ptr + (scp->mode*64) + 2));
807 	set_mode(scp);
808 	/* clear_graphics();*/
809 
810 	if (tp->t_winsize.ws_xpixel != scp->xsize
811 	    || tp->t_winsize.ws_ypixel != scp->ysize) {
812 	    tp->t_winsize.ws_xpixel = scp->xsize;
813 	    tp->t_winsize.ws_ypixel = scp->ysize;
814 	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
815 	}
816 	return 0;
817 
818     case VT_SETMODE:    	/* set screen switcher mode */
819 	bcopy(data, &scp->smode, sizeof(struct vt_mode));
820 	if (scp->smode.mode == VT_PROCESS) {
821 	    scp->proc = p;
822 	    scp->pid = scp->proc->p_pid;
823 	}
824 	return 0;
825 
826     case VT_GETMODE:    	/* get screen switcher mode */
827 	bcopy(&scp->smode, data, sizeof(struct vt_mode));
828 	return 0;
829 
830     case VT_RELDISP:    	/* screen switcher ioctl */
831 	switch(*data) {
832 	case VT_FALSE:  	/* user refuses to release screen, abort */
833 	    if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
834 		old_scp->status &= ~SWITCH_WAIT_REL;
835 		switch_in_progress = FALSE;
836 		return 0;
837 	    }
838 	    return EINVAL;
839 
840 	case VT_TRUE:   	/* user has released screen, go on */
841 	    if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
842 		scp->status &= ~SWITCH_WAIT_REL;
843 		exchange_scr();
844 		if (new_scp->smode.mode == VT_PROCESS) {
845 		    new_scp->status |= SWITCH_WAIT_ACQ;
846 		    psignal(new_scp->proc, new_scp->smode.acqsig);
847 		}
848 		else
849 		    switch_in_progress = FALSE;
850 		return 0;
851 	    }
852 	    return EINVAL;
853 
854 	case VT_ACKACQ: 	/* acquire acknowledged, switch completed */
855 	    if (scp == new_scp && (scp->status & SWITCH_WAIT_ACQ)) {
856 		scp->status &= ~SWITCH_WAIT_ACQ;
857 		switch_in_progress = FALSE;
858 		return 0;
859 	    }
860 	    return EINVAL;
861 
862 	default:
863 	    return EINVAL;
864 	}
865 	/* NOT REACHED */
866 
867     case VT_OPENQRY:    	/* return free virtual console */
868 	for (i = 0; i < MAXCONS; i++) {
869 	    tp = VIRTUAL_TTY(i);
870 	    if (!(tp->t_state & TS_ISOPEN)) {
871 		*data = i + 1;
872 		return 0;
873 	    }
874 	}
875 	return EINVAL;
876 
877     case VT_ACTIVATE:   	/* switch to screen *data */
878 	return switch_scr(scp, (*data) - 1);
879 
880     case VT_WAITACTIVE: 	/* wait for switch to occur */
881 	if (*data > MAXCONS || *data < 0)
882 	    return EINVAL;
883 	if (minor(dev) == (*data) - 1)
884 	    return 0;
885 	if (*data == 0) {
886 	    if (scp == cur_console)
887 		return 0;
888 	}
889 	else
890 	    scp = console[(*data) - 1];
891 	while ((error=tsleep((caddr_t)&scp->smode, PZERO|PCATCH,
892 			     "waitvt", 0)) == ERESTART) ;
893 	return error;
894 
895     case VT_GETACTIVE:
896 	*data = get_scr_num()+1;
897 	return 0;
898 
899     case KDENABIO:      	/* allow io operations */
900 	error = suser(p->p_ucred, &p->p_acflag);
901 	if (error != 0)
902 	    return error;
903 	fp = (struct trapframe *)p->p_md.md_regs;
904 	fp->tf_eflags |= PSL_IOPL;
905 	return 0;
906 
907     case KDDISABIO:     	/* disallow io operations (default) */
908 	fp = (struct trapframe *)p->p_md.md_regs;
909 	fp->tf_eflags &= ~PSL_IOPL;
910 	return 0;
911 
912     case KDSETMODE:     	/* set current mode of this (virtual) console */
913 	switch (*data) {
914 	case KD_TEXT:   	/* switch to TEXT (known) mode */
915 	    /* restore fonts & palette ! */
916 	    if (crtc_vga) {
917 		if (fonts_loaded & FONT_8)
918 		    copy_font(LOAD, FONT_8, font_8);
919 		if (fonts_loaded & FONT_14)
920 		    copy_font(LOAD, FONT_14, font_14);
921 		if (fonts_loaded & FONT_16)
922 		    copy_font(LOAD, FONT_16, font_16);
923 		if (configuration & CHAR_CURSOR)
924 		    set_destructive_cursor(scp, TRUE);
925 		load_palette();
926 	    }
927 	    /* FALL THROUGH */
928 
929 	case KD_TEXT1:  	/* switch to TEXT (known) mode */
930 	    /* no restore fonts & palette */
931 	    scp->status &= ~UNKNOWN_MODE;
932 	    if (crtc_vga && video_mode_ptr)
933 		set_mode(scp);
934 	    clear_screen(scp);
935 	    return 0;
936 
937 	case KD_GRAPHICS:	/* switch to GRAPHICS (unknown) mode */
938 	    scp->status |= UNKNOWN_MODE;
939 	    return 0;
940 	default:
941 	    return EINVAL;
942 	}
943 	/* NOT REACHED */
944 
945     case KDGETMODE:     	/* get current mode of this (virtual) console */
946 	*data = (scp->status & UNKNOWN_MODE) ? KD_GRAPHICS : KD_TEXT;
947 	return 0;
948 
949     case KDSBORDER:     	/* set border color of this (virtual) console */
950 	if (!crtc_vga)
951 	    return ENXIO;
952 	scp->border = *data;
953 	if (scp == cur_console)
954 	    set_border(scp->border);
955 	return 0;
956 
957     case KDSKBSTATE:    	/* set keyboard state (locks) */
958 	if (*data >= 0 && *data <= LOCK_KEY_MASK) {
959 	    scp->status &= ~LOCK_KEY_MASK;
960 	    scp->status |= *data;
961 	    if (scp == cur_console)
962 		update_leds(scp->status);
963 	    return 0;
964 	}
965 	return EINVAL;
966 
967     case KDGKBSTATE:    	/* get keyboard state (locks) */
968 	*data = scp->status & LOCK_KEY_MASK;
969 	return 0;
970 
971     case KDSETRAD:      	/* set keyboard repeat & delay rates */
972 	if (*data & 0x80)
973 	    return EINVAL;
974 	i = spltty();
975 	kbd_cmd(KB_SETRAD);
976 	kbd_cmd(*data);
977 	splx(i);
978 	return 0;
979 
980     case KDSKBMODE:     	/* set keyboard mode */
981 	switch (*data) {
982 	case K_RAW: 		/* switch to RAW scancode mode */
983 	    scp->status |= KBD_RAW_MODE;
984 	    return 0;
985 
986 	case K_XLATE:   	/* switch to XLT ascii mode */
987 	    if (scp == cur_console && scp->status == KBD_RAW_MODE)
988 		shfts = ctls = alts = agrs = metas = 0;
989 	    scp->status &= ~KBD_RAW_MODE;
990 	    return 0;
991 	default:
992 	    return EINVAL;
993 	}
994 	/* NOT REACHED */
995 
996     case KDGKBMODE:     	/* get keyboard mode */
997 	*data = (scp->status & KBD_RAW_MODE) ? K_RAW : K_XLATE;
998 	return 0;
999 
1000     case KDMKTONE:      	/* sound the bell */
1001 	if (*(int*)data)
1002 	    do_bell(scp, (*(int*)data)&0xffff,
1003 		    (((*(int*)data)>>16)&0xffff)*hz/1000);
1004 	else
1005 	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
1006 	return 0;
1007 
1008     case KIOCSOUND:     	/* make tone (*data) hz */
1009 	if (scp == cur_console) {
1010 	    if (*(int*)data) {
1011 		int pitch = TIMER_FREQ/(*(int*)data);
1012 
1013 		/* set command for counter 2, 2 byte write */
1014 		if (acquire_timer2(TIMER_16BIT|TIMER_SQWAVE))
1015 		    return EBUSY;
1016 
1017 		/* set pitch */
1018 		outb(TIMER_CNTR2, pitch);
1019 		outb(TIMER_CNTR2, (pitch>>8));
1020 
1021 		/* enable counter 2 output to speaker */
1022 		outb(IO_PPI, inb(IO_PPI) | 3);
1023 	    }
1024 	    else {
1025 		/* disable counter 2 output to speaker */
1026 		outb(IO_PPI, inb(IO_PPI) & 0xFC);
1027 		release_timer2();
1028 	    }
1029 	}
1030 	return 0;
1031 
1032     case KDGKBTYPE:     	/* get keyboard type */
1033 	*data = 0;  		/* type not known (yet) */
1034 	return 0;
1035 
1036     case KDSETLED:      	/* set keyboard LED status */
1037 	if (*data >= 0 && *data <= LED_MASK) {
1038 	    scp->status &= ~LED_MASK;
1039 	    scp->status |= *data;
1040 	    if (scp == cur_console)
1041 		update_leds(scp->status);
1042 	    return 0;
1043 	}
1044 	return EINVAL;
1045 
1046     case KDGETLED:      	/* get keyboard LED status */
1047 	*data = scp->status & LED_MASK;
1048 	return 0;
1049 
1050     case GETFKEY:       	/* get functionkey string */
1051 	if (*(u_short*)data < n_fkey_tab) {
1052 	    fkeyarg_t *ptr = (fkeyarg_t*)data;
1053 	    bcopy(&fkey_tab[ptr->keynum].str, ptr->keydef,
1054 		  fkey_tab[ptr->keynum].len);
1055 	    ptr->flen = fkey_tab[ptr->keynum].len;
1056 	    return 0;
1057 	}
1058 	else
1059 	    return EINVAL;
1060 
1061     case SETFKEY:       	/* set functionkey string */
1062 	if (*(u_short*)data < n_fkey_tab) {
1063 	    fkeyarg_t *ptr = (fkeyarg_t*)data;
1064 	    bcopy(ptr->keydef, &fkey_tab[ptr->keynum].str,
1065 		  min(ptr->flen, MAXFK));
1066 	    fkey_tab[ptr->keynum].len = min(ptr->flen, MAXFK);
1067 	    return 0;
1068 	}
1069 	else
1070 	    return EINVAL;
1071 
1072     case GIO_SCRNMAP:   	/* get output translation table */
1073 	bcopy(&scr_map, data, sizeof(scr_map));
1074 	return 0;
1075 
1076     case PIO_SCRNMAP:   	/* set output translation table */
1077 	bcopy(data, &scr_map, sizeof(scr_map));
1078 	return 0;
1079 
1080     case GIO_KEYMAP:    	/* get keyboard translation table */
1081 	bcopy(&key_map, data, sizeof(key_map));
1082 	return 0;
1083 
1084     case PIO_KEYMAP:    	/* set keyboard translation table */
1085 	bcopy(data, &key_map, sizeof(key_map));
1086 	return 0;
1087 
1088     case PIO_FONT8x8:   	/* set 8x8 dot font */
1089 	if (!crtc_vga)
1090 	    return ENXIO;
1091 	bcopy(data, font_8, 8*256);
1092 	fonts_loaded |= FONT_8;
1093 	copy_font(LOAD, FONT_8, font_8);
1094 	if (configuration & CHAR_CURSOR)
1095 	    set_destructive_cursor(scp, TRUE);
1096 	return 0;
1097 
1098     case GIO_FONT8x8:   	/* get 8x8 dot font */
1099 	if (!crtc_vga)
1100 	    return ENXIO;
1101 	if (fonts_loaded & FONT_8) {
1102 	    bcopy(font_8, data, 8*256);
1103 	    return 0;
1104 	}
1105 	else
1106 	    return ENXIO;
1107 
1108     case PIO_FONT8x14:  	/* set 8x14 dot font */
1109 	if (!crtc_vga)
1110 	    return ENXIO;
1111 	bcopy(data, font_14, 14*256);
1112 	fonts_loaded |= FONT_14;
1113 	copy_font(LOAD, FONT_14, font_14);
1114 	if (configuration & CHAR_CURSOR)
1115 	    set_destructive_cursor(scp, TRUE);
1116 	return 0;
1117 
1118     case GIO_FONT8x14:  	/* get 8x14 dot font */
1119 	if (!crtc_vga)
1120 	    return ENXIO;
1121 	if (fonts_loaded & FONT_14) {
1122 	    bcopy(font_14, data, 14*256);
1123 	    return 0;
1124 	}
1125 	else
1126 	    return ENXIO;
1127 
1128     case PIO_FONT8x16:  	/* set 8x16 dot font */
1129 	if (!crtc_vga)
1130 	    return ENXIO;
1131 	bcopy(data, font_16, 16*256);
1132 	fonts_loaded |= FONT_16;
1133 	copy_font(LOAD, FONT_16, font_16);
1134 	if (configuration & CHAR_CURSOR)
1135 	    set_destructive_cursor(scp, TRUE);
1136 	return 0;
1137 
1138     case GIO_FONT8x16:  	/* get 8x16 dot font */
1139 	if (!crtc_vga)
1140 	    return ENXIO;
1141 	if (fonts_loaded & FONT_16) {
1142 	    bcopy(font_16, data, 16*256);
1143 	    return 0;
1144 	}
1145 	else
1146 	    return ENXIO;
1147     default:
1148 	break;
1149     }
1150 
1151     error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1152     if (error >= 0)
1153 	return(error);
1154     error = ttioctl(tp, cmd, data, flag);
1155     if (error >= 0)
1156 	return(error);
1157     return(ENOTTY);
1158 }
1159 
1160 static void
1161 scstart(struct tty *tp)
1162 {
1163     struct clist *rbp;
1164     int s, len;
1165     u_char buf[PCBURST];
1166     scr_stat *scp = get_scr_stat(tp->t_dev);
1167 
1168     /* XXX who repeats the call when the above flags are cleared? */
1169     if (scp->status & SLKED || blink_in_progress)
1170 	return;
1171     s = spltty();
1172     if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1173 	tp->t_state |= TS_BUSY;
1174 	rbp = &tp->t_outq;
1175 	while (rbp->c_cc) {
1176 	    len = q_to_b(rbp, buf, PCBURST);
1177 	    splx(s);
1178 	    ansi_put(scp, buf, len);
1179 	    s = spltty();
1180 	}
1181 	tp->t_state &= ~TS_BUSY;
1182 	ttwwakeup(tp);
1183     }
1184     splx(s);
1185 }
1186 
1187 void
1188 sccnprobe(struct consdev *cp)
1189 {
1190     struct isa_device *dvp;
1191 
1192     /*
1193      * Take control if we are the highest priority enabled display device.
1194      */
1195     dvp = find_display();
1196     if (dvp != NULL && dvp->id_driver != &scdriver) {
1197 	cp->cn_pri = CN_DEAD;
1198 	return;
1199     }
1200 
1201     /* initialize required fields */
1202     cp->cn_dev = makedev(CDEV_MAJOR, MAXCONS);
1203     cp->cn_pri = CN_INTERNAL;
1204 }
1205 
1206 void
1207 sccninit(struct consdev *cp)
1208 {
1209     scinit();
1210 }
1211 
1212 void
1213 sccnputc(dev_t dev, int c)
1214 {
1215     u_char buf[1];
1216     scr_stat *scp = console[0];
1217     term_stat save = scp->term;
1218 
1219     scp->term = kernel_console;
1220     current_default = &kernel_default;
1221     if (scp->scr_buf == Crtat)
1222 	draw_cursor(scp, FALSE);
1223     buf[0] = c;
1224     ansi_put(scp, buf, 1);
1225     kernel_console = scp->term;
1226     current_default = &user_default;
1227     scp->term = save;
1228     if (scp == cur_console /* && scrn_timer not running */) {
1229 	if (scp->scr_buf != Crtat && (scp->start <= scp->end)) {
1230 	    bcopyw(scp->scr_buf + scp->start, Crtat + scp->start,
1231 		   (1 + scp->end - scp->start) * sizeof(u_short));
1232 	    scp->start = scp->xsize * scp->ysize;
1233 	    scp->end = 0;
1234 	    scp->status &= ~CURSOR_SHOWN;
1235 	}
1236 	draw_cursor(scp, TRUE);
1237     }
1238 }
1239 
1240 int
1241 sccngetc(dev_t dev)
1242 {
1243     int s = spltty();       /* block scintr while we poll */
1244     int c = scgetc(0);
1245     splx(s);
1246     return(c);
1247 }
1248 
1249 int
1250 sccncheckc(dev_t dev)
1251 {
1252     return (scgetc(1) & 0xff);
1253 }
1254 
1255 static void
1256 scrn_timer()
1257 {
1258     static int cursor_blinkrate;
1259     scr_stat *scp = cur_console;
1260 
1261     /* should we just return ? */
1262     if ((scp->status&UNKNOWN_MODE) || blink_in_progress || switch_in_progress) {
1263 	timeout((timeout_func_t)scrn_timer, 0, hz/10);
1264 	return;
1265     }
1266 
1267     if (!scrn_blanked) {
1268 	/* update screen image */
1269 	if (scp->start <= scp->end) {
1270 	    bcopyw(scp->scr_buf + scp->start, Crtat + scp->start,
1271 		   (1 + scp->end - scp->start) * sizeof(u_short));
1272 	    scp->status &= ~CURSOR_SHOWN;
1273 	    scp->start = scp->xsize * scp->ysize;
1274 	    scp->end = 0;
1275 	}
1276 	/* update "pseudo" mouse arrow */
1277 	if ((scp->status & MOUSE_ENABLED) && (scp->status & UPDATE_MOUSE))
1278 	    draw_mouse_image(scp);
1279 
1280 	/* update cursor image */
1281 	if (scp->status & CURSOR_ENABLED)
1282 	    draw_cursor(scp,
1283 		!(configuration&BLINK_CURSOR) || !(cursor_blinkrate++&0x04));
1284     }
1285     if (scrn_blank_time && (time.tv_sec>scrn_time_stamp+scrn_blank_time))
1286 	(*current_saver)(TRUE);
1287     timeout((timeout_func_t)scrn_timer, 0, hz/25);
1288 }
1289 
1290 static void
1291 clear_screen(scr_stat *scp)
1292 {
1293     move_crsr(scp, 0, 0);
1294     fillw(scp->term.cur_color | scr_map[0x20], scp->scr_buf,
1295 	  scp->xsize * scp->ysize);
1296     mark_all(scp);
1297 }
1298 
1299 static int
1300 switch_scr(scr_stat *scp, u_int next_scr)
1301 {
1302     if (switch_in_progress && (cur_console->proc != pfind(cur_console->pid)))
1303 	switch_in_progress = FALSE;
1304 
1305     if (next_scr >= MAXCONS || switch_in_progress ||
1306 	(cur_console->smode.mode == VT_AUTO
1307 	 && cur_console->status & UNKNOWN_MODE)) {
1308 	do_bell(scp, BELL_PITCH, BELL_DURATION);
1309 	return EINVAL;
1310     }
1311 
1312     /* is the wanted virtual console open ? */
1313     if (next_scr) {
1314 	struct tty *tp = VIRTUAL_TTY(next_scr);
1315 	if (!(tp->t_state & TS_ISOPEN)) {
1316 	    do_bell(scp, BELL_PITCH, BELL_DURATION);
1317 	    return EINVAL;
1318 	}
1319     }
1320     /* delay switch if actively updating screen */
1321     if (write_in_progress || blink_in_progress) {
1322 	delayed_next_scr = next_scr+1;
1323 	return 0;
1324     }
1325     switch_in_progress = TRUE;
1326     old_scp = cur_console;
1327     new_scp = console[next_scr];
1328     wakeup((caddr_t)&new_scp->smode);
1329     if (new_scp == old_scp) {
1330 	switch_in_progress = FALSE;
1331 	delayed_next_scr = FALSE;
1332 	return 0;
1333     }
1334 
1335     /* has controlling process died? */
1336     if (old_scp->proc && (old_scp->proc != pfind(old_scp->pid)))
1337 	old_scp->smode.mode = VT_AUTO;
1338     if (new_scp->proc && (new_scp->proc != pfind(new_scp->pid)))
1339 	new_scp->smode.mode = VT_AUTO;
1340 
1341     /* check the modes and switch approbiatly */
1342     if (old_scp->smode.mode == VT_PROCESS) {
1343 	old_scp->status |= SWITCH_WAIT_REL;
1344 	psignal(old_scp->proc, old_scp->smode.relsig);
1345     }
1346     else {
1347 	exchange_scr();
1348 	if (new_scp->smode.mode == VT_PROCESS) {
1349 	    new_scp->status |= SWITCH_WAIT_ACQ;
1350 	    psignal(new_scp->proc, new_scp->smode.acqsig);
1351 	}
1352 	else
1353 	    switch_in_progress = FALSE;
1354     }
1355     return 0;
1356 }
1357 
1358 static void
1359 exchange_scr(void)
1360 {
1361     move_crsr(old_scp, old_scp->xpos, old_scp->ypos);
1362     cur_console = new_scp;
1363     if (old_scp->mode != new_scp->mode || (old_scp->status & UNKNOWN_MODE)){
1364 	if (crtc_vga && video_mode_ptr)
1365 	    set_mode(new_scp);
1366     }
1367     move_crsr(new_scp, new_scp->xpos, new_scp->ypos);
1368     if ((old_scp->status & UNKNOWN_MODE) && crtc_vga) {
1369 	if (fonts_loaded & FONT_8)
1370 	    copy_font(LOAD, FONT_8, font_8);
1371 	if (fonts_loaded & FONT_14)
1372 	    copy_font(LOAD, FONT_14, font_14);
1373 	if (fonts_loaded & FONT_16)
1374 	    copy_font(LOAD, FONT_16, font_16);
1375 	if (configuration & CHAR_CURSOR)
1376 	    set_destructive_cursor(new_scp, TRUE);
1377 	load_palette();
1378     }
1379     if (old_scp->status & KBD_RAW_MODE || new_scp->status & KBD_RAW_MODE)
1380 	shfts = ctls = alts = agrs = metas = 0;
1381     update_leds(new_scp->status);
1382     delayed_next_scr = FALSE;
1383     bcopyw(new_scp->scr_buf, Crtat,
1384 	   (new_scp->xsize*new_scp->ysize)*sizeof(u_short));
1385     new_scp->status &= ~CURSOR_SHOWN;
1386 }
1387 
1388 static inline void
1389 move_crsr(scr_stat *scp, int x, int y)
1390 {
1391     if (x < 0 || y < 0 || x >= scp->xsize || y >= scp->ysize)
1392 	return;
1393     scp->xpos = x;
1394     scp->ypos = y;
1395     mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1396     scp->cursor_pos = scp->scr_buf + scp->ypos * scp->xsize + scp->xpos;
1397     mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1398 }
1399 
1400 static void
1401 scan_esc(scr_stat *scp, u_char c)
1402 {
1403     static u_char ansi_col[16] =
1404 	{0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15};
1405     int i, n;
1406     u_short *src, *dst, count;
1407 
1408     if (scp->term.esc == 1) {
1409 	switch (c) {
1410 
1411 	case '[':   /* Start ESC [ sequence */
1412 	    scp->term.esc = 2;
1413 	    scp->term.last_param = -1;
1414 	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
1415 		scp->term.param[i] = 1;
1416 	    scp->term.num_param = 0;
1417 	    return;
1418 
1419 	case 'M':   /* Move cursor up 1 line, scroll if at top */
1420 	    if (scp->ypos > 0)
1421 		move_crsr(scp, scp->xpos, scp->ypos - 1);
1422 	    else {
1423 		bcopyw(scp->scr_buf, scp->scr_buf + scp->xsize,
1424 		       (scp->ysize - 1) * scp->xsize * sizeof(u_short));
1425 		fillw(scp->term.cur_color | scr_map[0x20],
1426 		      scp->scr_buf, scp->xsize);
1427     		mark_all(scp);
1428 	    }
1429 	    break;
1430 #if notyet
1431 	case 'Q':
1432 	    scp->term.esc = 4;
1433 	    break;
1434 #endif
1435 	case 'c':   /* Clear screen & home */
1436 	    clear_screen(scp);
1437 	    break;
1438 	}
1439     }
1440     else if (scp->term.esc == 2) {
1441 	if (c >= '0' && c <= '9') {
1442 	    if (scp->term.num_param < MAX_ESC_PAR) {
1443 	    if (scp->term.last_param != scp->term.num_param) {
1444 		scp->term.last_param = scp->term.num_param;
1445 		scp->term.param[scp->term.num_param] = 0;
1446 	    }
1447 	    else
1448 		scp->term.param[scp->term.num_param] *= 10;
1449 	    scp->term.param[scp->term.num_param] += c - '0';
1450 	    return;
1451 	    }
1452 	}
1453 	scp->term.num_param = scp->term.last_param + 1;
1454 	switch (c) {
1455 
1456 	case ';':
1457 	    if (scp->term.num_param < MAX_ESC_PAR)
1458 		return;
1459 	    break;
1460 
1461 	case '=':
1462 	    scp->term.esc = 3;
1463 	    scp->term.last_param = -1;
1464 	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
1465 		scp->term.param[i] = 1;
1466 	    scp->term.num_param = 0;
1467 	    return;
1468 
1469 	case 'A':   /* up n rows */
1470 	    n = scp->term.param[0]; if (n < 1) n = 1;
1471 	    move_crsr(scp, scp->xpos, scp->ypos - n);
1472 	    break;
1473 
1474 	case 'B':   /* down n rows */
1475 	    n = scp->term.param[0]; if (n < 1) n = 1;
1476 	    move_crsr(scp, scp->xpos, scp->ypos + n);
1477 	    break;
1478 
1479 	case 'C':   /* right n columns */
1480 	    n = scp->term.param[0]; if (n < 1) n = 1;
1481 	    move_crsr(scp, scp->xpos + n, scp->ypos);
1482 	    break;
1483 
1484 	case 'D':   /* left n columns */
1485 	    n = scp->term.param[0]; if (n < 1) n = 1;
1486 	    move_crsr(scp, scp->xpos - n, scp->ypos);
1487 	    break;
1488 
1489 	case 'E':   /* cursor to start of line n lines down */
1490 	    n = scp->term.param[0]; if (n < 1) n = 1;
1491 	    move_crsr(scp, 0, scp->ypos + n);
1492 	    break;
1493 
1494 	case 'F':   /* cursor to start of line n lines up */
1495 	    n = scp->term.param[0]; if (n < 1) n = 1;
1496 	    move_crsr(scp, 0, scp->ypos - n);
1497 	    break;
1498 
1499 	case 'f':   /* Cursor move */
1500 	case 'H':
1501 	    if (scp->term.num_param == 0)
1502 		move_crsr(scp, 0, 0);
1503 	    else if (scp->term.num_param == 2)
1504 		move_crsr(scp, scp->term.param[1] - 1, scp->term.param[0] - 1);
1505 	    break;
1506 
1507 	case 'J':   /* Clear all or part of display */
1508 	    if (scp->term.num_param == 0)
1509 		n = 0;
1510 	    else
1511 		n = scp->term.param[0];
1512 	    switch (n) {
1513 	    case 0: /* clear form cursor to end of display */
1514 		fillw(scp->term.cur_color | scr_map[0x20],
1515 		      scp->cursor_pos,
1516 		      scp->scr_buf + scp->xsize * scp->ysize - scp->cursor_pos);
1517     		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1518     		mark_for_update(scp, scp->xsize * scp->ysize);
1519 		break;
1520 	    case 1: /* clear from beginning of display to cursor */
1521 		fillw(scp->term.cur_color | scr_map[0x20],
1522 		      scp->scr_buf,
1523 		      scp->cursor_pos - scp->scr_buf);
1524     		mark_for_update(scp, 0);
1525     		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1526 		break;
1527 	    case 2: /* clear entire display */
1528 		clear_screen(scp);
1529 		break;
1530 	    }
1531 	    break;
1532 
1533 	case 'K':   /* Clear all or part of line */
1534 	    if (scp->term.num_param == 0)
1535 		n = 0;
1536 	    else
1537 		n = scp->term.param[0];
1538 	    switch (n) {
1539 	    case 0: /* clear form cursor to end of line */
1540 		fillw(scp->term.cur_color | scr_map[0x20],
1541 		      scp->cursor_pos,
1542 		      scp->xsize - scp->xpos);
1543     		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1544     		mark_for_update(scp, scp->cursor_pos - scp->scr_buf +
1545 				scp->xsize - scp->xpos);
1546 		break;
1547 	    case 1: /* clear from beginning of line to cursor */
1548 		fillw(scp->term.cur_color | scr_map[0x20],
1549 		      scp->cursor_pos - (scp->xsize - scp->xpos),
1550 		      (scp->xsize - scp->xpos) + 1);
1551     		mark_for_update(scp, scp->ypos * scp->xsize);
1552     		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1553 		break;
1554 	    case 2: /* clear entire line */
1555 		fillw(scp->term.cur_color | scr_map[0x20],
1556 		      scp->cursor_pos - (scp->xsize - scp->xpos),
1557 		      scp->xsize);
1558     		mark_for_update(scp, scp->ypos * scp->xsize);
1559     		mark_for_update(scp, (scp->ypos + 1) * scp->xsize);
1560 		break;
1561 	    }
1562 	    break;
1563 
1564 	case 'L':   /* Insert n lines */
1565 	    n = scp->term.param[0]; if (n < 1) n = 1;
1566 	    if (n > scp->ysize - scp->ypos)
1567 		n = scp->ysize - scp->ypos;
1568 	    src = scp->scr_buf + scp->ypos * scp->xsize;
1569 	    dst = src + n * scp->xsize;
1570 	    count = scp->ysize - (scp->ypos + n);
1571 	    bcopyw(src, dst, count * scp->xsize * sizeof(u_short));
1572 	    fillw(scp->term.cur_color | scr_map[0x20], src,
1573 		  n * scp->xsize);
1574 	    mark_for_update(scp, scp->ypos * scp->xsize);
1575 	    mark_for_update(scp, scp->xsize * scp->ysize);
1576 	    break;
1577 
1578 	case 'M':   /* Delete n lines */
1579 	    n = scp->term.param[0]; if (n < 1) n = 1;
1580 	    if (n > scp->ysize - scp->ypos)
1581 		n = scp->ysize - scp->ypos;
1582 	    dst = scp->scr_buf + scp->ypos * scp->xsize;
1583 	    src = dst + n * scp->xsize;
1584 	    count = scp->ysize - (scp->ypos + n);
1585 	    bcopyw(src, dst, count * scp->xsize * sizeof(u_short));
1586 	    src = dst + count * scp->xsize;
1587 	    fillw(scp->term.cur_color | scr_map[0x20], src,
1588 		  n * scp->xsize);
1589 	    mark_for_update(scp, scp->ypos * scp->xsize);
1590 	    mark_for_update(scp, scp->xsize * scp->ysize);
1591 	    break;
1592 
1593 	case 'P':   /* Delete n chars */
1594 	    n = scp->term.param[0]; if (n < 1) n = 1;
1595 	    if (n > scp->xsize - scp->xpos)
1596 		n = scp->xsize - scp->xpos;
1597 	    dst = scp->cursor_pos;
1598 	    src = dst + n;
1599 	    count = scp->xsize - (scp->xpos + n);
1600 	    bcopyw(src, dst, count * sizeof(u_short));
1601 	    src = dst + count;
1602 	    fillw(scp->term.cur_color | scr_map[0x20], src, n);
1603 	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1604 	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
1605 	    break;
1606 
1607 	case '@':   /* Insert n chars */
1608 	    n = scp->term.param[0]; if (n < 1) n = 1;
1609 	    if (n > scp->xsize - scp->xpos)
1610 		n = scp->xsize - scp->xpos;
1611 	    src = scp->cursor_pos;
1612 	    dst = src + n;
1613 	    count = scp->xsize - (scp->xpos + n);
1614 	    bcopyw(src, dst, count * sizeof(u_short));
1615 	    fillw(scp->term.cur_color | scr_map[0x20], src, n);
1616 	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1617 	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
1618 	    break;
1619 
1620 	case 'S':   /* scroll up n lines */
1621 	    n = scp->term.param[0]; if (n < 1)  n = 1;
1622 	    if (n > scp->ysize)
1623 		n = scp->ysize;
1624 	    bcopyw(scp->scr_buf + (scp->xsize * n),
1625 		   scp->scr_buf,
1626 		   scp->xsize * (scp->ysize - n) * sizeof(u_short));
1627 	    fillw(scp->term.cur_color | scr_map[0x20],
1628 		  scp->scr_buf + scp->xsize * (scp->ysize - n),
1629 		  scp->xsize * n);
1630     	    mark_all(scp);
1631 	    break;
1632 
1633 	case 'T':   /* scroll down n lines */
1634 	    n = scp->term.param[0]; if (n < 1)  n = 1;
1635 	    if (n > scp->ysize)
1636 		n = scp->ysize;
1637 	    bcopyw(scp->scr_buf,
1638 		  scp->scr_buf + (scp->xsize * n),
1639 		  scp->xsize * (scp->ysize - n) *
1640 		  sizeof(u_short));
1641 	    fillw(scp->term.cur_color | scr_map[0x20],
1642 		  scp->scr_buf, scp->xsize * n);
1643     	    mark_all(scp);
1644 	    break;
1645 
1646 	case 'X':   /* erase n characters in line */
1647 	    n = scp->term.param[0]; if (n < 1)  n = 1;
1648 	    if (n > scp->xsize - scp->xpos)
1649 		n = scp->xsize - scp->xpos;
1650 	    fillw(scp->term.cur_color | scr_map[0x20],
1651 		  scp->scr_buf + scp->xpos +
1652 		  ((scp->xsize*scp->ypos) * sizeof(u_short)), n);
1653 	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1654 	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n);
1655 	    break;
1656 
1657 	case 'Z':   /* move n tabs backwards */
1658 	    n = scp->term.param[0]; if (n < 1)  n = 1;
1659 	    if ((i = scp->xpos & 0xf8) == scp->xpos)
1660 		i -= 8*n;
1661 	    else
1662 		i -= 8*(n-1);
1663 	    if (i < 0)
1664 		i = 0;
1665 	    move_crsr(scp, i, scp->ypos);
1666 	    break;
1667 
1668 	case '`':   /* move cursor to column n */
1669 	    n = scp->term.param[0]; if (n < 1)  n = 1;
1670 	    move_crsr(scp, n - 1, scp->ypos);
1671 	    break;
1672 
1673 	case 'a':   /* move cursor n columns to the right */
1674 	    n = scp->term.param[0]; if (n < 1)  n = 1;
1675 	    move_crsr(scp, scp->xpos + n, scp->ypos);
1676 	    break;
1677 
1678 	case 'd':   /* move cursor to row n */
1679 	    n = scp->term.param[0]; if (n < 1)  n = 1;
1680 	    move_crsr(scp, scp->xpos, n - 1);
1681 	    break;
1682 
1683 	case 'e':   /* move cursor n rows down */
1684 	    n = scp->term.param[0]; if (n < 1)  n = 1;
1685 	    move_crsr(scp, scp->xpos, scp->ypos + n);
1686 	    break;
1687 
1688 	case 'm':   /* change attribute */
1689 	    if (scp->term.num_param == 0) {
1690 		scp->term.attr_mask = NORMAL_ATTR;
1691 		scp->term.cur_attr =
1692 		    scp->term.cur_color = scp->term.std_color;
1693 		break;
1694 	    }
1695 	    for (i = 0; i < scp->term.num_param; i++) {
1696 		switch (n = scp->term.param[i]) {
1697 		case 0: /* back to normal */
1698 		    scp->term.attr_mask = NORMAL_ATTR;
1699 		    scp->term.cur_attr =
1700 			scp->term.cur_color = scp->term.std_color;
1701 		    break;
1702 		case 1: /* bold */
1703 		    scp->term.attr_mask |= BOLD_ATTR;
1704 		    scp->term.cur_attr = mask2attr(&scp->term);
1705 		    break;
1706 		case 4: /* underline */
1707 		    scp->term.attr_mask |= UNDERLINE_ATTR;
1708 		    scp->term.cur_attr = mask2attr(&scp->term);
1709 		    break;
1710 		case 5: /* blink */
1711 		    scp->term.attr_mask |= BLINK_ATTR;
1712 		    scp->term.cur_attr = mask2attr(&scp->term);
1713 		    break;
1714 		case 7: /* reverse video */
1715 		    scp->term.attr_mask |= REVERSE_ATTR;
1716 		    scp->term.cur_attr = mask2attr(&scp->term);
1717 		    break;
1718 		case 30: case 31: /* set fg color */
1719 		case 32: case 33: case 34:
1720 		case 35: case 36: case 37:
1721 		    scp->term.attr_mask |= FOREGROUND_CHANGED;
1722 		    scp->term.cur_color =
1723 			(scp->term.cur_color&0xF000) | (ansi_col[(n-30)&7]<<8);
1724 		    scp->term.cur_attr = mask2attr(&scp->term);
1725 		    break;
1726 		case 40: case 41: /* set bg color */
1727 		case 42: case 43: case 44:
1728 		case 45: case 46: case 47:
1729 		    scp->term.attr_mask |= BACKGROUND_CHANGED;
1730 		    scp->term.cur_color =
1731 			(scp->term.cur_color&0x0F00) | (ansi_col[(n-40)&7]<<12);
1732 		    scp->term.cur_attr = mask2attr(&scp->term);
1733 		    break;
1734 		}
1735 	    }
1736 	    break;
1737 
1738 	case 'x':
1739 	    if (scp->term.num_param == 0)
1740 		n = 0;
1741 	    else
1742 		n = scp->term.param[0];
1743 	    switch (n) {
1744 	    case 0:     /* reset attributes */
1745 		scp->term.attr_mask = NORMAL_ATTR;
1746 		scp->term.cur_attr =
1747 		    scp->term.cur_color = scp->term.std_color =
1748 		    current_default->std_color;
1749 		scp->term.rev_color = current_default->rev_color;
1750 		break;
1751 	    case 1:     /* set ansi background */
1752 		scp->term.attr_mask &= ~BACKGROUND_CHANGED;
1753 		scp->term.cur_color = scp->term.std_color =
1754 		    (scp->term.std_color & 0x0F00) |
1755 		    (ansi_col[(scp->term.param[1])&0x0F]<<12);
1756 		scp->term.cur_attr = mask2attr(&scp->term);
1757 		break;
1758 	    case 2:     /* set ansi foreground */
1759 		scp->term.attr_mask &= ~FOREGROUND_CHANGED;
1760 		scp->term.cur_color = scp->term.std_color =
1761 		    (scp->term.std_color & 0xF000) |
1762 		    (ansi_col[(scp->term.param[1])&0x0F]<<8);
1763 		scp->term.cur_attr = mask2attr(&scp->term);
1764 		break;
1765 	    case 3:     /* set ansi attribute directly */
1766 		scp->term.attr_mask &= ~(FOREGROUND_CHANGED|BACKGROUND_CHANGED);
1767 		scp->term.cur_color = scp->term.std_color =
1768 		    (scp->term.param[1]&0xFF)<<8;
1769 		scp->term.cur_attr = mask2attr(&scp->term);
1770 		break;
1771 	    case 5:     /* set ansi reverse video background */
1772 		scp->term.rev_color =
1773 		    (scp->term.rev_color & 0x0F00) |
1774 		    (ansi_col[(scp->term.param[1])&0x0F]<<12);
1775 		scp->term.cur_attr = mask2attr(&scp->term);
1776 		break;
1777 	    case 6:     /* set ansi reverse video foreground */
1778 		scp->term.rev_color =
1779 		    (scp->term.rev_color & 0xF000) |
1780 		    (ansi_col[(scp->term.param[1])&0x0F]<<8);
1781 		scp->term.cur_attr = mask2attr(&scp->term);
1782 		break;
1783 	    case 7:     /* set ansi reverse video directly */
1784 		scp->term.rev_color =
1785 		    (scp->term.param[1]&0xFF)<<8;
1786 		scp->term.cur_attr = mask2attr(&scp->term);
1787 		break;
1788 	    }
1789 	    break;
1790 
1791 	case 'z':   /* switch to (virtual) console n */
1792 	    if (scp->term.num_param == 1)
1793 		switch_scr(scp, scp->term.param[0]);
1794 	    break;
1795 	}
1796     }
1797     else if (scp->term.esc == 3) {
1798 	if (c >= '0' && c <= '9') {
1799 	    if (scp->term.num_param < MAX_ESC_PAR) {
1800 	    if (scp->term.last_param != scp->term.num_param) {
1801 		scp->term.last_param = scp->term.num_param;
1802 		scp->term.param[scp->term.num_param] = 0;
1803 	    }
1804 	    else
1805 		scp->term.param[scp->term.num_param] *= 10;
1806 	    scp->term.param[scp->term.num_param] += c - '0';
1807 	    return;
1808 	    }
1809 	}
1810 	scp->term.num_param = scp->term.last_param + 1;
1811 	switch (c) {
1812 
1813 	case ';':
1814 	    if (scp->term.num_param < MAX_ESC_PAR)
1815 		return;
1816 	    break;
1817 
1818 	case 'A':   /* set display border color */
1819 	    if (scp->term.num_param == 1)
1820 		scp->border=scp->term.param[0] & 0xff;
1821 		if (scp == cur_console)
1822 		    set_border(scp->border);
1823 	    break;
1824 
1825 	case 'B':   /* set bell pitch and duration */
1826 	    if (scp->term.num_param == 2) {
1827 		scp->bell_pitch = scp->term.param[0];
1828 		scp->bell_duration = scp->term.param[1]*10;
1829 	    }
1830 	    break;
1831 
1832 	case 'C':   /* set cursor type & shape */
1833 	    if (scp->term.num_param == 1) {
1834 		if (scp->term.param[0] & 0x01)
1835 		    configuration |= BLINK_CURSOR;
1836 		else
1837 		    configuration &= ~BLINK_CURSOR;
1838 		if (scp->term.param[0] & 0x02) {
1839 		    configuration |= CHAR_CURSOR;
1840 		    set_destructive_cursor(scp, TRUE);
1841 		} else
1842 		    configuration &= ~CHAR_CURSOR;
1843 	    }
1844 	    else if (scp->term.num_param == 2) {
1845 		scp->cursor_start = scp->term.param[0] & 0x1F;
1846 		scp->cursor_end = scp->term.param[1] & 0x1F;
1847 		if (configuration & CHAR_CURSOR)
1848 			set_destructive_cursor(scp, TRUE);
1849 	    }
1850 	    break;
1851 
1852 	case 'F':   /* set ansi foreground */
1853 	    if (scp->term.num_param == 1) {
1854 		scp->term.attr_mask &= ~FOREGROUND_CHANGED;
1855 		scp->term.cur_color = scp->term.std_color =
1856 		    (scp->term.std_color & 0xF000)
1857 		    | ((scp->term.param[0] & 0x0F) << 8);
1858 		scp->term.cur_attr = mask2attr(&scp->term);
1859 	    }
1860 	    break;
1861 
1862 	case 'G':   /* set ansi background */
1863 	    if (scp->term.num_param == 1) {
1864 		scp->term.attr_mask &= ~BACKGROUND_CHANGED;
1865 		scp->term.cur_color = scp->term.std_color =
1866 		    (scp->term.std_color & 0x0F00)
1867 		    | ((scp->term.param[0] & 0x0F) << 12);
1868 		scp->term.cur_attr = mask2attr(&scp->term);
1869 	    }
1870 	    break;
1871 
1872 	case 'H':   /* set ansi reverse video foreground */
1873 	    if (scp->term.num_param == 1) {
1874 		scp->term.rev_color =
1875 		    (scp->term.rev_color & 0xF000)
1876 		    | ((scp->term.param[0] & 0x0F) << 8);
1877 		scp->term.cur_attr = mask2attr(&scp->term);
1878 	    }
1879 	    break;
1880 
1881 	case 'I':   /* set ansi reverse video background */
1882 	    if (scp->term.num_param == 1) {
1883 		scp->term.rev_color =
1884 		    (scp->term.rev_color & 0x0F00)
1885 		    | ((scp->term.param[0] & 0x0F) << 12);
1886 		scp->term.cur_attr = mask2attr(&scp->term);
1887 	    }
1888 	    break;
1889 	}
1890     }
1891     scp->term.esc = 0;
1892 }
1893 
1894 static inline void
1895 draw_cursor(scr_stat *scp, int show)
1896 {
1897     if (show && !(scp->status & CURSOR_SHOWN)) {
1898 	u_short cursor_image = *(Crtat + (scp->cursor_pos - scp->scr_buf));
1899 
1900 	scp->cursor_saveunder = cursor_image;
1901 	if (configuration & CHAR_CURSOR) {
1902 	    set_destructive_cursor(scp, FALSE);
1903 	    cursor_image = (cursor_image & 0xff00) | DEAD_CHAR;
1904 	}
1905 	else {
1906 	    if ((cursor_image & 0x7000) == 0x7000) {
1907 		cursor_image &= 0x8fff;
1908 		if(!(cursor_image & 0x0700))
1909 		    cursor_image |= 0x0700;
1910 	    } else {
1911 		cursor_image |= 0x7000;
1912 		if ((cursor_image & 0x0700) == 0x0700)
1913 		    cursor_image &= 0xf0ff;
1914 	    }
1915 	}
1916 	*(Crtat + (scp->cursor_pos - scp->scr_buf)) = cursor_image;
1917 	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1918 	scp->status |= CURSOR_SHOWN;
1919     }
1920     if (!show && (scp->status & CURSOR_SHOWN)) {
1921 	*(Crtat + (scp->cursor_pos - scp->scr_buf)) = scp->cursor_saveunder;
1922 	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1923 	scp->status &= ~CURSOR_SHOWN;
1924     }
1925 }
1926 
1927 static void
1928 ansi_put(scr_stat *scp, u_char *buf, int len)
1929 {
1930     u_char *ptr = buf;
1931 
1932     if (scp->status & UNKNOWN_MODE)
1933 	return;
1934 
1935     /* make screensaver happy */
1936     if (scp == cur_console) {
1937 	scrn_time_stamp = time.tv_sec;
1938 	if (scrn_blanked) {
1939 	    (*current_saver)(FALSE);
1940 	    cur_console->start = 0;
1941 	    cur_console->end = cur_console->xsize * cur_console->ysize;
1942 	}
1943     }
1944     write_in_progress++;
1945 outloop:
1946     if (scp->term.esc) {
1947 	scan_esc(scp, *ptr++);
1948 	len--;
1949     }
1950     else if (PRINTABLE(*ptr)) {     /* Print only printables */
1951  	int cnt = len <= (scp->xsize-scp->xpos) ? len : (scp->xsize-scp->xpos);
1952  	u_short cur_attr = scp->term.cur_attr;
1953  	u_short *cursor_pos = scp->cursor_pos;
1954 	do {
1955 	    /*
1956 	     * gcc-2.6.3 generates poor (un)sign extension code.  Casting the
1957 	     * pointers in the following to volatile should have no effect,
1958 	     * but in fact speeds up this inner loop from 26 to 18 cycles
1959 	     * (+ cache misses) on i486's.
1960 	     */
1961 #define	UCVP(ucp)	((u_char volatile *)(ucp))
1962 	    *cursor_pos++ = UCVP(scr_map)[*UCVP(ptr)] | cur_attr;
1963 	    ptr++;
1964 	    cnt--;
1965 	} while (cnt && PRINTABLE(*ptr));
1966 	len -= (cursor_pos - scp->cursor_pos);
1967 	scp->xpos += (cursor_pos - scp->cursor_pos);
1968 	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1969 	mark_for_update(scp, cursor_pos - scp->scr_buf);
1970 	scp->cursor_pos = cursor_pos;
1971 	if (scp->xpos >= scp->xsize) {
1972 	    scp->xpos = 0;
1973 	    scp->ypos++;
1974 	}
1975     }
1976     else  {
1977 	switch(*ptr) {
1978 	case 0x07:
1979 	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
1980 	    break;
1981 
1982 	case 0x08:      /* non-destructive backspace */
1983 	    if (scp->cursor_pos > scp->scr_buf) {
1984 	    	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1985 		scp->cursor_pos--;
1986 	    	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1987 		if (scp->xpos > 0)
1988 		    scp->xpos--;
1989 		else {
1990 		    scp->xpos += scp->xsize - 1;
1991 		    scp->ypos--;
1992 		}
1993 	    }
1994 	    break;
1995 
1996 	case 0x09:  /* non-destructive tab */
1997 	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1998 	    scp->cursor_pos += (8 - scp->xpos % 8u);
1999 	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2000 	    if ((scp->xpos += (8 - scp->xpos % 8u)) >= scp->xsize) {
2001 	        scp->xpos = 0;
2002 	        scp->ypos++;
2003 	    }
2004 	    break;
2005 
2006 	case 0x0a:  /* newline, same pos */
2007 	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2008 	    scp->cursor_pos += scp->xsize;
2009 	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2010 	    scp->ypos++;
2011 	    break;
2012 
2013 	case 0x0c:  /* form feed, clears screen */
2014 	    clear_screen(scp);
2015 	    break;
2016 
2017 	case 0x0d:  /* return, return to pos 0 */
2018 	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2019 	    scp->cursor_pos -= scp->xpos;
2020 	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2021 	    scp->xpos = 0;
2022 	    break;
2023 
2024 	case 0x1b:  /* start escape sequence */
2025 	    scp->term.esc = 1;
2026 	    scp->term.num_param = 0;
2027 	    break;
2028 	}
2029 	ptr++; len--;
2030     }
2031     /* do we have to scroll ?? */
2032     if (scp->cursor_pos >= scp->scr_buf + scp->ysize * scp->xsize) {
2033 	if (scp->history) {
2034 	    bcopyw(scp->scr_buf, scp->history_head,
2035 		   scp->xsize * sizeof(u_short));
2036 	    scp->history_head += scp->xsize;
2037 	    if (scp->history_head + scp->xsize >
2038 		scp->history + scp->history_size)
2039 		scp->history_head = scp->history;
2040 	}
2041 	bcopyw(scp->scr_buf + scp->xsize, scp->scr_buf,
2042 	       scp->xsize * (scp->ysize - 1) * sizeof(u_short));
2043 	fillw(scp->term.cur_color | scr_map[0x20],
2044 	      scp->scr_buf + scp->xsize * (scp->ysize - 1),
2045 	      scp->xsize);
2046 	scp->cursor_pos -= scp->xsize;
2047 	scp->ypos--;
2048     	mark_all(scp);
2049     }
2050     if (len)
2051 	goto outloop;
2052     write_in_progress--;
2053     if (delayed_next_scr)
2054 	switch_scr(scp, delayed_next_scr - 1);
2055 }
2056 
2057 static void
2058 scinit(void)
2059 {
2060     u_short volatile *cp;
2061     u_short was;
2062     unsigned hw_cursor;
2063     int i;
2064 
2065     if (init_done)
2066 	return;
2067     init_done = TRUE;
2068     /*
2069      * Finish defaulting crtc variables for a mono screen.  Crtat is a
2070      * bogus common variable so that it can be shared with pcvt, so it
2071      * can't be statically initialized.  XXX.
2072      */
2073      Crtat = (u_short *)MONO_BUF;
2074     /*
2075      * If CGA memory seems to work, switch to color.
2076      */
2077     cp = (u_short *)CGA_BUF;
2078     was = *cp;
2079     *cp = (u_short) 0xA55A;
2080     if (*cp == 0xA55A) {
2081 	Crtat = (u_short *)cp;
2082 	crtc_addr = COLOR_BASE;
2083     }
2084     *cp = was;
2085 
2086     /*
2087      * Ensure a zero start address.  This is mainly to recover after
2088      * switching from pcvt using userconfig().  The registers are w/o
2089      * for old hardware so it's too hard to relocate the active screen
2090      * memory.
2091      */
2092     outb(crtc_addr, 12);
2093     outb(crtc_addr + 1, 0);
2094     outb(crtc_addr, 13);
2095     outb(crtc_addr + 1, 0);
2096 
2097     /* extract cursor location */
2098     outb(crtc_addr, 14);
2099     hw_cursor = inb(crtc_addr + 1) << 8;
2100     outb(crtc_addr, 15);
2101     hw_cursor |= inb(crtc_addr + 1);
2102 
2103     /* move hardware cursor out of the way */
2104     outb(crtc_addr, 14);
2105     outb(crtc_addr + 1, 0xff);
2106     outb(crtc_addr, 15);
2107     outb(crtc_addr + 1, 0xff);
2108 
2109     /* is this a VGA or higher ? */
2110     outb(crtc_addr, 7);
2111     if (inb(crtc_addr) == 7) {
2112 	u_long  pa;
2113 	u_long  segoff;
2114 
2115 	crtc_vga = TRUE;
2116 	/*
2117 	 * Get the BIOS video mode pointer.
2118 	 */
2119 	segoff = *(u_long *)pa_to_va(0x4a8);
2120 	pa = (((segoff & 0xffff0000) >> 12) + (segoff & 0xffff));
2121 	if (ISMAPPED(pa, sizeof(u_long))) {
2122 	    segoff = *(u_long *)pa_to_va(pa);
2123 	    pa = (((segoff & 0xffff0000) >> 12) + (segoff & 0xffff));
2124 	    if (ISMAPPED(pa, 64))
2125 		video_mode_ptr = (char *)pa_to_va(pa);
2126 	}
2127     }
2128     current_default = &user_default;
2129     console[0] = &main_console;
2130     init_scp(console[0]);
2131     console[0]->scr_buf = console[0]->mouse_pos =  Crtat;
2132     console[0]->cursor_pos = Crtat + hw_cursor;
2133     console[0]->xpos = hw_cursor % COL;
2134     console[0]->ypos = hw_cursor / COL;
2135     cur_console = console[0];
2136     for (i=1; i<MAXCONS; i++)
2137 	console[i] = NULL;
2138     kernel_console.esc = 0;
2139     kernel_console.attr_mask = NORMAL_ATTR;
2140     kernel_console.cur_attr =
2141 	kernel_console.cur_color = kernel_console.std_color =
2142 	kernel_default.std_color;
2143     kernel_console.rev_color = kernel_default.rev_color;
2144     /* initialize mapscrn array to a one to one map */
2145     for (i=0; i<sizeof(scr_map); i++)
2146 	scr_map[i] = i;
2147 }
2148 
2149 static scr_stat
2150 *alloc_scp()
2151 {
2152     scr_stat *scp;
2153 
2154     scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
2155     init_scp(scp);
2156     scp->scr_buf = scp->cursor_pos = scp->scr_buf = scp->mouse_pos =
2157 	(u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
2158 			  M_DEVBUF, M_WAITOK);
2159     scp->history_head = scp->history_pos = scp->history =
2160 	(u_short *)malloc(scp->history_size*sizeof(u_short),
2161 			  M_DEVBUF, M_WAITOK);
2162     bzero(scp->history_head, scp->history_size*sizeof(u_short));
2163     if (crtc_vga && video_mode_ptr)
2164 	set_mode(scp);
2165     clear_screen(scp);
2166     return scp;
2167 }
2168 
2169 static void
2170 init_scp(scr_stat *scp)
2171 {
2172     scp->mode = M_VGA_C80x25;
2173     scp->font = FONT_16;
2174     scp->xsize = COL;
2175     scp->ysize = ROW;
2176     scp->start = COL * ROW;
2177     scp->end = 0;
2178     scp->term.esc = 0;
2179     scp->term.attr_mask = NORMAL_ATTR;
2180     scp->term.cur_attr =
2181 	scp->term.cur_color = scp->term.std_color =
2182 	current_default->std_color;
2183     scp->term.rev_color = current_default->rev_color;
2184     scp->border = BG_BLACK;
2185     scp->cursor_start = *(char *)pa_to_va(0x461);
2186     scp->cursor_end = *(char *)pa_to_va(0x460);
2187     scp->mouse_xpos = scp->mouse_ypos = 0;
2188     scp->bell_pitch = BELL_PITCH;
2189     scp->bell_duration = BELL_DURATION;
2190     scp->status = (*(char *)pa_to_va(0x417) & 0x20) ? NLKED : 0;
2191     scp->status |= CURSOR_ENABLED;
2192     scp->pid = 0;
2193     scp->proc = NULL;
2194     scp->smode.mode = VT_AUTO;
2195     scp->history_head = scp->history_pos = scp->history = NULL;
2196     scp->history_size = HISTORY_SIZE;
2197 }
2198 
2199 static u_char
2200 *get_fstr(u_int c, u_int *len)
2201 {
2202     u_int i;
2203 
2204     if (!(c & FKEY))
2205 	return(NULL);
2206     i = (c & 0xFF) - F_FN;
2207     if (i > n_fkey_tab)
2208 	return(NULL);
2209     *len = fkey_tab[i].len;
2210     return(fkey_tab[i].str);
2211 }
2212 
2213 static void
2214 update_leds(int which)
2215 {
2216     int s;
2217     static u_char xlate_leds[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
2218 
2219     /* replace CAPS led with ALTGR led for ALTGR keyboards */
2220     if (key_map.n_keys > ALTGR_OFFSET) {
2221 	if (which & ALKED)
2222 	    which |= CLKED;
2223 	else
2224 	    which &= ~CLKED;
2225     }
2226     s = spltty();
2227     kbd_cmd(KB_SETLEDS);
2228     kbd_cmd(xlate_leds[which & LED_MASK]);
2229     splx(s);
2230 }
2231 
2232 static void
2233 history_to_screen(scr_stat *scp)
2234 {
2235     int i;
2236 
2237     for (i=0; i<scp->ysize; i++)
2238 	bcopyw(scp->history + (((scp->history_pos - scp->history) +
2239 	       scp->history_size-((i+1)*scp->xsize))%scp->history_size),
2240 	       scp->scr_buf + (scp->xsize * (scp->ysize-1 - i)),
2241 	       scp->xsize * sizeof(u_short));
2242     mark_all(scp);
2243 }
2244 
2245 static int
2246 history_up_line(scr_stat *scp)
2247 {
2248     if (WRAPHIST(scp, scp->history_pos, -(scp->xsize*scp->ysize)) !=
2249 	scp->history_head) {
2250 	scp->history_pos = WRAPHIST(scp, scp->history_pos, -scp->xsize);
2251 	history_to_screen(scp);
2252 	return 0;
2253     }
2254     else
2255 	return -1;
2256 }
2257 
2258 static int
2259 history_down_line(scr_stat *scp)
2260 {
2261     if (scp->history_pos != scp->history_head) {
2262 	scp->history_pos = WRAPHIST(scp, scp->history_pos, scp->xsize);
2263 	history_to_screen(scp);
2264 	return 0;
2265     }
2266     else
2267 	return -1;
2268 }
2269 
2270 /*
2271  * scgetc(noblock) - get character from keyboard.
2272  * If noblock = 0 wait until a key is pressed.
2273  * Else return NOKEY.
2274  */
2275 u_int
2276 scgetc(int noblock)
2277 {
2278     u_char scancode, keycode;
2279     u_int state, action;
2280     struct key_t *key;
2281     static u_char esc_flag = 0, compose = 0;
2282     static u_int chr = 0;
2283 
2284 next_code:
2285     kbd_wait();
2286     /* First see if there is something in the keyboard port */
2287     if (inb(KB_STAT) & KB_BUF_FULL)
2288 	scancode = inb(KB_DATA);
2289     else if (noblock)
2290 	return(NOKEY);
2291     else
2292 	goto next_code;
2293 
2294     add_keyboard_randomness(scancode);
2295 
2296     if (cur_console->status & KBD_RAW_MODE)
2297 	return scancode;
2298 #if ASYNCH
2299     if (scancode == KB_ACK || scancode == KB_RESEND) {
2300 	kbd_reply = scancode;
2301 	if (noblock)
2302 	    return(NOKEY);
2303 	goto next_code;
2304     }
2305 #endif
2306     keycode = scancode & 0x7F;
2307     switch (esc_flag) {
2308     case 0x00:      /* normal scancode */
2309 	switch(scancode) {
2310 	case 0xB8:  /* left alt  (compose key) */
2311 	    if (compose) {
2312 		compose = 0;
2313 		if (chr > 255) {
2314 		    do_bell(cur_console,
2315 			BELL_PITCH, BELL_DURATION);
2316 		    chr = 0;
2317 		}
2318 	    }
2319 	    break;
2320 	case 0x38:
2321 	    if (!compose) {
2322 		compose = 1;
2323 		chr = 0;
2324 	    }
2325 	    break;
2326 	case 0xE0:
2327 	case 0xE1:
2328 	    esc_flag = scancode;
2329 	    goto next_code;
2330 	}
2331 	break;
2332     case 0xE0:      /* 0xE0 prefix */
2333 	esc_flag = 0;
2334 	switch (keycode) {
2335 	case 0x1C:  /* right enter key */
2336 	    keycode = 0x59;
2337 	    break;
2338 	case 0x1D:  /* right ctrl key */
2339 	    keycode = 0x5A;
2340 	    break;
2341 	case 0x35:  /* keypad divide key */
2342 	    keycode = 0x5B;
2343 	    break;
2344 	case 0x37:  /* print scrn key */
2345 	    keycode = 0x5C;
2346 	    break;
2347 	case 0x38:  /* right alt key (alt gr) */
2348 	    keycode = 0x5D;
2349 	    break;
2350 	case 0x47:  /* grey home key */
2351 	    keycode = 0x5E;
2352 	    break;
2353 	case 0x48:  /* grey up arrow key */
2354 	    keycode = 0x5F;
2355 	    break;
2356 	case 0x49:  /* grey page up key */
2357 	    keycode = 0x60;
2358 	    break;
2359 	case 0x4B:  /* grey left arrow key */
2360 	    keycode = 0x61;
2361 	    break;
2362 	case 0x4D:  /* grey right arrow key */
2363 	    keycode = 0x62;
2364 	    break;
2365 	case 0x4F:  /* grey end key */
2366 	    keycode = 0x63;
2367 	    break;
2368 	case 0x50:  /* grey down arrow key */
2369 	    keycode = 0x64;
2370 	    break;
2371 	case 0x51:  /* grey page down key */
2372 	    keycode = 0x65;
2373 	    break;
2374 	case 0x52:  /* grey insert key */
2375 	    keycode = 0x66;
2376 	    break;
2377 	case 0x53:  /* grey delete key */
2378 	    keycode = 0x67;
2379 	    break;
2380 
2381 	/* the following 3 are only used on the MS "Natural" keyboard */
2382 	case 0x5b:  /* left Window key */
2383 	    keycode = 0x69;
2384 	    break;
2385 	case 0x5c:  /* right Window key */
2386 	    keycode = 0x6a;
2387 	    break;
2388 	case 0x5d:  /* menu key */
2389 	    keycode = 0x6b;
2390 	    break;
2391 	default:    /* ignore everything else */
2392 	    goto next_code;
2393 	}
2394 	break;
2395     case 0xE1:      /* 0xE1 prefix */
2396 	esc_flag = 0;
2397 	if (keycode == 0x1D)
2398 	    esc_flag = 0x1D;
2399 	goto next_code;
2400 	/* NOT REACHED */
2401     case 0x1D:      /* pause / break */
2402 	esc_flag = 0;
2403 	if (keycode != 0x45)
2404 	    goto next_code;
2405 	keycode = 0x68;
2406 	break;
2407     }
2408 
2409     /* if scroll-lock pressed allow history browsing */
2410     if (cur_console->history && cur_console->status & SLKED) {
2411 	int i;
2412 
2413 	cur_console->status &= ~CURSOR_ENABLED;
2414 	if (!(cur_console->status & BUFFER_SAVED)) {
2415 	    cur_console->status |= BUFFER_SAVED;
2416 	    cur_console->history_save = cur_console->history_head;
2417 
2418 	    /* copy screen into top of history buffer */
2419 	    for (i=0; i<cur_console->ysize; i++) {
2420 		bcopyw(cur_console->scr_buf + (cur_console->xsize * i),
2421 		       cur_console->history_head,
2422 		       cur_console->xsize * sizeof(u_short));
2423 		cur_console->history_head += cur_console->xsize;
2424 		if (cur_console->history_head + cur_console->xsize >
2425 		    cur_console->history + cur_console->history_size)
2426 		    cur_console->history_head=cur_console->history;
2427 	    }
2428 	    cur_console->history_pos = cur_console->history_head;
2429 	    history_to_screen(cur_console);
2430 	}
2431 	switch (scancode) {
2432 	case 0x47:  /* home key */
2433 	    cur_console->history_pos = cur_console->history_head;
2434 	    history_to_screen(cur_console);
2435 	    goto next_code;
2436 
2437 	case 0x4F:  /* end key */
2438 	    cur_console->history_pos =
2439 		WRAPHIST(cur_console, cur_console->history_head,
2440 			 cur_console->xsize*cur_console->ysize);
2441 	    history_to_screen(cur_console);
2442 	    goto next_code;
2443 
2444 	case 0x48:  /* up arrow key */
2445 	    if (history_up_line(cur_console))
2446 		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2447 	    goto next_code;
2448 
2449 	case 0x50:  /* down arrow key */
2450 	    if (history_down_line(cur_console))
2451 		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2452 	    goto next_code;
2453 
2454 	case 0x49:  /* page up key */
2455 	    for (i=0; i<cur_console->ysize; i++)
2456 	    if (history_up_line(cur_console)) {
2457 		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2458 		break;
2459 	    }
2460 	    goto next_code;
2461 
2462 	case 0x51:  /* page down key */
2463 	    for (i=0; i<cur_console->ysize; i++)
2464 	    if (history_down_line(cur_console)) {
2465 		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2466 		break;
2467 	    }
2468 	    goto next_code;
2469 	}
2470     }
2471 
2472     if (compose) {
2473 	switch (scancode) {
2474 	/* key pressed process it */
2475 	case 0x47: case 0x48: case 0x49:    /* keypad 7,8,9 */
2476 	    chr = (scancode - 0x40) + chr*10;
2477 	    goto next_code;
2478 	case 0x4B: case 0x4C: case 0x4D:    /* keypad 4,5,6 */
2479 	    chr = (scancode - 0x47) + chr*10;
2480 	    goto next_code;
2481 	case 0x4F: case 0x50: case 0x51:    /* keypad 1,2,3 */
2482 	    chr = (scancode - 0x4E) + chr*10;
2483 	    goto next_code;
2484 	case 0x52:              /* keypad 0 */
2485 	    chr *= 10;
2486 	    goto next_code;
2487 
2488 	/* key release, no interest here */
2489 	case 0xC7: case 0xC8: case 0xC9:    /* keypad 7,8,9 */
2490 	case 0xCB: case 0xCC: case 0xCD:    /* keypad 4,5,6 */
2491 	case 0xCF: case 0xD0: case 0xD1:    /* keypad 1,2,3 */
2492 	case 0xD2:              /* keypad 0 */
2493 	    goto next_code;
2494 
2495 	case 0x38:              /* left alt key */
2496 	    break;
2497 	default:
2498 	    if (chr) {
2499 		compose = chr = 0;
2500 		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2501 		goto next_code;
2502 	    }
2503 	    break;
2504 	}
2505     }
2506 
2507     state = (shfts ? 1 : 0 ) | (2 * (ctls ? 1 : 0)) | (4 * (alts ? 1 : 0));
2508     if ((!agrs && (cur_console->status & ALKED))
2509 	|| (agrs && !(cur_console->status & ALKED)))
2510 	keycode += ALTGR_OFFSET;
2511     key = &key_map.key[keycode];
2512     if ( ((key->flgs & FLAG_LOCK_C) && (cur_console->status & CLKED))
2513 	 || ((key->flgs & FLAG_LOCK_N) && (cur_console->status & NLKED)) )
2514 	state ^= 1;
2515 
2516     /* Check for make/break */
2517     action = key->map[state];
2518     if (scancode & 0x80) {      /* key released */
2519 	if (key->spcl & 0x80) {
2520 	    switch (action) {
2521 	    case LSH:
2522 		shfts &= ~1;
2523 		break;
2524 	    case RSH:
2525 		shfts &= ~2;
2526 		break;
2527 	    case LCTR:
2528 		ctls &= ~1;
2529 		break;
2530 	    case RCTR:
2531 		ctls &= ~2;
2532 		break;
2533 	    case LALT:
2534 		alts &= ~1;
2535 		break;
2536 	    case RALT:
2537 		alts &= ~2;
2538 		break;
2539 	    case NLK:
2540 		nlkcnt = 0;
2541 		break;
2542 	    case CLK:
2543 		clkcnt = 0;
2544 		break;
2545 	    case SLK:
2546 		slkcnt = 0;
2547 		break;
2548 	    case ASH:
2549 		agrs = 0;
2550 		break;
2551 	    case ALK:
2552 		alkcnt = 0;
2553 		break;
2554 	    case META:
2555 		metas = 0;
2556 		break;
2557 	    }
2558 	}
2559 	if (chr && !compose) {
2560 	    action = chr;
2561 	    chr = 0;
2562 	    return(action);
2563 	}
2564     } else {
2565 	/* key pressed */
2566 	if (key->spcl & (0x80>>state)) {
2567 	    switch (action) {
2568 	    /* LOCKING KEYS */
2569 	    case NLK:
2570 		if (!nlkcnt) {
2571 		    nlkcnt++;
2572 		    if (cur_console->status & NLKED)
2573 			cur_console->status &= ~NLKED;
2574 		    else
2575 			cur_console->status |= NLKED;
2576 		    update_leds(cur_console->status);
2577 		}
2578 		break;
2579 	    case CLK:
2580 		if (!clkcnt) {
2581 		    clkcnt++;
2582 		    if (cur_console->status & CLKED)
2583 			cur_console->status &= ~CLKED;
2584 		    else
2585 			cur_console->status |= CLKED;
2586 		    update_leds(cur_console->status);
2587 		}
2588 		break;
2589 	    case SLK:
2590 		if (!slkcnt) {
2591 		    slkcnt++;
2592 		    if (cur_console->status & SLKED) {
2593 			cur_console->status &= ~SLKED;
2594 			if (cur_console->status & BUFFER_SAVED){
2595 			    int i;
2596 			    u_short *ptr = cur_console->history_save;
2597 
2598 			    for (i=0; i<cur_console->ysize; i++) {
2599 				bcopyw(ptr,
2600 				       cur_console->scr_buf +
2601 				       (cur_console->xsize*i),
2602 				       cur_console->xsize * sizeof(u_short));
2603 				ptr += cur_console->xsize;
2604 				if (ptr + cur_console->xsize >
2605 				    cur_console->history +
2606 				    cur_console->history_size)
2607 				    ptr = cur_console->history;
2608 			    }
2609 			    cur_console->status &= ~BUFFER_SAVED;
2610 			    cur_console->history_head=cur_console->history_save;
2611 			    cur_console->status |= CURSOR_ENABLED;
2612 			    mark_all(cur_console);
2613 			}
2614 			scstart(VIRTUAL_TTY(get_scr_num()));
2615 		    }
2616 		    else
2617 			cur_console->status |= SLKED;
2618 		    update_leds(cur_console->status);
2619 		}
2620 		break;
2621 	    case ALK:
2622 		if (!alkcnt) {
2623 		    alkcnt++;
2624 		    if (cur_console->status & ALKED)
2625 			cur_console->status &= ~ALKED;
2626 		    else
2627 			cur_console->status |= ALKED;
2628 		    update_leds(cur_console->status);
2629 		}
2630 		break;
2631 
2632 	    /* NON-LOCKING KEYS */
2633 	    case NOP:
2634 		break;
2635 	    case RBT:
2636 		shutdown_nice();
2637 		break;
2638 	    case SUSP:
2639 #if NAPM > 0
2640 		apm_suspend();
2641 #endif
2642 		break;
2643 
2644 	    case DBG:
2645 #ifdef DDB          /* try to switch to console 0 */
2646 		if (cur_console->smode.mode == VT_AUTO &&
2647 		    console[0]->smode.mode == VT_AUTO)
2648 		    switch_scr(cur_console, 0);
2649 		Debugger("manual escape to debugger");
2650 		return(NOKEY);
2651 #else
2652 		printf("No debugger in kernel\n");
2653 #endif
2654 		break;
2655 	    case LSH:
2656 		shfts |= 1;
2657 		break;
2658 	    case RSH:
2659 		shfts |= 2;
2660 		break;
2661 	    case LCTR:
2662 		ctls |= 1;
2663 		break;
2664 	    case RCTR:
2665 		ctls |= 2;
2666 		break;
2667 	    case LALT:
2668 		alts |= 1;
2669 		break;
2670 	    case RALT:
2671 		alts |= 2;
2672 		break;
2673 	    case ASH:
2674 		agrs = 1;
2675 		break;
2676 	    case META:
2677 		metas = 1;
2678 		break;
2679 	    case NEXT:
2680 		switch_scr(cur_console, (get_scr_num() + 1) % MAXCONS);
2681 		break;
2682 	    case BTAB:
2683 		return(BKEY);
2684 	    default:
2685 		if (action >= F_SCR && action <= L_SCR) {
2686 		    switch_scr(cur_console, action - F_SCR);
2687 		    break;
2688 		}
2689 		if (action >= F_FN && action <= L_FN)
2690 		    action |= FKEY;
2691 		return(action);
2692 	    }
2693 	}
2694 	else {
2695 	    if (metas)
2696 		action |= MKEY;
2697 	    return(action);
2698 	}
2699     }
2700     goto next_code;
2701 }
2702 
2703 int
2704 scmmap(dev_t dev, int offset, int nprot)
2705 {
2706     if (offset > 0x20000 - PAGE_SIZE)
2707 	return -1;
2708     return i386_btop((VIDEOMEM + offset));
2709 }
2710 
2711 static void
2712 kbd_wait(void)
2713 {
2714     int i = 1000;
2715 
2716     while (i--) {
2717 	if ((inb(KB_STAT) & KB_READY) == 0)
2718 	    break;
2719 	DELAY (10);
2720     }
2721 }
2722 
2723 static void
2724 kbd_cmd(u_char command)
2725 {
2726     int retry = 5;
2727     do {
2728 	int i = 100000;
2729 
2730 	kbd_wait();
2731 #if ASYNCH
2732 	kbd_reply = 0;
2733 	outb(KB_DATA, command);
2734 	while (i--) {
2735 	    if (kbd_reply == KB_ACK)
2736 		return;
2737 	    if (kbd_reply == KB_RESEND)
2738 		break;
2739 	}
2740 #else
2741 	outb(KB_DATA, command);
2742 	while (i--) {
2743 	    if (inb(KB_STAT) & KB_BUF_FULL) {
2744 		int val;
2745 		DELAY(10);
2746 		val = inb(KB_DATA);
2747 		if (val == KB_ACK)
2748 		    return;
2749 		if (val == KB_RESEND)
2750 		    break;
2751 	    }
2752 	}
2753 #endif
2754     } while (retry--);
2755 }
2756 
2757 static void
2758 set_mode(scr_stat *scp)
2759 {
2760     char *modetable;
2761     char special_modetable[64];
2762     int font_size;
2763 
2764     if (scp != cur_console)
2765 	return;
2766 
2767     /* setup video hardware for the given mode */
2768     switch (scp->mode) {
2769     case M_VGA_M80x60:
2770 	bcopyw(video_mode_ptr+(64*M_VGA_M80x25),&special_modetable, 64);
2771 	goto special_80x60;
2772 
2773     case M_VGA_C80x60:
2774 	bcopyw(video_mode_ptr+(64*M_VGA_C80x25),&special_modetable, 64);
2775 special_80x60:
2776 	special_modetable[2]  = 0x08;
2777 	special_modetable[19] = 0x47;
2778 	goto special_480l;
2779 
2780     case M_VGA_M80x30:
2781 	bcopyw(video_mode_ptr+(64*M_VGA_M80x25),&special_modetable, 64);
2782 	goto special_80x30;
2783 
2784     case M_VGA_C80x30:
2785 	bcopyw(video_mode_ptr+(64*M_VGA_C80x25),&special_modetable, 64);
2786 special_80x30:
2787 	special_modetable[19] = 0x4f;
2788 special_480l:
2789 	special_modetable[9] |= 0xc0;
2790 	special_modetable[16] = 0x08;
2791 	special_modetable[17] = 0x3e;
2792 	special_modetable[26] = 0xea;
2793 	special_modetable[28] = 0xdf;
2794 	special_modetable[31] = 0xe7;
2795 	special_modetable[32] = 0x04;
2796 	modetable = special_modetable;
2797 	goto setup_mode;
2798 
2799     case M_ENH_B80x43:
2800 	bcopyw(video_mode_ptr+(64*M_ENH_B80x25),&special_modetable, 64);
2801 	goto special_80x43;
2802 
2803     case M_ENH_C80x43:
2804 	bcopyw(video_mode_ptr+(64*M_ENH_C80x25),&special_modetable, 64);
2805 special_80x43:
2806 	special_modetable[28] = 87;
2807 	goto special_80x50;
2808 
2809     case M_VGA_M80x50:
2810 	bcopyw(video_mode_ptr+(64*M_VGA_M80x25),&special_modetable, 64);
2811 	goto special_80x50;
2812 
2813     case M_VGA_C80x50:
2814 	bcopyw(video_mode_ptr+(64*M_VGA_C80x25),&special_modetable, 64);
2815 special_80x50:
2816 	special_modetable[2] = 8;
2817 	special_modetable[19] = 7;
2818 	modetable = special_modetable;
2819 	goto setup_mode;
2820 
2821     case M_VGA_C40x25: case M_VGA_C80x25:
2822     case M_VGA_M80x25:
2823     case M_B40x25:     case M_C40x25:
2824     case M_B80x25:     case M_C80x25:
2825     case M_ENH_B40x25: case M_ENH_C40x25:
2826     case M_ENH_B80x25: case M_ENH_C80x25:
2827 
2828 	modetable = video_mode_ptr + (scp->mode * 64);
2829 setup_mode:
2830 	set_vgaregs(modetable);
2831 	font_size = *(modetable + 2);
2832 
2833 	/* set font type (size) */
2834 	switch (font_size) {
2835 	case 0x10:
2836 	    outb(TSIDX, 0x03); outb(TSREG, 0x00);   /* font 0 */
2837 	    scp->font = FONT_16;
2838 	    break;
2839 	case 0x0E:
2840 	    outb(TSIDX, 0x03); outb(TSREG, 0x05);   /* font 1 */
2841 	    scp->font = FONT_14;
2842 	    break;
2843 	default:
2844 	case 0x08:
2845 	    outb(TSIDX, 0x03); outb(TSREG, 0x0A);   /* font 2 */
2846 	    scp->font = FONT_8;
2847 	    break;
2848 	}
2849 	if (configuration & CHAR_CURSOR)
2850 	    set_destructive_cursor(scp, TRUE);
2851 	break;
2852 
2853     case M_BG320:     case M_CG320:     case M_BG640:
2854     case M_CG320_D:   case M_CG640_E:
2855     case M_CG640x350: case M_ENH_CG640:
2856     case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
2857 
2858 	set_vgaregs(video_mode_ptr + (scp->mode * 64));
2859 	break;
2860 
2861     default:
2862 	/* call user defined function XXX */
2863 	break;
2864     }
2865 
2866     /* set border color for this (virtual) console */
2867     set_border(scp->border);
2868     return;
2869 }
2870 
2871 void
2872 set_border(int color)
2873 {
2874     inb(crtc_addr+6);               /* reset flip-flop */
2875     outb(ATC, 0x11); outb(ATC, color);
2876     inb(crtc_addr+6);               /* reset flip-flop */
2877     outb(ATC, 0x20);                /* enable Palette */
2878 }
2879 
2880 static void
2881 set_vgaregs(char *modetable)
2882 {
2883     int i, s = splhigh();
2884 
2885     outb(TSIDX, 0x00); outb(TSREG, 0x01);   	/* stop sequencer */
2886     outb(TSIDX, 0x07); outb(TSREG, 0x00);   	/* unlock registers */
2887     for (i=0; i<4; i++) {           		/* program sequencer */
2888 	outb(TSIDX, i+1);
2889 	outb(TSREG, modetable[i+5]);
2890     }
2891     outb(MISC, modetable[9]);       		/* set dot-clock */
2892     outb(TSIDX, 0x00); outb(TSREG, 0x03);   	/* start sequencer */
2893     outb(crtc_addr, 0x11);
2894     outb(crtc_addr+1, inb(crtc_addr+1) & 0x7F);
2895     for (i=0; i<25; i++) {          		/* program crtc */
2896 	outb(crtc_addr, i);
2897 	if (i == 14 || i == 15)     		/* no hardware cursor */
2898 	    outb(crtc_addr+1, 0xff);
2899 	else
2900 	    outb(crtc_addr+1, modetable[i+10]);
2901     }
2902     inb(crtc_addr+6);           		/* reset flip-flop */
2903     for (i=0; i<20; i++) {          		/* program attribute ctrl */
2904 	outb(ATC, i);
2905 	outb(ATC, modetable[i+35]);
2906     }
2907     for (i=0; i<9; i++) {           		/* program graph data ctrl */
2908 	outb(GDCIDX, i);
2909 	outb(GDCREG, modetable[i+55]);
2910     }
2911     inb(crtc_addr+6);           		/* reset flip-flop */
2912     outb(ATC ,0x20);            		/* enable palette */
2913     splx(s);
2914 }
2915 
2916 static void
2917 set_font_mode()
2918 {
2919     /* setup vga for loading fonts (graphics plane mode) */
2920     inb(crtc_addr+6);
2921     outb(ATC, 0x30); outb(ATC, 0x01);
2922 #if SLOW_VGA
2923     outb(TSIDX, 0x02); outb(TSREG, 0x04);
2924     outb(TSIDX, 0x04); outb(TSREG, 0x06);
2925     outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
2926     outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
2927     outb(GDCIDX, 0x06); outb(GDCREG, 0x05);
2928 #else
2929     outw(TSIDX, 0x0402);
2930     outw(TSIDX, 0x0604);
2931     outw(GDCIDX, 0x0204);
2932     outw(GDCIDX, 0x0005);
2933     outw(GDCIDX, 0x0506);               /* addr = a0000, 64kb */
2934 #endif
2935 }
2936 
2937 static void
2938 set_normal_mode()
2939 {
2940     int s = splhigh();
2941 
2942     /* setup vga for normal operation mode again */
2943     inb(crtc_addr+6);
2944     outb(ATC, 0x30); outb(ATC, 0x0C);
2945 #if SLOW_VGA
2946     outb(TSIDX, 0x02); outb(TSREG, 0x03);
2947     outb(TSIDX, 0x04); outb(TSREG, 0x02);
2948     outb(GDCIDX, 0x04); outb(GDCREG, 0x00);
2949     outb(GDCIDX, 0x05); outb(GDCREG, 0x10);
2950     if (crtc_addr == MONO_BASE) {
2951 	outb(GDCIDX, 0x06); outb(GDCREG, 0x0A); /* addr = b0000, 32kb */
2952     }
2953     else {
2954 	outb(GDCIDX, 0x06); outb(GDCREG, 0x0E); /* addr = b8000, 32kb */
2955     }
2956 #else
2957     outw(TSIDX, 0x0302);
2958     outw(TSIDX, 0x0204);
2959     outw(GDCIDX, 0x0004);
2960     outw(GDCIDX, 0x1005);
2961     if (crtc_addr == MONO_BASE)
2962 	outw(GDCIDX, 0x0A06);           /* addr = b0000, 32kb */
2963     else
2964 	outw(GDCIDX, 0x0E06);           /* addr = b8000, 32kb */
2965 #endif
2966     splx(s);
2967 }
2968 
2969 static void
2970 copy_font(int operation, int font_type, char* font_image)
2971 {
2972     int ch, line, segment, fontsize;
2973     u_char val;
2974 
2975     switch (font_type) {
2976     default:
2977     case FONT_8:
2978 	segment = 0x8000;
2979 	fontsize = 8;
2980 	break;
2981     case FONT_14:
2982 	segment = 0x4000;
2983 	fontsize = 14;
2984 	break;
2985     case FONT_16:
2986 	segment = 0x0000;
2987 	fontsize = 16;
2988 	break;
2989     }
2990     outb(TSIDX, 0x01); val = inb(TSREG);        /* disable screen */
2991     outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
2992     set_font_mode();
2993     for (ch=0; ch < 256; ch++)
2994 	for (line=0; line < fontsize; line++)
2995 	if (operation)
2996 	    *(char *)pa_to_va(VIDEOMEM+(segment)+(ch*32)+line) =
2997 		    font_image[(ch*fontsize)+line];
2998 	else
2999 	    font_image[(ch*fontsize)+line] =
3000 	    *(char *)pa_to_va(VIDEOMEM+(segment)+(ch*32)+line);
3001     set_normal_mode();
3002     outb(TSIDX, 0x01); outb(TSREG, val & 0xDF); /* enable screen */
3003 }
3004 
3005 static void
3006 set_destructive_cursor(scr_stat *scp, int force)
3007 {
3008     u_char cursor[32];
3009     caddr_t address;
3010     int i, font_size;
3011     char *font_buffer;
3012     static u_char old_saveunder = DEAD_CHAR;
3013 
3014     if (!force && (scp->cursor_saveunder & 0xFF) == old_saveunder)
3015 	return;
3016     old_saveunder = force ? DEAD_CHAR : scp->cursor_saveunder & 0xFF;
3017     switch (scp->font) {
3018     default:
3019     case FONT_8:
3020 	font_size = 8;
3021 	font_buffer = font_8;
3022 	address = (caddr_t)VIDEOMEM + 0x8000;
3023 	break;
3024     case FONT_14:
3025 	font_size = 14;
3026 	font_buffer = font_14;
3027 	address = (caddr_t)VIDEOMEM + 0x4000;
3028 	break;
3029     case FONT_16:
3030 	font_size = 16;
3031 	font_buffer = font_16;
3032 	address = (caddr_t)VIDEOMEM;
3033 	break;
3034     }
3035     bcopyw(font_buffer + ((scp->cursor_saveunder & 0xff) * font_size),
3036 	   cursor, font_size);
3037     for (i=0; i<32; i++)
3038 	if ((i >= scp->cursor_start && i <= scp->cursor_end) ||
3039 	    (scp->cursor_start >= font_size && i == font_size - 1))
3040 	    cursor[i] |= 0xff;
3041     while (!(inb(crtc_addr+6) & 0x08)) /* wait for vertical retrace */ ;
3042     set_font_mode();
3043     bcopy(cursor, (char *)pa_to_va(address) + DEAD_CHAR * 32, 32);
3044     set_normal_mode();
3045 }
3046 
3047 static void
3048 draw_mouse_image(scr_stat *scp)
3049 {
3050     caddr_t address;
3051     int i, font_size;
3052     char *font_buffer;
3053     u_short buffer[32];
3054     u_short xoffset, yoffset;
3055     u_short *crt_pos = Crtat + (scp->mouse_pos - scp->scr_buf);
3056 
3057     xoffset = scp->mouse_xpos % 8;
3058     switch (scp->font) {
3059     default:
3060     case FONT_8:
3061 	font_size = 8;
3062 	font_buffer = font_8;
3063 	yoffset = scp->mouse_ypos % 8;
3064 	address = (caddr_t)VIDEOMEM + 0x8000;
3065 	break;
3066     case FONT_14:
3067 	font_size = 14;
3068 	font_buffer = font_14;
3069 	yoffset = scp->mouse_ypos % 14;
3070 	address = (caddr_t)VIDEOMEM + 0x4000;
3071 	break;
3072     case FONT_16:
3073 	font_size = 16;
3074 	font_buffer = font_16;
3075 	yoffset = scp->mouse_ypos % 16;
3076 	address = (caddr_t)VIDEOMEM;
3077 	break;
3078     }
3079 
3080     bcopyw(font_buffer + ((*(scp->mouse_pos) & 0xff) * font_size),
3081 	   &scp->mouse_cursor[0], font_size);
3082     bcopyw(font_buffer + ((*(scp->mouse_pos+1) & 0xff) * font_size),
3083 	   &scp->mouse_cursor[32], font_size);
3084     bcopyw(font_buffer + ((*(scp->mouse_pos+scp->xsize) & 0xff) * font_size),
3085 	   &scp->mouse_cursor[64], font_size);
3086     bcopyw(font_buffer + ((*(scp->mouse_pos+scp->xsize+1) & 0xff) * font_size),
3087 	   &scp->mouse_cursor[96], font_size);
3088 
3089     for (i=0; i<font_size; i++) {
3090 	buffer[i] = scp->mouse_cursor[i]<<8 | scp->mouse_cursor[i+32];
3091 	buffer[i+font_size]=scp->mouse_cursor[i+64]<<8|scp->mouse_cursor[i+96];
3092     }
3093     for (i=0; i<16; i++) {
3094 	buffer[i+yoffset] =
3095 	    ( buffer[i+yoffset] & ~(mouse_and_mask[i] >> xoffset))
3096 	    | (mouse_or_mask[i] >> xoffset);
3097     }
3098     for (i=0; i<font_size; i++) {
3099 	scp->mouse_cursor[i] = (buffer[i] & 0xff00) >> 8;
3100 	scp->mouse_cursor[i+32] = buffer[i] & 0xff;
3101 	scp->mouse_cursor[i+64] = (buffer[i+font_size] & 0xff00) >> 8;
3102 	scp->mouse_cursor[i+96] = buffer[i+font_size] & 0xff;
3103     }
3104     if (scp->status & UPDATE_MOUSE) {
3105 	u_short *ptr = scp->scr_buf + (scp->mouse_oldpos - Crtat);
3106 
3107 	if (crt_pos != scp->mouse_oldpos) {
3108 	    *(scp->mouse_oldpos) = scp->mouse_saveunder[0];
3109 	    *(scp->mouse_oldpos+1) = scp->mouse_saveunder[1];
3110 	    *(scp->mouse_oldpos+scp->xsize) = scp->mouse_saveunder[2];
3111 	    *(scp->mouse_oldpos+scp->xsize+1) = scp->mouse_saveunder[3];
3112 	}
3113 	scp->mouse_saveunder[0] = *(scp->mouse_pos);
3114 	scp->mouse_saveunder[1] = *(scp->mouse_pos+1);
3115 	scp->mouse_saveunder[2] = *(scp->mouse_pos+scp->xsize);
3116 	scp->mouse_saveunder[3] = *(scp->mouse_pos+scp->xsize+1);
3117 	if ((scp->cursor_pos == (ptr)) ||
3118 	    (scp->cursor_pos == (ptr+1)) ||
3119 	    (scp->cursor_pos == (ptr+scp->xsize)) ||
3120 	    (scp->cursor_pos == (ptr+scp->xsize+1)) ||
3121 	    (scp->cursor_pos == (scp->mouse_pos)) ||
3122 	    (scp->cursor_pos == (scp->mouse_pos+1)) ||
3123 	    (scp->cursor_pos == (scp->mouse_pos+scp->xsize)) ||
3124 	    (scp->cursor_pos == (scp->mouse_pos+scp->xsize+1)))
3125 	    scp->status &= ~CURSOR_SHOWN;
3126     }
3127     scp->mouse_oldpos = crt_pos;
3128     while (!(inb(crtc_addr+6) & 0x08)) /* wait for vertical retrace */ ;
3129     *(crt_pos) = (*(scp->mouse_pos)&0xff00)|0xd0;
3130     *(crt_pos+1) = (*(scp->mouse_pos+1)&0xff00)|0xd1;
3131     *(crt_pos+scp->xsize) = (*(scp->mouse_pos+scp->xsize)&0xff00)|0xd2;
3132     *(crt_pos+scp->xsize+1) = (*(scp->mouse_pos+scp->xsize+1)&0xff00)|0xd3;
3133     set_font_mode();
3134     bcopy(scp->mouse_cursor, (char *)pa_to_va(address) + 0xd0 * 32, 128);
3135     set_normal_mode();
3136 }
3137 
3138 static void
3139 save_palette(void)
3140 {
3141     int i;
3142 
3143     outb(PALRADR, 0x00);
3144     for (i=0x00; i<0x300; i++)
3145 	palette[i] = inb(PALDATA);
3146     inb(crtc_addr+6);           /* reset flip/flop */
3147 }
3148 
3149 void
3150 load_palette(void)
3151 {
3152     int i;
3153 
3154     outb(PIXMASK, 0xFF);            /* no pixelmask */
3155     outb(PALWADR, 0x00);
3156     for (i=0x00; i<0x300; i++)
3157 	 outb(PALDATA, palette[i]);
3158     inb(crtc_addr+6);           /* reset flip/flop */
3159     outb(ATC, 0x20);            /* enable palette */
3160 }
3161 
3162 static void
3163 do_bell(scr_stat *scp, int pitch, int duration)
3164 {
3165     if (configuration & VISUAL_BELL) {
3166 	if (blink_in_progress)
3167 	    return;
3168 	blink_in_progress = 4;
3169 	if (scp != cur_console)
3170 	    blink_in_progress += 2;
3171 	blink_screen(cur_console);
3172 	timeout((timeout_func_t)blink_screen, cur_console, hz/10);
3173     } else {
3174 	if (scp != cur_console)
3175 	    pitch *= 2;
3176 	sysbeep(pitch, duration);
3177     }
3178 }
3179 
3180 static void
3181 blink_screen(scr_stat *scp)
3182 {
3183     if (blink_in_progress > 1) {
3184 	if (blink_in_progress & 1)
3185 	    fillw(kernel_default.std_color | scr_map[0x20],
3186 		  Crtat, scp->xsize * scp->ysize);
3187 	else
3188 	    fillw(kernel_default.rev_color | scr_map[0x20],
3189 		  Crtat, scp->xsize * scp->ysize);
3190 	blink_in_progress--;
3191 	timeout((timeout_func_t)blink_screen, scp, hz/10);
3192     }
3193     else {
3194 	blink_in_progress = FALSE;
3195     	mark_all(scp);
3196 	if (delayed_next_scr)
3197 	    switch_scr(scp, delayed_next_scr - 1);
3198     }
3199 }
3200 
3201 #endif /* NSC */
3202