iPXE
kb.c
Go to the documentation of this file.
1 #include <curses.h>
2 #include <stddef.h>
3 #include <unistd.h>
4 #include "mucurses.h"
5 
6 /** @file
7  *
8  * MuCurses keyboard input handling functions
9  */
10 
11 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
12 
13 #define INPUT_DELAY 200 // half-blocking delay timer resolution (ms)
14 #define INPUT_DELAY_TIMEOUT 1000 // half-blocking delay timeout
15 
16 int m_delay; /*
17  < 0 : blocking read
18  0 : non-blocking read
19  > 0 : timed blocking read
20  */
21 bool m_echo;
22 bool m_cbreak;
23 
24 static int _wgetc ( WINDOW *win ) {
25  int timer, c;
26 
27  if ( win == NULL )
28  return ERR;
29 
31  while ( ! win->scr->peek( win->scr ) ) {
32  if ( m_delay == 0 ) // non-blocking read
33  return ERR;
34  if ( timer > 0 ) { // time-limited blocking read
35  if ( m_delay > 0 )
36  timer -= INPUT_DELAY;
38  } else { return ERR; } // non-blocking read
39  }
40 
41  c = win->scr->getc( win->scr );
42 
43  if ( m_echo && ( c >= 32 && c <= 126 ) ) // printable ASCII characters
44  _wputch( win, (chtype) ( c | win->attrs ), WRAP );
45 
46  return c;
47 }
48 
49 /**
50  * Pop a character from the FIFO into a window
51  *
52  * @v *win window in which to echo input
53  * @ret c char from input stream
54  */
55 int wgetch ( WINDOW *win ) {
56  int c;
57 
58  c = _wgetc( win );
59 
60  if ( m_echo ) {
61  if ( c >= KEY_MIN ) {
62  switch(c) {
63  case KEY_LEFT :
64  case KEY_BACKSPACE :
65  _wcursback( win );
66  wdelch( win );
67  break;
68  default :
69  beep();
70  break;
71  }
72  } else {
73  _wputch( win, (chtype)( c | win->attrs ), WRAP );
74  }
75  }
76 
77  return c;
78 }
79 
80 /**
81  * Read at most n characters from the FIFO into a window
82  *
83  * @v *win window in which to echo input
84  * @v *str pointer to string in which to store result
85  * @v n maximum number of characters to read into string (inc. NUL)
86  * @ret rc return status code
87  */
88 int wgetnstr ( WINDOW *win, char *str, int n ) {
89  char *_str;
90  int c;
91 
92  if ( n == 0 ) {
93  *str = '\0';
94  return OK;
95  }
96 
97  _str = str;
98 
99  while ( ( c = _wgetc( win ) ) != ERR ) {
100  /* termination enforcement - don't let us go past the
101  end of the allocated buffer... */
102  if ( n == 0 && ( c >= 32 && c <= 126 ) ) {
103  _wcursback( win );
104  wdelch( win );
105  } else {
106  if ( c >= 32 && c <= 126 ) {
107  *(_str++) = c; n--;
108  } else {
109  switch(c) {
110  case KEY_LEFT :
111  case KEY_BACKSPACE :
112  _wcursback( win );
113  wdelch( win );
114  break;
115  case KEY_ENTER :
116  *_str = '\0';
117  return OK;
118  default :
119  beep();
120  break;
121  }
122  }
123  }
124  }
125 
126  return ERR;
127 }
128 
129 
130 /**
131  *
132  */
133 int echo ( void ) {
134  m_echo = TRUE;
135  return OK;
136 }
137 
138 /**
139  *
140  */
141 int noecho ( void ) {
142  m_echo = FALSE;
143  return OK;
144 }
int noecho(void)
Definition: kb.c:141
MuCurses header file.
#define KEY_ENTER
Definition: keys.h:127
static __always_inline void off_t int c
Definition: librm.h:173
int wgetch(WINDOW *win)
Pop a character from the FIFO into a window.
Definition: kb.c:55
static int _wgetc(WINDOW *win)
Definition: kb.c:24
Curses Window struct.
Definition: curses.h:89
bool m_cbreak
Definition: kb.c:22
int m_delay
Definition: kb.c:16
A timer.
Definition: timer.h:28
MuCurses core implementation specific header file.
Definition: sis900.h:208
#define ERR
Definition: curses.h:18
#define KEY_MIN
Minimum value for special keypresses.
Definition: keys.h:68
#define INPUT_DELAY_TIMEOUT
Definition: kb.c:14
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
bool(* peek)(struct _curses_screen *scr)
Checks to see whether a character is waiting in the input stream.
Definition: curses.h:78
#define INPUT_DELAY
Definition: kb.c:13
SCREEN * scr
screen with which window associates
Definition: curses.h:91
attr_t attrs
window attributes
Definition: curses.h:93
bool m_echo
Definition: kb.c:21
int wdelch(WINDOW *win)
Delete character under the cursor in a window.
Definition: clear.c:55
void _wputch(WINDOW *win, chtype ch, int wrap) __nonnull
Write a single character rendition to a window.
Definition: mucurses.c:50
#define TRUE
Definition: tlan.h:46
int(* getc)(struct _curses_screen *scr)
Pop a character from the keyboard input stream.
Definition: curses.h:70
int wgetnstr(WINDOW *win, char *str, int n)
Read at most n characters from the FIFO into a window.
Definition: kb.c:88
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition: timer.c:78
static struct timer * timer
Current timer.
Definition: timer.c:35
#define FALSE
Definition: tlan.h:45
void _wcursback(WINDOW *win) __nonnull
Retreat the cursor back one position (useful for a whole host of ops)
Definition: mucurses.c:87
uint32_t chtype
Definition: curses.h:29
#define KEY_BACKSPACE
Definition: keys.h:126
int beep(void)
Audible signal.
Definition: alert.c:17
#define WRAP
Definition: mucurses.h:12
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define KEY_LEFT
Left arrow.
Definition: keys.h:107
int echo(void)
Definition: kb.c:133