| 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 "ButtonMapper.h" |
| 9 |
#include "DefaultControllerTranslator.h" |
| 10 |
#include "DefaultKeyboardTranslator.h" |
| 11 |
#include "InputDefinitions.h" |
| 12 |
#include "LibretroDevice.h" |
| 13 |
#include "libretro/LibretroDLL.h" |
| 14 |
#include "libretro/LibretroEnvironment.h" |
| 15 |
#include "libretro/LibretroTranslator.h" |
| 16 |
#include "libretro-common/libretro.h" |
| 17 |
#include "log/Log.h" |
| 18 |
|
| 19 |
#include <tinyxml.h> |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
#include <sstream> |
| 23 |
|
| 24 |
#define BUTTONMAP_XML "buttonmap.xml" |
| 25 |
#define BUTTONMAP_XML_VERSION 2 |
| 26 |
#define BUTTONMAP_XML_MIN_VERSION 2 |
| 27 |
|
| 28 |
using namespace LIBRETRO; |
| 29 |
|
| 30 |
CButtonMapper::CButtonMapper(void) |
| 31 |
: m_bLoadAttempted(false) |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
CButtonMapper& CButtonMapper::Get(void) |
| 36 |
{ |
| 37 |
static CButtonMapper _instance; |
| 38 |
return _instance; |
| 39 |
} |
| 40 |
|
| 41 |
bool CButtonMapper::LoadButtonMap(void) |
| 42 |
{ |
| 43 |
bool bSuccess = false; |
| 44 |
|
| 45 |
m_devices.clear(); |
| 46 |
|
| 47 |
std::string strFilename = CLibretroEnvironment::Get().GetResourcePath(BUTTONMAP_XML); |
| 48 |
if (strFilename.empty()) |
| 49 |
{ |
| 50 |
esyslog("Could not locate buttonmap \"%s\"", BUTTONMAP_XML); |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
dsyslog("Loading libretro buttonmap %s", strFilename.c_str()); |
| 55 |
|
| 56 |
TiXmlDocument buttonMapXml; |
| 57 |
if (!buttonMapXml.LoadFile(strFilename)) |
| 58 |
{ |
| 59 |
esyslog("Failed to open file: %s (line %d)", buttonMapXml.ErrorDesc(), buttonMapXml.ErrorRow()); |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
TiXmlElement* pRootElement = buttonMapXml.RootElement(); |
| 64 |
bSuccess = Deserialize(pRootElement); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return bSuccess; |
| 69 |
} |
| 70 |
|
| 71 |
libretro_device_t CButtonMapper::GetLibretroType(const std::string& strControllerId) |
| 72 |
{ |
| 73 |
// Handle default controller unless it appears in buttonmap.xml |
| 74 |
if (strControllerId == DEFAULT_CONTROLLER_ID && !HasController(DEFAULT_CONTROLLER_ID)) |
| 75 |
return RETRO_DEVICE_ANALOG; |
| 76 |
|
| 77 |
// Handle default keyboard unless it appears in buttonmap.xml |
| 78 |
if (strControllerId == DEFAULT_KEYBOARD_ID && !HasController(DEFAULT_KEYBOARD_ID)) |
| 79 |
return RETRO_DEVICE_KEYBOARD; |
| 80 |
|
| 81 |
libretro_device_t deviceType = RETRO_DEVICE_NONE; |
| 82 |
|
| 83 |
// Check buttonmap for other controllers |
| 84 |
DeviceIt it = GetDevice(m_devices, strControllerId); |
| 85 |
if (it != m_devices.end()) |
| 86 |
deviceType = (*it)->Type(); |
| 87 |
|
| 88 |
return deviceType; |
| 89 |
} |
| 90 |
|
| 91 |
libretro_subclass_t CButtonMapper::GetSubclass(const std::string& strControllerId) |
| 92 |
{ |
| 93 |
// Handle default controller unless it appears in buttonmap.xml |
| 94 |
if (strControllerId == DEFAULT_CONTROLLER_ID && !HasController(DEFAULT_CONTROLLER_ID)) |
| 95 |
return RETRO_SUBCLASS_NONE; |
| 96 |
|
| 97 |
// Handle default keyboard unless it appears in buttonmap.xml |
| 98 |
if (strControllerId == DEFAULT_KEYBOARD_ID && !HasController(DEFAULT_KEYBOARD_ID)) |
| 99 |
return RETRO_SUBCLASS_NONE; |
| 100 |
|
| 101 |
libretro_subclass_t subclass = RETRO_SUBCLASS_NONE; |
| 102 |
|
| 103 |
// Check buttonmap for other controllers |
| 104 |
DeviceIt it = GetDevice(m_devices, strControllerId); |
| 105 |
if (it != m_devices.end()) |
| 106 |
subclass = (*it)->Subclass(); |
| 107 |
|
| 108 |
return subclass; |
| 109 |
} |
| 110 |
|
| 111 |
int CButtonMapper::GetLibretroIndex(const std::string& strControllerId, const std::string& strFeatureName) |
| 112 |
{ |
| 113 |
if (!strControllerId.empty() && !strFeatureName.empty()) |
| 114 |
{ |
| 115 |
// Handle default controller unless it appears in buttonmap.xml |
| 116 |
if (strControllerId == DEFAULT_CONTROLLER_ID && !HasController(DEFAULT_CONTROLLER_ID)) |
| 117 |
return CDefaultControllerTranslator::GetLibretroIndex(strFeatureName); |
| 118 |
|
| 119 |
// Handle default keyboard unless it appears in buttonmap.xml |
| 120 |
if (strControllerId == DEFAULT_KEYBOARD_ID && !HasController(DEFAULT_KEYBOARD_ID)) |
| 121 |
return CDefaultKeyboardTranslator::GetLibretroIndex(strFeatureName); |
| 122 |
|
| 123 |
// Check buttonmap for other controllers |
| 124 |
std::string mapto = GetFeature(strControllerId, strFeatureName); |
| 125 |
if (!mapto.empty()) |
| 126 |
return LibretroTranslator::GetFeatureIndex(mapto); |
| 127 |
} |
| 128 |
|
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
libretro_device_t CButtonMapper::GetLibretroDevice(const std::string& strControllerId, const std::string& strFeatureName) const |
| 133 |
{ |
| 134 |
if (!strControllerId.empty() && !strFeatureName.empty()) |
| 135 |
{ |
| 136 |
std::string mapto = GetFeature(strControllerId, strFeatureName); |
| 137 |
if (!mapto.empty()) |
| 138 |
return LibretroTranslator::GetLibretroDevice(mapto); |
| 139 |
} |
| 140 |
|
| 141 |
return RETRO_DEVICE_NONE; |
| 142 |
} |
| 143 |
|
| 144 |
int CButtonMapper::GetAxisID(const std::string& strControllerId, const std::string& strFeatureName) const |
| 145 |
{ |
| 146 |
if (!strControllerId.empty() && !strFeatureName.empty()) |
| 147 |
{ |
| 148 |
std::string axis = GetAxis(strControllerId, strFeatureName); |
| 149 |
if (!axis.empty()) |
| 150 |
return LibretroTranslator::GetAxisID(axis); |
| 151 |
} |
| 152 |
|
| 153 |
return -1; |
| 154 |
} |
| 155 |
|
| 156 |
std::string CButtonMapper::GetControllerFeature(const std::string& strControllerId, const std::string& strLibretroFeature) |
| 157 |
{ |
| 158 |
std::string feature; |
| 159 |
|
| 160 |
if (!strControllerId.empty() && !strLibretroFeature.empty()) |
| 161 |
{ |
| 162 |
// Handle default controller unless it appears in buttonmap.xml |
| 163 |
if (strControllerId == DEFAULT_CONTROLLER_ID && !HasController(DEFAULT_CONTROLLER_ID)) |
| 164 |
return CDefaultControllerTranslator::GetControllerFeature(strLibretroFeature); |
| 165 |
|
| 166 |
// No need to handle keyboard, keyboard input doesn't go from libretro to frontend |
| 167 |
|
| 168 |
DeviceIt it = GetDevice(m_devices, strControllerId); |
| 169 |
if (it != m_devices.end()) |
| 170 |
{ |
| 171 |
const DevicePtr &device = *it; |
| 172 |
|
| 173 |
const FeatureMap& features = device->Features(); |
| 174 |
for (auto& featurePair : features) |
| 175 |
{ |
| 176 |
const std::string& controllerFeature = featurePair.first; |
| 177 |
const FeatureMapItem& libretroFeature = featurePair.second; |
| 178 |
|
| 179 |
if (libretroFeature.feature == strLibretroFeature) |
| 180 |
{ |
| 181 |
feature = controllerFeature; |
| 182 |
break; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
return feature; |
| 189 |
} |
| 190 |
|
| 191 |
bool CButtonMapper::HasController(const std::string& strControllerId) const |
| 192 |
{ |
| 193 |
bool bFound = false; |
| 194 |
|
| 195 |
DeviceIt it = GetDevice(m_devices, strControllerId); |
| 196 |
if (it != m_devices.end()) |
| 197 |
bFound = true; |
| 198 |
|
| 199 |
return bFound; |
| 200 |
} |
| 201 |
|
| 202 |
std::string CButtonMapper::GetFeature(const std::string& strControllerId, const std::string& strFeatureName) const |
| 203 |
{ |
| 204 |
std::string mapto; |
| 205 |
|
| 206 |
DeviceIt it = GetDevice(m_devices, strControllerId); |
| 207 |
if (it != m_devices.end()) |
| 208 |
{ |
| 209 |
const DevicePtr &device = *it; |
| 210 |
|
| 211 |
const FeatureMap& features = device->Features(); |
| 212 |
for (auto& featurePair : features) |
| 213 |
{ |
| 214 |
const std::string& controllerFeature = featurePair.first; |
| 215 |
const std::string& libretroFeature = featurePair.second.feature; |
| 216 |
|
| 217 |
if (controllerFeature == strFeatureName) |
| 218 |
{ |
| 219 |
mapto = libretroFeature; |
| 220 |
break; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
return mapto; |
| 226 |
} |
| 227 |
|
| 228 |
std::string CButtonMapper::GetAxis(const std::string& strControllerId, const std::string& strFeatureName) const |
| 229 |
{ |
| 230 |
std::string axis; |
| 231 |
|
| 232 |
for (auto& device : m_devices) |
| 233 |
{ |
| 234 |
if (device->ControllerID() == strControllerId) |
| 235 |
{ |
| 236 |
const FeatureMap& features = device->Features(); |
| 237 |
for (auto& featurePair : features) |
| 238 |
{ |
| 239 |
const std::string& controllerFeature = featurePair.first; |
| 240 |
const std::string& libretroAxis = featurePair.second.axis; |
| 241 |
|
| 242 |
if (controllerFeature == strFeatureName) |
| 243 |
{ |
| 244 |
axis = libretroAxis; |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
break; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return axis; |
| 253 |
} |
| 254 |
|
| 255 |
bool CButtonMapper::Deserialize(TiXmlElement* pElement) |
| 256 |
{ |
| 257 |
bool bSuccess = false; |
| 258 |
|
| 259 |
if (pElement == nullptr || |
| 260 |
pElement->ValueStr() != BUTTONMAP_XML_ROOT) |
| 261 |
{ |
| 262 |
esyslog("Can't find root <%s> tag", BUTTONMAP_XML_ROOT); |
| 263 |
} |
| 264 |
else |
| 265 |
{ |
| 266 |
// Assume v1 if unspecified |
| 267 |
unsigned int version = 1; |
| 268 |
|
| 269 |
const char* strVersion = pElement->Attribute(BUTTONMAP_XML_ATTR_VERSION); |
| 270 |
if (strVersion == nullptr) |
| 271 |
{ |
| 272 |
esyslog("Buttonmap version required, expected version %u (min=%u)", |
| 273 |
BUTTONMAP_XML_VERSION, BUTTONMAP_XML_MIN_VERSION); |
| 274 |
} |
| 275 |
else |
| 276 |
{ |
| 277 |
std::istringstream ss(strVersion); |
| 278 |
ss >> version; |
| 279 |
if (version < BUTTONMAP_XML_MIN_VERSION) |
| 280 |
{ |
| 281 |
esyslog("Buttonmap with version %u too old, expected version %u (min=%u)", |
| 282 |
version, BUTTONMAP_XML_VERSION, BUTTONMAP_XML_MIN_VERSION); |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
dsyslog("Detected buttonmap version %u", version); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if (version >= BUTTONMAP_XML_MIN_VERSION) |
| 291 |
{ |
| 292 |
const TiXmlElement* pChild = pElement->FirstChildElement(BUTTONMAP_XML_ELM_CONTROLLER); |
| 293 |
if (!pChild) |
| 294 |
{ |
| 295 |
esyslog("Can't find <%s> tag", BUTTONMAP_XML_ELM_CONTROLLER); |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
bSuccess = true; |
| 300 |
|
| 301 |
for ( ; pChild != nullptr; pChild = pChild->NextSiblingElement(BUTTONMAP_XML_ELM_CONTROLLER)) |
| 302 |
{ |
| 303 |
DevicePtr device(std::make_shared<CLibretroDevice>()); |
| 304 |
|
| 305 |
if (!device->Deserialize(pChild, version)) |
| 306 |
{ |
| 307 |
bSuccess = false; |
| 308 |
break; |
| 309 |
} |
| 310 |
|
| 311 |
m_devices.emplace_back(std::move(device)); |
| 312 |
} |
| 313 |
|
| 314 |
if (bSuccess) |
| 315 |
dsyslog("Loaded buttonmap at version %u", version); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
return bSuccess; |
| 321 |
} |
| 322 |
|
| 323 |
CButtonMapper::DeviceIt CButtonMapper::GetDevice(const DeviceVector &devices, |
| 324 |
const std::string &controllerId) |
| 325 |
{ |
| 326 |
return std::find_if(devices.begin(), devices.end(), |
| 327 |
[&controllerId](const DevicePtr &device) |
| 328 |
{ |
| 329 |
return device->ControllerID() == controllerId; |
| 330 |
}); |
| 331 |
} |