2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
23 #include <ipxe/console.h>
24 #include <ipxe/process.h>
25 #include <ipxe/keys.h>
26 #include <ipxe/timer.h>
31 * Special key interpretation
35 #define GETKEY_TIMEOUT ( TICKS_PER_SEC / 4 )
38 * Read character from console if available within timeout period
40 * @v timeout Timeout period, in ticks (0=indefinite)
41 * @ret character Character read from console
43 static int getchar_timeout ( unsigned long timeout ) {
44 unsigned long start = currticks();
46 while ( ( timeout == 0 ) || ( ( currticks() - start ) < timeout ) ) {
59 * @v timeout Timeout period, in ticks (0=indefinite)
60 * @ret key Key pressed
62 * The returned key will be an ASCII value or a KEY_XXX special
63 * constant. This function differs from getchar() in that getchar()
64 * will return "special" keys (e.g. cursor keys) as a series of
65 * characters forming an ANSI escape sequence.
67 int getkey ( unsigned long timeout ) {
71 character = getchar_timeout ( timeout );
72 if ( character != ESC )
75 while ( ( character = getchar_timeout ( GETKEY_TIMEOUT ) ) >= 0 ) {
76 if ( character == '[' )
78 if ( isdigit ( character ) ) {
79 n = ( ( n * 10 ) + ( character - '0' ) );
82 if ( character >= 0x40 )
83 return KEY_ANSI ( n, character );