Skip to content

Commit a0280d9

Browse files
committed
Switch from Scp to ViGEm, use interrupt transfers instead of control transfers when possible
Seems the controller I received doesn't respond well to GET_REPORT control transfers, just seems to respond with whatever the state was when the controller was first plugged in, very weird (especially considering this controller is just an earlier revision of my main Madcatz controller, which responds fine...) Taking a look at the TigerGame driver it seemed to use interrupts instead, which xboxdevwiki says should be supported by all controllers. So I've made XB2X try to use interrupts if possible, reverting back to control transfers if interrupts aren't available or an error occurs in the interrupt request, hopefully this should make XB2X a lot more compatible with any other controllers that get thrown at it!
1 parent c618f38 commit a0280d9

32 files changed

+2294
-575
lines changed

3rdparty/ViGEm/Client.h

Lines changed: 495 additions & 0 deletions
Large diffs are not rendered by default.

3rdparty/ViGEm/Common.h

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2017 Benjamin "Nefarius" Höglinger
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
26+
#pragma once
27+
28+
//
29+
// Represents the desired target type for the emulated device.
30+
//
31+
typedef enum _VIGEM_TARGET_TYPE
32+
{
33+
//
34+
// Microsoft Xbox 360 Controller (wired)
35+
//
36+
Xbox360Wired,
37+
//
38+
// Microsoft Xbox One Controller (wired)
39+
//
40+
XboxOneWired,
41+
//
42+
// Sony DualShock 4 (wired)
43+
//
44+
DualShock4Wired
45+
46+
} VIGEM_TARGET_TYPE, *PVIGEM_TARGET_TYPE;
47+
48+
//
49+
// Possible XUSB report buttons.
50+
//
51+
typedef enum _XUSB_BUTTON
52+
{
53+
XUSB_GAMEPAD_DPAD_UP = 0x0001,
54+
XUSB_GAMEPAD_DPAD_DOWN = 0x0002,
55+
XUSB_GAMEPAD_DPAD_LEFT = 0x0004,
56+
XUSB_GAMEPAD_DPAD_RIGHT = 0x0008,
57+
XUSB_GAMEPAD_START = 0x0010,
58+
XUSB_GAMEPAD_BACK = 0x0020,
59+
XUSB_GAMEPAD_LEFT_THUMB = 0x0040,
60+
XUSB_GAMEPAD_RIGHT_THUMB = 0x0080,
61+
XUSB_GAMEPAD_LEFT_SHOULDER = 0x0100,
62+
XUSB_GAMEPAD_RIGHT_SHOULDER = 0x0200,
63+
XUSB_GAMEPAD_GUIDE = 0x0400,
64+
XUSB_GAMEPAD_A = 0x1000,
65+
XUSB_GAMEPAD_B = 0x2000,
66+
XUSB_GAMEPAD_X = 0x4000,
67+
XUSB_GAMEPAD_Y = 0x8000
68+
69+
} XUSB_BUTTON, *PXUSB_BUTTON;
70+
71+
//
72+
// Represents an XINPUT_GAMEPAD-compatible report structure.
73+
//
74+
typedef struct _XUSB_REPORT
75+
{
76+
USHORT wButtons;
77+
BYTE bLeftTrigger;
78+
BYTE bRightTrigger;
79+
SHORT sThumbLX;
80+
SHORT sThumbLY;
81+
SHORT sThumbRX;
82+
SHORT sThumbRY;
83+
84+
} XUSB_REPORT, *PXUSB_REPORT;
85+
86+
//
87+
// Initializes a _XUSB_REPORT structure.
88+
//
89+
VOID FORCEINLINE XUSB_REPORT_INIT(
90+
_Out_ PXUSB_REPORT Report
91+
)
92+
{
93+
RtlZeroMemory(Report, sizeof(XUSB_REPORT));
94+
}
95+
96+
//
97+
// The color value (RGB) of a DualShock 4 Lightbar
98+
//
99+
typedef struct _DS4_LIGHTBAR_COLOR
100+
{
101+
//
102+
// Red part of the Lightbar (0-255).
103+
//
104+
UCHAR Red;
105+
106+
//
107+
// Green part of the Lightbar (0-255).
108+
//
109+
UCHAR Green;
110+
111+
//
112+
// Blue part of the Lightbar (0-255).
113+
//
114+
UCHAR Blue;
115+
116+
} DS4_LIGHTBAR_COLOR, *PDS4_LIGHTBAR_COLOR;
117+
118+
//
119+
// DualShock 4 digital buttons
120+
//
121+
typedef enum _DS4_BUTTONS
122+
{
123+
DS4_BUTTON_THUMB_RIGHT = 1 << 15,
124+
DS4_BUTTON_THUMB_LEFT = 1 << 14,
125+
DS4_BUTTON_OPTIONS = 1 << 13,
126+
DS4_BUTTON_SHARE = 1 << 12,
127+
DS4_BUTTON_TRIGGER_RIGHT = 1 << 11,
128+
DS4_BUTTON_TRIGGER_LEFT = 1 << 10,
129+
DS4_BUTTON_SHOULDER_RIGHT = 1 << 9,
130+
DS4_BUTTON_SHOULDER_LEFT = 1 << 8,
131+
DS4_BUTTON_TRIANGLE = 1 << 7,
132+
DS4_BUTTON_CIRCLE = 1 << 6,
133+
DS4_BUTTON_CROSS = 1 << 5,
134+
DS4_BUTTON_SQUARE = 1 << 4
135+
136+
} DS4_BUTTONS, *PDS4_BUTTONS;
137+
138+
//
139+
// DualShock 4 special buttons
140+
//
141+
typedef enum _DS4_SPECIAL_BUTTONS
142+
{
143+
DS4_SPECIAL_BUTTON_PS = 1 << 0,
144+
DS4_SPECIAL_BUTTON_TOUCHPAD = 1 << 1
145+
146+
} DS4_SPECIAL_BUTTONS, *PDS4_SPECIAL_BUTTONS;
147+
148+
//
149+
// DualShock 4 directional pad (HAT) values
150+
//
151+
typedef enum _DS4_DPAD_DIRECTIONS
152+
{
153+
DS4_BUTTON_DPAD_NONE = 0x8,
154+
DS4_BUTTON_DPAD_NORTHWEST = 0x7,
155+
DS4_BUTTON_DPAD_WEST = 0x6,
156+
DS4_BUTTON_DPAD_SOUTHWEST = 0x5,
157+
DS4_BUTTON_DPAD_SOUTH = 0x4,
158+
DS4_BUTTON_DPAD_SOUTHEAST = 0x3,
159+
DS4_BUTTON_DPAD_EAST = 0x2,
160+
DS4_BUTTON_DPAD_NORTHEAST = 0x1,
161+
DS4_BUTTON_DPAD_NORTH = 0x0
162+
163+
} DS4_DPAD_DIRECTIONS, *PDS4_DPAD_DIRECTIONS;
164+
165+
//
166+
// DualShock 4 HID Input report
167+
//
168+
typedef struct _DS4_REPORT
169+
{
170+
BYTE bThumbLX;
171+
BYTE bThumbLY;
172+
BYTE bThumbRX;
173+
BYTE bThumbRY;
174+
USHORT wButtons;
175+
BYTE bSpecial;
176+
BYTE bTriggerL;
177+
BYTE bTriggerR;
178+
179+
} DS4_REPORT, *PDS4_REPORT;
180+
181+
//
182+
// Sets the current state of the D-PAD on a DualShock 4 report.
183+
//
184+
VOID FORCEINLINE DS4_SET_DPAD(
185+
_Out_ PDS4_REPORT Report,
186+
_In_ DS4_DPAD_DIRECTIONS Dpad
187+
)
188+
{
189+
Report->wButtons &= ~0xF;
190+
Report->wButtons |= (USHORT)Dpad;
191+
}
192+
193+
VOID FORCEINLINE DS4_REPORT_INIT(
194+
_Out_ PDS4_REPORT Report
195+
)
196+
{
197+
RtlZeroMemory(Report, sizeof(DS4_REPORT));
198+
199+
Report->bThumbLX = 0x80;
200+
Report->bThumbLY = 0x80;
201+
Report->bThumbRX = 0x80;
202+
Report->bThumbRY = 0x80;
203+
204+
DS4_SET_DPAD(Report, DS4_BUTTON_DPAD_NONE);
205+
}
206+

