forked from moonlight-stream/moonlight-embedded
-
Notifications
You must be signed in to change notification settings - Fork 75
/
vita.c
588 lines (501 loc) · 17.2 KB
/
vita.c
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
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2016 Ilya Zhuravlev, Sunguk Lee, Vasyl Horbachenko
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
#include <ctype.h>
#include "../graphics.h"
#include "../config.h"
#include "../connection.h"
#include "vita.h"
#include "mapping.h"
#include <Limelight.h>
#include <psp2/net/net.h>
#include <psp2/sysmodule.h>
#include <psp2/kernel/sysmem.h>
#include <psp2/kernel/threadmgr.h>
#include <psp2/ctrl.h>
#include <psp2/touch.h>
#include <psp2/rtc.h>
#define WIDTH 960
#define HEIGHT 544
struct mapping map = {0};
typedef struct input_data {
short button;
short lx;
short ly;
short rx;
short ry;
char lt;
char rt;
} input_data;
typedef struct Point {
short x;
short y;
} Point;
typedef struct Section {
Point left;
Point right;
} Section;
typedef struct TouchData {
short button;
short finger;
Point points[4];
} TouchData;
#define lerp(value, from_max, to_max) ((((value*10) * (to_max*10))/(from_max*10))/10)
double mouse_multiplier;
#define MOUSE_ACTION_DELAY 100000 // 100ms
inline bool mouse_click(short finger_count, bool press) {
int mode;
if (press) {
mode = BUTTON_ACTION_PRESS;
} else {
mode = BUTTON_ACTION_RELEASE;
}
switch (finger_count) {
case 1:
LiSendMouseButtonEvent(mode, BUTTON_LEFT);
return true;
case 2:
LiSendMouseButtonEvent(mode, BUTTON_RIGHT);
return true;
}
return false;
}
inline void move_mouse(TouchData old, TouchData cur) {
double delta_x = (cur.points[0].x - old.points[0].x) / 2.;
double delta_y = (cur.points[0].y - old.points[0].y) / 2.;
if (delta_x == 0 && delta_y == 0) {
return;
}
int x = lround(delta_x * mouse_multiplier);
int y = lround(delta_y * mouse_multiplier);
LiSendMouseMoveEvent(x, y);
}
inline void move_wheel(TouchData old, TouchData cur) {
int old_y = (old.points[0].y + old.points[1].y) / 2;
int cur_y = (cur.points[0].y + cur.points[1].y) / 2;
int delta_y = (cur_y - old_y) / 2;
if (!delta_y) {
return;
}
LiSendScrollEvent(delta_y);
}
SceCtrlData pad, pad_old;
TouchData touch;
TouchData touch_old, swipe;
SceTouchData front, back;
int front_state = NO_TOUCH_ACTION;
short finger_count = 0;
SceRtcTick current, until;
static int special_status;
input_data curr, old;
int controller_port;
// TODO config
static int VERTICAL;
static int HORIZONTAL;
#define IN_SECTION(SECTION, X, Y) \
((SECTION).left.x <= (X) && (X) <= (SECTION).right.x && \
(SECTION).left.y <= (Y) && (Y) <= (SECTION).right.y)
// TODO sections
Section BACK_SECTIONS[4];
Section FRONT_SECTIONS[4];
inline uint8_t read_backscreen() {
for (int i = 0; i < back.reportNum; i++) {
int x = lerp(back.report[i].x, 1919, WIDTH);
int y = lerp(back.report[i].y, 1087, HEIGHT);
if ((touch.button & TOUCHSEC_NORTHWEST) == 0) {
if (IN_SECTION(BACK_SECTIONS[0], x, y)) {
touch.button |= TOUCHSEC_NORTHWEST;
continue;
}
}
if ((touch.button & TOUCHSEC_NORTHEAST) == 0) {
if (IN_SECTION(BACK_SECTIONS[1], x, y)) {
touch.button |= TOUCHSEC_NORTHEAST;
continue;
}
}
if ((touch.button & TOUCHSEC_SOUTHWEST) == 0) {
if (IN_SECTION(BACK_SECTIONS[2], x, y)) {
touch.button |= TOUCHSEC_SOUTHWEST;
continue;
}
}
if ((touch.button & TOUCHSEC_SOUTHEAST) == 0) {
if (IN_SECTION(BACK_SECTIONS[3], x, y)) {
touch.button |= TOUCHSEC_SOUTHEAST;
continue;
}
}
}
return 0;
}
inline uint8_t read_frontscreen() {
for (int i = 0; i < front.reportNum; i++) {
int x = lerp(front.report[i].x, 1919, WIDTH);
int y = lerp(front.report[i].y, 1087, HEIGHT);
if ((touch.button & TOUCHSEC_SPECIAL_NW) == 0) {
if (IN_SECTION(FRONT_SECTIONS[0], x, y)) {
touch.button |= TOUCHSEC_SPECIAL_NW;
continue;
}
}
if ((touch.button & TOUCHSEC_SPECIAL_NE) == 0) {
if (IN_SECTION(FRONT_SECTIONS[1], x, y)) {
touch.button |= TOUCHSEC_SPECIAL_NE;
continue;
}
}
if ((touch.button & TOUCHSEC_SPECIAL_SW) == 0) {
if (IN_SECTION(FRONT_SECTIONS[2], x, y)) {
touch.button |= TOUCHSEC_SPECIAL_SW;
continue;
}
}
if ((touch.button & TOUCHSEC_SPECIAL_SE) == 0) {
if (IN_SECTION(FRONT_SECTIONS[3], x, y)) {
touch.button |= TOUCHSEC_SPECIAL_SE;
continue;
}
}
// FIXME if touch same section using multiple finger, they can count finger
touch.points[touch.finger].x = x;
touch.points[touch.finger].y = y;
touch.finger += 1;
}
return 0;
}
inline uint32_t is_pressed(uint32_t defined) {
uint32_t dev_type = defined & INPUT_TYPE_MASK;
uint32_t dev_val = defined & INPUT_VALUE_MASK;
switch(dev_type) {
case INPUT_TYPE_GAMEPAD:
return pad.buttons & dev_val;
case INPUT_TYPE_TOUCHSCREEN:
return touch.button & dev_val;
}
return 0;
}
inline uint32_t is_old_pressed(uint32_t defined) {
uint32_t dev_type = defined & INPUT_TYPE_MASK;
uint32_t dev_val = defined & INPUT_VALUE_MASK;
switch(dev_type) {
case INPUT_TYPE_GAMEPAD:
return pad_old.buttons & dev_val;
case INPUT_TYPE_TOUCHSCREEN:
return touch_old.button & dev_val;
}
return 0;
}
inline short read_analog(uint32_t defined) {
uint32_t dev_type = defined & INPUT_TYPE_MASK;
uint32_t dev_val = defined & INPUT_VALUE_MASK;
if (dev_type == INPUT_TYPE_ANALOG) {
int v;
switch(dev_val) {
case LEFTX:
v = pad.lx;
break;
case LEFTY:
v = pad.ly;
break;
case RIGHTX:
v = pad.rx;
break;
case RIGHTY:
v = pad.ry;
break;
case LEFT_TRIGGER:
return pad.lt;
case RIGHT_TRIGGER:
return pad.rt;
default:
return 0;
}
v = v * 256 - (1 << 15) + 128;
return (short)(v);
}
return is_pressed(defined) ? 0xff : 0;
}
inline void special(uint32_t defined, uint32_t pressed, uint32_t old_pressed) {
uint32_t dev_type = defined & INPUT_TYPE_MASK;
uint32_t dev_val = defined & INPUT_VALUE_MASK;
if (pressed) {
switch(dev_type) {
case INPUT_TYPE_SPECIAL:
if (dev_val == INPUT_SPECIAL_KEY_PAUSE) {
connection_minimize();
return;
}
case INPUT_TYPE_GAMEPAD:
curr.button |= dev_val;
return;
case INPUT_TYPE_ANALOG:
switch(dev_val) {
case LEFT_TRIGGER:
curr.lt = 0xff;
return;
case RIGHT_TRIGGER:
curr.rt = 0xff;
return;
}
return;
case INPUT_TYPE_MOUSE:
if (!old_pressed) {
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, dev_val);
}
return;
case INPUT_TYPE_KEYBOARD:
if (!old_pressed) {
LiSendKeyboardEvent(dev_val, KEY_ACTION_DOWN, 0);
}
return;
}
} else {
// released
switch(dev_type) {
case INPUT_TYPE_MOUSE:
if (old_pressed) {
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, dev_val);
}
return;
case INPUT_TYPE_KEYBOARD:
if (old_pressed) {
LiSendKeyboardEvent(dev_val, KEY_ACTION_UP, 0);
}
return;
}
}
}
static inline void vitainput_process(void) {
memset(&pad, 0, sizeof(pad));
memset(&touch, 0, sizeof(TouchData));
memset(&curr, 0, sizeof(input_data));
sceCtrlSetSamplingModeExt(SCE_CTRL_MODE_ANALOG_WIDE);
sceCtrlPeekBufferPositiveExt2(controller_port, &pad, 1);
sceTouchPeek(SCE_TOUCH_PORT_FRONT, &front, 1);
sceTouchPeek(SCE_TOUCH_PORT_BACK, &back, 1);
read_frontscreen();
read_backscreen();
sceRtcGetCurrentTick(¤t);
// buttons
curr.button |= is_pressed(map.btn_dpad_up) ? UP_FLAG : 0;
curr.button |= is_pressed(map.btn_dpad_left) ? LEFT_FLAG : 0;
curr.button |= is_pressed(map.btn_dpad_down) ? DOWN_FLAG : 0;
curr.button |= is_pressed(map.btn_dpad_right) ? RIGHT_FLAG : 0;
curr.button |= is_pressed(map.btn_start) ? PLAY_FLAG : 0;
curr.button |= is_pressed(map.btn_select) ? BACK_FLAG : 0;
curr.button |= is_pressed(map.btn_north) ? Y_FLAG : 0;
curr.button |= is_pressed(map.btn_east) ? B_FLAG : 0;
curr.button |= is_pressed(map.btn_south) ? A_FLAG : 0;
curr.button |= is_pressed(map.btn_west) ? X_FLAG : 0;
curr.button |= is_pressed(map.btn_thumbl) ? LB_FLAG : 0; // l1
curr.button |= is_pressed(map.btn_thumbr) ? RB_FLAG : 0; // r1
curr.button |= is_pressed(map.btn_tl2) ? LS_CLK_FLAG : 0; // l3
curr.button |= is_pressed(map.btn_tr2) ? RS_CLK_FLAG : 0; // r3
// analogs
curr.lt = read_analog(map.btn_tl); // l2
curr.rt = read_analog(map.btn_tr); // r2
curr.lx = read_analog(map.abs_x);
curr.ly = read_analog(map.abs_y);
curr.rx = read_analog(map.abs_rx);
curr.ry = read_analog(map.abs_ry);
// special touchscreen buttons
special(config.special_keys.nw,
is_pressed(INPUT_TYPE_TOUCHSCREEN | TOUCHSEC_SPECIAL_NW),
is_old_pressed(INPUT_TYPE_TOUCHSCREEN | TOUCHSEC_SPECIAL_NW));
special(config.special_keys.ne,
is_pressed(INPUT_TYPE_TOUCHSCREEN | TOUCHSEC_SPECIAL_NE),
is_old_pressed(INPUT_TYPE_TOUCHSCREEN | TOUCHSEC_SPECIAL_NE));
special(config.special_keys.sw,
is_pressed(INPUT_TYPE_TOUCHSCREEN | TOUCHSEC_SPECIAL_SW),
is_old_pressed(INPUT_TYPE_TOUCHSCREEN | TOUCHSEC_SPECIAL_SW));
special(config.special_keys.se,
is_pressed(INPUT_TYPE_TOUCHSCREEN | TOUCHSEC_SPECIAL_SE),
is_old_pressed(INPUT_TYPE_TOUCHSCREEN | TOUCHSEC_SPECIAL_SE));
// mouse
switch (front_state) {
case NO_TOUCH_ACTION:
if (touch.finger > 0) {
front_state = ON_SCREEN_TOUCH;
finger_count = touch.finger;
sceRtcTickAddMicroseconds(&until, ¤t, MOUSE_ACTION_DELAY);
}
break;
case ON_SCREEN_TOUCH:
if (sceRtcCompareTick(¤t, &until) < 0) {
if (touch.finger < finger_count) {
// TAP
if (mouse_click(finger_count, true)) {
front_state = SCREEN_TAP;
sceRtcTickAddMicroseconds(&until, ¤t, MOUSE_ACTION_DELAY);
} else {
front_state = NO_TOUCH_ACTION;
}
} else if (touch.finger > finger_count) {
// finger count changed
finger_count = touch.finger;
}
} else {
front_state = SWIPE_START;
}
break;
case SCREEN_TAP:
if (sceRtcCompareTick(¤t, &until) >= 0) {
mouse_click(finger_count, false);
front_state = NO_TOUCH_ACTION;
}
break;
case SWIPE_START:
memcpy(&swipe, &touch, sizeof(swipe));
front_state = ON_SCREEN_SWIPE;
break;
case ON_SCREEN_SWIPE:
if (touch.finger > 0) {
switch (touch.finger) {
case 1:
move_mouse(swipe, touch);
break;
case 2:
move_wheel(swipe, touch);
break;
}
memcpy(&swipe, &touch, sizeof(swipe));
} else {
front_state = NO_TOUCH_ACTION;
}
break;
}
if (memcmp(&curr, &old, sizeof(input_data)) != 0) {
LiSendControllerEvent(curr.button, curr.lt, curr.rt,
curr.lx, -1 * curr.ly, curr.rx, -1 * curr.ry);
memcpy(&old, &curr, sizeof(input_data));
memcpy(&pad_old, &pad, sizeof(SceCtrlData));
}
if (memcmp(&touch, &touch_old, sizeof(TouchData)) != 0) {
memcpy(&touch_old, &touch, sizeof(TouchData));
}
}
static uint8_t active_input_thread = 0;
int vitainput_thread(SceSize args, void *argp) {
while (1) {
if (active_input_thread) {
vitainput_process();
}
sceKernelDelayThread(5000); // 5 ms
}
return 0;
}
bool vitainput_init() {
sceCtrlSetSamplingModeExt(SCE_CTRL_MODE_ANALOG_WIDE);
sceTouchSetSamplingState(SCE_TOUCH_PORT_FRONT, SCE_TOUCH_SAMPLING_STATE_START);
sceTouchSetSamplingState(SCE_TOUCH_PORT_BACK, SCE_TOUCH_SAMPLING_STATE_START);
SceUID thid = sceKernelCreateThread("vitainput_thread", vitainput_thread, 0, 0x40000, 0, 0, NULL);
if (thid >= 0) {
sceKernelStartThread(thid, 0, NULL);
return true;
}
return false;
}
void vitainput_config(CONFIGURATION config) {
map.abs_x = LEFTX | INPUT_TYPE_ANALOG;
map.abs_y = LEFTY | INPUT_TYPE_ANALOG;
map.abs_rx = RIGHTX | INPUT_TYPE_ANALOG;
map.abs_ry = RIGHTY | INPUT_TYPE_ANALOG;
map.btn_dpad_up = SCE_CTRL_UP | INPUT_TYPE_GAMEPAD;
map.btn_dpad_down = SCE_CTRL_DOWN | INPUT_TYPE_GAMEPAD;
map.btn_dpad_left = SCE_CTRL_LEFT | INPUT_TYPE_GAMEPAD;
map.btn_dpad_right = SCE_CTRL_RIGHT | INPUT_TYPE_GAMEPAD;
map.btn_south = SCE_CTRL_CROSS | INPUT_TYPE_GAMEPAD;
map.btn_east = SCE_CTRL_CIRCLE | INPUT_TYPE_GAMEPAD;
map.btn_north = SCE_CTRL_TRIANGLE | INPUT_TYPE_GAMEPAD;
map.btn_west = SCE_CTRL_SQUARE | INPUT_TYPE_GAMEPAD;
map.btn_select = SCE_CTRL_SELECT | INPUT_TYPE_GAMEPAD;
map.btn_start = SCE_CTRL_START | INPUT_TYPE_GAMEPAD;
map.btn_thumbl = SCE_CTRL_L1 | INPUT_TYPE_GAMEPAD;
map.btn_thumbr = SCE_CTRL_R1 | INPUT_TYPE_GAMEPAD;
if (config.model == SCE_KERNEL_MODEL_VITATV) {
map.btn_tl = LEFT_TRIGGER | INPUT_TYPE_ANALOG;
map.btn_tr = RIGHT_TRIGGER | INPUT_TYPE_ANALOG;
map.btn_tl2 = SCE_CTRL_L3 | INPUT_TYPE_GAMEPAD;
map.btn_tr2 = SCE_CTRL_R3 | INPUT_TYPE_GAMEPAD;
} else {
map.btn_tl = TOUCHSEC_NORTHWEST | INPUT_TYPE_TOUCHSCREEN;
map.btn_tr = TOUCHSEC_NORTHEAST | INPUT_TYPE_TOUCHSCREEN;
map.btn_tl2 = TOUCHSEC_SOUTHWEST | INPUT_TYPE_TOUCHSCREEN;
map.btn_tr2 = TOUCHSEC_SOUTHEAST | INPUT_TYPE_TOUCHSCREEN;
}
if (config.mapping) {
char mapping_file_path[256];
sprintf(mapping_file_path, "ux0:data/moonlight/%s", config.mapping);
printf("Loading mapping at %s\n", mapping_file_path);
mapping_load(mapping_file_path, &map);
}
controller_port = config.model == SCE_KERNEL_MODEL_VITATV ? 1 : 0;
VERTICAL = (WIDTH - config.back_deadzone.left - config.back_deadzone.right) / 2
+ config.back_deadzone.left;
HORIZONTAL = (HEIGHT - config.back_deadzone.top - config.back_deadzone.bottom) / 2
+ config.back_deadzone.top;
BACK_SECTIONS[0].left.x = config.back_deadzone.left;
BACK_SECTIONS[0].left.y = config.back_deadzone.top;
BACK_SECTIONS[0].right.x = VERTICAL;
BACK_SECTIONS[0].right.y = HORIZONTAL;
BACK_SECTIONS[1].left.x = VERTICAL;
BACK_SECTIONS[1].left.y = config.back_deadzone.top;
BACK_SECTIONS[1].right.x = WIDTH - config.back_deadzone.right;
BACK_SECTIONS[1].right.y = HORIZONTAL;
BACK_SECTIONS[2].left.x = config.back_deadzone.left;
BACK_SECTIONS[2].left.y = HORIZONTAL;
BACK_SECTIONS[2].right.x = VERTICAL;
BACK_SECTIONS[2].right.y = HEIGHT - config.back_deadzone.bottom;
BACK_SECTIONS[3].left.x = VERTICAL;
BACK_SECTIONS[3].left.y = HORIZONTAL;
BACK_SECTIONS[3].right.x = WIDTH - config.back_deadzone.right;
BACK_SECTIONS[3].right.y = HEIGHT - config.back_deadzone.bottom;
FRONT_SECTIONS[0].left.x = config.special_keys.offset;
FRONT_SECTIONS[0].left.y = config.special_keys.offset;
FRONT_SECTIONS[0].right.x = config.special_keys.offset + config.special_keys.size;
FRONT_SECTIONS[0].right.y = config.special_keys.offset + config.special_keys.size;
FRONT_SECTIONS[1].left.x = WIDTH - config.special_keys.offset - config.special_keys.size;
FRONT_SECTIONS[1].left.y = config.special_keys.offset;
FRONT_SECTIONS[1].right.x = WIDTH - config.special_keys.offset;
FRONT_SECTIONS[1].right.y = config.special_keys.offset + config.special_keys.size;
FRONT_SECTIONS[2].left.x = config.special_keys.offset;
FRONT_SECTIONS[2].left.y = HEIGHT - config.special_keys.offset - config.special_keys.size;
FRONT_SECTIONS[2].right.x = config.special_keys.offset + config.special_keys.size;
FRONT_SECTIONS[2].right.y = HEIGHT - config.special_keys.offset;
FRONT_SECTIONS[3].left.x = WIDTH - config.special_keys.offset - config.special_keys.size;
FRONT_SECTIONS[3].left.y = HEIGHT - config.special_keys.offset - config.special_keys.size;
FRONT_SECTIONS[3].right.x = WIDTH - config.special_keys.offset;
FRONT_SECTIONS[3].right.y = HEIGHT - config.special_keys.offset;
mouse_multiplier = 1 + (0.01 * config.mouse_acceleration);
}
void vitainput_start(void) {
active_input_thread = true;
}
void vitainput_stop(void) {
active_input_thread = false;
}