-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKeyboardReader.cpp
More file actions
51 lines (41 loc) · 1.12 KB
/
Copy pathKeyboardReader.cpp
File metadata and controls
51 lines (41 loc) · 1.12 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
#include "KeyboardReader.h"
#include "ClientContext.h"
#include <cerrno>
#include <iostream>
#include <string>
#include <unistd.h>
KeyboardReader::KeyboardReader(ClientContext* clientContext, const std::function<void(std::string)>& callBack)
: core::eventreceiver::ReadEventReceiver("KeyboardReader", 0)
, callBack(callBack)
, clientContext(clientContext) {
if (enable(STDIN_FILENO)) {
std::cout << "KeyboardReader enabled";
} else {
std::cout << "KeyboardReader not enabled";
}
}
KeyboardReader::~KeyboardReader() {
if (clientContext != nullptr) {
clientContext->keyboardReaderAway();
}
}
void KeyboardReader::readEvent() {
char buffer[256];
ssize_t ret = read(STDIN_FILENO, buffer, 256);
if (ret > 0) {
if (clientContext != nullptr) {
callBack(std::string(buffer, ret));
}
} else {
std::cout << "Nothing entered or error: " << errno;
}
}
void KeyboardReader::stop() {
disable();
}
void KeyboardReader::clientContextAway() {
clientContext = nullptr;
}
void KeyboardReader::unobservedEvent() {
delete this;
}