CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Windows/DinputDevice.cpp
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "stdafx.h"
19
#include <limits.h>
20
#include <algorithm>
21
#include <mmsystem.h>
22
#include <XInput.h>
23
24
#include "Common/Input/InputState.h"
25
#include "Common/Input/KeyCodes.h"
26
#include "Common/LogReporting.h"
27
#include "Common/StringUtils.h"
28
#include "Common/System/NativeApp.h"
29
#include "Core/Config.h"
30
#include "Core/HLE/sceCtrl.h"
31
#include "Core/KeyMap.h"
32
#include "Windows/DinputDevice.h"
33
#pragma comment(lib,"dinput8.lib")
34
35
//initialize static members of DinputDevice
36
unsigned int DinputDevice::pInstances = 0;
37
LPDIRECTINPUT8 DinputDevice::pDI = NULL;
38
std::vector<DIDEVICEINSTANCE> DinputDevice::devices;
39
bool DinputDevice::needsCheck_ = true;
40
41
// In order from 0. There can be 128, but most controllers do not have that many.
42
static const InputKeyCode dinput_buttons[] = {
43
NKCODE_BUTTON_1,
44
NKCODE_BUTTON_2,
45
NKCODE_BUTTON_3,
46
NKCODE_BUTTON_4,
47
NKCODE_BUTTON_5,
48
NKCODE_BUTTON_6,
49
NKCODE_BUTTON_7,
50
NKCODE_BUTTON_8,
51
NKCODE_BUTTON_9,
52
NKCODE_BUTTON_10,
53
NKCODE_BUTTON_11,
54
NKCODE_BUTTON_12,
55
NKCODE_BUTTON_13,
56
NKCODE_BUTTON_14,
57
NKCODE_BUTTON_15,
58
NKCODE_BUTTON_16,
59
};
60
61
#define DIFF (JOY_POVRIGHT - JOY_POVFORWARD) / 2
62
#define JOY_POVFORWARD_RIGHT JOY_POVFORWARD + DIFF
63
#define JOY_POVRIGHT_BACKWARD JOY_POVRIGHT + DIFF
64
#define JOY_POVBACKWARD_LEFT JOY_POVBACKWARD + DIFF
65
#define JOY_POVLEFT_FORWARD JOY_POVLEFT + DIFF
66
67
struct XINPUT_DEVICE_NODE {
68
DWORD dwVidPid;
69
XINPUT_DEVICE_NODE* pNext;
70
};
71
XINPUT_DEVICE_NODE* g_pXInputDeviceList = NULL;
72
73
bool IsXInputDevice( const GUID* pGuidProductFromDirectInput ) {
74
XINPUT_DEVICE_NODE* pNode = g_pXInputDeviceList;
75
while( pNode )
76
{
77
if( pNode->dwVidPid == pGuidProductFromDirectInput->Data1 )
78
return true;
79
pNode = pNode->pNext;
80
}
81
82
return false;
83
}
84
85
LPDIRECTINPUT8 DinputDevice::getPDI()
86
{
87
if (pDI == NULL)
88
{
89
if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&pDI, NULL)))
90
{
91
pDI = NULL;
92
}
93
}
94
return pDI;
95
}
96
97
BOOL CALLBACK DinputDevice::DevicesCallback(
98
LPCDIDEVICEINSTANCE lpddi,
99
LPVOID pvRef
100
)
101
{
102
//check if a device with the same Instance guid is already saved
103
auto res = std::find_if(devices.begin(), devices.end(),
104
[lpddi](const DIDEVICEINSTANCE &to_consider){
105
return lpddi->guidInstance == to_consider.guidInstance;
106
});
107
if (res == devices.end()) //not yet in the devices list
108
{
109
// Ignore if device supports XInput
110
if (!IsXInputDevice(&lpddi->guidProduct)) {
111
devices.push_back(*lpddi);
112
}
113
}
114
return DIENUM_CONTINUE;
115
}
116
117
void DinputDevice::getDevices(bool refresh)
118
{
119
if (refresh)
120
{
121
getPDI()->EnumDevices(DI8DEVCLASS_GAMECTRL, &DinputDevice::DevicesCallback, NULL, DIEDFL_ATTACHEDONLY);
122
}
123
}
124
125
DinputDevice::DinputDevice(int devnum) {
126
pInstances++;
127
pDevNum = devnum;
128
pJoystick = NULL;
129
memset(lastButtons_, 0, sizeof(lastButtons_));
130
memset(lastPOV_, 0, sizeof(lastPOV_));
131
last_lX_ = 0;
132
last_lY_ = 0;
133
last_lZ_ = 0;
134
last_lRx_ = 0;
135
last_lRy_ = 0;
136
last_lRz_ = 0;
137
138
if (getPDI() == NULL)
139
{
140
return;
141
}
142
143
if (devnum >= MAX_NUM_PADS)
144
{
145
return;
146
}
147
148
getDevices(needsCheck_);
149
if ( (devnum >= (int)devices.size()) || FAILED(getPDI()->CreateDevice(devices.at(devnum).guidInstance, &pJoystick, NULL)))
150
{
151
return;
152
}
153
154
wchar_t guid[64];
155
if (StringFromGUID2(devices.at(devnum).guidProduct, guid, ARRAY_SIZE(guid)) != 0) {
156
KeyMap::NotifyPadConnected(DEVICE_ID_PAD_0 + pDevNum, StringFromFormat("%S: %S", devices.at(devnum).tszProductName, guid));
157
}
158
159
if (FAILED(pJoystick->SetDataFormat(&c_dfDIJoystick2))) {
160
pJoystick->Release();
161
pJoystick = NULL;
162
return;
163
}
164
165
DIPROPRANGE diprg;
166
diprg.diph.dwSize = sizeof(DIPROPRANGE);
167
diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
168
diprg.diph.dwHow = DIPH_DEVICE;
169
diprg.diph.dwObj = 0;
170
diprg.lMin = -10000;
171
diprg.lMax = 10000;
172
173
analog = FAILED(pJoystick->SetProperty(DIPROP_RANGE, &diprg.diph)) ? false : true;
174
175
// Other devices suffer if the deadzone is not set.
176
// TODO: The dead zone will be made configurable in the Control dialog.
177
DIPROPDWORD dipw;
178
dipw.diph.dwSize = sizeof(DIPROPDWORD);
179
dipw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
180
dipw.diph.dwHow = DIPH_DEVICE;
181
dipw.diph.dwObj = 0;
182
// dwData 10000 is deadzone(0% - 100%), multiply by config scalar
183
dipw.dwData = 0;
184
185
analog |= FAILED(pJoystick->SetProperty(DIPROP_DEADZONE, &dipw.diph)) ? false : true;
186
}
187
188
DinputDevice::~DinputDevice() {
189
if (pJoystick) {
190
pJoystick->Release();
191
pJoystick = NULL;
192
}
193
194
pInstances--;
195
196
//the whole instance counter is obviously highly thread-unsafe
197
//but I don't think creation and destruction operations will be
198
//happening at the same time and other values like pDI are
199
//unsafe as well anyway
200
if (pInstances == 0 && pDI) {
201
pDI->Release();
202
pDI = NULL;
203
}
204
}
205
206
void SendNativeAxis(InputDeviceID deviceId, int value, int &lastValue, InputAxis axisId) {
207
if (value != lastValue) {
208
AxisInput axis;
209
axis.deviceId = deviceId;
210
axis.axisId = axisId;
211
axis.value = (float)value * (1.0f / 10000.0f); // Convert axis to normalised float
212
NativeAxis(&axis, 1);
213
}
214
lastValue = value;
215
}
216
217
static LONG *ValueForAxisId(DIJOYSTATE2 &js, int axisId) {
218
switch (axisId) {
219
case JOYSTICK_AXIS_X: return &js.lX;
220
case JOYSTICK_AXIS_Y: return &js.lY;
221
case JOYSTICK_AXIS_Z: return &js.lZ;
222
case JOYSTICK_AXIS_RX: return &js.lRx;
223
case JOYSTICK_AXIS_RY: return &js.lRy;
224
case JOYSTICK_AXIS_RZ: return &js.lRz;
225
default: return nullptr;
226
}
227
}
228
229
int DinputDevice::UpdateState() {
230
if (!pJoystick) return -1;
231
232
DIJOYSTATE2 js;
233
234
if (FAILED(pJoystick->Poll())) {
235
if(pJoystick->Acquire() == DIERR_INPUTLOST)
236
return -1;
237
}
238
239
if(FAILED(pJoystick->GetDeviceState(sizeof(DIJOYSTATE2), &js)))
240
return -1;
241
242
ApplyButtons(js);
243
244
if (analog) {
245
// TODO: Use the batched interface.
246
AxisInput axis;
247
axis.deviceId = DEVICE_ID_PAD_0 + pDevNum;
248
249
SendNativeAxis(DEVICE_ID_PAD_0 + pDevNum, js.lX, last_lX_, JOYSTICK_AXIS_X);
250
SendNativeAxis(DEVICE_ID_PAD_0 + pDevNum, js.lY, last_lY_, JOYSTICK_AXIS_Y);
251
SendNativeAxis(DEVICE_ID_PAD_0 + pDevNum, js.lZ, last_lZ_, JOYSTICK_AXIS_Z);
252
SendNativeAxis(DEVICE_ID_PAD_0 + pDevNum, js.lRx, last_lRx_, JOYSTICK_AXIS_RX);
253
SendNativeAxis(DEVICE_ID_PAD_0 + pDevNum, js.lRy, last_lRy_, JOYSTICK_AXIS_RY);
254
SendNativeAxis(DEVICE_ID_PAD_0 + pDevNum, js.lRz, last_lRz_, JOYSTICK_AXIS_RZ);
255
}
256
257
//check if the values have changed from last time and skip polling the rest of the dinput devices if they did
258
//this doesn't seem to quite work if only the axis have changed
259
if ((memcmp(js.rgbButtons, pPrevState.rgbButtons, sizeof(BYTE) * 128) != 0)
260
|| (memcmp(js.rgdwPOV, pPrevState.rgdwPOV, sizeof(DWORD) * 4) != 0)
261
|| js.lVX != 0 || js.lVY != 0 || js.lVZ != 0 || js.lVRx != 0 || js.lVRy != 0 || js.lVRz != 0)
262
{
263
pPrevState = js;
264
return UPDATESTATE_SKIP_PAD;
265
}
266
return -1;
267
}
268
269
void DinputDevice::ApplyButtons(DIJOYSTATE2 &state) {
270
BYTE *buttons = state.rgbButtons;
271
u32 downMask = 0x80;
272
273
for (int i = 0; i < ARRAY_SIZE(dinput_buttons); ++i) {
274
if (state.rgbButtons[i] == lastButtons_[i]) {
275
continue;
276
}
277
278
bool down = (state.rgbButtons[i] & downMask) == downMask;
279
KeyInput key;
280
key.deviceId = DEVICE_ID_PAD_0 + pDevNum;
281
key.flags = down ? KEY_DOWN : KEY_UP;
282
key.keyCode = dinput_buttons[i];
283
NativeKey(key);
284
285
lastButtons_[i] = state.rgbButtons[i];
286
}
287
288
// Now the POV hat, which can technically go in any degree but usually does not.
289
if (LOWORD(state.rgdwPOV[0]) != lastPOV_[0]) {
290
KeyInput dpad[4]{};
291
for (int i = 0; i < 4; ++i) {
292
dpad[i].deviceId = DEVICE_ID_PAD_0 + pDevNum;
293
dpad[i].flags = KEY_UP;
294
}
295
dpad[0].keyCode = NKCODE_DPAD_UP;
296
dpad[1].keyCode = NKCODE_DPAD_LEFT;
297
dpad[2].keyCode = NKCODE_DPAD_DOWN;
298
dpad[3].keyCode = NKCODE_DPAD_RIGHT;
299
300
if (LOWORD(state.rgdwPOV[0]) != JOY_POVCENTERED) {
301
// These are the edges, so we use or.
302
if (state.rgdwPOV[0] >= JOY_POVLEFT_FORWARD || state.rgdwPOV[0] <= JOY_POVFORWARD_RIGHT) {
303
dpad[0].flags = KEY_DOWN;
304
}
305
if (state.rgdwPOV[0] >= JOY_POVBACKWARD_LEFT && state.rgdwPOV[0] <= JOY_POVLEFT_FORWARD) {
306
dpad[1].flags = KEY_DOWN;
307
}
308
if (state.rgdwPOV[0] >= JOY_POVRIGHT_BACKWARD && state.rgdwPOV[0] <= JOY_POVBACKWARD_LEFT) {
309
dpad[2].flags = KEY_DOWN;
310
}
311
if (state.rgdwPOV[0] >= JOY_POVFORWARD_RIGHT && state.rgdwPOV[0] <= JOY_POVRIGHT_BACKWARD) {
312
dpad[3].flags = KEY_DOWN;
313
}
314
}
315
316
NativeKey(dpad[0]);
317
NativeKey(dpad[1]);
318
NativeKey(dpad[2]);
319
NativeKey(dpad[3]);
320
321
lastPOV_[0] = LOWORD(state.rgdwPOV[0]);
322
}
323
}
324
325
size_t DinputDevice::getNumPads()
326
{
327
getDevices(needsCheck_);
328
needsCheck_ = false;
329
return devices.size();
330
}
331
332