From d02d46d27982a8e351736067ab9787f87052b989 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 29 Jun 2022 20:17:23 +0200 Subject: refactor: add termios abstraction --- src/engine/graphics/scene.cpp | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) (limited to 'src/engine/graphics/scene.cpp') 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 #include +#include #include -#include #include -#include -#include #include "engine/escape.hpp" +#include "engine/io/terminal.hpp" class IComponent; @@ -19,7 +19,7 @@ Scene::Scene(const IMatrixFactory &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(); - 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(~(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; } -- cgit v1.2.3-18-g5258