Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / console.c
1 #include "stddef.h"
2 #include <ipxe/console.h>
3 #include <ipxe/process.h>
4 #include <ipxe/nap.h>
5
6 /** @file */
7
8 FILE_LICENCE ( GPL2_OR_LATER );
9
10 /** Current console usage */
11 int console_usage = CONSOLE_USAGE_STDOUT;
12
13 /** Console width */
14 unsigned int console_width = CONSOLE_DEFAULT_WIDTH;
15
16 /** Console height */
17 unsigned int console_height = CONSOLE_DEFAULT_HEIGHT;
18
19 /**
20  * Write a single character to each console device
21  *
22  * @v character         Character to be written
23  *
24  * The character is written out to all enabled console devices, using
25  * each device's console_driver::putchar() method.
26  */
27 void putchar ( int character ) {
28         struct console_driver *console;
29
30         /* Automatic LF -> CR,LF translation */
31         if ( character == '\n' )
32                 putchar ( '\r' );
33
34         for_each_table_entry ( console, CONSOLES ) {
35                 if ( ( ! ( console->disabled & CONSOLE_DISABLED_OUTPUT ) ) &&
36                      ( console_usage & console->usage ) &&
37                      console->putchar )
38                         console->putchar ( character );
39         }
40 }
41
42 /**
43  * Check to see if any input is available on any console
44  *
45  * @ret console         Console device that has input available, or NULL
46  *
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.
50  */
51 static struct console_driver * has_input ( void ) {
52         struct console_driver *console;
53
54         for_each_table_entry ( console, CONSOLES ) {
55                 if ( ( ! ( console->disabled & CONSOLE_DISABLED_INPUT ) ) &&
56                      console->iskey ) {
57                         if ( console->iskey () )
58                                 return console;
59                 }
60         }
61         return NULL;
62 }
63
64 /**
65  * Read a single character from any console
66  *
67  * @ret character       Character read from a console.
68  *
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
73  *
74  * @code
75  *
76  *   int key = iskey() ? getchar() : -1;
77  *
78  * @endcode
79  *
80  * The character read will not be echoed back to any console.
81  */
82 int getchar ( void ) {
83         struct console_driver *console;
84         int character;
85
86         while ( 1 ) {
87                 console = has_input();
88                 if ( console && console->getchar ) {
89                         character = console->getchar ();
90                         break;
91                 }
92
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.
100                  */
101                 cpu_nap();
102
103                 /* Keep processing background tasks while we wait for
104                  * input.
105                  */
106                 step();
107         }
108
109         /* CR -> LF translation */
110         if ( character == '\r' )
111                 character = '\n';
112
113         return character;
114 }
115
116 /**
117  * Check for available input on any console
118  *
119  * @ret is_available    Input is available on a console
120  *
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
125  * blocking.
126  */
127 int iskey ( void ) {
128         return has_input() ? 1 : 0;
129 }
130
131 /**
132  * Configure console
133  *
134  * @v config            Console configuration
135  * @ret rc              Return status code
136  *
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.
140  *
141  * If configuration fails, then all consoles will be reset.
142  */
143 int console_configure ( struct console_configuration *config ) {
144         struct console_driver *console;
145         int rc;
146
147         /* Reset console width and height */
148         console_set_size ( CONSOLE_DEFAULT_WIDTH, CONSOLE_DEFAULT_HEIGHT );
149
150         /* Try to configure each console */
151         for_each_table_entry ( console, CONSOLES ) {
152                 if ( ( console->configure ) &&
153                      ( ( rc = console->configure ( config ) ) != 0 ) )
154                                 goto err;
155         }
156
157         return 0;
158
159  err:
160         /* Reset all consoles, avoiding a potential infinite loop */
161         if ( config )
162                 console_reset();
163         return rc;
164 }