mirror of
https://github.com/elasota/Aerofoil.git
synced 2025-12-14 03:59:36 +00:00
XInput support
This commit is contained in:
183
GpInputDriver_XInput/GpInputDriverXInput.cpp
Normal file
183
GpInputDriver_XInput/GpInputDriverXInput.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
#include "GpInputDriverXInput.h"
|
||||
#include "GpVOSEvent.h"
|
||||
#include "GpWindows.h"
|
||||
#include "IGpVOSEventQueue.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <new>
|
||||
|
||||
#pragma comment(lib, "xinput.lib")
|
||||
|
||||
void GpInputDriverXInput::ProcessInput()
|
||||
{
|
||||
for (DWORD i = 0; i < XUSER_MAX_COUNT; i++)
|
||||
{
|
||||
XINPUT_STATE newState;
|
||||
memset(&newState, 0, sizeof(newState));
|
||||
|
||||
DWORD result = XInputGetState(i, &newState);
|
||||
|
||||
if (result == ERROR_SUCCESS)
|
||||
{
|
||||
bool isNewState = false;
|
||||
unsigned int playerNumber = 0;
|
||||
if (m_xUserToPlayerNumber[i] < 0)
|
||||
{
|
||||
// Newly-connected player
|
||||
|
||||
for (int i = 0; i < XUSER_MAX_COUNT; i++)
|
||||
{
|
||||
if (!m_playerNumberIsConnected[i])
|
||||
{
|
||||
playerNumber = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
m_xUserToPlayerNumber[i] = playerNumber;
|
||||
m_playerNumberIsConnected[playerNumber] = true;
|
||||
|
||||
isNewState = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
playerNumber = m_xUserToPlayerNumber[i];
|
||||
isNewState = (newState.dwPacketNumber != m_controllerStates[i].dwPacketNumber);
|
||||
}
|
||||
|
||||
if (isNewState)
|
||||
{
|
||||
this->ProcessControllerStateChange(playerNumber, m_controllerStates[i], newState);
|
||||
m_controllerStates[i] = newState;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_xUserToPlayerNumber[i] >= 0)
|
||||
{
|
||||
// Was connected last frame, process as a blank state and then free the player slot
|
||||
memset(&newState, 0, sizeof(newState));
|
||||
|
||||
const unsigned int playerNumber = m_xUserToPlayerNumber[i];
|
||||
|
||||
this->ProcessControllerStateChange(playerNumber, m_controllerStates[i], newState);
|
||||
|
||||
m_controllerStates[i] = newState;
|
||||
m_playerNumberIsConnected[playerNumber] = false;
|
||||
m_xUserToPlayerNumber[i] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GpInputDriverXInput::Shutdown()
|
||||
{
|
||||
this->~GpInputDriverXInput();
|
||||
free(this);
|
||||
}
|
||||
|
||||
GpInputDriverXInput *GpInputDriverXInput::Create(const GpInputDriverProperties &props)
|
||||
{
|
||||
void *storage = malloc(sizeof(GpInputDriverXInput));
|
||||
if (!storage)
|
||||
return nullptr;
|
||||
|
||||
return new (storage) GpInputDriverXInput(props);
|
||||
}
|
||||
|
||||
GpInputDriverXInput::GpInputDriverXInput(const GpInputDriverProperties &props)
|
||||
: m_properties(props)
|
||||
{
|
||||
for (int i = 0; i < XUSER_MAX_COUNT; i++)
|
||||
m_xUserToPlayerNumber[i] = -1;
|
||||
|
||||
memset(m_controllerStates, 0, sizeof(m_controllerStates));
|
||||
memset(m_playerNumberIsConnected, 0, sizeof(m_playerNumberIsConnected));
|
||||
}
|
||||
|
||||
GpInputDriverXInput::~GpInputDriverXInput()
|
||||
{
|
||||
}
|
||||
|
||||
void GpInputDriverXInput::ProcessControllerStateChange(unsigned int playerNumber, const XINPUT_STATE &prevState, const XINPUT_STATE &newState)
|
||||
{
|
||||
const XINPUT_GAMEPAD &prevGamepad = prevState.Gamepad;
|
||||
const XINPUT_GAMEPAD &newGamepad = newState.Gamepad;
|
||||
|
||||
DWORD prevButtons = prevGamepad.wButtons;
|
||||
DWORD newButtons = newGamepad.wButtons;
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_START, playerNumber, GpGamepadButtons::kMisc1);
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_BACK, playerNumber, GpGamepadButtons::kMisc2);
|
||||
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_DPAD_UP, playerNumber, GpGamepadButtons::kDPadUp);
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_DPAD_DOWN, playerNumber, GpGamepadButtons::kDPadDown);
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_DPAD_LEFT, playerNumber, GpGamepadButtons::kDPadLeft);
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_DPAD_RIGHT, playerNumber, GpGamepadButtons::kDPadRight);
|
||||
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_LEFT_THUMB, playerNumber, GpGamepadButtons::kLeftStick);
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_RIGHT_THUMB, playerNumber, GpGamepadButtons::kRightStick);
|
||||
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_LEFT_SHOULDER, playerNumber, GpGamepadButtons::kLeftBumper);
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_RIGHT_SHOULDER, playerNumber, GpGamepadButtons::kRightBumper);
|
||||
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_Y, playerNumber, GpGamepadButtons::kFaceUp);
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_A, playerNumber, GpGamepadButtons::kFaceDown);
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_X, playerNumber, GpGamepadButtons::kFaceLeft);
|
||||
ProcessButtonStateChange(prevButtons, newButtons, XINPUT_GAMEPAD_B, playerNumber, GpGamepadButtons::kFaceRight);
|
||||
|
||||
ProcessAxisStateChange(ConvertTriggerValue(prevState.Gamepad.bLeftTrigger), ConvertTriggerValue(newState.Gamepad.bLeftTrigger), playerNumber, GpGamepadAxes::kLeftTrigger);
|
||||
ProcessAxisStateChange(ConvertTriggerValue(prevState.Gamepad.bRightTrigger), ConvertTriggerValue(newState.Gamepad.bRightTrigger), playerNumber, GpGamepadAxes::kRightTrigger);
|
||||
ProcessAxisStateChange(prevState.Gamepad.sThumbLX, newState.Gamepad.sThumbLX, playerNumber, GpGamepadAxes::kLeftStickX);
|
||||
ProcessAxisStateChange(prevState.Gamepad.sThumbLY, newState.Gamepad.sThumbLY, playerNumber, GpGamepadAxes::kLeftStickY);
|
||||
ProcessAxisStateChange(prevState.Gamepad.sThumbRX, newState.Gamepad.sThumbRX, playerNumber, GpGamepadAxes::kRightStickX);
|
||||
ProcessAxisStateChange(prevState.Gamepad.sThumbRY, newState.Gamepad.sThumbRY, playerNumber, GpGamepadAxes::kRightStickY);
|
||||
}
|
||||
|
||||
void GpInputDriverXInput::ProcessButtonStateChange(DWORD prevState, DWORD newState, DWORD deltaBit, unsigned int playerNum, GpGamepadButton_t gamepadButton)
|
||||
{
|
||||
DWORD deltaState = prevState ^ newState;
|
||||
|
||||
if ((deltaState & deltaBit) == 0)
|
||||
return;
|
||||
|
||||
const GpKeyboardInputEventType_t eventType = ((prevState & deltaBit) == 0) ? GpKeyboardInputEventTypes::kDown : GpKeyboardInputEventTypes::kUp;
|
||||
|
||||
if (GpVOSEvent *evt = m_properties.m_eventQueue->QueueEvent())
|
||||
{
|
||||
evt->m_eventType = GpVOSEventTypes::kKeyboardInput;
|
||||
evt->m_event.m_keyboardInputEvent.m_eventType = eventType;
|
||||
evt->m_event.m_keyboardInputEvent.m_keyIDSubset = GpKeyIDSubsets::kGamepadButton;
|
||||
evt->m_event.m_keyboardInputEvent.m_key.m_gamepadKey.m_button = gamepadButton;
|
||||
evt->m_event.m_keyboardInputEvent.m_key.m_gamepadKey.m_player = playerNum;
|
||||
}
|
||||
}
|
||||
|
||||
int16_t GpInputDriverXInput::ConvertTriggerValue(uint8_t v)
|
||||
{
|
||||
return static_cast<int16_t>((v * 257) >> 1);
|
||||
}
|
||||
|
||||
void GpInputDriverXInput::ProcessAxisStateChange(int16_t prevState, int16_t newState, unsigned int playerNum, GpGamepadAxis_t gamepadAxis)
|
||||
{
|
||||
if (prevState != newState)
|
||||
{
|
||||
if (prevState == -32768)
|
||||
prevState = -32767;
|
||||
if (newState == -32768)
|
||||
newState = -32767;
|
||||
|
||||
if (GpVOSEvent *evt = m_properties.m_eventQueue->QueueEvent())
|
||||
{
|
||||
evt->m_eventType = GpVOSEventTypes::kGamepadInput;
|
||||
evt->m_event.m_gamepadInputEvent.m_eventType = GpGamepadInputEventTypes::kAnalogAxisChanged;
|
||||
evt->m_event.m_gamepadInputEvent.m_event.m_analogAxisEvent.m_axis = gamepadAxis;
|
||||
evt->m_event.m_gamepadInputEvent.m_event.m_analogAxisEvent.m_state = newState;
|
||||
evt->m_event.m_gamepadInputEvent.m_event.m_analogAxisEvent.m_player = playerNum;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) IGpInputDriver *GpDriver_CreateInputDriver_XInput(const GpInputDriverProperties &properties)
|
||||
{
|
||||
return GpInputDriverXInput::Create(properties);
|
||||
}
|
||||
32
GpInputDriver_XInput/GpInputDriverXInput.h
Normal file
32
GpInputDriver_XInput/GpInputDriverXInput.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "IGpInputDriver.h"
|
||||
#include "GpInputDriverProperties.h"
|
||||
#include "GpVOSEvent.h"
|
||||
#include "GpWindows.h"
|
||||
|
||||
#include <Xinput.h>
|
||||
|
||||
class GpInputDriverXInput final : public IGpInputDriver
|
||||
{
|
||||
public:
|
||||
void ProcessInput() override;
|
||||
void Shutdown() override;
|
||||
|
||||
static GpInputDriverXInput *Create(const GpInputDriverProperties &props);
|
||||
|
||||
private:
|
||||
GpInputDriverXInput(const GpInputDriverProperties &props);
|
||||
~GpInputDriverXInput();
|
||||
|
||||
void ProcessControllerStateChange(unsigned int playerNumber, const XINPUT_STATE &prevState, const XINPUT_STATE &newState);
|
||||
void ProcessButtonStateChange(DWORD prevState, DWORD newState, DWORD deltaBit, unsigned int playerNum, GpGamepadButton_t gamepadButton);
|
||||
void ProcessAxisStateChange(int16_t prevState, int16_t newState, unsigned int playerNum, GpGamepadAxis_t gamepadAxis);
|
||||
|
||||
static int16_t ConvertTriggerValue(uint8_t v);
|
||||
|
||||
GpInputDriverProperties m_properties;
|
||||
int m_xUserToPlayerNumber[XUSER_MAX_COUNT];
|
||||
bool m_playerNumberIsConnected[XUSER_MAX_COUNT];
|
||||
XINPUT_STATE m_controllerStates[XUSER_MAX_COUNT];
|
||||
};
|
||||
130
GpInputDriver_XInput/GpInputDriver_XInput.vcxproj
Normal file
130
GpInputDriver_XInput/GpInputDriver_XInput.vcxproj
Normal file
@@ -0,0 +1,130 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{17B96F07-EF92-47CD-95A5-8E6EE38AB564}</ProjectGuid>
|
||||
<RootNamespace>GpInputDriverXInput</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
<Import Project="..\GpCommon.props" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
<Import Project="..\GpCommon.props" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
<Import Project="..\GpCommon.props" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
<Import Project="..\GpCommon.props" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="GpInputDriverXInput.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="GpInputDriverXInput.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
27
GpInputDriver_XInput/GpInputDriver_XInput.vcxproj.filters
Normal file
27
GpInputDriver_XInput/GpInputDriver_XInput.vcxproj.filters
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="GpInputDriverXInput.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="GpInputDriverXInput.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user