diff --git a/Windows/InputDevice.h b/Windows/InputDevice.h
new file mode 100644
index 0000000000..8598f4d7ef
--- /dev/null
+++ b/Windows/InputDevice.h
@@ -0,0 +1,10 @@
+#include "../Common/CommonTypes.h"
+#include "../Core/HLE/sceCtrl.h"
+
+#pragma once
+class InputDevice
+{
+public:
+ virtual int UpdateState() = 0;
+};
+
diff --git a/Windows/PPSSPP.vcxproj b/Windows/PPSSPP.vcxproj
index 4a9074e9de..fc3ec58516 100644
--- a/Windows/PPSSPP.vcxproj
+++ b/Windows/PPSSPP.vcxproj
@@ -141,7 +141,7 @@
MultiThreadedDebugDLL
- Winmm.lib;Ws2_32.lib;opengl32.lib;glu32.lib;comctl32.lib;dsound.lib;%(AdditionalDependencies)
+ Winmm.lib;Ws2_32.lib;opengl32.lib;glu32.lib;comctl32.lib;dsound.lib;xinput.lib;%(AdditionalDependencies)
true
$(OutDir)$(ProjectName).pdb
Windows
@@ -390,6 +390,7 @@
Create
Create
+
@@ -403,6 +404,7 @@
+
@@ -417,6 +419,7 @@
+
diff --git a/Windows/PPSSPP.vcxproj.filters b/Windows/PPSSPP.vcxproj.filters
index b7769f3b28..e4b591299f 100644
--- a/Windows/PPSSPP.vcxproj.filters
+++ b/Windows/PPSSPP.vcxproj.filters
@@ -23,6 +23,9 @@
{d53d0871-8572-4393-822e-d14db0a88dab}
+
+ {a1963305-bf88-43ef-8ba2-bb3933584b38}
+
@@ -92,6 +95,9 @@
Android
+
+ Windows\Input
+
@@ -161,6 +167,12 @@
Android
+
+ Windows\Input
+
+
+ Windows\Input
+
diff --git a/Windows/WindowsHost.cpp b/Windows/WindowsHost.cpp
index 580b1e12ec..fdbfc2bad1 100644
--- a/Windows/WindowsHost.cpp
+++ b/Windows/WindowsHost.cpp
@@ -85,6 +85,7 @@ void WindowsHost::SetDebugMode(bool mode)
void WindowsHost::BeginFrame()
{
+ xinput.UpdateState();
GL_BeginFrame();
}
void WindowsHost::EndFrame()
diff --git a/Windows/WindowsHost.h b/Windows/WindowsHost.h
index 3e7728d552..636211df49 100644
--- a/Windows/WindowsHost.h
+++ b/Windows/WindowsHost.h
@@ -1,4 +1,5 @@
#include "../Core/Host.h"
+#include "XinputDevice.h"
class WindowsHost : public Host
{
@@ -30,4 +31,5 @@ public:
private:
HWND displayWindow;
+ XinputDevice xinput;
};
\ No newline at end of file
diff --git a/Windows/XinputDevice.cpp b/Windows/XinputDevice.cpp
new file mode 100644
index 0000000000..d6130a594e
--- /dev/null
+++ b/Windows/XinputDevice.cpp
@@ -0,0 +1,109 @@
+#include "stdafx.h"
+#include "XinputDevice.h"
+#include
+#include
+
+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(XINPUT_STATE &state);
+
+int XinputDevice::UpdateState() {
+ 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 ) {
+ this->ApplyDiff(state);
+ Stick left = NormalizedDeadzoneFilter(state);
+ __CtrlSetAnalog(left.x, left.y);
+ this->prevState = state;
+ this->check_delay = 0;
+ } else {
+ // wait check_delay frames before polling the controller again
+ this->gamepad_idx = -1;
+ this->check_delay = 100;
+ }
+ return 0;
+}
+
+// We only filter the left stick since PSP has no analog triggers or right stick
+static Stick NormalizedDeadzoneFilter(XINPUT_STATE &state) {
+ static const short DEADZONE = XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
+ Stick left;
+ left.x = state.Gamepad.sThumbLX;
+ left.y = state.Gamepad.sThumbLY;
+
+ 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;
+}
+
+static const unsigned short xinput_ctrl_map[] = {
+ XINPUT_GAMEPAD_DPAD_UP, CTRL_UP,
+ XINPUT_GAMEPAD_DPAD_DOWN, CTRL_DOWN,
+ XINPUT_GAMEPAD_DPAD_LEFT, CTRL_LEFT,
+ XINPUT_GAMEPAD_DPAD_RIGHT, CTRL_RIGHT,
+ XINPUT_GAMEPAD_START, CTRL_START,
+ XINPUT_GAMEPAD_BACK, CTRL_SELECT,
+ XINPUT_GAMEPAD_LEFT_SHOULDER, CTRL_LTRIGGER,
+ XINPUT_GAMEPAD_RIGHT_SHOULDER, CTRL_RTRIGGER,
+ XINPUT_GAMEPAD_A, CTRL_CROSS,
+ XINPUT_GAMEPAD_B, CTRL_CIRCLE,
+ XINPUT_GAMEPAD_X, CTRL_SQUARE,
+ XINPUT_GAMEPAD_Y, CTRL_TRIANGLE,
+};
+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) {
+ unsigned short pressed = state.Gamepad.wButtons & ~this->prevState.Gamepad.wButtons;
+ unsigned short released = ~state.Gamepad.wButtons & this->prevState.Gamepad.wButtons;
+
+ for (int i = 1; i < USHRT_MAX; i <<= 1) {
+ if (pressed & i)
+ __CtrlButtonDown(CtrlForXinput(i));
+ if (released & i)
+ __CtrlButtonUp(CtrlForXinput(i));
+ }
+}
\ No newline at end of file
diff --git a/Windows/XinputDevice.h b/Windows/XinputDevice.h
new file mode 100644
index 0000000000..71a31becfe
--- /dev/null
+++ b/Windows/XinputDevice.h
@@ -0,0 +1,17 @@
+#pragma once
+#include "InputDevice.h"
+#include "Xinput.h"
+
+class XinputDevice :
+ public InputDevice
+{
+public:
+ XinputDevice();
+ virtual int UpdateState();
+private:
+ void ApplyDiff(XINPUT_STATE &state);
+ int gamepad_idx;
+ int check_delay;
+ XINPUT_STATE prevState;
+};
+