Skip to content
Permalink
Browse files

[xinput] Move motor definitions to xml file

  • Loading branch information...
garbear committed Aug 11, 2016
1 parent 5d67d15 commit de29de0c91cfe94e134ad35d0944892255cad5da
@@ -9,6 +9,7 @@
<feature name="guide" button="14" />
<feature name="left" button="13" />
<feature name="leftbumper" button="4" />
<feature name="leftmotor" motor="0" />
<feature name="leftstick">
<up axis="+1" />
<down axis="-1" />
@@ -19,6 +20,7 @@
<feature name="lefttrigger" axis="+4" />
<feature name="right" button="11" />
<feature name="rightbumper" button="5" />
<feature name="rightmotor" motor="1" />
<feature name="rightstick">
<up axis="+3" />
<down axis="-3" />
@@ -116,10 +118,12 @@
<feature name="l3" button="8" />
<feature name="left" button="13" />
<feature name="leftbumper" button="4" />
<feature name="leftmotor" motor="0" />
<feature name="lefttrigger" axis="+4" />
<feature name="r3" button="9" />
<feature name="right" button="11" />
<feature name="rightbumper" button="5" />
<feature name="rightmotor" motor="1" />
<feature name="righttrigger" axis="+5" />
<feature name="select" button="6" />
<feature name="square" button="2" />
@@ -22,7 +22,6 @@
#include "storage/ButtonMapTypes.h" // for FeatureVector

#include <string>
#include <vector>

namespace JOYSTICK
{
@@ -30,30 +30,6 @@ using namespace JOYSTICK;

#define MAX_JOYSTICKS 4

// --- DefaultFeatures ---------------------------------------------------------

namespace JOYSTICK
{
struct FeatureStruct
{
const char* controllerId;
const char* name;
JOYSTICK_FEATURE_TYPE type;
unsigned int driverIndex;
};

std::array<FeatureStruct, 4> DefaultFeatures = {
{
{ "game.controller.default", "leftmotor", JOYSTICK_FEATURE_TYPE_MOTOR, CJoystickXInput::MOTOR_LEFT },
{ "game.controller.default", "rightmotor", JOYSTICK_FEATURE_TYPE_MOTOR, CJoystickXInput::MOTOR_RIGHT },
{ "game.controller.ps", "strongmotor", JOYSTICK_FEATURE_TYPE_MOTOR, CJoystickXInput::MOTOR_LEFT },
{ "game.controller.ps", "weakmotor", JOYSTICK_FEATURE_TYPE_MOTOR, CJoystickXInput::MOTOR_RIGHT },
}
};
}

// --- CJoystickInterfaceXInput ------------------------------------------------

const char* CJoystickInterfaceXInput::Name(void) const
{
return INTERFACE_XINPUT;
@@ -83,16 +59,3 @@ bool CJoystickInterfaceXInput::ScanForJoysticks(JoystickVector& joysticks)

return true;
}

void CJoystickInterfaceXInput::GetFeatures(const std::string& controllerId, FeatureVector& features)
{
for (auto& featureStruct : DefaultFeatures)
{
if (controllerId == featureStruct.controllerId)
{
ADDON::JoystickFeature feature(featureStruct.name, featureStruct.type);
feature.SetPrimitive(ADDON::DriverPrimitive::CreateMotor(featureStruct.driverIndex));
features.push_back(std::move(feature));
}
}
}
@@ -34,6 +34,5 @@ namespace JOYSTICK
virtual bool Initialize(void) override;
virtual void Deinitialize(void) override;
virtual bool ScanForJoysticks(JoystickVector& joysticks) override;
virtual void GetFeatures(const std::string& controllerId, FeatureVector& features) override;
};
}
@@ -28,6 +28,7 @@
using namespace JOYSTICK;

#define HAT_CHAR 'h'
#define MOTOR_CHAR 'm'

