iPXE
Macros | Functions
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)
 
static int getchar_timeout (unsigned long timeout)
 Read character from console if available within timeout period. More...
 
int getkey (unsigned long timeout)
 Get single keypress. More...
 

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 39 of file getkey.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ getchar_timeout()

static 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 47 of file getkey.c.

47  {
48  unsigned long start = currticks();
49 
50  while ( ( timeout == 0 ) || ( ( currticks() - start ) < timeout ) ) {
51  step();
52  if ( iskey() )
53  return getchar();
54  cpu_nap();
55  }
56 
57  return -1;
58 }
uint32_t start
Starting offset.
Definition: netvsc.h:12
int getchar(void)
Read a single character from any console.
Definition: console.c:82
void cpu_nap(void)
Sleep until next CPU interrupt.
void step(void)
Single-step a single process.
Definition: process.c:98
void timeout(int)
unsigned long currticks(void)
Get current system time in ticks.
Definition: timer.c:42
int iskey(void)
Check for available input on any console.
Definition: console.c:127

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 71 of file getkey.c.

71  {
72  int character;
73  unsigned int n = 0;
74 
75  character = getchar_timeout ( timeout );
76  if ( character != ESC )
77  return character;
78 
79  character = getchar_timeout ( GETKEY_TIMEOUT );
80  if ( character < 0 )
81  return ESC;
82 
83  if ( isalpha ( character ) )
84  return ( toupper ( character ) - 'A' + 1 );
85 
86  while ( ( character = getchar_timeout ( GETKEY_TIMEOUT ) ) >= 0 ) {
87  if ( isdigit ( character ) ) {
88  n = ( ( n * 10 ) + ( character - '0' ) );
89  continue;
90  }
91  if ( character >= 0x40 )
92  return KEY_ANSI ( n, character );
93  }
94 
95  return ESC;
96 }
static int isalpha(int character)
Check if character is alphabetic.
Definition: ctype.h:64
static int toupper(int character)
Convert character to upper case.
Definition: ctype.h:109
static int isdigit(int character)
Check if character is a decimal digit.
Definition: ctype.h:18
#define ESC
Escape character.
Definition: ansiesc.h:92
static int getchar_timeout(unsigned long timeout)
Read character from console if available within timeout period.
Definition: getkey.c:47
void timeout(int)
unsigned int n
Definition: entropy.h:159
#define GETKEY_TIMEOUT
Definition: getkey.c:39
#define KEY_ANSI(n, terminator)
Definition: keys.h:60

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

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