forked from MERLev/remaPSV2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·1886 lines (1702 loc) · 63.6 KB
/
main.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
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stddef.h>
#include <vitasdk.h>
#include <taihen.h>
#include <psp2/motion.h>
#include <libk/string.h>
#include <libk/stdio.h>
#include <stdlib.h>
#include <taipool.h>
#include "renderer.h"
#define VERSION 2
#define SUBVERSION 0
#define HOOKS_NUM 17 // Hooked functions num
#define PHYS_BUTTONS_NUM 16 // Supported physical buttons num
#define TARGET_REMAPS 42 // Supported target remaps num
#define BUTTONS_NUM 38 // Supported buttons num
#define MENU_MODES 9 // Menu modes num
#define LONG_PRESS_TIME 350000 //0.35sec
#define COLOR_DEFAULT 0x00FFFFFF
#define COLOR_HEADER 0x00FF00FF
#define COLOR_CURSOR 0x0000FF00
#define COLOR_ACTIVE 0x0000FFFF
#define COLOR_DISABLE 0x000000FF
#define CHA_W 12 //Character size in pexels
#define CHA_H 20
#define L_0 5 //Left margin for text
#define L_1 18
#define L_2 36
#define BUFFERS_NUM 64
#define ANALOGS_DEADZONE_DEF 30
#define ANALOGS_FORCE_DIGITAL_DEF 0
#define ANOLOGS_OPTIONS_NUM 8
#define GYRO_SENS_DEF 127
#define GYRO_WHEEL_DEF 0
#define GYRO_DEADZONE_DEF 15
#define GYRO_OPTIONS_NUM 8
#define MULTITOUCH_FRONT_NUM 6
#define MULTITOUCH_REAR_NUM 4
#define TOUCH_OPTIONS_NUM 18
#define TOUCH_MODE_DEF 1
#define CNTRL_OPTIONS_NUM 3
#define SETTINGS_NUM 4
#define CREDITS_NUM 16
#ifndef max
# define max(a,b) (((a)>(b))?(a):(b))
# define min(a,b) (((a)<(b))?(a):(b))
# define lim(a,b,c) (((a)>(b))?(((a)<(c))?(a):(c)):(b))
#endif
static const uint16_t TOUCH_POINTS_DEF[16] = {
600, 272, //Front TL
1280, 272, // TR
600, 816, // BL
1280, 816, // BR
600, 272, //Rear TL
1280, 272, // TR
600, 608, // BL
1280, 608 // BR
};
static const uint8_t CNTRL_DEF[CNTRL_OPTIONS_NUM] = {
0, 0, 0
};
static const uint8_t SETTINGS_DEF[SETTINGS_NUM] = {
4, 3, //Opening keys
1, //Autosave game profile
0 //Startup delay
};
enum{
MAIN_MENU = 0,
REMAP_MENU,
ANALOG_MENU,
TOUCH_MENU,
GYRO_MENU,
CNTRL_MENU,
FUNCS_LIST,
SETTINGS_MENU,
CREDITS_MENU
};
enum{
POSITIVE = 0, NEGATIVE
};
enum{
PEEK = 0, READ
};
static uint8_t btn_mask[BUTTONS_NUM];
static uint8_t analogs_options[ANOLOGS_OPTIONS_NUM];
static uint8_t gyro_options[GYRO_OPTIONS_NUM];
static uint16_t touch_options[TOUCH_OPTIONS_NUM];
static uint8_t controller_options[CNTRL_OPTIONS_NUM];
static uint8_t settings_options[SETTINGS_NUM];
static uint8_t used_funcs[HOOKS_NUM];
//Circular cache to store remapped keys buffers per each ctrs hook
static SceCtrlData *remappedBuffers[HOOKS_NUM-5][BUFFERS_NUM];
static int remappedBuffersSizes[HOOKS_NUM];
static int remappedBuffersIdxs[HOOKS_NUM];
//Circular cache to store Touch buffers per each touch hook
static SceTouchData *remappedBuffersFront[4][BUFFERS_NUM];
static int remappedBuffersFrontSizes[4];
static int remappedBuffersFrontIdxs[4];
static SceTouchData *remappedBuffersRear[4][BUFFERS_NUM];
static int remappedBuffersRearSizes[4];
static int remappedBuffersRearIdxs[4];
uint8_t newEmulatedFrontTouchBuffer = 0;
uint8_t newEmulatedRearTouchBuffer = 0;
typedef struct EmulatedTouch{
SceTouchReport reports[MULTITOUCH_FRONT_NUM];
uint8_t num;
}EmulatedTouch;
EmulatedTouch etFront, etRear, prevEtFront, prevEtRear;
static uint8_t etFrontIdCounter = 64;
static uint8_t etRearIdCounter = 64;
static uint16_t TOUCH_SIZE[4] = {
1920, 1088, //Front
1919, 890 //Rear
};
static uint8_t delayedStartDone = 0;
static uint64_t startTick;
static uint8_t new_frame = 1;
static int screen_h = 272;
static int screen_w = 480;
static uint32_t ticker;
static uint8_t show_menu = 0;
static int cfg_i = 0;
static int menu_i = MAIN_MENU;
static uint32_t curr_buttons;
static uint32_t old_buttons;
static uint64_t tick;
static uint64_t pressedTicks[PHYS_BUTTONS_NUM];
static uint8_t current_hook = 0;
static SceUID hooks[HOOKS_NUM];
static tai_hook_ref_t refs[HOOKS_NUM];
static int model;
static char titleid[16];
static uint8_t internal_touch_call = 0;
static uint8_t internal_ext_call = 0;
static char fname[128];
static char* str_menus[MENU_MODES] = {
"MAIN MENU",
"REMAP MENU",
"ANALOG MENU",
"TOUCH MENU",
"GYRO_MENU",
"CONNECTED CONTROLLERS",
"USED FUNCTIONS",
"SETTINGS",
"CREDITS"
};
static char* str_main_menu[] = {
"Change remap settings",
"Change analog remap settings",
"Change touch remap settings",
"Change gyro remap settings",
"Setup external gamepads",
"Show imported functions",
"Settings",
"Credits",
"Return to the game"
};
static char* str_credits[CREDITS_NUM] = {
"Thanks to ",
"Tain Sueiras, nobodywasishere and RaveHeart",
"for their awesome support on Patreon",
"",
"Special thanks to",
" S1ngyy, for his analogs emulation code",
" pablojrl123, for ",
" customizable buttons activation code",
" Cassie, W0lfwang and TheIronUniverse",
" for enduring endless crashes",
" while testing this thing",
" Vita Nuova community",
" for all the help and support I got there",
"",
"Created by Rinnegatamante",
" Edits by Mer1e"
};
static char* str_yes_no[] = {
"No", "Yes"
};
static char* str_settings[] = {
"Save as Game profile", "Load Game profile", "Save as Global profile", "Load Global profile"
};
static char* str_funcs[HOOKS_NUM-1] = {
"sceCtrlPeekBufferPositive",
"sceCtrlPeekBufferPositive2",
"sceCtrlReadBufferPositive",
"sceCtrlReadBufferPositive2",
"sceCtrlPeekBufferPositiveExt",
"sceCtrlPeekBufferPositiveExt2",
"sceCtrlReadBufferPositiveExt",
"sceCtrlReadBufferPositiveExt2",
"sceCtrlPeekBufferNegative",
"sceCtrlPeekBufferNegative2",
"sceCtrlReadBufferNegative",
"sceCtrlReadBufferNegative2",
"sceTouchRead",
"sceTouchRead2",
"sceTouchPeek",
"sceTouchPeek2"
};
static uint32_t btns[PHYS_BUTTONS_NUM] = {
SCE_CTRL_CROSS, SCE_CTRL_CIRCLE, SCE_CTRL_TRIANGLE, SCE_CTRL_SQUARE,
SCE_CTRL_START, SCE_CTRL_SELECT, SCE_CTRL_LTRIGGER, SCE_CTRL_RTRIGGER,
SCE_CTRL_UP, SCE_CTRL_RIGHT, SCE_CTRL_LEFT, SCE_CTRL_DOWN, SCE_CTRL_L1,
SCE_CTRL_R1, SCE_CTRL_L3, SCE_CTRL_R3
};
static char* str_btns[PHYS_BUTTONS_NUM] = {
"Cross", "Circle", "Triangle", "Square",
"Start", "Select",
"LT/L2", "RT/R2",
"Up", "Right", "Left", "Down",
"L1", "R1", "L3", "R3"
};
static char* str_sections[] = {
"Buttons", "LStick", "RStick",
"FrontTouch", "RearTouch", "Gyroscope",
"","Disabled"
};
static char* str_analog_directions[] = {
"Left", "Right", "Up", "Down"
};
static char* str_touch_zones[] = {
"TopLeft", "TopRight", "BotLeft", "BotRight"
};
static char* str_gyro_directions[] = {
"Left", "Right", "Up", "Down",
"Roll Left","Roll Right"
};
static char* str_touch_points[] = {
"Point A", "Point B", "Point C", "Point D"
};
// Generic clamp function
int32_t clamp(int32_t value, int32_t mini, int32_t maxi) {
if (value < mini) { return mini; }
if (value > maxi) { return maxi; }
return value;
}
// Reset options per-menu
void resetRemapsOptions(){
for (int i = 0; i < BUTTONS_NUM; i++) {
btn_mask[i] = PHYS_BUTTONS_NUM;
}
}
void resetAnalogsOptions(){
for (int i = 0; i < ANOLOGS_OPTIONS_NUM; i++)
analogs_options[i] = i < 4 ? ANALOGS_DEADZONE_DEF : ANALOGS_FORCE_DIGITAL_DEF;
}
void resetTouchOptions(){
for (int i = 0; i < TOUCH_OPTIONS_NUM - 2; i++)
touch_options[i] = TOUCH_POINTS_DEF[i];
touch_options[16] = TOUCH_MODE_DEF;
touch_options[17] = TOUCH_MODE_DEF;
}
void resetGyroOptions() {
for (int i = 0; i < GYRO_OPTIONS_NUM; i++)
if (i < GYRO_OPTIONS_NUM - 2) {
gyro_options[i] = i < 3 ? GYRO_SENS_DEF : GYRO_DEADZONE_DEF;
}
else gyro_options[i] = GYRO_WHEEL_DEF;
}
void resetCntrlOptions(){
for (int i = 0; i < CNTRL_OPTIONS_NUM; i++)
controller_options[i] = CNTRL_DEF[i];
}
void resetSettingsOptions(){
for (int i = 0; i < SETTINGS_NUM; i++)
settings_options[i] = SETTINGS_DEF[i];
}
char* getControllerName(int id){
if (id == SCE_CTRL_TYPE_UNPAIRED) return "Unpaired controller";
else if (id == SCE_CTRL_TYPE_PHY) return "Physical controller for VITA";
else if (id == SCE_CTRL_TYPE_VIRT) return "Virtual controller for PSTV";
else if (id == SCE_CTRL_TYPE_DS3) return "DualShock 3";
else if (id == SCE_CTRL_TYPE_DS4) return "DualShock 4";
else return "Unknown controller";
}
//Calculate starting index for scroll menu
int calcStartingIndex(int idx, int entriesNum, int screenEntries){
int bottom_l = 3;
int ret = max(0, idx - (screenEntries - bottom_l));
while (ret > 0 && (entriesNum - ret - 2) < screenEntries) ret--;
return ret;
}
// Config Menu Renderer
void drawConfigMenu() {
char* _blank = " ";
//DRAW HEADER
drawStringF(0, 0, _blank);
drawStringF(0, CHA_H, _blank);
setTextColor(COLOR_HEADER);
drawStringF(L_0, 10, "remaPSV2 v.%hhu.%hhu %s", VERSION, SUBVERSION, str_menus[menu_i]);
drawString(screen_w - CHA_W*strlen(titleid) - 10, 10, titleid);
//DRAW MENU
uint8_t slim_mode = 0;//Mode for low res framebuffers;
if (screen_w < 850)
slim_mode = 1;
int i, y = CHA_H;
int screen_entries = ((float)screen_h - 10) / CHA_H - 1;
int avaliable_entries = screen_entries - 4 - (slim_mode ? 1 : 0);
char *footer1 ="", *footer2="";
switch (menu_i){
case MAIN_MENU:
for (i = calcStartingIndex(cfg_i, sizeof(str_main_menu)/sizeof(char*), avaliable_entries); i < sizeof(str_main_menu)/sizeof(char*); i++) {
if (cfg_i == i){//Draw cursor
setTextColor(COLOR_CURSOR);
drawString(L_0, y + CHA_H, (ticker % 16 < 8) ? "x" : "X");
}
setTextColor((i == cfg_i) ? COLOR_CURSOR : COLOR_DEFAULT);
drawStringF(L_1, y += CHA_H, "%s", str_main_menu[i]);
if (y + 40 > screen_h) break;
}
footer1 = "(X):select";
footer2 = "(O):close";
break;
case REMAP_MENU:
for (i = calcStartingIndex(cfg_i, BUTTONS_NUM, avaliable_entries); i < BUTTONS_NUM; i++) {
if (cfg_i == i){//Draw cursor
setTextColor(COLOR_CURSOR);
drawString(L_0 + CHA_W*10, y + CHA_H, (ticker % 16 < 8) ? "<" : ">");
}
char *srcSection = "", *srcAction = "", *targetSection = "", *targetAction = "";
//Source section
if (i == 0) srcSection = str_sections[0];
else if (i == PHYS_BUTTONS_NUM) srcSection = str_sections[3];
else if (i == PHYS_BUTTONS_NUM + 4) srcSection = str_sections[4];
else if (i == PHYS_BUTTONS_NUM + 8) srcSection = str_sections[1];
else if (i == PHYS_BUTTONS_NUM + 12) srcSection = str_sections[2];
else if (i == PHYS_BUTTONS_NUM + 16) srcSection = str_sections[5];
//Source Action
if (i < PHYS_BUTTONS_NUM) srcAction = str_btns[i];
else if (i < PHYS_BUTTONS_NUM + 4) srcAction = str_touch_zones[i - PHYS_BUTTONS_NUM];
else if (i < PHYS_BUTTONS_NUM + 8) srcAction = str_touch_zones[i - PHYS_BUTTONS_NUM-4];
else if (i < PHYS_BUTTONS_NUM + 12) srcAction = str_analog_directions[i - PHYS_BUTTONS_NUM-8];
else if (i < PHYS_BUTTONS_NUM + 16) srcAction = str_analog_directions[i - PHYS_BUTTONS_NUM-12];
else if (i < PHYS_BUTTONS_NUM + 22) srcAction = str_gyro_directions[i - PHYS_BUTTONS_NUM-16];
//Target Section
if (btn_mask[i] < PHYS_BUTTONS_NUM) targetSection = str_sections[0];
else if (btn_mask[i] == PHYS_BUTTONS_NUM) targetSection = str_sections[6];
else if (btn_mask[i] == PHYS_BUTTONS_NUM + 1) targetSection = str_sections[7];
else if (btn_mask[i] < PHYS_BUTTONS_NUM + 6) targetSection = str_sections[1];
else if (btn_mask[i] < PHYS_BUTTONS_NUM + 10) targetSection = str_sections[2];
else if (btn_mask[i] < PHYS_BUTTONS_NUM + 18) targetSection = str_sections[3];
else if (btn_mask[i] < PHYS_BUTTONS_NUM + 26) targetSection = str_sections[4];
//Target Action
if (btn_mask[i] < PHYS_BUTTONS_NUM) targetAction = str_btns[btn_mask[i]];
else if (btn_mask[i] < PHYS_BUTTONS_NUM + 2) targetAction = "";
else if (btn_mask[i] < PHYS_BUTTONS_NUM + 6) targetAction = str_analog_directions[btn_mask[i] - PHYS_BUTTONS_NUM-2];
else if (btn_mask[i] < PHYS_BUTTONS_NUM + 10) targetAction = str_analog_directions[btn_mask[i] - PHYS_BUTTONS_NUM-6];
else if (btn_mask[i] < PHYS_BUTTONS_NUM + 14) targetAction = str_touch_zones[btn_mask[i] - PHYS_BUTTONS_NUM-10];
else if (btn_mask[i] < PHYS_BUTTONS_NUM + 18) targetAction = str_touch_points[btn_mask[i] - PHYS_BUTTONS_NUM-14];
else if (btn_mask[i] < PHYS_BUTTONS_NUM + 22) targetAction = str_touch_zones[btn_mask[i] - PHYS_BUTTONS_NUM-18];
else if (btn_mask[i] < PHYS_BUTTONS_NUM + 26) targetAction = str_touch_points[btn_mask[i] - PHYS_BUTTONS_NUM-22];
setTextColor(COLOR_HEADER);
drawString(L_0, y += CHA_H, srcSection);
if (i == cfg_i) setTextColor(COLOR_CURSOR);
else if (btn_mask[i] == PHYS_BUTTONS_NUM) setTextColor(COLOR_DEFAULT);
else if (btn_mask[i] == PHYS_BUTTONS_NUM + 1) setTextColor(COLOR_DISABLE);
else setTextColor(COLOR_ACTIVE);
drawString(L_0 + CHA_W*11, y, srcAction);
if (btn_mask[i] == PHYS_BUTTONS_NUM) setTextColor(COLOR_DEFAULT);
else if (btn_mask[i] == PHYS_BUTTONS_NUM + 1) setTextColor(COLOR_DISABLE);
else setTextColor(COLOR_ACTIVE);
if (btn_mask[i] != PHYS_BUTTONS_NUM)
drawString(L_0 + CHA_W*19, y, " -> ");
drawString(L_0 + CHA_W*23, y, targetSection);
drawString(L_0 + CHA_W*34, y, targetAction);
if (y + 60 > screen_h) break;
}
setTextColor(COLOR_HEADER);
footer1 = "(<)(>):change (LT)(RT):section ([]):reset";
footer2 = " (start):reset all (O):back";
break;
case ANALOG_MENU:
for (i = calcStartingIndex(cfg_i, ANOLOGS_OPTIONS_NUM, avaliable_entries); i < ANOLOGS_OPTIONS_NUM; i++) {
if (y + 60 > screen_h) break;
if (cfg_i == i){//Draw cursor
setTextColor(COLOR_CURSOR);
drawString(L_0 + 26*CHA_W, y + CHA_H, (ticker % 16 < 8) ? "<" : ">");
}
if (!(i % 4)){ //Headers
setTextColor(COLOR_HEADER);
drawString(L_0, y+CHA_H, (i == 0) ? "Deadzone" : "Force digital");
}
if (i == cfg_i) setTextColor(COLOR_CURSOR);
else if (analogs_options[i] != ((i/2*2 < 4) ? ANALOGS_DEADZONE_DEF : ANALOGS_FORCE_DIGITAL_DEF))
setTextColor(COLOR_ACTIVE);
else setTextColor(COLOR_DEFAULT);
drawStringF(L_0+14*CHA_W, y+=CHA_H, "%s",
!(i % 2) ? (((i / 2) % 2 ) ? "Right Analog" : "Left Analog "): "");
if (i < 4)
drawStringF(L_0+27*CHA_W, y, "[%s axis]: %hhu",
(i % 2) ? "Y" : "X",
analogs_options[i]);
else
drawStringF(L_0+27*CHA_W, y, "[%s axis]: %s",
(i % 2) ? "Y" : "X",
str_yes_no[analogs_options[i]]);
}
footer1 = "(<)(>):change ([]):reset (start):reset all" ;
footer2 = "(O):back";
break;
case TOUCH_MENU:
for (i = calcStartingIndex(cfg_i, TOUCH_OPTIONS_NUM, avaliable_entries); i < TOUCH_OPTIONS_NUM; i++) {
if (y + 60 > screen_h) break;
if (cfg_i == i){//Draw cursor
setTextColor(COLOR_CURSOR);
drawString(L_0+ ((i<16) ? 16*CHA_W : 32*CHA_W), y + CHA_H, (ticker % 16 < 8) ? "<" : ">");
}
if (i == 0 || i == 8){ //Headers
setTextColor(COLOR_HEADER);
drawString(L_0, y+CHA_H, (i == 0) ? "Front" : "Rear");
}
if (i < 16){ //Points
if (i == cfg_i) setTextColor(COLOR_CURSOR);
else if (touch_options[i] != TOUCH_POINTS_DEF[i])
setTextColor(COLOR_ACTIVE);
else setTextColor(COLOR_DEFAULT);
if (i < 16){
if (!(i % 2))
drawString(L_0+6*CHA_W, y+CHA_H, str_touch_points[(i % 8)/2]);
drawStringF(L_0+14*CHA_W, y+=CHA_H, "%s:", !(i % 2) ? "x" : "y");
drawStringF(L_0+17*CHA_W, y, "%hu", touch_options[i]);
}
if (y + 60 > screen_h) break;
}
if (i == 16){ //Front touch mode
if (16 == cfg_i) setTextColor(COLOR_CURSOR);
else if (touch_options[16] == TOUCH_MODE_DEF) setTextColor(COLOR_DEFAULT);
else setTextColor(COLOR_ACTIVE);
drawString(L_0, y+=CHA_H, "Disable Front touch if remapped:");
drawString(L_0+33*CHA_W, y, str_yes_no[touch_options[16]]);
if (y + 60 > screen_h) break;
}
if (i==17){ //Rear touch mode
if (17 == cfg_i) setTextColor(COLOR_CURSOR);
else if (touch_options[17] == TOUCH_MODE_DEF) setTextColor(COLOR_DEFAULT);
else setTextColor(COLOR_ACTIVE);
drawString(L_0, y+=CHA_H, "Disable Rear touch if remapped:");
drawString(L_0+33*CHA_W, y, str_yes_no[touch_options[17]]);
if (y + 60 > screen_h) break;
}
}
footer1 = "(<)(>)[TOUCH](RS):change ([]):reset (start):reset all";
footer2 = "(O): back";
break;
case GYRO_MENU:
for (i = calcStartingIndex(cfg_i, GYRO_OPTIONS_NUM, avaliable_entries); i < GYRO_OPTIONS_NUM; i++) {
if (y + 60 > screen_h) break;
if (cfg_i == i) {//Draw cursor
setTextColor(COLOR_CURSOR);
drawString(L_0 + 17 * CHA_W, y + CHA_H, (ticker % 16 < 8) ? "<" : ">");
}
if (!(i % 3)) { //Headers
setTextColor(COLOR_HEADER);
drawString(L_0, y + CHA_H, (i == 0) ? "Sensivity" : (i == 3) ? "Deadzone" : "Mode");
}
if (i == cfg_i) setTextColor(COLOR_CURSOR);
else if (gyro_options[i] != ((i < 3) ? GYRO_SENS_DEF : (i == GYRO_OPTIONS_NUM - 2) ? GYRO_WHEEL_DEF : GYRO_DEADZONE_DEF))
setTextColor(COLOR_ACTIVE);
else setTextColor(COLOR_DEFAULT);
if (i < GYRO_OPTIONS_NUM - 2) {
drawStringF(L_0 + 10 * CHA_W, y += CHA_H, "%s axis:",
((i % 3) == 2) ? "Z" : ((i % 3) ? "Y" : "X"));
drawStringF(L_0 + 18 * CHA_W, y, "%hhu", gyro_options[i]);
}
else if (i == GYRO_OPTIONS_NUM - 2) {
drawString(L_0 + 10 * CHA_W, y += CHA_H, "Wheel:");
drawString(L_0 + 18 * CHA_W, y, str_yes_no[gyro_options[i]]);
}
else if (i == GYRO_OPTIONS_NUM - 1) {
drawString(L_0 + 10 * CHA_W, y += CHA_H, "Reset");
}
}
footer1 = "(<)(>):change ([]):reset (start):reset all";
footer2 = "(O): back";
break;
case CNTRL_MENU:;
SceCtrlPortInfo pi;
int res = sceCtrlGetControllerPortInfo(&pi);
if (res != 0){//Should not ever trigger
setTextColor(COLOR_DISABLE);
drawString(L_1, y+= CHA_H, "Error getting controllers info");
} else {
//Cursor
setTextColor(COLOR_CURSOR);
drawString(L_0, y + CHA_H + CHA_H * cfg_i, (ticker % 16 < 8) ? "<" : ">");
//Use external controller
setTextColor(cfg_i == 0 ? COLOR_CURSOR :
(controller_options[0] == CNTRL_DEF[0] ? COLOR_DEFAULT : COLOR_ACTIVE));
drawStringF(L_1, y += CHA_H, "Use external controller: %s", str_yes_no[controller_options[0]]);
//Port selection
setTextColor(cfg_i == 1 ? COLOR_CURSOR :
(controller_options[1] == CNTRL_DEF[1] ? COLOR_DEFAULT : COLOR_ACTIVE));
drawStringF(L_1, y += CHA_H, "Selected port: {%i} %s %s",
controller_options[1],
getControllerName(pi.port[controller_options[1]]),
controller_options[1] ? "" : "[DEFAULT]");
//Button swap
setTextColor(cfg_i == 2 ? COLOR_CURSOR :
(controller_options[2] == CNTRL_DEF[2] ? COLOR_DEFAULT : COLOR_ACTIVE));
drawStringF(L_1, y += CHA_H, "Swap L1<>LT R1<>RT : %s", str_yes_no[controller_options[2]]);
//Ports stats
y+=CHA_H;
setTextColor(COLOR_DEFAULT);
drawString(L_1, y+= CHA_H, "Detected controllers:");
for (int i = max(0, cfg_i - (avaliable_entries + 1)); i < 5; i++){
setTextColor((L_1 == cfg_i) ? COLOR_CURSOR : ((pi.port[i] != SCE_CTRL_TYPE_UNPAIRED) ? COLOR_ACTIVE : COLOR_DEFAULT));
drawStringF(L_1, y += CHA_H, "Port %i: %s", i, getControllerName(pi.port[i]));
if (y + 40 > screen_h) break;
}
}
footer1 = "(<)(>):change ([]):reset (start):reset all";
footer2 = "(O): back";
break;
case FUNCS_LIST:
for (i = calcStartingIndex(cfg_i, HOOKS_NUM - 1, avaliable_entries); i < HOOKS_NUM - 1; i++) {
if (cfg_i == i){//Draw cursor
setTextColor(COLOR_CURSOR);
drawString(L_0, y + CHA_H, "-");
}
setTextColor((i == cfg_i) ? COLOR_CURSOR : (used_funcs[i] ? COLOR_ACTIVE : COLOR_DEFAULT));
drawStringF(L_1, y += CHA_H, "%s : %s", str_funcs[i], used_funcs[i] ? "Yes" : "No");
if (y + 40 > screen_h) break;
}
setTextColor(COLOR_HEADER);
footer2 = "(O):back";
break;
case SETTINGS_MENU:;
//Cursor
setTextColor(COLOR_CURSOR);
if (cfg_i <= 2)
drawString(L_0, y + CHA_H + CHA_H * cfg_i, (ticker % 16 < 8) ? "<" : ">");
else
drawString(L_0, y + CHA_H + CHA_H * cfg_i, (ticker % 16 < 8) ? "x" : "X");
//Menu trigger keys
setTextColor(cfg_i == 0 ? COLOR_CURSOR :
(settings_options[0] == SETTINGS_DEF[0] ? COLOR_DEFAULT : COLOR_ACTIVE));
drawStringF(L_1, y += CHA_H, "Menu trigger first key : %s",
str_btns[settings_options[0]]);
setTextColor(cfg_i == 1 ? COLOR_CURSOR :
(settings_options[1] == SETTINGS_DEF[1] ? COLOR_DEFAULT : COLOR_ACTIVE));
drawStringF(L_1, y += CHA_H, " second key : %s",
str_btns[settings_options[1]]);
//Save game profile on close
setTextColor(cfg_i == 2 ? COLOR_CURSOR :
(settings_options[2] == SETTINGS_DEF[2] ? COLOR_DEFAULT : COLOR_ACTIVE));
drawStringF(L_1, y += CHA_H, "Save Game profile on close: %s", str_yes_no[settings_options[2]]);
//Startup delay
setTextColor(cfg_i == 3 ? COLOR_CURSOR :
(settings_options[3] == SETTINGS_DEF[3] ? COLOR_DEFAULT : COLOR_ACTIVE));
drawStringF(L_1, y += CHA_H, "Startup delay : %hhu seconds", settings_options[3]);
//Profile management
for (int i = 0; i < sizeof(str_settings)/sizeof(char*); i++){
setTextColor((cfg_i == (4 + i)) ? COLOR_CURSOR : COLOR_DEFAULT);
drawString(L_1, y += CHA_H, str_settings[i]);
}
//Footer
footer1 = "(<)(>):change ([]):reset (start):reset all";
footer2 = "(O): back";
break;
case CREDITS_MENU:
//y+=CHA_H;
for (i = calcStartingIndex(cfg_i, CREDITS_NUM, avaliable_entries); i < CREDITS_NUM; i++) {
if (cfg_i == i){//Draw cursor
setTextColor(COLOR_CURSOR);
drawString(L_0, y + CHA_H, "-");
}
setTextColor(COLOR_DEFAULT);
drawStringF(L_2, y += CHA_H, "%s", str_credits[i]);
if (y + 40 > screen_h) break;
}
footer2 = "(O):back";
break;
default:
break;
}
//DRAW FOOTER
setTextColor(COLOR_HEADER);
if (!slim_mode){
drawStringF(0, screen_h-CHA_H*1.5, _blank);
drawStringF(0, screen_h-CHA_H, _blank);
drawStringF(0, screen_h-CHA_H, _blank);
drawStringF(10, screen_h-CHA_H, footer1);
drawStringF(screen_w - CHA_W*strlen(footer2), screen_h-CHA_H, footer2);
} else {
drawStringF(0, screen_h-CHA_H*2.5, _blank);
drawStringF(0, screen_h-CHA_H*2, _blank);
drawStringF(0, screen_h-CHA_H, _blank);
drawStringF(10, screen_h-CHA_H*2, footer1);
drawStringF(screen_w - CHA_W*strlen(footer2) - 10, screen_h-CHA_H, footer2);
}
//DRAW TOUCH POINTER over everything else
if (menu_i != TOUCH_MENU || cfg_i >= 16)
return;
int left = touch_options[cfg_i - (cfg_i % 2)] - 8;
left *= (float)screen_w / ((cfg_i < 8) ? TOUCH_SIZE[0] : TOUCH_SIZE[2]);
left = min((max(0, left)), screen_w);
int top = touch_options[cfg_i - (cfg_i % 2) + 1] - 10;
top *= (float)screen_h / ((cfg_i < 8) ? TOUCH_SIZE[1] : TOUCH_SIZE[3]); //Scale to framebuffer size
top = min((max(0, top)), screen_h);//limit into screen
setTextColor((ticker % 4) ? COLOR_CURSOR : COLOR_DISABLE);
drawString(left, top, (ticker % 2) ? "" : "@");
}
void storeTouchPoint(EmulatedTouch *et, uint16_t x, uint16_t y){
for (int i = 0; i < et->num; i++)
if (et->reports[i].x == x && et->reports[i].y == y)
return;
et->reports[et->num].x = x;
et->reports[et->num].y = y;
et->num++;
}
//Anything -> Btn, Analog, Touch
void applyRemapRule(uint8_t btn_idx, uint32_t* map, uint32_t* stickpos) {
if (btn_mask[btn_idx] < PHYS_BUTTONS_NUM) { // -> Btn
if (!(*map & btns[btn_mask[btn_idx]])) {
*map += btns[btn_mask[btn_idx]];
}
} else if (btn_mask[btn_idx] == PHYS_BUTTONS_NUM) { // -> Original
if (btn_idx < PHYS_BUTTONS_NUM) {
if (!(*map & btns[btn_idx])) {
*map += btns[btn_idx];
}
} // -> Analog
} else if (PHYS_BUTTONS_NUM + 1 < btn_mask[btn_idx] && btn_mask[btn_idx] < PHYS_BUTTONS_NUM + 10) {
stickpos[btn_mask[btn_idx] - (PHYS_BUTTONS_NUM + 2)] += 127;
} else if (btn_mask[btn_idx] < PHYS_BUTTONS_NUM + 24){ // -> Touch
if (btn_mask[btn_idx] < PHYS_BUTTONS_NUM + 14){ //Front touch default
if (etFront.num == MULTITOUCH_FRONT_NUM) return;
storeTouchPoint(&etFront,
TOUCH_POINTS_DEF[(btn_mask[btn_idx] - (PHYS_BUTTONS_NUM + 10)) * 2],
TOUCH_POINTS_DEF[(btn_mask[btn_idx] - (PHYS_BUTTONS_NUM + 10)) * 2 + 1]);
} else if (btn_mask[btn_idx] < PHYS_BUTTONS_NUM + 18){ //Front touch custom
if (etFront.num == MULTITOUCH_FRONT_NUM) return;
storeTouchPoint(&etFront,
touch_options[(btn_mask[btn_idx] - (PHYS_BUTTONS_NUM + 14)) * 2],
touch_options[(btn_mask[btn_idx] - (PHYS_BUTTONS_NUM + 14)) * 2 + 1]);
} else if (btn_mask[btn_idx] < PHYS_BUTTONS_NUM + 22){ //Rear touch default
if (etRear.num == MULTITOUCH_REAR_NUM) return;
storeTouchPoint(&etRear,
TOUCH_POINTS_DEF[8 + (btn_mask[btn_idx] - (PHYS_BUTTONS_NUM + 18)) * 2],
TOUCH_POINTS_DEF[8 + (btn_mask[btn_idx] - (PHYS_BUTTONS_NUM + 18)) * 2 + 1]);
} else if (btn_mask[btn_idx] < PHYS_BUTTONS_NUM + 26){ //Rear touch custom
if (etRear.num == MULTITOUCH_REAR_NUM) return;
storeTouchPoint(&etRear,
touch_options[8 + (btn_mask[btn_idx] - (PHYS_BUTTONS_NUM + 22)) * 2],
touch_options[8 + (btn_mask[btn_idx] - (PHYS_BUTTONS_NUM + 22)) * 2 + 1]);
}
}
}
//Used to handle analog->analog mapping
void applyRemapRuleForAnalog(uint8_t btn_idx, uint32_t* map, uint32_t* stickpos, uint8_t stickposval) {
if (PHYS_BUTTONS_NUM + 1 < btn_mask[btn_idx] && btn_mask[btn_idx] < PHYS_BUTTONS_NUM + 10
&& !analogs_options[4 + (int) ((btn_idx - PHYS_BUTTONS_NUM - 8) / 2)]) {
// Analog -> Analog [ANALOG MODE]
stickpos[btn_mask[btn_idx] - (PHYS_BUTTONS_NUM + 2)] += 127 - stickposval;
} else {
// Analog -> Analog [DIGITAL MODE] and Analog -> Button
applyRemapRule(btn_idx, map, stickpos);
}
}
//Used to handle analog->gyro mapping
void applyRemapRuleForGyro(uint8_t btn_idx, uint32_t* map, uint32_t* stickpos, float gyroval){
if (PHYS_BUTTONS_NUM + 1 < btn_mask[btn_idx] && btn_mask[btn_idx] < PHYS_BUTTONS_NUM + 10) {
// Gyro -> Analog remap
stickpos[btn_mask[btn_idx] - (PHYS_BUTTONS_NUM + 2)] = stickpos[btn_mask[btn_idx] - (PHYS_BUTTONS_NUM + 2)] + clamp(gyroval, -127, 127);
} else {
// Gyro -> Btn remap
if ((((btn_idx == PHYS_BUTTONS_NUM + 16 || btn_idx == PHYS_BUTTONS_NUM + 17)) && gyroval > gyro_options[3] * 10) ||
(((btn_idx == PHYS_BUTTONS_NUM + 18 || btn_idx == PHYS_BUTTONS_NUM + 19)) && gyroval > gyro_options[4] * 10) ||
(((btn_idx == PHYS_BUTTONS_NUM + 20 || btn_idx == PHYS_BUTTONS_NUM + 21)) && gyroval > gyro_options[5] * 10))
applyRemapRule(btn_idx, map, stickpos);
}
}
void applyRemap(SceCtrlData *ctrl) {
// Gathering real touch data
SceTouchData front, rear;
internal_touch_call = 1;
sceTouchPeek(SCE_TOUCH_PORT_FRONT, &front, 1);
sceTouchPeek(SCE_TOUCH_PORT_BACK, &rear, 1);
internal_touch_call = 0;
// Gathering gyro data
SceMotionState motionstate;
sceMotionGetState(&motionstate);
// Applying remap rules for physical buttons
int i;
uint32_t new_map = 0;
uint32_t stickpos[8] = { };
for (i=0;i<PHYS_BUTTONS_NUM;i++) {
if (ctrl->buttons & btns[i]) applyRemapRule(i, &new_map, stickpos);
}
// Applying remap rules for front virtual buttons
int left = TOUCH_SIZE[0]/2; int top = TOUCH_SIZE[1]/2;
for (i=0;i<front.reportNum;i++) {
if (front.report[i].x > left && front.report[i].y > top) // Bot Right
applyRemapRule(PHYS_BUTTONS_NUM + 3, &new_map, stickpos);
else if (front.report[i].x <= left && front.report[i].y > top) // Bot Left
applyRemapRule(PHYS_BUTTONS_NUM + 2, &new_map, stickpos);
else if (front.report[i].x > left && front.report[i].y <= top) // Top Right
applyRemapRule(PHYS_BUTTONS_NUM + 1, &new_map, stickpos);
else if (front.report[i].x <= left && front.report[i].y <= top)// Top Left
applyRemapRule(PHYS_BUTTONS_NUM, &new_map, stickpos);
}
// Applying remap rules for rear virtual buttons
left = TOUCH_SIZE[2]/2; top = TOUCH_SIZE[3]/2;
for (i=0;i<rear.reportNum;i++) {
if (rear.report[i].x > left && rear.report[i].y > top) // Bot Right
applyRemapRule(PHYS_BUTTONS_NUM + 7, &new_map, stickpos);
else if (rear.report[i].x <= left && rear.report[i].y > top) // Bot Left
applyRemapRule(PHYS_BUTTONS_NUM + 6, &new_map, stickpos);
else if (rear.report[i].x > left && rear.report[i].y <= top) // Top Right
applyRemapRule(PHYS_BUTTONS_NUM + 5, &new_map, stickpos);
else if (rear.report[i].x <= left && rear.report[i].y <= top) // Top Left
applyRemapRule(PHYS_BUTTONS_NUM + 4, &new_map, stickpos);
}
// Applying remap rules for left analog
if (ctrl->lx < 127 - analogs_options[0]) // Left
applyRemapRuleForAnalog(PHYS_BUTTONS_NUM + 8, &new_map, stickpos, ctrl->lx);
else if (ctrl->lx > 127 + analogs_options[0]) // Right
applyRemapRuleForAnalog(PHYS_BUTTONS_NUM + 9, &new_map, stickpos, 255 - ctrl->lx);
if (ctrl->ly < 127 - analogs_options[1]) // Up
applyRemapRuleForAnalog(PHYS_BUTTONS_NUM + 10, &new_map, stickpos, ctrl->ly);
else if (ctrl->ly > 127 + analogs_options[1]) // Down
applyRemapRuleForAnalog(PHYS_BUTTONS_NUM + 11, &new_map, stickpos, 255 - ctrl->ly);
// Applying remap rules for right analog
if (ctrl->rx < 127 - analogs_options[2]) // Left
applyRemapRuleForAnalog(PHYS_BUTTONS_NUM + 12, &new_map, stickpos, ctrl->rx);
else if (ctrl->rx > 127 + analogs_options[2]) // Right
applyRemapRuleForAnalog(PHYS_BUTTONS_NUM + 13, &new_map, stickpos, 255 - ctrl->rx);
if (ctrl->ry < 127 - analogs_options[3]) // Up
applyRemapRuleForAnalog(PHYS_BUTTONS_NUM + 14, &new_map, stickpos, ctrl->ry);
else if (ctrl->ry > 127 + analogs_options[3]) // Down
applyRemapRuleForAnalog(PHYS_BUTTONS_NUM + 15, &new_map, stickpos, 255 - ctrl->ry);
// Applying remap for gyro
if (gyro_options[6] == 0) {
if (motionstate.angularVelocity.y > 0)
applyRemapRuleForGyro(PHYS_BUTTONS_NUM + 16, &new_map, stickpos,
motionstate.angularVelocity.y * gyro_options[0]);
if (motionstate.angularVelocity.y < 0)
applyRemapRuleForGyro(PHYS_BUTTONS_NUM + 17, &new_map, stickpos,
-motionstate.angularVelocity.y * gyro_options[0]);
if (motionstate.angularVelocity.x > 0)
applyRemapRuleForGyro(PHYS_BUTTONS_NUM + 18, &new_map, stickpos,
motionstate.angularVelocity.x * gyro_options[1]);
if (motionstate.angularVelocity.x < 0)
applyRemapRuleForGyro(PHYS_BUTTONS_NUM + 19, &new_map, stickpos,
-motionstate.angularVelocity.x * gyro_options[1]);
if (motionstate.angularVelocity.z > 0)
applyRemapRuleForGyro(PHYS_BUTTONS_NUM + 20, &new_map, stickpos,
motionstate.angularVelocity.z * gyro_options[2]);
if (motionstate.angularVelocity.z < 0)
applyRemapRuleForGyro(PHYS_BUTTONS_NUM + 21, &new_map, stickpos,
-motionstate.angularVelocity.z * gyro_options[2]);
}
else {
// Applying remap for gyro wheel mode
if (motionstate.deviceQuat.y < 0)
applyRemapRuleForGyro(PHYS_BUTTONS_NUM + 16, &new_map, stickpos,
motionstate.deviceQuat.y * gyro_options[0] * 4);
if (motionstate.deviceQuat.y > 0)
applyRemapRuleForGyro(PHYS_BUTTONS_NUM + 17, &new_map, stickpos,
-motionstate.deviceQuat.y * gyro_options[0] * 4);
if (motionstate.deviceQuat.x < 0)
applyRemapRuleForGyro(PHYS_BUTTONS_NUM + 18, &new_map, stickpos,
motionstate.deviceQuat.x * gyro_options[1] * 4);
if (motionstate.deviceQuat.x > 0)
applyRemapRuleForGyro(PHYS_BUTTONS_NUM + 19, &new_map, stickpos,
-motionstate.deviceQuat.x * gyro_options[1] * 4);
if (motionstate.deviceQuat.z < 0)
applyRemapRuleForGyro(PHYS_BUTTONS_NUM + 20, &new_map, stickpos,
motionstate.deviceQuat.z * gyro_options[2] * 4);
if (motionstate.deviceQuat.z > 0)
applyRemapRuleForGyro(PHYS_BUTTONS_NUM + 21, &new_map, stickpos,
-motionstate.deviceQuat.z * gyro_options[2] * 4);
}
// Nulling analogs if they're remapped
if ((ctrl->lx < 127 && btn_mask[PHYS_BUTTONS_NUM+8] != PHYS_BUTTONS_NUM) ||
(ctrl->lx > 127 && btn_mask[PHYS_BUTTONS_NUM+9] != PHYS_BUTTONS_NUM))
ctrl->lx = 127;
if ((ctrl->ly < 127 && btn_mask[PHYS_BUTTONS_NUM+10] != PHYS_BUTTONS_NUM) ||
(ctrl->ly > 127 && btn_mask[PHYS_BUTTONS_NUM+11] != PHYS_BUTTONS_NUM))
ctrl->ly = 127;
if ((ctrl->rx < 127 && btn_mask[PHYS_BUTTONS_NUM+12] != PHYS_BUTTONS_NUM) ||
(ctrl->rx > 127 && btn_mask[PHYS_BUTTONS_NUM+13] != PHYS_BUTTONS_NUM))
ctrl->rx = 127;
if ((ctrl->ry < 127 && btn_mask[PHYS_BUTTONS_NUM+14] != PHYS_BUTTONS_NUM) ||
(ctrl->ry > 127 && btn_mask[PHYS_BUTTONS_NUM+15] != PHYS_BUTTONS_NUM))
ctrl->ry = 127;
// Remove minimal drift if digital remap for stick directions is used
if (stickpos[0] || stickpos[1] || stickpos[2] || stickpos[3]){
if (abs(ctrl->lx - 127) < analogs_options[0])
ctrl->lx = 127;
if (abs(ctrl->ly - 127) < analogs_options[1])
ctrl->ly = 127;
}
if (stickpos[4] || stickpos[5] || stickpos[6] || stickpos[7]){
if (abs(ctrl->rx - 127) < analogs_options[2])
ctrl->rx = 127;
if (abs(ctrl->ry - 127) < analogs_options[3])
ctrl->ry = 127;
}
//Storing remap for analog axises
if (stickpos[0] || stickpos[1])
ctrl->lx = clamp(ctrl->lx - stickpos[0] + stickpos[1], 0, 255);
if (stickpos[2] || stickpos[3])
ctrl->ly = clamp(ctrl->ly - stickpos[2] + stickpos[3], 0, 255);
if (stickpos[4] || stickpos[5])
ctrl->rx = clamp(ctrl->rx - stickpos[4] + stickpos[5], 0, 255);
if (stickpos[6] || stickpos[7])
ctrl->ry = clamp(ctrl->ry - stickpos[6] + stickpos[7], 0, 255);
//Storing remap for HW buttons
ctrl->buttons = new_map;
//Telling that new emulated touch buffer is ready to be takn
newEmulatedFrontTouchBuffer = 1;
newEmulatedRearTouchBuffer = 1;
}
//Keep same touch id for continuus touches
uint8_t generateTouchId(int x, int y, int panel){
if (panel == SCE_TOUCH_PORT_FRONT){
for (int i = 0; i < prevEtFront.num; i++)
if (prevEtFront.reports[i].x == x && prevEtFront.reports[i].y == y)
return prevEtFront.reports[i].id;
etFrontIdCounter = (etFrontIdCounter + 1) % 127;
return etFrontIdCounter;
} else {
for (int i = 0; i < prevEtRear.num; i++)
if (prevEtRear.reports[i].x == x && prevEtRear.reports[i].y == y)
return prevEtRear.reports[i].id;
etRearIdCounter = (etRearIdCounter + 1) % 127;
return etRearIdCounter;
}
}
void addVirtualTouches(SceTouchData *pData, EmulatedTouch *et,
uint8_t touchPointsMaxNum, int panel){
int touchIdx = 0;
while (touchIdx < et->num && pData->reportNum < touchPointsMaxNum){
pData->report[pData->reportNum].x = et->reports[touchIdx].x;
pData->report[pData->reportNum].y = et->reports[touchIdx].y;
et->reports[touchIdx].id = generateTouchId(
et->reports[touchIdx].x, et->reports[touchIdx].y, panel);
pData->report[pData->reportNum].id = et->reports[touchIdx].id;
pData->reportNum ++;
touchIdx ++;
}
}
void updateTouchInfo(SceUInt32 port, SceTouchData *pData){
if (port == SCE_TOUCH_PORT_FRONT) {
if ((touch_options[16] == 1 && //Disable if remapped
(btn_mask[PHYS_BUTTONS_NUM] != PHYS_BUTTONS_NUM ||
btn_mask[PHYS_BUTTONS_NUM+1] != PHYS_BUTTONS_NUM ||
btn_mask[PHYS_BUTTONS_NUM+2] != PHYS_BUTTONS_NUM ||
btn_mask[PHYS_BUTTONS_NUM+3] != PHYS_BUTTONS_NUM)) ||
btn_mask[PHYS_BUTTONS_NUM] == PHYS_BUTTONS_NUM+1 ||
btn_mask[PHYS_BUTTONS_NUM+1] == PHYS_BUTTONS_NUM+1 ||
btn_mask[PHYS_BUTTONS_NUM+2] == PHYS_BUTTONS_NUM+1 ||
btn_mask[PHYS_BUTTONS_NUM+3] == PHYS_BUTTONS_NUM+1)
pData->reportNum = 0; //Disable pad
if (!newEmulatedFrontTouchBuffer){//New touchbuffer not ready - using previous one
addVirtualTouches(pData, &prevEtFront,
MULTITOUCH_FRONT_NUM, SCE_TOUCH_PORT_FRONT);
return;
}
addVirtualTouches(pData, &etFront,
MULTITOUCH_FRONT_NUM, SCE_TOUCH_PORT_FRONT);
prevEtFront = etFront;
etFront.num = 0;
newEmulatedFrontTouchBuffer = 0;
} else {
if ((touch_options[17] == 1 &&//Disable if remapped
(btn_mask[PHYS_BUTTONS_NUM+4] != PHYS_BUTTONS_NUM ||
btn_mask[PHYS_BUTTONS_NUM+5] != PHYS_BUTTONS_NUM ||
btn_mask[PHYS_BUTTONS_NUM+6] != PHYS_BUTTONS_NUM ||
btn_mask[PHYS_BUTTONS_NUM+7] != PHYS_BUTTONS_NUM)) ||
btn_mask[PHYS_BUTTONS_NUM+4] == PHYS_BUTTONS_NUM+1 ||
btn_mask[PHYS_BUTTONS_NUM+5] == PHYS_BUTTONS_NUM+1 ||
btn_mask[PHYS_BUTTONS_NUM+6] == PHYS_BUTTONS_NUM+1 ||
btn_mask[PHYS_BUTTONS_NUM+7] == PHYS_BUTTONS_NUM+1)
pData->reportNum = 0; //Disable pad
if (!newEmulatedRearTouchBuffer){//New touchbuffer not ready - using previous one
addVirtualTouches(pData, &prevEtRear,
MULTITOUCH_REAR_NUM, SCE_TOUCH_PORT_BACK);
return;
}
addVirtualTouches(pData, &etRear,
MULTITOUCH_REAR_NUM, SCE_TOUCH_PORT_BACK);
prevEtRear = etRear;
etRear.num = 0;
newEmulatedRearTouchBuffer = 0;
}
}
void saveSettings(){
// Just in case the folder doesn't exist
sceIoMkdir("ux0:/data/remaPSV", 0777);
// Opening settings config file and saving the config
SceUID fd = sceIoOpen("ux0:/data/remaPSV/settings.bin", SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777);
sceIoWrite(fd, settings_options, SETTINGS_NUM);
sceIoClose(fd);
}
void loadSettings(){
resetSettingsOptions();
// Just in case the folder doesn't exist
sceIoMkdir("ux0:/data/remaPSV", 0777);
// Loading config file for the selected app if exists
SceUID fd = sceIoOpen("ux0:/data/remaPSV/settings.bin", SCE_O_RDONLY, 0777);
if (fd >= 0){
sceIoRead(fd, settings_options, SETTINGS_NUM);
sceIoClose(fd);
}
}
void saveGlobalConfig(void) {
SceUID fd;
// Just in case the folder doesn't exist