std::string ButtonMapTranslator::ToString(const ADDON::DriverPrimitive& primitive)
{
@@ -63,37 +64,50 @@ std::string ButtonMapTranslator::ToString(const ADDON::DriverPrimitive& primitiv
return strPrimitive.str();
}

ADDON::DriverPrimitive ButtonMapTranslator::ToDriverPrimitive(const std::string& strPrimitive)
ADDON::DriverPrimitive ButtonMapTranslator::ToDriverPrimitive(const std::string& strPrimitive, JOYSTICK_DRIVER_PRIMITIVE_TYPE type)
{
ADDON::DriverPrimitive primitive;

if (!strPrimitive.empty())
{
bool bIsButton = std::isdigit(strPrimitive[0]) ? true : false;
bool bIsHat = (strPrimitive[0] == HAT_CHAR);
bool bIsAxis = (JoystickTranslator::TranslateSemiAxisDir(strPrimitive[0]) != JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN);

if (bIsButton)
switch (type)
{
case JOYSTICK_DRIVER_PRIMITIVE_TYPE_BUTTON:
{
primitive = ADDON::DriverPrimitive::CreateButton(std::atoi(strPrimitive.c_str()));
if (std::isdigit(strPrimitive[0]))
primitive = ADDON::DriverPrimitive::CreateButton(std::atoi(strPrimitive.c_str()));
break;
}
else if (bIsHat)
case JOYSTICK_DRIVER_PRIMITIVE_TYPE_HAT_DIRECTION:
{
unsigned int hatIndex = std::atoi(strPrimitive.substr(1).c_str());

size_t dirPos = strPrimitive.find_first_not_of("0123456789", 1);
if (dirPos != std::string::npos)
if (strPrimitive[0] == HAT_CHAR)
{
JOYSTICK_DRIVER_HAT_DIRECTION hatDir = JoystickTranslator::TranslateHatDir(strPrimitive.substr(dirPos));

if (hatDir != JOYSTICK_DRIVER_HAT_UNKNOWN)
primitive = ADDON::DriverPrimitive(hatIndex, hatDir);
unsigned int hatIndex = std::atoi(strPrimitive.substr(1).c_str());
size_t dirPos = strPrimitive.find_first_not_of("0123456789", 1);
if (dirPos != std::string::npos)
{
JOYSTICK_DRIVER_HAT_DIRECTION hatDir = JoystickTranslator::TranslateHatDir(strPrimitive.substr(dirPos));
if (hatDir != JOYSTICK_DRIVER_HAT_UNKNOWN)
primitive = ADDON::DriverPrimitive(hatIndex, hatDir);
}
}
break;
}
else if (bIsAxis)
case JOYSTICK_DRIVER_PRIMITIVE_TYPE_SEMIAXIS:
{
primitive = ADDON::DriverPrimitive(std::atoi(strPrimitive.substr(1).c_str()),
JoystickTranslator::TranslateSemiAxisDir(strPrimitive[0]));
JOYSTICK_DRIVER_SEMIAXIS_DIRECTION dir = JoystickTranslator::TranslateSemiAxisDir(strPrimitive[0]);
if (dir != JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN)
primitive = ADDON::DriverPrimitive(std::atoi(strPrimitive.substr(1).c_str()), dir);
break;
}
case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR:
{
if (std::isdigit(strPrimitive[0]))
primitive = ADDON::DriverPrimitive::CreateMotor(std::atoi(strPrimitive.c_str()));
break;
}
default:
break;
}
}

@@ -32,6 +32,10 @@ namespace JOYSTICK
* \brief Canonical string serialization of the driver primitive
*/
static std::string ToString(const ADDON::DriverPrimitive& primitive);
static ADDON::DriverPrimitive ToDriverPrimitive(const std::string& primitive);

/*!
* \brief Deserialize string representation of driver primitive
*/
static ADDON::DriverPrimitive ToDriverPrimitive(const std::string& primitive, JOYSTICK_DRIVER_PRIMITIVE_TYPE type);
};
}
@@ -513,25 +513,33 @@ bool CButtonMapXml::DeserializePrimitive(const TiXmlElement* pElement, ADDON::Dr
const char* button = pElement->Attribute(BUTTONMAP_XML_ATTR_FEATURE_BUTTON);
if (button)
{
primitive = ButtonMapTranslator::ToDriverPrimitive(button);
primitive = ButtonMapTranslator::ToDriverPrimitive(button, JOYSTICK_DRIVER_PRIMITIVE_TYPE_BUTTON);
}
else
{
const char* hat = pElement->Attribute(BUTTONMAP_XML_ATTR_FEATURE_HAT);
if (hat)
{
primitive = ButtonMapTranslator::ToDriverPrimitive(hat);
primitive = ButtonMapTranslator::ToDriverPrimitive(hat, JOYSTICK_DRIVER_PRIMITIVE_TYPE_HAT_DIRECTION);
}
else
{
const char* axis = pElement->Attribute(BUTTONMAP_XML_ATTR_FEATURE_AXIS);
if (axis)
{
primitive = ButtonMapTranslator::ToDriverPrimitive(axis);
primitive = ButtonMapTranslator::ToDriverPrimitive(axis, JOYSTICK_DRIVER_PRIMITIVE_TYPE_SEMIAXIS);
}
else
{
return false;
const char* motor = pElement->Attribute(BUTTONMAP_XML_ATTR_FEATURE_MOTOR);
if (motor)
{
primitive = ButtonMapTranslator::ToDriverPrimitive(motor, JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR);
}
else
{
return false;
}
}
}
}

0 comments on commit de29de0

Please sign in to comment.
You can’t perform that action at this time.