forked from Maschell/controller_patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControllerPatcherUtils.cpp
1162 lines (1010 loc) · 63.3 KB
/
ControllerPatcherUtils.cpp
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
/****************************************************************************
* Copyright (C) 2016,2017 Maschell
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "ControllerPatcherUtils.hpp"
#include <math.h>
#include <string.h>
#include <utils/logger.h>
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::getDataByHandle(s32 handle, my_cb_user ** data){
for(s32 i = 0;i< gHIDMaxDevices;i++){
for(s32 j = 0;j<4;j++){
//log_printf("%d %d %d %d\n",i,j,gHID_Devices[i].pad_data[j].handle,(u32)handle);
if(gHID_Devices[i].pad_data[j].handle == (u32)handle){
*data = gHID_Devices[i].pad_data[j].user_data;
return CONTROLLER_PATCHER_ERROR_NONE;
}
}
}
return CONTROLLER_PATCHER_ERROR_UNKNOWN;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Analyse inputs
*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::getButtonPressed(HID_Data * data, s32 * buttons_hold, s32 VPADButton){
if(data == NULL || buttons_hold == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
s32 deviceslot = data->slotdata.deviceslot;
s32 result = -1;
do{
if(data->type == DEVICE_TYPE_MOUSE){
HID_Mouse_Data * ms_data = &data->data_union.mouse.cur_mouse_data;
if(ms_data == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
if(gHID_Mouse_Mode == HID_MOUSE_MODE_TOUCH){
if(VPADButton == VPAD_BUTTON_TOUCH){
if(ms_data->left_click & 0x01){
result = 1; break;
}
}
}else if(gHID_Mouse_Mode == HID_MOUSE_MODE_AIM){
if(config_controller[deviceslot][CONTRPS_VPAD_BUTTON_LEFT][0] == CONTROLLER_PATCHER_VALUE_SET){
if(VPADButton == (int)gGamePadValues[config_controller[deviceslot][CONTRPS_VPAD_BUTTON_LEFT][1]]){
if(ms_data->left_click & 0x01){
result = 1; break;
}
}
}
if(config_controller[deviceslot][CONTRPS_VPAD_BUTTON_RIGHT][0] == CONTROLLER_PATCHER_VALUE_SET){
if(VPADButton == (int)gGamePadValues[config_controller[deviceslot][CONTRPS_VPAD_BUTTON_RIGHT][1]]){
if(ms_data->right_click & 0x01){
result = 1; break;
}
}
}
}
result = 0; break;
}
u8 * cur_data = &data->data_union.controller.cur_hid_data[0];
if(cur_data == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
s32 cur_config = 0;
if(VPADButton == VPAD_BUTTON_A){
cur_config = CONTRPS_VPAD_BUTTON_A;
}else if(VPADButton == VPAD_BUTTON_B){
cur_config = CONTRPS_VPAD_BUTTON_B;
}else if(VPADButton == VPAD_BUTTON_X){
cur_config = CONTRPS_VPAD_BUTTON_X;
}else if(VPADButton == VPAD_BUTTON_Y){
cur_config = CONTRPS_VPAD_BUTTON_Y;
}else if(VPADButton == VPAD_BUTTON_L){
cur_config = CONTRPS_VPAD_BUTTON_L;
}else if(VPADButton == VPAD_BUTTON_R){
cur_config = CONTRPS_VPAD_BUTTON_R;
}else if(VPADButton == VPAD_BUTTON_ZL){
cur_config = CONTRPS_VPAD_BUTTON_ZL;
}else if(VPADButton == VPAD_BUTTON_ZR){
cur_config = CONTRPS_VPAD_BUTTON_ZR;
}else if(VPADButton == VPAD_BUTTON_STICK_L){
cur_config = CONTRPS_VPAD_BUTTON_STICK_L;
}else if(VPADButton == VPAD_BUTTON_STICK_R){
cur_config = CONTRPS_VPAD_BUTTON_STICK_R;
}else if(VPADButton == VPAD_BUTTON_PLUS){
cur_config = CONTRPS_VPAD_BUTTON_PLUS;
}else if(VPADButton == VPAD_BUTTON_MINUS){
cur_config = CONTRPS_VPAD_BUTTON_MINUS;
}else if(VPADButton == VPAD_BUTTON_HOME){
cur_config = CONTRPS_VPAD_BUTTON_HOME;
}
//! Special DPAD treatment.
if(config_controller[deviceslot][CONTRPS_DPAD_MODE][0] == CONTROLLER_PATCHER_VALUE_SET){
if(config_controller[deviceslot][CONTRPS_DPAD_MODE][1] == CONTRPDM_Hat){
u8 mask = 0x0F;
if(config_controller[deviceslot][CONTRPS_DPAD_MASK][0] == CONTROLLER_PATCHER_VALUE_SET){
mask = config_controller[deviceslot][CONTRPS_DPAD_MASK][1];
}
if(cur_data[config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_NEUTRAL][0]] != config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_NEUTRAL][1]){ // Not neutral
u8 dir1_0 = 0,dir1_1 = 0;
u8 dir2_0 = 0,dir2_1 = 0;
u8 dir3_0 = 0,dir3_1 = 0;
u8 direction = 0;
if(VPADButton == VPAD_BUTTON_LEFT){
dir1_0 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_W][0];
dir2_0 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_NW][0];
dir3_0 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_SW][0];
dir1_1 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_W][1];
dir2_1 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_NW][1];
dir3_1 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_SW][1];
direction = 1;
}else if(VPADButton == VPAD_BUTTON_RIGHT){
dir1_0 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_E][0];
dir2_0 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_SE][0];
dir3_0 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_NE][0];
dir1_1 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_E][1];
dir2_1 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_SE][1];
dir3_1 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_NE][1];
direction = 1;
}else if(VPADButton == VPAD_BUTTON_DOWN){
dir1_0 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_S][0];
dir2_0 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_SE][0];
dir3_0 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_SW][0];
dir1_1 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_S][1];
dir2_1 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_SE][1];
dir3_1 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_SW][1];
direction = 1;
}else if(VPADButton == VPAD_BUTTON_UP){
dir1_0 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_N][0];
dir2_0 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_NW][0];
dir3_0 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_NE][0];
dir1_1 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_N][1];
dir2_1 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_NW][1];
dir3_1 = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_DPAD_NE][1];
direction = 1;
}
if(direction && (((cur_data[dir1_0] & mask) == dir1_1) ||
((cur_data[dir2_0] & mask) == dir2_1) ||
((cur_data[dir3_0] & mask) == dir3_1))) {result = 1; break;}
}
}else if(config_controller[deviceslot][CONTRPS_DPAD_MODE][1] == CONTRPDM_Absolute_2Values){
s32 contrps_value = 0;
if(VPADButton == VPAD_BUTTON_LEFT){
contrps_value = CONTRPS_VPAD_BUTTON_DPAD_ABS_LEFT;
}else if(VPADButton == VPAD_BUTTON_RIGHT){
contrps_value = CONTRPS_VPAD_BUTTON_DPAD_ABS_RIGHT;
}else if(VPADButton == VPAD_BUTTON_UP){
contrps_value = CONTRPS_VPAD_BUTTON_DPAD_ABS_UP;
}else if(VPADButton == VPAD_BUTTON_DOWN){
contrps_value = CONTRPS_VPAD_BUTTON_DPAD_ABS_DOWN;
}
if(contrps_value != 0){
s32 value_byte = CONTROLLER_PATCHER_INVALIDVALUE;
if((value_byte = config_controller[deviceslot][contrps_value][0]) != CONTROLLER_PATCHER_INVALIDVALUE){
if(cur_data[config_controller[deviceslot][contrps_value][0]] == config_controller[deviceslot][contrps_value][1]){
result = 1;
break;
}
}
}
}
}
//! Normal DPAD treatment.
if(VPADButton == VPAD_BUTTON_LEFT){
cur_config = CONTRPS_VPAD_BUTTON_LEFT;
}else if(VPADButton == VPAD_BUTTON_RIGHT){
cur_config = CONTRPS_VPAD_BUTTON_RIGHT;
}else if(VPADButton == VPAD_BUTTON_DOWN){
cur_config = CONTRPS_VPAD_BUTTON_DOWN;
}else if(VPADButton == VPAD_BUTTON_UP){
cur_config = CONTRPS_VPAD_BUTTON_UP;
}
if(result && config_controller[deviceslot][CONTRPS_DOUBLE_USE][0] == CONTROLLER_PATCHER_VALUE_SET){
if(config_controller[deviceslot][CONTRPS_DOUBLE_USE][1] == CONTROLLER_PATCHER_GC_DOUBLE_USE){
if(cur_data[config_controller[deviceslot][CONTRPS_DOUBLE_USE_BUTTON_ACTIVATOR][0]] & config_controller[deviceslot][CONTRPS_DOUBLE_USE_BUTTON_ACTIVATOR][1]){
if(checkValueinConfigController(deviceslot,CONTRPS_DOUBLE_USE_BUTTON_1_RELEASED,cur_config)){result = 0; break;}
if(checkValueinConfigController(deviceslot,CONTRPS_DOUBLE_USE_BUTTON_2_RELEASED,cur_config)){result = 0; break;}
if(checkValueinConfigController(deviceslot,CONTRPS_DOUBLE_USE_BUTTON_3_RELEASED,cur_config)){result = 0; break;}
if(checkValueinConfigController(deviceslot,CONTRPS_DOUBLE_USE_BUTTON_4_RELEASED,cur_config)){result = 0; break;}
if(checkValueinConfigController(deviceslot,CONTRPS_DOUBLE_USE_BUTTON_5_RELEASED,cur_config)){result = 0; break;}
}else{
if(checkValueinConfigController(deviceslot,CONTRPS_DOUBLE_USE_BUTTON_1_PRESSED,cur_config)){result = 0; break;}
if(checkValueinConfigController(deviceslot,CONTRPS_DOUBLE_USE_BUTTON_2_PRESSED,cur_config)){result = 0; break;}
if(checkValueinConfigController(deviceslot,CONTRPS_DOUBLE_USE_BUTTON_3_PRESSED,cur_config)){result = 0; break;}
if(checkValueinConfigController(deviceslot,CONTRPS_DOUBLE_USE_BUTTON_4_PRESSED,cur_config)){result = 0; break;}
if(checkValueinConfigController(deviceslot,CONTRPS_DOUBLE_USE_BUTTON_5_PRESSED,cur_config)){result = 0; break;}
}
}
}
if(isValueSet(data,cur_config) == 1){
result = 1; break;
}else{
//log_printf("Invalid data! deviceslot(slot): %d config: %d\n",deviceslot,cur_config);
}
}while(0); //The break will become handy ;)
if(result == 1){
*buttons_hold |= VPADButton; // -1 would be also true.
return 1;
}
return CONTROLLER_PATCHER_ERROR_NONE;
}
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::isValueSet(HID_Data * data,s32 cur_config){
if(data == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
u8 * cur_data = &data->data_union.controller.cur_hid_data[0];
if(cur_data == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
u32 hidmask = data->slotdata.hidmask;
s32 deviceslot = data->slotdata.deviceslot;
s32 result = CONTROLLER_PATCHER_ERROR_NONE;
if(config_controller[deviceslot][cur_config][0] != CONTROLLER_PATCHER_INVALIDVALUE){ //Invalid data
if(hidmask & gHID_LIST_KEYBOARD){
if(isInKeyboardData(cur_data,config_controller[deviceslot][cur_config][1]) > 0) {
result = 1;
}
}else{
if((cur_data[config_controller[deviceslot][cur_config][0]] & config_controller[deviceslot][cur_config][1]) == config_controller[deviceslot][cur_config][1]){
result = 1;
}
}
}
return result;
}
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::isInKeyboardData(unsigned char * keyboardData,s32 key){
if(keyboardData == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
for(s32 i = 0;i<HID_KEYBOARD_DATA_LENGTH;i++){
if(keyboardData[i] == 0 && i > 1){
break;
}else if (keyboardData[i] == key){
return 1;
}
}
return 0;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Utils for setting the Button data
*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::setButtonRemappingData(VPADData * old_buffer, VPADData * new_buffer,u32 VPADButton, s32 CONTRPS_SLOT){
if(old_buffer == NULL || new_buffer == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
u32 new_value = VPADButton;
if(config_controller[gGamePadSlot][CONTRPS_SLOT][0] != CONTROLLER_PATCHER_INVALIDVALUE){ //using new value!
new_value = gGamePadValues[config_controller[gGamePadSlot][CONTRPS_SLOT][1]];
}
setButtonData(old_buffer,new_buffer,VPADButton,new_value);
return CONTROLLER_PATCHER_ERROR_NONE;
}
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::setButtonData(VPADData * old_buffer, VPADData * new_buffer,u32 oldVPADButton,u32 newVPADButton){
if(old_buffer == NULL || new_buffer == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
if((old_buffer->btns_h & oldVPADButton) == oldVPADButton){
new_buffer->btns_h |= newVPADButton;
}
if((old_buffer->btns_r & oldVPADButton) == oldVPADButton){
new_buffer->btns_r |= newVPADButton;
}
if((old_buffer->btns_d & oldVPADButton) == oldVPADButton){
new_buffer->btns_d |= newVPADButton;
}
return CONTROLLER_PATCHER_ERROR_NONE;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Pad Status functions
*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::checkActivePad(u32 hidmask,s32 pad){
if(hidmask & gHID_LIST_GC && pad >= 0 && pad <= 3){
if (!(((gHID_Devices[gHID_SLOT_GC].pad_data[pad].data_union.controller.cur_hid_data[0] & 0x10) == 0) && ((gHID_Devices[gHID_SLOT_GC].pad_data[pad].data_union.controller.cur_hid_data[0] & 0x22) != 0x22))) return 1;
return CONTROLLER_PATCHER_ERROR_NO_PAD_CONNECTED;
}else{
s32 deviceslot = getDeviceSlot(hidmask);
if(deviceslot < 0 ) return CONTROLLER_PATCHER_ERROR_DEVICE_SLOT_NOT_FOUND;
s32 connected_pads = config_controller[deviceslot][CONTRPS_CONNECTED_PADS][1];
if((connected_pads & (1 << pad)) > 0){
return 1;
}
}
return CONTROLLER_PATCHER_ERROR_NO_PAD_CONNECTED;
}
/*
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::getActivePad(u32 hidmask){
if(hidmask & gHID_LIST_GC){
if (!(((gHID_Devices[gHID_SLOT_GC].pad_data[0].data_union.controller.cur_hid_data[0] & 0x10) == 0) && ((gHID_Devices[gHID_SLOT_GC].pad_data[0].data_union.controller.cur_hid_data[0] & 0x22) != 0x22))) return 0;
if (!(((gHID_Devices[gHID_SLOT_GC].pad_data[1].data_union.controller.cur_hid_data[0] & 0x10) == 0) && ((gHID_Devices[gHID_SLOT_GC].pad_data[1].data_union.controller.cur_hid_data[0] & 0x22) != 0x22))) return 1;
if (!(((gHID_Devices[gHID_SLOT_GC].pad_data[2].data_union.controller.cur_hid_data[0] & 0x10) == 0) && ((gHID_Devices[gHID_SLOT_GC].pad_data[2].data_union.controller.cur_hid_data[0] & 0x22) != 0x22))) return 2;
if (!(((gHID_Devices[gHID_SLOT_GC].pad_data[3].data_union.controller.cur_hid_data[0] & 0x10) == 0) && ((gHID_Devices[gHID_SLOT_GC].pad_data[3].data_union.controller.cur_hid_data[0] & 0x22) != 0x22))) return 3;
return CONTROLLER_PATCHER_ERROR_NO_PAD_CONNECTED;
}
return 0;
}*/
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Stick functions
*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
s16 ControllerPatcherUtils::signExtendValue(u16 input, u8 bit_length) {
// Check if the input is negative in its original bit length
if (input & (1 << (bit_length - 1))) {
// Sign-extend the value
return (s16)(input | (~((1 << bit_length) - 1)));
} else {
// Input is non-negative; return it directly with proper casting
return (s16)input;
}
}
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::normalizeStickValues(Vec2D * stick){
if(stick == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
f32 max_val = 0.0f;
f32 mul_val = 0.0f;
if((max_val = (fabs(stick->x)) + fabs(stick->y)) > 1.414f){
mul_val = 1.414f / max_val;
stick->x *= mul_val;
stick->y *= mul_val;
}
if(stick->x > 1.0f){ stick->x = 1.0f; }
if(stick->y > 1.0f){ stick->y = 1.0f; }
if(stick->x < -1.0f){ stick->x = -1.0f; }
if(stick->y < -1.0f){ stick->y = -1.0f; }
return CONTROLLER_PATCHER_ERROR_NONE;
}
f32 ControllerPatcherUtils::convertAnalogValue(s32 value, s32 default_val, s32 min, s32 max, u8 invert, u8 deadzone){
if(value >= max) return invert == 0x01 ? -1.0f : 1.0f;
if(value <= min) return invert == 0x01 ? 1.0f : -1.0f;
s32 range = 0;
s32 adjustedValue = value - default_val;
if(std::abs(adjustedValue) <= (s32)deadzone) return 0.0f;
if(adjustedValue > 0) {
range = max - (default_val + deadzone);
adjustedValue -= deadzone;
} else {
range = (default_val - deadzone) - min;
adjustedValue += deadzone;
}
f32 normalizedValue = (f32)adjustedValue / range;
return invert == 0x01 ? -normalizedValue : normalizedValue;
}
Vec2D ControllerPatcherUtils::getAnalogValueByButtons(u8 stick_values){
Vec2D stick;
stick.x = 0.0f;
stick.y = 0.0f;
u8 up = ((stick_values & STICK_VALUE_UP) == STICK_VALUE_UP);
u8 down = ((stick_values & STICK_VALUE_DOWN) == STICK_VALUE_DOWN);
u8 left = ((stick_values & STICK_VALUE_LEFT) == STICK_VALUE_LEFT);
u8 right = ((stick_values & STICK_VALUE_RIGHT) == STICK_VALUE_RIGHT);
if(up){
if(!down){
stick.y = 1.0f;
}
if(left || right){
stick.y = 0.707f;
if(left) stick.x = -0.707f;
if(right) stick.x = 0.707f;
}
}else if(down){
if(!up){
stick.y = -1.0f;
}
if(left || right){
stick.y = -0.707f;
if(left) stick.x = -0.707f;
if(right) stick.x = 0.707f;
}
}else{
if(left){
if(!right){
stick.x = -1.0f;
}
}else if(right){
if(!down){
stick.x = 1.0f;
}
}
}
return stick;
}
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::convertAnalogSticks(HID_Data * data, VPADData * buffer){
if(buffer == NULL || data == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
s32 deviceslot = data->slotdata.deviceslot;
if (data->type == DEVICE_TYPE_MOUSE){
if(gHID_Mouse_Mode == HID_MOUSE_MODE_AIM){ // TODO: tweak values
HID_Mouse_Data * ms_data = &data->data_union.mouse.cur_mouse_data;
if(ms_data == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
f32 x_value = ms_data->deltaX/10.0f;
f32 y_value = -1.0f*(ms_data->deltaY/10.0f);
if(config_controller[deviceslot][CONTRPS_MOUSE_STICK][0] != CONTROLLER_PATCHER_INVALIDVALUE){
if(config_controller[deviceslot][CONTRPS_MOUSE_STICK][1] == DEF_L_STICK){
buffer->lstick.x += x_value;
buffer->lstick.y += y_value;
return CONTROLLER_PATCHER_ERROR_NONE;
}
}
buffer->rstick.x += x_value;
buffer->rstick.y += y_value;
}
}else{
u8 * cur_data = &data->data_union.controller.cur_hid_data[0];
if(cur_data == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
s32 deadzone = 0;
u16 l_stick_x_axis_input = 0;
u16 l_stick_y_axis_input = 0;
u16 r_stick_x_axis_input = 0;
u16 r_stick_y_axis_input = 0;
s16 l_stick_x_axis_signed = 0;
s16 l_stick_y_axis_signed = 0;
s16 r_stick_x_axis_signed = 0;
s16 r_stick_y_axis_signed = 0;
u16 l_stick_x_axis_min = 0;
u16 l_stick_x_axis_max = 0;
u16 l_stick_x_axis_def = 0;
u16 l_stick_y_axis_min = 0;
u16 l_stick_y_axis_max = 0;
u16 l_stick_y_axis_def = 0;
u16 r_stick_x_axis_min = 0;
u16 r_stick_x_axis_max = 0;
u16 r_stick_x_axis_def = 0;
u16 r_stick_y_axis_min = 0;
u16 r_stick_y_axis_max = 0;
u16 r_stick_y_axis_def = 0;
if(config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X][0] != CONTROLLER_PATCHER_INVALIDVALUE){
if(config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_DEADZONE][0] == CONTROLLER_PATCHER_VALUE_SET){
deadzone = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_DEADZONE][1];
}
// Read 1st byte of axis HID data & Min/Max/Default values
l_stick_x_axis_input = cur_data[config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X][0]];
l_stick_x_axis_min = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX][0];
l_stick_x_axis_max = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX][1];
l_stick_x_axis_def = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X][1];
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_BIT_LENGTH][0] == CONTROLLER_PATCHER_VALUE_SET && config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_BIT_LENGTH][1] > 8){
// Need more than 8 bits, read 2nd byte of axis HID data
l_stick_x_axis_input |= (cur_data[config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X][0] + 1] << 8);
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_BIT_OFFSET][0] == CONTROLLER_PATCHER_VALUE_SET){
// Shift right to trim unwanted leading bits
l_stick_x_axis_input >>= config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_BIT_OFFSET][1];
}
// Mask off unwanted trailing bits
l_stick_x_axis_input &= ((1 << config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_BIT_LENGTH][1]) - 1);
// Combine most significant bytes of Min/Max/Default values
l_stick_x_axis_min |= (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX_MSB][0] << 8);
l_stick_x_axis_max |= (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX_MSB][1] << 8);
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_DEFAULT_MSB][0] == CONTROLLER_PATCHER_VALUE_SET){
l_stick_x_axis_def |= (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_DEFAULT_MSB][1] << 8);
}
}
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_SIGNED][1]) {
// Extend sign bits if axis HID data is signed
l_stick_x_axis_signed = signExtendValue(l_stick_x_axis_input, config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_BIT_LENGTH][1]);
buffer->lstick.x += convertAnalogValue(l_stick_x_axis_signed,
(s16)l_stick_x_axis_def,
(s16)l_stick_x_axis_min,
(s16)l_stick_x_axis_max,
config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_INVERT][1],
deadzone);
} else {
buffer->lstick.x += convertAnalogValue(l_stick_x_axis_input,
l_stick_x_axis_def,
l_stick_x_axis_min,
l_stick_x_axis_max,
config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_INVERT][1],
deadzone);
}
}
if(config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y][0] != CONTROLLER_PATCHER_INVALIDVALUE){
deadzone = 0;
if(config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_DEADZONE][0] == CONTROLLER_PATCHER_VALUE_SET){
deadzone = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_DEADZONE][1];
}
// Read 1st byte of axis HID data & Min/Max/Default values
l_stick_y_axis_input = cur_data[config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y][0]];
l_stick_y_axis_min = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_MINMAX][0];
l_stick_y_axis_max = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_MINMAX][1];
l_stick_y_axis_def = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y][1];
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_BIT_LENGTH][0] == CONTROLLER_PATCHER_VALUE_SET && config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_BIT_LENGTH][1] > 8){
// Need more than 8 bits, read 2nd byte of axis HID data
l_stick_y_axis_input |= (cur_data[config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y][0] + 1] << 8);
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_BIT_OFFSET][0] == CONTROLLER_PATCHER_VALUE_SET){
// Shift right to trim unwanted leading bits
l_stick_y_axis_input >>= config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_BIT_OFFSET][1];
}
// Mask off unwanted trailing bits
l_stick_y_axis_input &= ((1 << config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_BIT_LENGTH][1]) - 1);
// Combine most significant bytes of Min/Max/Default values
l_stick_y_axis_min |= (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_MINMAX_MSB][0] << 8);
l_stick_y_axis_max |= (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_MINMAX_MSB][1] << 8);
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_DEFAULT_MSB][0] == CONTROLLER_PATCHER_VALUE_SET){
l_stick_y_axis_def |= (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_DEFAULT_MSB][1] << 8);
}
}
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_SIGNED][1]) {
// Extend sign bits if axis HID data is signed
l_stick_y_axis_signed = signExtendValue(l_stick_y_axis_input, config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_BIT_LENGTH][1]);
buffer->lstick.y += convertAnalogValue(l_stick_y_axis_signed,
(s16)l_stick_y_axis_def,
(s16)l_stick_y_axis_min,
(s16)l_stick_y_axis_max,
config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_INVERT][1],
deadzone);
} else {
buffer->lstick.y += convertAnalogValue(l_stick_y_axis_input,
l_stick_y_axis_def,
l_stick_y_axis_min,
l_stick_y_axis_max,
config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_Y_INVERT][1],
deadzone);
}
}
if(config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X][0] != CONTROLLER_PATCHER_INVALIDVALUE){
deadzone = 0;
if(config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_DEADZONE][0] == CONTROLLER_PATCHER_VALUE_SET){
deadzone = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_DEADZONE][1];
}
// Read 1st byte of axis HID data & Min/Max/Default values
r_stick_x_axis_input = cur_data[config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X][0]];
r_stick_x_axis_min = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_MINMAX][0];
r_stick_x_axis_max = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_MINMAX][1];
r_stick_x_axis_def = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X][1];
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_BIT_LENGTH][0] == CONTROLLER_PATCHER_VALUE_SET && config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_BIT_LENGTH][1] > 8){
// Need more than 8 bits, read 2nd byte of axis HID data
r_stick_x_axis_input |= (cur_data[config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X][0] + 1] << 8);
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_BIT_OFFSET][0] == CONTROLLER_PATCHER_VALUE_SET){
// Shift right to trim unwanted leading bits
r_stick_x_axis_input >>= config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_BIT_OFFSET][1];
}
// Mask off unwanted trailing bits
r_stick_x_axis_input &= ((1 << config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_BIT_LENGTH][1]) - 1);
// Combine most significant bytes of Min/Max/Default values
r_stick_x_axis_min |= (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_MINMAX_MSB][0] << 8);
r_stick_x_axis_max |= (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_MINMAX_MSB][1] << 8);
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_DEFAULT_MSB][0] == CONTROLLER_PATCHER_VALUE_SET){
r_stick_x_axis_def |= (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_DEFAULT_MSB][1] << 8);
}
}
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_SIGNED][1]) {
// Extend sign bits if axis HID data is signed
r_stick_x_axis_signed = signExtendValue(r_stick_x_axis_input, config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_BIT_LENGTH][1]);
buffer->rstick.x += convertAnalogValue(r_stick_x_axis_signed,
(s16)r_stick_x_axis_def,
(s16)r_stick_x_axis_min,
(s16)r_stick_x_axis_max,
config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_INVERT][1],
deadzone);
} else {
buffer->rstick.x += convertAnalogValue(r_stick_x_axis_input,
r_stick_x_axis_def,
r_stick_x_axis_min,
r_stick_x_axis_max,
config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_X_INVERT][1],
deadzone);
}
}
if(config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y][0] != CONTROLLER_PATCHER_INVALIDVALUE){
deadzone = 0;
if(config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_DEADZONE][0] == CONTROLLER_PATCHER_VALUE_SET){
deadzone = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_DEADZONE][1];
}
// Read 1st byte of axis HID data & Min/Max/Default values
r_stick_y_axis_input = cur_data[config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y][0]];
r_stick_y_axis_min = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_MINMAX][0];
r_stick_y_axis_max = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_MINMAX][1];
r_stick_y_axis_def = config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y][1];
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_BIT_LENGTH][0] == CONTROLLER_PATCHER_VALUE_SET && config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_BIT_LENGTH][1] > 8){
// Need more than 8 bits, read 2nd byte of axis HID data
r_stick_y_axis_input |= (cur_data[config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y][0] + 1] << 8);
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_BIT_OFFSET][0] == CONTROLLER_PATCHER_VALUE_SET){
// Shift right to trim unwanted leading bits
r_stick_y_axis_input >>= config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_BIT_OFFSET][1];
}
// Mask off unwanted trailing bits
r_stick_y_axis_input &= ((1 << config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_BIT_LENGTH][1]) - 1);
// Combine most significant bytes of Min/Max/Default values
r_stick_y_axis_min |= (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_MINMAX_MSB][0] << 8);
r_stick_y_axis_max |= (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_MINMAX_MSB][1] << 8);
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_DEFAULT_MSB][0] == CONTROLLER_PATCHER_VALUE_SET){
r_stick_y_axis_def |= (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_DEFAULT_MSB][1] << 8);
}
}
if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_SIGNED][1]) {
// Extend sign bits if axis HID data is signed
r_stick_y_axis_signed = signExtendValue(r_stick_y_axis_input, config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_BIT_LENGTH][1]);
buffer->rstick.y += convertAnalogValue(r_stick_y_axis_signed,
(s16)r_stick_y_axis_def,
(s16)r_stick_y_axis_min,
(s16)r_stick_y_axis_max,
config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_INVERT][1],
deadzone);
} else {
buffer->rstick.y += convertAnalogValue(r_stick_y_axis_input,
r_stick_y_axis_def,
r_stick_y_axis_min,
r_stick_y_axis_max,
config_controller[deviceslot][CONTRPS_VPAD_BUTTON_R_STICK_Y_INVERT][1],
deadzone);
}
}
u8 stick_values = 0;
if(isValueSet(data,CONTRPS_VPAD_BUTTON_L_STICK_UP)){ stick_values |= STICK_VALUE_UP; }
if(isValueSet(data,CONTRPS_VPAD_BUTTON_L_STICK_DOWN)){ stick_values |= STICK_VALUE_DOWN; }
if(isValueSet(data,CONTRPS_VPAD_BUTTON_L_STICK_LEFT)){ stick_values |= STICK_VALUE_LEFT; }
if(isValueSet(data,CONTRPS_VPAD_BUTTON_L_STICK_RIGHT)){ stick_values |= STICK_VALUE_RIGHT; }
if(stick_values > 0 ){
Vec2D stick = getAnalogValueByButtons(stick_values);
buffer->lstick.x += stick.x;
buffer->lstick.y += stick.y;
}
stick_values = 0;
if(isValueSet(data,CONTRPS_VPAD_BUTTON_R_STICK_UP)){ stick_values |= STICK_VALUE_UP; }
if(isValueSet(data,CONTRPS_VPAD_BUTTON_R_STICK_DOWN)){ stick_values |= STICK_VALUE_DOWN; }
if(isValueSet(data,CONTRPS_VPAD_BUTTON_R_STICK_LEFT)){ stick_values |= STICK_VALUE_LEFT; }
if(isValueSet(data,CONTRPS_VPAD_BUTTON_R_STICK_RIGHT)){ stick_values |= STICK_VALUE_RIGHT; }
if(stick_values > 0 ){
Vec2D stick = getAnalogValueByButtons(stick_values);
buffer->rstick.x += stick.x;
buffer->rstick.y += stick.y;
}
if(config_controller[deviceslot][CONTRPS_VPAD_STICK_L_COPY_DPAD][0] != CONTROLLER_PATCHER_INVALIDVALUE){
if(config_controller[deviceslot][CONTRPS_VPAD_STICK_L_COPY_DPAD][0] == 1){
u8 stick_values = 0;
if(buffer->btns_h & VPAD_BUTTON_UP){ stick_values |= STICK_VALUE_UP; }
if(buffer->btns_h & VPAD_BUTTON_DOWN){ stick_values |= STICK_VALUE_DOWN; }
if(buffer->btns_h & VPAD_BUTTON_LEFT){ stick_values |= STICK_VALUE_LEFT; }
if(buffer->btns_h & VPAD_BUTTON_RIGHT){ stick_values |= STICK_VALUE_RIGHT; }
if(stick_values > 0 ){
Vec2D stick = getAnalogValueByButtons(stick_values);
buffer->lstick.x += stick.x;
buffer->lstick.y += stick.y;
}
}
}
if(config_controller[deviceslot][CONTRPS_VPAD_STICK_R_COPY_DPAD][0] != CONTROLLER_PATCHER_INVALIDVALUE){
if(config_controller[deviceslot][CONTRPS_VPAD_STICK_R_COPY_DPAD][0] == 1){
u8 stick_values = 0;
if(buffer->btns_h & VPAD_BUTTON_UP){ stick_values |= STICK_VALUE_UP; }
if(buffer->btns_h & VPAD_BUTTON_DOWN){ stick_values |= STICK_VALUE_DOWN; }
if(buffer->btns_h & VPAD_BUTTON_LEFT){ stick_values |= STICK_VALUE_LEFT; }
if(buffer->btns_h & VPAD_BUTTON_RIGHT){ stick_values |= STICK_VALUE_RIGHT; }
if(stick_values > 0 ){
Vec2D stick = getAnalogValueByButtons(stick_values);
buffer->rstick.x += stick.x;
buffer->rstick.y += stick.y;
}
}
}
// if (config_controller[deviceslot][CONTRPS_VPAD_BUTTON_L_STICK_X_SIGNED][1]) {
// s16 temp_l_stick_x_axis_min = (s16)l_stick_x_axis_min;
// s16 temp_l_stick_y_axis_min = (s16)l_stick_y_axis_min;
// s16 temp_l_stick_x_axis_def = (s16)l_stick_x_axis_def;
// s16 temp_l_stick_y_axis_def = (s16)l_stick_y_axis_def;
// s16 temp_l_stick_x_axis_max = (s16)l_stick_x_axis_max;
// s16 temp_l_stick_y_axis_max = (s16)l_stick_y_axis_max;
// log_printf(
// "MinX %+5d %04X DefX %+5d MaxX %+5d %04X MinY %+5d %04X DefY %+5d MaxY %+5d %04X X %+1.3f(%+5d %04X) Y %+1.3f(%+5d %04X)\n",
// temp_l_stick_x_axis_min,l_stick_x_axis_min,
// temp_l_stick_x_axis_def,
// temp_l_stick_x_axis_max,l_stick_x_axis_max,
// temp_l_stick_y_axis_min,l_stick_y_axis_min,
// temp_l_stick_y_axis_def,
// temp_l_stick_y_axis_max,l_stick_y_axis_max,
// buffer->lstick.x,l_stick_x_axis_signed,l_stick_x_axis_signed,
// buffer->lstick.y,l_stick_y_axis_signed,l_stick_y_axis_signed
// );
// } else {
// log_printf(
// "MinX %+5d %04X DefX %+5d MaxX %+5d %04X MinY %+5d %04X DefY %+5d MaxY %+5d %04X X %+1.3f(%+5d %04X) Y %+1.3f(%+5d %04X)\n",
// l_stick_x_axis_min,l_stick_x_axis_min,
// l_stick_x_axis_def,
// l_stick_x_axis_max,l_stick_x_axis_max,
// l_stick_y_axis_min,l_stick_y_axis_min,
// l_stick_y_axis_def,
// l_stick_y_axis_max,l_stick_y_axis_max,
// buffer->lstick.x,l_stick_x_axis_input,l_stick_x_axis_input,
// buffer->lstick.y,l_stick_y_axis_input,l_stick_y_axis_input
// );
// }
}
return CONTROLLER_PATCHER_ERROR_NONE;
}
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::setEmulatedSticks(VPADData * buffer, u32 * last_emulatedSticks){
if(buffer == NULL || last_emulatedSticks == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
u32 emulatedSticks = 0;
s32 l_x_full = (buffer->lstick.x > 0.5f || buffer->lstick.x < -0.5f)? 1:0;
s32 l_y_full = (buffer->lstick.y > 0.5f || buffer->lstick.y < -0.5f)? 1:0;
s32 r_x_full = (buffer->rstick.x > 0.5f || buffer->rstick.x < -0.5f)? 1:0;
s32 r_y_full = (buffer->rstick.y > 0.5f || buffer->rstick.y < -0.5f)? 1:0;
if((buffer->lstick.x > 0.5f) || (buffer->lstick.x > 0.1f && !l_y_full)){
emulatedSticks |= VPAD_STICK_L_EMULATION_RIGHT;
}
if((buffer->lstick.x < -0.5f) || (buffer->lstick.x < -0.1f && !l_y_full)){
emulatedSticks |= VPAD_STICK_L_EMULATION_LEFT;
}
if((buffer->lstick.y > 0.5f) || (buffer->lstick.y > 0.1f && !l_x_full)){
emulatedSticks |= VPAD_STICK_L_EMULATION_UP;
}
if((buffer->lstick.y < -0.5f) || (buffer->lstick.y < -0.1f && !l_x_full)){
emulatedSticks |= VPAD_STICK_L_EMULATION_DOWN;
}
if((buffer->rstick.x > 0.5f) || (buffer->rstick.x > 0.1f && !r_y_full)){
emulatedSticks |= VPAD_STICK_R_EMULATION_RIGHT;
}
if((buffer->rstick.x < -0.5f) || (buffer->rstick.x < -0.1f && !r_y_full)){
emulatedSticks |= VPAD_STICK_R_EMULATION_LEFT;
}
if((buffer->rstick.y > 0.5f) || (buffer->rstick.y > 0.1f && !r_x_full)){
emulatedSticks |= VPAD_STICK_R_EMULATION_UP;
}
if((buffer->rstick.y < -0.5f) || (buffer->rstick.y < -0.1f && !r_x_full)){
emulatedSticks |= VPAD_STICK_R_EMULATION_DOWN;
}
//Setting the emulated sticks
buffer->btns_h |= emulatedSticks;
buffer->btns_d |= (emulatedSticks & (~*last_emulatedSticks));
buffer->btns_r |= (*last_emulatedSticks & (~emulatedSticks));
*last_emulatedSticks = emulatedSticks;
return CONTROLLER_PATCHER_ERROR_NONE;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Touch functions
*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::setTouch(HID_Data * data,VPADData * buffer){
if(buffer == NULL || data == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
if(data->type == DEVICE_TYPE_MOUSE && gHID_Mouse_Mode == HID_MOUSE_MODE_TOUCH){
s32 buttons_hold;
if(getButtonPressed(data,&buttons_hold,VPAD_BUTTON_TOUCH)){
HID_Mouse_Data * ms_data = &data->data_union.mouse.cur_mouse_data;
if(ms_data == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
s32 x_mouse = 80 + ((int)(((ms_data->X)*1.0f/1280.0)*3890.0f));
s32 y_mouse = 3910 - ((int)(((ms_data->Y)*1.0f/720.0)*3760.0f));
buffer->tpdata.x = x_mouse;
buffer->tpdata.y = y_mouse;
buffer->tpdata.touched = 1;
buffer->tpdata.invalid = 0;
buffer->tpdata1.x = x_mouse;
buffer->tpdata1.y = y_mouse;
buffer->tpdata1.touched = 1;
buffer->tpdata1.invalid = 0;
buffer->tpdata2.x = x_mouse;
buffer->tpdata2.y = y_mouse;
buffer->tpdata2.touched = 1;
buffer->tpdata2.invalid = 0;
}
}
return CONTROLLER_PATCHER_ERROR_NONE;
}
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::checkAndSetMouseMode(HID_Data * data){
u32 hidmask = data->slotdata.hidmask;
if(hidmask & gHID_LIST_KEYBOARD){
u8 * cur_data = &data->data_union.controller.cur_hid_data[0];
u8 * last_data = &data->data_union.controller.last_hid_data[0];
if((isInKeyboardData(cur_data,HID_KEYBOARD_BUTTON_F1) > 0) && ((isInKeyboardData(cur_data,HID_KEYBOARD_BUTTON_F1) > 0) != (isInKeyboardData(last_data,HID_KEYBOARD_BUTTON_F1) > 0)) && gMouseModeCoolDown == 0){
gMouseModeCoolDown = 60;
if(gHID_Mouse_Mode == HID_MOUSE_MODE_AIM){
gHID_Mouse_Mode = HID_MOUSE_MODE_TOUCH;
//log_printf("ControllerPatcherUtils::checkAndSetMouseMode(line %d): Mouse mode changed! to touch \n",__LINE__);
}else if(gHID_Mouse_Mode == HID_MOUSE_MODE_TOUCH){
//log_printf("ControllerPatcherUtils::checkAndSetMouseMode(line %d): Mouse mode changed! to aim \n",__LINE__);
gHID_Mouse_Mode = HID_MOUSE_MODE_AIM;
}
}
if(gMouseModeCoolDown > 0){
gMouseModeCoolDown--;
}
}
return CONTROLLER_PATCHER_ERROR_NONE;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Other functions
*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::translateToPro(VPADData * vpad_buffer,KPADData * pro_buffer,u32 * lastButtonsPressesPRO){
if(vpad_buffer == NULL || pro_buffer == NULL || lastButtonsPressesPRO == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
s32 buttons_hold = 0;
if(vpad_buffer->btns_h & VPAD_BUTTON_A) buttons_hold |= WPAD_PRO_BUTTON_A;
if(vpad_buffer->btns_h & VPAD_BUTTON_B) buttons_hold |= WPAD_PRO_BUTTON_B;
if(vpad_buffer->btns_h & VPAD_BUTTON_X) buttons_hold |= WPAD_PRO_BUTTON_X;
if(vpad_buffer->btns_h & VPAD_BUTTON_Y) buttons_hold |= WPAD_PRO_BUTTON_Y;
if(vpad_buffer->btns_h & VPAD_BUTTON_PLUS) buttons_hold |= WPAD_PRO_BUTTON_PLUS;
if(vpad_buffer->btns_h & VPAD_BUTTON_MINUS) buttons_hold |= WPAD_PRO_BUTTON_MINUS;
if(vpad_buffer->btns_h & VPAD_BUTTON_HOME) buttons_hold |= WPAD_PRO_BUTTON_HOME;
if(vpad_buffer->btns_h & VPAD_BUTTON_LEFT) buttons_hold |= WPAD_PRO_BUTTON_LEFT;
if(vpad_buffer->btns_h & VPAD_BUTTON_RIGHT) buttons_hold |= WPAD_PRO_BUTTON_RIGHT;
if(vpad_buffer->btns_h & VPAD_BUTTON_UP) buttons_hold |= WPAD_PRO_BUTTON_UP;
if(vpad_buffer->btns_h & VPAD_BUTTON_DOWN) buttons_hold |= WPAD_PRO_BUTTON_DOWN;
if(vpad_buffer->btns_h & VPAD_BUTTON_L) buttons_hold |= WPAD_PRO_TRIGGER_L;
if(vpad_buffer->btns_h & VPAD_BUTTON_ZL) buttons_hold |= WPAD_PRO_TRIGGER_ZL;
if(vpad_buffer->btns_h & VPAD_BUTTON_R) buttons_hold |= WPAD_PRO_TRIGGER_R;
if(vpad_buffer->btns_h & VPAD_BUTTON_ZR) buttons_hold |= WPAD_PRO_TRIGGER_ZR;
if(vpad_buffer->btns_h & VPAD_BUTTON_STICK_L) buttons_hold |= WPAD_PRO_BUTTON_STICK_L;
if(vpad_buffer->btns_h & VPAD_BUTTON_STICK_R) buttons_hold |= WPAD_PRO_BUTTON_STICK_R;
if(vpad_buffer->btns_h & VPAD_STICK_L_EMULATION_LEFT) buttons_hold |= WPAD_PRO_STICK_L_EMULATION_LEFT;
if(vpad_buffer->btns_h & VPAD_STICK_L_EMULATION_RIGHT) buttons_hold |= WPAD_PRO_STICK_L_EMULATION_RIGHT;
if(vpad_buffer->btns_h & VPAD_STICK_L_EMULATION_UP) buttons_hold |= WPAD_PRO_STICK_L_EMULATION_UP;
if(vpad_buffer->btns_h & VPAD_STICK_L_EMULATION_DOWN) buttons_hold |= WPAD_PRO_STICK_L_EMULATION_DOWN;
if(vpad_buffer->btns_h & VPAD_STICK_R_EMULATION_LEFT) buttons_hold |= WPAD_PRO_STICK_R_EMULATION_LEFT;
if(vpad_buffer->btns_h & VPAD_STICK_R_EMULATION_RIGHT) buttons_hold |= WPAD_PRO_STICK_R_EMULATION_RIGHT;
if(vpad_buffer->btns_h & VPAD_STICK_R_EMULATION_UP) buttons_hold |= WPAD_PRO_STICK_R_EMULATION_UP;
if(vpad_buffer->btns_h & VPAD_STICK_R_EMULATION_DOWN) buttons_hold |= WPAD_PRO_STICK_R_EMULATION_DOWN;
pro_buffer->pro.lstick_x = vpad_buffer->lstick.x;
pro_buffer->pro.lstick_y = vpad_buffer->lstick.y;
pro_buffer->pro.rstick_x = vpad_buffer->rstick.x;
pro_buffer->pro.rstick_y = vpad_buffer->rstick.y;
/*
pro_buffer->unused_1[1] = 0xBF800000;
pro_buffer->unused_1[3] = 0x3F800000;
pro_buffer->angle_x = 0x3F800000;
pro_buffer->unused_3[4] = 0x3F800000;
pro_buffer->unused_3[7] = 0x3F800000;
pro_buffer->unused_6[17] = 0x3F800000;
pro_buffer->unused_7[1] = 0x3F800000;
pro_buffer->unused_7[5] = 0x3F800000;*/
pro_buffer->pro.btns_h = buttons_hold;
pro_buffer->pro.btns_d = (buttons_hold & (~*lastButtonsPressesPRO));
pro_buffer->pro.btns_r = (*lastButtonsPressesPRO & (~buttons_hold));
*lastButtonsPressesPRO = buttons_hold;
pro_buffer->format = WPAD_FMT_PRO_CONTROLLER;
pro_buffer->wpad_error = 0x00;
pro_buffer->device_type = WPAD_EXT_PRO_CONTROLLER;
pro_buffer->pro.wired = 1;
pro_buffer->pro.charging = 1;
return CONTROLLER_PATCHER_ERROR_NONE;
}
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::translateToProWPADRead(VPADData * vpad_buffer,WPADReadData * pro_buffer){
if(vpad_buffer == NULL || pro_buffer == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
s32 buttons_hold = 0;
if(vpad_buffer->btns_h & VPAD_BUTTON_A) buttons_hold |= WPAD_PRO_BUTTON_A;
if(vpad_buffer->btns_h & VPAD_BUTTON_B) buttons_hold |= WPAD_PRO_BUTTON_B;
if(vpad_buffer->btns_h & VPAD_BUTTON_X) buttons_hold |= WPAD_PRO_BUTTON_X;
if(vpad_buffer->btns_h & VPAD_BUTTON_Y) buttons_hold |= WPAD_PRO_BUTTON_Y;
if(vpad_buffer->btns_h & VPAD_BUTTON_PLUS) buttons_hold |= WPAD_PRO_BUTTON_PLUS;
if(vpad_buffer->btns_h & VPAD_BUTTON_MINUS) buttons_hold |= WPAD_PRO_BUTTON_MINUS;
if(vpad_buffer->btns_h & VPAD_BUTTON_HOME) buttons_hold |= WPAD_PRO_BUTTON_HOME;
if(vpad_buffer->btns_h & VPAD_BUTTON_LEFT) buttons_hold |= WPAD_PRO_BUTTON_LEFT;
if(vpad_buffer->btns_h & VPAD_BUTTON_RIGHT) buttons_hold |= WPAD_PRO_BUTTON_RIGHT;
if(vpad_buffer->btns_h & VPAD_BUTTON_UP) buttons_hold |= WPAD_PRO_BUTTON_UP;
if(vpad_buffer->btns_h & VPAD_BUTTON_DOWN) buttons_hold |= WPAD_PRO_BUTTON_DOWN;
if(vpad_buffer->btns_h & VPAD_BUTTON_L) buttons_hold |= WPAD_PRO_TRIGGER_L;
if(vpad_buffer->btns_h & VPAD_BUTTON_ZL) buttons_hold |= WPAD_PRO_TRIGGER_ZL;
if(vpad_buffer->btns_h & VPAD_BUTTON_R) buttons_hold |= WPAD_PRO_TRIGGER_R;
if(vpad_buffer->btns_h & VPAD_BUTTON_ZR) buttons_hold |= WPAD_PRO_TRIGGER_ZR;
if(vpad_buffer->btns_h & VPAD_BUTTON_STICK_L) buttons_hold |= WPAD_PRO_BUTTON_STICK_L;
if(vpad_buffer->btns_h & VPAD_BUTTON_STICK_R) buttons_hold |= WPAD_PRO_BUTTON_STICK_R;
if(vpad_buffer->btns_h & VPAD_STICK_L_EMULATION_LEFT) buttons_hold |= WPAD_PRO_STICK_L_EMULATION_LEFT;
if(vpad_buffer->btns_h & VPAD_STICK_L_EMULATION_RIGHT) buttons_hold |= WPAD_PRO_STICK_L_EMULATION_RIGHT;
if(vpad_buffer->btns_h & VPAD_STICK_L_EMULATION_UP) buttons_hold |= WPAD_PRO_STICK_L_EMULATION_UP;
if(vpad_buffer->btns_h & VPAD_STICK_L_EMULATION_DOWN) buttons_hold |= WPAD_PRO_STICK_L_EMULATION_DOWN;
if(vpad_buffer->btns_h & VPAD_STICK_R_EMULATION_LEFT) buttons_hold |= WPAD_PRO_STICK_R_EMULATION_LEFT;
if(vpad_buffer->btns_h & VPAD_STICK_R_EMULATION_RIGHT) buttons_hold |= WPAD_PRO_STICK_R_EMULATION_RIGHT;
if(vpad_buffer->btns_h & VPAD_STICK_R_EMULATION_UP) buttons_hold |= WPAD_PRO_STICK_R_EMULATION_UP;
if(vpad_buffer->btns_h & VPAD_STICK_R_EMULATION_DOWN) buttons_hold |= WPAD_PRO_STICK_R_EMULATION_DOWN;
pro_buffer->l_stick_x = (s16) (vpad_buffer->lstick.x * 950.0f);
pro_buffer->l_stick_y = (s16) (vpad_buffer->lstick.y * 950.0f);
pro_buffer->r_stick_x = (s16) (vpad_buffer->rstick.x * 950.0f);
pro_buffer->r_stick_y = (s16) (vpad_buffer->rstick.y * 950.0f);
pro_buffer->buttons = buttons_hold;
pro_buffer->fmt = WPAD_FMT_PRO_CONTROLLER;
pro_buffer->err = 0x00;
pro_buffer->dev = WPAD_EXT_PRO_CONTROLLER;
return CONTROLLER_PATCHER_ERROR_NONE;
}
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::translateToVPAD(VPADData * vpad_buffer,KPADData * pro_buffer,u32 * lastButtonsPressesVPAD){
if(vpad_buffer == NULL || pro_buffer == NULL || lastButtonsPressesVPAD == NULL) return CONTROLLER_PATCHER_ERROR_NULL_POINTER;
s32 buttons_hold = 0;
if(pro_buffer->pro.btns_h & WPAD_PRO_BUTTON_A) buttons_hold |= VPAD_BUTTON_A;
if(pro_buffer->pro.btns_h & WPAD_PRO_BUTTON_B) buttons_hold |= VPAD_BUTTON_B;
if(pro_buffer->pro.btns_h & WPAD_PRO_BUTTON_X) buttons_hold |= VPAD_BUTTON_X;
if(pro_buffer->pro.btns_h & WPAD_PRO_BUTTON_Y) buttons_hold |= VPAD_BUTTON_Y;
if(pro_buffer->pro.btns_h & WPAD_PRO_BUTTON_PLUS) buttons_hold |= VPAD_BUTTON_PLUS;
if(pro_buffer->pro.btns_h & WPAD_PRO_BUTTON_MINUS) buttons_hold |= VPAD_BUTTON_MINUS;
if(pro_buffer->pro.btns_h & WPAD_PRO_BUTTON_HOME) buttons_hold |= VPAD_BUTTON_HOME;
if(pro_buffer->pro.btns_h & WPAD_PRO_BUTTON_LEFT) buttons_hold |= VPAD_BUTTON_LEFT;
if(pro_buffer->pro.btns_h & WPAD_PRO_BUTTON_RIGHT) buttons_hold |= VPAD_BUTTON_RIGHT;
if(pro_buffer->pro.btns_h & WPAD_PRO_BUTTON_UP) buttons_hold |= VPAD_BUTTON_UP;
if(pro_buffer->pro.btns_h & WPAD_PRO_BUTTON_DOWN) buttons_hold |= VPAD_BUTTON_DOWN;
if(pro_buffer->pro.btns_h & WPAD_PRO_TRIGGER_L) buttons_hold |= VPAD_BUTTON_L;
if(pro_buffer->pro.btns_h & WPAD_PRO_TRIGGER_ZL) buttons_hold |= VPAD_BUTTON_ZL;