Select only one console when several are available, add console_get_cursor_position(), console_get_size() and console_status_request()

This commit is contained in:
Laurent Vivier 2007-09-16 15:26:17 +00:00
parent a3aaf95a3c
commit abb1e988bf
2 changed files with 155 additions and 65 deletions

View File

@ -24,14 +24,13 @@ enum {
}; };
static int selected_console; static int selected_console;
int wait_char;
void void
console_init(emile_l2_header_t* info) console_init(void)
{ {
selected_console = CONSOLE_ALL; selected_console = CONSOLE_ALL;
wait_char = 0;
vga_init(info);
serial_init(info);
} }
int console_putchar(int c) int console_putchar(int c)
@ -85,69 +84,100 @@ int console_keypressed(int timeout)
int int
console_select(int timeout) console_select(int timeout)
{ {
if (vga_is_available() + int modem_available = 0;
serial_is_available(SERIAL_MODEM_PORT) + int printer_available = 0;
serial_is_available(SERIAL_PRINTER_PORT) < 1) int vga_available = 0;
return 0; int available = 0;
int ret;
printf("Press a key on this console to select it\n"); if (vga_is_available())
if (console_keypressed(timeout * 60))
return 1;
if (selected_console == CONSOLE_ALL)
{ {
if (vga_is_available()) vga_available = 1;
selected_console = CONSOLE_VGA; available++;
else if (serial_is_available(SERIAL_MODEM_PORT))
selected_console = CONSOLE_MODEM;
else if (serial_is_available(SERIAL_PRINTER_PORT))
selected_console = CONSOLE_PRINTER;
} }
return 0; if (serial_is_available(SERIAL_MODEM_PORT))
{
selected_console = CONSOLE_MODEM;
ret = console_status_request();
if (ret == 0)
{
modem_available = 1;
available++;
}
}
if (serial_is_available(SERIAL_PRINTER_PORT))
{
selected_console = CONSOLE_PRINTER;
ret = console_status_request();
if (ret == 0)
{
printer_available = 1;
available++;
}
}
if (available > 1)
{
selected_console = CONSOLE_ALL;
console_clear();
console_set_position(1,1);
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;
} }
int console_getchar() int console_getchar()
{ {
int c; int c;
long time = Ticks + wait_char * 60;
switch(selected_console) c = 0;
{ do {
case CONSOLE_ALL: switch(selected_console)
if (vga_is_available()) {
{ case CONSOLE_ALL:
c = keyboard_getchar(); if (vga_is_available())
{
c = keyboard_getchar();
if (c)
break;
}
c = serial_getchar(SERIAL_MODEM_PORT);
if (c) if (c)
return c; break;
} c = serial_getchar(SERIAL_PRINTER_PORT);
c = serial_getchar(SERIAL_MODEM_PORT);
if (c)
return c;
c = serial_getchar(SERIAL_PRINTER_PORT);
if (c)
return c;
break;
case CONSOLE_VGA:
if (vga_is_available())
{
c = keyboard_getchar();
if (c) if (c)
return c; break;
} break;
case CONSOLE_MODEM: case CONSOLE_VGA:
c = serial_getchar(SERIAL_MODEM_PORT); if (vga_is_available())
if (c) c = keyboard_getchar();
return c; break;
break; case CONSOLE_MODEM:
case CONSOLE_PRINTER: c = serial_getchar(SERIAL_MODEM_PORT);
c = serial_getchar(SERIAL_PRINTER_PORT); break;
if (c) case CONSOLE_PRINTER:
return c; c = serial_getchar(SERIAL_PRINTER_PORT);
break; break;
} }
} while ((c == 0) && (Ticks < time));
return 0; return c;
} }
void console_clear(void) void console_clear(void)
@ -190,31 +220,52 @@ void console_set_cursor_position(int l, int c)
printf("\033[%d;%dH", l, c); printf("\033[%d;%dH", l, c);
} }
void console_get_cursor_position(int *l, int *c) 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)
{ {
int car; int car;
char buf[16]; char buf[16];
int i; int i;
int ret;
int saved_wait = wait_char;
ret = -1;
*l = *c = 0; *l = *c = 0;
while(console_getchar() != 0); console_flush();
printf("\033[6n"); printf("\033[6n");
while ( (car = console_getchar()) && (car != '\033')); wait_char = 1;
if (console_getchar() != '\033')
goto out;
if (console_getchar() != '[') if (console_getchar() != '[')
return; goto out;
ret = 0;
i = 0; i = 0;
while ( (car = console_getchar()) != ';') while ( (car = console_getchar()) && (car != ';'))
buf[i++] = car; buf[i++] = car;
buf[i] = 0; buf[i] = 0;
*l = strtol(buf, NULL, 10); *l = strtol(buf, NULL, 10);
i = 0; i = 0;
while ( (car = console_getchar()) != 'R') while ( (car = console_getchar()) && (car != 'R'))
buf[i++] = car; buf[i++] = car;
buf[i] = 0; buf[i] = 0;
*c = strtol(buf, NULL, 10); *c = strtol(buf, NULL, 10);
out:
wait_char = saved_wait;
return ret;
} }
void console_select_charset(char c) void console_select_charset(char c)
@ -222,11 +273,48 @@ void console_select_charset(char c)
printf("\033(%c", c); printf("\033(%c", c);
} }
void console_get_size(int *l, int *c) int console_get_size(int *l, int *c)
{ {
int ret = 0;
console_cursor_save(); console_cursor_save();
console_set_cursor_position(255, 255); console_set_cursor_position(255, 255);
console_get_cursor_position(l, c); ret = console_get_cursor_position(l, c);
console_cursor_restore(); console_cursor_restore();
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;
} }
#endif #endif

View File

@ -12,9 +12,10 @@
#include "misc.h" #include "misc.h"
#include "head.h" #include "head.h"
extern void console_init(emile_l2_header_t* info); extern void console_init(void);
extern inline int console_putchar(int c); extern inline int console_putchar(int c);
extern void console_putstring(const char *s); extern void console_putstring(const char *s);
int wait_char;
#ifdef USE_CLI #ifdef USE_CLI
extern int console_keypressed(int timeout); extern int console_keypressed(int timeout);
extern int console_getchar(void); extern int console_getchar(void);
@ -26,9 +27,10 @@ extern void console_cursor_restore(void);
extern void console_video_inverse(void); extern void console_video_inverse(void);
extern void console_video_normal(void); extern void console_video_normal(void);
extern void console_set_cursor_position(int l, int c); extern void console_set_cursor_position(int l, int c);
extern void console_get_cursor_position(int* l, int* c); extern int console_get_cursor_position(int* l, int* c);
extern void console_get_size(int *l, int *c); extern int console_get_size(int *l, int *c);
extern int console_select(int timeout); extern int console_select(int timeout);
extern int console_status_request();
#endif #endif
#endif #endif