Jpp  pmt_effective_area_update
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JKeypress.hh
Go to the documentation of this file.
1 #ifndef __JSYSTEM__JKEYPRESS__
2 #define __JSYSTEM__JKEYPRESS__
3 
4 #include <termios.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <istream>
9 #include <ostream>
10 
12 #include "JLang/JTimeval.hh"
13 
14 
15 /**
16  * \file
17  * Keyboard settings for unbuffered input.
18  * \author mdejong
19  */
20 namespace JSYSTEM {}
21 namespace JPP { using namespace JSYSTEM; }
22 
23 namespace JSYSTEM {
24 
26  using JLANG::JTimeval;
27 
28 
29  /**
30  * Enable unbuffered terminal input.
31  */
32  class JKeypress {
33  public:
34  /**
35  * Constructor.
36  *
37  * The settings are modified by the constructor and
38  * the original settings are reset by the destructor.
39  *
40  * \param echo enable/disable echo of input character
41  */
42  JKeypress(const bool echo = true)
43  {
44  tcgetattr(STDIN_FILENO, &termcap);
45 
46  termios buffer = termcap;
47 
48  // Disable canonical mode, and set buffer size to 1 byte.
49 
50  buffer.c_lflag &= ~ICANON;
51  buffer.c_lflag &= (echo ? ECHO : ~ECHO);
52  buffer.c_cc[VTIME] = 0;
53  buffer.c_cc[VMIN] = 1;
54 
55  tcsetattr(STDIN_FILENO, TCSANOW, &buffer);
56  }
57 
58 
59  /**
60  * Destructor.
61  */
63  {
64  tcsetattr(STDIN_FILENO, TCSANOW, &termcap);
65  }
66 
67 
68  /**
69  * Get single character.
70  * This method returns as soon as input from terminal is available.
71  *
72  * \return character
73  */
74  char get()
75  {
76  char c;
77 
78  ::read(STDIN_FILENO, &c, 1);
79 
80  return c;
81  }
82 
83 
84  /**
85  * Timeout method.
86  * This method returns immediatly if input from terminal is available,
87  * else after the specified timeout period.
88  *
89  * \param timeout timeout
90  * \return true if input available; else false
91  */
93  {
94  mask.set(STDIN_FILENO);
95 
96  return mask.in_avail(timeout);
97  }
98 
99 
100  private:
101  termios termcap;
103  };
104 }
105 
106 #endif
bool timeout(JTimeval timeout)
Timeout method.
Definition: JKeypress.hh:92
void set(const int file_descriptor)
Set file descriptor.
bool read(Vec &v, std::istream &is)
Read a Vec(tor) from a stream.
Definition: io_ascii.hh:141
~JKeypress()
Destructor.
Definition: JKeypress.hh:62
Auxiliary class for time values.
Definition: JTimeval.hh:26
then echo
JFileDescriptorMask mask
Definition: JKeypress.hh:102
Enable unbuffered terminal input.
Definition: JKeypress.hh:32
Auxiliary class for method select.
JKeypress(const bool echo=true)
Constructor.
Definition: JKeypress.hh:42
bool in_avail(JTimeval timeout=JTimeval::min())
Check availability of input.