00001 // kbhit.h 00002 00003 #ifdef _WIN32 00004 #else 00005 00006 #ifndef KBHIT_H_ 00007 #define KBHIT_H_ 00008 00009 00010 // non-blocking keyboard i/o for linux 00011 // on win32 use kbhit() and read() 00012 00013 00014 /* *************************************************************************** 00015 * 00016 * Copyright 1992-2005 by Pete Wilson All Rights Reserved 00017 * 50 Staples Street : Lowell Massachusetts 01851 : USA 00018 * http://www.pwilson.net/ pete at pwilson dot net +1 978-454-4547 00019 * 00020 * This item is free software: you can redistribute it and/or modify it as 00021 * long as you preserve this copyright notice. Pete Wilson prepared this item 00022 * hoping it might be useful, but it has NO WARRANTY WHATEVER, not even any 00023 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00024 * 00025 *************************************************************************** */ 00026 00027 /* *************************************************************************** 00028 * 00029 * KBHIT.C 00030 * 00031 * Based on the work of W. Richard Stevens in "Advanced Programming in 00032 * the Unix Environment," Addison-Wesley; and of Floyd Davidson. 00033 * 00034 * Contains these functions: 00035 * 00036 * To set the TTY mode: 00037 * tty_set_raw() Unix setup to read a character at a time. 00038 * tty_set_cooked() Unix setup to reverse tty_set_raw() 00039 * 00040 * To read keyboard input: 00041 * kb_getc() keyboard get character, NON-BLOCKING. If a char 00042 * has been typed, return it. Else return 0. 00043 * kb_getc_w() kb get char with wait: BLOCKING. Wait for a char 00044 * to be typed and return it. 00045 * 00046 * How to use: 00047 * tty_set_raw() set the TTY mode to read one char at a time. 00048 * kb_getc() read chars one by one. 00049 * tty_set_cooked() VERY IMPORTANT: restore cooked mode when done. 00050 * 00051 * Revision History: 00052 * 00053 * DATE DESCRIPTION 00054 * ----------- -------------------------------------------- 00055 * 12-jan-2002 new 00056 * 20-aug-2002 cleanup 00057 * 24-nov-2003 Fixed kb_getc() so that it really is non blocking(JH) 00058 * 10-sep-2006 Let kb_getc() work right under certain Unix/Linux flavors 00059 * 00060 *************************************************************************** */ 00061 00062 #ifdef __cplusplus 00063 extern "C" { 00064 #endif 00065 00066 #include <stdio.h> 00067 #include <stdlib.h> 00068 #include <string.h> 00069 #include <termios.h> 00070 #include <unistd.h> 00071 #include <errno.h> 00072 00073 #ifndef STDIN_FILENO 00074 #define STDIN_FILENO 0 00075 #endif 00076 00077 extern int errno; 00078 00079 00080 00081 /* *************************************************************************** 00082 * 00083 * set_tty_raw(), put the user's TTY in one-character-at-a-time mode. 00084 * returns 0 on success, -1 on failure. 00085 * 00086 *************************************************************************** */ 00087 int 00088 set_tty_raw(void); 00089 00090 /* *************************************************************************** 00091 * 00092 * set_tty_cbreak(), put the user's TTY in cbreak mode. 00093 * returns 0 on success, -1 on failure. 00094 * 00095 *************************************************************************** */ 00096 int 00097 set_tty_cbreak(); 00098 00099 /* *************************************************************************** 00100 * 00101 * set_tty_cooked(), restore normal TTY mode. Very important to call 00102 * the function before exiting else the TTY won't be too usable. 00103 * returns 0 on success, -1 on failure. 00104 * 00105 *************************************************************************** */ 00106 int 00107 set_tty_cooked(); 00108 00109 /* *************************************************************************** 00110 * 00111 * kb_getc(), if there's a typed character waiting to be read, 00112 * return it; else return 0. 00113 * 10-sep-2006: kb_getc() fails (it hangs on the read() and never returns 00114 * until a char is typed) under some Unix/Linux versions: ubuntu, suse, and 00115 * maybe others. To make it work, please uncomment two source lines below. 00116 * 00117 *************************************************************************** */ 00118 unsigned char 00119 kb_getc(void); 00120 00121 /* *************************************************************************** 00122 * 00123 * kb_getc_w(), wait for a character to be typed and return it. 00124 * 00125 *************************************************************************** */ 00126 unsigned char 00127 kb_getc_w(void); 00128 00129 00130 #define TEST 00131 #ifdef TEST 00132 00133 void echo(unsigned char ch); 00134 00135 //static enum 00136 //{ 00137 // CH_ONLY, CH_HEX 00138 //} how_echo = CH_ONLY; 00139 00140 00141 00142 void 00143 echo(unsigned char ch); 00144 00145 #endif /* test */ 00146 00147 00148 #ifdef __cplusplus 00149 } 00150 #endif 00151 00152 00153 00154 00155 #endif /*KBHIT_H_*/ 00156 00157 #endif // _WIN32 00158