| 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 "ControllerNode.h" |
| 10 |
|
| 11 |
#include "ControllerHub.h" |
| 12 |
#include "games/controllers/Controller.h" |
| 13 |
#include "games/controllers/input/PhysicalTopology.h" |
| 14 |
#include "games/ports/types/PortNode.h" |
| 15 |
|
| 16 |
#include <algorithm> |
| 17 |
#include <memory> |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
using namespace KODI; |
| 21 |
using namespace GAME; |
| 22 |
|
| 23 |
CControllerNode::CControllerNode() : m_hub(new CControllerHub) |
| 24 |
{ |
| 25 |
} |
| 26 |
|
| 27 |
CControllerNode::~CControllerNode() = default; |
| 28 |
|
| 29 |
CControllerNode& CControllerNode::operator=(const CControllerNode& rhs) |
| 30 |
{ |
| 31 |
if (this != &rhs) |
| 32 |
{ |
| 33 |
m_controller = rhs.m_controller; |
| 34 |
m_portAddress = rhs.m_portAddress; |
| 35 |
m_controllerAddress = rhs.m_controllerAddress; |
| 36 |
m_hub = std::make_unique<CControllerHub>(*rhs.m_hub); |
| 37 |
} |
| 38 |
|
| 39 |
return *this; |
| 40 |
} |
| 41 |
|
| 42 |
CControllerNode& CControllerNode::operator=(CControllerNode&& rhs) noexcept |
| 43 |
{ |
| 44 |
if (this != &rhs) |
| 45 |
{ |
| 46 |
m_controller = std::move(rhs.m_controller); |
| 47 |
m_portAddress = std::move(rhs.m_portAddress); |
| 48 |
m_controllerAddress = std::move(rhs.m_controllerAddress); |
| 49 |
m_hub = std::move(rhs.m_hub); |
| 50 |
} |
| 51 |
|
| 52 |
return *this; |
| 53 |
} |
| 54 |
|
| 55 |
void CControllerNode::Clear() |
| 56 |
{ |
| 57 |
m_controller.reset(); |
| 58 |
m_portAddress.clear(); |
| 59 |
m_controllerAddress.clear(); |
| 60 |
m_hub = std::make_unique<CControllerHub>(); |
| 61 |
} |
| 62 |
|
| 63 |
void CControllerNode::SetController(ControllerPtr controller) |
| 64 |
{ |
| 65 |
m_controller = std::move(controller); |
| 66 |
} |
| 67 |
|
| 68 |
void CControllerNode::GetControllers(ControllerVector& controllers) const |
| 69 |
{ |
| 70 |
if (m_controller) |
| 71 |
{ |
| 72 |
const ControllerPtr& myController = m_controller; |
| 73 |
|
| 74 |
auto it = std::find_if(controllers.begin(), controllers.end(), |
| 75 |
[&myController](const ControllerPtr& controller) |
| 76 |
{ return myController->ID() == controller->ID(); }); |
| 77 |
|
| 78 |
if (it == controllers.end()) |
| 79 |
controllers.emplace_back(m_controller); |
| 80 |
} |
| 81 |
|
| 82 |
m_hub->GetControllers(controllers); |
| 83 |
} |
| 84 |
|
| 85 |
void CControllerNode::SetPortAddress(std::string portAddress) |
| 86 |
{ |
| 87 |
m_portAddress = std::move(portAddress); |
| 88 |
} |
| 89 |
|
| 90 |
void CControllerNode::SetControllerAddress(std::string controllerAddress) |
| 91 |
{ |
| 92 |
m_controllerAddress = std::move(controllerAddress); |
| 93 |
} |
| 94 |
|
| 95 |
void CControllerNode::SetHub(CControllerHub hub) |
| 96 |
{ |
| 97 |
m_hub = std::make_unique<CControllerHub>(std::move(hub)); |
| 98 |
} |
| 99 |
|
| 100 |
bool CControllerNode::IsControllerAccepted(const std::string& controllerId) const |
| 101 |
{ |
| 102 |
bool bAccepted = false; |
| 103 |
|
| 104 |
for (const auto& port : m_hub->GetPorts()) |
| 105 |
{ |
| 106 |
if (port.IsControllerAccepted(controllerId)) |
| 107 |
{ |
| 108 |
bAccepted = true; |
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return bAccepted; |
| 114 |
} |
| 115 |
|
| 116 |
bool CControllerNode::IsControllerAccepted(const std::string& portAddress, |
| 117 |
const std::string& controllerId) const |
| 118 |
{ |
| 119 |
bool bAccepted = false; |
| 120 |
|
| 121 |
for (const auto& port : m_hub->GetPorts()) |
| 122 |
{ |
| 123 |
if (port.IsControllerAccepted(portAddress, controllerId)) |
| 124 |
{ |
| 125 |
bAccepted = true; |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return bAccepted; |
| 131 |
} |
| 132 |
|
| 133 |
bool CControllerNode::ProvidesInput() const |
| 134 |
{ |
| 135 |
return m_controller && m_controller->Topology().ProvidesInput(); |
| 136 |
} |
| 137 |
|
| 138 |
void CControllerNode::GetInputPorts(std::vector<std::string>& inputPorts) const |
| 139 |
{ |
| 140 |
if (ProvidesInput()) |
| 141 |
inputPorts.emplace_back(m_portAddress); |
| 142 |
|
| 143 |
m_hub->GetInputPorts(inputPorts); |
| 144 |
} |