2012-11-12 15:38:21 +00:00
|
|
|
#include <limits.h>
|
2013-03-30 22:32:34 +00:00
|
|
|
#include "XinputDevice.h"
|
2013-04-16 14:34:20 +00:00
|
|
|
#include "Core/Config.h"
|
2013-03-30 23:25:10 +00:00
|
|
|
#include "input/input_state.h"
|
2012-11-12 15:38:21 +00:00
|
|
|
|
2012-11-14 01:15:54 +00:00
|
|
|
#ifndef XUSER_MAX_COUNT
|
|
|
|
#define XUSER_MAX_COUNT 4
|
|
|
|
#endif
|
|
|
|
|
2012-11-12 15:38:21 +00:00
|
|
|
XinputDevice::XinputDevice() {
|
|
|
|
ZeroMemory( &this->prevState, sizeof(this->prevState) );
|
|
|
|
this->check_delay = 0;
|
|
|
|
this->gamepad_idx = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Stick {
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
};
|
2013-03-30 23:25:10 +00:00
|
|
|
static Stick NormalizedDeadzoneFilter(short x, short y);
|
2012-11-12 15:38:21 +00:00
|
|
|
|
2013-03-30 22:32:34 +00:00
|
|
|
int XinputDevice::UpdateState(InputState &input_state) {
|
2013-04-16 14:34:20 +00:00
|
|
|
if (g_Config.iForceInputDevice > 0) return -1;
|
2012-11-12 15:38:21 +00:00
|
|
|
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 ) {
|
2013-03-30 23:25:10 +00:00
|
|
|
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;
|
2013-04-01 10:35:02 +00:00
|
|
|
|
|
|
|
// Also convert the analog triggers.
|
|
|
|
input_state.pad_ltrigger = state.Gamepad.bLeftTrigger / 255.0f;
|
|
|
|
input_state.pad_rtrigger = state.Gamepad.bRightTrigger / 255.0f;
|
|
|
|
|
2012-11-12 15:38:21 +00:00
|
|
|
this->prevState = state;
|
|
|
|
this->check_delay = 0;
|
2013-04-01 18:15:16 +00:00
|
|
|
|
|
|
|
// If there's an XInput pad, skip following pads. This prevents DInput and XInput
|
|
|
|
// from colliding.
|
2013-04-08 01:41:26 +00:00
|
|
|
return UPDATESTATE_SKIP_PAD;
|
2012-11-12 15:38:21 +00:00
|
|
|
} else {
|
|
|
|
// wait check_delay frames before polling the controller again
|
|
|
|
this->gamepad_idx = -1;
|
|
|
|
this->check_delay = 100;
|
2012-11-12 17:32:35 +00:00
|
|
|
return -1;
|
2012-11-12 15:38:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only filter the left stick since PSP has no analog triggers or right stick
|
2013-03-30 23:25:10 +00:00
|
|
|
static Stick NormalizedDeadzoneFilter(short x, short y) {
|
2012-11-12 15:38:21 +00:00
|
|
|
static const short DEADZONE = XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
|
|
|
|
Stick left;
|
2013-03-30 23:25:10 +00:00
|
|
|
left.x = x;
|
|
|
|
left.y = y;
|
2012-11-12 15:38:21 +00:00
|
|
|
|
|
|
|
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;
|
2012-11-17 18:56:28 +00:00
|
|
|
} else {
|
2012-11-12 15:38:21 +00:00
|
|
|
left.x = left.y = 0;
|
2012-11-17 18:56:28 +00:00
|
|
|
}
|
2012-11-12 15:38:21 +00:00
|
|
|
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
2013-03-31 09:30:50 +00:00
|
|
|
// 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[] = {
|
2013-03-30 23:25:10 +00:00
|
|
|
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,
|
2013-03-31 09:30:50 +00:00
|
|
|
XINPUT_GAMEPAD_LEFT_THUMB, PAD_BUTTON_LEFT_THUMB,
|
|
|
|
XINPUT_GAMEPAD_RIGHT_THUMB, PAD_BUTTON_RIGHT_THUMB,
|
2012-11-12 15:38:21 +00:00
|
|
|
};
|
2013-03-31 09:30:50 +00:00
|
|
|
|
2012-11-12 15:38:21 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-30 23:25:10 +00:00
|
|
|
void XinputDevice::ApplyDiff(XINPUT_STATE &state, InputState &input_state) {
|
2012-11-12 15:38:21 +00:00
|
|
|
for (int i = 1; i < USHRT_MAX; i <<= 1) {
|
2013-03-31 09:30:50 +00:00
|
|
|
if (state.Gamepad.wButtons & i)
|
2013-03-30 23:25:10 +00:00
|
|
|
input_state.pad_buttons |= CtrlForXinput(i);
|
2012-11-12 15:38:21 +00:00
|
|
|
}
|
|
|
|
}
|