2004-02-15 20:46:45 +00:00
|
|
|
/*
|
|
|
|
*
|
2007-05-30 22:05:10 +00:00
|
|
|
* (c) 2004-2007 Laurent Vivier <Laurent@lvivier.info>
|
2004-02-15 20:46:45 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-05-23 17:09:48 +00:00
|
|
|
#include <stdio.h>
|
2007-06-06 13:21:52 +00:00
|
|
|
#include <stdlib.h>
|
2007-09-03 17:51:12 +00:00
|
|
|
#include <sys/types.h>
|
2005-05-23 17:09:48 +00:00
|
|
|
|
2005-11-08 02:06:40 +00:00
|
|
|
#include <macos/lowmem.h>
|
2004-02-15 20:46:45 +00:00
|
|
|
|
2004-06-22 21:21:32 +00:00
|
|
|
#include "vga.h"
|
2007-05-30 22:05:10 +00:00
|
|
|
#include "console.h"
|
2004-06-25 11:15:16 +00:00
|
|
|
#include "serial.h"
|
2005-08-27 15:48:13 +00:00
|
|
|
#include "keyboard.h"
|
2005-11-28 23:21:00 +00:00
|
|
|
#include "config.h"
|
2004-06-25 11:15:16 +00:00
|
|
|
|
2007-09-11 23:03:10 +00:00
|
|
|
enum {
|
|
|
|
CONSOLE_ALL = 0,
|
|
|
|
CONSOLE_VGA,
|
|
|
|
CONSOLE_MODEM,
|
|
|
|
CONSOLE_PRINTER,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int selected_console;
|
2007-09-16 15:26:17 +00:00
|
|
|
int wait_char;
|
2004-02-15 20:46:45 +00:00
|
|
|
|
|
|
|
void
|
2007-09-16 15:26:17 +00:00
|
|
|
console_init(void)
|
2004-02-15 20:46:45 +00:00
|
|
|
{
|
2007-09-11 23:03:10 +00:00
|
|
|
selected_console = CONSOLE_ALL;
|
2007-09-16 15:26:17 +00:00
|
|
|
wait_char = 0;
|
2004-02-15 20:46:45 +00:00
|
|
|
}
|
|
|
|
|
2005-11-08 02:06:40 +00:00
|
|
|
int console_putchar(int c)
|
2004-02-15 20:46:45 +00:00
|
|
|
{
|
2007-09-11 23:03:10 +00:00
|
|
|
switch(selected_console)
|
|
|
|
{
|
|
|
|
case CONSOLE_ALL:
|
|
|
|
vga_put(c);
|
|
|
|
serial_put(SERIAL_MODEM_PORT, c);
|
|
|
|
serial_put(SERIAL_PRINTER_PORT, c);
|
|
|
|
break;
|
|
|
|
case CONSOLE_VGA:
|
|
|
|
vga_put(c);
|
|
|
|
break;
|
|
|
|
case CONSOLE_MODEM:
|
|
|
|
serial_put(SERIAL_MODEM_PORT, c);
|
|
|
|
break;
|
|
|
|
case CONSOLE_PRINTER:
|
|
|
|
serial_put(SERIAL_PRINTER_PORT, c);
|
|
|
|
break;
|
|
|
|
}
|
2005-05-23 17:09:48 +00:00
|
|
|
|
|
|
|
return c;
|
2004-02-15 20:46:45 +00:00
|
|
|
}
|
|
|
|
|
2005-05-25 06:26:00 +00:00
|
|
|
void console_putstring(const char *s)
|
2004-02-15 20:46:45 +00:00
|
|
|
{
|
|
|
|
while(*s)
|
2005-05-25 06:26:00 +00:00
|
|
|
console_putchar(*(s++));
|
2004-02-15 20:46:45 +00:00
|
|
|
}
|
2005-08-27 15:48:13 +00:00
|
|
|
|
|
|
|
#ifdef USE_CLI
|
|
|
|
int console_keypressed(int timeout)
|
|
|
|
{
|
|
|
|
long time = Ticks + timeout;
|
|
|
|
|
2007-08-18 13:32:30 +00:00
|
|
|
while (!timeout || (Ticks < time))
|
2005-08-27 15:48:13 +00:00
|
|
|
{
|
2007-09-11 23:03:10 +00:00
|
|
|
if (serial_keypressed(SERIAL_MODEM_PORT))
|
|
|
|
return CONSOLE_MODEM;
|
|
|
|
|
|
|
|
if (serial_keypressed(SERIAL_PRINTER_PORT))
|
|
|
|
return CONSOLE_PRINTER;
|
2005-08-27 15:48:13 +00:00
|
|
|
|
2007-09-11 23:03:10 +00:00
|
|
|
if (vga_is_available() && keyboard_keypressed())
|
|
|
|
return CONSOLE_VGA;
|
2005-08-27 15:48:13 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-11 23:03:10 +00:00
|
|
|
int
|
|
|
|
console_select(int timeout)
|
|
|
|
{
|
2007-09-16 15:26:17 +00:00
|
|
|
int modem_available = 0;
|
|
|
|
int printer_available = 0;
|
|
|
|
int vga_available = 0;
|
|
|
|
int available = 0;
|
|
|
|
int ret;
|
2007-09-11 23:03:10 +00:00
|
|
|
|
2007-09-16 15:26:17 +00:00
|
|
|
if (vga_is_available())
|
|
|
|
{
|
|
|
|
vga_available = 1;
|
|
|
|
available++;
|
|
|
|
}
|
2007-09-11 23:03:10 +00:00
|
|
|
|
2007-09-16 15:26:17 +00:00
|
|
|
if (serial_is_available(SERIAL_MODEM_PORT))
|
|
|
|
{
|
|
|
|
selected_console = CONSOLE_MODEM;
|
|
|
|
ret = console_status_request();
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
modem_available = 1;
|
|
|
|
available++;
|
|
|
|
}
|
|
|
|
}
|
2007-09-11 23:03:10 +00:00
|
|
|
|
2007-09-16 15:26:17 +00:00
|
|
|
if (serial_is_available(SERIAL_PRINTER_PORT))
|
2007-09-11 23:03:10 +00:00
|
|
|
{
|
2007-09-16 15:26:17 +00:00
|
|
|
selected_console = CONSOLE_PRINTER;
|
|
|
|
ret = console_status_request();
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
printer_available = 1;
|
|
|
|
available++;
|
|
|
|
}
|
2007-09-11 23:03:10 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 15:26:17 +00:00
|
|
|
if (available > 1)
|
|
|
|
{
|
|
|
|
selected_console = CONSOLE_ALL;
|
|
|
|
console_clear();
|
2007-09-16 15:33:18 +00:00
|
|
|
console_set_cursor_position(1,1);
|
2007-09-16 15:26:17 +00:00
|
|
|
printf("Please, press a key to select this console\n");
|
|
|
|
|
|
|
|
selected_console = console_keypressed(timeout * 60);
|
|
|
|
if (selected_console)
|
|
|
|
return 0;
|
|
|
|
timeout = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vga_available)
|
|
|
|
selected_console = CONSOLE_VGA;
|
|
|
|
else if (modem_available)
|
|
|
|
selected_console = CONSOLE_MODEM;
|
|
|
|
else if (printer_available)
|
|
|
|
selected_console = CONSOLE_PRINTER;
|
|
|
|
|
|
|
|
return timeout;
|
2007-09-11 23:03:10 +00:00
|
|
|
}
|
|
|
|
|
2005-08-27 15:48:13 +00:00
|
|
|
int console_getchar()
|
|
|
|
{
|
|
|
|
int c;
|
2007-09-16 15:26:17 +00:00
|
|
|
long time = Ticks + wait_char * 60;
|
|
|
|
|
|
|
|
c = 0;
|
|
|
|
do {
|
|
|
|
switch(selected_console)
|
|
|
|
{
|
|
|
|
case CONSOLE_ALL:
|
|
|
|
if (vga_is_available())
|
|
|
|
{
|
|
|
|
c = keyboard_getchar();
|
|
|
|
if (c)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = serial_getchar(SERIAL_MODEM_PORT);
|
2007-09-11 23:03:10 +00:00
|
|
|
if (c)
|
2007-09-16 15:26:17 +00:00
|
|
|
break;
|
|
|
|
c = serial_getchar(SERIAL_PRINTER_PORT);
|
2007-09-11 23:03:10 +00:00
|
|
|
if (c)
|
2007-09-16 15:26:17 +00:00
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case CONSOLE_VGA:
|
|
|
|
if (vga_is_available())
|
|
|
|
c = keyboard_getchar();
|
|
|
|
break;
|
|
|
|
case CONSOLE_MODEM:
|
|
|
|
c = serial_getchar(SERIAL_MODEM_PORT);
|
|
|
|
break;
|
|
|
|
case CONSOLE_PRINTER:
|
|
|
|
c = serial_getchar(SERIAL_PRINTER_PORT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while ((c == 0) && (Ticks < time));
|
2007-09-11 23:03:10 +00:00
|
|
|
|
2007-09-16 15:26:17 +00:00
|
|
|
return c;
|
2005-08-27 15:48:13 +00:00
|
|
|
}
|
2007-05-30 22:05:10 +00:00
|
|
|
|
|
|
|
void console_clear(void)
|
|
|
|
{
|
|
|
|
printf("\033[2J");
|
|
|
|
}
|
|
|
|
|
2005-08-28 21:41:31 +00:00
|
|
|
void console_cursor_on(void)
|
|
|
|
{
|
2007-05-30 22:05:10 +00:00
|
|
|
printf("\033[?25h");
|
2005-08-28 21:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void console_cursor_off(void)
|
|
|
|
{
|
2007-05-30 22:05:10 +00:00
|
|
|
printf("\033[?25l");
|
2005-08-28 21:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void console_cursor_restore(void)
|
|
|
|
{
|
2007-05-30 22:05:10 +00:00
|
|
|
printf("\0338");
|
2005-08-28 21:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void console_cursor_save(void)
|
|
|
|
{
|
2007-05-30 22:05:10 +00:00
|
|
|
printf("\0337");
|
|
|
|
}
|
|
|
|
|
|
|
|
void console_video_inverse(void)
|
|
|
|
{
|
|
|
|
printf("\033[7m");
|
|
|
|
}
|
|
|
|
|
|
|
|
void console_video_normal(void)
|
|
|
|
{
|
|
|
|
printf("\033[27m");
|
2005-08-28 21:41:31 +00:00
|
|
|
}
|
2007-06-01 20:48:44 +00:00
|
|
|
|
2007-06-06 13:21:52 +00:00
|
|
|
void console_set_cursor_position(int l, int c)
|
2007-06-01 20:48:44 +00:00
|
|
|
{
|
2007-06-06 13:21:52 +00:00
|
|
|
printf("\033[%d;%dH", l, c);
|
2007-06-01 20:48:44 +00:00
|
|
|
}
|
2007-08-12 20:43:28 +00:00
|
|
|
|
2007-09-16 15:26:17 +00:00
|
|
|
void console_flush()
|
|
|
|
{
|
|
|
|
int saved_wait = wait_char;
|
|
|
|
wait_char = 0;
|
|
|
|
|
|
|
|
while(console_getchar() != 0);
|
|
|
|
wait_char = saved_wait;
|
|
|
|
}
|
|
|
|
|
|
|
|
int console_get_cursor_position(int *l, int *c)
|
2007-09-11 23:03:10 +00:00
|
|
|
{
|
|
|
|
int car;
|
|
|
|
char buf[16];
|
|
|
|
int i;
|
2007-09-16 15:26:17 +00:00
|
|
|
int ret;
|
|
|
|
int saved_wait = wait_char;
|
2007-09-11 23:03:10 +00:00
|
|
|
|
2007-09-16 15:26:17 +00:00
|
|
|
ret = -1;
|
2007-09-11 23:03:10 +00:00
|
|
|
*l = *c = 0;
|
|
|
|
|
2007-09-16 15:26:17 +00:00
|
|
|
console_flush();
|
2007-09-11 23:03:10 +00:00
|
|
|
printf("\033[6n");
|
|
|
|
|
2007-09-16 15:26:17 +00:00
|
|
|
wait_char = 1;
|
|
|
|
|
|
|
|
if (console_getchar() != '\033')
|
|
|
|
goto out;
|
2007-09-11 23:03:10 +00:00
|
|
|
|
|
|
|
if (console_getchar() != '[')
|
2007-09-16 15:26:17 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = 0;
|
2007-09-11 23:03:10 +00:00
|
|
|
i = 0;
|
2007-09-16 15:26:17 +00:00
|
|
|
while ( (car = console_getchar()) && (car != ';'))
|
2007-09-11 23:03:10 +00:00
|
|
|
buf[i++] = car;
|
|
|
|
buf[i] = 0;
|
|
|
|
*l = strtol(buf, NULL, 10);
|
|
|
|
i = 0;
|
2007-09-16 15:26:17 +00:00
|
|
|
while ( (car = console_getchar()) && (car != 'R'))
|
2007-09-11 23:03:10 +00:00
|
|
|
buf[i++] = car;
|
|
|
|
buf[i] = 0;
|
|
|
|
*c = strtol(buf, NULL, 10);
|
2007-09-16 15:26:17 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
wait_char = saved_wait;
|
|
|
|
return ret;
|
2007-09-11 23:03:10 +00:00
|
|
|
}
|
|
|
|
|
2007-08-12 20:43:28 +00:00
|
|
|
void console_select_charset(char c)
|
|
|
|
{
|
|
|
|
printf("\033(%c", c);
|
|
|
|
}
|
2007-09-11 23:03:10 +00:00
|
|
|
|
2007-09-16 15:26:17 +00:00
|
|
|
int console_get_size(int *l, int *c)
|
2007-09-11 23:03:10 +00:00
|
|
|
{
|
2007-09-16 15:26:17 +00:00
|
|
|
int ret = 0;
|
2007-09-11 23:03:10 +00:00
|
|
|
console_cursor_save();
|
|
|
|
console_set_cursor_position(255, 255);
|
2007-09-16 15:26:17 +00:00
|
|
|
ret = console_get_cursor_position(l, c);
|
2007-09-11 23:03:10 +00:00
|
|
|
console_cursor_restore();
|
2007-09-16 15:26:17 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int console_status_request()
|
|
|
|
{
|
|
|
|
int car;
|
|
|
|
int status;
|
|
|
|
int i;
|
|
|
|
char buf[16];
|
|
|
|
int saved_wait = wait_char;
|
|
|
|
|
|
|
|
console_flush();
|
|
|
|
printf("\033[5n");
|
|
|
|
|
|
|
|
status = -1;
|
|
|
|
wait_char = 1;
|
|
|
|
car = console_getchar();
|
|
|
|
if (car != '\033')
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
car = console_getchar();
|
|
|
|
if (car != '[')
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while ( (car = console_getchar()) && (car != 'n'))
|
|
|
|
buf[i++] = car;
|
|
|
|
buf[i] = 0;
|
|
|
|
if (car != 'n')
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
status = strtol(buf, NULL, 10);
|
|
|
|
|
|
|
|
out:
|
|
|
|
wait_char = saved_wait;
|
|
|
|
return status;
|
2007-09-11 23:03:10 +00:00
|
|
|
}
|
2005-08-27 15:48:13 +00:00
|
|
|
#endif
|