forked from nefarius/ScpToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
2083 lines (1946 loc) · 63.4 KB
/
Config.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
#include "Global.h"
#include "resource.h"
#include "InputManager.h"
#include "Config.h"
#include "Diagnostics.h"
#include "DeviceEnumerator.h"
#include "KeyboardQueue.h"
#include "WndProcEater.h"
#include "DualShock3.h"
// Needed to know if raw input is available. It requires XP or higher.
#include "RawInput.h"
GeneralConfig config;
// 1 if running inside a PS2 emulator. Set to 1 on any
// of the PS2-specific functions (PS2EgetLibVersion2, PS2EgetLibType).
// Only affects if I allow read input in GS thread to be set.
// Also disables usage of AutoAnalog mode if in PS2 mode.
u8 ps2e = 0;
HWND hWndProp = 0;
int selected = 0;
// Older versions of PCSX2 don't always create the ini dir on startup, so LilyPad does it
// for it. But if PCSX2 sets the ini path with a call to setSettingsDir, then it means
// we shouldn't make our own.
bool createIniDir = true;
HWND hWnds[2][4];
HWND hWndGeneral = 0;
struct GeneralSettingsBool {
wchar_t *name;
unsigned int ControlId;
u8 defaultValue;
};
// Ties together config data structure, config files, and general config
// dialog.
const GeneralSettingsBool BoolOptionsInfo[] = {
{L"Force Cursor Hide", IDC_FORCE_HIDE, 0},
{L"Mouse Unfocus", IDC_MOUSE_UNFOCUS, 1},
{L"Background", IDC_BACKGROUND, 1},
{L"Multiple Bindings", IDC_MULTIPLE_BINDING, 0},
{L"DirectInput Game Devices", IDC_G_DI, 1},
{L"XInput", IDC_G_XI, 1},
{L"DualShock 3", IDC_G_DS3, 0},
{L"Multitap 1", IDC_MULTITAP1, 0},
{L"Multitap 2", IDC_MULTITAP2, 0},
{L"Escape Fullscreen Hack", IDC_ESCAPE_FULLSCREEN_HACK, 1},
{L"Disable Screen Saver", IDC_DISABLE_SCREENSAVER, 1},
{L"Logging", IDC_DEBUG_FILE, 0},
{L"Save State in Title", IDC_SAVE_STATE_TITLE, 0}, //No longer required, PCSX2 now handles it - avih 2011-05-17
{L"GH2", IDC_GH2_HACK, 0},
{L"Turbo Key Hack", IDC_TURBO_KEY_HACK, 0},
{L"Vista Volume", IDC_VISTA_VOLUME, 1},
};
void Populate(int port, int slot);
void SetupLogSlider(HWND hWndSlider) {
SendMessage(hWndSlider, TBM_SETRANGEMIN, 0, 1);
SendMessage(hWndSlider, TBM_SETPAGESIZE, 0, 1<<13);
SendMessage(hWndSlider, TBM_SETRANGEMAX, 1, 1<<22);
SendMessage(hWndSlider, TBM_SETTIC, 0, 1<<21);
SendMessage(hWndSlider, TBM_SETTIC, 0, 1<<20);
SendMessage(hWndSlider, TBM_SETTIC, 0, 3<<20);
}
int GetLogSliderVal(HWND hWnd, int id) {
HWND hWndSlider = GetDlgItem(hWnd, id);
int val = SendMessage(hWndSlider, TBM_GETPOS, 0, 0);
if (val <= (1<<21)) {
val = (val+16) >> 5;
}
else {
double v = (val - (1<<21))/(double)((1<<22)- (1<<21));
double max = log((double)(1<<23));
double min = log((double)BASE_SENSITIVITY);
v = v*(max-min)+min;
// exp is not implemented in ntdll, so use pow instead.
// More than accurate enough for my needs.
// v = exp(v);
v = pow(2.7182818284590451, v);
if (v > (1<<23)) v = (1<<23);
val = (int)floor(v+0.5);
}
if (!val) val = 1;
if (IsDlgButtonChecked(hWnd, id+1) == BST_CHECKED) {
val = -val;
}
return val;
}
void SetLogSliderVal(HWND hWnd, int id, HWND hWndText, int val) {
HWND hWndSlider = GetDlgItem(hWnd, id);
int sliderPos = 0;
wchar_t temp[30];
EnableWindow(hWndSlider, val != 0);
EnableWindow(hWndText, val != 0);
EnableWindow(GetDlgItem(hWnd, id+1), val != 0);
if (!val) val = BASE_SENSITIVITY;
CheckDlgButton(hWnd, id+1, BST_CHECKED * (val<0));
if (val < 0) {
val = -val;
}
if (val <= BASE_SENSITIVITY) {
sliderPos = val<<5;
}
else {
double max = log((double)(1<<23));
double min = log((double)BASE_SENSITIVITY);
double v = log((double)(val));
v = (v-min)/(max-min);
v = ((1<<22)- (1<<21)) *v + (1<<21);
// _ftol
sliderPos = (int)(floor(v+0.5));
}
SendMessage(hWndSlider, TBM_SETPOS, 1, sliderPos);
int val2 = (int)(1000*(__int64)val/BASE_SENSITIVITY);
wsprintfW(temp, L"%i.%03i", val2/1000, val2%1000);
SetWindowTextW(hWndText, temp);
}
void RefreshEnabledDevices(int updateDeviceList) {
// Clears all device state.
static int lastXInputState = -1;
if (updateDeviceList || lastXInputState != config.gameApis.xInput) {
EnumDevices(config.gameApis.xInput);
lastXInputState = config.gameApis.xInput;
}
for (int i=0; i<dm->numDevices; i++) {
Device *dev = dm->devices[i];
if (!dev->attached && dev->displayName[0] != '[') {
wchar_t *newName = (wchar_t*) malloc(sizeof(wchar_t) * (wcslen(dev->displayName) + 12));
wsprintfW(newName, L"[Detached] %s", dev->displayName);
free(dev->displayName);
dev->displayName = newName;
}
if ((dev->type == KEYBOARD && dev->api == IGNORE_KEYBOARD) ||
(dev->type == KEYBOARD && dev->api == config.keyboardApi) ||
(dev->type == MOUSE && dev->api == config.mouseApi) ||
(dev->type == OTHER &&
((dev->api == DI && config.gameApis.directInput) ||
(dev->api == DS3 && config.gameApis.dualShock3) ||
(dev->api == XINPUT && config.gameApis.xInput)))) {
if (config.gameApis.dualShock3 && dev->api == DI && dev->displayName &&
!wcsicmp(dev->displayName, L"DX PLAYSTATION(R)3 Controller")) {
dm->DisableDevice(i);
}
else {
dm->EnableDevice(i);
}
}
else {
dm->DisableDevice(i);
}
}
}
// Disables/enables devices as necessary. Also updates diagnostic list
// and pad1/pad2 bindings lists, depending on params. Also updates device
// list, if indicated.
// Must be called at some point when entering config mode, as I disable
// (non-keyboard/mice) devices with no active bindings when the emulator's running.
void RefreshEnabledDevicesAndDisplay(int updateDeviceList = 0, HWND hWnd = 0, int populate=0) {
RefreshEnabledDevices(updateDeviceList);
if (hWnd) {
HWND hWndList = GetDlgItem(hWnd, IDC_LIST);
ListView_SetExtendedListViewStyleEx(hWndList, LVS_EX_DOUBLEBUFFER, LVS_EX_DOUBLEBUFFER);
int count = ListView_GetItemCount(hWndList);
LVITEM item;
item.iItem = 0;
item.iSubItem = 0;
item.mask = LVIF_TEXT | LVIF_PARAM;
for (int j=0; j<dm->numDevices; j++) {
if (dm->devices[j]->enabled && dm->devices[j]->api != IGNORE_KEYBOARD) {
item.lParam = j;
item.pszText = dm->devices[j]->displayName;
if (count > 0) {
ListView_SetItem(hWndList, &item);
count --;
}
else {
ListView_InsertItem(hWndList, &item);
}
item.iItem++;
}
}
// This way, won't scroll list to start.
while(count > 0) {
ListView_DeleteItem(hWndList, item.iItem);
count --;
}
}
if (populate) {
for (int i=0; i<8; i++) {
Populate(i&1, i>>1);
}
}
}
wchar_t *GetCommandStringW(u8 command, int port, int slot) {
static wchar_t temp[34];
if (command >= 0x20 && command <= 0x27) {
if (config.padConfigs[port][slot].type == GuitarPad && (command == 0x20 || command == 0x22)) {
HWND hWnd = GetDlgItem(hWnds[port][slot], 0x10F0+command);
int res = GetWindowTextW(hWnd, temp, 20);
if ((unsigned int)res-1 <= 18) return temp;
}
static wchar_t stick[2] = {'L', 'R'};
static wchar_t *dir[] = {
L"Up", L"Right",
L"Down", L"Left"};
wsprintfW(temp, L"%c-Stick %s", stick[(command-0x20)/4], dir[command & 3]);
return temp;
}
/* Get text from the buttons. */
if (command >= 0x0C && command <=0x28) {
HWND hWnd = GetDlgItem(hWnds[port][slot], 0x10F0+command);
if (!hWnd) {
wchar_t *strings[] = {
L"Lock Buttons",
L"Lock Input",
L"Lock Direction",
L"Mouse",
L"Select",
L"L3",
L"R3",
L"Start",
L"Up",
L"Right",
L"Down",
L"Left",
L"L2",
L"R2",
L"L1",
L"R1",
L"Triangle",
L"Circle",
L"Square",
L"Cross",
L"L-Stick Up",
L"L-Stick Right",
L"L-Stick Down",
L"L-Stick Left",
L"R-Stick Up",
L"R-Stick Right",
L"R-Stick Down",
L"R-Stick Left",
L"Analog",
};
return strings[command - 0xC];
}
int res = GetWindowTextW(hWnd, temp, 20);
if ((unsigned int)res-1 <= 18) return temp;
}
else if (command == 0x7F) {
return L"Ignore Key";
}
return L"";
}
static wchar_t iniFile[MAX_PATH*2] = L"inis/LilyPad.ini";
void CALLBACK PADsetSettingsDir( const char *dir )
{
//swprintf_s( iniFile, L"%S", (dir==NULL) ? "inis" : dir );
//uint targlen = MultiByteToWideChar(CP_ACP, 0, dir, -1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, dir, -1, iniFile, MAX_PATH*2);
wcscat_s(iniFile, L"/LilyPad-SCP.ini");
createIniDir = false;
}
int GetBinding(int port, int slot, int index, Device *&dev, Binding *&b, ForceFeedbackBinding *&ffb);
int BindCommand(Device *dev, unsigned int uid, unsigned int port, unsigned int slot, int command, int sensitivity, int turbo, int deadZone);
int CreateEffectBinding(Device *dev, wchar_t *effectName, unsigned int port, unsigned int slot, unsigned int motor, ForceFeedbackBinding **binding);
void SelChanged(int port, int slot) {
HWND hWnd = hWnds[port][slot];
if (!hWnd) return;
HWND hWndTemp, hWndList = GetDlgItem(hWnd, IDC_LIST);
int j, i = ListView_GetSelectedCount(hWndList);
wchar_t *devName = L"N/A";
wchar_t *key = L"N/A";
wchar_t *command = L"N/A";
// Second value is now turbo.
int turbo = -1;
int sensitivity = 0;
int deadZone = 0;
int nonButtons = 0;
// Set if sensitivity != 0, but need to disable flip anyways.
// Only used to relative axes.
int disableFlip = 0;
wchar_t temp[4][1000];
Device *dev;
int bFound = 0;
int ffbFound = 0;
ForceFeedbackBinding *ffb = 0;
Binding *b = 0;
if (i >= 1) {
int index = -1;
int flipped = 0;
while (1) {
index = ListView_GetNextItem(hWndList, index, LVNI_SELECTED);
if (index < 0) break;
LVITEMW item;
item.iItem = index;
item.mask = LVIF_TEXT;
item.pszText = temp[3];
for (j=0; j<3; j++) {
item.iSubItem = j;
item.cchTextMax = sizeof(temp[0])/sizeof(temp[3][0]);
if (!ListView_GetItem(hWndList, &item)) break;
if (!bFound && !ffbFound)
wcscpy(temp[j], temp[3]);
else if (wcsicmp(temp[j], temp[3])) {
int q = 0;
while (temp[j][q] == temp[3][q]) q++;
if (q && temp[j][q-1] == ' ' && temp[j][q] && temp[j][q+1] == 0) q--;
if (j == 1) {
// Really ugly, but merges labels for multiple directions for same axis.
if ((temp[j][q] == 0 || (temp[j][q] == ' ' && temp[j][q+2] == 0)) &&
(temp[3][q] == 0 || (temp[3][q] == ' ' && temp[3][q+2] == 0))) {
temp[j][q] = 0;
continue;
}
}
// Merge different directions for same stick.
else if (j == 2 && q > 4) {
temp[j][q] = 0;
continue;
}
wcscpy(temp[j], L"*");
}
}
if (j == 3) {
devName = temp[0];
key = temp[1];
command = temp[2];
if (GetBinding(port, slot, index, dev, b, ffb)) {
if (b) {
bFound ++;
VirtualControl *control = &dev->virtualControls[b->controlIndex];
// Ignore
if (b->command != 0x7F) {
// Only relative axes can't have negative sensitivity.
if (((control->uid >> 16) & 0xFF) == RELAXIS) {
disableFlip = 1;
}
turbo += b->turbo;
if (b->sensitivity < 0) {
flipped ++;
sensitivity -= b->sensitivity;
}
else {
sensitivity += b->sensitivity;
}
if (((control->uid >> 16)&0xFF) != PSHBTN && ((control->uid >> 16)&0xFF) != TGLBTN) {
deadZone += b->deadZone;
nonButtons++;
}
}
else disableFlip = 1;
}
else ffbFound++;
}
}
}
if ((bFound && ffbFound) || ffbFound > 1) {
ffb = 0;
turbo = -1;
deadZone = 0;
sensitivity = 0;
disableFlip = 1;
bFound = ffbFound = 0;
}
else if (bFound) {
turbo++;
sensitivity /= bFound;
if (nonButtons) {
deadZone /= nonButtons;
}
if (bFound > 1) disableFlip = 1;
else if (flipped) {
sensitivity = -sensitivity;
}
}
}
for (i=IDC_SLIDER1; i<ID_DELETE; i++) {
hWndTemp = GetDlgItem(hWnd, i);
if (hWndTemp)
ShowWindow(hWndTemp, ffb == 0);
}
for (i=0x1300; i<0x1400; i++) {
hWndTemp = GetDlgItem(hWnd, i);
if (hWndTemp) {
int enable = ffb != 0;
if (ffb && i >= IDC_FF_AXIS1_ENABLED) {
int index = (i - IDC_FF_AXIS1_ENABLED) / 16;
if (index >= dev->numFFAxes) {
enable = 0;
}
else {
int type = i - index*16;
AxisEffectInfo *info = 0;
if (dev->numFFAxes > index) {
info = ffb->axes+index;
}
if (type == IDC_FF_AXIS1) {
// takes care of IDC_FF_AXIS1_FLIP as well.
SetupLogSlider(hWndTemp);
int force = 0;
if (info) {
force = info->force;
}
SetLogSliderVal(hWnd, i, GetDlgItem(hWnd, i-IDC_FF_AXIS1+IDC_FF_AXIS1_SCALE), force);
}
else if (type == IDC_FF_AXIS1_ENABLED) {
CheckDlgButton(hWnd, i, BST_CHECKED * (info->force!=0));
wsprintfW(temp[0], L"Axis %i", index+1);
if (dev->ffAxes[index].displayName && wcslen(dev->ffAxes[index].displayName) < 50) {
wchar_t *end = wcschr(temp[0], 0);
wsprintfW(end, L": %s", dev->ffAxes[index].displayName);
}
SetWindowText(hWndTemp, temp[0]);
}
}
}
ShowWindow(hWndTemp, enable);
}
}
if (!ffb) {
SetLogSliderVal(hWnd, IDC_SLIDER1, GetDlgItem(hWnd, IDC_AXIS_SENSITIVITY1), sensitivity);
SetLogSliderVal(hWnd, IDC_SLIDER_DEADZONE, GetDlgItem(hWnd, IDC_AXIS_DEADZONE), deadZone);
if (disableFlip) EnableWindow(GetDlgItem(hWnd, IDC_FLIP1), 0);
EnableWindow(GetDlgItem(hWnd, IDC_TURBO), turbo >= 0);
if (turbo > 0 && turbo < bFound) {
SendMessage(GetDlgItem(hWnd, IDC_TURBO), BM_SETSTYLE, BS_AUTO3STATE, 0);
CheckDlgButton(hWnd, IDC_TURBO, BST_INDETERMINATE);
}
else {
SendMessage(GetDlgItem(hWnd, IDC_TURBO), BM_SETSTYLE, BS_AUTOCHECKBOX, 0);
CheckDlgButton(hWnd, IDC_TURBO, BST_CHECKED * (bFound && turbo == bFound));
}
HWND hWndCombo = GetDlgItem(hWnd, IDC_AXIS_DIRECTION);
int enableCombo = 0;
SendMessage(hWndCombo, CB_RESETCONTENT, 0, 0);
if (b && bFound == 1) {
VirtualControl *control = &dev->virtualControls[b->controlIndex];
unsigned int uid = control->uid;
if (((uid>>16) & 0xFF) == ABSAXIS) {
enableCombo = 1;
wchar_t *endings[3] = {L" -", L" +", L""};
wchar_t *string = temp[3];
wcscpy(string, key);
wchar_t *end = wcschr(string, 0);
int sel = 2;
if (!(uid & UID_AXIS)) {
end[-2] = 0;
sel = (end[-1] == '+');
end -= 2;
}
for (int i=0; i<3; i++) {
wcscpy(end, endings[i]);
SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM) string);
if (i == sel)
SendMessage(hWndCombo, CB_SETCURSEL, i, 0);
}
}
}
EnableWindow(hWndCombo, enableCombo);
if (!enableCombo) {
SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM) key);
SendMessage(hWndCombo, CB_SELECTSTRING, i, (LPARAM) key);
}
SetDlgItemText(hWnd, IDC_AXIS_DEVICE1, devName);
SetDlgItemText(hWnd, IDC_AXIS_CONTROL1, command);
}
else {
wchar_t temp2[2000];
wsprintfW(temp2, L"%s / %s", devName, command);
SetDlgItemText(hWnd, ID_FF, temp2);
hWndTemp = GetDlgItem(hWnd, IDC_FF_EFFECT);
SendMessage(hWndTemp, CB_RESETCONTENT, 0, 0);
for (i=0; i<dev->numFFEffectTypes; i++) {
SendMessage(hWndTemp, CB_INSERTSTRING, i, (LPARAM)dev->ffEffectTypes[i].displayName);
}
SendMessage(hWndTemp, CB_SETCURSEL, ffb->effectIndex, 0);
EnableWindow(hWndTemp, dev->numFFEffectTypes>1);
}
}
void UnselectAll(HWND hWnd) {
int i = ListView_GetSelectedCount(hWnd);
while (i-- > 0) {
int index = ListView_GetNextItem(hWnd, -1, LVNI_SELECTED);
ListView_SetItemState(hWnd, index, 0, LVIS_SELECTED);
}
}
int GetItemIndex(int port, int slot, Device *dev, ForceFeedbackBinding *binding) {
int count = 0;
for (int i = 0; i<dm->numDevices; i++) {
Device *dev2 = dm->devices[i];
if (!dev2->enabled) continue;
if (dev2 != dev) {
count += dev2->pads[port][slot].numBindings + dev2->pads[port][slot].numFFBindings;
continue;
}
return count += dev2->pads[port][slot].numBindings + (binding - dev2->pads[port][slot].ffBindings);
}
return -1;
}
int GetItemIndex(int port, int slot, Device *dev, Binding *binding) {
int count = 0;
for (int i = 0; i<dm->numDevices; i++) {
Device *dev2 = dm->devices[i];
if (!dev2->enabled) continue;
if (dev2 != dev) {
count += dev2->pads[port][slot].numBindings + dev2->pads[port][slot].numFFBindings;
continue;
}
return count += binding - dev->pads[port][slot].bindings;
}
return -1;
}
// Doesn't check if already displayed.
int ListBoundCommand(int port, int slot, Device *dev, Binding *b) {
if (!hWnds[port][slot]) return -1;
HWND hWndList = GetDlgItem(hWnds[port][slot], IDC_LIST);
int index = -1;
if (hWndList) {
index = GetItemIndex(port, slot, dev, b);
if (index >= 0) {
LVITEM item;
item.mask = LVIF_TEXT;
item.pszText = dev->displayName;
item.iItem = index;
item.iSubItem = 0;
SendMessage(hWndList, LVM_INSERTITEM, 0, (LPARAM)&item);
item.mask = LVIF_TEXT;
item.iSubItem = 1;
item.pszText = dev->GetVirtualControlName(&dev->virtualControls[b->controlIndex]);
SendMessage(hWndList, LVM_SETITEM, 0, (LPARAM)&item);
item.iSubItem = 2;
item.pszText = GetCommandStringW(b->command, port, slot);
SendMessage(hWndList, LVM_SETITEM, 0, (LPARAM)&item);
}
}
return index;
}
int ListBoundEffect(int port, int slot, Device *dev, ForceFeedbackBinding *b) {
if (!hWnds[port][slot]) return -1;
HWND hWndList = GetDlgItem(hWnds[port][slot], IDC_LIST);
int index = -1;
if (hWndList) {
index = GetItemIndex(port, slot, dev, b);
if (index >= 0) {
LVITEM item;
item.mask = LVIF_TEXT;
item.pszText = dev->displayName;
item.iItem = index;
item.iSubItem = 0;
SendMessage(hWndList, LVM_INSERTITEM, 0, (LPARAM)&item);
item.mask = LVIF_TEXT;
item.iSubItem = 1;
item.pszText = dev->ffEffectTypes[b->effectIndex].displayName;
SendMessage(hWndList, LVM_SETITEM, 0, (LPARAM)&item);
item.iSubItem = 2;
wchar_t *ps2Motors[2] = {L"Big Motor", L"Small Motor"};
item.pszText = ps2Motors[b->motor];
SendMessage(hWndList, LVM_SETITEM, 0, (LPARAM)&item);
}
}
return index;
}
// Only for use with control bindings. Affects all highlighted bindings.
void ChangeValue(int port, int slot, int *newSensitivity, int *newTurbo, int *newDeadZone) {
if (!hWnds[port][slot]) return;
HWND hWndList = GetDlgItem(hWnds[port][slot], IDC_LIST);
int count = ListView_GetSelectedCount(hWndList);
if (count < 1) return;
int index = -1;
while (1) {
index = ListView_GetNextItem(hWndList, index, LVNI_SELECTED);
if (index < 0) break;
Device *dev;
Binding *b;
ForceFeedbackBinding *ffb;
if (!GetBinding(port, slot, index, dev, b, ffb) || ffb) return;
if (newSensitivity) {
// Don't change flip state when modifying multiple controls.
if (count > 1 && b->sensitivity < 0)
b->sensitivity = -*newSensitivity;
else
b->sensitivity = *newSensitivity;
}
if (newDeadZone) {
if (b->deadZone) {
b->deadZone = *newDeadZone;
}
}
if (newTurbo) {
b->turbo = *newTurbo;
}
}
PropSheet_Changed(hWndProp, hWnds[port][slot]);
SelChanged(port, slot);
}
// Only for use with effect bindings.
void ChangeEffect(int port, int slot, int id, int *newForce, unsigned int *newEffectType) {
if (!hWnds[port][slot]) return;
HWND hWndList = GetDlgItem(hWnds[port][slot], IDC_LIST);
int i = ListView_GetSelectedCount(hWndList);
if (i != 1) return;
int index = ListView_GetNextItem(hWndList, -1, LVNI_SELECTED);
Device *dev;
Binding *b;
ForceFeedbackBinding *ffb;
if (!GetBinding(port, slot, index, dev, b, ffb) || b) return;
if (newForce) {
unsigned int axisIndex = (id - IDC_FF_AXIS1_ENABLED)/16;
if (axisIndex < (unsigned int)dev->numFFAxes) {
ffb->axes[axisIndex].force = *newForce;
}
}
if (newEffectType && *newEffectType < (unsigned int)dev->numFFEffectTypes) {
ffb->effectIndex = *newEffectType;
ListView_DeleteItem(hWndList, index);
index = ListBoundEffect(port, slot, dev, ffb);
ListView_SetItemState(hWndList, index, LVIS_SELECTED, LVIS_SELECTED);
}
PropSheet_Changed(hWndProp, hWnds[port][slot]);
SelChanged(port, slot);
}
void Populate(int port, int slot) {
if (!hWnds[port][slot]) return;
HWND hWnd = GetDlgItem(hWnds[port][slot], IDC_LIST);
ListView_DeleteAllItems(hWnd);
int i, j;
int multipleBinding = config.multipleBinding;
config.multipleBinding = 1;
for (j=0; j<dm->numDevices; j++) {
Device *dev = dm->devices[j];
if (!dev->enabled) continue;
for (i=0; i<dev->pads[port][slot].numBindings; i++) {
ListBoundCommand(port, slot, dev, dev->pads[port][slot].bindings+i);
}
for (i=0; i<dev->pads[port][slot].numFFBindings; i++) {
ListBoundEffect(port, slot, dev, dev->pads[port][slot].ffBindings+i);
}
}
config.multipleBinding = multipleBinding;
hWnd = GetDlgItem(hWnds[port][slot], IDC_FORCEFEEDBACK);
SendMessage(hWnd, CB_RESETCONTENT, 0, 0);
int added = 0;
for (i=0; i<dm->numDevices; i++) {
Device *dev = dm->devices[i];
if (dev->enabled && dev->numFFAxes && dev->numFFEffectTypes) {
SendMessage(hWnd, CB_INSERTSTRING, added, (LPARAM)dev->displayName);
SendMessage(hWnd, CB_SETITEMDATA, added, i);
added++;
}
}
SendMessage(hWnd, CB_SETCURSEL, 0, 0);
EnableWindow(hWnd, added!=0);
EnableWindow(GetDlgItem(hWnds[port][slot], ID_BIG_MOTOR), added!=0);
EnableWindow(GetDlgItem(hWnds[port][slot], ID_SMALL_MOTOR), added!=0);
SelChanged(port, slot);
}
int WritePrivateProfileInt(wchar_t *s1, wchar_t *s2, int v, wchar_t *ini) {
wchar_t temp[100];
_itow(v, temp, 10);
return WritePrivateProfileStringW(s1, s2, temp, ini);
}
void SetVolume(int volume) {
if (volume > 100) volume = 100;
if (volume < 0) volume = 0;
config.volume = volume;
unsigned int val = 0xFFFF * volume/100;
val = val | (val<<16);
for (int i=waveOutGetNumDevs()-1; i>=0; i--) {
waveOutSetVolume((HWAVEOUT)i, val);
}
WritePrivateProfileInt(L"General Settings", L"Volume", config.volume, iniFile);
}
int SaveSettings(wchar_t *file=0) {
// Need this either way for saving path.
if (!file) {
file = iniFile;
}
else {
wchar_t *c = wcsrchr(file, '\\');
if (*c) {
*c = 0;
wcscpy(config.lastSaveConfigPath, file);
wcscpy(config.lastSaveConfigFileName, c+1);
*c = '\\';
}
}
DeleteFileW(file);
WritePrivateProfileStringW(L"General Settings", L"Last Config Path", config.lastSaveConfigPath, iniFile);
WritePrivateProfileStringW(L"General Settings", L"Last Config Name", config.lastSaveConfigFileName, iniFile);
// Just check first, last, and all pad bindings. Should be more than enough. No real need to check
// config path.
int noError = 1;
for (int i=0; i<sizeof(BoolOptionsInfo)/sizeof(BoolOptionsInfo[0]); i++) {
noError &= WritePrivateProfileInt(L"General Settings", BoolOptionsInfo[i].name, config.bools[i], file);
}
WritePrivateProfileInt(L"General Settings", L"Close Hacks", config.closeHacks, file);
WritePrivateProfileInt(L"General Settings", L"Keyboard Mode", config.keyboardApi, file);
WritePrivateProfileInt(L"General Settings", L"Mouse Mode", config.mouseApi, file);
WritePrivateProfileInt(L"General Settings", L"Volume", config.volume, file);
for (int port=0; port<2; port++) {
for (int slot=0; slot<4; slot++) {
wchar_t temp[50];
wsprintf(temp, L"Pad %i %i", port, slot);
WritePrivateProfileInt(temp, L"Mode", config.padConfigs[port][slot].type, file);
noError &= WritePrivateProfileInt(temp, L"Auto Analog", config.padConfigs[port][slot].autoAnalog, file);
}
}
for (int i=0; i<dm->numDevices; i++) {
wchar_t id[50];
wchar_t temp[50], temp2[1000];
wsprintfW(id, L"Device %i", i);
Device *dev = dm->devices[i];
wchar_t *name = dev->displayName;
while (name[0] == '[') {
wchar_t *name2 = wcschr(name, ']');
if (!name2) break;
name = name2+1;
while (iswspace(name[0])) name++;
}
WritePrivateProfileStringW(id, L"Display Name", name, file);
WritePrivateProfileStringW(id, L"Instance ID", dev->instanceID, file);
if (dev->productID) {
WritePrivateProfileStringW(id, L"Product ID", dev->productID, file);
}
WritePrivateProfileInt(id, L"API", dev->api, file);
WritePrivateProfileInt(id, L"Type", dev->type, file);
int ffBindingCount = 0;
int bindingCount = 0;
for (int port=0; port<2; port++) {
for (int slot=0; slot<4; slot++) {
for (int j=0; j<dev->pads[port][slot].numBindings; j++) {
Binding *b = dev->pads[port][slot].bindings+j;
VirtualControl *c = &dev->virtualControls[b->controlIndex];
wsprintfW(temp, L"Binding %i", bindingCount++);
wsprintfW(temp2, L"0x%08X, %i, %i, %i, %i, %i, %i", c->uid, port, b->command, b->sensitivity, b->turbo, slot, b->deadZone);
noError &= WritePrivateProfileStringW(id, temp, temp2, file);
}
for (int j=0; j<dev->pads[port][slot].numFFBindings; j++) {
ForceFeedbackBinding *b = dev->pads[port][slot].ffBindings+j;
ForceFeedbackEffectType *eff = &dev->ffEffectTypes[b->effectIndex];
wsprintfW(temp, L"FF Binding %i", ffBindingCount++);
wsprintfW(temp2, L"%s %i, %i, %i", eff->effectID, port, b->motor, slot);
for (int k=0; k<dev->numFFAxes; k++) {
ForceFeedbackAxis *axis = dev->ffAxes + k;
AxisEffectInfo *info = b->axes + k;
wsprintfW(wcschr(temp2,0), L", %i, %i", axis->id, info->force);
}
noError &= WritePrivateProfileStringW(id, temp, temp2, file);
}
}
}
}
if (!noError) {
MessageBoxA(hWndProp, "Unable to save settings. Make sure the disk is not full or write protected, the file isn't write protected, and that the app has permissions to write to the directory. On Vista, try running in administrator mode.", "Error Writing Configuration File", MB_OK | MB_ICONERROR);
}
return !noError;
}
u8 GetPrivateProfileBool(wchar_t *s1, wchar_t *s2, int def, wchar_t *ini) {
return (0!=GetPrivateProfileIntW(s1, s2, def, ini));
}
int LoadSettings(int force, wchar_t *file) {
if (dm && !force) return 0;
if( createIniDir )
{
CreateDirectory(L"inis", 0);
createIniDir = false;
}
// Could just do ClearDevices() instead, but if I ever add any extra stuff,
// this will still work.
UnloadConfigs();
dm = new InputDeviceManager();
if (!file) {
file = iniFile;
GetPrivateProfileStringW(L"General Settings", L"Last Config Path", L"inis", config.lastSaveConfigPath, sizeof(config.lastSaveConfigPath), file);
GetPrivateProfileStringW(L"General Settings", L"Last Config Name", L"LilyPad.lily", config.lastSaveConfigFileName, sizeof(config.lastSaveConfigFileName), file);
}
else {
wchar_t *c = wcsrchr(file, '\\');
if (c) {
*c = 0;
wcscpy(config.lastSaveConfigPath, file);
wcscpy(config.lastSaveConfigFileName, c+1);
*c = '\\';
WritePrivateProfileStringW(L"General Settings", L"Last Config Path", config.lastSaveConfigPath, iniFile);
WritePrivateProfileStringW(L"General Settings", L"Last Config Name", config.lastSaveConfigFileName, iniFile);
}
}
for (int i=0; i<sizeof(BoolOptionsInfo)/sizeof(BoolOptionsInfo[0]); i++) {
config.bools[i] = GetPrivateProfileBool(L"General Settings", BoolOptionsInfo[i].name, BoolOptionsInfo[i].defaultValue, file);
}
config.closeHacks = (u8)GetPrivateProfileIntW(L"General Settings", L"Close Hacks", 0, file);
if (config.closeHacks&1) config.closeHacks &= ~2;
config.keyboardApi = (DeviceAPI)GetPrivateProfileIntW(L"General Settings", L"Keyboard Mode", WM, file);
if (!config.keyboardApi) config.keyboardApi = WM;
config.mouseApi = (DeviceAPI) GetPrivateProfileIntW(L"General Settings", L"Mouse Mode", 0, file);
config.volume = GetPrivateProfileInt(L"General Settings", L"Volume", 100, file);
OSVERSIONINFO os;
os.dwOSVersionInfoSize = sizeof(os);
config.osVersion = 0;
if (GetVersionEx(&os)) {
config.osVersion = os.dwMajorVersion;
}
if (config.osVersion < 6) config.vistaVolume = 0;
if (!config.vistaVolume) config.volume = 100;
if (config.vistaVolume) SetVolume(config.volume);
if (!InitializeRawInput()) {
if (config.keyboardApi == RAW) config.keyboardApi = WM;
if (config.mouseApi == RAW) config.mouseApi = WM;
}
if (config.debug) {
CreateDirectory(L"logs", 0);
}
for (int port=0; port<2; port++) {
for (int slot=0; slot<4; slot++) {
wchar_t temp[50];
wsprintf(temp, L"Pad %i %i", port, slot);
config.padConfigs[port][slot].type = (PadType) GetPrivateProfileInt(temp, L"Mode", Dualshock2Pad, file);
config.padConfigs[port][slot].autoAnalog = GetPrivateProfileBool(temp, L"Auto Analog", 0, file);
}
}
int i=0;
int multipleBinding = config.multipleBinding;
// Disabling multiple binding only prevents new multiple bindings.
config.multipleBinding = 1;
while (1) {
wchar_t id[50];
wchar_t temp[50], temp2[1000], temp3[1000], temp4[1000];
wsprintfW(id, L"Device %i", i++);
if (!GetPrivateProfileStringW(id, L"Display Name", 0, temp2, 1000, file) || !temp2[0] ||
!GetPrivateProfileStringW(id, L"Instance ID", 0, temp3, 1000, file) || !temp3[0]) {
if (i >= 100) break;
continue;
}
wchar_t *id2 = 0;
if (GetPrivateProfileStringW(id, L"Product ID", 0, temp4, 1000, file) && temp4[0])
id2 = temp4;
int api = GetPrivateProfileIntW(id, L"API", 0, file);
int type = GetPrivateProfileIntW(id, L"Type", 0, file);
if (!api || !type) continue;
Device *dev = new Device((DeviceAPI)api, (DeviceType)type, temp2, temp3, id2);
dev->attached = 0;
dm->AddDevice(dev);
int j = 0;
int last = 0;
while (1) {
wsprintfW(temp, L"Binding %i", j++);
if (!GetPrivateProfileStringW(id, temp, 0, temp2, 1000, file)) {
if (j >= 100) {
if (!last) break;
last = 0;
}
continue;
}
last = 1;
unsigned int uid;
int port, command, sensitivity, turbo, slot = 0, deadZone = 0;
int w = 0;
char string[1000];
while (temp2[w]) {
string[w] = (char)temp2[w];
w++;
}
string[w] = 0;
int len = sscanf(string, " %i , %i , %i , %i , %i , %i , %i", &uid, &port, &command, &sensitivity, &turbo, &slot, &deadZone);
if (len >= 5 && type) {
VirtualControl *c = dev->GetVirtualControl(uid);
if (!c) c = dev->AddVirtualControl(uid, -1);
if (c) {
BindCommand(dev, uid, port, slot, command, sensitivity, turbo, deadZone);
}
}
}
j = 0;
while (1) {
wsprintfW(temp, L"FF Binding %i", j++);
if (!GetPrivateProfileStringW(id, temp, 0, temp2, 1000, file)) {
if (j >= 10) {
if (!last) break;
last = 0;
}
continue;
}
last = 1;
int port, slot, motor;
int w = 0;
char string[1000];
char effect[1000];
while (temp2[w]) {
string[w] = (char)temp2[w];
w++;
}
string[w] = 0;
// wcstok not in ntdll. More effore than its worth to shave off
// whitespace without it.
if (sscanf(string, " %s %i , %i , %i", effect, &port, &motor, &slot) == 4) {
char *s = strchr(strchr(strchr(string, ',')+1, ',')+1, ',');
if (!s) continue;
s++;
w = 0;
while (effect[w]) {
temp2[w] = effect[w];
w++;
}
temp2[w] = 0;
ForceFeedbackEffectType *eff = dev->GetForcefeedbackEffect(temp2);
if (!eff) {
// At the moment, don't record effect types.
// Only used internally, anyways, so not an issue.
dev->AddFFEffectType(temp2, temp2, EFFECT_CONSTANT);
// eff = &dev->ffEffectTypes[dev->numFFEffectTypes-1];
}
ForceFeedbackBinding *b;
CreateEffectBinding(dev, temp2, port, slot, motor, &b);
if (b) {
while (1) {
int axisID = atoi(s);
if (!(s = strchr(s, ','))) break;
s++;
int force = atoi(s);
int i;
for (i=0; i<dev->numFFAxes; i++) {
if (axisID == dev->ffAxes[i].id) break;
}
if (i == dev->numFFAxes) {
dev->AddFFAxis(L"?", axisID);
}