- recoded wgetch and wgetnstr for greater more efficiency
- wgetch now includes non, part and full blocking read support
This commit is contained in:
parent
35557914e3
commit
1c87d54bb8
|
@ -1,65 +1,74 @@
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <timer.h>
|
||||||
#include "core.h"
|
#include "core.h"
|
||||||
|
#include "input.h"
|
||||||
|
|
||||||
/** @file
|
/** @file
|
||||||
*
|
*
|
||||||
* MuCurses keyboard input handling functions
|
* MuCurses keyboard input handling functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define INPUT_BUFFER_LEN 80
|
|
||||||
|
|
||||||
bool echo_on = FALSE;
|
|
||||||
bool delay = FALSE;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Check KEY_ code supported status
|
||||||
*
|
*
|
||||||
|
* @v kc keycode value to check
|
||||||
|
* @ret TRUE KEY_* supported
|
||||||
|
* @ret FALSE KEY_* unsupported
|
||||||
*/
|
*/
|
||||||
int has_key ( int ch ) {
|
int has_key ( int kc __unused ) {
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Push a character back onto the FIFO
|
|
||||||
*
|
|
||||||
* @v ch char to push to head of input stream
|
|
||||||
* @ret rc return status code
|
|
||||||
*/
|
|
||||||
int ungetch ( int ch ) {
|
|
||||||
stdscr->scr->pushc( stdscr->scr, ch );
|
|
||||||
return OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pop a character from the FIFO into a window
|
* Pop a character from the FIFO into a window
|
||||||
*
|
*
|
||||||
* @v *win window in which to echo input
|
* @v *win window in which to echo input
|
||||||
* @ret ch char from input stream
|
* @ret c char from input stream
|
||||||
*/
|
*/
|
||||||
int wgetch ( WINDOW *win ) {
|
int wgetch ( WINDOW *win ) {
|
||||||
int ch;
|
int c, timer;
|
||||||
if ( win == NULL )
|
if ( win == NULL )
|
||||||
return ERR;
|
return ERR;
|
||||||
|
|
||||||
ch = win->scr->popc( win->scr );
|
timer = INPUT_DELAY_TIMEOUT;
|
||||||
|
while ( ! win->scr->peek( win->scr ) ) {
|
||||||
|
if ( m_delay == 0 ) // non-blocking read
|
||||||
|
return ERR;
|
||||||
|
if ( timer > 0 ) {
|
||||||
|
if ( m_delay > 0 )
|
||||||
|
timer -= INPUT_DELAY;
|
||||||
|
mdelay( INPUT_DELAY );
|
||||||
|
} else { return ERR; }
|
||||||
|
}
|
||||||
|
|
||||||
if ( echo_on ) {
|
c = win->scr->getc( win->scr );
|
||||||
if ( ch == KEY_LEFT || ch == KEY_BACKSPACE ) {
|
|
||||||
if ( win->curs_x == 0 ) {
|
if ( m_echo ) {
|
||||||
wmove( win, --(win->curs_y), win->width - 1 );
|
if ( c >= 0401 && c <= 0633 ) {
|
||||||
wdelch( win );
|
switch(c) {
|
||||||
} else {
|
case KEY_LEFT :
|
||||||
wmove( win, win->curs_y, --(win->curs_x) );
|
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) );
|
||||||
wdelch( win );
|
wdelch( win );
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
beep();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} else if ( ch >= 0401 && ch <= 0633 ) {
|
|
||||||
beep();
|
|
||||||
} else {
|
} else {
|
||||||
_wputch( win, (chtype)( ch | win->attrs ), WRAP );
|
_wputch( win, (chtype)( c | win->attrs ), WRAP );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ch;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,33 +76,30 @@ int wgetch ( WINDOW *win ) {
|
||||||
*
|
*
|
||||||
* @v *win window in which to echo input
|
* @v *win window in which to echo input
|
||||||
* @v *str pointer to string in which to store result
|
* @v *str pointer to string in which to store result
|
||||||
|
* @ret rc return status code
|
||||||
*/
|
*/
|
||||||
int wgetnstr ( WINDOW *win, char *str, int n ) {
|
int wgetnstr ( WINDOW *win, char *str, int n ) {
|
||||||
char *str_start;
|
char *_str;
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
if ( n < 0 )
|
_str = str;
|
||||||
return ERR;
|
|
||||||
|
|
||||||
str_start = str;
|
while ( ( ( c = wgetch( win ) ) != KEY_ENTER ) && !( n == 0 ) ) {
|
||||||
|
if ( c >= 0401 && c <= 0633 ) {
|
||||||
for ( ; ( c = wgetch(win) ) && n; n-- ) {
|
switch(c) {
|
||||||
if ( n == 1 ) { // last character must be a newline...
|
case KEY_LEFT :
|
||||||
if ( c == '\n' ) {
|
case KEY_BACKSPACE :
|
||||||
*str = '\0';
|
if ( _str > str ) {
|
||||||
} else { // ...otherwise beep and wait for one
|
_str--; n++;
|
||||||
beep();
|
}
|
||||||
++n;
|
break;
|
||||||
continue;
|
case KEY_ENTER :
|
||||||
|
*_str = '\0';
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} else if ( c == '\n' ) {
|
} else { // *should* only be ASCII chars now
|
||||||
*str = '\0';
|
*(_str++) = (char)c;
|
||||||
break;
|
n--;
|
||||||
} else {
|
|
||||||
if ( c == KEY_LEFT || c == KEY_BACKSPACE ) {
|
|
||||||
if ( ! ( str == str_start ) )
|
|
||||||
str--;
|
|
||||||
} else { *str = c; str++; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue