| 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 "GameClientDevice.h" |
| 10 |
|
| 11 |
#include "GameClientPort.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "addons/kodi-dev-kit/include/kodi/addon-instance/Game.h" |
| 14 |
#include "games/GameServices.h" |
| 15 |
#include "games/controllers/Controller.h" |
| 16 |
#include "games/controllers/input/PhysicalTopology.h" |
| 17 |
#include "utils/StringUtils.h" |
| 18 |
#include "utils/log.h" |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
|
| 22 |
using namespace KODI; |
| 23 |
using namespace GAME; |
| 24 |
|
| 25 |
CGameClientDevice::CGameClientDevice(const game_input_device& device) |
| 26 |
: m_controller(GetController(device.controller_id)) |
| 27 |
{ |
| 28 |
if (m_controller && device.available_ports != nullptr) |
| 29 |
{ |
| 30 |
// Look for matching ports. We enumerate in physical order because logical |
| 31 |
// order can change per emulator. |
| 32 |
for (const auto& physicalPort : m_controller->Topology().Ports()) |
| 33 |
{ |
| 34 |
for (unsigned int i = 0; i < device.port_count; i++) |
| 35 |
{ |
| 36 |
const auto& logicalPort = device.available_ports[i]; |
| 37 |
if (logicalPort.port_id != nullptr && logicalPort.port_id == physicalPort.ID()) |
| 38 |
{ |
| 39 |
// Handle matching ports |
| 40 |
AddPort(logicalPort, physicalPort); |
| 41 |
break; |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
CGameClientDevice::CGameClientDevice(const ControllerPtr& controller) : m_controller(controller) |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
CGameClientDevice::~CGameClientDevice() = default; |
| 53 |
|
| 54 |
void CGameClientDevice::AddPort(const game_input_port& logicalPort, |
| 55 |
const CPhysicalPort& physicalPort) |
| 56 |
{ |
| 57 |
std::unique_ptr<CGameClientPort> port(new CGameClientPort(logicalPort, physicalPort)); |
| 58 |
m_ports.emplace_back(std::move(port)); |
| 59 |
} |
| 60 |
|
| 61 |
ControllerPtr CGameClientDevice::GetController(const char* controllerId) |
| 62 |
{ |
| 63 |
ControllerPtr controller; |
| 64 |
|
| 65 |
if (controllerId != nullptr) |
| 66 |
{ |
| 67 |
controller = CServiceBroker::GetGameServices().GetController(controllerId); |
| 68 |
if (!controller) |
| 69 |
CLog::Log(LOGERROR, "Invalid controller ID: {}", controllerId); |
| 70 |
} |
| 71 |
|
| 72 |
return controller; |
| 73 |
} |