iPXE
getkey.c
Go to the documentation of this file.
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 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <ctype.h>
28#include <ipxe/console.h>
29#include <ipxe/process.h>
30#include <ipxe/keys.h>
31#include <ipxe/timer.h>
32#include <ipxe/nap.h>
33
34/** @file
35 *
36 * Special key interpretation
37 *
38 */
39
40#define GETKEY_TIMEOUT ( TICKS_PER_SEC / 4 )
41
42/**
43 * Read character from console if available within timeout period
44 *
45 * @v timeout Timeout period, in ticks (0=indefinite)
46 * @ret character Character read from console
47 */
48static int getchar_timeout ( unsigned long timeout ) {
49 unsigned long start = currticks();
50
51 while ( ( timeout == 0 ) || ( ( currticks() - start ) < timeout ) ) {
52 step();
53 if ( iskey() )
54 return getchar();
55 cpu_nap();
56 }
57
58 return -1;
59}
60
61/**
62 * Get single keypress
63 *
64 * @v timeout Timeout period, in ticks (0=indefinite)
65 * @ret key Key pressed
66 *
67 * The returned key will be an ASCII value or a KEY_XXX special
68 * constant. This function differs from getchar() in that getchar()
69 * will return "special" keys (e.g. cursor keys) as a series of
70 * characters forming an ANSI escape sequence.
71 */
72int getkey ( unsigned long timeout ) {
73 int character;
74 unsigned int n = 0;
75
76 character = getchar_timeout ( timeout );
77 if ( character != ESC )
78 return character;
79
80 character = getchar_timeout ( GETKEY_TIMEOUT );
81 if ( character < 0 )
82 return ESC;
83
84 if ( isalpha ( character ) )
85 return ( toupper ( character ) - 'A' + 1 );
86
87 while ( ( character = getchar_timeout ( GETKEY_TIMEOUT ) ) >= 0 ) {
88 if ( isdigit ( character ) ) {
89 n = ( ( n * 10 ) + ( character - '0' ) );
90 continue;
91 }
92 if ( character >= 0x40 )
93 return KEY_ANSI ( n, character );
94 }
95
96 return ESC;
97}
#define ESC
Escape character.
Definition ansiesc.h:93
int getchar(void)
Read a single character from any console.
Definition console.c:86
int iskey(void)
Check for available input on any console.
Definition console.c:131
Character types.
static int toupper(int character)
Convert character to upper case.
Definition ctype.h:121
static int isdigit(int character)
Check if character is a decimal digit.
Definition ctype.h:30
static int isalpha(int character)
Check if character is alphabetic.
Definition ctype.h:76
void timeout(int)
#define GETKEY_TIMEOUT
Definition getkey.c:40
static int getchar_timeout(unsigned long timeout)
Read character from console if available within timeout period.
Definition getkey.c:48
int getkey(unsigned long timeout)
Get single keypress.
Definition getkey.c:72
uint32_t start
Starting offset.
Definition netvsc.h:1
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
User interaction.
CPU sleeping.
void cpu_nap(void)
Sleep with interrupts enabled until next CPU interrupt.
iPXE timers
Key definitions.
#define KEY_ANSI(n, terminator)
Construct ANSI escape sequence key value.
Definition keys.h:87
void step(void)
Single-step a single process.
Definition process.c:99
Processes.
unsigned long currticks(void)
Get current system time in ticks.
Definition timer.c:43