summaryrefslogtreecommitdiffstats
path: root/xbmc/games/addons/input/GameClientDevice.cpp
blob: 589fb66cb4eba0dd67660596281b9a876a63f496 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 *  Copyright (C) 2017-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "GameClientDevice.h"

#include "GameClientPort.h"
#include "ServiceBroker.h"
#include "addons/kodi-dev-kit/include/kodi/addon-instance/Game.h"
#include "games/GameServices.h"
#include "games/controllers/Controller.h"
#include "games/controllers/input/PhysicalTopology.h"
#include "utils/StringUtils.h"
#include "utils/log.h"

#include <algorithm>

using namespace KODI;
using namespace GAME;

CGameClientDevice::CGameClientDevice(const game_input_device& device)
  : m_controller(GetController(device.controller_id))
{
  if (m_controller && device.available_ports != nullptr)
  {
    // Look for matching ports. We enumerate in physical order because logical
    // order can change per emulator.
    for (const auto& physicalPort : m_controller->Topology().Ports())
    {
      for (unsigned int i = 0; i < device.port_count; i++)
      {
        const auto& logicalPort = device.available_ports[i];
        if (logicalPort.port_id != nullptr && logicalPort.port_id == physicalPort.ID())
        {
          // Handle matching ports
          AddPort(logicalPort, physicalPort);
          break;
        }
      }
    }
  }
}

CGameClientDevice::CGameClientDevice(const ControllerPtr& controller) : m_controller(controller)
{
}

CGameClientDevice::~CGameClientDevice() = default;

void CGameClientDevice::AddPort(const game_input_port& logicalPort,
                                const CPhysicalPort& physicalPort)
{
  std::unique_ptr<CGameClientPort> port(new CGameClientPort(logicalPort, physicalPort));
  m_ports.emplace_back(std::move(port));
}

ControllerPtr CGameClientDevice::GetController(const char* controllerId)
{
  ControllerPtr controller;

  if (controllerId != nullptr)
  {
    controller = CServiceBroker::GetGameServices().GetController(controllerId);
    if (!controller)
      CLog::Log(LOGERROR, "Invalid controller ID: {}", controllerId);
  }

  return controller;
}