getkey.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <ctype.h>
00022 #include <console.h>
00023 #include <gpxe/process.h>
00024 #include <gpxe/keys.h>
00025 #include <gpxe/timer.h>
00026 
00027 /** @file
00028  *
00029  * Special key interpretation
00030  *
00031  */
00032 
00033 #define GETKEY_TIMEOUT ( TICKS_PER_SEC / 4 )
00034 
00035 /**
00036  * Read character from console if available within timeout period
00037  *
00038  * @v timeout           Timeout period, in ticks
00039  * @ret character       Character read from console
00040  */
00041 static int getchar_timeout ( unsigned long timeout ) {
00042         unsigned long expiry = ( currticks() + timeout );
00043 
00044         while ( currticks() < expiry ) {
00045                 step();
00046                 if ( iskey() )
00047                         return getchar();
00048         }
00049 
00050         return -1;
00051 }
00052 
00053 /**
00054  * Get single keypress
00055  *
00056  * @ret key             Key pressed
00057  *
00058  * The returned key will be an ASCII value or a KEY_XXX special
00059  * constant.  This function differs from getchar() in that getchar()
00060  * will return "special" keys (e.g. cursor keys) as a series of
00061  * characters forming an ANSI escape sequence.
00062  */
00063 int getkey ( void ) {
00064         int character;
00065         unsigned int n = 0;
00066 
00067         character = getchar();
00068         if ( character != ESC )
00069                 return character;
00070 
00071         while ( ( character = getchar_timeout ( GETKEY_TIMEOUT ) ) >= 0 ) {
00072                 if ( character == '[' )
00073                         continue;
00074                 if ( isdigit ( character ) ) {
00075                         n = ( ( n * 10 ) + ( character - '0' ) );
00076                         continue;
00077                 }
00078                 if ( character >= 0x40 )
00079                         return KEY_ANSI ( n, character );
00080         }
00081 
00082         return ESC;
00083 }

Generated on Tue Apr 6 20:00:51 2010 for gPXE by  doxygen 1.5.7.1