-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOpenXinput.cpp
More file actions
2822 lines (2314 loc) · 82.3 KB
/
Copy pathOpenXinput.cpp
File metadata and controls
2822 lines (2314 loc) · 82.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) Nemirtingas
* This file is part of the OpenXinput project.
*
* OpenXinput project is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXinput project is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXinput project. If not, see <https://www.gnu.org/licenses/>
*/
#include "OpenXinputInternal.h"
#if(_WIN32_WINNT >= _WIN32_WINNT_WIN10)
// XInputEnable is deprecated since Windows 10, disable the warning if needed to build Xinput.
#pragma warning(disable : 4995)
#endif
/////////////////////////////////////////////////////
// Declarations
/////////////////////////////////////////////////////
struct DeviceInfo_t {
DWORD status;
HANDLE hDevice;
HANDLE hGuideWait;
BYTE dwUserIndex;
LPWSTR lpDevicePath;
DWORD dwDevicePathSize;
LPWSTR field_18;
SIZE_T field_1C;
WORD wType;
WORD field_22;
XINPUT_STATE DeviceState;
XINPUT_VIBRATION DeviceVibration;
WORD field_38;
WORD field_3A;
WORD field_3C;
WORD field_3E;
WORD field_40;
WORD field_42;
WORD field_44;
WORD field_46;
WORD field_48;
WORD field_4A;
WORD field_4C;
WORD vendorId;
WORD productId;
BYTE inputId;
BYTE bTriggers;
WORD wButtons;
WORD LeftStickVirtualKey;
WORD RightStickVirtualKey;
WORD field_5A;
WORD field_5C;
WORD field_5E;
};
// All theses structs are passed to the XUSB driver, they __NEED__ to be unaligned.
#pragma pack(push, 1)
struct InSetLEDBuffer_t
{
BYTE deviceIndex;
BYTE state;
BYTE pad1;
BYTE pad2;
BYTE unk0;
};
struct InVibrationBuffer_t
{
BYTE deviceIndex;
BYTE pad1;
BYTE leftMotorSpeed;
BYTE rightMotorSpeed;
BYTE unk0;
};
struct InGetAudioDeviceInformation_t
{
WORD wType;
BYTE DeviceIndex;
};
struct OutGetAudioDeviceInformation_t
{
WORD unk0;
WORD vendorId;
WORD productId;
BYTE inputId;
};
struct InGetLEDBuffer_t
{
WORD wType;
BYTE DeviceIndex;
};
struct OutGetLEDBuffer_t
{
BYTE unk0;
BYTE unk1;
BYTE LEDState;
};
struct OutDeviceInfos_t
{
WORD wType;
BYTE deviceIndex;
BYTE unk1;
WORD unk2;
WORD unk3;
WORD vendorId;
WORD productId;
};
struct InPowerOffBuffer_t
{
WORD wType;
BYTE DeviceIndex;
};
struct InWaitForGuideButtonBuffer_t
{
WORD wType;
BYTE DeviceIndex;
};
struct OutWaitForGuideButtonBuffer_t
{
BYTE field_0;
BYTE field_1;
BYTE status;
BYTE field_3;
BYTE field_4;
DWORD field_5;
BYTE field_9;
BYTE field_A;
WORD field_B;
BYTE field_D;
BYTE field_E;
WORD field_F;
WORD field_11;
WORD field_13;
WORD field_15;
BYTE field_17;
BYTE field_18;
BYTE field_19;
BYTE field_1A;
BYTE field_1B;
BYTE field_1C;
};
struct InGamepadState0100
{
BYTE DeviceIndex;
};
struct GamepadState0100
{
BYTE status;
BYTE unk0;
BYTE inputId;
DWORD dwPacketNumber;
BYTE unk2;
WORD wButtons;
BYTE bLeftTrigger;
BYTE bRightTrigger;
SHORT sThumbLX;
SHORT sThumbLY;
SHORT sThumbRX;
SHORT sThumbRY;
};
struct InGamepadState0101
{
WORD wType;
BYTE DeviceIndex;
};
struct GamepadState0101
{
BYTE unk0;
BYTE inputId;
BYTE status;
BYTE unk2;
BYTE unk3;
DWORD dwPacketNumber;
BYTE unk4;
BYTE unk5;
WORD wButtons;
BYTE bLeftTrigger;
BYTE bRightTrigger;
SHORT sThumbLX;
SHORT sThumbLY;
SHORT sThumbRX;
SHORT sThumbRY;
BYTE unk6;
BYTE unk7;
BYTE unk8;
BYTE unk9;
BYTE unk10;
BYTE unk11;
};
struct InGamepadCapabilities0101
{
WORD wType;
BYTE DeviceIndex;
};
struct GamepadCapabilities0101
{
BYTE unk0;
BYTE unk1;
BYTE Type;
BYTE SubType;
WORD wButtons;
BYTE bLeftTrigger;
BYTE bRightTrigger;
SHORT sThumbLX;
SHORT sThumbLY;
SHORT sThumbRX;
SHORT sThumbRY;
BYTE unk16;
BYTE unk17;
BYTE unk18;
BYTE unk19;
BYTE unk20;
BYTE unk21;
BYTE bLeftMotorSpeed;
BYTE bRightMotorSpeed;
};
struct InGamepadBatteryInformation0102
{
WORD wType;
BYTE DeviceIndex;
BYTE DeviceType;
};
struct GamepadBatteryInformation0102
{
WORD wType;
BYTE BatteryType;
BYTE BatteryLevel;
};
#pragma pack(pop)
struct GetStateApiParam_t
{
XINPUT_STATE* pState;
};
struct SetStateApiParam_t
{
XINPUT_VIBRATION* pVibration;
};
struct DSoundCallbackParam_t
{
PCWSTR DevicePath;
GUID* HeadphoneGuid;
GUID* MicrophoneGuid;
};
struct GetAudioDeviceGuidsApiParam_t
{
GUID* pHeadphoneGuid;
GUID* pMicrophoneGuid;
};
struct GetBatteryInformationApiParam_t
{
LPBYTE DeviceType;
XINPUT_BATTERY_INFORMATION* pBatteryInformation;
};
struct GetCapabilitiesApiParam_t
{
XINPUT_CAPABILITIES* pCapabilities;
};
struct GetKeystrokeApiParam_t
{
LPDWORD lpReserved;
XINPUT_KEYSTROKE* pKeystroke;
LPDWORD lpLastError;
};
struct WaitGuideButtonApiParam_t
{
LPHANDLE lphEvent;
XINPUT_LISTEN_STATE* pListenState;
};
struct WaitForGuideButtonHelperApiParam_t
{
HANDLE hGuideWait;
HANDLE hGuideEvent;
BYTE dwUserIndex;
XINPUT_LISTEN_STATE* pListenState;
};
constexpr DWORD SET_USER_LED_ON_CREATE = (1 << 0);
constexpr DWORD DISABLE_USER_LED_ON_DESTROY = (1 << 1);
constexpr DWORD DISABLE_VIBRATION_ON_DESTROY = (1 << 2);
/////////////////////////////////////////////////////
// Anonymous namespace
/////////////////////////////////////////////////////
namespace {
static PVOID g_pDetailBuffer;
static DWORD g_dwDetailBufferSize;
static decltype(DeviceIoControl)* g_pfnDeviceIoControl;
static decltype(SetupDiGetClassDevsW)* g_pfnGetClassDevs;
static decltype(SetupDiEnumDeviceInterfaces)* g_pfnEnumDeviceInterfaces;
static decltype(SetupDiGetDeviceInterfaceDetailW)* g_pfnGetDeviceInterfaceDetail;
static decltype(SetupDiDestroyDeviceInfoList)* g_pfnDestroyDeviceInfoList;
static BOOL g_IsInitialized = FALSE;
static BOOL g_IsCommunicationEnabled = FALSE;
static RTL_CRITICAL_SECTION g_csGlobalLock;
static DWORD g_dwSettings = SET_USER_LED_ON_CREATE | DISABLE_USER_LED_ON_DESTROY | DISABLE_VIBRATION_ON_DESTROY;
static DWORD g_dwDeviceListSize = 0;
static DeviceInfo_t** g_pDeviceList;
static DWORD g_dwLogVerbosity = 0;
DWORD WINAPI XInputReturnCodeFromHRESULT(HRESULT hr);
HRESULT WINAPI GrowList(DWORD newSize);
void WINAPI CopyGamepadStateToDeviceInfo(DeviceInfo_t* pDevice, GamepadState0100* pGamepadState);
void WINAPI CopyGamepadStateToDeviceInfo(DeviceInfo_t* pDevice, GamepadState0101* pGamepadState);
void WINAPI TranslateCapabilities(GamepadCapabilities0101* pGamepadCapabilities, XINPUT_CAPABILITIES* pCapabilities);
HRESULT WINAPI SendReceiveIoctl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBufferSize, LPVOID lpOutBuffer, DWORD nOutBufferSize, LPOVERLAPPED lpOverlapped);
HRESULT WINAPI SendIoctl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBufferSize);
HRESULT WINAPI ReceiveIoctl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpOutBuffer, DWORD nOufBufferSize);
HRESULT WINAPI EnumerateXInputDevices();
HRESULT WINAPI EnumerateDevicesOnDeviceInterface(HANDLE hDevice, LPCWSTR lpDevicePath);
HRESULT WINAPI ProcessEnumeratedDevice(DeviceInfo_t* pDevice);
HRESULT WINAPI GetDeviceList(HDEVINFO* phDevInfo);
HRESULT WINAPI GetDeviceInterfaceData(HDEVINFO DeviceInfoSet, DWORD MemberIndex, PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
HRESULT WINAPI GetDeviceInterfaceDetail(HDEVINFO hDeviceInfo, PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData, PSP_DEVICE_INTERFACE_DETAIL_DATA_W* ppDeviceInterfaceDetailData);
DWORD WINAPI WaitForGuideButtonHelper(void* pParam);
BOOL CALLBACK DirectSoundEnumerateCallback(PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_W_DATA lpData, LPVOID lpUser);
}
/////////////////////////////////////////////////////
// Protocol namespace
/////////////////////////////////////////////////////
namespace Protocol {
DEFINE_HIDDEN_GUID(XUSB_INTERFACE_CLASS_GUID, 0xEC87F1E3, 0xC13B, 0x4100, 0xB5, 0xF7, 0x8B, 0x84, 0xD5, 0x42, 0x60, 0xCB);
constexpr DWORD IOCTL_XINPUT_BASE = 0x8000;
// 16bits 12bits 3bits 3bits
static DWORD IOCTL_XINPUT_GET_INFORMATION = CTL_CODE(IOCTL_XINPUT_BASE, 0x800, METHOD_BUFFERED, FILE_READ_ACCESS); // 0x80006000
static DWORD IOCTL_XINPUT_GET_CAPABILITIES = CTL_CODE(IOCTL_XINPUT_BASE, 0x801, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS); // 0x8000E004
static DWORD IOCTL_XINPUT_GET_LED_STATE = CTL_CODE(IOCTL_XINPUT_BASE, 0x802, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS); // 0x8000E008
static DWORD IOCTL_XINPUT_GET_GAMEPAD_STATE = CTL_CODE(IOCTL_XINPUT_BASE, 0x803, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS); // 0x8000E00C
static DWORD IOCTL_XINPUT_SET_GAMEPAD_STATE = CTL_CODE(IOCTL_XINPUT_BASE, 0x804, METHOD_BUFFERED, FILE_WRITE_ACCESS); // 0x8000A010
static DWORD IOCTL_XINPUT_WAIT_FOR_GUIDE_BUTTON = CTL_CODE(IOCTL_XINPUT_BASE, 0x805, METHOD_BUFFERED, FILE_WRITE_ACCESS); // 0x8000A014
static DWORD IOCTL_XINPUT_GET_BATTERY_INFORMATION = CTL_CODE(IOCTL_XINPUT_BASE, 0x806, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS); // 0x8000E018
static DWORD IOCTL_XINPUT_POWER_DOWN_DEVICE = CTL_CODE(IOCTL_XINPUT_BASE, 0x807, METHOD_BUFFERED, FILE_WRITE_ACCESS); // 0x8000A01C
static DWORD IOCTL_XINPUT_GET_AUDIO_INFORMATION = CTL_CODE(IOCTL_XINPUT_BASE, 0x808, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS); // 0x8000E020
namespace LEDState {
static BYTE XINPUT_LED_OFF = 0;
static BYTE XINPUT_LED_BLINK = 1;
static BYTE XINPUT_LED_1_SWITCH_BLINK = 2;
static BYTE XINPUT_LED_2_SWITCH_BLINK = 3;
static BYTE XINPUT_LED_3_SWITCH_BLINK = 4;
static BYTE XINPUT_LED_4_SWITCH_BLINK = 5;
static BYTE XINPUT_LED_1 = 6;
static BYTE XINPUT_LED_2 = 7;
static BYTE XINPUT_LED_3 = 8;
static BYTE XINPUT_LED_4 = 9;
static BYTE XINPUT_LED_CYCLE = 10;
static BYTE XINPUT_LED_FAST_BLINK = 11;
static BYTE XINPUT_LED_SLOW_BLINK = 12;
static BYTE XINPUT_LED_FLIPFLOP = 13;
static BYTE XINPUT_LED_ALLBLINK = 14;
static BYTE XINPUT_PORT_TO_LED_MAP[] =
{
XINPUT_LED_1,
XINPUT_LED_2,
XINPUT_LED_3,
XINPUT_LED_4,
};
#define MAX_XINPUT_PORT_TO_LED_MAP (sizeof(Protocol::LEDState::XINPUT_PORT_TO_LED_MAP)/sizeof(*Protocol::LEDState::XINPUT_PORT_TO_LED_MAP))
static BYTE XINPUT_LED_TO_PORT_MAP[] =
{
0xFF,//XINPUT_LED_OFF
0xFF,//XINPUT_LED_BLINK
0x00,//XINPUT_LED_1_SWITCH_BLINK
0x01,//XINPUT_LED_2_SWITCH_BLINK
0x02,//XINPUT_LED_3_SWITCH_BLINK
0x03,//XINPUT_LED_4_SWITCH_BLINK
0x00,//XINPUT_LED_1
0x01,//XINPUT_LED_2
0x02,//XINPUT_LED_3
0x03,//XINPUT_LED_4
0xFF,//XINPUT_LED_CYCLE
0xFF,//XINPUT_LED_FAST_BLINK
0xFF,//XINPUT_LED_SLOW_BLINK
0xFF,//XINPUT_LED_FLIPFLOP
0xFF,//XINPUT_LED_ALLBLINK
0x00,//UNUSED
};
}//namespace LEDState
}//namespace Protocol
/////////////////////////////////////////////////////
// DeviceEnum namespace
/////////////////////////////////////////////////////
namespace DeviceEnum {
HRESULT WINAPI Initialize();
HRESULT WINAPI Close();
HRESULT WINAPI GetDeviceOnPort(DWORD dwUserIndex, DeviceInfo_t** ppDevice, bool rescan);
}
/////////////////////////////////////////////////////
// DeviceInfo namespace
/////////////////////////////////////////////////////
namespace DeviceInfo {
struct XINPUT_AUDIO_INFORMATION
{
WORD vendorId;
WORD productId;
BYTE inputId;
};
namespace Enabled {
HRESULT WINAPI GetState(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
HRESULT WINAPI SetVibration(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
}
namespace Disabled {
HRESULT WINAPI GetState(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
HRESULT WINAPI SetVibration(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
}
DeviceInfo_t* WINAPI Create(HANDLE hDevice, LPCWSTR lpDevicePath);
void WINAPI Destroy(DeviceInfo_t* pDevice);
void WINAPI Recycle(DeviceInfo_t* pDevice);
BOOL WINAPI IsDeviceInactive(DeviceInfo_t* pDevice);
HRESULT WINAPI GetKeystroke(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
HRESULT WINAPI PowerOffController(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
HRESULT WINAPI CancelGuideButtonWait(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
HRESULT WINAPI GetAudioDeviceGuids(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
HRESULT WINAPI GetBatteryInformation(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
HRESULT WINAPI GetCapabilities(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
HRESULT WINAPI WaitForGuideButton(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
void WINAPI OnEnableSettingChanged(BOOL bEnabled);
static HRESULT(WINAPI *g_pfnGetStateDispatcher)(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
static HRESULT(WINAPI *g_pfnSetVibrationDispatcher)(DeviceInfo_t* pDevice, void* pParams, DWORD reserved);
static HRESULT(WINAPI *g_pfnGetKeystrokeDispatcher)(DeviceInfo_t* pDevice, void* pParams, DWORD reserved) = GetKeystroke;
static HRESULT(WINAPI *g_pfnPowerOffController)(DeviceInfo_t* pDevice, void* pParams, DWORD reserved) = PowerOffController;
static HRESULT(WINAPI *g_pfnCancelGuideButtonWait)(DeviceInfo_t* pDevice, void* pParams, DWORD reserved) = CancelGuideButtonWait;
static HRESULT(WINAPI *g_pfnGetAudioDeviceGuidsDispatcher)(DeviceInfo_t* pDevice, void* pParams, DWORD reserved) = GetAudioDeviceGuids;
static HRESULT(WINAPI *g_pfnGetBatteryInformationDispatcher)(DeviceInfo_t* pDevice, void* pParams, DWORD reserved) = GetBatteryInformation;
static HRESULT(WINAPI *g_pfnGetCapabilitiesDispatcher)(DeviceInfo_t* pDevice, void* pParams, DWORD reserved) = GetCapabilities;
static HRESULT(WINAPI *g_pfnWaitForGuideButton)(DeviceInfo_t* pDevice, void* pParams, DWORD reserved) = WaitForGuideButton;
}
/////////////////////////////////////////////////////
// DeviceList namespace
/////////////////////////////////////////////////////
namespace DeviceList {
constexpr size_t InitialDeviceListSize = XUSER_MAX_COUNT * 2;
HRESULT WINAPI Initialize();
HRESULT WINAPI Close();
HRESULT WINAPI SetDeviceOnPort(DWORD dwUserIndex, DeviceInfo_t* pDevice);
HRESULT WINAPI GetDeviceOnPort(DWORD dwUserIndex, DeviceInfo_t** ppDevice);
HRESULT WINAPI IsDevicePresent(DeviceInfo_t* pDevice);
BOOL WINAPI IsDeviceOnPort(DWORD dwUserIndex);
}
/////////////////////////////////////////////////////
// DriverComm namespace
/////////////////////////////////////////////////////
namespace DriverComm {
HRESULT WINAPI Initialize();
HRESULT WINAPI Close();
BOOL WINAPI CheckForDriverHook(DWORD driverHook, LPVOID hookFunction);
HRESULT WINAPI SendLEDState(DeviceInfo_t* pDevice, BYTE ledState);
HRESULT WINAPI SendDeviceVibration(DeviceInfo_t* pDevice);
HRESULT WINAPI GetDeviceInfoFromInterface(HANDLE hDevice, OutDeviceInfos_t* pDeviceInfos);
HRESULT WINAPI GetLatestDeviceInfo(DeviceInfo_t* pDevice);
HRESULT WINAPI GetCapabilities(DeviceInfo_t* pDevice, XINPUT_CAPABILITIES* pCapabilities);
HRESULT WINAPI GetBatteryInformation(DeviceInfo_t* pDevice, BYTE DeviceType, XINPUT_BATTERY_INFORMATION* pBatteryInformation);
HRESULT WINAPI GetAudioDeviceInformation(DeviceInfo_t* pDevice, DeviceInfo::XINPUT_AUDIO_INFORMATION* pAudioInformation);
HRESULT WINAPI GetLEDState(DeviceInfo_t* pDevice, BYTE* ledState);
HRESULT WINAPI PowerOffController(DeviceInfo_t* pDevice);
HRESULT WINAPI WaitForGuideButton(HANDLE hDevice, DWORD dwUserIndex, XINPUT_LISTEN_STATE* pListenState);
HRESULT WINAPI CancelGuideButtonWait(DeviceInfo_t* pDevice);
namespace SetupDiWrapper {
HDEVINFO WINAPI GetClassDevs(const GUID* ClassGuid, PCWSTR Enumerator, HWND hwndParent, DWORD Flags);
BOOL WINAPI EnumDeviceInterfaces(HDEVINFO DeviceInfoSet, PSP_DEVINFO_DATA DeviceInfoData, const GUID* InterfaceClassGuid, DWORD MemberIndex, PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
BOOL WINAPI GetDeviceInterfaceDetail(HDEVINFO DeviceInfoSet, PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData, PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData, DWORD DeviceInterfaceDetailDataSize, PDWORD RequiredSize, PSP_DEVINFO_DATA DeviceInfoData);
BOOL WINAPI DestroyDeviceInfoList(HDEVINFO DeviceInfoSet);
}
}
/////////////////////////////////////////////////////
// Utilities namespace
/////////////////////////////////////////////////////
namespace Utilities {
HRESULT WINAPI Initialize();
HRESULT WINAPI Close();
LPVOID WINAPI MemAlloc(DWORD size);
void WINAPI MemFree(LPVOID lpMem);
HRESULT WINAPI SafeCopyToUntrustedBuffer(void* pDst, const void* pSrc, DWORD size);
HRESULT WINAPI SafeCopyFromUntrustedBuffer(void* pDst, const void* pSrc, DWORD size);
BOOL WINAPI IsSettingSet(DWORD setting);
BOOL WINAPI CheckForSettings(DWORD setting, LPVOID lpParam);
}
/////////////////////////////////////////////////////
// XInputCore namespace
/////////////////////////////////////////////////////
namespace XInputCore {
BOOL WINAPI Initialize();
BOOL WINAPI Close();
HRESULT WINAPI Enter();
HRESULT WINAPI Leave();
HRESULT WINAPI ProcessAPIRequest(DWORD dwUserIndex, HRESULT(*pfn_ApiAction)(DeviceInfo_t*, void*, DWORD), void* pApiParam, DWORD reserved);
void WINAPI EnableCommunications(BOOL bEnabled);
}
/////////////////////////////////////////////////////
// QuickDriverEnum namespace
/////////////////////////////////////////////////////
class QuickDriverEnum
{
HDEVINFO hDevInfo;
DWORD MemberIndex;
PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData;
DWORD DeviceInterfaceDetailDataSize;
public:
QuickDriverEnum()
{
hDevInfo = (HDEVINFO)INVALID_HANDLE_VALUE;
MemberIndex = 0;
DeviceInterfaceDetailData = nullptr;
DeviceInterfaceDetailDataSize = 0;
}
~QuickDriverEnum()
{
if (hDevInfo != (HDEVINFO)INVALID_HANDLE_VALUE)
{
DriverComm::SetupDiWrapper::DestroyDeviceInfoList(hDevInfo);
hDevInfo = (HDEVINFO)INVALID_HANDLE_VALUE;
MemberIndex = 0;
}
if (DeviceInterfaceDetailData != nullptr)
{
Utilities::MemFree(DeviceInterfaceDetailData);
DeviceInterfaceDetailData = nullptr;
DeviceInterfaceDetailDataSize = 0;
}
}
bool Restart()
{
if (hDevInfo != (HDEVINFO)INVALID_HANDLE_VALUE)
{
DriverComm::SetupDiWrapper::DestroyDeviceInfoList(hDevInfo);
hDevInfo = (HDEVINFO)INVALID_HANDLE_VALUE;
}
MemberIndex = 0;
if (DeviceInterfaceDetailData == nullptr)
{
DeviceInterfaceDetailDataSize = (sizeof(DWORD) + MAX_PATH * sizeof(WCHAR));
DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA_W)Utilities::MemAlloc(DeviceInterfaceDetailDataSize);
if (DeviceInterfaceDetailData == nullptr)
{
DeviceInterfaceDetailDataSize = 0;
return false;
}
}
hDevInfo = DriverComm::SetupDiWrapper::GetClassDevs(&Protocol::XUSB_INTERFACE_CLASS_GUID, nullptr, nullptr, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
return hDevInfo != (HDEVINFO)INVALID_HANDLE_VALUE;
}
bool GetNext(LPHANDLE phDevice)
{
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
DWORD RequiredSize;
if (phDevice == nullptr)
return false;
*phDevice = INVALID_HANDLE_VALUE;
if (hDevInfo == INVALID_HANDLE_VALUE || DeviceInterfaceDetailData == nullptr)
return false;
ZeroMemory(&DeviceInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA));
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if (DriverComm::SetupDiWrapper::EnumDeviceInterfaces(hDevInfo, nullptr, &Protocol::XUSB_INTERFACE_CLASS_GUID, MemberIndex, &DeviceInterfaceData) == FALSE)
return false;
++MemberIndex;
DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W);
RequiredSize = 0;
if (DriverComm::SetupDiWrapper::GetDeviceInterfaceDetail(hDevInfo, &DeviceInterfaceData, DeviceInterfaceDetailData, DeviceInterfaceDetailDataSize, &RequiredSize, nullptr) == TRUE)
{
*phDevice = CreateFileW(DeviceInterfaceDetailData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
return true;
}
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return false;
Utilities::MemFree(DeviceInterfaceDetailData);
DeviceInterfaceDetailDataSize = RequiredSize;
DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA_W)Utilities::MemAlloc(DeviceInterfaceDetailDataSize);
if (DeviceInterfaceDetailData == nullptr)
{
DeviceInterfaceDetailDataSize = 0;
return false;
}
DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W);
if (DriverComm::SetupDiWrapper::GetDeviceInterfaceDetail(hDevInfo, &DeviceInterfaceData, DeviceInterfaceDetailData, DeviceInterfaceDetailDataSize, nullptr, nullptr) == FALSE)
return false;
*phDevice = CreateFileW(DeviceInterfaceDetailData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
return true;
}
bool MinFillFromInterface(HANDLE hDevice, DeviceInfo_t *pDevice)
{
OutDeviceInfos_t outBuff;
if (hDevice == INVALID_HANDLE_VALUE || pDevice == nullptr)
return false;
if (DriverComm::GetDeviceInfoFromInterface(hDevice, &outBuff) < 0 || (outBuff.unk2 & 0x80))
return false;
ZeroMemory(pDevice, sizeof(DeviceInfo_t));
pDevice->hDevice = hDevice;
pDevice->status = DEVICE_STATUS_ACTIVE;
pDevice->hGuideWait = INVALID_HANDLE_VALUE;
pDevice->wType = outBuff.wType;
pDevice->vendorId = outBuff.vendorId;
pDevice->productId = outBuff.productId;
return true;
}
};
/////////////////////////////////////////////////////
// Controller namespace
/////////////////////////////////////////////////////
DWORD WINAPI Controller_GetUserKeystroke(DeviceInfo_t* pDevice, BYTE unk1, DWORD reserved, XINPUT_KEYSTROKE* pKeystroke);
DWORD WINAPI Controller_CalculateKeyFromThumbPos(SHORT base, SHORT X, SHORT Y);
/////////////////////////////////////////////////////
// Implementations
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
// Anonymous namespace
/////////////////////////////////////////////////////
namespace {
DWORD WINAPI XInputReturnCodeFromHRESULT(HRESULT hr)
{
if (hr >= 0)
{
if (hr == 1)
return ERROR_DEVICE_NOT_CONNECTED;
return ERROR_SUCCESS;
}
if (hr == E_OUTOFMEMORY)
return ERROR_OUTOFMEMORY;
if (hr == E_INVALIDARG)
return ERROR_BAD_ARGUMENTS;
return hr;
}
HRESULT WINAPI GrowList(DWORD newSize)
{
DeviceInfo_t** ppDevice;
ppDevice = (DeviceInfo_t**)Utilities::MemAlloc(newSize * sizeof(DeviceInfo_t*));
if (!ppDevice)
return E_OUTOFMEMORY;
CopyMemory(ppDevice, g_pDeviceList, g_dwDeviceListSize * sizeof(DeviceInfo_t*));
Utilities::MemFree(g_pDeviceList);
g_pDeviceList = ppDevice;
g_dwDeviceListSize = newSize;
return S_OK;
}
void WINAPI CopyGamepadStateToDeviceInfo(DeviceInfo_t* pDevice, GamepadState0100 *pGamepadState)
{
pDevice->DeviceState.dwPacketNumber = pGamepadState->dwPacketNumber;
pDevice->DeviceState.Gamepad.bLeftTrigger = pGamepadState->bLeftTrigger;
pDevice->DeviceState.Gamepad.bRightTrigger = pGamepadState->bRightTrigger;
pDevice->DeviceState.Gamepad.sThumbLX = pGamepadState->sThumbLX;
pDevice->DeviceState.Gamepad.sThumbLY = pGamepadState->sThumbLY;
pDevice->DeviceState.Gamepad.sThumbRX = pGamepadState->sThumbRX;
pDevice->DeviceState.Gamepad.sThumbRY = pGamepadState->sThumbRY;
pDevice->DeviceState.Gamepad.wButtons = pGamepadState->wButtons;
if (pGamepadState->status == 1)
pDevice->status |= DEVICE_STATUS_ACTIVE;
else
pDevice->status &= ~DEVICE_STATUS_ACTIVE;
pDevice->inputId = pGamepadState->inputId;
}
void WINAPI CopyGamepadStateToDeviceInfo(DeviceInfo_t* pDevice, GamepadState0101* pGamepadState)
{
pDevice->DeviceState.dwPacketNumber = pGamepadState->dwPacketNumber;
pDevice->DeviceState.Gamepad.bLeftTrigger = pGamepadState->bLeftTrigger;
pDevice->DeviceState.Gamepad.bRightTrigger = pGamepadState->bRightTrigger;
pDevice->DeviceState.Gamepad.sThumbLX = pGamepadState->sThumbLX;
pDevice->DeviceState.Gamepad.sThumbLY = pGamepadState->sThumbLY;
pDevice->DeviceState.Gamepad.sThumbRX = pGamepadState->sThumbRX;
pDevice->DeviceState.Gamepad.sThumbRY = pGamepadState->sThumbRY;
pDevice->DeviceState.Gamepad.wButtons = pGamepadState->wButtons;
pDevice->DeviceState.Gamepad.wButtons &= XINPUT_BUTTON_MASK;
if (pGamepadState->status == 1)
pDevice->status |= DEVICE_STATUS_ACTIVE;
else
pDevice->status &= ~DEVICE_STATUS_ACTIVE;
pDevice->inputId = pGamepadState->inputId;
}
void WINAPI TranslateCapabilities(GamepadCapabilities0101* pGamepadCapabilities, XINPUT_CAPABILITIES* pCapabilities)
{
pCapabilities->Type = pGamepadCapabilities->Type;
pCapabilities->SubType = pGamepadCapabilities->SubType;
pCapabilities->Flags = XINPUT_CAPS_VOICE_SUPPORTED;
pCapabilities->Gamepad.wButtons = pGamepadCapabilities->wButtons;
pCapabilities->Gamepad.bLeftTrigger = pGamepadCapabilities->bLeftTrigger;
pCapabilities->Gamepad.bRightTrigger = pGamepadCapabilities->bRightTrigger;
pCapabilities->Gamepad.sThumbLX = pGamepadCapabilities->sThumbLX;
pCapabilities->Gamepad.sThumbLY = pGamepadCapabilities->sThumbLY;
pCapabilities->Gamepad.sThumbRX = pGamepadCapabilities->sThumbRX;
pCapabilities->Gamepad.sThumbRY = pGamepadCapabilities->sThumbRY;
pCapabilities->Vibration.wLeftMotorSpeed = pGamepadCapabilities->bLeftMotorSpeed;
pCapabilities->Vibration.wRightMotorSpeed = pGamepadCapabilities->bRightMotorSpeed;
pCapabilities->Gamepad.wButtons &= XINPUT_BUTTON_MASK;
}
HRESULT WINAPI SendReceiveIoctl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBufferSize, LPVOID lpOutBuffer, DWORD nOutBufferSize, LPOVERLAPPED lpOverlapped)
{
DWORD BytesReturned = 0;
BOOL bRes = FALSE;
if (g_pfnDeviceIoControl)
bRes = g_pfnDeviceIoControl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, &BytesReturned, lpOverlapped);
else
bRes = DeviceIoControl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, &BytesReturned, lpOverlapped);
if (bRes == TRUE)
return S_OK;
if (GetLastError() == ERROR_IO_PENDING && lpOverlapped)
return E_PENDING;
return E_FAIL;
}
HRESULT WINAPI SendIoctl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBufferSize)
{
return SendReceiveIoctl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, nullptr, 0, nullptr);
}
HRESULT WINAPI ReceiveIoctl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpOutBuffer, DWORD nOufBufferSize)
{
return SendReceiveIoctl(hDevice, dwIoControlCode, nullptr, 0, lpOutBuffer, nOufBufferSize, nullptr);
}
HRESULT WINAPI EnumerateXInputDevices()
{
HRESULT hr;
HDEVINFO hDeviceInfoSet;
DWORD dwMemberIndex = 0;
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA_W pDeviceInterfaceDetailData;
HANDLE hDevice;
if ((hr = GetDeviceList(&hDeviceInfoSet)) < 0)
return hr;
while (GetDeviceInterfaceData(hDeviceInfoSet, dwMemberIndex++, &DeviceInterfaceData) >= 0)
{
if ((hr = GetDeviceInterfaceDetail(hDeviceInfoSet, &DeviceInterfaceData, &pDeviceInterfaceDetailData)) < 0)
continue;
hDevice = CreateFileW(pDeviceInterfaceDetailData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hDevice != INVALID_HANDLE_VALUE)
{
hr = EnumerateDevicesOnDeviceInterface(hDevice, pDeviceInterfaceDetailData->DevicePath);
if (hr < 0)
hr = S_OK;
CloseHandle(hDevice);
hDevice = INVALID_HANDLE_VALUE;
}
}
if (hDeviceInfoSet != (HDEVINFO)INVALID_HANDLE_VALUE)
{
DriverComm::SetupDiWrapper::DestroyDeviceInfoList(hDeviceInfoSet);
hDeviceInfoSet = (HDEVINFO)INVALID_HANDLE_VALUE;
}
return hr;
}
HRESULT WINAPI EnumerateDevicesOnDeviceInterface(HANDLE hDevice, LPCWSTR lpDevicePath)
{
HRESULT hr;
DeviceInfo_t* pDevice = nullptr;
OutDeviceInfos_t deviceInfos = {};
hr = DriverComm::GetDeviceInfoFromInterface(hDevice, &deviceInfos);
if (hr < 0 || (deviceInfos.unk3 & 0x80))
return hr;
for (int i = 0; i < deviceInfos.deviceIndex; ++i)
{
if (pDevice != nullptr)
{
DeviceInfo::Recycle(pDevice);
}
else
{
pDevice = DeviceInfo::Create(hDevice, lpDevicePath);
if (pDevice == nullptr)
return E_OUTOFMEMORY;
}
pDevice->dwUserIndex = i;
pDevice->productId = deviceInfos.productId;
pDevice->vendorId = deviceInfos.vendorId;
pDevice->wType = deviceInfos.wType;
hr = DriverComm::GetLatestDeviceInfo(pDevice);
if (hr >= 0)
{
hr = ProcessEnumeratedDevice(pDevice);
if (hr == S_OK)
{
pDevice = nullptr;
}
}
}
if (pDevice)
{
DeviceInfo::Destroy(pDevice);
pDevice = nullptr;
}
return hr;
}
HRESULT WINAPI ProcessEnumeratedDevice(DeviceInfo_t* pDevice)
{
DWORD dwUserIndex = 255;
HRESULT hr;
BYTE LEDState;
if (DeviceInfo::IsDeviceInactive(pDevice) == TRUE)
return 1;
hr = DeviceList::IsDevicePresent(pDevice);
if (hr < 0)
return hr;
if (hr == 0)
return 1;
LEDState = 0;
if (DriverComm::GetLEDState(pDevice, &LEDState) >= 0 && LEDState < 15)
{
dwUserIndex = Protocol::LEDState::XINPUT_LED_TO_PORT_MAP[LEDState];
if (dwUserIndex != 255)
{
hr = DeviceList::IsDeviceOnPort(dwUserIndex);
if (hr < 0)
return hr;
if (hr != TRUE)
{
dwUserIndex = 255;
DriverComm::SendLEDState(pDevice, Protocol::LEDState::XINPUT_LED_FLIPFLOP);
}
}
}
for (int i = 0; i < XUSER_MAX_COUNT && dwUserIndex == 255; ++i)
{
hr = DeviceList::IsDeviceOnPort(i);
if (hr < 0)
return hr;
if (hr == TRUE)
dwUserIndex = i;
}
if (dwUserIndex >= XUSER_MAX_COUNT)
return 1;
hr = DeviceList::SetDeviceOnPort(dwUserIndex, pDevice);
if (hr < 0)
return hr;
if (Utilities::IsSettingSet(SET_USER_LED_ON_CREATE))
DriverComm::SendLEDState(pDevice, Protocol::LEDState::XINPUT_PORT_TO_LED_MAP[dwUserIndex % MAX_XINPUT_PORT_TO_LED_MAP]);
return S_OK;
}
HRESULT WINAPI GetDeviceList(HDEVINFO* phDevInfo)
{
HRESULT hr;
*phDevInfo = DriverComm::SetupDiWrapper::GetClassDevs(&Protocol::XUSB_INTERFACE_CLASS_GUID, nullptr, nullptr, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if (*phDevInfo == (HDEVINFO)INVALID_HANDLE_VALUE)
hr = E_FAIL;
else
hr = S_OK;