2 #include <ipxe/console.h>
3 #include <ipxe/process.h>
8 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
10 /** Current console usage */
11 int console_usage = CONSOLE_USAGE_STDOUT;
14 unsigned int console_width = CONSOLE_DEFAULT_WIDTH;
17 unsigned int console_height = CONSOLE_DEFAULT_HEIGHT;
20 * Write a single character to each console device
22 * @v character Character to be written
24 * The character is written out to all enabled console devices, using
25 * each device's console_driver::putchar() method.
27 void putchar ( int character ) {
28 struct console_driver *console;
30 /* Automatic LF -> CR,LF translation */
31 if ( character == '\n' )
34 for_each_table_entry ( console, CONSOLES ) {
35 if ( ( ! ( console->disabled & CONSOLE_DISABLED_OUTPUT ) ) &&
36 ( console_usage & console->usage ) &&
38 console->putchar ( character );
43 * Check to see if any input is available on any console
45 * @ret console Console device that has input available, or NULL
47 * All enabled console devices are checked once for available input
48 * using each device's console_driver::iskey() method. The first
49 * console device that has available input will be returned, if any.
51 static struct console_driver * has_input ( void ) {
52 struct console_driver *console;
54 for_each_table_entry ( console, CONSOLES ) {
55 if ( ( ! ( console->disabled & CONSOLE_DISABLED_INPUT ) ) &&
57 if ( console->iskey () )
65 * Read a single character from any console
67 * @ret character Character read from a console.
69 * A character will be read from the first enabled console device that
70 * has input available using that console's console_driver::getchar()
71 * method. If no console has input available to be read, this method
72 * will block. To perform a non-blocking read, use something like
76 * int key = iskey() ? getchar() : -1;
80 * The character read will not be echoed back to any console.
82 int getchar ( void ) {
83 struct console_driver *console;
87 console = has_input();
88 if ( console && console->getchar ) {
89 character = console->getchar ();
93 /* Doze for a while (until the next interrupt). This works
94 * fine, because the keyboard is interrupt-driven, and the
95 * timer interrupt (approx. every 50msec) takes care of the
96 * serial port, which is read by polling. This reduces the
97 * power dissipation of a modern CPU considerably, and also
98 * makes Etherboot waiting for user interaction waste a lot
99 * less CPU time in a VMware session.
103 /* Keep processing background tasks while we wait for
109 /* CR -> LF translation */
110 if ( character == '\r' )
117 * Check for available input on any console
119 * @ret is_available Input is available on a console
121 * All enabled console devices are checked once for available input
122 * using each device's console_driver::iskey() method. If any console
123 * device has input available, this call will return true. If this
124 * call returns true, you can then safely call getchar() without
128 return has_input() ? 1 : 0;
134 * @v config Console configuration
135 * @ret rc Return status code
137 * The configuration is passed to all configurable consoles, including
138 * those which are currently disabled. Consoles may choose to enable
139 * or disable themselves depending upon the configuration.
141 * If configuration fails, then all consoles will be reset.
143 int console_configure ( struct console_configuration *config ) {
144 struct console_driver *console;
147 /* Reset console width and height */
148 console_set_size ( CONSOLE_DEFAULT_WIDTH, CONSOLE_DEFAULT_HEIGHT );
150 /* Try to configure each console */
151 for_each_table_entry ( console, CONSOLES ) {
152 if ( ( console->configure ) &&
153 ( ( rc = console->configure ( config ) ) != 0 ) )
160 /* Reset all consoles, avoiding a potential infinite loop */