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