| 1 |
/* |
| 2 |
* Copyright (C) 2017-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 "ControllerTopology.h" |
| 9 |
#include "InputDefinitions.h" |
| 10 |
#include "InputTranslator.h" |
| 11 |
#include "libretro/LibretroEnvironment.h" |
| 12 |
#include "libretro/LibretroTranslator.h" |
| 13 |
#include "log/Log.h" |
| 14 |
|
| 15 |
#include <tinyxml.h> |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
#include <sstream> |
| 19 |
|
| 20 |
using namespace LIBRETRO; |
| 21 |
|
| 22 |
#define TOPOLOGY_XML "topology.xml" |
| 23 |
|
| 24 |
#define ADDRESS_SEPARATOR '/' |
| 25 |
|
| 26 |
//! @todo Remove me when these are added to the Game API |
| 27 |
#if !defined(KEYBOARD_PORT_ID) |
| 28 |
#define KEYBOARD_PORT_ID "keyboard" |
| 29 |
#endif |
| 30 |
#if !defined(MOUSE_PORT_ID) |
| 31 |
#define MOUSE_PORT_ID "mouse" |
| 32 |
#endif |
| 33 |
|
| 34 |
CControllerTopology& CControllerTopology::GetInstance() |
| 35 |
{ |
| 36 |
static CControllerTopology instance; |
| 37 |
return instance; |
| 38 |
} |
| 39 |
|
| 40 |
bool CControllerTopology::LoadTopology() |
| 41 |
{ |
| 42 |
bool bSuccess = false; |
| 43 |
|
| 44 |
Clear(); |
| 45 |
|
| 46 |
std::string strFilename = CLibretroEnvironment::Get().GetResourcePath(TOPOLOGY_XML); |
| 47 |
if (strFilename.empty()) |
| 48 |
{ |
| 49 |
dsyslog("Could not locate controller topology \"%s\"", TOPOLOGY_XML); |
| 50 |
} |
| 51 |
else |
| 52 |
{ |
| 53 |
dsyslog("Loading controller topology \"%s\"", strFilename.c_str()); |
| 54 |
|
| 55 |
TiXmlDocument topologyXml; |
| 56 |
if (topologyXml.LoadFile(strFilename)) |
| 57 |
{ |
| 58 |
TiXmlElement* pRootElement = topologyXml.RootElement(); |
| 59 |
bSuccess = Deserialize(pRootElement); |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
esyslog("Failed to load controller topology: %s (line %d)", topologyXml.ErrorDesc(), topologyXml.ErrorRow()); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return bSuccess; |
| 68 |
} |
| 69 |
|
| 70 |
void CControllerTopology::Clear() |
| 71 |
{ |
| 72 |
m_ports.clear(); |
| 73 |
} |
| 74 |
|
| 75 |
game_input_topology *CControllerTopology::GetTopology() |
| 76 |
{ |
| 77 |
if (!m_ports.empty()) |
| 78 |
{ |
| 79 |
game_input_topology *topology = new game_input_topology; |
| 80 |
|
| 81 |
int unsigned portCount = 0; |
| 82 |
topology->ports = GetPorts(m_ports, portCount); |
| 83 |
topology->port_count = portCount; |
| 84 |
topology->player_limit = m_playerLimit; |
| 85 |
|
| 86 |
return topology; |
| 87 |
} |
| 88 |
|
| 89 |
return nullptr; |
| 90 |
} |
| 91 |
|
| 92 |
void CControllerTopology::FreeTopology(game_input_topology *topology) |
| 93 |
{ |
| 94 |
if (topology != nullptr) |
| 95 |
FreePorts(topology->ports, topology->port_count); |
| 96 |
|
| 97 |
delete topology; |
| 98 |
} |
| 99 |
|
| 100 |
void CControllerTopology::FreePorts(game_input_port *ports, unsigned int portCount) |
| 101 |
{ |
| 102 |
if (ports != nullptr) |
| 103 |
{ |
| 104 |
for (unsigned int i = 0; i < portCount; i++) |
| 105 |
FreeControllers(ports[i].accepted_devices, ports[i].device_count); |
| 106 |
} |
| 107 |
|
| 108 |
delete[] ports; |
| 109 |
} |
| 110 |
|
| 111 |
unsigned int CControllerTopology::GetPlayerCount(const PortPtr& port) |
| 112 |
{ |
| 113 |
unsigned int playerCount = 0; |
| 114 |
|
| 115 |
const ControllerPtr& controller = GetActiveController(port); |
| 116 |
if (controller) |
| 117 |
playerCount += GetPlayerCount(controller); |
| 118 |
|
| 119 |
return playerCount; |
| 120 |
} |
| 121 |
|
| 122 |
unsigned int CControllerTopology::GetPlayerCount(const ControllerPtr& controller) |
| 123 |
{ |
| 124 |
unsigned int playerCount = 0; |
| 125 |
|
| 126 |
if (controller->bProvidesInput) |
| 127 |
playerCount++; |
| 128 |
|
| 129 |
for (const PortPtr &port : controller->ports) |
| 130 |
playerCount += GetPlayerCount(port); |
| 131 |
|
| 132 |
return playerCount; |
| 133 |
} |
| 134 |
|
| 135 |
int CControllerTopology::GetPortIndex(const std::string &address) const |
| 136 |
{ |
| 137 |
int portIndex = -1; |
| 138 |
unsigned int playerCount = 0; |
| 139 |
|
| 140 |
if (m_ports.empty()) |
| 141 |
{ |
| 142 |
// If topology is unknown, use the first port |
| 143 |
portIndex = 0; |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
for (const auto &port : m_ports) |
| 148 |
{ |
| 149 |
if (port->type == GAME_PORT_CONTROLLER) |
| 150 |
{ |
| 151 |
portIndex = GetPortIndex(port, address, playerCount); |
| 152 |
if (portIndex >= 0) |
| 153 |
break; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// Reset port index if it exceeds the player limit |
| 159 |
if (m_playerLimit >= 0 && portIndex >= m_playerLimit) |
| 160 |
portIndex = -1; |
| 161 |
|
| 162 |
return portIndex; |
| 163 |
} |
| 164 |
|
| 165 |
int CControllerTopology::GetPortIndex(const PortPtr &port, const std::string &portAddress, unsigned int &playerCount) |
| 166 |
{ |
| 167 |
int portIndex = -1; |
| 168 |
|
| 169 |
std::string portId; |
| 170 |
std::string remainingAddress; |
| 171 |
SplitAddress(portAddress, portId, remainingAddress); |
| 172 |
|
| 173 |
if (port->portId == portId) |
| 174 |
{ |
| 175 |
if (remainingAddress.empty()) |
| 176 |
{ |
| 177 |
// Base case |
| 178 |
portIndex = playerCount; |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
const ControllerPtr& controller = GetActiveController(port); |
| 183 |
if (controller) |
| 184 |
{ |
| 185 |
// Player count is incremented as we visit controllers recursively |
| 186 |
portIndex = GetPortIndex(controller, remainingAddress, playerCount); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
else |
| 191 |
{ |
| 192 |
// Player count wasn't incremented by visiting controllers for this port, |
| 193 |
// so calculate and increment it now |
| 194 |
playerCount += GetPlayerCount(port); |
| 195 |
} |
| 196 |
|
| 197 |
return portIndex; |
| 198 |
} |
| 199 |
|
| 200 |
int CControllerTopology::GetPortIndex(const ControllerPtr &controller, const std::string &portAddress, unsigned int &playerCount) |
| 201 |
{ |
| 202 |
int portIndex = -1; |
| 203 |
|
| 204 |
if (controller->bProvidesInput) |
| 205 |
playerCount++; |
| 206 |
|
| 207 |
std::string portControllerId; |
| 208 |
std::string remainingAddress; |
| 209 |
SplitAddress(portAddress, portControllerId, remainingAddress); |
| 210 |
|
| 211 |
if (controller->controllerId == portControllerId) |
| 212 |
{ |
| 213 |
const auto &ports = controller->ports; |
| 214 |
|
| 215 |
for (const auto &port : ports) |
| 216 |
{ |
| 217 |
portIndex = GetPortIndex(port, remainingAddress, playerCount); |
| 218 |
if (portIndex >= 0) |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return portIndex; |
| 224 |
} |
| 225 |
|
| 226 |
bool CControllerTopology::GetConnectionPortIndex(const std::string &address, int &connectionPort) const |
| 227 |
{ |
| 228 |
for (const auto &port : m_ports) |
| 229 |
{ |
| 230 |
if (GetConnectionPortIndex(port, address, connectionPort)) |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
bool CControllerTopology::GetConnectionPortIndex(const PortPtr &port, const std::string &portAddress, int &connectionPort) |
| 238 |
{ |
| 239 |
std::string portId; |
| 240 |
std::string remainingAddress; |
| 241 |
SplitAddress(portAddress, portId, remainingAddress); |
| 242 |
|
| 243 |
if (port->portId == portId) |
| 244 |
{ |
| 245 |
if (remainingAddress.empty()) |
| 246 |
{ |
| 247 |
// Base case |
| 248 |
if (!port->connectionPort.empty()) |
| 249 |
{ |
| 250 |
std::istringstream(port->connectionPort) >> connectionPort; |
| 251 |
return true; |
| 252 |
} |
| 253 |
else |
| 254 |
{ |
| 255 |
return false; |
| 256 |
} |
| 257 |
} |
| 258 |
else |
| 259 |
{ |
| 260 |
const ControllerPtr& controller = GetActiveController(port); |
| 261 |
if (controller) |
| 262 |
{ |
| 263 |
if (GetConnectionPortIndex(controller, remainingAddress, connectionPort)) |
| 264 |
return true; |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
return false; |
| 270 |
} |
| 271 |
|
| 272 |
bool CControllerTopology::GetConnectionPortIndex(const ControllerPtr &controller, const std::string &portAddress, int &connectionPort) |
| 273 |
{ |
| 274 |
std::string portControllerId; |
| 275 |
std::string remainingAddress; |
| 276 |
SplitAddress(portAddress, portControllerId, remainingAddress); |
| 277 |
|
| 278 |
if (controller->controllerId == portControllerId) |
| 279 |
{ |
| 280 |
const auto &ports = controller->ports; |
| 281 |
|
| 282 |
for (const auto &childPort : ports) |
| 283 |
{ |
| 284 |
if (GetConnectionPortIndex(childPort, remainingAddress, connectionPort)) |
| 285 |
return true; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
std::string CControllerTopology::GetAddress(unsigned int portIndex) const |
| 293 |
{ |
| 294 |
std::string address; |
| 295 |
unsigned int playerCount = 0; |
| 296 |
|
| 297 |
if (m_ports.empty()) |
| 298 |
{ |
| 299 |
return DEFAULT_PORT_ID; |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
for (const auto &port : m_ports) |
| 304 |
{ |
| 305 |
if (port->type == GAME_PORT_CONTROLLER) |
| 306 |
{ |
| 307 |
address = GetAddress(port, portIndex, playerCount); |
| 308 |
if (!address.empty()) |
| 309 |
break; |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
return address; |
| 315 |
} |
| 316 |
|
| 317 |
std::string CControllerTopology::GetAddress(const PortPtr &port, unsigned int portIndex, unsigned int &playerCount) |
| 318 |
{ |
| 319 |
std::string address; |
| 320 |
|
| 321 |
if (portIndex == playerCount) |
| 322 |
{ |
| 323 |
// Base case |
| 324 |
address = ADDRESS_SEPARATOR + port->portId; |
| 325 |
} |
| 326 |
else |
| 327 |
{ |
| 328 |
|
| 329 |
const ControllerPtr& controller = GetActiveController(port); |
| 330 |
if (controller) |
| 331 |
{ |
| 332 |
std::string controllerAddress = GetAddress(controller, portIndex, playerCount); |
| 333 |
if (!controllerAddress.empty()) |
| 334 |
address = ADDRESS_SEPARATOR + port->portId + controllerAddress; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
playerCount++; |
| 339 |
|
| 340 |
return address; |
| 341 |
} |
| 342 |
|
| 343 |
std::string CControllerTopology::GetAddress(const ControllerPtr &controller, unsigned int portIndex, unsigned int &playerCount) |
| 344 |
{ |
| 345 |
std::string address; |
| 346 |
|
| 347 |
const auto &ports = controller->ports; |
| 348 |
for (const auto &port : ports) |
| 349 |
{ |
| 350 |
std::string portAddress = GetAddress(port, portIndex, playerCount); |
| 351 |
if (!portAddress.empty()) |
| 352 |
{ |
| 353 |
address = ADDRESS_SEPARATOR + controller->controllerId + portAddress; |
| 354 |
break; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
if (controller->bProvidesInput) |
| 359 |
playerCount++; |
| 360 |
|
| 361 |
return address; |
| 362 |
} |
| 363 |
|
| 364 |
bool CControllerTopology::SetDevice(GAME_PORT_TYPE portType, const std::string &controllerId) |
| 365 |
{ |
| 366 |
for (const auto &port : m_ports) |
| 367 |
{ |
| 368 |
if (port->type == portType) |
| 369 |
{ |
| 370 |
const auto &accepts = port->accepts; |
| 371 |
|
| 372 |
auto it = std::find_if(accepts.begin(), accepts.end(), |
| 373 |
[&controllerId](const ControllerPtr &controller) |
| 374 |
{ |
| 375 |
return controllerId == controller->controllerId; |
| 376 |
}); |
| 377 |
|
| 378 |
if (it != accepts.end()) |
| 379 |
{ |
| 380 |
port->activeId = controllerId; |
| 381 |
return true; |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
void CControllerTopology::RemoveDevice(GAME_PORT_TYPE portType) |
| 390 |
{ |
| 391 |
for (const auto &port : m_ports) |
| 392 |
{ |
| 393 |
if (port->type == portType) |
| 394 |
port->activeId.clear(); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
bool CControllerTopology::SetController(const std::string &portAddress, const std::string &controllerId, bool bProvidesInput) |
| 399 |
{ |
| 400 |
if (m_ports.empty()) |
| 401 |
{ |
| 402 |
// No topology was specified, create one now |
| 403 |
m_ports.emplace_back(CreateDefaultPort(controllerId)); |
| 404 |
} |
| 405 |
|
| 406 |
for (const auto &port : m_ports) |
| 407 |
{ |
| 408 |
if (port->type == GAME_PORT_CONTROLLER) |
| 409 |
{ |
| 410 |
if (SetController(port, portAddress, controllerId, bProvidesInput)) |
| 411 |
return true; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
return false; |
| 416 |
} |
| 417 |
|
| 418 |
bool CControllerTopology::SetController(const PortPtr &port, const std::string &portAddress, const std::string &controllerId, bool bProvidesInput) |
| 419 |
{ |
| 420 |
bool bSuccess = false; |
| 421 |
|
| 422 |
std::string portId; |
| 423 |
std::string remainingAddress; |
| 424 |
SplitAddress(portAddress, portId, remainingAddress); |
| 425 |
|
| 426 |
if (port->portId == portId) |
| 427 |
{ |
| 428 |
const auto &accepts = port->accepts; |
| 429 |
|
| 430 |
if (remainingAddress.empty()) |
| 431 |
{ |
| 432 |
// Base case |
| 433 |
auto it = std::find_if(accepts.begin(), accepts.end(), |
| 434 |
[&controllerId](const ControllerPtr &controller) |
| 435 |
{ |
| 436 |
return controllerId == controller->controllerId; |
| 437 |
}); |
| 438 |
|
| 439 |
if (it != accepts.end()) |
| 440 |
{ |
| 441 |
port->activeId = controllerId; |
| 442 |
(*it)->bProvidesInput = bProvidesInput; |
| 443 |
bSuccess = true; |
| 444 |
} |
| 445 |
} |
| 446 |
else |
| 447 |
{ |
| 448 |
const ControllerPtr& controller = GetActiveController(port); |
| 449 |
if (controller) |
| 450 |
{ |
| 451 |
if (SetController(controller, remainingAddress, controllerId, bProvidesInput)) |
| 452 |
bSuccess = true; |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
return bSuccess; |
| 458 |
} |
| 459 |
|
| 460 |
bool CControllerTopology::SetController(const ControllerPtr &controller, const std::string &portAddress, const std::string &controllerId, bool bProvidesInput) |
| 461 |
{ |
| 462 |
std::string portControllerId; |
| 463 |
std::string remainingAddress; |
| 464 |
SplitAddress(portAddress, portControllerId, remainingAddress); |
| 465 |
|
| 466 |
if (controller->controllerId == portControllerId) |
| 467 |
{ |
| 468 |
const auto &ports = controller->ports; |
| 469 |
for (const auto &port : ports) |
| 470 |
{ |
| 471 |
if (SetController(port, remainingAddress, controllerId, bProvidesInput)) |
| 472 |
return true; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
return false; |
| 477 |
} |
| 478 |
|
| 479 |
void CControllerTopology::RemoveController(const std::string &portAddress) |
| 480 |
{ |
| 481 |
for (const auto &port : m_ports) |
| 482 |
{ |
| 483 |
if (port->type == GAME_PORT_CONTROLLER) |
| 484 |
RemoveController(port, portAddress); |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
void CControllerTopology::RemoveController(const PortPtr &port, const std::string &portAddress) |
| 489 |
{ |
| 490 |
std::string portId; |
| 491 |
std::string remainingAddress; |
| 492 |
SplitAddress(portAddress, portId, remainingAddress); |
| 493 |
|
| 494 |
if (port->portId == portId) |
| 495 |
{ |
| 496 |
if (remainingAddress.empty()) |
| 497 |
{ |
| 498 |
// Base case |
| 499 |
port->activeId.clear(); |
| 500 |
} |
| 501 |
else |
| 502 |
{ |
| 503 |
const ControllerPtr& controller = GetActiveController(port); |
| 504 |
if (controller) |
| 505 |
RemoveController(controller, remainingAddress); |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
void CControllerTopology::RemoveController(const ControllerPtr &controller, const std::string &portAddress) |
| 511 |
{ |
| 512 |
std::string portControllerId; |
| 513 |
std::string remainingAddress; |
| 514 |
SplitAddress(portAddress, portControllerId, remainingAddress); |
| 515 |
|
| 516 |
if (controller->controllerId == portControllerId) |
| 517 |
{ |
| 518 |
const auto &ports = controller->ports; |
| 519 |
for (const auto &port : ports) |
| 520 |
RemoveController(port, remainingAddress); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
libretro_device_t CControllerTopology::TypeOverride(const std::string& portAddress, const std::string& controllerId) const |
| 525 |
{ |
| 526 |
return TypeOverride(m_ports, JoinAddress(portAddress, controllerId)); |
| 527 |
} |
| 528 |
|
| 529 |
libretro_device_t CControllerTopology::TypeOverride(const std::vector<PortPtr>& ports, const std::string &controllerAddress) |
| 530 |
{ |
| 531 |
for (const auto &port : ports) |
| 532 |
{ |
| 533 |
if (port->type == GAME_PORT_CONTROLLER) |
| 534 |
{ |
| 535 |
std::string portId; |
| 536 |
std::string remainingAddress; |
| 537 |
SplitAddress(controllerAddress, portId, remainingAddress); |
| 538 |
|
| 539 |
if (port->portId == portId) |
| 540 |
return TypeOverride(port->accepts, remainingAddress); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
return RETRO_DEVICE_NONE; |
| 545 |
} |
| 546 |
|
| 547 |
libretro_device_t CControllerTopology::TypeOverride(const std::vector<ControllerPtr>& controllers, const std::string &controllerAddress) |
| 548 |
{ |
| 549 |
std::string controllerId; |
| 550 |
std::string remainingAddress; |
| 551 |
SplitAddress(controllerAddress, controllerId, remainingAddress); |
| 552 |
|
| 553 |
if (remainingAddress.empty()) |
| 554 |
{ |
| 555 |
// Base case |
| 556 |
auto it = std::find_if(controllers.begin(), controllers.end(), |
| 557 |
[&controllerId](const ControllerPtr &controller) |
| 558 |
{ |
| 559 |
return controllerId == controller->controllerId; |
| 560 |
}); |
| 561 |
|
| 562 |
if (it != controllers.end()) |
| 563 |
return (*it)->type; |
| 564 |
} |
| 565 |
else |
| 566 |
{ |
| 567 |
for (const auto& controller : controllers) |
| 568 |
{ |
| 569 |
if (controller->controllerId == controllerId) |
| 570 |
return TypeOverride(controller->ports, remainingAddress); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
return RETRO_DEVICE_NONE; |
| 575 |
} |
| 576 |
|
| 577 |
libretro_subclass_t CControllerTopology::SubclassOverride(const std::string& portAddress, const std::string& controllerId) const |
| 578 |
{ |
| 579 |
return SubclassOverride(m_ports, JoinAddress(portAddress, controllerId)); |
| 580 |
} |
| 581 |
|
| 582 |
libretro_subclass_t CControllerTopology::SubclassOverride(const std::vector<PortPtr>& ports, const std::string &controllerAddress) |
| 583 |
{ |
| 584 |
for (const auto &port : ports) |
| 585 |
{ |
| 586 |
if (port->type == GAME_PORT_CONTROLLER) |
| 587 |
{ |
| 588 |
std::string portId; |
| 589 |
std::string remainingAddress; |
| 590 |
SplitAddress(controllerAddress, portId, remainingAddress); |
| 591 |
|
| 592 |
if (port->portId == portId) |
| 593 |
return SubclassOverride(port->accepts, remainingAddress); |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
return RETRO_SUBCLASS_NONE; |
| 598 |
} |
| 599 |
|
| 600 |
libretro_subclass_t CControllerTopology::SubclassOverride(const std::vector<ControllerPtr>& controllers, const std::string &controllerAddress) |
| 601 |
{ |
| 602 |
std::string controllerId; |
| 603 |
std::string remainingAddress; |
| 604 |
SplitAddress(controllerAddress, controllerId, remainingAddress); |
| 605 |
|
| 606 |
if (remainingAddress.empty()) |
| 607 |
{ |
| 608 |
// Base case |
| 609 |
auto it = std::find_if(controllers.begin(), controllers.end(), |
| 610 |
[&controllerId](const ControllerPtr &controller) |
| 611 |
{ |
| 612 |
return controllerId == controller->controllerId; |
| 613 |
}); |
| 614 |
|
| 615 |
if (it != controllers.end()) |
| 616 |
return (*it)->subclass; |
| 617 |
} |
| 618 |
else |
| 619 |
{ |
| 620 |
for (const auto& controller : controllers) |
| 621 |
{ |
| 622 |
if (controller->controllerId == controllerId) |
| 623 |
return SubclassOverride(controller->ports, remainingAddress); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
return RETRO_SUBCLASS_NONE; |
| 628 |
} |
| 629 |
|
| 630 |
bool CControllerTopology::Deserialize(const TiXmlElement* pElement) |
| 631 |
{ |
| 632 |
bool bSuccess = false; |
| 633 |
|
| 634 |
if (pElement == nullptr || |
| 635 |
pElement->ValueStr() != TOPOLOGY_XML_ROOT) |
| 636 |
{ |
| 637 |
esyslog("Can't find root <%s> tag", TOPOLOGY_XML_ROOT); |
| 638 |
} |
| 639 |
else |
| 640 |
{ |
| 641 |
const char* strPlayerLimit = pElement->Attribute(TOPOLOGY_XML_ATTR_PLAYER_LIMIT); |
| 642 |
if (strPlayerLimit != nullptr) |
| 643 |
{ |
| 644 |
std::istringstream ssPlayerLimit(strPlayerLimit); |
| 645 |
ssPlayerLimit >> m_playerLimit; |
| 646 |
} |
| 647 |
|
| 648 |
const TiXmlElement* pChild = pElement->FirstChildElement(TOPOLOGY_XML_ELEM_PORT); |
| 649 |
if (pChild == nullptr) |
| 650 |
{ |
| 651 |
esyslog("Can't find <%s> tag", TOPOLOGY_XML_ELEM_PORT); |
| 652 |
} |
| 653 |
else |
| 654 |
{ |
| 655 |
bSuccess = true; |
| 656 |
|
| 657 |
for ( ; pChild != nullptr; pChild = pChild->NextSiblingElement(TOPOLOGY_XML_ELEM_PORT)) |
| 658 |
{ |
| 659 |
PortPtr port = DeserializePort(pChild); |
| 660 |
|
| 661 |
if (!port) |
| 662 |
{ |
| 663 |
bSuccess = false; |
| 664 |
break; |
| 665 |
} |
| 666 |
|
| 667 |
m_ports.emplace_back(std::move(port)); |
| 668 |
} |
| 669 |
|
| 670 |
if (bSuccess) |
| 671 |
dsyslog("Loaded controller topology with %u ports", m_ports.size()); |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
return bSuccess; |
| 676 |
} |
| 677 |
|
| 678 |
CControllerTopology::PortPtr CControllerTopology::DeserializePort(const TiXmlElement* pElement) |
| 679 |
{ |
| 680 |
PortPtr port; |
| 681 |
|
| 682 |
const char* strPortType = pElement->Attribute(TOPOLOGY_XML_ATTR_PORT_TYPE); |
| 683 |
GAME_PORT_TYPE portType = CInputTranslator::GetPortType(strPortType != nullptr ? strPortType : ""); |
| 684 |
|
| 685 |
//! @todo Remove this hack |
| 686 |
if (portType == GAME_PORT_UNKNOWN) |
| 687 |
portType = GAME_PORT_CONTROLLER; |
| 688 |
|
| 689 |
std::string portId; |
| 690 |
switch (portType) |
| 691 |
{ |
| 692 |
case GAME_PORT_CONTROLLER: |
| 693 |
{ |
| 694 |
const char* strPortId = pElement->Attribute(TOPOLOGY_XML_ATTR_PORT_ID); |
| 695 |
if (strPortId != nullptr) |
| 696 |
portId = strPortId; |
| 697 |
else |
| 698 |
esyslog("<%s> tag is missing attribute \"%s\", can't proceed without port ID", TOPOLOGY_XML_ELEM_PORT, TOPOLOGY_XML_ATTR_PORT_ID); |
| 699 |
break; |
| 700 |
} |
| 701 |
case GAME_PORT_KEYBOARD: |
| 702 |
{ |
| 703 |
portId = KEYBOARD_PORT_ID; |
| 704 |
break; |
| 705 |
} |
| 706 |
case GAME_PORT_MOUSE: |
| 707 |
{ |
| 708 |
portId = MOUSE_PORT_ID; |
| 709 |
break; |
| 710 |
} |
| 711 |
default: |
| 712 |
{ |
| 713 |
esyslog("<%s> tag attribute \"%s\" has unknown value: \"%s\"", TOPOLOGY_XML_ELEM_PORT, TOPOLOGY_XML_ATTR_PORT_TYPE, strPortType != nullptr ? strPortType : ""); |
| 714 |
break; |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
if (!portId.empty()) |
| 719 |
{ |
| 720 |
const char* strConnectionPort = pElement->Attribute(TOPOLOGY_XML_ATTR_CONNECTION_PORT); |
| 721 |
std::string connectionPort = strConnectionPort != nullptr ? strConnectionPort : ""; |
| 722 |
|
| 723 |
const char* strForceConnected = pElement->Attribute(TOPOLOGY_XML_ATTR_FORCE_CONNECTED); |
| 724 |
bool forceConnected = (strForceConnected != nullptr && std::string(strForceConnected) == "true"); |
| 725 |
|
| 726 |
port.reset(new Port{ portType, portId, std::move(connectionPort), forceConnected }); |
| 727 |
|
| 728 |
const TiXmlElement* pChild = pElement->FirstChildElement(TOPOLOGY_XML_ELEM_ACCEPTS); |
| 729 |
if (pChild == nullptr) |
| 730 |
{ |
| 731 |
dsyslog("<%s> tag with ID \"%s\" is missing <%s> node, port won't accept any controllers", TOPOLOGY_XML_ELEM_PORT, portId.c_str(), TOPOLOGY_XML_ELEM_ACCEPTS); |
| 732 |
} |
| 733 |
else |
| 734 |
{ |
| 735 |
for ( ; pChild != nullptr; pChild = pChild->NextSiblingElement(TOPOLOGY_XML_ELEM_ACCEPTS)) |
| 736 |
{ |
| 737 |
ControllerPtr controller = DeserializeController(pChild); |
| 738 |
|
| 739 |
if (!controller) |
| 740 |
{ |
| 741 |
port.reset(); |
| 742 |
break; |
| 743 |
} |
| 744 |
|
| 745 |
port->accepts.emplace_back(std::move(controller)); |
| 746 |
} |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
return port; |
| 751 |
} |
| 752 |
|
| 753 |
CControllerTopology::ControllerPtr CControllerTopology::DeserializeController(const TiXmlElement* pElement) |
| 754 |
{ |
| 755 |
ControllerPtr controller; |
| 756 |
|
| 757 |
const char* strControllerId = pElement->Attribute(TOPOLOGY_XML_ATTR_CONTROLLER_ID); |
| 758 |
if (strControllerId == nullptr) |
| 759 |
{ |
| 760 |
esyslog("<%s> tag is missing attribute \"%s\", can't proceed without controller ID", TOPOLOGY_XML_ELEM_ACCEPTS, TOPOLOGY_XML_ATTR_CONTROLLER_ID); |
| 761 |
} |
| 762 |
else |
| 763 |
{ |
| 764 |
// Device type and subclass overrides |
| 765 |
libretro_device_t type = RETRO_DEVICE_NONE; |
| 766 |
libretro_subclass_t subclass = RETRO_SUBCLASS_NONE; |
| 767 |
|
| 768 |
const char* strType = pElement->Attribute(TOPOLOGY_XML_ATTR_DEVICE_TYPE); |
| 769 |
if (strType != nullptr) |
| 770 |
type = LibretroTranslator::GetDeviceType(strType); |
| 771 |
|
| 772 |
const char* strSubclass = pElement->Attribute(TOPOLOGY_XML_ATTR_DEVICE_SUBCLASS); |
| 773 |
if (strSubclass != nullptr) |
| 774 |
std::istringstream(strSubclass) >> subclass; |
| 775 |
|
| 776 |
controller.reset(new Controller{ strControllerId, {}, {}, type, subclass }); |
| 777 |
|
| 778 |
const TiXmlElement* pChild = pElement->FirstChildElement(TOPOLOGY_XML_ELEM_PORT); |
| 779 |
for ( ; pChild != nullptr; pChild = pChild->NextSiblingElement(TOPOLOGY_XML_ELEM_PORT)) |
| 780 |
{ |
| 781 |
PortPtr port = DeserializePort(pChild); |
| 782 |
|
| 783 |
if (!port) |
| 784 |
{ |
| 785 |
controller.reset(); |
| 786 |
break; |
| 787 |
} |
| 788 |
|
| 789 |
controller->ports.emplace_back(std::move(port)); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
return controller; |
| 794 |
} |
| 795 |
|
| 796 |
game_input_port *CControllerTopology::GetPorts(const std::vector<PortPtr> &portVec, unsigned int &portCount) |
| 797 |
{ |
| 798 |
game_input_port *ports = nullptr; |
| 799 |
|
| 800 |
portCount = static_cast<unsigned int>(portVec.size()); |
| 801 |
if (portCount > 0) |
| 802 |
{ |
| 803 |
ports = new game_input_port[portCount]; |
| 804 |
|
| 805 |
for (unsigned int i = 0; i < portCount; i++) |
| 806 |
{ |
| 807 |
ports[i].type = portVec[i]->type; |
| 808 |
ports[i].port_id = portVec[i]->portId.c_str(); |
| 809 |
ports[i].force_connected = portVec[i]->forceConnected; |
| 810 |
|
| 811 |
unsigned int deviceCount = 0; |
| 812 |
ports[i].accepted_devices = GetControllers(portVec[i]->accepts, deviceCount); |
| 813 |
ports[i].device_count = deviceCount; |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
return ports; |
| 818 |
} |
| 819 |
|
| 820 |
game_input_device *CControllerTopology::GetControllers(const std::vector<ControllerPtr> &controllerVec, unsigned int &deviceCount) |
| 821 |
{ |
| 822 |
game_input_device *devices = nullptr; |
| 823 |
|
| 824 |
deviceCount = static_cast<unsigned int>(controllerVec.size()); |
| 825 |
if (deviceCount > 0) |
| 826 |
{ |
| 827 |
devices = new game_input_device[deviceCount]; |
| 828 |
|
| 829 |
for (unsigned int i = 0; i < deviceCount; i++) |
| 830 |
{ |
| 831 |
devices[i].controller_id = controllerVec[i]->controllerId.c_str(); |
| 832 |
|
| 833 |
unsigned int portCount = 0; |
| 834 |
devices[i].available_ports = GetPorts(controllerVec[i]->ports, portCount); |
| 835 |
devices[i].port_count = portCount; |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
return devices; |
| 840 |
} |
| 841 |
|
| 842 |
void CControllerTopology::FreeControllers(game_input_device *devices, unsigned int deviceCount) |
| 843 |
{ |
| 844 |
for (unsigned int i = 0; i < deviceCount; i++) |
| 845 |
FreePorts(devices[i].available_ports, devices[i].port_count); |
| 846 |
|
| 847 |
delete[] devices; |
| 848 |
} |
| 849 |
|
| 850 |
CControllerTopology::PortPtr CControllerTopology::CreateDefaultPort(const std::string &acceptedController) |
| 851 |
{ |
| 852 |
PortPtr port(new Port{GAME_PORT_CONTROLLER, DEFAULT_PORT_ID, {}, {}}); |
| 853 |
|
| 854 |
port->accepts.emplace_back(new Controller{ acceptedController }); |
| 855 |
|
| 856 |
return port; |
| 857 |
} |
| 858 |
|
| 859 |
const CControllerTopology::ControllerPtr& CControllerTopology::GetActiveController(const PortPtr& port) |
| 860 |
{ |
| 861 |
if (!port->activeId.empty()) |
| 862 |
{ |
| 863 |
const auto &accepts = port->accepts; |
| 864 |
|
| 865 |
auto it = std::find_if(accepts.begin(), accepts.end(), |
| 866 |
[&port](const ControllerPtr &controller) |
| 867 |
{ |
| 868 |
return port->activeId == controller->controllerId; |
| 869 |
}); |
| 870 |
|
| 871 |
if (it != accepts.end()) |
| 872 |
return *it; |
| 873 |
} |
| 874 |
|
| 875 |
static const ControllerPtr empty; |
| 876 |
return empty; |
| 877 |
} |
| 878 |
|
| 879 |
void CControllerTopology::SplitAddress(const std::string &address, std::string &nodeId, std::string &remainingAddress) |
| 880 |
{ |
| 881 |
// Start searching after leading / |
| 882 |
size_t pos = address.find(ADDRESS_SEPARATOR, 1); |
| 883 |
if (pos == std::string::npos) |
| 884 |
{ |
| 885 |
// Skip leading / to extract node ID |
| 886 |
nodeId = address.substr(1); |
| 887 |
remainingAddress.clear(); |
| 888 |
} |
| 889 |
else |
| 890 |
{ |
| 891 |
// Skip leading / to extract node ID |
| 892 |
nodeId = address.substr(1, pos - 1); |
| 893 |
remainingAddress = address.substr(pos); |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
std::string CControllerTopology::JoinAddress(const std::string& address, const std::string& nodeId) |
| 898 |
{ |
| 899 |
return address + ADDRESS_SEPARATOR + nodeId; |
| 900 |
} |