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