summaryrefslogtreecommitdiffstats
path: root/xbmc/games/addons/input/GameClientPort.cpp
blob: c582fff9fc4357165ae962e50f2ef5fe89405bcd (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
/*
 *  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 "GameClientPort.h"

#include "GameClientDevice.h"
#include "addons/kodi-dev-kit/include/kodi/addon-instance/Game.h"
#include "games/addons/GameClientTranslator.h"
#include "games/controllers/Controller.h"
#include "games/controllers/input/PhysicalTopology.h"
#include "utils/StringUtils.h"

#include <algorithm>

using namespace KODI;
using namespace GAME;

CGameClientPort::CGameClientPort(const game_input_port& port)
  : m_type(CGameClientTranslator::TranslatePortType(port.type)),
    m_portId(port.port_id ? port.port_id : ""),
    m_forceConnected(port.force_connected)
{
  if (port.accepted_devices != nullptr)
  {
    for (unsigned int i = 0; i < port.device_count; i++)
    {
      std::unique_ptr<CGameClientDevice> device(new CGameClientDevice(port.accepted_devices[i]));

      if (device->Controller() != CController::EmptyPtr)
        m_acceptedDevices.emplace_back(std::move(device));
    }
  }
}

CGameClientPort::CGameClientPort(const ControllerVector& controllers)
  : m_type(PORT_TYPE::CONTROLLER), m_portId(DEFAULT_PORT_ID)
{
  for (const auto& controller : controllers)
    m_acceptedDevices.emplace_back(new CGameClientDevice(controller));
}

CGameClientPort::CGameClientPort(const game_input_port& logicalPort,
                                 const CPhysicalPort& physicalPort)
  : m_type(PORT_TYPE::CONTROLLER),
    m_portId(physicalPort.ID()),
    m_forceConnected(logicalPort.force_connected)
{
  if (logicalPort.accepted_devices != nullptr)
  {
    for (unsigned int i = 0; i < logicalPort.device_count; i++)
    {
      // Ensure device is physically compatible
      const game_input_device& deviceStruct = logicalPort.accepted_devices[i];
      std::string controllerId = deviceStruct.controller_id ? deviceStruct.controller_id : "";

      if (physicalPort.IsCompatible(controllerId))
      {
        std::unique_ptr<CGameClientDevice> device(new CGameClientDevice(deviceStruct));

        if (device->Controller() != CController::EmptyPtr)
          m_acceptedDevices.emplace_back(std::move(device));
      }
    }
  }
}

CGameClientPort::~CGameClientPort() = default;