kb.c

Go to the documentation of this file.
00001 #include <curses.h>
00002 #include <stddef.h>
00003 #include <unistd.h>
00004 #include "mucurses.h"
00005 
00006 /** @file
00007  *
00008  * MuCurses keyboard input handling functions
00009  */
00010 
00011 #define INPUT_DELAY             200 // half-blocking delay timer resolution (ms)
00012 #define INPUT_DELAY_TIMEOUT     1000 // half-blocking delay timeout
00013 
00014 int m_delay; /* 
00015                 < 0 : blocking read
00016                 0   : non-blocking read
00017                 > 0 : timed blocking read
00018              */
00019 bool m_echo;
00020 bool m_cbreak;
00021 
00022 static int _wgetc ( WINDOW *win ) {
00023         int timer, c;
00024 
00025         if ( win == NULL )
00026                 return ERR;
00027 
00028         timer = INPUT_DELAY_TIMEOUT;
00029         while ( ! win->scr->peek( win->scr ) ) {
00030                 if ( m_delay == 0 ) // non-blocking read
00031                         return ERR;
00032                 if ( timer > 0 ) {  // time-limited blocking read
00033                         if ( m_delay > 0 )
00034                                 timer -= INPUT_DELAY;
00035                         mdelay( INPUT_DELAY );
00036                 } else { return ERR; } // non-blocking read
00037         }
00038 
00039         c = win->scr->getc( win->scr );
00040 
00041         if ( m_echo && ( c >= 32 && c <= 126 ) ) // printable ASCII characters
00042                 _wputch( win, (chtype) ( c | win->attrs ), WRAP );
00043 
00044         return c;
00045 }
00046 
00047 /**
00048  * Pop a character from the FIFO into a window
00049  *
00050  * @v *win      window in which to echo input
00051  * @ret c       char from input stream
00052  */
00053 int wgetch ( WINDOW *win ) {
00054         int c;
00055 
00056         c = _wgetc( win );
00057 
00058         if ( m_echo ) {
00059                 if ( c >= KEY_MIN ) {
00060                         switch(c) {
00061                         case KEY_LEFT :
00062                         case KEY_BACKSPACE :
00063                                 _wcursback( win );
00064                                 wdelch( win );
00065                                 break;
00066                         default :
00067                                 beep();
00068                                 break;
00069                         }
00070                 } else {
00071                         _wputch( win, (chtype)( c | win->attrs ), WRAP );
00072                 }
00073         }
00074 
00075         return c;
00076 }
00077 
00078 /**
00079  * Read at most n characters from the FIFO into a window
00080  *
00081  * @v *win      window in which to echo input
00082  * @v *str      pointer to string in which to store result
00083  * @v n         maximum number of characters to read into string (inc. NUL)
00084  * @ret rc      return status code
00085  */
00086 int wgetnstr ( WINDOW *win, char *str, int n ) {
00087         char *_str;
00088         int c;
00089 
00090         if ( n == 0 ) {
00091                 str = '\0';
00092                 return OK;
00093         }
00094 
00095         _str = str;
00096 
00097         while ( ( c = _wgetc( win ) ) != ERR ) {
00098                 /* termination enforcement - don't let us go past the
00099                    end of the allocated buffer... */
00100                 if ( n == 0 && ( c >= 32 && c <= 126 ) ) {
00101                         _wcursback( win );
00102                         wdelch( win );
00103                 } else {
00104                         if ( c >= KEY_MIN ) {
00105                                 switch(c) {
00106                                 case KEY_LEFT :
00107                                 case KEY_BACKSPACE :
00108                                         _wcursback( win );
00109                                         wdelch( win );
00110                                         break;
00111                                 case KEY_ENTER :
00112                                         *_str = '\0';
00113                                         return OK;
00114                                 default :
00115                                         beep();
00116                                         break;
00117                                 }
00118                         }
00119                         if ( c >= 32 && c <= 126 ) {
00120                                 *(_str++) = c; n--;
00121                         }
00122                 }
00123         }
00124 
00125         return ERR;
00126 }
00127 
00128 
00129 /**
00130  *
00131  */
00132 int echo ( void ) {
00133         m_echo = TRUE;
00134         return OK;
00135 }
00136 
00137 /**
00138  *
00139  */
00140 int noecho ( void ) {
00141         m_echo = FALSE;
00142         return OK;
00143 }

Generated on Tue Apr 6 20:01:06 2010 for gPXE by  doxygen 1.5.7.1