Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
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 */
20namespace JSYSTEM {}
21namespace JPP { using namespace JSYSTEM; }
22
23namespace 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
108 };
109}
110
111#endif
Auxiliary class for method select.
void set(const int file_descriptor)
Set file descriptor.
bool in_avail(JTimeval timeout=JTimeval::min())
Check availability of input.
Auxiliary class for time values.
Definition JTimeval.hh:29
Enable unbuffered terminal input.
Definition JKeypress.hh:32
JKeypress & operator=(JKeypress &&)
bool timeout(JTimeval timeout)
Timeout method.
Definition JKeypress.hh:92
~JKeypress()
Destructor.
Definition JKeypress.hh:62
char get()
Get single character.
Definition JKeypress.hh:74
JKeypress(JKeypress &&)
JKeypress(const bool echo=true)
Constructor.
Definition JKeypress.hh:42
JFileDescriptorMask mask
Definition JKeypress.hh:102
JKeypress(const JKeypress &)
JKeypress & operator=(const JKeypress &)
bool read(Vec &v, std::istream &is)
Read a Vec(tor) from a stream.
Definition io_ascii.hh:142
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for operating system calls.