#include "input.hpp" #include #include 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> &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(); } tcgetattr(STDIN_FILENO, _original_termios.get()); auto raw_termios = termios(*_original_termios); raw_termios.c_lflag &= static_cast(~(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(static_cast(event)); }