| 1 |
/* |
| 2 |
* Copyright (C) 2017-2021 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 "ControllerHub.h" |
| 10 |
|
| 11 |
#include "ControllerTree.h" |
| 12 |
#include "games/controllers/Controller.h" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <utility> |
| 16 |
|
| 17 |
using namespace KODI; |
| 18 |
using namespace GAME; |
| 19 |
|
| 20 |
CControllerHub::~CControllerHub() = default; |
| 21 |
|
| 22 |
CControllerHub& CControllerHub::operator=(const CControllerHub& rhs) |
| 23 |
{ |
| 24 |
if (this != &rhs) |
| 25 |
{ |
| 26 |
m_ports = rhs.m_ports; |
| 27 |
} |
| 28 |
|
| 29 |
return *this; |
| 30 |
} |
| 31 |
|
| 32 |
CControllerHub& CControllerHub::operator=(CControllerHub&& rhs) noexcept |
| 33 |
{ |
| 34 |
if (this != &rhs) |
| 35 |
{ |
| 36 |
m_ports = std::move(rhs.m_ports); |
| 37 |
} |
| 38 |
|
| 39 |
return *this; |
| 40 |
} |
| 41 |
|
| 42 |
void CControllerHub::Clear() |
| 43 |
{ |
| 44 |
m_ports.clear(); |
| 45 |
} |
| 46 |
|
| 47 |
void CControllerHub::SetPorts(PortVec ports) |
| 48 |
{ |
| 49 |
m_ports = std::move(ports); |
| 50 |
} |
| 51 |
|
| 52 |
bool CControllerHub::IsControllerAccepted(const std::string& controllerId) const |
| 53 |
{ |
| 54 |
return std::any_of(m_ports.begin(), m_ports.end(), |
| 55 |
[controllerId](const CPortNode& port) |
| 56 |
{ return port.IsControllerAccepted(controllerId); }); |
| 57 |
} |
| 58 |
|
| 59 |
bool CControllerHub::IsControllerAccepted(const std::string& portAddress, |
| 60 |
const std::string& controllerId) const |
| 61 |
{ |
| 62 |
return std::any_of(m_ports.begin(), m_ports.end(), |
| 63 |
[portAddress, controllerId](const CPortNode& port) |
| 64 |
{ return port.IsControllerAccepted(portAddress, controllerId); }); |
| 65 |
} |
| 66 |
|
| 67 |
ControllerVector CControllerHub::GetControllers() const |
| 68 |
{ |
| 69 |
ControllerVector controllers; |
| 70 |
GetControllers(controllers); |
| 71 |
return controllers; |
| 72 |
} |
| 73 |
|
| 74 |
void CControllerHub::GetControllers(ControllerVector& controllers) const |
| 75 |
{ |
| 76 |
for (const CPortNode& port : m_ports) |
| 77 |
{ |
| 78 |
for (const CControllerNode& node : port.GetCompatibleControllers()) |
| 79 |
node.GetControllers(controllers); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
const CPortNode& CControllerHub::GetPort(const std::string& address) const |
| 84 |
{ |
| 85 |
return GetPortInternal(m_ports, address); |
| 86 |
} |
| 87 |
|
| 88 |
const CPortNode& CControllerHub::GetPortInternal(const PortVec& ports, const std::string& address) |
| 89 |
{ |
| 90 |
for (const CPortNode& port : ports) |
| 91 |
{ |
| 92 |
// Base case |
| 93 |
if (port.GetAddress() == address) |
| 94 |
return port; |
| 95 |
|
| 96 |
// Check children |
| 97 |
for (const CControllerNode& controller : port.GetCompatibleControllers()) |
| 98 |
{ |
| 99 |
const CPortNode& port = GetPortInternal(controller.GetHub().GetPorts(), address); |
| 100 |
if (port.GetAddress() == address) |
| 101 |
return port; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// Not found |
| 106 |
static const CPortNode empty{}; |
| 107 |
return empty; |
| 108 |
} |
| 109 |
|
| 110 |
void CControllerHub::GetInputPorts(std::vector<std::string>& inputPorts) const |
| 111 |
{ |
| 112 |
for (const CPortNode& port : m_ports) |
| 113 |
port.GetInputPorts(inputPorts); |
| 114 |
} |