aboutsummaryrefslogtreecommitdiff
path: root/src/engine/io/terminal.cpp
blob: e6d7d3ac2d5c9f214e3e77f345aa5424a5374431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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);
}