Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Windows/XinputDevice.cpp
5670 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(padData_); ++i) {
155
padData_[i].checkDelayUpdates = (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 (padData_[i].checkDelayUpdates-- > 0)
173
continue;
174
DWORD dwResult = PPSSPP_XInputGetState(i, &state);
175
if (dwResult == ERROR_SUCCESS) {
176
if (!padData_[i].connected) {
177
padData_[i].connected = true;
178
padData_[i].vendorId = 0;
179
padData_[i].productId = 0;
180
#if !PPSSPP_PLATFORM(UWP)
181
XINPUT_CAPABILITIES_EX caps{};
182
if (PPSSPP_XInputGetCapabilitiesEx != nullptr && PPSSPP_XInputGetCapabilitiesEx(1, i, 0, &caps) == ERROR_SUCCESS) {
183
padData_[i].vendorId = caps.VendorId;
184
padData_[i].productId = caps.ProductId;
185
}
186
#endif
187
KeyMap::NotifyPadConnected(DEVICE_ID_XINPUT_0 + i, StringFromFormat("Xbox pad %d: %d/%d", (i + 1), padData_[i].vendorId, padData_[i].productId));
188
}
189
XINPUT_VIBRATION vibration{};
190
UpdatePad(i, state, vibration);
191
anySuccess = true;
192
} else if (dwResult == ERROR_DEVICE_NOT_CONNECTED) {
193
if (padData_[i].connected) {
194
ReleaseAllKeys(i);
195
KeyMap::NotifyPadDisconnected(DEVICE_ID_XINPUT_0 + i);
196
padData_[i].connected = false;
197
}
198
padData_[i].checkDelayUpdates = 30;
199
} else {
200
padData_[i].checkDelayUpdates = 30;
201
}
202
}
203
204
// If we get XInput, skip the others. This might not actually be a good idea,
205
// and was done to avoid conflicts between DirectInput and XInput.
206
return 0; // anySuccess ? UPDATESTATE_SKIP_PAD : 0;
207
}
208
209
void XinputDevice::ReleaseAllKeys(int pad) {
210
for (int i = 0; i < ARRAY_SIZE(xinput_ctrl_map); i++) {
211
const auto &mapping = xinput_ctrl_map[i];
212
KeyInput key;
213
key.deviceId = (InputDeviceID)(DEVICE_ID_XINPUT_0 + pad);
214
key.flags = KeyInputFlags::UP;
215
key.keyCode = mapping.to;
216
NativeKey(key);
217
}
218
static const InputAxis allAxes[6] = {
219
JOYSTICK_AXIS_X,
220
JOYSTICK_AXIS_Y,
221
JOYSTICK_AXIS_Z,
222
JOYSTICK_AXIS_RZ,
223
JOYSTICK_AXIS_LTRIGGER,
224
JOYSTICK_AXIS_RTRIGGER,
225
};
226
for (const auto axisId : allAxes) {
227
AxisInput axis;
228
axis.deviceId = (InputDeviceID)(DEVICE_ID_XINPUT_0 + pad);
229
axis.axisId = axisId;
230
axis.value = 0;
231
NativeAxis(&axis, 1);
232
}
233
}
234
235
void XinputDevice::UpdatePad(int pad, const XINPUT_STATE &state, XINPUT_VIBRATION &vibration) {
236
ApplyButtons(pad, state);
237
ApplyVibration(pad, vibration);
238
239
AxisInput axis[6];
240
int axisCount = 0;
241
for (int i = 0; i < ARRAY_SIZE(axis); i++) {
242
axis[i].deviceId = (InputDeviceID)(DEVICE_ID_XINPUT_0 + pad);
243
}
244
auto sendAxis = [&](InputAxis axisId, float value, int axisIndex) {
245
if (value != padData_[pad].prevAxisValue[axisIndex]) {
246
padData_[pad].prevAxisValue[axisIndex] = value;
247
axis[axisCount].axisId = axisId;
248
axis[axisCount].value = value;
249
axisCount++;
250
}
251
};
252
253
sendAxis(JOYSTICK_AXIS_X, (float)state.Gamepad.sThumbLX / 32767.0f, 0);
254
sendAxis(JOYSTICK_AXIS_Y, (float)state.Gamepad.sThumbLY / 32767.0f, 1);
255
sendAxis(JOYSTICK_AXIS_Z, (float)state.Gamepad.sThumbRX / 32767.0f, 2);
256
sendAxis(JOYSTICK_AXIS_RZ, (float)state.Gamepad.sThumbRY / 32767.0f, 3);
257
sendAxis(JOYSTICK_AXIS_LTRIGGER, (float)state.Gamepad.bLeftTrigger / 255.0f, 4);
258
sendAxis(JOYSTICK_AXIS_RTRIGGER, (float)state.Gamepad.bRightTrigger / 255.0f, 5);
259
260
if (axisCount) {
261
NativeAxis(axis, axisCount);
262
}
263
264
padData_[pad].prevState = state;
265
padData_[pad].checkDelayUpdates = 0;
266
}
267
268
void XinputDevice::ApplyButtons(int pad, const XINPUT_STATE &state) {
269
const u32 buttons = state.Gamepad.wButtons;
270
271
const u32 downMask = buttons & (~padData_[pad].prevButtons);
272
const u32 upMask = (~buttons) & padData_[pad].prevButtons;
273
padData_[pad].prevButtons = buttons;
274
275
for (int i = 0; i < ARRAY_SIZE(xinput_ctrl_map); i++) {
276
if (downMask & xinput_ctrl_map[i].from) {
277
KeyInput key;
278
key.deviceId = DEVICE_ID_XINPUT_0 + pad;
279
key.flags = KeyInputFlags::DOWN;
280
key.keyCode = xinput_ctrl_map[i].to;
281
NativeKey(key);
282
}
283
if (upMask & xinput_ctrl_map[i].from) {
284
KeyInput key;
285
key.deviceId = DEVICE_ID_XINPUT_0 + pad;
286
key.flags = KeyInputFlags::UP;
287
key.keyCode = xinput_ctrl_map[i].to;
288
NativeKey(key);
289
}
290
}
291
}
292
293
void XinputDevice::ApplyVibration(int pad, XINPUT_VIBRATION &vibration) {
294
if (PSP_IsInited()) {
295
newVibrationTime_ = time_now_d();
296
// We have to run PPSSPP_XInputSetState at time intervals
297
// since it bugs otherwise with very high fast-forward speeds
298
// and freezes at constant vibration or no vibration at all.
299
if (newVibrationTime_ - prevVibrationTime_ >= 1.0 / 64.0) {
300
if (GetUIState() == UISTATE_INGAME) {
301
vibration.wLeftMotorSpeed = sceCtrlGetLeftVibration(); // use any value between 0-65535 here
302
vibration.wRightMotorSpeed = sceCtrlGetRightVibration(); // use any value between 0-65535 here
303
} else {
304
vibration.wLeftMotorSpeed = 0;
305
vibration.wRightMotorSpeed = 0;
306
}
307
308
if (padData_[pad].prevVibration.wLeftMotorSpeed != vibration.wLeftMotorSpeed || padData_[pad].prevVibration.wRightMotorSpeed != vibration.wRightMotorSpeed) {
309
PPSSPP_XInputSetState(pad, &vibration);
310
padData_[pad].prevVibration = vibration;
311
}
312
prevVibrationTime_ = newVibrationTime_;
313
}
314
} else {
315
DWORD dwResult = PPSSPP_XInputSetState(pad, &vibration);
316
if (dwResult != ERROR_SUCCESS) {
317
padData_[pad].checkDelayUpdates = 30;
318
}
319
}
320
}
321
322