• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Kbhit.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 kbhit() and getch() for Linux/UNIX
00003 Chris Giese <geezer@execpc.com> http://my.execpc.com/~geezer
00004 Release date: ?
00005 This code is public domain (no copyright).
00006 You can do whatever you want with it.
00007 *****************************************************************************/
00008 #if defined(_WIN32)
00009 #include <conio.h> /* kbhit(), getch() */
00010 #elif defined(_XBOX) || defined(X360)
00011 #elif !defined(_PS3) && !defined(__PS3__) && !defined(SN_TARGET_PS3)
00012 #include <sys/time.h> /* struct timeval, select() */
00013 /* ICANON, ECHO, TCSANOW, struct termios */
00014 #include <termios.h> /* tcgetattr(), tcsetattr() */
00015 #include <stdlib.h> /* atexit(), exit() */
00016 #include <unistd.h> /* read() */
00017 #include <stdio.h> /* printf() */
00018 #include <string.h> /* memcpy */
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 /* put keyboard (stdin, actually) in raw, unbuffered mode */
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 /* when we exit, go back to normal, "cooked" mode */
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 /* check stdin (fd 0) for activity */
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 /* stdin = fd 0 */
00078         if(read(0, &temp, 1) != 1)
00079                 return 0;
00080         return temp;
00081 }
00082 #endif
00083 
00084 

Generated on Thu Sep 30 2010 01:27:24 for RakNet by  doxygen 1.7.1