-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecon.c
More file actions
314 lines (272 loc) · 6.82 KB
/
recon.c
File metadata and controls
314 lines (272 loc) · 6.82 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#define _GNU_SOURCE // NOLINT(*-reserved-identifier)
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <linux/input.h>
#include <sys/wait.h>
#include "key_seq.h"
#include "screen.h"
#include "terminal.h"
const char *dev_fb = "/dev/fb1";
const char *sys_backlight = "/sys/class/backlight/backlight_pwm0";
const char *screenshot_filename = "/tmp/reccon.bmp";
static int test_bit(const int nr, const unsigned long *addr) {
return (addr[nr / (8 * sizeof(unsigned long))] & 1 << (nr % (8 * sizeof(unsigned long)))) != 0;
}
#define NBITS(x) (((x) + 8 * sizeof(unsigned long) - 1) / (8 * sizeof(unsigned long)))
int find_keyboard_device(char *buffer, const size_t buffer_size) {
DIR *dir = opendir("/dev/input");
if (!dir) {
perror("opendir");
return -1;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strncmp(entry->d_name, "event", 5) != 0) {
continue;
}
snprintf(buffer, buffer_size, "/dev/input/%s", entry->d_name);
const int fd = open(buffer, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
continue;
}
// Check if device is a keyboard (EV_KEY and KEY_A)
unsigned long evbit[NBITS(KEY_MAX)] = {0};
if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(evbit)), evbit) < 0) {
close(fd);
continue;
}
if (test_bit(KEY_A, evbit)) {
close(fd);
closedir(dir);
return 0;
}
close(fd);
}
closedir(dir);
return -1;
}
struct keyboard_thread_args {
int input_fd;
int master_fd;
enum keypad *keypad;
};
// ReSharper disable once CppDFAConstantFunctionResult
void *keyboard_thread(void *arg) {
const struct keyboard_thread_args args = *(struct keyboard_thread_args *)arg;
int shift_held = 0, ctrl_held = 0;
for (;;) {
struct input_event ev;
int n;
while ((n = read(args.input_fd, &ev, sizeof(ev))) == sizeof(ev)) {
if (ev.type == EV_KEY) {
if (ev.value == 1) {
// Key press
if (ev.code == KEY_LEFTCTRL || ev.code == KEY_RIGHTCTRL) {
ctrl_held = 1;
}
else if (ev.code == KEY_LEFTSHIFT || ev.code == KEY_RIGHTSHIFT) {
shift_held = 1;
}
else {
const char *seq = key_to_seq(ev.code, shift_held, ctrl_held, *args.keypad);
if (seq) {
n = (int)strlen(seq);
if (write(args.master_fd, seq, n) < n) {
perror("write sequence");
return NULL;
}
}
}
}
else if (ev.value == 0) {
// Key release
if (ev.code == KEY_LEFTCTRL || ev.code == KEY_RIGHTCTRL) {
ctrl_held = 0;
}
else if (ev.code == KEY_LEFTSHIFT || ev.code == KEY_RIGHTSHIFT) {
shift_held = 0;
}
}
// NOTE: ignored key repeat ev.value == 2
}
}
if (n < 0 && errno != EAGAIN) {
perror("read input");
break;
}
}
return NULL;
}
#ifdef WITH_STDIN
struct stdin_thread_args {
int input_fd;
int master_fd;
};
// ReSharper disable once CppDFAConstantFunctionResult
void *stdin_thread(void *arg) {
const struct stdin_thread_args args = *(struct stdin_thread_args *)arg;
for (;;) {
unsigned char seq[1024];
int n;
while ((n = read(args.input_fd, seq, sizeof(seq))) > 0) {
if (write(args.master_fd, seq, n) < n) {
perror("write sequence");
return NULL;
}
}
if (n < 0 && errno != EAGAIN) {
perror("read std input");
break;
}
}
return NULL;
}
#endif
terminal_t terminal;
static void sigusr1_handler(int) {
if (terminal.screen) {
if (0 == screen_save_bmp(terminal.screen, screenshot_filename)) {
printf("screenshot saved to %s\n", screenshot_filename);
}
else {
perror("save screenshot");
}
}
}
int main(const int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <program> [args...]\n", argv[0]);
return 1;
}
char dev_input[256];
if (find_keyboard_device(dev_input, sizeof(dev_input)) < 0) {
fprintf(stderr, "reccon: no keyboard on startup\n");
return 1;
}
// Open input
const int input_fd = open(dev_input, O_RDONLY);
if (input_fd < 0) {
perror("open input");
return 1;
}
// Open framebuffer
screen_t screen;
if (screen_open(&screen, dev_fb, sys_backlight) < 0) {
close(input_fd);
return 1;
}
screen_clear(screen, 75);
// Create PTY
const int master_fd = posix_openpt(O_RDWR | O_NOCTTY);
if (master_fd < 0) {
close(input_fd);
close(screen.fd);
perror("posix_openpt");
return 1;
}
grantpt(master_fd);
unlockpt(master_fd);
const pid_t pid = fork();
if (pid < 0) {
close(input_fd);
close(screen.fd);
close(master_fd);
perror("fork");
return 1;
}
if (pid == 0) {
// close opened fds
close(input_fd);
close(screen.fd);
// becomes PTY slave process
const int slave_fd = open(ptsname(master_fd), O_RDWR);
close(master_fd);
if (slave_fd < 0) {
perror("open slave");
exit(1);
}
// Set terminal size (80x24 as default)
struct winsize ws = {
.ws_row = screen.rows,
.ws_col = screen.columns,
.ws_xpixel = 0,
.ws_ypixel = 0
};
ioctl(slave_fd, TIOCSWINSZ, &ws);
struct termios tios;
tcgetattr(slave_fd, &tios);
cfmakeraw(&tios); // disable all input processing
// Enable these flags to make Ctrl+C work
tios.c_lflag |= ISIG; // Enable signal generation
tios.c_lflag |= ECHO; // Enable echo (optional)
tios.c_cc[VINTR] = 0x03; // Set Ctrl+C as interrupt character
tcsetattr(slave_fd, TCSANOW, &tios);
setenv("TERM", "xterm", 1);
setenv("COLORTERM", "truecolor", 1);
// make slave stdin/stdout/stderr
dup2(slave_fd, STDIN_FILENO);
dup2(slave_fd, STDOUT_FILENO);
dup2(slave_fd, STDERR_FILENO);
if (slave_fd > 2) close(slave_fd);
setsid(); // new session
ioctl(STDIN_FILENO, TIOCSCTTY, 0); // Controlling tty
execvp(argv[1], &argv[1]);
perror("execvp");
exit(1);
}
terminal_init(&terminal, &screen, master_fd);
#ifdef DEBUG_DUMP
terminal.debug_fd = open(DEBUG_DUMP, O_CREAT | O_WRONLY, 0644);
#endif
pthread_t keyboard;
struct keyboard_thread_args args = {
.input_fd = input_fd,
.master_fd = master_fd,
.keypad = &terminal.keypad
};
pthread_create(&keyboard, nullptr, keyboard_thread, &args);
#ifdef WITH_STDIN
pthread_t input;
struct stdin_thread_args args2 = {
.input_fd = fileno(stdin),
.master_fd = master_fd,
};
pthread_create(&input, nullptr, stdin_thread, &args2);
#endif
signal(SIGUSR1, sigusr1_handler);
for (;;) {
// Read from PTY master
unsigned char buf[1024];
ssize_t n;
while ((n = read(master_fd, buf, sizeof(buf))) > 0) {
// echo to local console
// write(STDOUT_FILENO, buf, n);
terminal_feed(&terminal, buf, n);
screen_redraw(&screen);
}
if (errno != EAGAIN) {
fprintf(stderr, "Child exited\n");
break;
}
}
pthread_cancel(keyboard);
pthread_join(keyboard, nullptr);
#ifdef WITH_STDIN
pthread_cancel(input);
pthread_join(input, nullptr);
#endif
close(master_fd);
close(input_fd);
screen_clear(screen, 0);
screen_close(&screen);
wait(nullptr);
return 0;
}