-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathglobal.cpp
More file actions
87 lines (66 loc) · 1.57 KB
/
global.cpp
File metadata and controls
87 lines (66 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "global.h"
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <chrono>
#include <thread>
static termios origtio = {};
void resetTerminalMode()
{
if (origtio.c_lflag) {
tcsetattr(STDIN_FILENO, TCSANOW, &origtio);
}
}
void setTerminalMode(bool enableEcho)
{
if (isatty(STDIN_FILENO)) {
termios tio {};
if (tcgetattr(STDIN_FILENO, &tio) == 0) {
if (!origtio.c_lflag) {
origtio = tio;
}
tio.c_lflag &= ~ICANON; // disabled canonical mode
if (enableEcho) {
tio.c_lflag |= ECHO; // enabled echo
} else {
tio.c_lflag &= ~ECHO; // disabled echo
}
// read one char
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSANOW, &tio) != 0) {
//std::perror("tcsetattr");
}
}
}
}
QByteArray readStdInput()
{
const int fd = STDIN_FILENO;
QByteArray bytes;
char buf[1024];
pollfd pfd {
.fd = fd,
.events = POLLIN,
.revents = 0
};
while (true) {
int pret = epoll(&pfd, 1, 0);
if (pret <= 0) {
break;
}
if (!(pfd.revents & POLLIN)) {
break;
}
ssize_t n = eread(fd, buf, sizeof(buf));
if (n <= 0) {
break;
}
bytes.append(buf, n);
}
return bytes;
}
void Sleep(int msecs)
{
std::this_thread::sleep_for(std::chrono::milliseconds(msecs));
}