diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-29 20:17:23 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-29 20:17:23 +0200 |
commit | d02d46d27982a8e351736067ab9787f87052b989 (patch) | |
tree | 540ddb694585ea4b6662101e358a94c81c509ed3 /src/engine/graphics/scene.cpp | |
parent | d2a76bd8cfd883070259bf572a9f723a37c0d96e (diff) |
refactor: add termios abstraction
Diffstat (limited to 'src/engine/graphics/scene.cpp')
-rw-r--r-- | src/engine/graphics/scene.cpp | 35 |
1 files changed, 13 insertions, 22 deletions
diff --git a/src/engine/graphics/scene.cpp b/src/engine/graphics/scene.cpp index 7c23c7d..8ec8ddf 100644 --- a/src/engine/graphics/scene.cpp +++ b/src/engine/graphics/scene.cpp @@ -1,13 +1,13 @@ #include "scene.hpp" +#include <cstdint> #include <fmt/core.h> +#include <iostream> #include <sys/ioctl.h> -#include <termios.h> #include <unistd.h> -#include <cstdint> -#include <iostream> #include "engine/escape.hpp" +#include "engine/io/terminal.hpp" class IComponent; @@ -19,7 +19,7 @@ Scene::Scene(const IMatrixFactory<MatrixElement> &matrix_factory) noexcept void Scene::enter() noexcept { - if (_is_shown || _original_termios != nullptr) + if (_is_shown) { return; } @@ -28,30 +28,22 @@ void Scene::enter() noexcept fmt::print(ENABLE_ALT_BUFFER, fmt::arg("esc", ESC)); std::cout.flush(); - // Create a backup of the current terminal state - _original_termios = std::make_shared<termios>(); - tcgetattr(STDIN_FILENO, _original_termios.get()); + _original_terminal_state = get_terminal_state(STDIN_FILENO); - auto new_termios = termios(*_original_termios); + auto new_terminal_state = TerminalState(_original_terminal_state); - // Set the local mode flags of the new termios structure - // - // The following flags are disabled: - // ECHO - Echoing input characters - // ICANON - Canonical mode (line by line input) - // ISIG - Generate the corresponding signals for the characters - // INTR, QUIT, SUSP and DSUSP - new_termios.c_lflag &= static_cast<std::uint32_t>(~(ECHO | ICANON | ISIG)); + new_terminal_state.set_local_mode_flag(TerminalLocalModeFlag::echo, false); + new_terminal_state.set_local_mode_flag(TerminalLocalModeFlag::icanon, false); + new_terminal_state.set_local_mode_flag(TerminalLocalModeFlag::isig, false); - // Set a new terminal state - tcsetattr(STDIN_FILENO, TCSAFLUSH, &new_termios); + set_terminal_state(STDIN_FILENO, new_terminal_state); _is_shown = true; } void Scene::leave() noexcept { - if (!_is_shown || _original_termios == nullptr) + if (!_is_shown) { return; } @@ -60,10 +52,9 @@ void Scene::leave() noexcept fmt::print(DISABLE_ALT_BUFFER, fmt::arg("esc", ESC)); std::cout.flush(); - // Restore the original terminal state - tcsetattr(STDIN_FILENO, TCSAFLUSH, _original_termios.get()); + set_terminal_state(STDIN_FILENO, _original_terminal_state); - _original_termios = nullptr; + _original_terminal_state = {}; _is_shown = false; } |