| 1 |
/* |
| 2 |
* Copyright (C) 2015-2021 Team Kodi (https://kodi.tv) |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
* See LICENSE.md for more information. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "LibretroDevice.h" |
| 9 |
#include "ButtonMapper.h" |
| 10 |
#include "InputDefinitions.h" |
| 11 |
#include "LibretroDeviceInput.h" |
| 12 |
#include "libretro/LibretroTranslator.h" |
| 13 |
#include "libretro-common/libretro.h" |
| 14 |
#include "log/Log.h" |
| 15 |
|
| 16 |
#include <tinyxml.h> |
| 17 |
|
| 18 |
#include <sstream> |
| 19 |
|
| 20 |
using namespace LIBRETRO; |
| 21 |
|
| 22 |
CLibretroDevice::CLibretroDevice() : |
| 23 |
m_type(RETRO_DEVICE_NONE) |
| 24 |
{ |
| 25 |
} |
| 26 |
|
| 27 |
CLibretroDevice::CLibretroDevice(const std::string &controllerId) |
| 28 |
: m_controllerId(controllerId), |
| 29 |
m_type(CButtonMapper::Get().GetLibretroType(controllerId)), |
| 30 |
m_subclass(CButtonMapper::Get().GetSubclass(controllerId)), |
| 31 |
m_input(new CLibretroDeviceInput(controllerId)) |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
CLibretroDevice::~CLibretroDevice() |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
bool CLibretroDevice::Deserialize(const TiXmlElement* pElement, unsigned int buttonMapVersion) |
| 40 |
{ |
| 41 |
if (!pElement) |
| 42 |
return false; |
| 43 |
|
| 44 |
// Controller ID |
| 45 |
const char* controllerId = pElement->Attribute(BUTTONMAP_XML_ATTR_CONTROLLER_ID); |
| 46 |
if (!controllerId) |
| 47 |
{ |
| 48 |
esyslog("<%s> tag has no \"%s\" attribute", BUTTONMAP_XML_ELM_CONTROLLER, BUTTONMAP_XML_ATTR_CONTROLLER_ID); |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
// Device type |
| 53 |
const char* type = pElement->Attribute(BUTTONMAP_XML_ATTR_DEVICE_TYPE); |
| 54 |
if (!type) |
| 55 |
{ |
| 56 |
esyslog("<%s> tag has no \"%s\" attribute", BUTTONMAP_XML_ELM_CONTROLLER, BUTTONMAP_XML_ATTR_DEVICE_TYPE); |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
m_controllerId = controllerId; |
| 61 |
m_type = LibretroTranslator::GetDeviceType(type); |
| 62 |
|
| 63 |
if (m_type == RETRO_DEVICE_NONE) |
| 64 |
{ |
| 65 |
esyslog("<%s> tag has invalid device type: \"%s\"", BUTTONMAP_XML_ELM_CONTROLLER, type); |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
// Device subclass |
| 70 |
const char* subclass = pElement->Attribute(BUTTONMAP_XML_ATTR_DEVICE_SUBCLASS); |
| 71 |
if (subclass) |
| 72 |
std::istringstream(subclass) >> m_subclass; |
| 73 |
else |
| 74 |
m_subclass = RETRO_SUBCLASS_NONE; |
| 75 |
|
| 76 |
// Features |
| 77 |
const TiXmlElement* pFeature = pElement->FirstChildElement(BUTTONMAP_XML_ELM_FEATURE); |
| 78 |
for (; pFeature != nullptr; pFeature = pFeature->NextSiblingElement(BUTTONMAP_XML_ELM_FEATURE)) |
| 79 |
{ |
| 80 |
const char* name = pFeature->Attribute(BUTTONMAP_XML_ATTR_FEATURE_NAME); |
| 81 |
if (!name) |
| 82 |
{ |
| 83 |
esyslog("<%s> tag has no \"%s\" attribute", BUTTONMAP_XML_ELM_FEATURE, BUTTONMAP_XML_ATTR_FEATURE_NAME); |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
const char* mapto = pFeature->Attribute(BUTTONMAP_XML_ATTR_FEATURE_MAPTO); |
| 88 |
if (!mapto) |
| 89 |
{ |
| 90 |
esyslog("<%s> tag has no \"%s\" attribute", BUTTONMAP_XML_ELM_FEATURE, BUTTONMAP_XML_ATTR_FEATURE_MAPTO); |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
const char* axis = pFeature->Attribute(BUTTONMAP_XML_ATTR_FEATURE_AXIS); |
| 95 |
|
| 96 |
FeatureMapItem libretroFeature = { mapto }; |
| 97 |
|
| 98 |
// Ensure feature is valid |
| 99 |
if (LibretroTranslator::GetFeatureIndex(libretroFeature.feature) < 0) |
| 100 |
{ |
| 101 |
esyslog("<%s> tag has invalid \"%s\" attribute: \"%s\"", BUTTONMAP_XML_ELM_FEATURE, BUTTONMAP_XML_ATTR_FEATURE_MAPTO, mapto); |
| 102 |
continue; |
| 103 |
} |
| 104 |
|
| 105 |
if (axis != nullptr) |
| 106 |
{ |
| 107 |
libretroFeature.axis = axis; |
| 108 |
|
| 109 |
// Ensure axis is valid |
| 110 |
if (LibretroTranslator::GetAxisID(libretroFeature.axis) < 0) |
| 111 |
{ |
| 112 |
esyslog("<%s> tag has invalid \"%s\" attribute: \"%s\"", BUTTONMAP_XML_ELM_FEATURE, BUTTONMAP_XML_ATTR_FEATURE_AXIS, axis); |
| 113 |
continue; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
m_featureMap[name] = std::move(libretroFeature); |
| 118 |
} |
| 119 |
|
| 120 |
return true; |
| 121 |
} |