00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00032 #include <conio.h>
00033
00034 int _kbhit()
00035 {
00036 static int initialized;
00037
00038 fd_set rfds;
00039 struct timeval tv;
00040 int retcode;
00041
00042 if(!initialized)
00043 {
00044 if(tcgetattr(STDIN_FILENO, &term_attribs) < 0)
00045 {
00046 perror("tcgetattr: ");
00047 exit(-1);
00048 }
00049
00050 term_attribs_old = term_attribs;
00051
00052 if(atexit(restore_term))
00053 {
00054 perror("atexit: ");
00055 exit(-1);
00056 }
00057
00058 term_attribs.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
00059 term_attribs.c_iflag &= ~(IXON | BRKINT | INPCK | ICRNL | ISTRIP);
00060 term_attribs.c_cflag &= ~(CSIZE | PARENB);
00061 term_attribs.c_cflag |= CS8;
00062 term_attribs.c_cc[VTIME] = 0;
00063 term_attribs.c_cc[VMIN] = 0;
00064
00065 if(tcsetattr(STDIN_FILENO, TCSANOW, &term_attribs) < 0)
00066 {
00067 perror("tcsetattr: ");
00068 exit(-1);
00069 }
00070
00071 initialized = 1;
00072 }
00073
00074 FD_ZERO(&rfds);
00075 FD_SET(STDIN_FILENO, &rfds);
00076 memset(&tv, 0, sizeof(tv));
00077
00078 retcode = select(1, &rfds, NULL, NULL, &tv);
00079 if(retcode == -1 && errno == EINTR)
00080 {
00081 return 0;
00082 }
00083 else if(retcode < 0)
00084 {
00085 perror("select: ");
00086 exit(-1);
00087 }
00088 else if(FD_ISSET(STDIN_FILENO, &rfds))
00089 {
00090 return retcode;
00091 }
00092
00093 return 0;
00094 }
00095
00096 int getch()
00097 {
00098 fd_set rfds;
00099 int retcode;
00100
00101 FD_ZERO(&rfds);
00102 FD_SET(STDIN_FILENO, &rfds);
00103
00104 retcode = select(1, &rfds, NULL, NULL, NULL);
00105 if(retcode == -1 && errno == EINTR)
00106 {
00107 return 0;
00108 }
00109 else if(retcode < 0)
00110 {
00111 perror("select: ");
00112 exit(-1);
00113 }
00114
00115 return getchar();
00116 }