iPXE
getkey.c File Reference

Special key interpretation. More...

#include <ctype.h>
#include <ipxe/console.h>
#include <ipxe/process.h>
#include <ipxe/keys.h>
#include <ipxe/timer.h>
#include <ipxe/nap.h>

Go to the source code of this file.

Macros

#define GETKEY_TIMEOUT   ( TICKS_PER_SEC / 4 )

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static int getchar_timeout (unsigned long timeout)
 Read character from console if available within timeout period.
int getkey (unsigned long timeout)
 Get single keypress.

Detailed Description

Special key interpretation.

Definition in file getkey.c.

Macro Definition Documentation

◆ GETKEY_TIMEOUT

#define GETKEY_TIMEOUT   ( TICKS_PER_SEC / 4 )

Definition at line 40 of file getkey.c.

Referenced by getkey().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ getchar_timeout()

int getchar_timeout ( unsigned long timeout)
static

Read character from console if available within timeout period.

Parameters
timeoutTimeout period, in ticks (0=indefinite)
Return values
characterCharacter read from console

Definition at line 48 of file getkey.c.

48 {
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}
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
void timeout(int)
uint32_t start
Starting offset.
Definition netvsc.h:1
void cpu_nap(void)
Sleep with interrupts enabled until next CPU interrupt.
void step(void)
Single-step a single process.
Definition process.c:99
unsigned long currticks(void)
Get current system time in ticks.
Definition timer.c:43

References cpu_nap(), currticks(), getchar(), iskey(), start, step(), and timeout().

Referenced by getkey().

◆ getkey()

int getkey ( unsigned long timeout)

Get single keypress.

Parameters
timeoutTimeout period, in ticks (0=indefinite)
Return values
keyKey pressed

The returned key will be an ASCII value or a KEY_XXX special constant. This function differs from getchar() in that getchar() will return "special" keys (e.g. cursor keys) as a series of characters forming an ANSI escape sequence.

Definition at line 72 of file getkey.c.

72 {
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
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
#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
#define KEY_ANSI(n, terminator)
Construct ANSI escape sequence key value.
Definition keys.h:87

References ESC, getchar_timeout(), GETKEY_TIMEOUT, isalpha(), isdigit(), KEY_ANSI, timeout(), and toupper().

Referenced by bios_inject(), form_loop(), main_loop(), menu_loop(), prompt(), pxe_menu_prompt_and_select(), pxe_menu_select(), and readline_history().