From c29c868475d085890454b50271bff8ad5b4b43bc Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Wed, 28 Jun 2006 11:48:40 +0000 Subject: [PATCH] - implemented cursor retreat function (_wcursback) as a core function - reimplemented cleaner wgetnstr - fixed wdelch for use with _wcursback --- src/hci/mucurses/clear.c | 5 +-- src/hci/mucurses/core.c | 19 +++++++++ src/hci/mucurses/core.h | 1 + src/hci/mucurses/kb.c | 90 +++++++++++++++++++++++----------------- 4 files changed, 74 insertions(+), 41 deletions(-) diff --git a/src/hci/mucurses/clear.c b/src/hci/mucurses/clear.c index 3f5cf9e8..a32f693f 100644 --- a/src/hci/mucurses/clear.c +++ b/src/hci/mucurses/clear.c @@ -51,11 +51,8 @@ int wclrtoeol ( WINDOW *win ) { * @ret rc return status code */ int wdelch ( WINDOW *win ) { - struct cursor_pos pos; - - _store_curs_pos( win, &pos ); _wputch( win, (unsigned)' ', NOWRAP ); - _restore_curs_pos( win, &pos ); + _wcursback( win ); return OK; } diff --git a/src/hci/mucurses/core.c b/src/hci/mucurses/core.c index 7805f428..deadc5d4 100644 --- a/src/hci/mucurses/core.c +++ b/src/hci/mucurses/core.c @@ -48,6 +48,25 @@ void _wputch ( WINDOW *win, chtype ch, int wrap ) { } } +/** + * Retreat the cursor back one position (useful for a whole host of + * ops) + * + * @v *win window in which to retreat + */ +void _wcursback ( WINDOW *win ) { + if ( win->curs_x == 0 ) { + if ( win->curs_y == 0 ) + win->curs_y = win->height - 1; + win->curs_x = win->width = 1; + } else { + win->curs_x--; + } + + win->scr->movetoyx( win->scr, win->ori_y + win->curs_y, + win->ori_x + win->curs_x ); +} + /** * Write a chtype string to a window * diff --git a/src/hci/mucurses/core.h b/src/hci/mucurses/core.h index 9a8797af..7d77681e 100644 --- a/src/hci/mucurses/core.h +++ b/src/hci/mucurses/core.h @@ -13,6 +13,7 @@ void _wputch ( WINDOW *win, chtype ch, int wrap ); void _wputchstr ( WINDOW *win, const chtype *chstr, int wrap, int n ); void _wputstr ( WINDOW *win, const char *str, int wrap, int n ); +void _wcursback ( WINDOW *win ); int wmove ( WINDOW *win, int y, int x ); #endif /* CURSES_H */ diff --git a/src/hci/mucurses/kb.c b/src/hci/mucurses/kb.c index 114e6b38..63948469 100644 --- a/src/hci/mucurses/kb.c +++ b/src/hci/mucurses/kb.c @@ -30,14 +30,9 @@ int has_key ( int kc __unused ) { return TRUE; } -/** - * Pop a character from the FIFO into a window - * - * @v *win window in which to echo input - * @ret c char from input stream - */ -int wgetch ( WINDOW *win ) { - int c, timer; +int _wgetc ( WINDOW *win ) { + int timer, c; + if ( win == NULL ) return ERR; @@ -45,28 +40,38 @@ int wgetch ( WINDOW *win ) { while ( ! win->scr->peek( win->scr ) ) { if ( m_delay == 0 ) // non-blocking read return ERR; - if ( timer > 0 ) { + if ( timer > 0 ) { // time-limited blocking read if ( m_delay > 0 ) timer -= INPUT_DELAY; mdelay( INPUT_DELAY ); - } else { return ERR; } + } else { return ERR; } // non-blocking read } c = win->scr->getc( win->scr ); + if ( m_echo && ( c >= 32 && c <= 126 ) ) // printable ASCII characters + _wputch( win, (chtype) ( c | win->attrs ), WRAP ); + + return c; +} + +/** + * Pop a character from the FIFO into a window + * + * @v *win window in which to echo input + * @ret c char from input stream + */ +int wgetch ( WINDOW *win ) { + int c; + + c = _wgetc( win ); + if ( m_echo ) { if ( c >= 0401 && c <= 0633 ) { switch(c) { case KEY_LEFT : case KEY_BACKSPACE : - if ( win->curs_x == 0 ) - wmove( win, - --(win->curs_y), - win->width - 1 ); - else - wmove( win, - win->curs_y, - --(win->curs_x) ); + _wcursback( win ); wdelch( win ); break; default : @@ -86,38 +91,49 @@ int wgetch ( WINDOW *win ) { * * @v *win window in which to echo input * @v *str pointer to string in which to store result + * @v n maximum number of characters to read into string (inc. NUL) * @ret rc return status code */ int wgetnstr ( WINDOW *win, char *str, int n ) { char *_str; int c; + if ( n == 0 ) { + str = '\0'; + return OK; + } + _str = str; - while (!( n == 0 ) ) { - c = wgetch( win ); - if ( c >= 0401 && c <= 0633 ) { - switch(c) { - case KEY_LEFT : - case KEY_BACKSPACE : - if ( _str > str ) { - _str--; n++; + while ( ( c = _wgetc( win ) ) != ERR ) { + /* termination enforcement - don't let us go past the + end of the allocated buffer... */ + if ( n == 0 && ( c >= 32 && c <= 126 ) ) { + _wcursback( win ); + wdelch( win ); + } else { + if ( c >= 0401 && c <= 0633 ) { + switch(c) { + case KEY_LEFT : + case KEY_BACKSPACE : + _wcursback( win ); + wdelch( win ); + break; + case KEY_ENTER : + *_str = '\0'; + return OK; + default : + beep(); + break; } - break; - case KEY_ENTER : - *_str = '\0'; - break; } - } else if ( c == '\n' ) { - *_str = '\0'; - break; - }else { // *should* only be ASCII chars now - *(_str++) = (char)c; - n--; + if ( c >= 32 && c <= 126 ) { + *(_str++) = c; n--; + } } } - return OK; + return ERR; }