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
11FILE_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
16int m_delay; /*
17 < 0 : blocking read
18 0 : non-blocking read
19 > 0 : timed blocking read
20 */
21bool m_echo;
23
24static 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 )
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 */
55int 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 */
88int 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 */
133int echo ( void ) {
134 m_echo = TRUE;
135 return OK;
136}
137
138/**
139 *
140 */
141int noecho ( void ) {
142 m_echo = FALSE;
143 return OK;
144}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
int beep(void)
Audible signal.
Definition alert.c:17
int wdelch(WINDOW *win)
Delete character under the cursor in a window.
Definition clear.c:56
MuCurses header file.
uint32_t chtype
Definition curses.h:30
#define ERR
Definition curses.h:19
#define OK
Definition curses.h:25
struct _curses_window WINDOW
Curses Window struct.
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define INPUT_DELAY_TIMEOUT
Definition kb.c:14
int echo(void)
Definition kb.c:133
static int _wgetc(WINDOW *win)
Definition kb.c:24
bool m_cbreak
Definition kb.c:22
bool m_echo
Definition kb.c:21
int noecho(void)
Definition kb.c:141
int wgetch(WINDOW *win)
Pop a character from the FIFO into a window.
Definition kb.c:55
int m_delay
Definition kb.c:16
int wgetnstr(WINDOW *win, char *str, int n)
Read at most n characters from the FIFO into a window.
Definition kb.c:88
#define INPUT_DELAY
Definition kb.c:13
#define KEY_BACKSPACE
Definition keys.h:128
#define KEY_ENTER
Definition keys.h:129
#define KEY_MIN
Minimum value for special keypresses.
Definition keys.h:70
#define KEY_LEFT
Left arrow.
Definition keys.h:109
void _wcursback(WINDOW *win) __nonnull
Retreat the cursor back one position (useful for a whole host of ops)
Definition mucurses.c:88
void _wputch(WINDOW *win, chtype ch, int wrap) __nonnull
Write a single character rendition to a window.
Definition mucurses.c:51
MuCurses core implementation specific header file.
#define WRAP
Definition mucurses.h:13
int(* getc)(struct _curses_screen *scr)
Pop a character from the keyboard input stream.
Definition curses.h:71
bool(* peek)(struct _curses_screen *scr)
Checks to see whether a character is waiting in the input stream.
Definition curses.h:79
attr_t attrs
window attributes
Definition curses.h:94
SCREEN * scr
screen with which window associates
Definition curses.h:92
A timer.
Definition timer.h:29
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition timer.c:79
#define TRUE
Definition tlan.h:46
#define FALSE
Definition tlan.h:45