Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / getkey.c
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <ctype.h>
23 #include <ipxe/console.h>
24 #include <ipxe/process.h>
25 #include <ipxe/keys.h>
26 #include <ipxe/timer.h>
27 #include <ipxe/nap.h>
28
29 /** @file
30  *
31  * Special key interpretation
32  *
33  */
34
35 #define GETKEY_TIMEOUT ( TICKS_PER_SEC / 4 )
36
37 /**
38  * Read character from console if available within timeout period
39  *
40  * @v timeout           Timeout period, in ticks (0=indefinite)
41  * @ret character       Character read from console
42  */
43 static int getchar_timeout ( unsigned long timeout ) {
44         unsigned long start = currticks();
45
46         while ( ( timeout == 0 ) || ( ( currticks() - start ) < timeout ) ) {
47                 step();
48                 if ( iskey() )
49                         return getchar();
50                 cpu_nap();
51         }
52
53         return -1;
54 }
55
56 /**
57  * Get single keypress
58  *
59  * @v timeout           Timeout period, in ticks (0=indefinite)
60  * @ret key             Key pressed
61  *
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.
66  */
67 int getkey ( unsigned long timeout ) {
68         int character;
69         unsigned int n = 0;
70
71         character = getchar_timeout ( timeout );
72         if ( character != ESC )
73                 return character;
74
75         while ( ( character = getchar_timeout ( GETKEY_TIMEOUT ) ) >= 0 ) {
76                 if ( character == '[' )
77                         continue;
78                 if ( isdigit ( character ) ) {
79                         n = ( ( n * 10 ) + ( character - '0' ) );
80                         continue;
81                 }
82                 if ( character >= 0x40 )
83                         return KEY_ANSI ( n, character );
84         }
85
86         return ESC;
87 }