2005-04-08 17:01:17 +02:00
|
|
|
#include "stddef.h"
|
2011-03-09 21:09:26 +01:00
|
|
|
#include <ipxe/console.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/process.h>
|
|
|
|
#include <ipxe/nap.h>
|
2005-04-08 17:01:17 +02:00
|
|
|
|
2005-05-20 12:24:11 +02:00
|
|
|
/** @file */
|
|
|
|
|
2009-05-01 16:41:06 +02:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2012-03-26 18:25:08 +02:00
|
|
|
/** Current console usage */
|
|
|
|
int console_usage = CONSOLE_USAGE_STDOUT;
|
|
|
|
|
2005-05-20 12:24:11 +02:00
|
|
|
/**
|
|
|
|
* Write a single character to each console device.
|
|
|
|
*
|
|
|
|
* @v character Character to be written
|
2005-05-24 02:11:25 +02:00
|
|
|
* @ret None -
|
|
|
|
* @err None -
|
2005-05-20 12:24:11 +02:00
|
|
|
*
|
|
|
|
* The character is written out to all enabled console devices, using
|
|
|
|
* each device's console_driver::putchar() method.
|
|
|
|
*
|
2005-04-08 17:01:17 +02:00
|
|
|
*/
|
|
|
|
void putchar ( int character ) {
|
|
|
|
struct console_driver *console;
|
|
|
|
|
|
|
|
/* Automatic LF -> CR,LF translation */
|
|
|
|
if ( character == '\n' )
|
|
|
|
putchar ( '\r' );
|
|
|
|
|
2009-03-12 20:41:40 +01:00
|
|
|
for_each_table_entry ( console, CONSOLES ) {
|
2012-03-26 18:25:08 +02:00
|
|
|
if ( ( ! console->disabled ) &&
|
|
|
|
( console_usage & console->usage ) &&
|
|
|
|
console->putchar )
|
2005-04-08 17:01:17 +02:00
|
|
|
console->putchar ( character );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-20 12:24:11 +02:00
|
|
|
/**
|
|
|
|
* Check to see if any input is available on any console.
|
|
|
|
*
|
2005-05-24 02:11:25 +02:00
|
|
|
* @v None -
|
2005-05-20 12:24:11 +02:00
|
|
|
* @ret console Console device that has input available, if any.
|
|
|
|
* @ret NULL No console device has input available.
|
2005-05-24 02:11:25 +02:00
|
|
|
* @err None -
|
2005-05-20 12:24:11 +02:00
|
|
|
*
|
|
|
|
* All enabled console devices are checked once for available input
|
|
|
|
* using each device's console_driver::iskey() method. The first
|
|
|
|
* console device that has available input will be returned, if any.
|
|
|
|
*
|
2005-04-08 17:01:17 +02:00
|
|
|
*/
|
|
|
|
static struct console_driver * has_input ( void ) {
|
|
|
|
struct console_driver *console;
|
|
|
|
|
2009-03-12 20:41:40 +01:00
|
|
|
for_each_table_entry ( console, CONSOLES ) {
|
2005-04-08 17:01:17 +02:00
|
|
|
if ( ( ! console->disabled ) && console->iskey ) {
|
|
|
|
if ( console->iskey () )
|
|
|
|
return console;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-05-20 12:24:11 +02:00
|
|
|
/**
|
|
|
|
* Read a single character from any console.
|
|
|
|
*
|
2005-05-24 02:11:25 +02:00
|
|
|
* @v None -
|
2005-05-20 12:24:11 +02:00
|
|
|
* @ret character Character read from a console.
|
2005-05-24 02:11:25 +02:00
|
|
|
* @err None -
|
2005-05-20 12:24:11 +02:00
|
|
|
*
|
|
|
|
* A character will be read from the first enabled console device that
|
|
|
|
* has input available using that console's console_driver::getchar()
|
|
|
|
* method. If no console has input available to be read, this method
|
|
|
|
* will block. To perform a non-blocking read, use something like
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
*
|
|
|
|
* int key = iskey() ? getchar() : -1;
|
|
|
|
*
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* The character read will not be echoed back to any console.
|
|
|
|
*
|
2005-04-08 17:01:17 +02:00
|
|
|
*/
|
|
|
|
int getchar ( void ) {
|
|
|
|
struct console_driver *console;
|
2008-06-11 13:06:10 +02:00
|
|
|
int character;
|
|
|
|
|
|
|
|
while ( 1 ) {
|
|
|
|
console = has_input();
|
|
|
|
if ( console && console->getchar ) {
|
|
|
|
character = console->getchar ();
|
|
|
|
break;
|
|
|
|
}
|
2005-04-08 17:01:17 +02:00
|
|
|
|
|
|
|
/* Doze for a while (until the next interrupt). This works
|
|
|
|
* fine, because the keyboard is interrupt-driven, and the
|
|
|
|
* timer interrupt (approx. every 50msec) takes care of the
|
|
|
|
* serial port, which is read by polling. This reduces the
|
|
|
|
* power dissipation of a modern CPU considerably, and also
|
|
|
|
* makes Etherboot waiting for user interaction waste a lot
|
|
|
|
* less CPU time in a VMware session.
|
|
|
|
*/
|
|
|
|
cpu_nap();
|
2006-12-27 23:59:40 +01:00
|
|
|
|
|
|
|
/* Keep processing background tasks while we wait for
|
|
|
|
* input.
|
|
|
|
*/
|
|
|
|
step();
|
2005-04-08 17:01:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CR -> LF translation */
|
|
|
|
if ( character == '\r' )
|
|
|
|
character = '\n';
|
|
|
|
|
|
|
|
return character;
|
|
|
|
}
|
|
|
|
|
2005-05-20 12:24:11 +02:00
|
|
|
/** Check for available input on any console.
|
|
|
|
*
|
2005-05-24 02:11:25 +02:00
|
|
|
* @v None -
|
2005-05-20 12:24:11 +02:00
|
|
|
* @ret True Input is available on a console
|
|
|
|
* @ret False Input is not available on any console
|
2005-05-24 02:11:25 +02:00
|
|
|
* @err None -
|
2005-05-20 12:24:11 +02:00
|
|
|
*
|
|
|
|
* All enabled console devices are checked once for available input
|
|
|
|
* using each device's console_driver::iskey() method. If any console
|
|
|
|
* device has input available, this call will return True. If this
|
|
|
|
* call returns True, you can then safely call getchar() without
|
|
|
|
* blocking.
|
|
|
|
*
|
2005-04-08 17:01:17 +02:00
|
|
|
*/
|
|
|
|
int iskey ( void ) {
|
|
|
|
return has_input() ? 1 : 0;
|
|
|
|
}
|