forked from ps2homebrew/Open-PS2-Loader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
guigame.c
1487 lines (1265 loc) · 54.2 KB
/
guigame.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
/*
Per Game Integration by BatRastard
PADEMU by belek666
*/
#include "include/opl.h"
#include "include/gui.h"
#include "include/ioman.h"
#include "include/lang.h"
#include "include/pad.h"
#include "include/config.h"
#include "include/ethsupport.h"
#include "include/compatupd.h"
#include "include/cheatman.h"
#include "include/system.h"
#include "include/guigame.h"
#include "include/ds34common.h"
#ifdef PADEMU
#include <libds34bt.h>
#include <libds34usb.h>
#endif
#define NEWLIB_PORT_AWARE
#include <fileXio_rpc.h> // fileXioDevctl("genvmc:", ***)
static int configSourceID;
static int dmaMode;
static int compatMode;
static int EnableGSM;
static int GSMVMode;
static int GSMXOffset;
static int GSMYOffset;
static int GSMFIELDFix;
static int EnableCheat;
static int CheatMode;
static int forceGlobalOSDLanguage;
#ifdef PADEMU
static int EnablePadEmu;
static int PadEmuSettings;
static union
{
struct
{
unsigned int left_stick_slowdown : 4;
unsigned int right_stick_slowdown : 4;
unsigned int l_slowdown_enable : 1;
unsigned int l_slowdown_toggle : 1;
unsigned int r_slowdown_enable : 1;
unsigned int r_slowdown_toggle : 1;
unsigned int lx_invert : 1;
unsigned int ly_invert : 1;
unsigned int rx_invert : 1;
unsigned int ry_invert : 1;
unsigned int turbo_speed : 2;
};
int raw;
} PadMacroSettings;
#endif
static char hexid[32];
static char altStartup[32];
static char vmc1[32];
static char vmc2[32];
static char hexDiscID[15];
static char configSource[128];
// forward declarations.
static void guiGameLoadGSMConfig(config_set_t *configSet, config_set_t *configGame);
static void guiGameLoadCheatsConfig(config_set_t *configSet, config_set_t *configGame);
#ifdef PADEMU
static void guiGameLoadPadEmuConfig(config_set_t *configSet, config_set_t *configGame);
static void guiGameLoadPadMacroConfig(config_set_t *configSet, config_set_t *configGame);
#endif
static int guiGameSaveOSDLanguageGameConfig(config_set_t *configSet, int result);
static void guiGameLoadOSDLanguageConfig(config_set_t *configSet, config_set_t *configGame);
int guiGameAltStartupNameHandler(char *text, int maxLen)
{
int i;
int result = diaShowKeyb(text, maxLen, 0, NULL);
if (result) {
for (i = 0; text[i]; i++) {
if (text[i] > 96 && text[i] < 123)
text[i] -= 32;
}
}
return result;
}
char *gameConfigSource(void)
{
char *source = configSource;
return source;
}
// VMC
typedef struct
{ // size = 76
int VMC_status; // 0=available, 1=busy
int VMC_error;
int VMC_progress;
char VMC_msg[64];
} statusVMCparam_t;
#define OPERATION_CREATE 0
#define OPERATION_CREATING 1
#define OPERATION_ABORTING 2
#define OPERATION_ENDING 3
#define OPERATION_END 4
static short vmc_refresh;
static int vmc_operation;
static statusVMCparam_t vmc_status;
int guiGameVmcNameHandler(char *text, int maxLen)
{
int result = diaShowKeyb(text, maxLen, 0, NULL);
if (result)
vmc_refresh = 1;
return result;
}
static int guiGameRefreshVMCConfig(item_list_t *support, char *name)
{
int size = support->itemCheckVMC(support, name, 0);
if (size != -1) {
diaSetLabel(diaVMC, VMC_STATUS, _l(_STR_VMC_FILE_EXISTS));
if (size == 8)
diaSetInt(diaVMC, VMC_SIZE, 0);
else if (size == 16)
diaSetInt(diaVMC, VMC_SIZE, 1);
else if (size == 32)
diaSetInt(diaVMC, VMC_SIZE, 2);
else if (size == 64)
diaSetInt(diaVMC, VMC_SIZE, 3);
else {
diaSetInt(diaVMC, VMC_SIZE, 0);
diaSetLabel(diaVMC, VMC_STATUS, _l(_STR_VMC_FILE_ERROR));
}
diaSetLabel(diaVMC, VMC_BUTTON_CREATE, _l(_STR_MODIFY));
diaSetVisible(diaVMC, VMC_BUTTON_DELETE, 1);
if (gEnableWrite) {
diaSetEnabled(diaVMC, VMC_SIZE, 1);
diaSetEnabled(diaVMC, VMC_BUTTON_CREATE, 1);
diaSetEnabled(diaVMC, VMC_BUTTON_DELETE, 1);
} else {
diaSetEnabled(diaVMC, VMC_SIZE, 0);
diaSetEnabled(diaVMC, VMC_BUTTON_CREATE, 0);
diaSetEnabled(diaVMC, VMC_BUTTON_DELETE, 0);
}
} else {
diaSetLabel(diaVMC, VMC_BUTTON_CREATE, _l(_STR_CREATE));
diaSetLabel(diaVMC, VMC_STATUS, _l(_STR_VMC_FILE_NEW));
diaSetInt(diaVMC, VMC_SIZE, 0);
diaSetEnabled(diaVMC, VMC_SIZE, 1);
diaSetEnabled(diaVMC, VMC_BUTTON_CREATE, 1);
diaSetVisible(diaVMC, VMC_BUTTON_DELETE, 0);
}
return size;
}
static int guiGameVMCUpdater(int modified)
{
if (vmc_refresh) {
vmc_refresh = 0;
return VMC_REFRESH;
}
if ((vmc_operation == OPERATION_CREATING) || (vmc_operation == OPERATION_ABORTING)) {
int result = fileXioDevctl("genvmc:", 0xC0DE0003, NULL, 0, (void *)&vmc_status, sizeof(vmc_status));
if (result == 0) {
diaSetLabel(diaVMC, VMC_STATUS, vmc_status.VMC_msg);
diaSetInt(diaVMC, VMC_PROGRESS, vmc_status.VMC_progress);
if (vmc_status.VMC_error != 0)
LOG("GUI VMCUpdater: %d\n", vmc_status.VMC_error);
if (vmc_status.VMC_status == 0x00) {
diaSetLabel(diaVMC, VMC_BUTTON_CREATE, _l(_STR_OK));
vmc_operation = OPERATION_ENDING;
return VMC_BUTTON_CREATE;
}
} else
LOG("GUI Status result: %d\n", result);
}
return 0;
}
static int guiGameShowVMCConfig(int id, item_list_t *support, char *VMCName, int slot, int validate)
{
int result = validate ? VMC_BUTTON_CREATE : 0;
char vmc[32];
if (strlen(VMCName))
strncpy(vmc, VMCName, sizeof(vmc));
else {
if (validate)
return 1; // nothing to validate if no user input
char *startup = support->itemGetStartup(support, id);
snprintf(vmc, sizeof(vmc), "%s_%d", startup, slot);
}
vmc_refresh = 0;
vmc_operation = OPERATION_CREATE;
diaSetEnabled(diaVMC, VMC_NAME, 1);
diaSetEnabled(diaVMC, VMC_SIZE, 1);
diaSetInt(diaVMC, VMC_PROGRESS, 0);
const char *VMCSizes[] = {"8 Mb", "16 Mb", "32 Mb", "64 Mb", NULL};
diaSetEnum(diaVMC, VMC_SIZE, VMCSizes);
int size = guiGameRefreshVMCConfig(support, vmc);
diaSetString(diaVMC, VMC_NAME, vmc);
do {
if (result == VMC_BUTTON_CREATE) {
if (vmc_operation == OPERATION_CREATE) { // User start creation of VMC
int sizeUI;
diaGetInt(diaVMC, VMC_SIZE, &sizeUI);
if (sizeUI == 1)
sizeUI = 16;
else if (sizeUI == 2)
sizeUI = 32;
else if (sizeUI == 3)
sizeUI = 64;
else
sizeUI = 8;
if (sizeUI != size) {
support->itemCheckVMC(support, vmc, sizeUI);
diaSetEnabled(diaVMC, VMC_NAME, 0);
diaSetEnabled(diaVMC, VMC_SIZE, 0);
diaSetLabel(diaVMC, VMC_BUTTON_CREATE, _l(_STR_ABORT));
vmc_operation = OPERATION_CREATING;
} else
break;
} else if (vmc_operation == OPERATION_ENDING) {
if (validate)
break; // directly close VMC config dialog
vmc_operation = OPERATION_END;
} else if (vmc_operation == OPERATION_END) { // User closed creation dialog of VMC
break;
} else if (vmc_operation == OPERATION_CREATING) { // User canceled creation of VMC
fileXioDevctl("genvmc:", 0xC0DE0002, NULL, 0, NULL, 0);
vmc_operation = OPERATION_ABORTING;
}
} else if (result == VMC_BUTTON_DELETE) {
if (guiMsgBox(_l(_STR_DELETE_WARNING), 1, diaVMC)) {
support->itemCheckVMC(support, vmc, -1);
diaSetString(diaVMC, VMC_NAME, "");
break;
}
} else if (result == VMC_REFRESH) { // User changed the VMC name
diaGetString(diaVMC, VMC_NAME, vmc, sizeof(vmc));
size = guiGameRefreshVMCConfig(support, vmc);
}
result = diaExecuteDialog(diaVMC, result, 1, &guiGameVMCUpdater);
if ((result == 0) && (vmc_operation == OPERATION_CREATE))
break;
} while (1);
return result;
}
void guiGameShowVMCMenu(int id, item_list_t *support)
{
int result = -1;
// show dialog
do {
diaSetLabel(diaVMCConfig, COMPAT_VMC1_DEFINE, vmc1);
diaSetLabel(diaVMCConfig, COMPAT_VMC2_DEFINE, vmc2);
if (strlen(vmc1))
diaSetLabel(diaVMCConfig, COMPAT_VMC1_ACTION, _l(_STR_RESET));
else
diaSetLabel(diaVMCConfig, COMPAT_VMC1_ACTION, _l(_STR_USE_GENERIC));
if (strlen(vmc2))
diaSetLabel(diaVMCConfig, COMPAT_VMC2_ACTION, _l(_STR_RESET));
else
diaSetLabel(diaVMCConfig, COMPAT_VMC2_ACTION, _l(_STR_USE_GENERIC));
result = diaExecuteDialog(diaVMCConfig, result, 1, NULL);
if (result == COMPAT_VMC1_DEFINE) {
if (menuCheckParentalLock() == 0) {
if (guiGameShowVMCConfig(id, support, vmc1, 0, 0))
diaGetString(diaVMC, VMC_NAME, vmc1, sizeof(vmc1));
}
} else if (result == COMPAT_VMC2_DEFINE) {
if (menuCheckParentalLock() == 0) {
if (guiGameShowVMCConfig(id, support, vmc2, 1, 0))
diaGetString(diaVMC, VMC_NAME, vmc2, sizeof(vmc2));
}
} else if (result == COMPAT_VMC1_ACTION) {
if (menuCheckParentalLock() == 0) {
if (strlen(vmc1))
vmc1[0] = '\0';
else
snprintf(vmc1, sizeof(vmc1), "generic_%d", 0);
}
} else if (result == COMPAT_VMC2_ACTION) {
if (menuCheckParentalLock() == 0) {
if (strlen(vmc2))
vmc2[0] = '\0';
else
snprintf(vmc2, sizeof(vmc2), "generic_%d", 1);
}
}
} while (result >= COMPAT_NOEXIT);
guiGameShowVMCConfig(id, support, vmc1, 0, 1);
guiGameShowVMCConfig(id, support, vmc2, 1, 1);
}
// GSM
static void guiGameSetGSMSettingsState(void)
{
int previousSource = gGSMSource;
diaGetInt(diaGSConfig, GSMCFG_GSMSOURCE, &gGSMSource);
// update GUI to display per-game or global settings if changed
if (previousSource != gGSMSource && gGSMSource == SETTINGS_GLOBAL) {
config_set_t *configSet = gameMenuLoadConfig(diaGSConfig);
configRemoveKey(configSet, CONFIG_ITEM_GSMSOURCE);
guiGameLoadGSMConfig(configSet, configGetByType(CONFIG_GAME));
} else if (previousSource != gGSMSource && gGSMSource == SETTINGS_PERGAME) {
config_set_t *configSet = gameMenuLoadConfig(diaGSConfig);
configSetInt(configSet, CONFIG_ITEM_GSMSOURCE, gGSMSource);
guiGameLoadGSMConfig(configSet, configGetByType(CONFIG_GAME));
}
diaGetInt(diaGSConfig, GSMCFG_ENABLEGSM, &EnableGSM);
diaSetEnabled(diaGSConfig, GSMCFG_GSMVMODE, EnableGSM);
diaSetEnabled(diaGSConfig, GSMCFG_GSMXOFFSET, EnableGSM);
diaSetEnabled(diaGSConfig, GSMCFG_GSMYOFFSET, EnableGSM);
diaSetEnabled(diaGSConfig, GSMCFG_GSMFIELDFIX, EnableGSM);
}
static int guiGameGSMUpdater(int modified)
{
if (modified) {
guiGameSetGSMSettingsState();
}
return 0;
}
void guiGameShowGSConfig(void)
{
// configure the enumerations
const char *settingsSource[] = {_l(_STR_GLOBAL_SETTINGS), _l(_STR_PERGAME_SETTINGS), NULL};
// clang-format off
const char *gsmvmodeNames[] = {
"NTSC",
"NTSC Non Interlaced",
"PAL",
"PAL Non Interlaced",
"PAL @60Hz",
"PAL @60Hz Non Interlaced",
"PS1 NTSC (HDTV 480p @60Hz)",
"PS1 PAL (HDTV 576p @50Hz)",
"HDTV 480p @60Hz",
"HDTV 576p @50Hz",
"HDTV 720p @60Hz",
"HDTV 1080i @60Hz",
"HDTV 1080i @60Hz Non Interlaced",
"VGA 640x480p @60Hz",
"VGA 640x480p @72Hz",
"VGA 640x480p @75Hz",
"VGA 640x480p @85Hz",
"VGA 640x960i @60Hz",
"VGA 800x600p @56Hz",
"VGA 800x600p @60Hz",
"VGA 800x600p @72Hz",
"VGA 800x600p @75Hz",
"VGA 800x600p @85Hz",
"VGA 1024x768p @60Hz",
"VGA 1024x768p @70Hz",
"VGA 1024x768p @75Hz",
"VGA 1024x768p @85Hz",
"VGA 1280x1024p @60Hz",
"VGA 1280x1024p @75Hz",
NULL};
// clang-format on
diaSetEnum(diaGSConfig, GSMCFG_GSMSOURCE, settingsSource);
diaSetEnum(diaGSConfig, GSMCFG_GSMVMODE, gsmvmodeNames);
diaSetEnabled(diaGSConfig, GSMCFG_GSMVMODE, EnableGSM);
diaSetEnabled(diaGSConfig, GSMCFG_GSMXOFFSET, EnableGSM);
diaSetEnabled(diaGSConfig, GSMCFG_GSMYOFFSET, EnableGSM);
diaSetEnabled(diaGSConfig, GSMCFG_GSMFIELDFIX, EnableGSM);
diaExecuteDialog(diaGSConfig, -1, 1, &guiGameGSMUpdater);
}
// CHEATS
static void guiGameSetCheatSettingsState(void)
{
int previousSource = gCheatSource;
diaGetInt(diaCheatConfig, CHTCFG_CHEATSOURCE, &gCheatSource);
// update GUI to display per-game or global settings if changed
if (previousSource != gCheatSource && gCheatSource == SETTINGS_GLOBAL) {
config_set_t *configSet = gameMenuLoadConfig(diaCheatConfig);
configRemoveKey(configSet, CONFIG_ITEM_CHEATSSOURCE);
guiGameLoadCheatsConfig(configSet, configGetByType(CONFIG_GAME));
} else if (previousSource != gCheatSource && gCheatSource == SETTINGS_PERGAME) {
config_set_t *configSet = gameMenuLoadConfig(diaCheatConfig);
configSetInt(configSet, CONFIG_ITEM_CHEATSSOURCE, gCheatSource);
guiGameLoadCheatsConfig(configSet, configGetByType(CONFIG_GAME));
}
diaGetInt(diaCheatConfig, CHTCFG_ENABLECHEAT, &EnableCheat);
diaGetInt(diaCheatConfig, CHTCFG_CHEATMODE, &CheatMode);
diaSetEnabled(diaCheatConfig, CHTCFG_CHEATMODE, EnableCheat);
}
static int guiGameCheatUpdater(int modified)
{
if (modified) {
guiGameSetCheatSettingsState();
}
return 0;
}
void guiGameShowCheatConfig(void)
{
// configure the enumerations
const char *settingsSource[] = {_l(_STR_GLOBAL_SETTINGS), _l(_STR_PERGAME_SETTINGS), NULL};
const char *cheatmodeNames[] = {_l(_STR_CHEATMODEAUTO), _l(_STR_CHEATMODESELECT), NULL};
diaSetEnum(diaCheatConfig, CHTCFG_CHEATSOURCE, settingsSource);
diaSetEnum(diaCheatConfig, CHTCFG_CHEATMODE, cheatmodeNames);
diaSetEnabled(diaCheatConfig, CHTCFG_CHEATMODE, EnableCheat);
diaExecuteDialog(diaCheatConfig, -1, 1, &guiGameCheatUpdater);
}
// PADEMU
#ifdef PADEMU
// from https://www.bluetooth.com/specifications/assigned-numbers/host-controller-interface
static char *bt_ver_str[] = {
"1.0b",
"1.1",
"1.2",
"2.0 + EDR",
"2.1 + EDR",
"3.0 + HS",
"4.0",
"4.1",
"4.2",
"5.0",
};
static const char *PadEmuPorts_enums[][5] = {
{"1P", "2P", NULL, NULL, NULL},
{"1A", "1B", "1C", "1D", NULL},
{"2A", "2B", "2C", "2D", NULL},
};
#define BtnBit_Off 16
// This enum, along with lookup tables, can be re-used anywhere an enum containing all PS2 buttons is necessary.
static const char *button_names_enum[] = {
"Off",
"Up", "Down", "Left", "Right",
"L1", "R1", "L2", "R2",
"Cross", "Circle", "Square", "Triangle",
"L3", "R3", "Start", "Select",
NULL};
static const char button_enum_to_bit_number[] = {
BtnBit_Off, // Off
DS2BtnBit_Up,
DS2BtnBit_Down,
DS2BtnBit_Left,
DS2BtnBit_Right,
DS2BtnBit_L1,
DS2BtnBit_R1,
DS2BtnBit_L2,
DS2BtnBit_R2,
DS2BtnBit_Cross,
DS2BtnBit_Circle,
DS2BtnBit_Square,
DS2BtnBit_Triangle,
DS2BtnBit_L3,
DS2BtnBit_R3,
DS2BtnBit_Start,
DS2BtnBit_Select,
};
static const char bit_number_to_button_enum[] = {
[BtnBit_Off] = 0, // Off
[DS2BtnBit_Up] = 1,
[DS2BtnBit_Down] = 2,
[DS2BtnBit_Left] = 3,
[DS2BtnBit_Right] = 4,
[DS2BtnBit_L1] = 5,
[DS2BtnBit_R1] = 6,
[DS2BtnBit_L2] = 7,
[DS2BtnBit_R2] = 8,
[DS2BtnBit_Cross] = 9,
[DS2BtnBit_Circle] = 10,
[DS2BtnBit_Square] = 11,
[DS2BtnBit_Triangle] = 12,
[DS2BtnBit_L3] = 13,
[DS2BtnBit_R3] = 14,
[DS2BtnBit_Start] = 15,
[DS2BtnBit_Select] = 16,
};
static u8 ds3_mac[6];
static u8 dg_mac[6];
static char ds3_str[18];
static char dg_str[18];
static char vid_str[5];
static char pid_str[5];
static char rev_str[5];
static char hci_str[26];
static char lmp_str[26];
static char man_str[5];
static int ds3macset = 0;
static int dgmacset = 0;
static int dg_discon = 0;
static int ver_set = 0, feat_set = 0;
static int forceGlobalPadEmu;
static int forceGlobalPadMacro;
static char *bdaddr_to_str(u8 *bdaddr, char *addstr)
{
snprintf(addstr, 18, "%02X:%02X:%02X:%02X:%02X:%02X", bdaddr[0], bdaddr[1], bdaddr[2], bdaddr[3], bdaddr[4], bdaddr[5]);
return addstr;
}
static char *hex_to_str(char *str, u16 hex)
{
sprintf(str, "%04X", hex);
return str;
}
static char *ver_to_str(char *str, u8 ma, u16 mi)
{
if (ma > 9)
ma = 0;
sprintf(str, "%X.%04X BT %s", ma, mi, bt_ver_str[ma]);
return str;
}
static int guiGamePadEmuUpdater(int modified)
{
int PadEmuMode, PadPort, PadEmuVib, PadEmuPort, PadEmuMtap, PadEmuMtapPort, PadEmuWorkaround;
static int oldPadPort;
int previousSource = gPadEmuSource;
diaGetInt(diaPadEmuConfig, PADCFG_PADEMU_SOURCE, &gPadEmuSource);
// update GUI to display per-game or global settings if changed
if (previousSource != gPadEmuSource && gPadEmuSource == SETTINGS_GLOBAL) {
config_set_t *configSet = gameMenuLoadConfig(diaPadEmuConfig);
configRemoveKey(configSet, CONFIG_ITEM_PADEMUSOURCE);
guiGameLoadPadEmuConfig(configSet, configGetByType(CONFIG_GAME));
} else if (previousSource != gPadEmuSource && gPadEmuSource == SETTINGS_PERGAME) {
config_set_t *configSet = gameMenuLoadConfig(diaPadEmuConfig);
configSetInt(configSet, CONFIG_ITEM_PADEMUSOURCE, gPadEmuSource);
guiGameLoadPadEmuConfig(configSet, configGetByType(CONFIG_GAME));
}
diaGetInt(diaPadEmuConfig, PADCFG_PADEMU_ENABLE, &EnablePadEmu);
diaGetInt(diaPadEmuConfig, PADCFG_PADEMU_MODE, &PadEmuMode);
diaGetInt(diaPadEmuConfig, PADCFG_PADPORT, &PadPort);
diaGetInt(diaPadEmuConfig, PADCFG_PADEMU_PORT, &PadEmuPort);
diaGetInt(diaPadEmuConfig, PADCFG_PADEMU_VIB, &PadEmuVib);
diaGetInt(diaPadEmuConfig, PADCFG_PADEMU_MTAP, &PadEmuMtap);
diaGetInt(diaPadEmuConfig, PADCFG_PADEMU_MTAP_PORT, &PadEmuMtapPort);
diaGetInt(diaPadEmuConfig, PADCFG_PADEMU_WORKAROUND, &PadEmuWorkaround);
diaSetEnabled(diaPadEmuConfig, PADCFG_PADEMU_MTAP, EnablePadEmu);
diaSetEnabled(diaPadEmuConfig, PADCFG_PADEMU_MTAP_PORT, PadEmuMtap);
diaSetEnabled(diaPadEmuConfig, PADCFG_PADEMU_MODE, EnablePadEmu);
diaSetEnabled(diaPadEmuConfig, PADCFG_PADPORT, EnablePadEmu);
diaSetEnabled(diaPadEmuConfig, PADCFG_PADEMU_VIB, PadEmuPort & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_USBDG_MAC, (PadEmuMode == 1) & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_PAD_MAC, (PadEmuMode == 1) & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_PAIR, (PadEmuMode == 1) & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_USBDG_MAC_STR, (PadEmuMode == 1) & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_PAD_MAC_STR, (PadEmuMode == 1) & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_PAIR_STR, (PadEmuMode == 1) & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_BTINFO, (PadEmuMode == 1) & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_PADEMU_WORKAROUND, (PadEmuMode == 1) & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_PADEMU_WORKAROUND_STR, (PadEmuMode == 1) & EnablePadEmu);
if (modified) {
if (PadEmuMtap) {
diaSetEnum(diaPadEmuConfig, PADCFG_PADPORT, PadEmuPorts_enums[PadEmuMtapPort]);
diaSetEnabled(diaPadEmuConfig, PADCFG_PADEMU_PORT, (PadPort == 0) & EnablePadEmu);
PadEmuSettings |= 0x00000E00;
} else {
diaSetEnum(diaPadEmuConfig, PADCFG_PADPORT, PadEmuPorts_enums[0]);
diaSetEnabled(diaPadEmuConfig, PADCFG_PADEMU_PORT, EnablePadEmu);
PadEmuSettings &= 0xFFFF03FF;
if (PadPort > 1) {
PadPort = 0;
diaSetInt(diaPadEmuConfig, PADCFG_PADPORT, PadPort);
}
}
if (PadPort != oldPadPort) {
diaSetInt(diaPadEmuConfig, PADCFG_PADEMU_PORT, (PadEmuSettings >> (8 + PadPort)) & 1);
diaSetInt(diaPadEmuConfig, PADCFG_PADEMU_VIB, (PadEmuSettings >> (16 + PadPort)) & 1);
oldPadPort = PadPort;
}
}
PadEmuSettings |= PadEmuMode | (PadEmuPort << (8 + PadPort)) | (PadEmuVib << (16 + PadPort)) | (PadEmuMtap << 24) | ((PadEmuMtapPort - 1) << 25) | (PadEmuWorkaround << 26);
PadEmuSettings &= (~(PadEmuMode ? 0 : 1) & ~(!PadEmuPort << (8 + PadPort)) & ~(!PadEmuVib << (16 + PadPort)) & ~(!PadEmuMtap << 24) & ~(!(PadEmuMtapPort - 1) << 25) & ~(!PadEmuWorkaround << 26));
if (PadEmuMode == 1) {
if (ds34bt_get_status(0) & DS34BT_STATE_USB_CONFIGURED) {
if (dg_discon) {
dgmacset = 0;
dg_discon = 0;
}
if (!dgmacset) {
if (ds34bt_get_bdaddr(dg_mac)) {
dgmacset = 1;
diaSetLabel(diaPadEmuConfig, PADCFG_USBDG_MAC, bdaddr_to_str(dg_mac, dg_str));
} else {
dgmacset = 0;
}
}
} else {
dg_discon = 1;
}
if (!dgmacset) {
diaSetLabel(diaPadEmuConfig, PADCFG_USBDG_MAC, _l(_STR_NOT_CONNECTED));
}
if (ds34usb_get_status(0) & DS34USB_STATE_RUNNING) {
if (!ds3macset) {
if (ds34usb_get_bdaddr(0, ds3_mac)) {
ds3macset = 1;
diaSetLabel(diaPadEmuConfig, PADCFG_PAD_MAC, bdaddr_to_str(ds3_mac, ds3_str));
} else {
ds3macset = 0;
}
}
} else {
diaSetLabel(diaPadEmuConfig, PADCFG_PAD_MAC, _l(_STR_NOT_CONNECTED));
ds3macset = 0;
}
}
return 0;
}
static int guiGamePadEmuInfoUpdater(int modified)
{
hci_information_t info;
u8 feat[8];
int i, j;
u8 data;
int supported;
if (ds34bt_get_status(0) & DS34BT_STATE_USB_CONFIGURED) {
if (!ver_set) {
if (ds34bt_get_version(&info)) {
ver_set = 1;
diaSetLabel(diaPadEmuInfo, PADCFG_VID, hex_to_str(vid_str, info.vid));
diaSetLabel(diaPadEmuInfo, PADCFG_PID, hex_to_str(pid_str, info.pid));
diaSetLabel(diaPadEmuInfo, PADCFG_REV, hex_to_str(rev_str, info.rev));
diaSetLabel(diaPadEmuInfo, PADCFG_HCIVER, ver_to_str(hci_str, info.hci_ver, info.hci_rev));
diaSetLabel(diaPadEmuInfo, PADCFG_LMPVER, ver_to_str(lmp_str, info.lmp_ver, info.lmp_subver));
diaSetLabel(diaPadEmuInfo, PADCFG_MANID, hex_to_str(man_str, info.mf_name));
} else {
ver_set = 0;
}
}
if (!feat_set) {
if (ds34bt_get_features(feat)) {
feat_set = 1;
supported = 0;
for (i = 0, j = 0; i < 64; i++) {
data = (feat[j] >> (i - j * 8)) & 1;
diaSetLabel(diaPadEmuInfo, PADCFG_FEAT_START + i, _l(_STR_NO - data));
j = (i + 1) / 8;
if (i == 25 || i == 26 || i == 39) {
if (data)
supported++;
}
}
if (supported == 3)
diaSetLabel(diaPadEmuInfo, PADCFG_BT_SUPPORTED, _l(_STR_BT_SUPPORTED));
else
diaSetLabel(diaPadEmuInfo, PADCFG_BT_SUPPORTED, _l(_STR_BT_NOTSUPPORTED));
} else {
feat_set = 0;
}
}
} else {
ver_set = 0;
feat_set = 0;
}
return 0;
}
void guiGameShowPadEmuConfig(int forceGlobal)
{
const char *settingsSource[] = {_l(_STR_GLOBAL_SETTINGS), _l(_STR_PERGAME_SETTINGS), NULL};
const char *PadEmuModes[] = {_l(_STR_DS34USB_MODE), _l(_STR_DS34BT_MODE), NULL};
int PadEmuMtap, PadEmuMtapPort, i;
forceGlobalPadEmu = forceGlobal;
diaSetEnabled(diaPadEmuConfig, PADCFG_PADEMU_SOURCE, !forceGlobalPadEmu);
if (forceGlobalPadEmu)
guiGameLoadPadEmuConfig(NULL, configGetByType(CONFIG_GAME));
diaSetEnum(diaPadEmuConfig, PADCFG_PADEMU_SOURCE, settingsSource);
diaSetEnum(diaPadEmuConfig, PADCFG_PADEMU_MODE, PadEmuModes);
PadEmuMtap = (PadEmuSettings >> 24) & 1;
PadEmuMtapPort = ((PadEmuSettings >> 25) & 1) + 1;
diaSetEnabled(diaPadEmuConfig, PADCFG_PADEMU_PORT, EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_USBDG_MAC, PadEmuSettings & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_PAD_MAC, PadEmuSettings & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_PAIR, PadEmuSettings & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_USBDG_MAC_STR, PadEmuSettings & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_PAD_MAC_STR, PadEmuSettings & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_PAIR_STR, PadEmuSettings & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_BTINFO, PadEmuSettings & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_PADEMU_WORKAROUND, PadEmuSettings & EnablePadEmu);
diaSetVisible(diaPadEmuConfig, PADCFG_PADEMU_WORKAROUND_STR, PadEmuSettings & EnablePadEmu);
if (PadEmuMtap) {
diaSetEnum(diaPadEmuConfig, PADCFG_PADPORT, PadEmuPorts_enums[PadEmuMtapPort]);
PadEmuSettings |= 0x00000E00;
} else {
diaSetEnum(diaPadEmuConfig, PADCFG_PADPORT, PadEmuPorts_enums[0]);
PadEmuSettings &= 0xFFFF03FF;
}
int result = -1;
while (result != 0) {
result = diaExecuteDialog(diaPadEmuConfig, result, 1, &guiGamePadEmuUpdater);
if (result == PADCFG_PAIR) {
if (ds3macset && dgmacset) {
if (ds34usb_get_status(0) & DS34USB_STATE_RUNNING) {
if (ds34usb_set_bdaddr(0, dg_mac))
ds3macset = 0;
}
}
}
if (result == PADCFG_BTINFO) {
for (i = PADCFG_FEAT_START; i < PADCFG_FEAT_END + 1; i++)
diaSetLabel(diaPadEmuInfo, i, _l(_STR_NO));
diaSetLabel(diaPadEmuInfo, PADCFG_VID, _l(_STR_NOT_CONNECTED));
diaSetLabel(diaPadEmuInfo, PADCFG_PID, _l(_STR_NOT_CONNECTED));
diaSetLabel(diaPadEmuInfo, PADCFG_REV, _l(_STR_NOT_CONNECTED));
diaSetLabel(diaPadEmuInfo, PADCFG_HCIVER, _l(_STR_NOT_CONNECTED));
diaSetLabel(diaPadEmuInfo, PADCFG_LMPVER, _l(_STR_NOT_CONNECTED));
diaSetLabel(diaPadEmuInfo, PADCFG_MANID, _l(_STR_NOT_CONNECTED));
diaSetLabel(diaPadEmuInfo, PADCFG_BT_SUPPORTED, _l(_STR_NOT_CONNECTED));
ver_set = 0;
feat_set = 0;
diaExecuteDialog(diaPadEmuInfo, -1, 1, &guiGamePadEmuInfoUpdater);
}
if (result == UIID_BTN_OK)
break;
}
}
static int guiGamePadMacroUpdater(int modified)
{
int previousSource = gPadMacroSource;
diaGetInt(diaPadMacroConfig, PADMACRO_CFG_SOURCE, &gPadMacroSource);
// update GUI to display per-game or global settings if changed
if (previousSource != gPadMacroSource && gPadMacroSource == SETTINGS_GLOBAL) {
config_set_t *configSet = gameMenuLoadConfig(diaPadMacroConfig);
configRemoveKey(configSet, CONFIG_ITEM_PADMACROSOURCE);
guiGameLoadPadMacroConfig(configSet, configGetByType(CONFIG_GAME));
} else if (previousSource != gPadMacroSource && gPadMacroSource == SETTINGS_PERGAME) {
config_set_t *configSet = gameMenuLoadConfig(diaPadMacroConfig);
configSetInt(configSet, CONFIG_ITEM_PADMACROSOURCE, gPadMacroSource);
guiGameLoadPadMacroConfig(configSet, configGetByType(CONFIG_GAME));
}
int slowdown_l, slowdown_r;
diaGetInt(diaPadMacroConfig, PADMACRO_SLOWDOWN_L, &slowdown_l);
PadMacroSettings.l_slowdown_enable = slowdown_l != 0 ? 1 : 0;
PadMacroSettings.left_stick_slowdown = button_enum_to_bit_number[slowdown_l];
diaGetInt(diaPadMacroConfig, PADMACRO_SLOWDOWN_R, &slowdown_r);
PadMacroSettings.r_slowdown_enable = slowdown_r != 0 ? 1 : 0;
PadMacroSettings.right_stick_slowdown = button_enum_to_bit_number[slowdown_r];
int toggle_l, toggle_r;
diaGetInt(diaPadMacroConfig, PADMACRO_SLOWDOWN_TOGGLE_L, &toggle_l);
PadMacroSettings.l_slowdown_toggle = toggle_l;
diaGetInt(diaPadMacroConfig, PADMACRO_SLOWDOWN_TOGGLE_R, &toggle_r);
PadMacroSettings.r_slowdown_toggle = toggle_r;
int lx_invert, ly_invert, rx_invert, ry_invert;
diaGetInt(diaPadMacroConfig, PADMACRO_INVERT_LX, &lx_invert);
PadMacroSettings.lx_invert = lx_invert;
diaGetInt(diaPadMacroConfig, PADMACRO_INVERT_LY, &ly_invert);
PadMacroSettings.ly_invert = ly_invert;
diaGetInt(diaPadMacroConfig, PADMACRO_INVERT_RX, &rx_invert);
PadMacroSettings.rx_invert = rx_invert;
diaGetInt(diaPadMacroConfig, PADMACRO_INVERT_RY, &ry_invert);
PadMacroSettings.ry_invert = ry_invert;
int turbo_speed;
diaGetInt(diaPadMacroConfig, PADMACRO_TURBO_SPEED, &turbo_speed);
PadMacroSettings.turbo_speed = 4 - turbo_speed;
return 0;
}
void guiGameShowPadMacroConfig(int forceGlobal)
{
const char *settingsSource[] = {_l(_STR_GLOBAL_SETTINGS), _l(_STR_PERGAME_SETTINGS), NULL};
forceGlobalPadMacro = forceGlobal;
diaSetEnabled(diaPadMacroConfig, PADMACRO_CFG_SOURCE, !forceGlobalPadMacro);
if (forceGlobalPadMacro) {
guiGameLoadPadMacroConfig(NULL, configGetByType(CONFIG_GAME));
}
diaSetEnum(diaPadMacroConfig, PADMACRO_CFG_SOURCE, settingsSource);
diaSetEnum(diaPadMacroConfig, PADMACRO_SLOWDOWN_L, button_names_enum);
diaSetEnum(diaPadMacroConfig, PADMACRO_SLOWDOWN_R, button_names_enum);
const char *toggle_enum[] = {_l(_STR_ACT_WHILE_PRESSED), _l(_STR_ACT_TOGGLED), NULL};
diaSetEnum(diaPadMacroConfig, PADMACRO_SLOWDOWN_TOGGLE_L, toggle_enum);
diaSetEnum(diaPadMacroConfig, PADMACRO_SLOWDOWN_TOGGLE_R, toggle_enum);
int result = -1;
while (result != 0) {
result = diaExecuteDialog(diaPadMacroConfig, result, 1, &guiGamePadMacroUpdater);
if (result == UIID_BTN_OK)
break;
}
}
static int guiGameSavePadEmuGameConfig(config_set_t *configSet, int result)
{
if (gPadEmuSource == SETTINGS_PERGAME) {
diaGetInt(diaPadEmuConfig, PADCFG_PADEMU_ENABLE, &EnablePadEmu);
result = configSetInt(configSet, CONFIG_ITEM_PADEMUSOURCE, gPadEmuSource);
if (EnablePadEmu != 0)
result = configSetInt(configSet, CONFIG_ITEM_ENABLEPADEMU, EnablePadEmu);
else
configRemoveKey(configSet, CONFIG_ITEM_ENABLEPADEMU);
if (PadEmuSettings != 0)
result = configSetInt(configSet, CONFIG_ITEM_PADEMUSETTINGS, PadEmuSettings);
else
configRemoveKey(configSet, CONFIG_ITEM_PADEMUSETTINGS);
}
return result;
}
static int guiGameSavePadMacroGameConfig(config_set_t *configSet, int result)
{
if (gPadMacroSource == SETTINGS_PERGAME) {
result = configSetInt(configSet, CONFIG_ITEM_PADMACROSOURCE, gPadMacroSource);
if (PadMacroSettings.raw != 0)
result = configSetInt(configSet, CONFIG_ITEM_PADMACROSETTINGS, PadMacroSettings.raw);
else
configRemoveKey(configSet, CONFIG_ITEM_PADMACROSETTINGS);
}
return result;
}
void guiGameSavePadEmuGlobalConfig(config_set_t *configGame)
{
if (gPadEmuSource == SETTINGS_GLOBAL) {
configSetInt(configGame, CONFIG_ITEM_ENABLEPADEMU, EnablePadEmu);
configSetInt(configGame, CONFIG_ITEM_PADEMUSETTINGS, PadEmuSettings);
}
}
void guiGameSavePadMacroGlobalConfig(config_set_t *configGame)
{
if (gPadMacroSource == SETTINGS_GLOBAL) {
configSetInt(configGame, CONFIG_ITEM_PADMACROSETTINGS, PadMacroSettings.raw);
}
}
#endif
void guiGameShowCompatConfig(int id, item_list_t *support, config_set_t *configSet)
{
int i;
if (support->flags & MODE_FLAG_COMPAT_DMA) {
const char *dmaModes[] = {"MDMA 0", "MDMA 1", "MDMA 2", "UDMA 0", "UDMA 1", "UDMA 2", "UDMA 3", "UDMA 4", NULL};
diaSetEnum(diaCompatConfig, COMPAT_DMA, dmaModes);
} else {
const char *dmaModes[] = {NULL};
diaSetEnum(diaCompatConfig, COMPAT_DMA, dmaModes);
}
int result = diaExecuteDialog(diaCompatConfig, -1, 1, NULL);
if (result) {
compatMode = 0;
for (i = 0; i < COMPAT_MODE_COUNT; ++i) {
int mdpart;
diaGetInt(diaCompatConfig, COMPAT_MODE_BASE + i, &mdpart);
compatMode |= (mdpart ? 1 : 0) << i;
}
if (result == COMPAT_LOADFROMDISC) {
if (sysGetDiscID(hexDiscID) >= 0)
diaSetString(diaCompatConfig, COMPAT_GAMEID, hexDiscID);
else
guiMsgBox(_l(_STR_ERROR_LOADING_ID), 0, NULL);
}
if (result == COMPAT_DL_DEFAULTS)
guiShowNetCompatUpdateSingle(id, support, configSet);
diaGetInt(diaCompatConfig, COMPAT_DMA, &dmaMode);
diaGetString(diaCompatConfig, COMPAT_GAMEID, hexid, sizeof(hexid));
diaGetString(diaCompatConfig, COMPAT_ALTSTARTUP, altStartup, sizeof(altStartup));
}
}
// sets variables without writing to users cfg file.. follow up with menuSaveConfig() to write
int guiGameSaveConfig(config_set_t *configSet, item_list_t *support)
{
int i;
int result = 0;
config_set_t *configGame = configGetByType(CONFIG_GAME);
compatMode = 0;
for (i = 0; i < COMPAT_MODE_COUNT; ++i) {
int mdpart;
diaGetInt(diaCompatConfig, COMPAT_MODE_BASE + i, &mdpart);
compatMode |= (mdpart ? 1 : 0) << i;
}
if (support->flags & MODE_FLAG_COMPAT_DMA) {
diaGetInt(diaCompatConfig, COMPAT_DMA, &dmaMode);
if (dmaMode != 7)
result = configSetInt(configSet, CONFIG_ITEM_DMA, dmaMode);
else
configRemoveKey(configSet, CONFIG_ITEM_DMA);
}