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/io/terminal.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/engine/io/terminal.cpp (limited to 'src/engine/io/terminal.cpp') diff --git a/src/engine/io/terminal.cpp b/src/engine/io/terminal.cpp new file mode 100644 index 0000000..e6d7d3a --- /dev/null +++ b/src/engine/io/terminal.cpp @@ -0,0 +1,54 @@ +#include "terminal.hpp" + +TerminalState::TerminalState(termios term_attrs) noexcept : _term_attrs(term_attrs) {} + +void TerminalState::set_input_mode_flag(TerminalInputModeFlag flag, bool is_on) noexcept +{ + auto flag_num = static_cast(flag); + + _term_attrs.c_iflag &= is_on ? flag_num : ~flag_num; +} + +void TerminalState::set_output_mode_flag(TerminalOutputModeFlag flag, bool is_on) noexcept +{ + auto flag_num = static_cast(flag); + + _term_attrs.c_oflag &= is_on ? flag_num : ~flag_num; +} + +void TerminalState::set_control_mode_flag( + TerminalControlModeFlag flag, + bool is_on) noexcept +{ + auto flag_num = static_cast(flag); + + _term_attrs.c_cflag &= is_on ? flag_num : ~flag_num; +} + +void TerminalState::set_local_mode_flag(TerminalLocalModeFlag flag, bool is_on) noexcept +{ + auto flag_num = static_cast(flag); + + _term_attrs.c_lflag &= is_on ? flag_num : ~flag_num; +} + +auto TerminalState::get_attributes() noexcept -> termios +{ + return _term_attrs; +} + +auto get_terminal_state(int term_fd) noexcept -> TerminalState +{ + termios term_attrs{}; + + tcgetattr(term_fd, &term_attrs); + + return TerminalState(term_attrs); +} + +void set_terminal_state(int term_fd, TerminalState state) noexcept +{ + auto state_attrs = state.get_attributes(); + + tcsetattr(term_fd, TCSAFLUSH, &state_attrs); +} -- cgit v1.2.3-18-g5258