diff --git a/second/console.c b/second/console.c index 8e3e456..45b1d1d 100644 --- a/second/console.c +++ b/second/console.c @@ -24,14 +24,13 @@ enum { }; static int selected_console; +int wait_char; void -console_init(emile_l2_header_t* info) +console_init(void) { selected_console = CONSOLE_ALL; - - vga_init(info); - serial_init(info); + wait_char = 0; } int console_putchar(int c) @@ -85,69 +84,100 @@ int console_keypressed(int timeout) int console_select(int timeout) { - if (vga_is_available() + - serial_is_available(SERIAL_MODEM_PORT) + - serial_is_available(SERIAL_PRINTER_PORT) < 1) - return 0; + int modem_available = 0; + int printer_available = 0; + int vga_available = 0; + int available = 0; + int ret; - printf("Press a key on this console to select it\n"); - - if (console_keypressed(timeout * 60)) - return 1; - - if (selected_console == CONSOLE_ALL) + if (vga_is_available()) { - if (vga_is_available()) - selected_console = CONSOLE_VGA; - else if (serial_is_available(SERIAL_MODEM_PORT)) - selected_console = CONSOLE_MODEM; - else if (serial_is_available(SERIAL_PRINTER_PORT)) - selected_console = CONSOLE_PRINTER; + vga_available = 1; + available++; } - 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 c; + long time = Ticks + wait_char * 60; - switch(selected_console) - { - case CONSOLE_ALL: - if (vga_is_available()) - { - c = keyboard_getchar(); + 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); if (c) - return c; - } - 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(); + break; + c = serial_getchar(SERIAL_PRINTER_PORT); if (c) - return c; - } - case CONSOLE_MODEM: - c = serial_getchar(SERIAL_MODEM_PORT); - if (c) - return c; - break; - case CONSOLE_PRINTER: - c = serial_getchar(SERIAL_PRINTER_PORT); - if (c) - return c; - break; - } + 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)); - return 0; + return c; } void console_clear(void) @@ -190,31 +220,52 @@ void console_set_cursor_position(int l, int 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; char buf[16]; int i; + int ret; + int saved_wait = wait_char; + ret = -1; *l = *c = 0; - while(console_getchar() != 0); + console_flush(); printf("\033[6n"); - while ( (car = console_getchar()) && (car != '\033')); + wait_char = 1; + + if (console_getchar() != '\033') + goto out; if (console_getchar() != '[') - return; + goto out; + + ret = 0; i = 0; - while ( (car = console_getchar()) != ';') + while ( (car = console_getchar()) && (car != ';')) buf[i++] = car; buf[i] = 0; *l = strtol(buf, NULL, 10); i = 0; - while ( (car = console_getchar()) != 'R') + while ( (car = console_getchar()) && (car != 'R')) buf[i++] = car; buf[i] = 0; *c = strtol(buf, NULL, 10); + +out: + wait_char = saved_wait; + return ret; } void console_select_charset(char c) @@ -222,11 +273,48 @@ void console_select_charset(char 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_set_cursor_position(255, 255); - console_get_cursor_position(l, c); + ret = console_get_cursor_position(l, c); 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 diff --git a/second/console.h b/second/console.h index 37120df..3b47e86 100644 --- a/second/console.h +++ b/second/console.h @@ -12,9 +12,10 @@ #include "misc.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 void console_putstring(const char *s); +int wait_char; #ifdef USE_CLI extern int console_keypressed(int timeout); 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_normal(void); extern void console_set_cursor_position(int l, int c); -extern void console_get_cursor_position(int* l, int* c); -extern void console_get_size(int *l, int *c); +extern int console_get_cursor_position(int* l, int* c); +extern int console_get_size(int *l, int *c); extern int console_select(int timeout); +extern int console_status_request(); #endif #endif