#include "input.hpp" #include void InputHandler::listen() const noexcept { char character = 0; while (read(STDIN_FILENO, &character, 1) == 1) { notify(character); } } void InputHandler::attach(const char &key, const std::shared_ptr &command) noexcept { auto key_index = _key_as_index(key); _key_commands.at(key_index).push_back(command); } void InputHandler::notify(const char &key) const noexcept { auto key_index = _key_as_index(key); for (const auto &command : _key_commands.at(key_index)) { command->execute(); } } 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; } InputHandler::_KeyCommandsSizeType InputHandler::_key_as_index(const char &key) noexcept { return static_cast<_KeyCommandsSizeType>(static_cast(key)); }