vidconsole.c revision 199767f8919635c4928607450d9e0abb932109ce
/*-
* Copyright (c) 1998 Michael Smith (msmith@freebsd.org)
* Copyright (c) 1997 Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Id: probe_keyboard.c,v 1.13 1997/06/09 05:10:55 bde Exp
*/
#include <stand.h>
#include <bootstrap.h>
#include <btxv86.h>
#include "libi386.h"
#if KEYBOARD_PROBE
static int probe_keyboard(void);
#endif
static int vidc_started;
#ifdef TERM_EMU
#define MAXARGS 8
#define KEYBUFSZ 10
#define DEFAULT_FGCOLOR 7
#define DEFAULT_BGCOLOR 0
void end_term(void);
void bail_out(int c);
void vidc_term_emu(int c);
void get_pos(int *x, int *y);
void CD(void);
void CM(void);
void HO(void);
static int esc;
#endif
"text",
0,
};
static void
{
/* look for a keyboard */
#if KEYBOARD_PROBE
if (probe_keyboard())
#endif
{
}
/* XXX for now, always assume we can do BIOS screen output */
}
static int
{
int i;
if (vidc_started && arg == 0)
return (0);
vidc_started = 1;
#ifdef TERM_EMU
/* Init terminal emulator */
end_term();
#endif
(void)vidc_getchar(cp);
return (0); /* XXX reinit? */
}
void
vidc_biosputchar(int c)
{
v86int();
}
static void
vidc_rawputchar(int c)
{
int i;
if (c == '\t')
/* lame tab expansion */
for (i = 0; i < 8; i++)
vidc_rawputchar(' ');
else {
#ifndef TERM_EMU
vidc_biosputchar(c);
#else
/* Emulate AH=0eh (teletype output) */
switch(c) {
case '\a':
vidc_biosputchar(c);
return;
case '\r':
curx = 0;
break;
case '\n':
cury++;
curx = 0;
if (cury > 24) {
cury--;
}
break;
case '\b':
if (curx > 0)
curx--;
break;
default:
if (curx > 79) {
curx = 0;
cury++;
}
if (cury > 24) {
curx = 0;
cury--;
}
}
curx++;
if (curx > 79) {
curx = 0;
if (cury == 25)
else
cury++;
}
}
}
#endif
}
}
#ifdef TERM_EMU
/* Get cursor position on the screen. Result is in edx. Sets
* curx and cury appropriately.
*/
void
get_pos(int *x, int *y)
{
v86int();
}
/* Move cursor to x rows and y cols (0-based). */
void
{
v86int();
*_x = x;
*_y = y;
/* If there is ctrl char at this position, cursor would be invisible.
* Make it a space instead.
*/
v86int();
}
v86int();
}
/* Scroll up the whole window by a number of rows. If rows==0,
* clear the window. fg and bg are attributes for the new lines
* inserted in the window.
*/
void
{
if (rows == 0)
rows = 25;
v86int();
}
/* Write character and attribute at cursor position. */
void
{
v86int();
}
/**************************************************************/
/*
* Screen manipulation functions. They use accumulated data in
* args[] and argc variables.
*
*/
/* Clear line from current position to end of line */
void
{
if (direction == 0) { /* from cursor to end */
}
v86int();
end_term();
return;
}
/* Clear display from current position to end of screen */
void
CD(void)
{
if (curx > 0) {
v86int();
if (++cury > 24) {
end_term();
return;
}
}
v86int();
end_term();
}
/* Absolute cursor move to args[0] rows and args[1] columns
* (the coordinates are 1-based).
*/
void
CM(void)
{
if (args[0] > 0)
args[0]--;
if (args[1] > 0)
args[1]--;
end_term();
}
/* Home cursor (left top corner) */
void
HO(void)
{
argc = 1;
CM();
}
/* Clear internal state of the terminal emulation code */
void
end_term(void)
{
esc = 0;
argc = -1;
}
/* Gracefully exit ESC-sequence processing in case of misunderstanding */
void
bail_out(int c)
{
int i;
if (esc) {
vidc_rawputchar('\033');
if (esc != '\033')
for (i = 0; i <= argc; ++i) {
while (*ch)
vidc_rawputchar(*ch++);
}
}
vidc_rawputchar(c);
end_term();
}
static void
get_arg(int c)
{
if (argc < 0)
argc = 0;
}
/* Emulate basic capabilities of sun-color terminal */
void
vidc_term_emu(int c)
{
static int ansi_col[] = {
0, 4, 2, 6, 1, 5, 3, 7,
};
int t;
int i;
switch (esc) {
case 0:
switch (c) {
case '\033':
esc = c;
break;
default:
vidc_rawputchar(c);
break;
}
break;
case '\033':
switch (c) {
case '[':
esc = c;
args[0] = 0;
argc = -1;
break;
default:
bail_out(c);
break;
}
break;
case '[':
switch (c) {
case ';':
if (argc < 0) /* XXX */
argc = 0;
bail_out(c);
else
break;
case 'A': /* UP = \E[%dA */
if (argc == 0) {
int x, y;
get_pos(&x, &y);
CM();
} else
bail_out(c);
break;
case 'B': /* DO = \E[%dB */
if (argc == 0) {
int x, y;
get_pos(&x, &y);
CM();
} else
bail_out(c);
break;
case 'C': /* RI = \E[%dC */
if (argc == 0) {
int x, y;
get_pos(&x, &y);
args[0] = y + 1;
CM();
} else
bail_out(c);
break;
case 'H': /* ho = \E[H */
if (argc < 0)
HO();
else if (argc == 1)
CM();
else
bail_out(c);
break;
case 'J': /* cd = \E[J */
if (argc < 0)
CD();
else
bail_out(c);
break;
case 'K':
if (argc < 0)
CL(0);
else if (argc == 0)
switch (args[0]) {
case 0:
case 1:
case 2:
break;
default:
bail_out(c);
}
else
bail_out(c);
break;
case 'm':
if (argc < 0) {
}
for (i = 0; i <= argc; ++i) {
switch (args[i]) {
case 0: /* back to normal */
break;
case 1: /* bold */
fg_c |= 0x8;
break;
case 4: /* underline */
case 5: /* blink */
bg_c |= 0x8;
break;
case 7: /* reverse */
t = fg_c;
bg_c = t;
break;
case 30: case 31: case 32: case 33:
case 34: case 35: case 36: case 37:
break;
case 39: /* normal */
break;
case 40: case 41: case 42: case 43:
case 44: case 45: case 46: case 47:
break;
case 49: /* normal */
break;
}
}
end_term();
break;
default:
if (isdigit(c))
get_arg(c);
else
bail_out(c);
break;
}
break;
default:
bail_out(c);
break;
}
}
#endif
static void
{
#ifdef TERM_EMU
vidc_term_emu(c);
#else
vidc_rawputchar(c);
#endif
}
static int
{
int i, c;
for (i = 0; i < KEYBUFSZ; i++) {
if (keybuf[i] != 0) {
c = keybuf[i];
keybuf[i] = 0;
return (c);
}
}
if (vidc_ischar(cp)) {
v86int();
}
/* extended keys */
case 0x4800: /* up */
keybuf[0] = '[';
return (0x1b); /* esc */
case 0x4b00: /* left */
keybuf[0] = '[';
return (0x1b); /* esc */
case 0x4d00: /* right */
keybuf[0] = '[';
return (0x1b); /* esc */
case 0x5000: /* down */
keybuf[0] = '[';
return (0x1b); /* esc */
default:
return (-1);
}
} else {
return (-1);
}
}
static int
{
int i;
for (i = 0; i < KEYBUFSZ; i++) {
if (keybuf[i] != 0) {
return (1);
}
}
v86int();
}
#if KEYBOARD_PROBE
#define PROBE_MAXRETRY 5
#define PROBE_MAXWAIT 400
#define IO_DUMMY 0x84
/* selected defines from kbdio.h */
#define KBD_DATA_PORT 0 /* data port, read/write
* also used as keyboard command
* and mouse command port
*/
#define KBDC_ECHO 0x00ee
#define KBDS_ANY_BUFFER_FULL 0x0001
#define KBDS_INPUT_BUFFER_FULL 0x0002
#define KBD_ECHO 0x00ee
/* 7 microsec delay necessary for some keyboard controllers */
static void
delay7(void)
{
/*
* I know this is broken, but no timer is available yet at this stage...
* See also comments in `delay1ms()'.
*/
}
/*
* This routine uses an inb to an unused port, the time to execute that
* inb is approximately 1.25uS. This value is pretty constant across
* all CPU's and all buses, with the exception of some PCI implentations
* that do not forward this I/O address to the ISA bus as they know it
* is not a valid ISA bus address, those machines execute this inb in
* 60 nS :-(.
*
*/
static void
delay1ms(void)
{
int i = 800;
while (--i >= 0)
(void)inb(0x84);
}
/*
* console can be used for input.
*
* Perform a simple test on the keyboard; issue the ECHO command and see
* if the right answer is returned. We don't do anything as drastic as
* full keyboard reset; it will be too troublesome and take too much time.
*/
static int
probe_keyboard(void)
{
int retry = PROBE_MAXRETRY;
int wait;
int i;
while (--retry >= 0) {
/* flush any noise */
delay7();
delay1ms();
}
/* wait until the controller can accept a command */
& (KBDS_INPUT_BUFFER_FULL | KBDS_ANY_BUFFER_FULL)) == 0)
break;
if (i & KBDS_ANY_BUFFER_FULL) {
delay7();
}
delay1ms();
}
if (wait <= 0)
continue;
/* send the ECHO command */
/* wait for a response */
break;
delay1ms();
}
if (wait <= 0)
continue;
delay7();
#ifdef PROBE_KBD_BEBUG
printf("probe_keyboard: got 0x%x.\n", i);
#endif
if (i == KBD_ECHO) {
/* got the right answer */
return (1);
}
}
return (0);
}
#endif /* KEYBOARD_PROBE */