forked from CommanderRag/xbox-controller-emulator-linux
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathemulator.c
More file actions
404 lines (346 loc) · 13.5 KB
/
emulator.c
File metadata and controls
404 lines (346 loc) · 13.5 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <argp.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <time.h>
#include <errno.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include "keys.h"
#define GAMEPAD_NAME "Virtual XPAD"
#define MOUSE_SENSITIVITY 512
#define MOUSE_SENSITIVITY_NEGATIVE -512
char pathKeyboard[256] = "???";
char pathMouse[256] = "???";
bool verbose = false;
bool q_pressed = false;
int absMultiplier = 8;
static int parse_opt(int key, char *arg, struct argp_state *state)
{
switch(key)
{
case 'm':
if(strlen(arg)<256) strcpy(pathMouse,arg);
break;
case 'k':
if(strlen(arg)<256) strcpy(pathKeyboard,arg);
break;
case 'v':
verbose = true;
break;
}
return 0;
}
int parse_arguments(int argc, char**argv)
{
struct argp_option options[] =
{
{ "mouse", 'm', "PATH", 0, "The path to mouse (should look something like /dev/input/eventX)" },
{ "keyboard", 'k', "PATH", 0, "The path to keyboard (should look something like /dev/input/eventX)" },
{ "verbose", 'v', 0, OPTION_ARG_OPTIONAL, "Show more info" },
{ 0 }
};
struct argp argp = { options, parse_opt };
return argp_parse (&argp, argc, argv, 0, 0, 0);
}
void exitFunc(int keyboard_fd, int mouse_fd, int gamepad_fd)
{
close(keyboard_fd);
close(mouse_fd);
if (ioctl(gamepad_fd, UI_DEV_DESTROY) < 0)
{
printf("Error destroying gamepad! \n");
}
close(gamepad_fd);
}
void send_sync_event(int gamepad_fd, struct input_event gamepad_event)
{
memset(&gamepad_event, 0, sizeof(struct input_event));
gamepad_event.type = EV_SYN;
gamepad_event.code = 0;
gamepad_event.value = 0;
if(write(gamepad_fd, &gamepad_event, sizeof(struct input_event)) < 0)
{
printf("Error writing sync event\n");
}
}
// TYPE Is The event to write to the gamepad and CODE is an integer value for the button on the gamepad
void send_event(int gamepad_fd, struct input_event gamepad_event, int TYPE, int CODE, int VALUE)
{
memset(&gamepad_event, 0, sizeof(struct input_event));
gamepad_event.type = TYPE;
gamepad_event.code = CODE;
gamepad_event.value = VALUE;
if(write(gamepad_fd, &gamepad_event, sizeof(struct input_event)) < 0)
{
printf("Error writing event to gamepad!\n");
}
else if(verbose)
{
printf("-> Gamepad: type %d code %d value %d ", gamepad_event.type, gamepad_event.code, gamepad_event.value);
}
}
void send_event_and_sync(int gamepad_fd, struct input_event gamepad_event, int TYPE, int CODE, int VALUE)
{
send_event(gamepad_fd, gamepad_event, TYPE, CODE, VALUE);
send_sync_event(gamepad_fd, gamepad_event);
}
int main(int argc, char *argv[])
{
parse_arguments(argc, argv);
sleep(1);
int rcode = 0;
char keyboard_name[256] = "Unknown";
int keyboard_fd = open(pathKeyboard, O_RDONLY | O_NONBLOCK);
if (keyboard_fd == -1)
{
printf("Failed to open keyboard -> %s\n",pathKeyboard);
exit(1);
}
rcode = ioctl(keyboard_fd, EVIOCGNAME(sizeof(keyboard_name)), keyboard_name);
printf("Reading from : %s \n", keyboard_name);
// printf("Getting exclusive access: ");
// rcode = ioctl(keyboard_fd, EVIOCGRAB, 1);
// printf("%s\n", (rcode == 0) ? "SUCCESS" : "FAILURE");>>
struct input_event keyboard_event;
char mouse_name[256] = "Unknown";
int mouse_fd = open(pathMouse, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (mouse_fd == -1)
{
printf("Failed to open mouse -> %s\n",pathMouse);
exit(1);
}
rcode = ioctl(mouse_fd, EVIOCGNAME(sizeof(mouse_name)), mouse_name);
printf("Reading from : %s \n", mouse_name);
struct input_event mouse_event;
// printf("Getting exclusive access: ");
// rcode = ioctl(mouse_fd, EVIOCGRAB, 1);
// printf("%s\n", (rcode == 0) ? "SUCCESS" : "FAILURE");
// Now, create gamepad
int gamepad_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if (gamepad_fd < 0)
{
printf("Opening of input failed! \n");
return 1;
}
ioctl(gamepad_fd, UI_SET_EVBIT, EV_KEY); // setting Gamepad keys
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_A);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_B);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_X);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_Y);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_TL);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_TR);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_TL2);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_TR2);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_START);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_SELECT);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_THUMBL);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_THUMBR);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_DPAD_UP);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_DPAD_DOWN);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_DPAD_LEFT);
ioctl(gamepad_fd, UI_SET_KEYBIT, BTN_DPAD_RIGHT);
ioctl(gamepad_fd, UI_SET_EVBIT, EV_ABS); // setting Gamepad thumbsticks
ioctl(gamepad_fd, UI_SET_ABSBIT, ABS_X);
ioctl(gamepad_fd, UI_SET_ABSBIT, ABS_Y);
ioctl(gamepad_fd, UI_SET_ABSBIT, ABS_RX);
ioctl(gamepad_fd, UI_SET_ABSBIT, ABS_RY);
ioctl(gamepad_fd, UI_SET_ABSBIT, ABS_TILT_X);
ioctl(gamepad_fd, UI_SET_ABSBIT, ABS_TILT_Y);
struct uinput_user_dev uidev;
memset(&uidev, 0, sizeof(uidev));
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, GAMEPAD_NAME); // Name of Gamepad
uidev.id.bustype = BUS_USB;
uidev.id.vendor = 0x3;
uidev.id.product = 0x3;
uidev.id.version = 2;
uidev.absmax[ABS_X] = 32767; // Parameters of thumbsticks
uidev.absmin[ABS_X] = -32768;
uidev.absfuzz[ABS_X] = 0;
uidev.absflat[ABS_X] = 15;
uidev.absmax[ABS_Y] = 32767;
uidev.absmin[ABS_Y] = -32768;
uidev.absfuzz[ABS_Y] = 0;
uidev.absflat[ABS_Y] = 15;
uidev.absmax[ABS_RX] = 512;
uidev.absmin[ABS_RX] = -512;
uidev.absfuzz[ABS_RX] = 0;
uidev.absflat[ABS_RX] = 16;
uidev.absmax[ABS_RY] = 512;
uidev.absmin[ABS_RY] = -512;
uidev.absfuzz[ABS_RY] = 0;
uidev.absflat[ABS_RY] = 16;
if (write(gamepad_fd, &uidev, sizeof(uidev)) < 0)
{
printf("Failed to write! \n");
return 1;
}
if (ioctl(gamepad_fd, UI_DEV_CREATE) < 0)
{
printf("Failed to create gamepad! \n");
return 1;
}
sleep(0.6);
struct input_event gamepad_ev;
while (1)
{
sleep(0.001);
if (read(keyboard_fd, &keyboard_event, sizeof(keyboard_event)) != -1)
{
if(verbose)
{
printf("Event: Keyboard: type %d code %d value %d ", keyboard_event.type, keyboard_event.code, keyboard_event.value);
}
if (keyboard_event.code == KEY_Q && keyboard_event.value == 1)
{
q_pressed = !q_pressed;
}
if (keyboard_event.code == KEY_ENTER && keyboard_event.value == 1 && q_pressed == true)
{
exitFunc(keyboard_fd, mouse_fd, gamepad_fd);
break;
}
if (keyboard_event.value != 2) // only care about button press and not hold
{
switch (keyboard_event.code)
{
case KEY_C:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_Y, keyboard_event.value);
break;
case KEY_X:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_X, keyboard_event.value);
break;
case KEY_SPACE:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_A, keyboard_event.value);
break;
case KEY_LEFTALT:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_B, keyboard_event.value);
break;
case KEY_ENTER:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_START, keyboard_event.value);
break;
case KEY_TAB:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_SELECT, keyboard_event.value);
break;
}
}
bool pressedOrHold = keyboard_event.value == 2 || keyboard_event.value == 1;
switch (keyboard_event.code)
{
case KEY_W:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_ABS, ABS_Y, pressedOrHold ? -2000 * absMultiplier : 0);
break;
case KEY_S:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_ABS, ABS_Y, pressedOrHold ? 2000 * absMultiplier : 0);
break;
case KEY_A:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_ABS, ABS_X, pressedOrHold ? -2000 * absMultiplier : 0);
break;
case KEY_D:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_ABS, ABS_X, pressedOrHold ? 2000 * absMultiplier : 0);
break;
}
switch (keyboard_event.code)
{
case KEY_LEFTSHIFT:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_TL, keyboard_event.value);
break;
case KEY_RIGHTSHIFT:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_TR, keyboard_event.value);
break;
case KEY_Q:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_TL2, keyboard_event.value);
break;
case KEY_E:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_TR2, keyboard_event.value);
break;
case KEY_UP:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_DPAD_UP, keyboard_event.value);
break;
case KEY_DOWN:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_DPAD_DOWN, keyboard_event.value);
break;
case KEY_LEFT:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_DPAD_LEFT, keyboard_event.value);
break;
case KEY_RIGHT:
send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_DPAD_RIGHT, keyboard_event.value);
break;
case KEY_PAGEUP:
absMultiplier >= 15 ? absMultiplier = 15 : absMultiplier++;
break;
case KEY_PAGEDOWN:
absMultiplier <= 1 ? absMultiplier = 1 : absMultiplier--;
break;
// reset view joystick on left control
case KEY_LEFTCTRL:
{
send_event(gamepad_fd, gamepad_ev, EV_ABS, ABS_RX, 0);
send_event(gamepad_fd, gamepad_ev, EV_ABS, ABS_RY, 0);
// send one sync event for both axes
send_sync_event(gamepad_fd, gamepad_ev);
}
}
if(verbose) printf("\n");
}
if (read(mouse_fd, &mouse_event, sizeof(struct input_event)) != -1)
{
if(verbose)
{
printf("Event: Mouse: type %d code %d value %d ", mouse_event.type, mouse_event.code, mouse_event.value);
}
switch (mouse_event.type)
{
case EV_REL:
if (mouse_event.code == REL_X)
{
int toWrite = mouse_event.value * 32;
if (mouse_event.value > 0 && toWrite > MOUSE_SENSITIVITY) toWrite = MOUSE_SENSITIVITY;
if (mouse_event.value < 0 && toWrite < MOUSE_SENSITIVITY_NEGATIVE) toWrite = MOUSE_SENSITIVITY_NEGATIVE;
if (mouse_event.value == 0) toWrite = 0;
send_event_and_sync(gamepad_fd, gamepad_ev, EV_ABS, ABS_RX, toWrite);
}
if (mouse_event.code == REL_Y)
{
int toWrite = mouse_event.value * 32;
if (mouse_event.value > 0 && toWrite > MOUSE_SENSITIVITY) toWrite = MOUSE_SENSITIVITY;
if (mouse_event.value < 0 && toWrite < MOUSE_SENSITIVITY_NEGATIVE) toWrite = MOUSE_SENSITIVITY_NEGATIVE;
if (mouse_event.value == 0) toWrite = 0;
send_event_and_sync(gamepad_fd, gamepad_ev, EV_ABS, ABS_RY, toWrite);
}
if (mouse_event.code == REL_WHEEL)
{
absMultiplier += mouse_event.value;
if (absMultiplier >= 15) absMultiplier = 15;
if (absMultiplier <= 1) absMultiplier = 1;
}
break;
case EV_KEY:
if (mouse_event.code == BTN_LEFT) send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_THUMBL, mouse_event.value);
if (mouse_event.code == BTN_RIGHT) send_event_and_sync(gamepad_fd, gamepad_ev, EV_KEY, BTN_THUMBR, mouse_event.value);
// reset controller state
if (mouse_event.code == BTN_MIDDLE)
{
send_event(gamepad_fd, gamepad_ev, EV_ABS, ABS_RX, 0);
send_event(gamepad_fd, gamepad_ev, EV_ABS, ABS_RY, 0);
// send one sync event for both axes
send_sync_event(gamepad_fd, gamepad_ev);
}
break;
}
if(verbose) printf("\n");
}
}
printf("Exiting.\n");
rcode = ioctl(keyboard_fd, EVIOCGRAB, 1);
close(keyboard_fd);
rcode = ioctl(mouse_fd, EVIOCGRAB, 1);
close(mouse_fd);
return 0;
}