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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
module;
#include <charconv>
#include <concepts>
#include <cstdint>
#include <utility>
#include <variant>
export module ttwhy.terminal:events;
import ttwhy.core;
export namespace ttwhy::terminal
{
//! Represents standard C0 terminal control keys.
enum struct control_key : std::uint8_t
{
enter,
escape,
tab,
backspace,
};
//! Represents terminal cursor and keypad navigation keys.
enum struct navigation_key : std::uint8_t
{
up,
down,
left,
right,
home,
end,
delete_key,
insert_key,
page_up,
page_down,
};
//! Represents terminal function keys (F1 through F20).
enum struct function_key : std::uint8_t
{
f1,
f2,
f3,
f4,
f5,
f6,
f7,
f8,
f9,
f10,
f11,
f12,
f13,
f14,
f15,
f16,
f17,
f18,
f19,
f20,
};
//! Represents individual keyboard modifier keys.
enum struct modifier : std::uint8_t
{
none = 0,
shift = 1,
alt = 2,
ctrl = 4,
meta = 8,
};
//! Represents a bitmask combination of keyboard modifiers.
enum struct modifier_set : std::uint8_t
{
};
//! Combine two keyboard modifiers into a modifier set.
//!
//! @param lhs The first modifier to combine.
//! @param rhs The second modifier to combine.
//! @return A modifier set containing both modifiers.
constexpr auto operator|(modifier lhs, modifier rhs) -> modifier_set
{
return static_cast<modifier_set>(std::to_underlying(lhs) | std::to_underlying(rhs));
}
//! Add a keyboard modifier to an existing modifier set.
//!
//! @param lhs The existing modifier set.
//! @param rhs The modifier to add.
//! @return The resulting modifier set.
constexpr auto operator|(modifier_set lhs, modifier rhs) -> modifier_set
{
return static_cast<modifier_set>(std::to_underlying(lhs) | std::to_underlying(rhs));
}
//! Compute the bitwise intersection of two keyboard modifiers.
//!
//! @param lhs The first modifier.
//! @param rhs The second modifier.
//! @return The intersection as a modifier set.
constexpr auto operator&(modifier lhs, modifier rhs) -> modifier_set
{
return static_cast<modifier_set>(std::to_underlying(lhs) & std::to_underlying(rhs));
}
//! Check for the presence of a keyboard modifier in a modifier set.
//!
//! @param lhs The modifier set.
//! @param rhs The modifier to test.
//! @return The intersection as a modifier set.
constexpr auto operator&(modifier_set lhs, modifier rhs) -> modifier_set
{
return static_cast<modifier_set>(std::to_underlying(lhs) & std::to_underlying(rhs));
}
//! Check whether all specified modifiers are present in a modifier set.
//!
//! @tparam Modifier The modifier types to check.
//! @param mask The modifier set to inspect.
//! @param flag The modifier flags that must all be present.
//! @return True if all specified flags are present, false otherwise.
template<std::same_as<modifier>... Modifier>
constexpr auto has_all(modifier_set mask, Modifier... flag) -> bool
{
return (true && ... && (mask & flag));
}
//! Check whether any of the specified modifiers are present in a modifier set.
//!
//! @tparam Modifier The modifier types to check.
//! @param mask The modifier set to inspect.
//! @param flag The modifier flags to test for.
//! @return True if at least one of the specified flags is present, false otherwise.
template<std::same_as<modifier>... Modifier>
constexpr auto has_any(modifier_set mask, Modifier... flag) -> bool
{
return (false || ... || (mask & flag));
}
//! Represents a printable ASCII or raw character event.
struct character_event
{
char value{};
};
//! Represents a terminal control key event.
struct control_event
{
control_key key{};
};
//! Represents a terminal cursor or keypad navigation event.
struct navigation_event
{
navigation_key key{};
modifier_set modifiers{};
};
//! Represents a Control-key chord event.
struct ctrl_chord_event
{
char key{};
};
//! Represents a terminal function key event.
struct function_key_event
{
function_key key{};
modifier_set modifiers{};
};
//! Variant representing any input event produced by a terminal scanner.
using input_event =
std::variant<character_event, control_event, navigation_event, ctrl_chord_event, function_key_event>;
//! Validates that a sink can receive all terminal input event types.
//!
//! @tparam Sink The sink callable type to validate.
template<typename Sink>
concept event_sink = ttwhy::event_sink<Sink, character_event> && ttwhy::event_sink<Sink, control_event> &&
ttwhy::event_sink<Sink, navigation_event> && ttwhy::event_sink<Sink, ctrl_chord_event> &&
ttwhy::event_sink<Sink, function_key_event>;
} // namespace ttwhy::terminal
|