kbhit.cpp
Go to the documentation of this file.
00001 // .cpp
00002 
00003 #ifdef _WIN32
00004 #else
00005 
00006 
00007 #include "hrl_lib/kbhit.h"
00008 
00009 static struct termios termattr, save_termattr;
00010 static int ttysavefd = -1;
00011 static enum 
00012 { 
00013   RESET, RAW, CBREAK 
00014 } ttystate = RESET;
00015 
00016 /* ***************************************************************************
00017  *
00018  * set_tty_raw(), put the user's TTY in one-character-at-a-time mode.
00019  * returns 0 on success, -1 on failure.
00020  *
00021  *************************************************************************** */
00022 int
00023 set_tty_raw(void) 
00024 {
00025   int i;
00026 
00027   i = tcgetattr (STDIN_FILENO, &termattr);
00028   if (i < 0) 
00029   {
00030     printf("tcgetattr() returned %d for fildes=%d\n",i,STDIN_FILENO); 
00031     perror ("");
00032     return -1;
00033   }
00034   save_termattr = termattr;
00035 
00036   termattr.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
00037   termattr.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
00038   termattr.c_cflag &= ~(CSIZE | PARENB);
00039   termattr.c_cflag |= CS8;
00040   termattr.c_oflag &= ~(OPOST);
00041    
00042   termattr.c_cc[VMIN] = 1;  /* or 0 for some Unices;  see note 1 */
00043   termattr.c_cc[VTIME] = 0;
00044 
00045   i = tcsetattr (STDIN_FILENO, TCSANOW, &termattr);
00046   if (i < 0) 
00047   {
00048     printf("tcsetattr() returned %d for fildes=%d\n",i,STDIN_FILENO); 
00049     perror("");
00050     return -1;
00051   }
00052    
00053   ttystate = RAW;
00054   ttysavefd = STDIN_FILENO;
00055 
00056   return 0;
00057 }
00058 
00059 /* ***************************************************************************
00060  *
00061  * set_tty_cbreak(), put the user's TTY in cbreak mode.
00062  * returns 0 on success, -1 on failure.
00063  *
00064  *************************************************************************** */
00065 int 
00066 set_tty_cbreak() 
00067 {
00068   int i;
00069 
00070   i = tcgetattr (STDIN_FILENO, &termattr);
00071   if (i < 0) 
00072   {
00073     printf("tcgetattr() returned %d for fildes=%d\n",i,STDIN_FILENO); 
00074     perror ("");
00075     return -1;
00076   }
00077 
00078   save_termattr = termattr;
00079 
00080   termattr.c_lflag &= ~(ECHO | ICANON);
00081   termattr.c_cc[VMIN] = 1;
00082   termattr.c_cc[VTIME] = 0;
00083       
00084   i = tcsetattr (STDIN_FILENO, TCSANOW, &termattr);
00085   if (i < 0) 
00086   {
00087     printf("tcsetattr() returned %d for fildes=%d\n",i,STDIN_FILENO); 
00088     perror ("");
00089     return -1;
00090   }
00091   ttystate = CBREAK;
00092   ttysavefd = STDIN_FILENO;
00093 
00094   return 0;
00095 }
00096 
00097 /* ***************************************************************************
00098  *
00099  * set_tty_cooked(), restore normal TTY mode. Very important to call
00100  *   the function before exiting else the TTY won't be too usable.
00101  * returns 0 on success, -1 on failure.
00102  *
00103  *************************************************************************** */
00104 int
00105 set_tty_cooked() 
00106 {
00107   int i;
00108   if (ttystate != CBREAK && ttystate != RAW) 
00109   {
00110     return 0;
00111   }
00112   i = tcsetattr (STDIN_FILENO, TCSAFLUSH, &save_termattr);
00113   if (i < 0) 
00114   {
00115     return -1;
00116   }
00117   ttystate = RESET;
00118   return 0;
00119 }
00120 
00121 /* ***************************************************************************
00122  *
00123  * kb_getc(), if there's a typed character waiting to be read,
00124  *   return it; else return 0.
00125  * 10-sep-2006: kb_getc() fails (it hangs on the read() and never returns
00126  * until a char is typed) under some Unix/Linux versions: ubuntu, suse, and
00127  * maybe others. To make it work, please uncomment two source lines below.
00128  *
00129  *************************************************************************** */
00130 unsigned char
00131 kb_getc(void) 
00132 {
00133   int i;
00134   unsigned char ch;
00135   ssize_t size;
00136   termattr.c_cc[VMIN] = 0; /* uncomment if needed */
00137   i = tcsetattr (STDIN_FILENO, TCSANOW, &termattr);
00138   size = read (STDIN_FILENO, &ch, 1);
00139   termattr.c_cc[VMIN] = 1; /* uncomment if needed */
00140   i = tcsetattr (STDIN_FILENO, TCSANOW, &termattr);
00141   if (size == 0)
00142   {
00143     return 0;
00144   }
00145   else
00146   {
00147     return ch;
00148   }
00149 }
00150 
00151 /* ***************************************************************************
00152  *
00153  * kb_getc_w(), wait for a character to be typed and return it.
00154  *
00155  *************************************************************************** */
00156 unsigned char
00157 kb_getc_w(void) 
00158 {
00159   unsigned char ch;
00160   size_t size;
00161 
00162   while (1)
00163   {
00164 
00165     usleep(20000);        /* 1/50th second: thanks, Floyd! */
00166 
00167     size = read (STDIN_FILENO, &ch, 1);
00168     if (size > 0)
00169     {
00170       break;
00171     }
00172   }
00173   return ch;
00174 }
00175 
00176 
00177 #endif // _WIN32
00178 


hrl_lib
Author(s): Cressel Anderson, Travis Deyle, Advait Jain, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:06