Jpp
Public Member Functions | Private Attributes | List of all members
JSYSTEM::JKeypress Class Reference

Enable unbuffered terminal input. More...

#include <JKeypress.hh>

Public Member Functions

 JKeypress (const bool echo=true)
 Constructor. More...
 
 ~JKeypress ()
 Destructor. More...
 
char get ()
 Get single character. More...
 
bool timeout (JTimeval timeout)
 Timeout method. More...
 

Private Attributes

termios termcap
 
JFileDescriptorMask mask
 

Detailed Description

Enable unbuffered terminal input.

Definition at line 32 of file JKeypress.hh.

Constructor & Destructor Documentation

◆ JKeypress()

JSYSTEM::JKeypress::JKeypress ( const bool  echo = true)
inline

Constructor.

The settings are modified by the constructor and the original settings are reset by the destructor.

Parameters
echoenable/disable echo of input character

Definition at line 42 of file JKeypress.hh.

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  }

◆ ~JKeypress()

JSYSTEM::JKeypress::~JKeypress ( )
inline

Destructor.

Definition at line 62 of file JKeypress.hh.

63  {
64  tcsetattr(STDIN_FILENO, TCSANOW, &termcap);
65  }

Member Function Documentation

◆ get()

char JSYSTEM::JKeypress::get ( )
inline

Get single character.

This method returns as soon as input from terminal is available.

Returns
character

Definition at line 74 of file JKeypress.hh.

75  {
76  char c;
77 
78  ::read(STDIN_FILENO, &c, 1);
79 
80  return c;
81  }

◆ timeout()

bool JSYSTEM::JKeypress::timeout ( JTimeval  timeout)
inline

Timeout method.

This method returns immediatly if input from terminal is available, else after the specified timeout period.

Parameters
timeouttimeout
Returns
true if input available; else false

Definition at line 92 of file JKeypress.hh.

93  {
94  mask.set(STDIN_FILENO);
95 
96  return mask.in_avail(timeout);
97  }

Member Data Documentation

◆ termcap

termios JSYSTEM::JKeypress::termcap
private

Definition at line 101 of file JKeypress.hh.

◆ mask

JFileDescriptorMask JSYSTEM::JKeypress::mask
private

Definition at line 102 of file JKeypress.hh.


The documentation for this class was generated from the following file:
JLANG::JFileDescriptorMask::in_avail
bool in_avail(JTimeval timeout=JTimeval::min())
Check availability of input.
Definition: JFileDescriptorMask.hh:264
JSYSTEM::JKeypress::mask
JFileDescriptorMask mask
Definition: JKeypress.hh:102
JLANG::JFileDescriptorMask::set
void set(const int file_descriptor)
Set file descriptor.
Definition: JFileDescriptorMask.hh:154
read
bool read(Vec &v, std::istream &is)
Read a Vec(tor) from a stream.
Definition: io_ascii.hh:141
JSYSTEM::JKeypress::termcap
termios termcap
Definition: JKeypress.hh:101
JSYSTEM::JKeypress::timeout
bool timeout(JTimeval timeout)
Timeout method.
Definition: JKeypress.hh:92