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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include "screen.h"
#include <psp2/ctrl.h>
#include <psp2/touch.h>
#include <psp2/types.h>
#include <psp2/sysmodule.h>
#include <psp2/net/net.h>
#include <psp2/net/netctl.h>
#include <psp2/kernel/threadmgr.h>
#include <psp2/kernel/processmgr.h>
#include "common/packet.h"
_Static_assert(SCE_TOUCH_SAMPLING_STATE_START == 1, "Stop using old VitaSDK");
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#define BIT_TOGGLE(w, m, f) (w & ~m) | (-f & m)
#define UINPUT_IOCTL_BASE 'U'
#define EV_SYN 0x00
#define EV_KEY 0x01
#define EV_ABS 0x03
#define BTN_DPAD_UP 0x220
#define BTN_DPAD_DOWN 0x221
#define BTN_DPAD_LEFT 0x222
#define BTN_DPAD_RIGHT 0x223
#define BTN_SOUTH 0x130
#define BTN_EAST 0x131
#define BTN_NORTH 0x133
#define BTN_WEST 0x134
#define BTN_TL 0x136
#define BTN_TR 0x137
#define BTN_TL2 0x138
#define BTN_TR2 0x139
#define BTN_SELECT 0x13a
#define BTN_START 0x13b
#define BTN_MODE 0x13c
#define ABS_X 0x00
#define ABS_Y 0x01
#define ABS_RX 0x03
#define ABS_RY 0x04
#define ABS_HAT0X 0x10
#define ABS_HAT0Y 0x11
#define ABS_MT_POSITION_X 0x35
#define ABS_MT_POSITION_Y 0x36
#define ABS_MAX 0x3f
#define ABS_CNT (ABS_MAX+1)
#define SYN_REPORT 0
#define BUS_VIRTUAL 0x06
struct input_id {
uint16_t bustype;
uint16_t vendor;
uint16_t product;
uint16_t version;
};
#define UINPUT_MAX_NAME_SIZE 80
struct uinput_user_dev {
char name[UINPUT_MAX_NAME_SIZE];
struct input_id id;
uint32_t ff_effects_max;
int32_t absmax[ABS_CNT];
int32_t absmin[ABS_CNT];
int32_t absfuzz[ABS_CNT];
int32_t absflat[ABS_CNT];
};
struct timeval {
#ifdef HOST_32BIT
uint32_t tv_sec, tv_usec;
#else
uint64_t tv_sec, tv_usec;
#endif
};
struct input_event {
struct timeval time;
uint16_t type;
uint16_t code;
int32_t value;
};
#define INPUT_LAG 20
enum {
SCE_CTRL_EXTRA_L2TRIGGER = 0x020000,
SCE_CTRL_EXTRA_R2TRIGGER = 0x040000,
SCE_CTRL_EXTRA_MENU = 0x080000,
};
static struct timeval
us_to_timeval(const uint64_t us)
{
return (struct timeval){
.tv_sec = us / (uint64_t)1e6,
.tv_usec = us % (uint64_t)1e6,
};
}
static SceUInt
ms_to_us(const SceUInt ms)
{
return ms * 1000;
}
static SceUInt
s_to_us(const SceUInt s)
{
return ms_to_us(s * 1000);
}
static void
setup(void)
{
sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG);
sceTouchSetSamplingState(SCE_TOUCH_PORT_FRONT, SCE_TOUCH_SAMPLING_STATE_START);
sceTouchSetSamplingState(SCE_TOUCH_PORT_BACK, SCE_TOUCH_SAMPLING_STATE_START);
sceTouchEnableTouchForce(SCE_TOUCH_PORT_FRONT);
sceTouchEnableTouchForce(SCE_TOUCH_PORT_BACK);
if (!screen_init())
errx(EXIT_FAILURE, "screen_init");
if (sceSysmoduleLoadModule(SCE_SYSMODULE_NET) != 0)
errx(EXIT_FAILURE, "sceSysmoduleLoadModule");
if ((unsigned int)sceNetShowNetstat() == SCE_NET_ERROR_ENOTINIT) {
static uint8_t sce_net_memory[1024 * 1024];
SceNetInitParam initparam = {
.memory = sce_net_memory,
.size = sizeof(sce_net_memory),
};
if (sceNetInit(&initparam) != 0)
errx(EXIT_FAILURE, "sceNetInit");
}
if (sceNetCtlInit() != 0)
errx(EXIT_FAILURE, "sceNetCtlInit");
}
static void
terminate(void)
{
sceNetCtlTerm();
sceNetTerm();
sceSysmoduleUnloadModule(SCE_SYSMODULE_NET);
screen_deinit();
}
static void
disconnect(int fd)
{
sceNetSocketClose(fd);
}
static int
connect(const char *ip, SceNetCtlInfo *info)
{
assert(ip && info);
if (sceNetCtlInetGetInfo(SCE_NETCTL_INFO_GET_IP_ADDRESS, info) != 0) {
warnx("sceNetCtlInetGetInfo");
return -1;
}
SceNetSockaddrIn addr = {
.sin_family = SCE_NET_AF_INET,
.sin_port = sceNetHtons(5000),
};
if (!sceNetInetPton(SCE_NET_AF_INET, ip, &addr.sin_addr)) {
warnx("sceNetInetPton");
return -1;
}
int fd;
if ((fd = sceNetSocket("vita_uinput", SCE_NET_AF_INET, SCE_NET_SOCK_STREAM, 0)) < 0) {
warnx("sceNetSocket: %d", fd);
return fd;
}
if (sceNetConnect(fd, (SceNetSockaddr*)&addr, sizeof(addr))) {
warnx("sceNetConnect: failed connecting to %s", ip);
disconnect(fd);
return -1;
}
return fd;
}
static bool
uinputd_ioctl_other(const int fd, const enum ioctl_dir dir, const uint8_t type, const uint8_t nr, const uint16_t bytes, void *arg)
{
const struct packet packet = {
.type = PACKET_IOCTL,
.ioctl.dir = dir,
.ioctl.type = type,
.ioctl.nr = nr,
.ioctl.arg = IOCTL_ARG_OTHER,
.ioctl.bytes = bytes,
};
if (sceNetSend(fd, &packet, sizeof(packet), 0) != sizeof(packet)) {
warnx("sceNetSend: failed to send %u bytes", sizeof(packet));
return false;
}
if (bytes > 0 && sceNetSend(fd, arg, bytes, 0) != bytes) {
warnx("sceNetSend: failed to send %u bytes", bytes);
return false;
}
return true;
}
static bool
uinputd_ioctl_int(const int fd, const enum ioctl_dir dir, const uint8_t type, const uint8_t nr, int arg)
{
const struct packet packet = {
.type = PACKET_IOCTL,
.ioctl.dir = dir,
.ioctl.type = type,
.ioctl.nr = nr,
.ioctl.arg = IOCTL_ARG_INT,
.ioctl.integer = arg,
};
if (sceNetSend(fd, &packet, sizeof(packet), 0) != sizeof(packet)) {
warnx("sceNetSend: failed to send %u bytes", sizeof(packet));
return false;
}
return true;
}
static bool
uinputd_write(const int fd, const int32_t bytes, const void *data)
{
if (bytes <= 0 || !data)
return true;
const struct packet packet = {
.type = PACKET_WRITE,
.write.bytes = bytes,
};
if (sceNetSend(fd, &packet, sizeof(packet), 0) != sizeof(packet)) {
warnx("sceNetSend: failed to send %u bytes", sizeof(packet));
return false;
}
if (sceNetSend(fd, data, bytes, 0) != bytes) {
warnx("sceNetSend: failed to send %ld bytes", bytes);
return false;
}
return true;
}
static bool
process_touch(const SceTouchData *new, SceTouchData *old, SceCtrlData *ctrl, const bool back_touchpad)
{
if (old->reportNum > 0)
goto end;
for (size_t i = 0; i < new->reportNum; ++i) {
if (back_touchpad) {
if (new->report[i].y > 362 && new->report[i].y < 726) {
if (new->report[i].x < 960 && new->report[i].x > 480) {
ctrl->buttons |= SCE_CTRL_EXTRA_L2TRIGGER;
} else if (new->report[i].x > 960 && new->report[i].x < 1440) {
ctrl->buttons |= SCE_CTRL_EXTRA_R2TRIGGER;
}
}
} else {
ctrl->buttons |= SCE_CTRL_EXTRA_MENU;
}
}
end:
*old = *new;
return true;
}
static void
ctrl_query_changed_analogs(const SceCtrlData *new, const SceCtrlData *old, uint32_t out_changed[4])
{
assert(new && old);
for (size_t i = 0; i < 4; ++i)
out_changed[i] = (uint32_t)~0;
const uint8_t fuzz = 2;
const uint32_t code[4] = { ABS_X, ABS_Y, ABS_RX, ABS_RY };
const uint8_t new_analogs[4] = { new->lx, new->ly, new->rx, new->ry };
const uint8_t old_analogs[4] = { old->lx, old->ly, old->rx, old->ry };
for (size_t i = 0, c = 0; i < ARRAY_SIZE(new_analogs); ++i) {
if (old_analogs[i] - fuzz <= new_analogs[i] && old_analogs[i] + fuzz >= new_analogs[i])
continue;
assert(c < ARRAY_SIZE(code));
out_changed[c++] = code[i];
}
}
static bool
process_ctrl(const int fd, const SceCtrlData *new, SceCtrlData *old, bool *out_changed)
{
assert(new && old && out_changed);
{
struct {
uint32_t code, value;
bool changed;
} abs[] = {
{ ABS_HAT0X, 127, false },
{ ABS_HAT0Y, 127, false },
};
const struct {
uint32_t bit, abs, value;
} map[] = {
{ SCE_CTRL_UP, 1, -127 },
{ SCE_CTRL_DOWN, 1, 128 },
{ SCE_CTRL_LEFT, 0, -127 },
{ SCE_CTRL_RIGHT, 0, 128},
};
for (size_t i = 0; i < ARRAY_SIZE(map); ++i) {
if ((new->buttons & map[i].bit) == (old->buttons & map[i].bit))
continue;
abs[map[i].abs].changed = true;
abs[map[i].abs].value += (new->buttons & map[i].bit ? map[i].value : 0);
}
for (size_t i = 0; i < ARRAY_SIZE(abs); ++i) {
if (!abs[i].changed)
continue;
const struct input_event ev = {
.time = us_to_timeval(new->timeStamp),
.type = EV_ABS,
.code = abs[i].code,
.value = abs[i].value,
};
if (!uinputd_write(fd, sizeof(ev), &ev)) {
warnx("write: failed to write %u bytes", sizeof(ev));
return false;
}
}
}
{
const struct {
uint32_t bit, code;
} map[] = {
{ SCE_CTRL_TRIANGLE, BTN_NORTH },
{ SCE_CTRL_CROSS, BTN_SOUTH },
{ SCE_CTRL_SQUARE, BTN_WEST },
{ SCE_CTRL_CIRCLE, BTN_EAST },
{ SCE_CTRL_START, BTN_START },
{ SCE_CTRL_SELECT, BTN_SELECT },
{ SCE_CTRL_UP, BTN_DPAD_UP },
{ SCE_CTRL_DOWN, BTN_DPAD_DOWN },
{ SCE_CTRL_LEFT, BTN_DPAD_LEFT },
{ SCE_CTRL_RIGHT, BTN_DPAD_RIGHT },
{ SCE_CTRL_LTRIGGER, BTN_TL },
{ SCE_CTRL_RTRIGGER, BTN_TR },
{ SCE_CTRL_EXTRA_L2TRIGGER, BTN_TL2 },
{ SCE_CTRL_EXTRA_R2TRIGGER, BTN_TR2 },
{ SCE_CTRL_EXTRA_MENU, BTN_MODE },
};
for (size_t i = 0; i < ARRAY_SIZE(map); ++i) {
if ((new->buttons & map[i].bit) == (old->buttons & map[i].bit))
continue;
const struct input_event ev = {
.time = us_to_timeval(new->timeStamp),
.type = EV_KEY,
.code = map[i].code,
.value = !!(new->buttons & map[i].bit),
};
if (!uinputd_write(fd, sizeof(ev), &ev)) {
warnx("write: failed to write %u bytes", sizeof(ev));
return false;
}
*out_changed = true;
old->buttons = BIT_TOGGLE(old->buttons, map[i].bit, (new->buttons & map[i].bit));
}
}
{
const uint8_t values[] = {
new->lx,
new->ly,
0,
new->rx,
new->ry,
};
uint8_t dummy;
uint8_t* old_values_ref[] = {
&old->lx,
&old->ly,
&dummy,
&old->rx,
&old->ry,
};
uint32_t changed[4];
ctrl_query_changed_analogs(new, old, changed);
for (size_t i = 0; i < ARRAY_SIZE(changed) && changed[i] != (uint32_t)~0; ++i) {
assert(changed[i] < ARRAY_SIZE(values));
const struct input_event ev = {
.time = us_to_timeval(new->timeStamp),
.type = EV_ABS,
.code = changed[i],
.value = values[changed[i]],
};
if (!uinputd_write(fd, sizeof(ev), &ev)) {
warnx("write: failed to write %u bytes", sizeof(ev));
return false;
}
*out_changed = true;
*old_values_ref[changed[i]] = values[changed[i]];
}
}
return true;
}
static bool
process_syn(const int fd)
{
const struct input_event ev = {
.type = EV_SYN,
.code = SYN_REPORT,
};
if (!uinputd_write(fd, sizeof(ev), &ev)) {
warnx("write: failed to write %u bytes", sizeof(ev));
return false;
}
return true;
}
static bool
setup_uinput(const int fd)
{
if (!uinputd_ioctl_int(fd, IOCTL_IOW, UINPUT_IOCTL_BASE, 100, EV_KEY)) {
warnx("ioctl(UI_SET_EVBIT): EV_KEY");
return false;
}
if (!uinputd_ioctl_int(fd, IOCTL_IOW, UINPUT_IOCTL_BASE, 100, EV_ABS)) {
warnx("ioctl(UI_SET_EVBIT): EV_ABS");
return false;
}
if (!uinputd_ioctl_int(fd, IOCTL_IOW, UINPUT_IOCTL_BASE, 100, EV_SYN)) {
warnx("ioctl(UI_SET_EVBIT): EV_SYN");
return false;
}
{
const int keys[] = {
BTN_NORTH,
BTN_SOUTH,
BTN_WEST,
BTN_EAST,
BTN_START,
BTN_SELECT,
BTN_DPAD_UP,
BTN_DPAD_DOWN,
BTN_DPAD_LEFT,
BTN_DPAD_RIGHT,
BTN_TL,
BTN_TR,
BTN_TL2,
BTN_TR2,
BTN_MODE
};
for (size_t i = 0; i < ARRAY_SIZE(keys); ++i) {
if (!uinputd_ioctl_int(fd, IOCTL_IOW, UINPUT_IOCTL_BASE, 101, keys[i])) {
warnx("ioctl (UI_SET_KEYBIT:%d)", keys[i]);
return false;
}
}
}
{
const int abs[] = {
ABS_X,
ABS_Y,
ABS_RX,
ABS_RY,
ABS_HAT0X,
ABS_HAT0Y,
ABS_MT_POSITION_X,
ABS_MT_POSITION_Y,
};
for (size_t i = 0; i < ARRAY_SIZE(abs); ++i) {
if (!uinputd_ioctl_int(fd, IOCTL_IOW, UINPUT_IOCTL_BASE, 103, abs[i])) {
warnx("ioctl (UI_SET_ABSBIT:%d)", abs[i]);
return false;
}
}
}
{
struct uinput_user_dev setup = {
.name = "PS Vita",
.id = {
.bustype = BUS_VIRTUAL,
.vendor = 0x054C,
.product = 0x04E4,
.version = 0x0100,
},
.absmax = {
[ABS_X] = 255,
[ABS_Y] = 255,
[ABS_RX] = 255,
[ABS_RY] = 255,
[ABS_HAT0X] = 255,
[ABS_HAT0Y] = 255,
},
};
if (!uinputd_write(fd, sizeof(setup), &setup)) {
warnx("write: failed to write %u bytes", sizeof(setup));
return false;
}
if (!uinputd_ioctl_other(fd, IOCTL_NONE, UINPUT_IOCTL_BASE, 1, 0, NULL)) {
warnx("ioctl (UI_DEV_CREATE)");
return false;
}
}
return true;
}
static void
run(const int fd)
{
struct {
SceCtrlData ctrl;
SceTouchData touch[2];
} old = {0}, new = {0};
bool error = !setup_uinput(fd);
while (!error) {
sceKernelPowerTick(0);
sceKernelDelayThread(ms_to_us(INPUT_LAG));
sceTouchRead(SCE_TOUCH_PORT_FRONT, &new.touch[0], 1);
sceTouchRead(SCE_TOUCH_PORT_BACK, &new.touch[1], 1);
sceCtrlReadBufferPositive(0, &new.ctrl, 1);
bool changed = false;
for (size_t i = 0; i < ARRAY_SIZE(new.touch); ++i) {
if (!process_touch(&new.touch[i], &old.touch[i], &new.ctrl, (i == 1)))
break;
}
if (!process_ctrl(fd, &new.ctrl, &old.ctrl, &changed))
break;
if (changed && !process_syn(fd))
break;
}
}
int
main(void)
{
setup();
atexit(terminate);
TODO
while (true) {
int fd;
SceNetCtlInfo info;
const char *ip = "192.168.1.2";
if ((fd = connect(ip, &info)) >= 0) {
printf("vita-uinput %s\n", VITA_UINPUT_VERSION);
printf("Vita IP: %s\n", info.ip_address);
printf("Server IP: %s\n", ip);
printf("sizeof(struct packet) is %u bytes\n", sizeof(struct packet));
printf("sizeof(struct input_event) is %u bytes\n", sizeof(struct input_event));
printf("sizeof(struct uinput_user_dev) is %u bytes\n", sizeof(struct uinput_user_dev));
run(fd);
}
disconnect(fd);
warnx("Reconnecting after 5 seconds");
sceKernelDelayThread(s_to_us(5));
screen_clear();
}
exit(EXIT_SUCCESS);
return EXIT_SUCCESS;
}
|