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