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