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/io/terminal.cpp | |
parent | d2a76bd8cfd883070259bf572a9f723a37c0d96e (diff) |
refactor: add termios abstraction
Diffstat (limited to 'src/engine/io/terminal.cpp')
-rw-r--r-- | src/engine/io/terminal.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
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<tcflag_t>(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<tcflag_t>(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<tcflag_t>(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<tcflag_t>(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); +} |