| 1 |
/* |
| 2 |
* Copyright (C) 2017-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "GameClientTopology.h" |
| 10 |
|
| 11 |
#include "GameClientDevice.h" |
| 12 |
#include "GameClientPort.h" |
| 13 |
#include "addons/kodi-dev-kit/include/kodi/c-api/addon-instance/game.h" |
| 14 |
#include "games/controllers/Controller.h" |
| 15 |
|
| 16 |
#include <sstream> |
| 17 |
#include <utility> |
| 18 |
|
| 19 |
using namespace KODI; |
| 20 |
using namespace GAME; |
| 21 |
|
| 22 |
#define CONTROLLER_ADDRESS_SEPARATOR '/' |
| 23 |
|
| 24 |
namespace |
| 25 |
{ |
| 26 |
// Helper function |
| 27 |
bool ContainsOnlySeparator(const std::string& address, |
| 28 |
std::string::size_type firstSep, |
| 29 |
std::string::size_type lastSep) |
| 30 |
{ |
| 31 |
if (firstSep == std::string::npos) |
| 32 |
return true; |
| 33 |
|
| 34 |
if (firstSep == lastSep && address[firstSep] == CONTROLLER_ADDRESS_SEPARATOR) |
| 35 |
return true; |
| 36 |
|
| 37 |
return false; |
| 38 |
} |
| 39 |
} // namespace |
| 40 |
|
| 41 |
CGameClientTopology::CGameClientTopology(GameClientPortVec ports, int playerLimit) |
| 42 |
: m_ports(std::move(ports)), m_playerLimit(playerLimit), m_controllers(GetControllerTree(m_ports)) |
| 43 |
{ |
| 44 |
} |
| 45 |
|
| 46 |
void CGameClientTopology::Clear() |
| 47 |
{ |
| 48 |
m_ports.clear(); |
| 49 |
m_controllers.Clear(); |
| 50 |
} |
| 51 |
|
| 52 |
CControllerTree CGameClientTopology::GetControllerTree(const GameClientPortVec& ports) |
| 53 |
{ |
| 54 |
CControllerTree tree; |
| 55 |
|
| 56 |
PortVec controllerPorts; |
| 57 |
for (const GameClientPortPtr& port : ports) |
| 58 |
{ |
| 59 |
CPortNode portNode = GetPortNode(port, ""); |
| 60 |
controllerPorts.emplace_back(std::move(portNode)); |
| 61 |
} |
| 62 |
|
| 63 |
tree.SetPorts(std::move(controllerPorts)); |
| 64 |
|
| 65 |
return tree; |
| 66 |
} |
| 67 |
|
| 68 |
CPortNode CGameClientTopology::GetPortNode(const GameClientPortPtr& port, |
| 69 |
const std::string& controllerAddress) |
| 70 |
{ |
| 71 |
CPortNode portNode; |
| 72 |
|
| 73 |
std::string portAddress = MakeAddress(controllerAddress, port->ID()); |
| 74 |
|
| 75 |
portNode.SetConnected(false); |
| 76 |
portNode.SetPortType(port->PortType()); |
| 77 |
portNode.SetPortID(port->ID()); |
| 78 |
portNode.SetAddress(portAddress); |
| 79 |
portNode.SetForceConnected(port->ForceConnected()); |
| 80 |
|
| 81 |
ControllerNodeVec nodes; |
| 82 |
for (const GameClientDevicePtr& device : port->Devices()) |
| 83 |
{ |
| 84 |
CControllerNode controllerNode = GetControllerNode(device, portAddress); |
| 85 |
nodes.emplace_back(std::move(controllerNode)); |
| 86 |
} |
| 87 |
portNode.SetCompatibleControllers(std::move(nodes)); |
| 88 |
|
| 89 |
return portNode; |
| 90 |
} |
| 91 |
|
| 92 |
CControllerNode CGameClientTopology::GetControllerNode(const GameClientDevicePtr& device, |
| 93 |
const std::string& portAddress) |
| 94 |
{ |
| 95 |
CControllerNode controllerNode; |
| 96 |
|
| 97 |
const std::string controllerAddress = MakeAddress(portAddress, device->Controller()->ID()); |
| 98 |
|
| 99 |
controllerNode.SetController(device->Controller()); |
| 100 |
controllerNode.SetPortAddress(portAddress); |
| 101 |
controllerNode.SetControllerAddress(controllerAddress); |
| 102 |
|
| 103 |
PortVec ports; |
| 104 |
for (const GameClientPortPtr& port : device->Ports()) |
| 105 |
{ |
| 106 |
CPortNode portNode = GetPortNode(port, controllerAddress); |
| 107 |
ports.emplace_back(std::move(portNode)); |
| 108 |
} |
| 109 |
|
| 110 |
CControllerHub controllerHub; |
| 111 |
controllerHub.SetPorts(std::move(ports)); |
| 112 |
controllerNode.SetHub(std::move(controllerHub)); |
| 113 |
|
| 114 |
return controllerNode; |
| 115 |
} |
| 116 |
|
| 117 |
std::string CGameClientTopology::MakeAddress(const std::string& baseAddress, |
| 118 |
const std::string& nodeId) |
| 119 |
{ |
| 120 |
std::ostringstream address; |
| 121 |
|
| 122 |
// Strip leading and trailing slashes from base address |
| 123 |
std::string basePart; |
| 124 |
|
| 125 |
std::string::size_type first = baseAddress.find_first_not_of(CONTROLLER_ADDRESS_SEPARATOR); |
| 126 |
std::string::size_type last = baseAddress.find_last_not_of(CONTROLLER_ADDRESS_SEPARATOR); |
| 127 |
if (!ContainsOnlySeparator(baseAddress, first, last)) |
| 128 |
basePart = baseAddress.substr(first, last - first + 1); |
| 129 |
|
| 130 |
// Strip leading and trailing slashes from node ID |
| 131 |
std::string nodePart; |
| 132 |
|
| 133 |
first = nodeId.find_first_not_of(CONTROLLER_ADDRESS_SEPARATOR); |
| 134 |
last = nodeId.find_last_not_of(CONTROLLER_ADDRESS_SEPARATOR); |
| 135 |
if (!ContainsOnlySeparator(nodeId, first, last)) |
| 136 |
nodePart = nodeId.substr(first, last - first + 1); |
| 137 |
|
| 138 |
// Start with the root port address |
| 139 |
address << ROOT_PORT_ADDRESS; |
| 140 |
|
| 141 |
if (!basePart.empty() && !nodePart.empty()) |
| 142 |
{ |
| 143 |
address << basePart; |
| 144 |
address << CONTROLLER_ADDRESS_SEPARATOR; |
| 145 |
address << nodePart; |
| 146 |
} |
| 147 |
else if (!basePart.empty()) |
| 148 |
{ |
| 149 |
address << basePart; |
| 150 |
} |
| 151 |
else if (!nodePart.empty()) |
| 152 |
{ |
| 153 |
address << nodePart; |
| 154 |
} |
| 155 |
|
| 156 |
return address.str(); |
| 157 |
} |
| 158 |
|
| 159 |
std::pair<std::string, std::string> CGameClientTopology::SplitAddress( |
| 160 |
const std::string& nodeAddress) |
| 161 |
{ |
| 162 |
std::string baseAddress; |
| 163 |
std::string nodeId; |
| 164 |
|
| 165 |
size_t separatorPos = nodeAddress.find_last_of(CONTROLLER_ADDRESS_SEPARATOR); |
| 166 |
if (separatorPos != std::string::npos) |
| 167 |
{ |
| 168 |
baseAddress = nodeAddress.substr(0, separatorPos); |
| 169 |
nodeId = nodeAddress.substr(separatorPos + 1); |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
baseAddress = nodeAddress; |
| 174 |
} |
| 175 |
|
| 176 |
// Add a leading slash, if necessary |
| 177 |
if (baseAddress.empty() || baseAddress[0] != CONTROLLER_ADDRESS_SEPARATOR) |
| 178 |
baseAddress.insert(0, 1, CONTROLLER_ADDRESS_SEPARATOR); |
| 179 |
|
| 180 |
return std::make_pair(baseAddress, nodeId); |
| 181 |
} |