iPXE
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., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <ctype.h>
00027 #include <ipxe/console.h>
00028 #include <ipxe/process.h>
00029 #include <ipxe/keys.h>
00030 #include <ipxe/timer.h>
00031 #include <ipxe/nap.h>
00032 
00033 /** @file
00034  *
00035  * Special key interpretation
00036  *
00037  */
00038 
00039 #define GETKEY_TIMEOUT ( TICKS_PER_SEC / 4 )
00040 
00041 /**
00042  * Read character from console if available within timeout period
00043  *
00044  * @v timeout           Timeout period, in ticks (0=indefinite)
00045  * @ret character       Character read from console
00046  */
00047 static int getchar_timeout ( unsigned long timeout ) {
00048         unsigned long start = currticks();
00049 
00050         while ( ( timeout == 0 ) || ( ( currticks() - start ) < timeout ) ) {
00051                 step();
00052                 if ( iskey() )
00053                         return getchar();
00054                 cpu_nap();
00055         }
00056 
00057         return -1;
00058 }
00059 
00060 /**
00061  * Get single keypress
00062  *
00063  * @v timeout           Timeout period, in ticks (0=indefinite)
00064  * @ret key             Key pressed
00065  *
00066  * The returned key will be an ASCII value or a KEY_XXX special
00067  * constant.  This function differs from getchar() in that getchar()
00068  * will return "special" keys (e.g. cursor keys) as a series of
00069  * characters forming an ANSI escape sequence.
00070  */
00071 int getkey ( unsigned long timeout ) {
00072         int character;
00073         unsigned int n = 0;
00074 
00075         character = getchar_timeout ( timeout );
00076         if ( character != ESC )
00077                 return character;
00078 
00079         character = getchar_timeout ( GETKEY_TIMEOUT );
00080         if ( character < 0 )
00081                 return ESC;
00082 
00083         if ( isalpha ( character ) )
00084                 return ( toupper ( character ) - 'A' + 1 );
00085 
00086         while ( ( character = getchar_timeout ( GETKEY_TIMEOUT ) ) >= 0 ) {
00087                 if ( isdigit ( character ) ) {
00088                         n = ( ( n * 10 ) + ( character - '0' ) );
00089                         continue;
00090                 }
00091                 if ( character >= 0x40 )
00092                         return KEY_ANSI ( n, character );
00093         }
00094 
00095         return ESC;
00096 }