From 2210b19244f8ae2823d4d3e6380af81688f720b3 Mon Sep 17 00:00:00 2001 From: "Diogo Franco (Kovensky)" Date: Mon, 12 Nov 2012 15:38:21 +0000 Subject: [PATCH] Xinput input support Picks the first Xinput device available, waits 100 frames between retries when no devices are available. Controls are hardcoded so every x360 controller button corresponds to the PSP button in the same physical position. Input from right stick and from analog triggers is ignored. The Xinput code though is overriden by SampleControls' hardcoded keyboard input. XinputDevice was tested to work with samples/controller/basic when SampleControls is disabled. --- Windows/InputDevice.h | 10 +++ Windows/PPSSPP.vcxproj | 5 +- Windows/PPSSPP.vcxproj.filters | 12 ++++ Windows/WindowsHost.cpp | 1 + Windows/WindowsHost.h | 2 + Windows/XinputDevice.cpp | 109 +++++++++++++++++++++++++++++++++ Windows/XinputDevice.h | 17 +++++ 7 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 Windows/InputDevice.h create mode 100644 Windows/XinputDevice.cpp create mode 100644 Windows/XinputDevice.h 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; +}; +