-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cxx
211 lines (181 loc) · 6.27 KB
/
main.cxx
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <csignal>
#include <exception>
#include <filesystem>
#include <fmt/core.h>
#include <functional>
#include <spdlog/spdlog.h>
#include <string_view>
#include <vector>
import foresight.keyboard;
import foresight.intercept;
struct options {
// NOLINTBEGIN(*-non-private-member-variables-in-classes)
enum struct action_type : std::uint8_t {
none = 0,
help,
intercept,
} action = action_type::none;
/// intercept file
std::vector<std::filesystem::path> files;
bool grab = false;
// NOLINTEND(*-non-private-member-variables-in-classes)
void set_action(action_type const inp_action) {
if (action == inp_action) {
return;
}
if (action != action_type::none) {
throw std::invalid_argument(fmt::format("Invalid argument syntax, two actions provided."));
}
action = inp_action;
}
};
void print_help() {
fmt::println("{}", R"TEXT(Usage: foresight [options] [action]
arguments:
-h | --help Print help.
actions:
intercept [files...] Intercept the files and print everything to stdout.
-g | --grab Grab the input.
Stops everyone else from using the input.
Only use this if you know what you're doing!
redirect [files...] Redirect stdin to the specified files.
help Print help.
Example Usages:
$ keyboard=/dev/input/event1
$ foresight intercept $keyboard | x2y | foresight redirect $keyboard
----------------------------- --- ----------------------------
| | |
| | |
| | |
| | |
`----> Intercept the input | `---> put the modified input back
/
/
/
-----------------------
/
$ cat x2y.c # you can do it with any programming language you like
#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
int main(void) {
setbuf(stdin, NULL); // disable stdin buffer
setbuf(stdout, NULL); // disable stdout buffer
struct input_event event;
// read from the input
while (fread(&event, sizeof(event), 1, stdin) == 1) {
// modify the input however you like
// here, we change "x" to "y"
if (event.type == EV_KEY && event.code == KEY_X)
event.code = KEY_Y;
// write it to stdout
fwrite(&event, sizeof(event), 1, stdout);
}
}
)TEXT");
}
options check_opts(int const argc, char const* const* argv) {
using enum options::action_type;
options opts{};
if (argc <= 1) {
return opts;
}
// NOLINTNEXTLINE(*-pro-bounds-pointer-arithmetic)
if (std::string_view const action_str{argv[1]}; action_str == "intercept") {
opts.set_action(intercept);
} else if (action_str == "help") {
opts.set_action(help);
}
for (std::size_t index = 2; index < argc; ++index) {
std::string_view const opt{argv[index]}; // NOLINT(*-pro-bounds-pointer-arithmetic)
if (opt == "--help" || opt == "-h") {
opts.set_action(help);
}
switch (opts.action) {
case intercept: {
if (opt == "--grab" || opt == "-g") {
opts.grab = true;
break;
}
opts.files.emplace_back(opt);
if (auto const status = std::filesystem::status(opts.files.back()); !exists(status)) {
throw std::invalid_argument(
fmt::format("File does not exist: {}", opts.files.back().string()));
} else if (!is_character_file(status)) {
throw std::invalid_argument(
fmt::format("It's not a file: {}", opts.files.back().string()));
}
break;
}
default: {
throw std::invalid_argument(fmt::format("Invalid argument {}", opt));
}
}
}
switch (opts.action) {
case intercept:
if (opts.files.empty()) {
throw std::invalid_argument("Please provide /dev/input/eventX file as an argument.");
}
break;
default: break;
}
return opts;
}
namespace {
// NOLINTBEGIN(*-avoid-non-const-global-variables)
std::sig_atomic_t volatile sig;
std::vector<std::function<void(std::sig_atomic_t)>> actions{};
// NOLINTEND(*-avoid-non-const-global-variables)
} // namespace
void handle_signals(int const signal) {
sig = signal;
for (auto const& func : actions) {
func(sig);
}
}
int run_action(options const& opts) {
using enum options::action_type;
switch (opts.action) {
case none:
case help: {
print_help();
return EXIT_FAILURE;
}
case intercept: {
interceptor inpor{opts.files};
inpor.set_output(stdout);
if (opts.grab) {
inpor.grab_input();
}
actions.emplace_back([&inpor](std::sig_atomic_t const sig) {
switch (sig) {
case SIGINT:
case SIGKILL:
case SIGTERM: inpor.stop(); break;
default: break;
}
});
return inpor.loop();
}
default: {
keyboard kbd;
return kbd.loop();
}
}
// std::unreachable();
}
int main(int const argc, char const* const* argv) try
{
std::ignore = std::signal(SIGINT, handle_signals);
std::ignore = std::signal(SIGTERM, handle_signals);
std::ignore = std::signal(SIGKILL, handle_signals);
auto const opts = check_opts(argc, argv);
return run_action(opts);
} catch (std::invalid_argument const& err) {
fmt::println(stderr, "{}", err.what());
} catch (std::exception const& err) {
spdlog::critical("Fatal exception: {}", err.what());
} catch (...) {
spdlog::critical("Fatal unknown exception.");
}