Repository navigation
Expand file tree
/
Copy pathemulator.cpp
More file actions
302 lines (270 loc) · 12.2 KB
/
Copy pathemulator.cpp
File metadata and controls
302 lines (270 loc) · 12.2 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <time.h>
#include <errno.h>
#include <stdint.h>
#include <signal.h>
#include <poll.h>
#include <stdbool.h>
// Keep file descriptors global so the asynchronous signal handler can clean them up
volatile sig_atomic_t keep_running = 1;
int global_keyboard_fd = -1;
int global_mouse_fd = -1;
int global_gamepad_fd = -1;
// The path to the keyboard and mouse (Update these to your event nodes)
char KEYBOARD_PATH[] = "/dev/input/event0";
char MOUSE_PATH[] = "/dev/input/event1";
bool q_pressed = false;
// Held state of the movement keys, so opposite keys cancel instead of the
// last event winning (releasing W while S is held must keep the stick down).
bool w_down = false, s_down = false, a_down = false, d_down = false;
// The mouse only reports motion, never "stopped", so the right stick is
// re-centred when no motion has arrived for this long.
const int MOUSE_IDLE_RECENTER_MS = 30;
bool right_stick_deflected = false;
long long last_mouse_motion_ms = 0;
static long long now_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
int MOUSE_SENSITIVITY = 299;
int MOUSE_SENSITIVITY_NEGATIVE = -299;
// FEATURE 2: Global Cleanup Routine Triggered by OS Signals
void cleanup_system(void)
{
printf("\n[!] Shutting down daemon. Cleaning kernel interfaces...\n");
if (global_keyboard_fd >= 0) close(global_keyboard_fd);
if (global_mouse_fd >= 0) close(global_mouse_fd);
if (global_gamepad_fd >= 0) {
// Destroy the virtual uinput device explicitly
ioctl(global_gamepad_fd, UI_DEV_DESTROY);
close(global_gamepad_fd);
}
}
void handle_signal(int sig)
{
keep_running = 0;
}
void send_sync_event(int gamepad_fd)
{
struct input_event sync_ev;
memset(&sync_ev, 0, sizeof(struct input_event));
sync_ev.type = EV_SYN;
sync_ev.code = SYN_REPORT;
sync_ev.value = 0;
if (write(gamepad_fd, &sync_ev, sizeof(struct input_event)) < 0) {
perror("Error writing sync event");
}
}
void emit_gamepad_event(int gamepad_fd, uint16_t type, uint16_t code, int32_t value)
{
struct input_event ev;
memset(&ev, 0, sizeof(struct input_event));
ev.type = type;
ev.code = code;
ev.value = value;
if (write(gamepad_fd, &ev, sizeof(struct input_event)) < 0) {
perror("Error writing event to virtual gamepad");
}
}
int main(int argc, char *argv[])
{
// FEATURE 2: Register POSIX Signal Traps
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handle_signal;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
// Register safe termination cleanup exit code hook
atexit(cleanup_system);
char keyboard_name[256] = "Unknown";
global_keyboard_fd = open(KEYBOARD_PATH, O_RDONLY | O_NONBLOCK);
if (global_keyboard_fd == -1) {
perror("Failed to open keyboard device");
exit(EXIT_FAILURE);
}
ioctl(global_keyboard_fd, EVIOCGNAME(sizeof(keyboard_name)), keyboard_name);
printf("[+] Reading Keyboard Input From: %s\n", keyboard_name);
char mouse_name[256] = "Unknown";
global_mouse_fd = open(MOUSE_PATH, O_RDONLY | O_NONBLOCK);
if (global_mouse_fd == -1) {
perror("Failed to open mouse device");
exit(EXIT_FAILURE);
}
ioctl(global_mouse_fd, EVIOCGNAME(sizeof(mouse_name)), mouse_name);
printf("[+] Reading Mouse Input From: %s\n", mouse_name);
// Open uinput control context interface
global_gamepad_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if (global_gamepad_fd < 0) {
perror("Failed to open /dev/uinput. Are you running as root?");
exit(EXIT_FAILURE);
}
// Configure Keys
ioctl(global_gamepad_fd, UI_SET_EVBIT, EV_KEY);
int keys_to_set[] = {BTN_A, BTN_B, BTN_X, BTN_Y, BTN_TL, BTN_TR, BTN_TL2, BTN_TR2,
BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR,
BTN_DPAD_UP, BTN_DPAD_DOWN, BTN_DPAD_LEFT, BTN_DPAD_RIGHT};
for (size_t i = 0; i < sizeof(keys_to_set)/sizeof(int); i++) {
ioctl(global_gamepad_fd, UI_SET_KEYBIT, keys_to_set[i]);
}
// Configure Absolute Joysticks
ioctl(global_gamepad_fd, UI_SET_EVBIT, EV_ABS);
ioctl(global_gamepad_fd, UI_SET_ABSBIT, ABS_X);
ioctl(global_gamepad_fd, UI_SET_ABSBIT, ABS_Y);
ioctl(global_gamepad_fd, UI_SET_ABSBIT, ABS_RX);
ioctl(global_gamepad_fd, UI_SET_ABSBIT, ABS_RY);
struct uinput_user_dev uidev;
memset(&uidev, 0, sizeof(uidev));
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "Virtual Xbox Gamepad Daemon");
uidev.id.bustype = BUS_USB;
uidev.id.vendor = 0x3;
uidev.id.product = 0x3;
uidev.id.version = 2;
uidev.absmax[ABS_X] = 32767; uidev.absmin[ABS_X] = -32768; uidev.absflat[ABS_X] = 15;
uidev.absmax[ABS_Y] = 32767; uidev.absmin[ABS_Y] = -32768; uidev.absflat[ABS_Y] = 15;
uidev.absmax[ABS_RX] = 512; uidev.absmin[ABS_RX] = -512; uidev.absflat[ABS_RX] = 16;
uidev.absmax[ABS_RY] = 512; uidev.absmin[ABS_RY] = -512; uidev.absflat[ABS_RY] = 16;
if (write(global_gamepad_fd, &uidev, sizeof(uidev)) < 0) {
perror("Failed to define layout constraints to uinput block");
exit(EXIT_FAILURE);
}
if (ioctl(global_gamepad_fd, UI_DEV_CREATE) < 0) {
perror("Failed to instantiate uinput subsystem virtual architecture");
exit(EXIT_FAILURE);
}
// FEATURE 1: Setup pollfd structure to multiplex monitoring across descriptors
struct pollfd fds[2];
fds[0].fd = global_keyboard_fd;
fds[0].events = POLLIN;
fds[1].fd = global_mouse_fd;
fds[1].events = POLLIN;
printf("[+] Gamepad emulation daemon listening. Press Ctrl+C or Q+Enter to stop.\n");
struct input_event ev;
while (keep_running) {
// Blocks thread explicitly until kernel updates target descriptors. -1 means no timeout
// Block forever when idle; only wake on a timer while the right stick
// is deflected, so it can be re-centred after the mouse stops.
int timeout_ms = -1;
if (right_stick_deflected) {
long long left = MOUSE_IDLE_RECENTER_MS - (now_ms() - last_mouse_motion_ms);
timeout_ms = left > 0 ? (int)left : 0;
}
int poll_ret = poll(fds, 2, timeout_ms);
if (poll_ret < 0) {
if (errno == EINTR) continue; // Woken up by system call signal return handle
perror("Poll system configuration crashed");
break;
}
if (right_stick_deflected &&
now_ms() - last_mouse_motion_ms >= MOUSE_IDLE_RECENTER_MS) {
emit_gamepad_event(global_gamepad_fd, EV_ABS, ABS_RX, 0);
emit_gamepad_event(global_gamepad_fd, EV_ABS, ABS_RY, 0);
send_sync_event(global_gamepad_fd);
right_stick_deflected = false;
}
// Processing Keyboard Event
if (fds[0].revents & POLLIN) {
while (read(global_keyboard_fd, &ev, sizeof(struct input_event)) > 0) {
if (ev.type == EV_KEY) {
if (ev.code == KEY_Q && ev.value == 1) {
q_pressed = !q_pressed;
}
if (ev.code == KEY_ENTER && ev.value == 1 && q_pressed) {
keep_running = 0;
break;
}
// Key mappings
if (ev.code == KEY_ENTER && ev.value != 2) {
emit_gamepad_event(global_gamepad_fd, EV_KEY, BTN_A, ev.value);
send_sync_event(global_gamepad_fd);
}
else if (ev.code == KEY_G && ev.value != 2) {
emit_gamepad_event(global_gamepad_fd, EV_KEY, BTN_Y, ev.value);
send_sync_event(global_gamepad_fd);
}
else if (ev.code == KEY_E && ev.value != 2) {
emit_gamepad_event(global_gamepad_fd, EV_KEY, BTN_X, ev.value);
send_sync_event(global_gamepad_fd);
}
else if (ev.code == KEY_SPACE && ev.value != 2) {
emit_gamepad_event(global_gamepad_fd, EV_KEY, BTN_A, ev.value);
send_sync_event(global_gamepad_fd);
}
else if (ev.code == KEY_W || ev.code == KEY_S ||
ev.code == KEY_A || ev.code == KEY_D) {
bool down = (ev.value != 0); // 1 = press, 2 = autorepeat, 0 = release
if (ev.code == KEY_W) w_down = down;
if (ev.code == KEY_S) s_down = down;
if (ev.code == KEY_A) a_down = down;
if (ev.code == KEY_D) d_down = down;
int x = (d_down ? 32767 : 0) + (a_down ? -32768 : 0);
int y = (s_down ? 32767 : 0) + (w_down ? -32768 : 0);
emit_gamepad_event(global_gamepad_fd, EV_ABS, ABS_X, x == -1 ? 0 : x);
emit_gamepad_event(global_gamepad_fd, EV_ABS, ABS_Y, y == -1 ? 0 : y);
send_sync_event(global_gamepad_fd);
}
else if (ev.code == KEY_C && ev.value != 2) {
emit_gamepad_event(global_gamepad_fd, EV_KEY, BTN_B, ev.value);
send_sync_event(global_gamepad_fd);
}
else if (ev.code == KEY_LEFTSHIFT) {
emit_gamepad_event(global_gamepad_fd, EV_KEY, BTN_THUMBL, ev.value);
send_sync_event(global_gamepad_fd);
}
else if (ev.code == KEY_RIGHTSHIFT) {
emit_gamepad_event(global_gamepad_fd, EV_KEY, BTN_THUMBR, ev.value);
send_sync_event(global_gamepad_fd);
}
else if (ev.code == KEY_LEFTCTRL) {
emit_gamepad_event(global_gamepad_fd, EV_ABS, ABS_RX, 0);
emit_gamepad_event(global_gamepad_fd, EV_ABS, ABS_RY, 0);
send_sync_event(global_gamepad_fd);
}
}
}
}
// Processing Mouse Event
if (fds[1].revents & POLLIN) {
while (read(global_mouse_fd, &ev, sizeof(struct input_event)) > 0) {
if (ev.type == EV_REL) {
right_stick_deflected = true;
last_mouse_motion_ms = now_ms();
if (ev.code == REL_X) {
int toWrite = (ev.value > 0) ? MOUSE_SENSITIVITY : ((ev.value < 0) ? MOUSE_SENSITIVITY_NEGATIVE : 0);
emit_gamepad_event(global_gamepad_fd, EV_ABS, ABS_RX, toWrite);
send_sync_event(global_gamepad_fd);
}
if (ev.code == REL_Y) {
int toWrite = (ev.value > 0) ? MOUSE_SENSITIVITY : ((ev.value < 0) ? MOUSE_SENSITIVITY_NEGATIVE : 0);
emit_gamepad_event(global_gamepad_fd, EV_ABS, ABS_RY, toWrite);
send_sync_event(global_gamepad_fd);
}
}
else if (ev.type == EV_KEY) {
if (ev.code == BTN_LEFT) {
emit_gamepad_event(global_gamepad_fd, EV_KEY, BTN_TL2, ev.value);
send_sync_event(global_gamepad_fd);
}
else if (ev.code == BTN_RIGHT) {
emit_gamepad_event(global_gamepad_fd, EV_KEY, BTN_TR2, ev.value);
send_sync_event(global_gamepad_fd);
}
else if (ev.code == BTN_MIDDLE) {
emit_gamepad_event(global_gamepad_fd, EV_ABS, ABS_RX, 0);
emit_gamepad_event(global_gamepad_fd, EV_ABS, ABS_RY, 0);
send_sync_event(global_gamepad_fd);
}
}
}
}
}
return 0; // Triggering safe atexit registered hook function automatically
}