File: addon.cpp

package info (click to toggle)
kodi-peripheral-xarcade 21.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 564 kB
  • sloc: cpp: 392; ansic: 19; makefile: 9
file content (115 lines) | stat: -rw-r--r-- 2,783 bytes parent folder | download | duplicates (2)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 *  Copyright (C) 2016-2021 Garrett Brown
 *  Copyright (C) 2016-2021 Team Kodi
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSE.md for more information.
 */

#define PERIPHERAL_ADDON_JOYSTICKS

#include "addon.h"

#include "utils/CommonMacros.h"
#include "xarcade/XArcadeDefines.h"
#include "xarcade/XArcadeDevice.h"
#include "xarcade/XArcadeScanner.h"
#include "xarcade/XArcadeTypes.h"
#include "xarcade/XArcadeUtils.h"

#include <algorithm>

using namespace XARCADE;

CPeripheralXArcade::CPeripheralXArcade() :
  m_scanner(new CXArcadeScanner)
{
}

ADDON_STATUS CPeripheralXArcade::Create()
{
  return ADDON_STATUS_OK;
}

CPeripheralXArcade::~CPeripheralXArcade() = default;

ADDON_STATUS CPeripheralXArcade::SetSetting(const std::string& settingName, const kodi::addon::CSettingValue& settingValue)
{
  return ADDON_STATUS_OK;
}

void CPeripheralXArcade::GetCapabilities(kodi::addon::PeripheralCapabilities& capabilities)
{
  capabilities.SetProvidesJoysticks(true);
  capabilities.SetProvidesJoystickRumble(false);
  capabilities.SetProvidesJoystickPowerOff(false);
  capabilities.SetProvidesButtonmaps(false);
}

PERIPHERAL_ERROR CPeripheralXArcade::PerformDeviceScan(std::vector<std::shared_ptr<kodi::addon::Peripheral>>& scan_results)
{
  // Close disconnected devices
  m_devices.erase(std::remove_if(m_devices.begin(), m_devices.end(),
    [](const DevicePtr& device)
    {
      return !device->IsOpen();
    }), m_devices.end());

  // Open new devices
  DeviceVector newDevices = m_scanner->GetDevices();
  for (auto& device : newDevices)
  {
    if (device->Open())
      m_devices.emplace_back(std::move(device));
  }

  // Get peripheral info
  JoystickVector joysticks;
  for (auto& device : m_devices)
    device->GetJoystickInfo(joysticks);

  // Upcast array pointers
  std::vector<kodi::addon::Peripheral*> peripherals;
  for (auto& joystick : joysticks)
    scan_results.emplace_back(joystick);

  return PERIPHERAL_NO_ERROR;
}

PERIPHERAL_ERROR CPeripheralXArcade::GetEvents(std::vector<kodi::addon::PeripheralEvent>& events)
{
  for (auto& device : m_devices)
    device->GetEvents(events);

  return PERIPHERAL_NO_ERROR;
}

bool CPeripheralXArcade::SendEvent(const kodi::addon::PeripheralEvent& event)
{
  return false;
}

PERIPHERAL_ERROR CPeripheralXArcade::GetJoystickInfo(unsigned int index, kodi::addon::Joystick& info)
{
  JoystickPtr joystick;

  for (auto& device : m_devices)
  {
    if (device->GetPeripheralIndex(0) == index ||
        device->GetPeripheralIndex(1) == index)
    {
      joystick = device->GetJoystick(index);
      break;
    }
  }

  if (joystick)
  {
    info = *joystick;
    return PERIPHERAL_NO_ERROR;
  }

  return PERIPHERAL_ERROR_NOT_CONNECTED;
}

ADDONCREATOR(CPeripheralXArcade) // Don't touch this!