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