Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #if defined(_WIN32)
00009 #include <conio.h>
00010 #elif defined(_XBOX) || defined(X360)
00011 #elif !defined(_PS3) && !defined(__PS3__) && !defined(SN_TARGET_PS3)
00012 #include <sys/time.h>
00013
00014 #include <termios.h>
00015 #include <stdlib.h>
00016 #include <unistd.h>
00017 #include <stdio.h>
00018 #include <string.h>
00019
00020 static struct termios g_old_kbd_mode;
00021
00022
00023 static void cooked(void)
00024 {
00025 tcsetattr(0, TCSANOW, &g_old_kbd_mode);
00026 }
00027
00028
00029 static void raw(void)
00030 {
00031 static char init;
00032
00033 struct termios new_kbd_mode;
00034
00035 if(init)
00036 return;
00037
00038 tcgetattr(0, &g_old_kbd_mode);
00039 memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios));
00040 new_kbd_mode.c_lflag &= ~(ICANON | ECHO);
00041 new_kbd_mode.c_cc[VTIME] = 0;
00042 new_kbd_mode.c_cc[VMIN] = 1;
00043 tcsetattr(0, TCSANOW, &new_kbd_mode);
00044
00045 atexit(cooked);
00046
00047 init = 1;
00048 }
00049
00050
00051 static int kbhit(void)
00052 {
00053 struct timeval timeout;
00054 fd_set read_handles;
00055 int status;
00056
00057 raw();
00058
00059 FD_ZERO(&read_handles);
00060 FD_SET(0, &read_handles);
00061 timeout.tv_sec = timeout.tv_usec = 0;
00062 status = select(0 + 1, &read_handles, NULL, NULL, &timeout);
00063 if(status < 0)
00064 {
00065 printf("select() failed in kbhit()\n");
00066 exit(1);
00067 }
00068 return status;
00069 }
00070
00071
00072 static int getch(void)
00073 {
00074 unsigned char temp;
00075
00076 raw();
00077
00078 if(read(0, &temp, 1) != 1)
00079 return 0;
00080 return temp;
00081 }
00082 #endif
00083
00084