[cloud] Allow aws-import script to run on Python 3.6
[ipxe.git] / 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_OR_UBDL );
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 * @ret character Character written
24 *
25 * The character is written out to all enabled console devices, using
26 * each device's console_driver::putchar() method.
27 */
28 int putchar ( int character ) {
29 struct console_driver *console;
30
31 /* Automatic LF -> CR,LF translation */
32 if ( character == '\n' )
33 putchar ( '\r' );
34
35 for_each_table_entry ( console, CONSOLES ) {
36 if ( ( ! ( console->disabled & CONSOLE_DISABLED_OUTPUT ) ) &&
37 ( console_usage & console->usage ) &&
38 console->putchar )
39 console->putchar ( character );
40 }
41
42 return character;
43 }
44
45 /**
46 * Check to see if any input is available on any console
47 *
48 * @ret console Console device that has input available, or NULL
49 *
50 * All enabled console devices are checked once for available input
51 * using each device's console_driver::iskey() method. The first
52 * console device that has available input will be returned, if any.
53 */
54 static struct console_driver * has_input ( void ) {
55 struct console_driver *console;
56
57 for_each_table_entry ( console, CONSOLES ) {
58 if ( ( ! ( console->disabled & CONSOLE_DISABLED_INPUT ) ) &&
59 console->iskey ) {
60 if ( console->iskey () )
61 return console;
62 }
63 }
64 return NULL;
65 }
66
67 /**
68 * Read a single character from any console
69 *
70 * @ret character Character read from a console.
71 *
72 * A character will be read from the first enabled console device that
73 * has input available using that console's console_driver::getchar()
74 * method. If no console has input available to be read, this method
75 * will block. To perform a non-blocking read, use something like
76 *
77 * @code
78 *
79 * int key = iskey() ? getchar() : -1;
80 *
81 * @endcode
82 *
83 * The character read will not be echoed back to any console.
84 */
85 int getchar ( void ) {
86 struct console_driver *console;
87 int character;
88
89 while ( 1 ) {
90 console = has_input();
91 if ( console && console->getchar ) {
92 character = console->getchar ();
93 break;
94 }
95
96 /* Doze for a while (until the next interrupt). This works
97 * fine, because the keyboard is interrupt-driven, and the
98 * timer interrupt (approx. every 50msec) takes care of the
99 * serial port, which is read by polling. This reduces the
100 * power dissipation of a modern CPU considerably, and also
101 * makes Etherboot waiting for user interaction waste a lot
102 * less CPU time in a VMware session.
103 */
104 cpu_nap();
105
106 /* Keep processing background tasks while we wait for
107 * input.
108 */
109 step();
110 }
111
112 /* CR -> LF translation */
113 if ( character == '\r' )
114 character = '\n';
115
116 return character;
117 }
118
119 /**
120 * Check for available input on any console
121 *
122 * @ret is_available Input is available on a console
123 *
124 * All enabled console devices are checked once for available input
125 * using each device's console_driver::iskey() method. If any console
126 * device has input available, this call will return true. If this
127 * call returns true, you can then safely call getchar() without
128 * blocking.
129 */
130 int iskey ( void ) {
131 return has_input() ? 1 : 0;
132 }
133
134 /**
135 * Configure console
136 *
137 * @v config Console configuration
138 * @ret rc Return status code
139 *
140 * The configuration is passed to all configurable consoles, including
141 * those which are currently disabled. Consoles may choose to enable
142 * or disable themselves depending upon the configuration.
143 *
144 * If configuration fails, then all consoles will be reset.
145 */
146 int console_configure ( struct console_configuration *config ) {
147 struct console_driver *console;
148 int rc;
149
150 /* Reset console width and height */
151 console_set_size ( CONSOLE_DEFAULT_WIDTH, CONSOLE_DEFAULT_HEIGHT );
152
153 /* Try to configure each console */
154 for_each_table_entry ( console, CONSOLES ) {
155 if ( ( console->configure ) &&
156 ( ( rc = console->configure ( config ) ) != 0 ) )
157 goto err;
158 }
159
160 return 0;
161
162 err:
163 /* Reset all consoles, avoiding a potential infinite loop */
164 if ( config )
165 console_reset();
166 return rc;
167 }