3rdparty/ViGEm/Util.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include "Common.h"
4+
#include <limits.h>
5+
6+
VOID FORCEINLINE XUSB_TO_DS4_REPORT(
7+
_Out_ PXUSB_REPORT Input,
8+
_Out_ PDS4_REPORT Output
9+
)
10+
{
11+
if (Input->wButtons & XUSB_GAMEPAD_BACK) Output->wButtons |= DS4_BUTTON_SHARE;
12+
if (Input->wButtons & XUSB_GAMEPAD_START) Output->wButtons |= DS4_BUTTON_OPTIONS;
13+
if (Input->wButtons & XUSB_GAMEPAD_LEFT_THUMB) Output->wButtons |= DS4_BUTTON_THUMB_LEFT;
14+
if (Input->wButtons & XUSB_GAMEPAD_RIGHT_THUMB) Output->wButtons |= DS4_BUTTON_THUMB_RIGHT;
15+
if (Input->wButtons & XUSB_GAMEPAD_LEFT_SHOULDER) Output->wButtons |= DS4_BUTTON_SHOULDER_LEFT;
16+
if (Input->wButtons & XUSB_GAMEPAD_RIGHT_SHOULDER) Output->wButtons |= DS4_BUTTON_SHOULDER_RIGHT;
17+
if (Input->wButtons & XUSB_GAMEPAD_GUIDE) Output->bSpecial |= DS4_SPECIAL_BUTTON_PS;
18+
if (Input->wButtons & XUSB_GAMEPAD_A) Output->wButtons |= DS4_BUTTON_CROSS;
19+
if (Input->wButtons & XUSB_GAMEPAD_B) Output->wButtons |= DS4_BUTTON_CIRCLE;
20+
if (Input->wButtons & XUSB_GAMEPAD_X) Output->wButtons |= DS4_BUTTON_SQUARE;
21+
if (Input->wButtons & XUSB_GAMEPAD_Y) Output->wButtons |= DS4_BUTTON_TRIANGLE;
22+
23+
Output->bTriggerL = Input->bLeftTrigger;
24+
Output->bTriggerR = Input->bRightTrigger;
25+
26+
if (Input->bLeftTrigger > 0)Output->wButtons |= DS4_BUTTON_TRIGGER_LEFT;
27+
if (Input->bRightTrigger > 0)Output->wButtons |= DS4_BUTTON_TRIGGER_RIGHT;
28+
29+
if (Input->wButtons & XUSB_GAMEPAD_DPAD_UP) DS4_SET_DPAD(Output, DS4_BUTTON_DPAD_NORTH);
30+
if (Input->wButtons & XUSB_GAMEPAD_DPAD_RIGHT) DS4_SET_DPAD(Output, DS4_BUTTON_DPAD_EAST);
31+
if (Input->wButtons & XUSB_GAMEPAD_DPAD_DOWN) DS4_SET_DPAD(Output, DS4_BUTTON_DPAD_SOUTH);
32+
if (Input->wButtons & XUSB_GAMEPAD_DPAD_LEFT) DS4_SET_DPAD(Output, DS4_BUTTON_DPAD_WEST);
33+
34+
if (Input->wButtons & XUSB_GAMEPAD_DPAD_UP
35+
&& Input->wButtons & XUSB_GAMEPAD_DPAD_RIGHT) DS4_SET_DPAD(Output, DS4_BUTTON_DPAD_NORTHEAST);
36+
if (Input->wButtons & XUSB_GAMEPAD_DPAD_RIGHT
37+
&& Input->wButtons & XUSB_GAMEPAD_DPAD_DOWN) DS4_SET_DPAD(Output, DS4_BUTTON_DPAD_SOUTHEAST);
38+
if (Input->wButtons & XUSB_GAMEPAD_DPAD_DOWN
39+
&& Input->wButtons & XUSB_GAMEPAD_DPAD_LEFT) DS4_SET_DPAD(Output, DS4_BUTTON_DPAD_SOUTHWEST);
40+
if (Input->wButtons & XUSB_GAMEPAD_DPAD_LEFT
41+
&& Input->wButtons & XUSB_GAMEPAD_DPAD_UP) DS4_SET_DPAD(Output, DS4_BUTTON_DPAD_NORTHWEST);
42+
43+
Output->bThumbLX = ((Input->sThumbLX + ((USHRT_MAX / 2) + 1)) / 257);
44+
Output->bThumbLY = (-(Input->sThumbLY + ((USHRT_MAX / 2) - 1)) / 257);
45+
Output->bThumbLY = (Output->bThumbLY == 0) ? 0xFF : Output->bThumbLY;
46+
Output->bThumbRX = ((Input->sThumbRX + ((USHRT_MAX / 2) + 1)) / 257);
47+
Output->bThumbRY = (-(Input->sThumbRY + ((USHRT_MAX / 2) + 1)) / 257);
48+
Output->bThumbRY = (Output->bThumbRY == 0) ? 0xFF : Output->bThumbRY;
49+
}
50+

0 commit comments

Comments
 (0)