Browse Source

- recoded wgetch and wgetnstr for greater more efficiency

- wgetch now includes non, part and full blocking read support
tags/v0.9.3
Dan Lynch 18 years ago
parent
commit
1c87d54bb8
1 changed files with 58 additions and 52 deletions
  1. 58
    52
      src/hci/mucurses/kb.c

+ 58
- 52
src/hci/mucurses/kb.c View File

@@ -1,65 +1,74 @@
1 1
 #include <curses.h>
2 2
 #include <stddef.h>
3
+#include <timer.h>
3 4
 #include "core.h"
5
+#include "input.h"
4 6
 
5 7
 /** @file
6 8
  *
7 9
  * MuCurses keyboard input handling functions
8 10
  */
9 11
 
10
-#define INPUT_BUFFER_LEN 80
11
-
12
-bool echo_on = FALSE;
13
-bool delay = FALSE;
14
-
15 12
 /**
13
+ * Check KEY_ code supported status
16 14
  *
15
+ * @v kc	keycode value to check
16
+ * @ret TRUE	KEY_* supported
17
+ * @ret FALSE	KEY_* unsupported
17 18
  */
18
-int has_key ( int ch ) {
19
+int has_key ( int kc __unused ) {
19 20
 	return TRUE;
20 21
 }
21 22
 
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 23
 /**
34 24
  * Pop a character from the FIFO into a window
35 25
  *
36 26
  * @v *win	window in which to echo input
37
- * @ret ch	char from input stream
27
+ * @ret c	char from input stream
38 28
  */
39 29
 int wgetch ( WINDOW *win ) {
40
-	int ch;
30
+	int c, timer;
41 31
 	if ( win == NULL )
42 32
 		return ERR;
43 33
 
44
-	ch = win->scr->popc( win->scr );
34
+	timer = INPUT_DELAY_TIMEOUT;
35
+	while ( ! win->scr->peek( win->scr ) ) {
36
+		if ( m_delay == 0 ) // non-blocking read
37
+			return ERR;
38
+		if ( timer > 0 ) {
39
+			if ( m_delay > 0 )
40
+				timer -= INPUT_DELAY;
41
+			mdelay( INPUT_DELAY );
42
+		} else { return ERR; }
43
+	}
45 44
 
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) );
45
+	c = win->scr->getc( win->scr );
46
+
47
+	if ( m_echo ) {
48
+		if ( c >= 0401 && c <= 0633 ) {
49
+			switch(c) {
50
+			case KEY_LEFT :
51
+			case KEY_BACKSPACE :
52
+				if ( win->curs_x == 0 )
53
+					wmove( win, 
54
+					       --(win->curs_y), 
55
+					       win->width - 1 );
56
+				else
57
+					wmove( win, 
58
+					       win->curs_y, 
59
+					       --(win->curs_x) );
53 60
 				wdelch( win );
61
+				break;
62
+			default :
63
+				beep();
64
+				break;
54 65
 			}
55
-		} else if ( ch >= 0401 && ch <= 0633 ) {
56
-			beep();
57 66
 		} else {
58
-			_wputch( win, (chtype)( ch | win->attrs ), WRAP );
67
+			_wputch( win, (chtype)( c | win->attrs ), WRAP );
59 68
 		}
60 69
 	}
61 70
 
62
-	return ch;
71
+	return c;
63 72
 }
64 73
 
65 74
 /**
@@ -67,33 +76,30 @@ int wgetch ( WINDOW *win ) {
67 76
  *
68 77
  * @v *win	window in which to echo input
69 78
  * @v *str	pointer to string in which to store result
79
+ * @ret rc	return status code
70 80
  */
71 81
 int wgetnstr ( WINDOW *win, char *str, int n ) {
72
-	char *str_start;
82
+	char *_str;
73 83
 	int c;
74 84
 
75
-	if ( n < 0 )
76
-		return ERR;
77
-
78
-	str_start = str;
85
+	_str = str;
79 86
 
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;
87
+	while ( ( ( c = wgetch( win ) ) != KEY_ENTER ) && !( n == 0 ) ) {
88
+		if ( c >= 0401 && c <= 0633 ) {
89
+			switch(c) {
90
+			case KEY_LEFT :
91
+			case KEY_BACKSPACE :
92
+				if ( _str > str ) {
93
+					_str--; n++;
94
+				}
95
+				break;
96
+			case KEY_ENTER :
97
+				*_str = '\0';
98
+				break;
88 99
 			}
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++; }
100
+		} else { // *should* only be ASCII chars now
101
+			*(_str++) = (char)c;
102
+			n--;
97 103
 		}
98 104
 	}
99 105
 

Loading…
Cancel
Save