-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDinputDevice.cpp
More file actions
375 lines (320 loc) · 10.3 KB
/
DinputDevice.cpp
File metadata and controls
375 lines (320 loc) · 10.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
// Copyright (c) 2012- PPSSPP Project.
// This program 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, version 2.0 or later versions.
// This program 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 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <limits.h>
#include <algorithm>
#include "Core/HLE/sceCtrl.h"
#include "DinputDevice.h"
#include "ControlMapping.h"
#include "Core/Config.h"
#include "input/input_state.h"
#include "base/NativeApp.h"
#include "input/keycodes.h"
#include "Core/Reporting.h"
#include "Xinput.h"
#pragma comment(lib,"dinput8.lib")
#ifdef min
#undef min
#undef max
#endif
//initialize static members of DinputDevice
unsigned int DinputDevice::pInstances = 0;
LPDIRECTINPUT8 DinputDevice::pDI = NULL;
std::vector<DIDEVICEINSTANCE> DinputDevice::devices;
// In order from 0. There can be 128, but most controllers do not have that many.
static const int dinput_buttons[] = {
NKCODE_BUTTON_1,
NKCODE_BUTTON_2,
NKCODE_BUTTON_3,
NKCODE_BUTTON_4,
NKCODE_BUTTON_5,
NKCODE_BUTTON_6,
NKCODE_BUTTON_7,
NKCODE_BUTTON_8,
NKCODE_BUTTON_9,
NKCODE_BUTTON_10,
NKCODE_BUTTON_11,
NKCODE_BUTTON_12,
NKCODE_BUTTON_13,
NKCODE_BUTTON_14,
NKCODE_BUTTON_15,
NKCODE_BUTTON_16,
};
static float NormalizedDeadzoneFilter(short value);
#define DIFF (JOY_POVRIGHT - JOY_POVFORWARD) / 2
#define JOY_POVFORWARD_RIGHT JOY_POVFORWARD + DIFF
#define JOY_POVRIGHT_BACKWARD JOY_POVRIGHT + DIFF
#define JOY_POVBACKWARD_LEFT JOY_POVBACKWARD + DIFF
#define JOY_POVLEFT_FORWARD JOY_POVLEFT + DIFF
struct XINPUT_DEVICE_NODE {
DWORD dwVidPid;
XINPUT_DEVICE_NODE* pNext;
};
XINPUT_DEVICE_NODE* g_pXInputDeviceList = NULL;
bool IsXInputDevice( const GUID* pGuidProductFromDirectInput ) {
XINPUT_DEVICE_NODE* pNode = g_pXInputDeviceList;
while( pNode )
{
if( pNode->dwVidPid == pGuidProductFromDirectInput->Data1 )
return true;
pNode = pNode->pNext;
}
return false;
}
LPDIRECTINPUT8 DinputDevice::getPDI()
{
if (pDI == NULL)
{
if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&pDI, NULL)))
{
pDI = NULL;
}
}
return pDI;
}
BOOL CALLBACK DinputDevice::DevicesCallback(
LPCDIDEVICEINSTANCE lpddi,
LPVOID pvRef
)
{
//check if a device with the same Instance guid is already saved
auto res = std::find_if(devices.begin(), devices.end(),
[lpddi](const DIDEVICEINSTANCE &to_consider){
return lpddi->guidInstance == to_consider.guidInstance;
});
if (res == devices.end()) //not yet in the devices list
{
// Ignore if device supports XInput
if (!IsXInputDevice(&lpddi->guidProduct)) {
devices.push_back(*lpddi);
}
}
return DIENUM_CONTINUE;
}
void DinputDevice::getDevices()
{
if (devices.empty())
{
getPDI()->EnumDevices(DI8DEVCLASS_GAMECTRL, &DinputDevice::DevicesCallback, NULL, DIEDFL_ATTACHEDONLY);
}
}
DinputDevice::DinputDevice(int devnum) {
pInstances++;
pDevNum = devnum;
pJoystick = NULL;
memset(lastButtons_, 0, sizeof(lastButtons_));
memset(lastPOV_, 0, sizeof(lastPOV_));
last_lX_ = 0;
last_lY_ = 0;
last_lZ_ = 0;
last_lRx_ = 0;
last_lRy_ = 0;
last_lRz_ = 0;
if (getPDI() == NULL)
{
return;
}
if (devnum >= MAX_NUM_PADS)
{
return;
}
getDevices();
if ( (devnum >= (int)devices.size()) || FAILED(getPDI()->CreateDevice(devices.at(devnum).guidInstance, &pJoystick, NULL)))
{
return;
}
if (FAILED(pJoystick->SetDataFormat(&c_dfDIJoystick2))) {
pJoystick->Release();
pJoystick = NULL;
return;
}
DIPROPRANGE diprg;
diprg.diph.dwSize = sizeof(DIPROPRANGE);
diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
diprg.diph.dwHow = DIPH_DEVICE;
diprg.diph.dwObj = 0;
diprg.lMin = -10000;
diprg.lMax = 10000;
analog = FAILED(pJoystick->SetProperty(DIPROP_RANGE, &diprg.diph)) ? false : true;
// Other devices suffer if the deadzone is not set.
// TODO: The dead zone will be made configurable in the Control dialog.
DIPROPDWORD dipw;
dipw.diph.dwSize = sizeof(DIPROPDWORD);
dipw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipw.diph.dwHow = DIPH_DEVICE;
dipw.diph.dwObj = 0;
// dwData 10000 is deadzone(0% - 100%), multiply by config scalar
dipw.dwData = (int)(g_Config.fDInputAnalogDeadzone * 10000);
analog |= FAILED(pJoystick->SetProperty(DIPROP_DEADZONE, &dipw.diph)) ? false : true;
}
DinputDevice::~DinputDevice() {
if (pJoystick) {
pJoystick->Release();
pJoystick = NULL;
}
pInstances--;
//the whole instance counter is obviously highly thread-unsafe
//but I don't think creation and destruction operations will be
//happening at the same time and other values like pDI are
//unsafe as well anyway
if (pInstances == 0 && pDI) {
pDI->Release();
pDI = NULL;
}
}
void SendNativeAxis(int deviceId, short value, short &lastValue, int axisId) {
if (value == lastValue)
return;
AxisInput axis;
axis.deviceId = deviceId;
axis.axisId = axisId;
axis.value = (float)value / 10000.0f; // Convert axis to normalised float
NativeAxis(axis);
lastValue = value;
}
inline float Signs(short val) {
return (0 < val) - (val < 0);
}
inline float LinearMaps(short val, short a0, short a1, short b0, short b1) {
return b0 + (((val - a0) * (b1 - b0)) / (a1 - a0));
}
int DinputDevice::UpdateState(InputState &input_state) {
if (!pJoystick) return -1;
DIJOYSTATE2 js;
if (FAILED(pJoystick->Poll())) {
if(pJoystick->Acquire() == DIERR_INPUTLOST)
return -1;
}
if(FAILED(pJoystick->GetDeviceState(sizeof(DIJOYSTATE2), &js)))
return -1;
ApplyButtons(js, input_state);
if (analog) {
AxisInput axis;
axis.deviceId = DEVICE_ID_PAD_0 + pDevNum;
// Circle to Square mapping, cribbed from XInputDevice
float sx = js.lX;
float sy = js.lY;
float scaleFactor = sqrtf((sx * sx + sy * sy) / std::max(sx * sx, sy * sy));
js.lX = (short)(sx * scaleFactor);
js.lY = (short)(sy * scaleFactor);
// Linear range mapping (used to invert deadzones)
float dz = g_Config.fDInputAnalogDeadzone;
int idzm = g_Config.iDInputAnalogInverseMode;
float idz = g_Config.fDInputAnalogInverseDeadzone;
float md = std::max(dz, idz);
float st = g_Config.fDInputAnalogSensitivity;
float magnitude = sqrtf(js.lX * js.lX + js.lY * js.lY);
if (magnitude > dz * 10000.0f) {
if (idzm == 1)
{
short xSign = Signs(js.lX);
if (xSign != 0.0f) {
js.lX = LinearMaps(js.lX, xSign * (short)(dz * 10000), xSign * 10000, xSign * (short)(md * 10000), xSign * 10000 * st);
}
}
else if (idzm == 2)
{
short ySign = Signs(js.lY);
if (ySign != 0.0f) {
js.lY = LinearMaps(js.lY, ySign * (short)(dz * 10000.0f), ySign * 10000, ySign * (short)(md * 10000.0f), ySign * 10000 * st);
}
}
else if (idzm == 3)
{
float xNorm = (float)js.lX / magnitude;
float yNorm = (float)js.lY / magnitude;
float mapMag = LinearMaps(magnitude, dz * 10000.0f, 10000.0f, md * 10000.0f, 10000.0f * st);
js.lX = (short)(xNorm * mapMag);
js.lY = (short)(yNorm * mapMag);
}
}
else
{
js.lX = 0;
js.lY = 0;
}
js.lX = (short)std::min(10000.0f, std::max((float)js.lX, -10000.0f));
js.lY = (short)std::min(10000.0f, std::max((float)js.lY, -10000.0f));
SendNativeAxis(DEVICE_ID_PAD_0 + pDevNum, js.lX, last_lX_, JOYSTICK_AXIS_X);
SendNativeAxis(DEVICE_ID_PAD_0 + pDevNum, js.lY, last_lY_, JOYSTICK_AXIS_Y);
SendNativeAxis(DEVICE_ID_PAD_0 + pDevNum, js.lZ, last_lZ_, JOYSTICK_AXIS_Z);
SendNativeAxis(DEVICE_ID_PAD_0 + pDevNum, js.lRx, last_lRx_, JOYSTICK_AXIS_RX);
SendNativeAxis(DEVICE_ID_PAD_0 + pDevNum, js.lRy, last_lRy_, JOYSTICK_AXIS_RY);
SendNativeAxis(DEVICE_ID_PAD_0 + pDevNum, js.lRz, last_lRz_, JOYSTICK_AXIS_RZ);
}
//check if the values have changed from last time and skip polling the rest of the dinput devices if they did
//this doesn't seem to quite work if only the axis have changed
if ((memcmp(js.rgbButtons, pPrevState.rgbButtons, sizeof(BYTE) * 128) != 0)
|| (memcmp(js.rgdwPOV, pPrevState.rgdwPOV, sizeof(DWORD) * 4) != 0)
|| js.lVX != 0 || js.lVY != 0 || js.lVZ != 0 || js.lVRx != 0 || js.lVRy != 0 || js.lVRz != 0)
{
pPrevState = js;
return UPDATESTATE_SKIP_PAD;
}
return -1;
}
void DinputDevice::ApplyButtons(DIJOYSTATE2 &state, InputState &input_state) {
BYTE *buttons = state.rgbButtons;
u32 downMask = 0x80;
for (int i = 0; i < ARRAY_SIZE(dinput_buttons); ++i) {
if (state.rgbButtons[i] == lastButtons_[i]) {
continue;
}
bool down = (state.rgbButtons[i] & downMask) == downMask;
KeyInput key;
key.deviceId = DEVICE_ID_PAD_0 + pDevNum;
key.flags = down ? KEY_DOWN : KEY_UP;
key.keyCode = dinput_buttons[i];
NativeKey(key);
lastButtons_[i] = state.rgbButtons[i];
}
// Now the POV hat, which can technically go in any degree but usually does not.
if (LOWORD(state.rgdwPOV[0]) != lastPOV_[0]) {
KeyInput dpad[4];
for (int i = 0; i < 4; ++i) {
dpad[i].deviceId = DEVICE_ID_PAD_0 + pDevNum;
dpad[i].flags = KEY_UP;
}
dpad[0].keyCode = NKCODE_DPAD_UP;
dpad[1].keyCode = NKCODE_DPAD_LEFT;
dpad[2].keyCode = NKCODE_DPAD_DOWN;
dpad[3].keyCode = NKCODE_DPAD_RIGHT;
if (LOWORD(state.rgdwPOV[0]) != JOY_POVCENTERED) {
// These are the edges, so we use or.
if (state.rgdwPOV[0] >= JOY_POVLEFT_FORWARD || state.rgdwPOV[0] <= JOY_POVFORWARD_RIGHT) {
dpad[0].flags = KEY_DOWN;
}
if (state.rgdwPOV[0] >= JOY_POVBACKWARD_LEFT && state.rgdwPOV[0] <= JOY_POVLEFT_FORWARD) {
dpad[1].flags = KEY_DOWN;
}
if (state.rgdwPOV[0] >= JOY_POVRIGHT_BACKWARD && state.rgdwPOV[0] <= JOY_POVBACKWARD_LEFT) {
dpad[2].flags = KEY_DOWN;
}
if (state.rgdwPOV[0] >= JOY_POVFORWARD_RIGHT && state.rgdwPOV[0] <= JOY_POVRIGHT_BACKWARD) {
dpad[3].flags = KEY_DOWN;
}
}
NativeKey(dpad[0]);
NativeKey(dpad[1]);
NativeKey(dpad[2]);
NativeKey(dpad[3]);
lastPOV_[0] = LOWORD(state.rgdwPOV[0]);
}
}
size_t DinputDevice::getNumPads()
{
if (devices.empty())
{
getDevices();
}
return devices.size();
}