-
Notifications
You must be signed in to change notification settings - Fork 16
/
hotkey.cpp
3914 lines (3322 loc) · 115 KB
/
hotkey.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
#ifdef __MINGW32__
#define _WIN32_IE 0x0501
#define _WIN32_WINNT 0x0501
#endif
#define STRICT
#include <winsock2.h>
#include <windows.h>
#include <tchar.h>
#include <io.h>
#include "types.h"
#include "main.h"
#include "pcejin.h"
#include "mednafen.h"
#include "dd.h"
#include "d3d.h"
#include <string>
#if (((defined(_MSC_VER) && _MSC_VER >= 1300)) || defined(__MINGW32__))
// both MINGW and VS.NET use fstream instead of fstream.h which is deprecated
#include <fstream>
using namespace std;
#else
// for VC++ 6
#include <fstream.h>
#endif
#include "hotkey.h"
#include "main.h"
#include "resource.h"
#define DIRECTINPUT_VERSION 0x0800
#include "directx/dinput.h"
static void ReadHotkey(const char* name, WORD& output);
typedef struct
{
COLORREF crForeGnd; // Foreground text colour
COLORREF crBackGnd; // Background text colour
HFONT hFont; // The font
HWND hwnd; // The control's window handle
} InputCust;
COLORREF CheckButtonKey( WORD Key);
COLORREF CheckHotKey( WORD Key, int modifiers);
InputCust * GetInputCustom(HWND hwnd);
#define CUSTKEY_ALT_MASK 0x01
#define CUSTKEY_CTRL_MASK 0x02
#define CUSTKEY_SHIFT_MASK 0x04
struct SJoypad {
BOOL Enabled;
WORD Left_Up;
WORD Left_Down;
WORD Left_Right;
WORD Left_Left;
WORD Right_Up;
WORD Right_Down;
WORD Right_Left;
WORD Right_Right;
WORD Start;
WORD Select;
WORD B;
WORD A;
WORD ___I;
WORD ___II;
WORD ___X;
WORD ___Y;
WORD L;
WORD R;
};
//Right_Down, Right_Left, Select, Start, Left_Up, Left_Down, Left_Left, Left_Rigth, Right_Right, Right_Up, L, R, B, A
//these get shifted over by two in the vb core
#define A_MASK 0x0001
#define B_MASK 0x0002
#define R_MASK 0x0004
#define L_MASK 0x0008
//these should be correct but let's try...
#if 0
#define RIGHT_UP_MASK 0x0010
#define RIGHT_RIGHT_MASK 0x0020
#define LEFT_RIGHT_MASK 0x0040
#define LEFT_LEFT_MASK 0x0080
#define LEFT_DOWN_MASK 0x0100
#define LEFT_UP_MASK 0x0200
#define START_MASK 0x0400
#define SELECT_MASK 0x0800
#define RIGHT_LEFT_MASK 0x1000
#define RIGHT_DOWN_MASK 0x2000
#endif
//these. why did i have to swap right down and right right? something is probably messed in the hotkeys somewhere
#define RIGHT_UP_MASK 0x0010
#define RIGHT_RIGHT_MASK 0x2000
#define LEFT_RIGHT_MASK 0x0040
#define LEFT_LEFT_MASK 0x0080
#define LEFT_DOWN_MASK 0x0100
#define LEFT_UP_MASK 0x0200
#define START_MASK 0x0400
#define SELECT_MASK 0x0800
#define RIGHT_LEFT_MASK 0x1000
#define RIGHT_DOWN_MASK 0x0020
/*
#define SELECT_MASK 0x0004
#define START_MASK 0x0008
#define UP_MASK 0x0010
#define RIGHT_MASK 0x0020
#define DOWN_MASK 0x0040
#define LEFT_MASK 0x0080
#define LID_MASK 0x0040
#define DEBUG_MASK 0x0080
#define X_MASK 0x0400
#define Y_MASK 0x0800
*/
struct SJoyState{
bool Attached;
//JOYCAPS Caps;
int Threshold;
bool Left;
bool Right;
bool Up;
bool Down;
bool RightLeft;
bool RightRight;
bool RightUp;
bool RightDown;
bool PovLeft;
bool PovRight;
bool PovUp;
bool PovDown;
bool PovDnLeft;
bool PovDnRight;
bool PovUpLeft;
bool PovUpRight;
bool RUp;
bool RDown;
bool UUp;
bool UDown;
bool VUp;
bool VDown;
bool ZUp;
bool ZDown;
bool Button[32];
};
extern SJoypad Joypad[16];
extern SJoypad ToggleJoypadStorage[8];
//extern SCustomKeys CustomKeys;
extern SJoypad TurboToggleJoypadStorage[8];
void RunInputConfig();
void RunHotkeyConfig();
void input_process();
int GetNumHotKeysAssignedTo (WORD Key, int modifiers);
struct SGuitar {
BOOL Enabled;
WORD GREEN;
WORD RED;
WORD YELLOW;
WORD BLUE;
};
extern SGuitar Guitar;
void TranslateKey(WORD keyz,char *out);
LRESULT InputCustom_OnPaint(InputCust *ccp, WPARAM wParam, LPARAM lParam);
// Gamepad Dialog Strings
#define INPUTCONFIG_TITLE "Input Configuration"
#define INPUTCONFIG_JPTOGGLE "Enabled"
//#define INPUTCONFIG_DIAGTOGGLE "Toggle Diagonals"
//#define INPUTCONFIG_OK "&OK"
//#define INPUTCONFIG_CANCEL "&Cancel"
#define INPUTCONFIG_JPCOMBO "Joypad #%d"
#define INPUTCONFIG_LABEL_UP "Up"
#define INPUTCONFIG_LABEL_DOWN "Down"
#define INPUTCONFIG_LABEL_LEFT "Left"
#define INPUTCONFIG_LABEL_RIGHT "Right"
#define INPUTCONFIG_LABEL_I "I"
#define INPUTCONFIG_LABEL_II "II"
#define INPUTCONFIG_LABEL_X "X"
#define INPUTCONFIG_LABEL_Y "Y"
#define INPUTCONFIG_LABEL_L "L"
#define INPUTCONFIG_LABEL_R "R"
#define INPUTCONFIG_LABEL_RUN "Run"
#define INPUTCONFIG_LABEL_SELECT "Select"
#define INPUTCONFIG_LABEL_UPLEFT "R-Up"
#define INPUTCONFIG_LABEL_UPRIGHT "R-Left"
#define INPUTCONFIG_LABEL_DOWNRIGHT "R-Down"
#define INPUTCONFIG_LABEL_DOWNLEFT "R-Right"
#define INPUTCONFIG_LABEL_BLUE "Blue means the button is already mapped.\nPink means it conflicts with a custom hotkey.\nRed means it's reserved by Windows.\nButtons can be disabled using Escape.\nGrayed buttons arent supported yet (sorry!)"
#define INPUTCONFIG_LABEL_UNUSED ""
#define INPUTCONFIG_LABEL_CLEAR_TOGGLES_AND_TURBO "Clear All"
#define INPUTCONFIG_LABEL_MAKE_TURBO "TempTurbo"
#define INPUTCONFIG_LABEL_MAKE_HELD "Autohold"
#define INPUTCONFIG_LABEL_MAKE_TURBO_HELD "Autofire"
#define INPUTCONFIG_LABEL_CONTROLLER_TURBO_PANEL_MOD " Turbo"
// Hotkeys Dialog Strings
#define HOTKEYS_TITLE "Hotkey Configuration"
#define HOTKEYS_CONTROL_MOD "Ctrl + "
#define HOTKEYS_SHIFT_MOD "Shift + "
#define HOTKEYS_ALT_MOD "Alt + "
#define HOTKEYS_LABEL_BLUE "Blue means the hotkey is already mapped.\nPink means it conflicts with a game button.\nRed means it's reserved by Windows.\nA hotkey can be disabled using Escape."
#define HOTKEYS_HKCOMBO "Page %d"
// gaming buttons and axes
#define GAMEDEVICE_JOYNUMPREFIX "(J%x)" // don't change this
#define GAMEDEVICE_JOYBUTPREFIX "#[%d]" // don't change this
#define GAMEDEVICE_XNEG "Left"
#define GAMEDEVICE_XPOS "Right"
#define GAMEDEVICE_YPOS "Up"
#define GAMEDEVICE_YNEG "Down"
#define GAMEDEVICE_R_XNEG "RS-Left"
#define GAMEDEVICE_R_XPOS "RS-Right"
#define GAMEDEVICE_R_YPOS "RS-Up"
#define GAMEDEVICE_R_YNEG "RS-Down"
#define GAMEDEVICE_POVLEFT "POV Left"
#define GAMEDEVICE_POVRIGHT "POV Right"
#define GAMEDEVICE_POVUP "POV Up"
#define GAMEDEVICE_POVDOWN "POV Down"
#define GAMEDEVICE_POVDNLEFT "POV Dn Left"
#define GAMEDEVICE_POVDNRIGHT "POV Dn Right"
#define GAMEDEVICE_POVUPLEFT "POV Up Left"
#define GAMEDEVICE_POVUPRIGHT "POV Up Right"
#define GAMEDEVICE_ZPOS "Z Up"
#define GAMEDEVICE_ZNEG "Z Down"
#define GAMEDEVICE_RPOS "R Up"
#define GAMEDEVICE_RNEG "R Down"
#define GAMEDEVICE_UPOS "U Up"
#define GAMEDEVICE_UNEG "U Down"
#define GAMEDEVICE_VPOS "V Up"
#define GAMEDEVICE_VNEG "V Down"
#define GAMEDEVICE_BUTTON "Button %d"
// gaming general
#define GAMEDEVICE_DISABLED "Disabled"
// gaming keys
#define GAMEDEVICE_KEY "#%d"
#define GAMEDEVICE_NUMPADPREFIX "Numpad-%c"
#define GAMEDEVICE_VK_TAB "Tab"
#define GAMEDEVICE_VK_BACK "Backspace"
#define GAMEDEVICE_VK_CLEAR "Delete"
#define GAMEDEVICE_VK_RETURN "Enter"
#define GAMEDEVICE_VK_LSHIFT "LShift"
#define GAMEDEVICE_VK_RSHIFT "RShift"
#define GAMEDEVICE_VK_LCONTROL "LCtrl"
#define GAMEDEVICE_VK_RCONTROL "RCtrl"
#define GAMEDEVICE_VK_LMENU "LAlt"
#define GAMEDEVICE_VK_RMENU "RAlt"
#define GAMEDEVICE_VK_PAUSE "Pause"
#define GAMEDEVICE_VK_CAPITAL "Capslock"
#define GAMEDEVICE_VK_ESCAPE "Disabled"
#define GAMEDEVICE_VK_SPACE "Space"
#define GAMEDEVICE_VK_PRIOR "PgUp"
#define GAMEDEVICE_VK_NEXT "PgDn"
#define GAMEDEVICE_VK_HOME "Home"
#define GAMEDEVICE_VK_END "End"
#define GAMEDEVICE_VK_LEFT "Left"
#define GAMEDEVICE_VK_RIGHT "Right"
#define GAMEDEVICE_VK_UP "Up"
#define GAMEDEVICE_VK_DOWN "Down"
#define GAMEDEVICE_VK_SELECT "Select"
#define GAMEDEVICE_VK_PRINT "Print"
#define GAMEDEVICE_VK_EXECUTE "Execute"
#define GAMEDEVICE_VK_SNAPSHOT "SnapShot"
#define GAMEDEVICE_VK_INSERT "Insert"
#define GAMEDEVICE_VK_DELETE "Delete"
#define GAMEDEVICE_VK_HELP "Help"
#define GAMEDEVICE_VK_LWIN "LWinKey"
#define GAMEDEVICE_VK_RWIN "RWinKey"
#define GAMEDEVICE_VK_APPS "AppKey"
#define GAMEDEVICE_VK_MULTIPLY "Numpad *"
#define GAMEDEVICE_VK_ADD "Numpad +"
#define GAMEDEVICE_VK_SEPARATOR "Separator"
#define GAMEDEVICE_VK_OEM_1 "Semi-Colon"
#define GAMEDEVICE_VK_OEM_7 "Apostrophe"
#define GAMEDEVICE_VK_OEM_COMMA "Comma"
#define GAMEDEVICE_VK_OEM_PERIOD "Period"
#define GAMEDEVICE_VK_SUBTRACT "Numpad -"
#define GAMEDEVICE_VK_DECIMAL "Numpad ."
#define GAMEDEVICE_VK_DIVIDE "Numpad /"
#define GAMEDEVICE_VK_NUMLOCK "Num-lock"
#define GAMEDEVICE_VK_SCROLL "Scroll-lock"
#define GAMEDEVICE_VK_OEM_MINUS "-"
#define GAMEDEVICE_VK_OEM_PLUS "="
#define GAMEDEVICE_VK_SHIFT "Shift"
#define GAMEDEVICE_VK_CONTROL "Control"
#define GAMEDEVICE_VK_MENU "Alt"
#define GAMEDEVICE_VK_OEM_4 "["
#define GAMEDEVICE_VK_OEM_6 "]"
#define GAMEDEVICE_VK_OEM_5 "\\"
#define GAMEDEVICE_VK_OEM_2 "/"
#define GAMEDEVICE_VK_OEM_3 "`"
#define GAMEDEVICE_VK_F1 "F1"
#define GAMEDEVICE_VK_F2 "F2"
#define GAMEDEVICE_VK_F3 "F3"
#define GAMEDEVICE_VK_F4 "F4"
#define GAMEDEVICE_VK_F5 "F5"
#define GAMEDEVICE_VK_F6 "F6"
#define GAMEDEVICE_VK_F7 "F7"
#define GAMEDEVICE_VK_F8 "F8"
#define GAMEDEVICE_VK_F9 "F9"
#define GAMEDEVICE_VK_F10 "F10"
#define GAMEDEVICE_VK_F11 "F11"
#define GAMEDEVICE_VK_F12 "F12"
#define BUTTON_OK "&OK"
#define BUTTON_CANCEL "&Cancel"
static TCHAR szClassName[] = _T("InputCustom");
static TCHAR szHotkeysClassName[] = _T("InputCustomHot");
static TCHAR szGuitarClassName[] = _T("InputCustomGuitar");
static LRESULT CALLBACK InputCustomWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK HotInputCustomWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK GuitarInputCustomWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
SJoyState Joystick [16];
SJoyState JoystickF [16];
SJoypad ToggleJoypadStorage[8];
SJoypad TurboToggleJoypadStorage[8];
u32 joypads [8];
//the main input configuration:
SJoypad DefaultJoypad[16] = {
{
true, /* Joypad 1 enabled */
VK_UP, VK_DOWN, VK_RIGHT, VK_LEFT , /* Left, Right, Up, Down */
0, 0, 0, 0, /* Left_Up, Left_Down, Right_Up, Right_Down */
VK_RETURN, VK_RSHIFT, /* Start, Select */
0, 0, /* Lid, Debug */
'F', 'D', /* A B */
'S', 'A', /* X Y */
'Q', 'W' /* L R */
},
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
SJoypad Joypad[16];
SGuitar DefaultGuitar = { false, 'E', 'R', 'T', 'Y' };
SGuitar Guitar;
u8 guitarState = 0;
//extern volatile BOOL paused;
#define MAXKEYPAD 15
#define WM_CUSTKEYDOWN (WM_USER+50)
#define WM_CUSTKEYUP (WM_USER+51)
#define NUM_HOTKEY_CONTROLS 20
#define COUNT(a) (sizeof (a) / sizeof (a[0]))
const int IDC_LABEL_HK_Table[NUM_HOTKEY_CONTROLS] = {
IDC_LABEL_HK1 , IDC_LABEL_HK2 , IDC_LABEL_HK3 , IDC_LABEL_HK4 , IDC_LABEL_HK5 ,
IDC_LABEL_HK6 , IDC_LABEL_HK7 , IDC_LABEL_HK8 , IDC_LABEL_HK9 , IDC_LABEL_HK10,
IDC_LABEL_HK11, IDC_LABEL_HK12, IDC_LABEL_HK13, IDC_LABEL_HK14, IDC_LABEL_HK15,
IDC_LABEL_HK16, IDC_LABEL_HK17, IDC_LABEL_HK18, IDC_LABEL_HK19, IDC_LABEL_HK20,
};
const int IDC_HOTKEY_Table[NUM_HOTKEY_CONTROLS] = {
IDC_HOTKEY1 , IDC_HOTKEY2 , IDC_HOTKEY3 , IDC_HOTKEY4 , IDC_HOTKEY5 ,
IDC_HOTKEY6 , IDC_HOTKEY7 , IDC_HOTKEY8 , IDC_HOTKEY9 , IDC_HOTKEY10,
IDC_HOTKEY11, IDC_HOTKEY12, IDC_HOTKEY13, IDC_HOTKEY14, IDC_HOTKEY15,
IDC_HOTKEY16, IDC_HOTKEY17, IDC_HOTKEY18, IDC_HOTKEY19, IDC_HOTKEY20,
};
typedef char TcDIBuf[512];
TcDIBuf cDIBuf;
LPDIRECTINPUT8 pDI;
LPDIRECTINPUTDEVICE8 pJoystick;
DIDEVCAPS DIJoycap;
LPDIRECTINPUTEFFECT pEffect;
char JoystickName[255];
BOOL Feedback;
static LPDIRECTINPUT8 tmp_pDI = NULL;
static BOOL tmp_Feedback = FALSE;
static char tmp_device_name[255] = { 0 };
static LPDIRECTINPUTDEVICE8 tmp_Device = NULL;
static LPDIRECTINPUTDEVICE8 tmp_Joystick = NULL;
BOOL CALLBACK EnumCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
{
if ( FAILED( tmp_pDI->CreateDevice(lpddi->guidInstance, &tmp_Device, NULL) ) )
{
tmp_Device = NULL;
return DIENUM_CONTINUE;
}
strcpy(tmp_device_name, lpddi->tszProductName);
if (lpddi->guidFFDriver.Data1) tmp_Feedback = TRUE;
return DIENUM_STOP;
}
LPDIRECTINPUTDEVICE8 EnumDevices(LPDIRECTINPUT8 pDI)
{
tmp_pDI = pDI;
tmp_Feedback = FALSE;
memset(tmp_device_name, 0, 255);
if( FAILED( pDI->EnumDevices(DI8DEVCLASS_GAMECTRL,
EnumCallback,
NULL,
DIEDFL_ATTACHEDONLY) ) )
return NULL;
Feedback = tmp_Feedback;
strcpy(JoystickName, tmp_device_name);
return tmp_Device;
}
BOOL CALLBACK EnumObjects(const DIDEVICEOBJECTINSTANCE* pdidoi,VOID* pContext)
{
if( pdidoi->dwType & DIDFT_AXIS )
{
DIPROPRANGE diprg;
diprg.diph.dwSize = sizeof(DIPROPRANGE);
diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
diprg.diph.dwHow = DIPH_BYID;
diprg.diph.dwObj = pdidoi->dwType;
diprg.lMin = -10000;
diprg.lMax = 10000;
if( FAILED(tmp_Joystick->SetProperty(DIPROP_RANGE, &diprg.diph)) )
return DIENUM_STOP;
}
return DIENUM_CONTINUE;
}
static void ReadControl(const char* name, const char* controller, WORD& output)
{
UINT temp;
temp = GetPrivateProfileInt(controller,name,-1,IniName);
if(temp != -1) {
output = temp;
}
}
void LoadInputConfig()
{
memcpy(&Joypad,&DefaultJoypad,sizeof(Joypad));
//read from configuration file
Joypad[0].Enabled = true;
Joypad[1].Enabled = true;
Joypad[2].Enabled = true;
Joypad[3].Enabled = true;
Joypad[4].Enabled = true;
char temp[50];
for(int i = 0; i < 10; i++) {
snprintf(temp, 40, "Controller%d", i);
ReadControl("Left_Left",temp,Joypad[i].Left_Left);
ReadControl("Left_Right",temp,Joypad[i].Left_Right);
ReadControl("Left_Up",temp,Joypad[i].Left_Up);
ReadControl("Left_Down",temp,Joypad[i].Left_Down);
ReadControl("Right_Left",temp,Joypad[i].Right_Left);
ReadControl("Right_Right",temp,Joypad[i].Right_Right);
ReadControl("Right_Up",temp,Joypad[i].Right_Up);
ReadControl("Right_Down",temp,Joypad[i].Right_Down);
ReadControl("Start",temp,Joypad[i].Start);
ReadControl("Select",temp,Joypad[i].Select);
// ReadControl("Lid",temp,Joypad[i].Lid);
// ReadControl("Debug",temp,Joypad[i].Debug);
ReadControl("B",temp,Joypad[i].B);
ReadControl("A",temp,Joypad[i].A);
// ReadControl("X",temp,Joypad[i].X);
// ReadControl("Y",temp,Joypad[i].Y);
ReadControl("L",temp,Joypad[i].L);
ReadControl("R",temp,Joypad[i].R);
}
}
static void WriteControl(char* name, char* controller, WORD val)
{
WritePrivateProfileInt(controller,name,val,IniName);
}
static void SaveInputConfig()
{
char temp[50];
for(int i = 0; i < 10; i++) {
snprintf(temp, 40, "Controller%d", i);
WriteControl("Left_Left",temp,Joypad[i].Left_Left);
WriteControl("Left_Right",temp,Joypad[i].Left_Right);
WriteControl("Left_Up",temp,Joypad[i].Left_Up);
WriteControl("Left_Down",temp,Joypad[i].Left_Down);
WriteControl("Right_Left",temp,Joypad[i].Right_Left);
WriteControl("Right_Right",temp,Joypad[i].Right_Right);
WriteControl("Right_Up",temp,Joypad[i].Right_Up);
WriteControl("Right_Down",temp,Joypad[i].Right_Down);
WriteControl("Start",temp,Joypad[i].Start);
WriteControl("Select",temp,Joypad[i].Select);
// ReadControl("Lid",temp,Joypad[i].Lid);
// ReadControl("Debug",temp,Joypad[i].Debug);
WriteControl("B",temp,Joypad[i].B);
WriteControl("A",temp,Joypad[i].A);
// ReadControl("X",temp,Joypad[i].X);
// ReadControl("Y",temp,Joypad[i].Y);
WriteControl("L",temp,Joypad[i].L);
WriteControl("R",temp,Joypad[i].R);
/*
WriteControl("Left",temp,Joypad[i].Left);
WriteControl("Right",temp,Joypad[i].Right);
WriteControl("Up",temp,Joypad[i].Up);
WriteControl("Down",temp,Joypad[i].Down);
WriteControl("Left_Up",temp,Joypad[i].Left_Up);
WriteControl("Left_Down",temp,Joypad[i].Left_Down);
WriteControl("Right_Up",temp,Joypad[i].Right_Up);
WriteControl("Right_Down",temp,Joypad[i].Right_Down);
WriteControl("Run",temp,Joypad[i].Run);
WriteControl("Select",temp,Joypad[i].Select);
WriteControl("Lid",temp,Joypad[i].Lid);
WriteControl("Debug",temp,Joypad[i].Debug);
WriteControl("I",temp,Joypad[i].I);
WriteControl("II",temp,Joypad[i].II);
WriteControl("X",temp,Joypad[i].X);
WriteControl("Y",temp,Joypad[i].Y);
WriteControl("L",temp,Joypad[i].L);
WriteControl("R",temp,Joypad[i].R);
*/
}
}
BOOL di_init()
{
HWND hParentWnd = g_hWnd;
pDI = NULL;
pJoystick = NULL;
Feedback = FALSE;
memset(cDIBuf, 0, sizeof(cDIBuf));
memset(JoystickName, 0, sizeof(JoystickName));
if(FAILED(DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&pDI,NULL)))
return FALSE;
pJoystick = EnumDevices(pDI);
if (pJoystick)
{
if(!FAILED(pJoystick->SetDataFormat(&c_dfDIJoystick2)))
{
if(FAILED(pJoystick->SetCooperativeLevel(hParentWnd,DISCL_BACKGROUND|DISCL_EXCLUSIVE)))
{
pJoystick->Release();
pJoystick = NULL;
}
else
{
tmp_Joystick = pJoystick;
pJoystick->EnumObjects(::EnumObjects, (VOID*)hParentWnd, DIDFT_ALL);
memset(&DIJoycap,0,sizeof(DIDEVCAPS));
DIJoycap.dwSize=sizeof(DIDEVCAPS);
pJoystick->GetCapabilities(&DIJoycap);
}
}
else
{
pJoystick->Release();
pJoystick = NULL;
}
}
if (pJoystick)
{
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = 0;
if ( !FAILED( pJoystick->SetProperty(DIPROP_AUTOCENTER, &dipdw.diph) ) )
{
DWORD rgdwAxes[1] = { DIJOFS_Y };
LONG rglDirection[2] = { 0 };
DICONSTANTFORCE cf = { 0 };
DIEFFECT eff;
cf.lMagnitude = (DI_FFNOMINALMAX * 100);
memset(&eff, 0, sizeof(eff));
eff.dwSize = sizeof(DIEFFECT);
eff.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS;
eff.dwDuration = INFINITE;
eff.dwSamplePeriod = 0;
eff.dwGain = DI_FFNOMINALMAX;
eff.dwTriggerButton = DIEB_NOTRIGGER;
eff.dwTriggerRepeatInterval = 0;
eff.cAxes = 1;
eff.rgdwAxes = rgdwAxes;
eff.rglDirection = rglDirection;
eff.lpEnvelope = 0;
eff.cbTypeSpecificParams = sizeof( DICONSTANTFORCE );
eff.lpvTypeSpecificParams = &cf;
eff.dwStartDelay = 0;
if( FAILED( pJoystick->CreateEffect(GUID_ConstantForce, &eff, &pEffect, NULL) ) )
Feedback = FALSE;
}
else
Feedback = FALSE;
}
printf("DirectX Input: \n");
// INFO("DirectX Input: \n");
if (pJoystick != NULL)
{
printf(" - gamecontrol successfully inited: %s\n", JoystickName);
// INFO(" - gamecontrol successfully inited: %s\n", JoystickName);
if (Feedback) printf("\t\t\t\t (with FeedBack support)\n");
}
// paused = FALSE;
return TRUE;
}
BOOL JoystickEnabled()
{
return (pJoystick==NULL?FALSE:TRUE);
}
HWND funky;
//WPARAM tid;
//
void JoystickChanged( short ID, short Movement)
{
// don't allow two changes to happen too close together in time
{
static bool first = true;
static DWORD lastTime = 0;
if(first || timeGetTime() - lastTime > 300) // 0.3 seconds
{
first = false;
lastTime = timeGetTime();
}
else
{
return; // too soon after last change
}
}
WORD JoyKey;
JoyKey = 0x8000;
JoyKey |= (WORD)(ID << 8);
JoyKey |= Movement;
SendMessage(funky,WM_USER+45,JoyKey,0);
// KillTimer(funky,tid);
}
int FunkyNormalize(int cur, int min, int max)
{
int Result = 0;
if ((max - min) == 0)
return (Result);
Result = cur - min;
Result = (Result * 200) / (max - min);
Result -= 100;
return Result;
}
#define S9X_JOY_NEUTRAL 60
void CheckAxis (short joy, short control, int val,
int min, int max,
bool &first, bool &second)
{
if (FunkyNormalize (val, min, max) < -S9X_JOY_NEUTRAL)
{
second = false;
if (!first)
{
JoystickChanged (joy, control);
first = true;
}
}
else
first = false;
if (FunkyNormalize (val, min, max) > S9X_JOY_NEUTRAL)
{
first = false;
if (!second)
{
JoystickChanged (joy, (short) (control + 1));
second = true;
}
}
else
second = false;
}
void CheckAxis_game (int val, int min, int max, bool &first, bool &second)
{
if (FunkyNormalize (val, min, max) < -S9X_JOY_NEUTRAL)
{
second = false;
first = true;
}
else
first = false;
if (FunkyNormalize (val, min, max) > S9X_JOY_NEUTRAL)
{
first = false;
second = true;
}
else
second = false;
}
void S9xUpdateJoyState()
{
memset(&Joystick[0],0,sizeof(Joystick[0]));
int C = 0;
if (pJoystick)
{
DIJOYSTATE2 JoyStatus;
HRESULT hr=pJoystick->Poll();
if (FAILED(hr))
pJoystick->Acquire();
else
{
hr=pJoystick->GetDeviceState(sizeof(JoyStatus),&JoyStatus);
if (FAILED(hr)) hr=pJoystick->Acquire();
else
{
CheckAxis_game(JoyStatus.lX,-10000,10000,Joystick[0].Left,Joystick[0].Right);
CheckAxis_game(JoyStatus.lY,-10000,10000,Joystick[0].Up,Joystick[0].Down);
switch (JoyStatus.rgdwPOV[0])
{
case JOY_POVBACKWARD:
Joystick[C].PovDown = true;
break;
case 4500:
//Joystick[C].PovUpRight = true;
Joystick[C].PovUp = true;
Joystick[C].PovRight = true;
break;
case 13500:
//Joystick[C].PovDnRight = true;
Joystick[C].PovDown = true;
Joystick[C].PovRight = true;
break;
case 22500:
//Joystick[C].PovDnLeft = true;
Joystick[C].PovDown = true;
Joystick[C].PovLeft = true;
break;
case 31500:
//Joystick[C].PovUpLeft = true;
Joystick[C].PovUp = true;
Joystick[C].PovLeft = true;
break;
case JOY_POVFORWARD:
Joystick[C].PovUp = true;
break;
case JOY_POVLEFT:
Joystick[C].PovLeft = true;
break;
case JOY_POVRIGHT:
Joystick[C].PovRight = true;
break;
default:
break;
}
for(int B=0;B<128;B++)
if( JoyStatus.rgbButtons[B] )
Joystick[C].Button[B] = true;
}
}
}
}
void di_poll_scan()
{
int C = 0;
if (pJoystick)
{
DIJOYSTATE2 JoyStatus;
HRESULT hr=pJoystick->Poll();
if (FAILED(hr))
pJoystick->Acquire();
else
{
hr=pJoystick->GetDeviceState(sizeof(JoyStatus),&JoyStatus);
if (FAILED(hr)) hr=pJoystick->Acquire();
else
{
CheckAxis(0,0,JoyStatus.lX,-10000,10000,JoystickF[C].Left,JoystickF[C].Right);
CheckAxis(0,2,JoyStatus.lY,-10000,10000,JoystickF[C].Down,JoystickF[C].Up);
CheckAxis(0,8,JoyStatus.lRx,-10000,10000,JoystickF[C].RightLeft,JoystickF[C].RightRight);
CheckAxis(0,10,JoyStatus.lRy,-10000,10000,JoystickF[C].RightDown,JoystickF[C].RightUp);
switch (JoyStatus.rgdwPOV[0])
{
case JOY_POVBACKWARD:
if( !JoystickF[C].PovDown)
{ JoystickChanged( C, 7); }
JoystickF[C].PovDown = true;
JoystickF[C].PovUp = false;
JoystickF[C].PovLeft = false;
JoystickF[C].PovRight = false;
JoystickF[C].PovDnLeft = false;
JoystickF[C].PovDnRight = false;
JoystickF[C].PovUpLeft = false;
JoystickF[C].PovUpRight = false;
break;
case 4500:
if( !JoystickF[C].PovUpRight)
{ JoystickChanged( C, 52); }
JoystickF[C].PovDown = false;
JoystickF[C].PovUp = false;
JoystickF[C].PovLeft = false;
JoystickF[C].PovRight = false;
JoystickF[C].PovDnLeft = false;
JoystickF[C].PovDnRight = false;
JoystickF[C].PovUpLeft = false;
JoystickF[C].PovUpRight = true;
break;
case 13500:
if( !JoystickF[C].PovDnRight)
{ JoystickChanged( C, 50); }
JoystickF[C].PovDown = false;
JoystickF[C].PovUp = false;
JoystickF[C].PovLeft = false;
JoystickF[C].PovRight = false;
JoystickF[C].PovDnLeft = false;
JoystickF[C].PovDnRight = true;
JoystickF[C].PovUpLeft = false;
JoystickF[C].PovUpRight = false;
break;
case 22500:
if( !JoystickF[C].PovDnLeft)
{ JoystickChanged( C, 49); }
JoystickF[C].PovDown = false;
JoystickF[C].PovUp = false;
JoystickF[C].PovLeft = false;
JoystickF[C].PovRight = false;
JoystickF[C].PovDnLeft = true;
JoystickF[C].PovDnRight = false;
JoystickF[C].PovUpLeft = false;
JoystickF[C].PovUpRight = false;
break;
case 31500:
if( !JoystickF[C].PovUpLeft)
{ JoystickChanged( C, 51); }
JoystickF[C].PovDown = false;
JoystickF[C].PovUp = false;
JoystickF[C].PovLeft = false;
JoystickF[C].PovRight = false;
JoystickF[C].PovDnLeft = false;
JoystickF[C].PovDnRight = false;
JoystickF[C].PovUpLeft = true;
JoystickF[C].PovUpRight = false;
break;
case JOY_POVFORWARD:
if( !JoystickF[C].PovUp)
{ JoystickChanged( C, 6); }
JoystickF[C].PovDown = false;
JoystickF[C].PovUp = true;
JoystickF[C].PovLeft = false;
JoystickF[C].PovRight = false;
JoystickF[C].PovDnLeft = false;
JoystickF[C].PovDnRight = false;
JoystickF[C].PovUpLeft = false;
JoystickF[C].PovUpRight = false;
break;
case JOY_POVLEFT:
if( !JoystickF[C].PovLeft)
{ JoystickChanged( C, 4); }
JoystickF[C].PovDown = false;
JoystickF[C].PovUp = false;
JoystickF[C].PovLeft = true;
JoystickF[C].PovRight = false;
JoystickF[C].PovDnLeft = false;
JoystickF[C].PovDnRight = false;
JoystickF[C].PovUpLeft = false;
JoystickF[C].PovUpRight = false;
break;
case JOY_POVRIGHT:
if( !JoystickF[C].PovRight)
{ JoystickChanged( C, 5); }
JoystickF[C].PovDown = false;
JoystickF[C].PovUp = false;
JoystickF[C].PovLeft = false;
JoystickF[C].PovRight = true;
JoystickF[C].PovDnLeft = false;
JoystickF[C].PovDnRight = false;
JoystickF[C].PovUpLeft = false;
JoystickF[C].PovUpRight = false;
break;
default:
JoystickF[C].PovDown = false;
JoystickF[C].PovUp = false;
JoystickF[C].PovLeft = false;
JoystickF[C].PovRight = false;
JoystickF[C].PovDnLeft = false;
JoystickF[C].PovDnRight = false;
JoystickF[C].PovUpLeft = false;
JoystickF[C].PovUpRight = false;
break;
}
for(int B=0;B<128;B++)
if( JoyStatus.rgbButtons[B] )
{
if( !JoystickF[C].Button[B])
{
JoystickChanged( C, (short)(12+B));
JoystickF[C].Button[B] = true;
}
}
else
{ JoystickF[C].Button[B] = false; }
}
}
}
}
void FunkyJoyStickTimer()
{
di_poll_scan();
}