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