diff options
author | HampusM <hampus@hampusmat.com> | 2022-02-28 21:30:30 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:54 +0200 |
commit | ede689d23e57c9b701ab19aa9112a0b2368865c9 (patch) | |
tree | e8c7afc7b2e5caf43b1b0c52356c0596021f25cb /src/engine/user/input.cpp | |
parent | f0883534b3303cf2b74f3ab51efe16615d2561a9 (diff) |
feat: add input handler & quitting with 'q'
Diffstat (limited to 'src/engine/user/input.cpp')
-rw-r--r-- | src/engine/user/input.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/engine/user/input.cpp b/src/engine/user/input.cpp new file mode 100644 index 0000000..e68f303 --- /dev/null +++ b/src/engine/user/input.cpp @@ -0,0 +1,64 @@ +#include "input.hpp" + +#include <unistd.h> + +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<Callback>(); + } + + _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<termios>(); + } + + tcgetattr(STDIN_FILENO, _original_termios.get()); + + auto raw_termios = termios(*_original_termios); + + raw_termios.c_lflag &= static_cast<unsigned int>(~(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; +} |