|
@@ -0,0 +1,101 @@
|
|
1
|
+#include <curses.h>
|
|
2
|
+#include <stddef.h>
|
|
3
|
+#include "core.h"
|
|
4
|
+
|
|
5
|
+/** @file
|
|
6
|
+ *
|
|
7
|
+ * MuCurses keyboard input handling functions
|
|
8
|
+ */
|
|
9
|
+
|
|
10
|
+#define INPUT_BUFFER_LEN 80
|
|
11
|
+
|
|
12
|
+bool echo_on = FALSE;
|
|
13
|
+bool delay = FALSE;
|
|
14
|
+
|
|
15
|
+/**
|
|
16
|
+ *
|
|
17
|
+ */
|
|
18
|
+int has_key ( int ch ) {
|
|
19
|
+ return TRUE;
|
|
20
|
+}
|
|
21
|
+
|
|
22
|
+/**
|
|
23
|
+ * Push a character back onto the FIFO
|
|
24
|
+ *
|
|
25
|
+ * @v ch char to push to head of input stream
|
|
26
|
+ * @ret rc return status code
|
|
27
|
+ */
|
|
28
|
+int ungetch ( int ch ) {
|
|
29
|
+ stdscr->scr->pushc( stdscr->scr, ch );
|
|
30
|
+ return OK;
|
|
31
|
+}
|
|
32
|
+
|
|
33
|
+/**
|
|
34
|
+ * Pop a character from the FIFO into a window
|
|
35
|
+ *
|
|
36
|
+ * @v *win window in which to echo input
|
|
37
|
+ * @ret ch char from input stream
|
|
38
|
+ */
|
|
39
|
+int wgetch ( WINDOW *win ) {
|
|
40
|
+ int ch;
|
|
41
|
+ if ( win == NULL )
|
|
42
|
+ return ERR;
|
|
43
|
+
|
|
44
|
+ ch = win->scr->popc( win->scr );
|
|
45
|
+
|
|
46
|
+ if ( echo_on ) {
|
|
47
|
+ if ( ch == KEY_LEFT || ch == KEY_BACKSPACE ) {
|
|
48
|
+ if ( win->curs_x == 0 ) {
|
|
49
|
+ wmove( win, --(win->curs_y), win->width - 1 );
|
|
50
|
+ wdelch( win );
|
|
51
|
+ } else {
|
|
52
|
+ wmove( win, win->curs_y, --(win->curs_x) );
|
|
53
|
+ wdelch( win );
|
|
54
|
+ }
|
|
55
|
+ } else if ( ch >= 0401 && ch <= 0633 ) {
|
|
56
|
+ beep();
|
|
57
|
+ } else {
|
|
58
|
+ _wputch( win, (chtype)( ch | win->attrs ), WRAP );
|
|
59
|
+ }
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ return ch;
|
|
63
|
+}
|
|
64
|
+
|
|
65
|
+/**
|
|
66
|
+ * Read at most n characters from the FIFO into a window
|
|
67
|
+ *
|
|
68
|
+ * @v *win window in which to echo input
|
|
69
|
+ * @v *str pointer to string in which to store result
|
|
70
|
+ */
|
|
71
|
+int wgetnstr ( WINDOW *win, char *str, int n ) {
|
|
72
|
+ char *str_start;
|
|
73
|
+ int c;
|
|
74
|
+
|
|
75
|
+ if ( n < 0 )
|
|
76
|
+ return ERR;
|
|
77
|
+
|
|
78
|
+ str_start = str;
|
|
79
|
+
|
|
80
|
+ for ( ; ( c = wgetch(win) ) && n; n-- ) {
|
|
81
|
+ if ( n == 1 ) { // last character must be a newline...
|
|
82
|
+ if ( c == '\n' ) {
|
|
83
|
+ *str = '\0';
|
|
84
|
+ } else { // ...otherwise beep and wait for one
|
|
85
|
+ beep();
|
|
86
|
+ ++n;
|
|
87
|
+ continue;
|
|
88
|
+ }
|
|
89
|
+ } else if ( c == '\n' ) {
|
|
90
|
+ *str = '\0';
|
|
91
|
+ break;
|
|
92
|
+ } else {
|
|
93
|
+ if ( c == KEY_LEFT || c == KEY_BACKSPACE ) {
|
|
94
|
+ if ( ! ( str == str_start ) )
|
|
95
|
+ str--;
|
|
96
|
+ } else { *str = c; str++; }
|
|
97
|
+ }
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ return OK;
|
|
101
|
+}
|