#include "input.hpp" #include void InputHandler::listen() const { char character = 0; while (read(STDIN_FILENO, &character, 1) == 1) { notify(character); } } void InputHandler::attach(const char &event, Callback callback) { if (_key_observers.count(event) == 0) { _key_observers[event] = std::vector(); } _key_observers[event].push_back(callback); } void InputHandler::notify(const char &event) const { if (_key_observers.count(event) == 0) { return; } for (const auto &observer : _key_observers.at(event)) { observer(); } } void InputHandler::enter_raw_mode() { 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() { if (_original_termios == nullptr) { return; } tcsetattr(STDIN_FILENO, TCSAFLUSH, _original_termios.get()); _original_termios = nullptr; }