aboutsummaryrefslogtreecommitdiff
path: root/src/engine/io/terminal.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/io/terminal.cpp')
-rw-r--r--src/engine/io/terminal.cpp54
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);
+}