Explorar el Código

- implemented cursor retreat function (_wcursback) as a core function

- reimplemented cleaner wgetnstr
- fixed wdelch for use with _wcursback
tags/v0.9.3
Dan Lynch hace 18 años
padre
commit
c29c868475
Se han modificado 4 ficheros con 74 adiciones y 41 borrados
  1. 1
    4
      src/hci/mucurses/clear.c
  2. 19
    0
      src/hci/mucurses/core.c
  3. 1
    0
      src/hci/mucurses/core.h
  4. 53
    37
      src/hci/mucurses/kb.c

+ 1
- 4
src/hci/mucurses/clear.c Ver fichero

@@ -51,11 +51,8 @@ int wclrtoeol ( WINDOW *win ) {
51 51
  * @ret rc	return status code
52 52
  */
53 53
 int wdelch ( WINDOW *win ) {
54
-	struct cursor_pos pos;
55
-
56
-	_store_curs_pos( win, &pos );
57 54
 	_wputch( win, (unsigned)' ', NOWRAP );
58
-	_restore_curs_pos( win, &pos );
55
+	_wcursback( win );
59 56
 
60 57
 	return OK;
61 58
 }

+ 19
- 0
src/hci/mucurses/core.c Ver fichero

@@ -48,6 +48,25 @@ void _wputch ( WINDOW *win, chtype ch, int wrap ) {
48 48
 	}
49 49
 }
50 50
 
51
+/**
52
+ * Retreat the cursor back one position (useful for a whole host of
53
+ * ops)
54
+ *
55
+ * @v *win	window in which to retreat
56
+ */
57
+void _wcursback ( WINDOW *win ) {
58
+	if ( win->curs_x == 0 ) {
59
+		if ( win->curs_y == 0 )
60
+			win->curs_y = win->height - 1;
61
+		win->curs_x = win->width = 1;
62
+	} else {
63
+		win->curs_x--;
64
+	}
65
+
66
+	win->scr->movetoyx( win->scr, win->ori_y + win->curs_y,
67
+   			     	      win->ori_x + win->curs_x );
68
+}
69
+
51 70
 /**
52 71
  * Write a chtype string to a window
53 72
  *

+ 1
- 0
src/hci/mucurses/core.h Ver fichero

@@ -13,6 +13,7 @@
13 13
 void _wputch ( WINDOW *win, chtype ch, int wrap );
14 14
 void _wputchstr ( WINDOW *win, const chtype *chstr, int wrap, int n );
15 15
 void _wputstr ( WINDOW *win, const char *str, int wrap, int n );
16
+void _wcursback ( WINDOW *win );
16 17
 int wmove ( WINDOW *win, int y, int x );
17 18
 
18 19
 #endif /* CURSES_H */

+ 53
- 37
src/hci/mucurses/kb.c Ver fichero

@@ -30,14 +30,9 @@ int has_key ( int kc __unused ) {
30 30
 	return TRUE;
31 31
 }
32 32
 
33
-/**
34
- * Pop a character from the FIFO into a window
35
- *
36
- * @v *win	window in which to echo input
37
- * @ret c	char from input stream
38
- */
39
-int wgetch ( WINDOW *win ) {
40
-	int c, timer;
33
+int _wgetc ( WINDOW *win ) {
34
+	int timer, c;
35
+
41 36
 	if ( win == NULL )
42 37
 		return ERR;
43 38
 
@@ -45,28 +40,38 @@ int wgetch ( WINDOW *win ) {
45 40
 	while ( ! win->scr->peek( win->scr ) ) {
46 41
 		if ( m_delay == 0 ) // non-blocking read
47 42
 			return ERR;
48
-		if ( timer > 0 ) {
43
+		if ( timer > 0 ) {  // time-limited blocking read
49 44
 			if ( m_delay > 0 )
50 45
 				timer -= INPUT_DELAY;
51 46
 			mdelay( INPUT_DELAY );
52
-		} else { return ERR; }
47
+		} else { return ERR; } // non-blocking read
53 48
 	}
54 49
 
55 50
 	c = win->scr->getc( win->scr );
56 51
 
52
+	if ( m_echo && ( c >= 32 && c <= 126 ) ) // printable ASCII characters
53
+		_wputch( win, (chtype) ( c | win->attrs ), WRAP );
54
+
55
+	return c;
56
+}
57
+
58
+/**
59
+ * Pop a character from the FIFO into a window
60
+ *
61
+ * @v *win	window in which to echo input
62
+ * @ret c	char from input stream
63
+ */
64
+int wgetch ( WINDOW *win ) {
65
+	int c;
66
+
67
+	c = _wgetc( win );
68
+
57 69
 	if ( m_echo ) {
58 70
 		if ( c >= 0401 && c <= 0633 ) {
59 71
 			switch(c) {
60 72
 			case KEY_LEFT :
61 73
 			case KEY_BACKSPACE :
62
-				if ( win->curs_x == 0 )
63
-					wmove( win, 
64
-					       --(win->curs_y), 
65
-					       win->width - 1 );
66
-				else
67
-					wmove( win, 
68
-					       win->curs_y, 
69
-					       --(win->curs_x) );
74
+				_wcursback( win );
70 75
 				wdelch( win );
71 76
 				break;
72 77
 			default :
@@ -86,38 +91,49 @@ int wgetch ( WINDOW *win ) {
86 91
  *
87 92
  * @v *win	window in which to echo input
88 93
  * @v *str	pointer to string in which to store result
94
+ * @v n		maximum number of characters to read into string (inc. NUL)
89 95
  * @ret rc	return status code
90 96
  */
91 97
 int wgetnstr ( WINDOW *win, char *str, int n ) {
92 98
 	char *_str;
93 99
 	int c;
94 100
 
101
+	if ( n == 0 ) {
102
+		str = '\0';
103
+		return OK;
104
+	}
105
+
95 106
 	_str = str;
96 107
 
97
-	while (!( n == 0 ) ) {
98
-		c = wgetch( win );
99
-		if ( c >= 0401 && c <= 0633 ) {
100
-			switch(c) {
101
-			case KEY_LEFT :
102
-			case KEY_BACKSPACE :
103
-				if ( _str > str ) {
104
-					_str--; n++;
108
+	while ( ( c = _wgetc( win ) ) != ERR ) {
109
+		/* termination enforcement - don't let us go past the
110
+		   end of the allocated buffer... */
111
+		if ( n == 0 && ( c >= 32 && c <= 126 ) ) {
112
+			_wcursback( win );
113
+			wdelch( win );
114
+		} else {
115
+			if ( c >= 0401 && c <= 0633 ) {
116
+				switch(c) {
117
+				case KEY_LEFT :
118
+				case KEY_BACKSPACE :
119
+					_wcursback( win );
120
+					wdelch( win );
121
+					break;
122
+				case KEY_ENTER :
123
+					*_str = '\0';
124
+					return OK;
125
+				default :
126
+					beep();
127
+					break;
105 128
 				}
106
-				break;
107
-			case KEY_ENTER :
108
-				*_str = '\0';
109
-				break;
110 129
 			}
111
-		} else if ( c == '\n' ) {
112
-			*_str = '\0';
113
-			break;
114
-		}else { // *should* only be ASCII chars now
115
-			*(_str++) = (char)c;
116
-			n--;
130
+			if ( c >= 32 && c <= 126 ) {
131
+				*(_str++) = c; n--;
132
+			}
117 133
 		}
118 134
 	}
119 135
 
120
-	return OK;
136
+	return ERR;
121 137
 }
122 138
 
123 139
 

Loading…
Cancelar
Guardar