forked from hrydgard/ppsspp
/
XinputDevice.cpp
126 lines (108 loc) · 3.66 KB
/
XinputDevice.cpp
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
#include <limits.h>
#include "XinputDevice.h"
#include "input/input_state.h"
#ifndef XUSER_MAX_COUNT
#define XUSER_MAX_COUNT 4
#endif
XinputDevice::XinputDevice() {
ZeroMemory( &this->prevState, sizeof(this->prevState) );
this->check_delay = 0;
this->gamepad_idx = -1;
}
struct Stick {
float x;
float y;
};
static Stick NormalizedDeadzoneFilter(short x, short y);
int XinputDevice::UpdateState(InputState &input_state) {
if (this->check_delay-- > 0) return -1;
XINPUT_STATE state;
ZeroMemory( &state, sizeof(XINPUT_STATE) );
DWORD dwResult;
if (this->gamepad_idx >= 0)
dwResult = XInputGetState( this->gamepad_idx, &state );
else {
// use the first gamepad that responds
for (int i = 0; i < XUSER_MAX_COUNT; i++) {
dwResult = XInputGetState( i, &state );
if (dwResult == ERROR_SUCCESS) {
this->gamepad_idx = i;
break;
}
}
}
if ( dwResult == ERROR_SUCCESS ) {
ApplyDiff(state, input_state);
Stick left = NormalizedDeadzoneFilter(state.Gamepad.sThumbLX, state.Gamepad.sThumbLY);
Stick right = NormalizedDeadzoneFilter(state.Gamepad.sThumbRX, state.Gamepad.sThumbRY);
input_state.pad_lstick_x += left.x;
input_state.pad_lstick_y += left.y;
input_state.pad_rstick_x += right.x;
input_state.pad_rstick_y += right.y;
// Also convert the analog triggers.
input_state.pad_ltrigger = state.Gamepad.bLeftTrigger / 255.0f;
input_state.pad_rtrigger = state.Gamepad.bRightTrigger / 255.0f;
this->prevState = state;
this->check_delay = 0;
// If there's an XInput pad, skip following pads. This prevents DInput and XInput
// from colliding.
return UPDATESTATE_SKIP_PAD;
} else {
// wait check_delay frames before polling the controller again
this->gamepad_idx = -1;
this->check_delay = 100;
return -1;
}
}
// We only filter the left stick since PSP has no analog triggers or right stick
static Stick NormalizedDeadzoneFilter(short x, short y) {
static const short DEADZONE = XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
Stick left;
left.x = x;
left.y = y;
float magnitude = sqrt(left.x*left.x + left.y*left.y);
Stick norm;
norm.x = left.x / magnitude;
norm.y = left.y / magnitude;
if (magnitude > DEADZONE) {
if (magnitude > 32767) magnitude = 32767;
// normalize the magnitude
magnitude -= DEADZONE;
magnitude /= (32767 - DEADZONE);
// normalize the axis
left.x = norm.x * magnitude;
left.y = norm.y * magnitude;
} else {
left.x = left.y = 0;
}
return left;
}
// Yes, this maps more than the PSP has, but that's fine as this lets us
// map buttons to extra functionality like speedup.
static const unsigned int xinput_ctrl_map[] = {
XINPUT_GAMEPAD_DPAD_UP, PAD_BUTTON_UP,
XINPUT_GAMEPAD_DPAD_DOWN, PAD_BUTTON_DOWN,
XINPUT_GAMEPAD_DPAD_LEFT, PAD_BUTTON_LEFT,
XINPUT_GAMEPAD_DPAD_RIGHT, PAD_BUTTON_RIGHT,
XINPUT_GAMEPAD_START, PAD_BUTTON_START,
XINPUT_GAMEPAD_BACK, PAD_BUTTON_SELECT,
XINPUT_GAMEPAD_LEFT_SHOULDER, PAD_BUTTON_LBUMPER,
XINPUT_GAMEPAD_RIGHT_SHOULDER, PAD_BUTTON_RBUMPER,
XINPUT_GAMEPAD_A, PAD_BUTTON_A,
XINPUT_GAMEPAD_B, PAD_BUTTON_B,
XINPUT_GAMEPAD_X, PAD_BUTTON_X,
XINPUT_GAMEPAD_Y, PAD_BUTTON_Y,
XINPUT_GAMEPAD_LEFT_THUMB, PAD_BUTTON_LEFT_THUMB,
XINPUT_GAMEPAD_RIGHT_THUMB, PAD_BUTTON_RIGHT_THUMB,
};
static inline u32 CtrlForXinput(int xinput) {
for (int i = 0; i < sizeof(xinput_ctrl_map)/sizeof(xinput_ctrl_map[0]); i += 2)
if (xinput_ctrl_map[i] == xinput) return (u32) xinput_ctrl_map[i+1];
return 0;
}
void XinputDevice::ApplyDiff(XINPUT_STATE &state, InputState &input_state) {
for (int i = 1; i < USHRT_MAX; i <<= 1) {
if (state.Gamepad.wButtons & i)
input_state.pad_buttons |= CtrlForXinput(i);
}
}