blob: d335fc9210f3de9e92e5c4b7cac62754ea48e30c (
plain)
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
|
#include "input.hpp"
#include <iostream>
#include <unistd.h>
void InputHandler::listen() const noexcept
{
char character = 0;
while (!std::cin.read(&character, 1).fail())
{
notify_subscribers(character, nullptr);
}
}
void InputHandler::subscribe(
const Event &event,
const std::shared_ptr<ISubscriber<Context>> &subscriber
) noexcept
{
auto event_index = _event_as_index(event);
_subscribers.at(event_index).push_back(subscriber);
}
void InputHandler::notify_subscribers(const Event &event, const Context &context)
const noexcept
{
auto event_index = _event_as_index(event);
for (const auto &subscriber : _subscribers.at(event_index))
{
subscriber->update(context);
}
}
void InputHandler::enter_raw_mode() noexcept
{
if (_original_termios == nullptr)
{
_original_termios = std::make_shared<termios>();
}
tcgetattr(STDIN_FILENO, _original_termios.get());
auto raw_termios = termios(*_original_termios);
raw_termios.c_lflag &= static_cast<uint32_t>(~(ECHO | ICANON | ISIG));
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_termios);
}
void InputHandler::leave_raw_mode() noexcept
{
if (_original_termios == nullptr)
{
return;
}
tcsetattr(STDIN_FILENO, TCSAFLUSH, _original_termios.get());
_original_termios = nullptr;
}
auto InputHandler::_event_as_index(const Event &event) noexcept
-> InputHandler::SubscribersSizeType
{
return static_cast<SubscribersSizeType>(static_cast<uint8_t>(event));
}
|