Added netdev_nullify to natsemi_remove()
[gpxe.git] / src / core / getkey.c
blob71ec6cc1be4bdd236ae85dd7589d3512aa18f8f2
1 /*
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <console.h>
20 #include <latch.h>
21 #include <gpxe/process.h>
22 #include <gpxe/keys.h>
24 /** @file
26 * Special key interpretation
30 #define GETKEY_TIMEOUT ( TICKS_PER_SEC / 4 )
32 /**
33 * Read character from console if available within timeout period
35 * @v timeout Timeout period, in ticks
36 * @ret character Character read from console
38 static int getchar_timeout ( unsigned long timeout ) {
39 unsigned long expiry = ( currticks() + timeout );
41 while ( currticks() < expiry ) {
42 step();
43 if ( iskey() )
44 return getchar();
47 return -1;
50 /**
51 * Get single keypress
53 * @ret key Key pressed
55 * The returned key will be an ASCII value or a KEY_XXX special
56 * constant. This function differs from getchar() in that getchar()
57 * will return "special" keys (e.g. cursor keys) as a series of
58 * characters forming an ANSI escape sequence.
60 int getkey ( void ) {
61 int character;
62 int key;
64 character = getchar();
65 if ( character != ESC )
66 return character;
68 key = 0;
69 while ( ( character = getchar_timeout ( GETKEY_TIMEOUT ) ) >= 0 ) {
70 if ( character == '[' )
71 continue;
72 if ( ! key )
73 key = KEY_ANSI ( character );
74 if ( character >= 0x40 )
75 break;
78 return ( key ? key : ESC );