Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Windows/XinputDevice.cpp
3185 views
1
#include "ppsspp_config.h"
2
3
#include <climits>
4
#include <algorithm>
5
6
#include "Common/System/NativeApp.h"
7
#include "Common/CommonWindows.h"
8
#include "Common/Log.h"
9
#include "Common/StringUtils.h"
10
#include "Common/TimeUtil.h"
11
#include "Common/Input/InputState.h"
12
#include "Common/Input/KeyCodes.h"
13
#include "XinputDevice.h"
14
#include "Core/Config.h"
15
#include "Core/Core.h"
16
#include "Core/System.h"
17
#include "Core/KeyMap.h"
18
#include "Core/HLE/sceCtrl.h"
19
20
// Utilities to dynamically load XInput. Adapted from SDL.
21
22
#if !PPSSPP_PLATFORM(UWP)
23
24
#ifndef __MINGW32__
25
struct XINPUT_CAPABILITIES_EX {
26
XINPUT_CAPABILITIES Capabilities;
27
WORD VendorId;
28
WORD ProductId;
29
WORD VersionNumber;
30
WORD unk1;
31
DWORD unk2;
32
};
33
#endif
34
35
typedef DWORD (WINAPI *XInputGetState_t) (DWORD dwUserIndex, XINPUT_STATE* pState);
36
typedef DWORD (WINAPI *XInputSetState_t) (DWORD dwUserIndex, XINPUT_VIBRATION* pVibration);
37
typedef DWORD (WINAPI *XInputGetCapabilitiesEx_t) (DWORD unknown, DWORD dwUserIndex, DWORD flags, XINPUT_CAPABILITIES_EX *pCapabilities);
38
39
static XInputGetState_t PPSSPP_XInputGetState = nullptr;
40
static XInputSetState_t PPSSPP_XInputSetState = nullptr;
41
static XInputGetCapabilitiesEx_t PPSSPP_XInputGetCapabilitiesEx = nullptr;
42
static DWORD PPSSPP_XInputVersion = 0;
43
static HMODULE s_pXInputDLL = nullptr;
44
static int s_XInputDLLRefCount = 0;
45
46
static void UnloadXInputDLL();
47
48
static int LoadXInputDLL() {
49
DWORD version = 0;
50
51
if (s_pXInputDLL) {
52
s_XInputDLLRefCount++;
53
return 0; /* already loaded */
54
}
55
56
version = (1 << 16) | 4;
57
s_pXInputDLL = LoadLibrary( L"XInput1_4.dll" ); // 1.4 Ships with Windows 8.
58
if (!s_pXInputDLL) {
59
version = (1 << 16) | 3;
60
s_pXInputDLL = LoadLibrary( L"XInput1_3.dll" ); // 1.3 Ships with Vista and Win7, can be installed as a restributable component.
61
if (!s_pXInputDLL) {
62
version = (1 << 16) | 0;
63
s_pXInputDLL = LoadLibrary( L"XInput9_1_0.dll" ); // 1.0 ships with any Windows since WinXP
64
}
65
}
66
if (!s_pXInputDLL) {
67
return -1;
68
}
69
70
PPSSPP_XInputVersion = version;
71
s_XInputDLLRefCount = 1;
72
73
/* 100 is the ordinal for _XInputGetStateEx, which returns the same struct as XinputGetState, but with extra data in wButtons for the guide button, we think...
74
Let's try the name first, though - then fall back to ordinal, then to a non-Ex version (xinput9_1_0.dll doesn't have Ex) */
75
PPSSPP_XInputGetState = (XInputGetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetStateEx" );
76
if ( !PPSSPP_XInputGetState ) {
77
PPSSPP_XInputGetState = (XInputGetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, (LPCSTR)100 );
78
if ( !PPSSPP_XInputGetState ) {
79
PPSSPP_XInputGetState = (XInputGetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetState" );
80
}
81
}
82
83
if ( !PPSSPP_XInputGetState ) {
84
UnloadXInputDLL();
85
return -1;
86
}
87
88
/* Let's try the name first, then fall back to a non-Ex version (xinput9_1_0.dll doesn't have Ex) */
89
PPSSPP_XInputSetState = (XInputSetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputSetStateEx");
90
if (!PPSSPP_XInputSetState) {
91
PPSSPP_XInputSetState = (XInputSetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputSetState");
92
}
93
94
if (!PPSSPP_XInputSetState) {
95
UnloadXInputDLL();
96
return -1;
97
}
98
99
if (PPSSPP_XInputVersion >= ((1 << 16) | 4)) {
100
PPSSPP_XInputGetCapabilitiesEx = (XInputGetCapabilitiesEx_t)GetProcAddress((HMODULE)s_pXInputDLL, (LPCSTR)108);
101
}
102
103
return 0;
104
}
105
106
static void UnloadXInputDLL() {
107
if ( s_pXInputDLL ) {
108
if (--s_XInputDLLRefCount == 0) {
109
FreeLibrary( s_pXInputDLL );
110
s_pXInputDLL = nullptr;
111
}
112
}
113
}
114
115
#else
116
static int LoadXInputDLL() { return 0; }
117
static void UnloadXInputDLL() {}
118
#define PPSSPP_XInputGetState XInputGetState
119
#define PPSSPP_XInputSetState XInputSetState
120
#endif
121
122
#ifndef XUSER_MAX_COUNT
123
#define XUSER_MAX_COUNT 4
124
#endif
125
126
// Undocumented. Steam annoyingly grabs this button though....
127
#define XINPUT_GUIDE_BUTTON 0x400
128
129
130
// Permanent map. Actual mapping happens elsewhere.
131
static const struct { int from; InputKeyCode to; } xinput_ctrl_map[] = {
132
{XINPUT_GAMEPAD_A, NKCODE_BUTTON_A},
133
{XINPUT_GAMEPAD_B, NKCODE_BUTTON_B},
134
{XINPUT_GAMEPAD_X, NKCODE_BUTTON_X},
135
{XINPUT_GAMEPAD_Y, NKCODE_BUTTON_Y},
136
{XINPUT_GAMEPAD_BACK, NKCODE_BUTTON_SELECT},
137
{XINPUT_GAMEPAD_START, NKCODE_BUTTON_START},
138
{XINPUT_GAMEPAD_LEFT_SHOULDER, NKCODE_BUTTON_L1},
139
{XINPUT_GAMEPAD_RIGHT_SHOULDER, NKCODE_BUTTON_R1},
140
{XINPUT_GAMEPAD_LEFT_THUMB, NKCODE_BUTTON_THUMBL},
141
{XINPUT_GAMEPAD_RIGHT_THUMB, NKCODE_BUTTON_THUMBR},
142
{XINPUT_GAMEPAD_DPAD_UP, NKCODE_DPAD_UP},
143
{XINPUT_GAMEPAD_DPAD_DOWN, NKCODE_DPAD_DOWN},
144
{XINPUT_GAMEPAD_DPAD_LEFT, NKCODE_DPAD_LEFT},
145
{XINPUT_GAMEPAD_DPAD_RIGHT, NKCODE_DPAD_RIGHT},
146
{XINPUT_GUIDE_BUTTON, NKCODE_HOME},
147
};
148
149
XinputDevice::XinputDevice() {
150
if (LoadXInputDLL() != 0) {
151
WARN_LOG(Log::sceCtrl, "Failed to load XInput! DLL missing");
152
}
153
154
for (size_t i = 0; i < ARRAY_SIZE(check_delay); ++i) {
155
check_delay[i] = (int)i;
156
}
157
}
158
159
XinputDevice::~XinputDevice() {
160
UnloadXInputDLL();
161
}
162
163
int XinputDevice::UpdateState() {
164
#if !PPSSPP_PLATFORM(UWP)
165
if (!s_pXInputDLL)
166
return 0;
167
#endif
168
169
bool anySuccess = false;
170
for (int i = 0; i < XUSER_MAX_COUNT; i++) {
171
XINPUT_STATE state{};
172
if (check_delay[i]-- > 0)
173
continue;
174
DWORD dwResult = PPSSPP_XInputGetState(i, &state);
175
if (dwResult == ERROR_SUCCESS) {
176
XINPUT_VIBRATION vibration{};
177
UpdatePad(i, state, vibration);
178
anySuccess = true;
179
} else {
180
check_delay[i] = 30;
181
}
182
}
183
184
// If we get XInput, skip the others. This might not actually be a good idea,
185
// and was done to avoid conflicts between DirectInput and XInput.
186
return 0; // anySuccess ? UPDATESTATE_SKIP_PAD : 0;
187
}
188
189
void XinputDevice::UpdatePad(int pad, const XINPUT_STATE &state, XINPUT_VIBRATION &vibration) {
190
if (!notified_[pad]) {
191
notified_[pad] = true;
192
#if !PPSSPP_PLATFORM(UWP)
193
XINPUT_CAPABILITIES_EX caps{};
194
if (PPSSPP_XInputGetCapabilitiesEx != nullptr && PPSSPP_XInputGetCapabilitiesEx(1, pad, 0, &caps) == ERROR_SUCCESS) {
195
KeyMap::NotifyPadConnected(DEVICE_ID_XINPUT_0 + pad, StringFromFormat("Xbox 360 Pad: %d/%d", caps.VendorId, caps.ProductId));
196
} else {
197
#else
198
{
199
#endif
200
KeyMap::NotifyPadConnected(DEVICE_ID_XINPUT_0 + pad, "Xbox 360 Pad");
201
}
202
}
203
ApplyButtons(pad, state);
204
ApplyVibration(pad, vibration);
205
206
AxisInput axis[6];
207
int axisCount = 0;
208
for (int i = 0; i < ARRAY_SIZE(axis); i++) {
209
axis[i].deviceId = (InputDeviceID)(DEVICE_ID_XINPUT_0 + pad);
210
}
211
auto sendAxis = [&](InputAxis axisId, float value, int axisIndex) {
212
if (value != prevAxisValue_[pad][axisIndex]) {
213
prevAxisValue_[pad][axisIndex] = value;
214
axis[axisCount].axisId = axisId;
215
axis[axisCount].value = value;
216
axisCount++;
217
}
218
};
219
220
sendAxis(JOYSTICK_AXIS_X, (float)state.Gamepad.sThumbLX / 32767.0f, 0);
221
sendAxis(JOYSTICK_AXIS_Y, (float)state.Gamepad.sThumbLY / 32767.0f, 1);
222
sendAxis(JOYSTICK_AXIS_Z, (float)state.Gamepad.sThumbRX / 32767.0f, 2);
223
sendAxis(JOYSTICK_AXIS_RZ, (float)state.Gamepad.sThumbRY / 32767.0f, 3);
224
sendAxis(JOYSTICK_AXIS_LTRIGGER, (float)state.Gamepad.bLeftTrigger / 255.0f, 4);
225
sendAxis(JOYSTICK_AXIS_RTRIGGER, (float)state.Gamepad.bRightTrigger / 255.0f, 5);
226
227
if (axisCount) {
228
NativeAxis(axis, axisCount);
229
}
230
231
prevState[pad] = state;
232
check_delay[pad] = 0;
233
}
234
235
void XinputDevice::ApplyButtons(int pad, const XINPUT_STATE &state) {
236
const u32 buttons = state.Gamepad.wButtons;
237
238
const u32 downMask = buttons & (~prevButtons_[pad]);
239
const u32 upMask = (~buttons) & prevButtons_[pad];
240
prevButtons_[pad] = buttons;
241
242
for (int i = 0; i < ARRAY_SIZE(xinput_ctrl_map); i++) {
243
if (downMask & xinput_ctrl_map[i].from) {
244
KeyInput key;
245
key.deviceId = DEVICE_ID_XINPUT_0 + pad;
246
key.flags = KEY_DOWN;
247
key.keyCode = xinput_ctrl_map[i].to;
248
NativeKey(key);
249
}
250
if (upMask & xinput_ctrl_map[i].from) {
251
KeyInput key;
252
key.deviceId = DEVICE_ID_XINPUT_0 + pad;
253
key.flags = KEY_UP;
254
key.keyCode = xinput_ctrl_map[i].to;
255
NativeKey(key);
256
}
257
}
258
}
259
260
void XinputDevice::ApplyVibration(int pad, XINPUT_VIBRATION &vibration) {
261
if (PSP_IsInited()) {
262
newVibrationTime_ = time_now_d();
263
// We have to run PPSSPP_XInputSetState at time intervals
264
// since it bugs otherwise with very high fast-forward speeds
265
// and freezes at constant vibration or no vibration at all.
266
if (newVibrationTime_ - prevVibrationTime >= 1.0 / 64.0) {
267
if (GetUIState() == UISTATE_INGAME) {
268
vibration.wLeftMotorSpeed = sceCtrlGetLeftVibration(); // use any value between 0-65535 here
269
vibration.wRightMotorSpeed = sceCtrlGetRightVibration(); // use any value between 0-65535 here
270
} else {
271
vibration.wLeftMotorSpeed = 0;
272
vibration.wRightMotorSpeed = 0;
273
}
274
275
if (prevVibration[pad].wLeftMotorSpeed != vibration.wLeftMotorSpeed || prevVibration[pad].wRightMotorSpeed != vibration.wRightMotorSpeed) {
276
PPSSPP_XInputSetState(pad, &vibration);
277
prevVibration[pad] = vibration;
278
}
279
prevVibrationTime = newVibrationTime_;
280
}
281
} else {
282
DWORD dwResult = PPSSPP_XInputSetState(pad, &vibration);
283
if (dwResult != ERROR_SUCCESS) {
284
check_delay[pad] = 30;
285
}
286
}
287
}
288
289