From 6485715a3cb7ef3daf141daf8deacca82ddd83ba Mon Sep 17 00:00:00 2001 From: William McBrine Date: Thu, 5 Jan 2006 03:52:21 +0000 Subject: [PATCH] Simplified wgetch(); reformatting. --- pdcurses/getch.c | 313 ++++++++++++++++++++--------------------------- 1 file changed, 132 insertions(+), 181 deletions(-) diff --git a/pdcurses/getch.c b/pdcurses/getch.c index bd7778b6..c5aaf15b 100644 --- a/pdcurses/getch.c +++ b/pdcurses/getch.c @@ -19,7 +19,7 @@ */ #define CURSES_LIBRARY 1 #ifdef HAVE_CONFIG_H -# include +# include #endif #include @@ -39,7 +39,7 @@ #endif #ifdef PDCDEBUG -char *rcsid_getch = "$Id: getch.c,v 1.12 2006/01/03 20:07:15 wmcbrine Exp $"; +char *rcsid_getch = "$Id: getch.c,v 1.13 2006/01/05 03:52:21 wmcbrine Exp $"; #endif /*man-start********************************************************************* @@ -47,58 +47,57 @@ char *rcsid_getch = "$Id: getch.c,v 1.12 2006/01/03 20:07:15 wmcbrine Exp $"; Name: getch Synopsis: - int getch(void); - int wgetch(WINDOW *win); - int mvgetch(int y, int x); - int mvwgetch(WINDOW *win, int y, int x); - int ungetch(int ch); + int getch(void); + int wgetch(WINDOW *win); + int mvgetch(int y, int x); + int mvwgetch(WINDOW *win, int y, int x); + int ungetch(int ch); X/Open Description: - With the getch(), wgetch(), mvgetch(), and mvwgetch() functions, - a character is read from the terminal associated with the window. - In nodelay mode, if there is no input - waiting, the value ERR is returned. In delay mode, the program will - hang until the system passes text through to the program. - Depending on the setting of cbreak(), this will be after one - character or after the first newline. Unless noecho() has - been set, the character will also be echoed into the designated - window. + With the getch(), wgetch(), mvgetch(), and mvwgetch() functions, + a character is read from the terminal associated with the window. + In nodelay mode, if there is no input waiting, the value ERR is + returned. In delay mode, the program will hang until the system + passes text through to the program. Depending on the setting of + cbreak(), this will be after one character or after the first + newline. Unless noecho() has been set, the character will also + be echoed into the designated window. - If keypad() is TRUE, and a function key is pressed, the token for - that function key will be returned instead of the raw characters. - Possible function keys are defined in with integers - beginning with 0401, whose names begin with KEY_. If a character - is received that could be the beginning of a function key (such as - escape), curses will set a timer. If the remainder of the sequence - does not come in within the designated time, the character will be - passed through, otherwise the function key value will be returned. - For this reason, on many terminals, there will be a delay after a - user presses the escape key before the escape is returned to the - program. (Use by a programmer of the escape key for a single - character function is discouraged.) + If keypad() is TRUE, and a function key is pressed, the token for + that function key will be returned instead of the raw characters. + Possible function keys are defined in with integers + beginning with 0401, whose names begin with KEY_. If a character + is received that could be the beginning of a function key (such as + escape), curses will set a timer. If the remainder of the sequence + does not come in within the designated time, the character will be + passed through, otherwise the function key value will be returned. + For this reason, on many terminals, there will be a delay after a + user presses the escape key before the escape is returned to the + program. (Use by a programmer of the escape key for a single + character function is discouraged.) - If nodelay(win,TRUE) has been called on the window and no input is - waiting, the value ERR is returned. + If nodelay(win,TRUE) has been called on the window and no input + is waiting, the value ERR is returned. - The ungetch() function places ch back onto the input queue to be - returned by the next call to wgetch(). + The ungetch() function places ch back onto the input queue to be + returned by the next call to wgetch(). - NOTE: getch(), mvgetch() and mvwgetch() are implemented as macros. + NOTE: getch(), mvgetch() and mvwgetch() are implemented as macros. PDCurses Description: - Given the nature of the PC, there is no such timer set for an - incoming ESCAPE value, because function keys generate unique - scan codes that are not prefixed with the ESCAPE character. + Given the nature of the PC, there is no such timer set for an + incoming ESCAPE value, because function keys generate unique + scan codes that are not prefixed with the ESCAPE character. - Also, note that the getch() definition will conflict with - many DOS compiler's runtime libraries. + Also, note that the getch() definition will conflict with + many DOS compiler's runtime libraries. X/Open Return Value: - This functions return ERR or the value of the character, meta - character or function key token. + This functions return ERR or the value of the character, meta + character or function key token. X/Open Errors: - No errors are defined for this function. + No errors are defined for this function. Portability X/Open BSD SYS V Dec '88 @@ -110,11 +109,11 @@ char *rcsid_getch = "$Id: getch.c,v 1.12 2006/01/03 20:07:15 wmcbrine Exp $"; **man-end**********************************************************************/ -static WINDOW *w; /* to reduce stack usage */ +/* these defines to get around DOS library conflicts */ -/* this define to get around DOS libraries conflict */ #define getch PDC_getch #define ungetch PDC_ungetch + /***********************************************************************/ #ifdef HAVE_PROTO int PDC_CDECL PDC_getch(void) @@ -126,10 +125,11 @@ int PDC_CDECL PDC_getch() PDC_LOG(("getch() - called\n")); if (stdscr == (WINDOW *)NULL) - return( ERR ); + return ERR; - return(wgetch(stdscr)); + return wgetch(stdscr); } + /***********************************************************************/ #ifdef HAVE_PROTO int PDC_CDECL wgetch(WINDOW *win) @@ -145,76 +145,60 @@ extern int c_ungind; /* ungetch() push index */ extern int c_ungch[NUNGETCH]; /* array of ungotten chars */ extern WINDOW* _getch_win_; -/* signed key; MH*/ - int key; -#if defined(DOS) || defined(OS2) || defined(WIN32) - bool cbr; -#endif -/* static chtype buffer[_INBUFSIZ];MH*/ /* character buffer */ - static int buffer[_INBUFSIZ]; /* character buffer */ + int key; + + static int buffer[_INBUFSIZ]; /* character buffer */ short display_key = 0x100; int waitingtenths = 0; PDC_LOG(("wgetch() - called\n")); if (win == (WINDOW *)NULL) - return( ERR ); + return ERR; + + /* set the number of 1/20th second napms() calls */ if ( SP->delaytenths ) - waitingtenths = 2*SP->delaytenths; /* set the number of 1/20th second napms() calls */ - else if ( win->_delayms ) - { - /* - * As granularity of clocks is not ideal for waiting for individual periods - * of 1 millisecond, we need to determine a reasonably accurate mechanism - * based on the specified delay period. As delaying by 1/20th of a second - * is reasonable, then determine how many 1/20th seconds are in the specified - * delay time, and pause that many times. - */ - waitingtenths = win->_delayms / 50; - if (waitingtenths == 0) - waitingtenths = 1; - } + waitingtenths = 2*SP->delaytenths; + else + if ( win->_delayms ) + { +/* + * As granularity of clocks is not ideal for waiting for individual periods + * of 1 millisecond, we need to determine a reasonably accurate mechanism + * based on the specified delay period. As delaying by 1/20th of a second + * is reasonable, then determine how many 1/20th seconds are in the specified + * delay time, and pause that many times. + */ + waitingtenths = win->_delayms / 50; + if (waitingtenths == 0) + waitingtenths = 1; + } - PDC_LOG(("initial: %d delaytenths %d delayms %d\n", waitingtenths,SP->delaytenths, win->_delayms)); + PDC_LOG(("initial: %d delaytenths %d delayms %d\n", + waitingtenths, SP->delaytenths, win->_delayms)); /* wrs (7/31/93) -- System V curses refreshes window when wgetch is called */ /* if there have been changes to it and it is not a pad */ - if( ! (win->_flags & _PAD) ) - { - if( is_wintouched(win) ) - wrefresh(win); - } + + if ( !(win->_flags & _PAD) && is_wintouched(win) ) + wrefresh(win); _getch_win_ = win; - if (c_ungind) - { /* if ungotten char exists */ - /* remove and return it */ - if ((! (win->_flags & _PAD)) && (win->_nodelay)) - { - if ( is_wintouched(win) ) - wrefresh(win); - } - return( c_ungch[--c_ungind] ); - } + /* if ungotten char exists, remove and return it */ - if ((!SP->raw_inp) && (!SP->cbreak)) - { - /* - * if normal - */ - if (c_gindex < c_pindex) - { - /* - * and data in buffer - */ - return( buffer[c_gindex++] ); - } - } + if (c_ungind) + return c_ungch[--c_ungind]; - w = win; /* static for speed & stack */ - c_pindex = 0; /* prepare to buffer data */ + /* if normal and data in buffer */ + + if ((!SP->raw_inp && !SP->cbreak) && (c_gindex < c_pindex)) + return buffer[c_gindex++]; + + /* prepare to buffer data */ + + c_pindex = 0; c_gindex = 0; /* @@ -224,113 +208,83 @@ extern WINDOW* _getch_win_; for(;;) /* loop for any buffering */ { -#if defined(DOS) || defined(OS2) || defined(WIN32) +#ifdef XCURSES + key = PDC_rawgetch(); + if (!(_getch_win_->_use_keypad) && (unsigned int)key > 255) + key = -1; +#else if (SP->raw_inp) - { - /* - * get a raw character - */ - key = PDC_rawgetch(); - } + key = PDC_rawgetch(); /* get a raw character */ else { /* * get a system character * if break return proper */ - cbr = PDC_get_ctrl_break(); + bool cbr = PDC_get_ctrl_break(); PDC_set_ctrl_break(SP->orgcbr); key = PDC_sysgetch(); - PDC_set_ctrl_break(cbr); /* restore as it was */ + PDC_set_ctrl_break(cbr); } #endif -#if defined(XCURSES) - key = PDC_rawgetch(); - if (!(_getch_win_->_use_keypad) && (unsigned int)key > 255) - key = (-1); -#endif - /* * Handle timeout() and halfdelay(). */ - if (SP->delaytenths || w->_delayms) + if (SP->delaytenths || win->_delayms) { - PDC_LOG(("waiting: %d delaytenths %d delayms %d\n", waitingtenths,SP->delaytenths, w->_delayms)); + PDC_LOG(("waiting: %d delaytenths %d delayms %d\n", + waitingtenths, SP->delaytenths, win->_delayms)); - if (waitingtenths == 0 && key == (-1)) - return(ERR); - if (key == (-1)) + if (waitingtenths == 0 && key == -1) + return ERR; + + if (key == -1) { waitingtenths--; - napms(50); /* sleep for 1/20th second */ + napms(50); /* sleep for 1/20th second */ continue; } } else - { - if (w->_nodelay) - { - /* - * if nodelay and no char, return ERR - */ - if (key == -1) - return( ERR ); - else if ( ! SP->echo ) { - if ( ! (w->_flags & _PAD) ) { - if ( is_wintouched(w) ) - wrefresh(w); - } - } - } - } + if ((win->_nodelay) && (key == -1)) + return ERR; - if ((key == '\r') && - (SP->autocr) && - (!SP->raw_inp)) - { - /* - * translate CR - */ + /* translate CR */ + + if ((key == '\r') && (SP->autocr) && (!SP->raw_inp)) key = '\n'; - } + + /* if echo is enabled */ + if ( SP->echo && (key < display_key) ) { - /* - * if echo is enabled - */ - waddch(w, key); - wrefresh(w); - } - if ( (SP->raw_inp || SP->cbreak) ) - { - /* - * if no buffering - */ - return( key ); + waddch(win, key); + wrefresh(win); } + /* if no buffering */ + + if ( (SP->raw_inp || SP->cbreak) ) + return key; + + /* if no overflow, put data in buffer */ + if (c_pindex < _INBUFSIZ - 2) - { - /* - * if no overflow, put data in buffer - */ buffer[c_pindex++] = key; - } + + /* if we got a line */ + if ((key == '\n') || (key == '\r')) - { - /* - * if we got a line - */ - return( buffer[c_gindex++] ); - } + return buffer[c_gindex++]; } } + /***********************************************************************/ #ifdef HAVE_PROTO int PDC_CDECL mvgetch(int y, int x) #else -int PDC_CDECL mvgetch(y,x) +int PDC_CDECL mvgetch(y, x) int y; int x; #endif @@ -338,18 +292,17 @@ int x; { PDC_LOG(("mvgetch() - called\n")); - if (stdscr == (WINDOW *)NULL) - return( ERR ); + if ((stdscr == (WINDOW *)NULL) || (move(y, x) == ERR)) + return ERR; - if (move(y, x) == ERR) - return(ERR); - return(getch()); + return getch(); } + /***********************************************************************/ #ifdef HAVE_PROTO int PDC_CDECL mvwgetch(WINDOW *win, int y, int x) #else -int PDC_CDECL mvwgetch(win,y,x) +int PDC_CDECL mvwgetch(win, y, x) WINDOW *win; int y; int x; @@ -358,13 +311,12 @@ int x; { PDC_LOG(("mvwgetch() - called\n")); - if (win == (WINDOW *)NULL) - return( ERR ); - - if (wmove(win,y, x) == ERR) + if ((win == (WINDOW *)NULL) || (wmove(win, y, x) == ERR)) return(ERR); - return(wgetch(win)); + + return wgetch(win); } + /***********************************************************************/ #ifdef HAVE_PROTO int PDC_CDECL PDC_ungetch(int ch) @@ -380,9 +332,8 @@ extern int c_ungch[NUNGETCH]; /* array of ungotten chars */ PDC_LOG(("ungetch() - called\n")); if (c_ungind >= NUNGETCH) /* pushback stack full */ - return( ERR ); + return ERR; c_ungch[c_ungind++] = ch; - return( OK ); + return OK; } -