Pol  Revision:4b29d2b
kbhit.cpp
Go to the documentation of this file.
1 // History
2 // 2006/10/07 Shinigami: FreeBSD fix - changed __linux__ to __unix__
3 
4 // this file only used in Linux build
5 #ifdef LINUX
6 
7 #include "kbhit.h"
8 #include <unistd.h> // read()
9 namespace Pol
10 {
11 namespace Clib
12 {
14 {
15  tcgetattr( 0, &initial_settings );
16  new_settings = initial_settings;
17  new_settings.c_lflag &= ~ICANON;
18  new_settings.c_lflag &= ~ECHO;
19  new_settings.c_lflag |= ISIG; // enable signals, like ctrl-c
20  new_settings.c_cc[VMIN] = 1;
21  new_settings.c_cc[VTIME] = 0;
22  tcsetattr( 0, TCSANOW, &new_settings );
23  peek_character = -1;
24 }
25 
27 {
28  tcsetattr( 0, TCSANOW, &initial_settings );
29 }
30 
32 {
33  if ( peek_character != -1 )
34  return 1;
35 
36  new_settings.c_cc[VMIN] = 0;
37  tcsetattr( 0, TCSANOW, &new_settings );
38  unsigned char ch;
39  int nread = read( 0, &ch, 1 );
40  new_settings.c_cc[VMIN] = 1;
41  tcsetattr( 0, TCSANOW, &new_settings );
42 
43  if ( nread == 1 )
44  {
45  peek_character = ch;
46  return 1;
47  }
48  return 0;
49 }
50 
52 {
53  char ch;
54 
55  if ( peek_character != -1 )
56  {
57  ch = peek_character;
58  peek_character = -1;
59  }
60  else
61  {
62  int numread = read( 0, &ch, 1 );
63  if ( numread != 1 )
64  throw;
65  }
66 
67  return ch;
68 }
69 }
70 }
71 
72 #endif
struct termios initial_settings new_settings
Definition: kbhit.h:21
Definition: berror.cpp:12