| 1 |
/* |
| 2 |
* Copyright (C) 2017-2021 Team Kodi (https://kodi.tv) |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
* See LICENSE.md for more information. |
| 6 |
*/ |
| 7 |
|
| 8 |
#pragma once |
| 9 |
|
| 10 |
#include "input/InputTypes.h" |
| 11 |
|
| 12 |
#include <kodi/addon-instance/Game.h> |
| 13 |
#include <libretro-common/libretro.h> |
| 14 |
|
| 15 |
#include <memory> |
| 16 |
#include <string> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
class TiXmlElement; |
| 20 |
|
| 21 |
namespace LIBRETRO |
| 22 |
{ |
| 23 |
class CControllerTopology |
| 24 |
{ |
| 25 |
public: |
| 26 |
CControllerTopology() = default; |
| 27 |
|
| 28 |
static CControllerTopology& GetInstance(); |
| 29 |
|
| 30 |
bool LoadTopology(); |
| 31 |
|
| 32 |
void Clear(); |
| 33 |
|
| 34 |
game_input_topology* GetTopology(); |
| 35 |
|
| 36 |
static void FreeTopology(game_input_topology *topology); |
| 37 |
|
| 38 |
static void FreePorts(game_input_port *ports, unsigned int portCount); |
| 39 |
|
| 40 |
int GetPortIndex(const std::string &address) const; |
| 41 |
|
| 42 |
bool GetConnectionPortIndex(const std::string &address, int &connectionPortId) const; |
| 43 |
|
| 44 |
std::string GetAddress(unsigned int portIndex) const; |
| 45 |
|
| 46 |
bool SetDevice(GAME_PORT_TYPE portType, const std::string &controllerId); |
| 47 |
void RemoveDevice(GAME_PORT_TYPE portType); |
| 48 |
|
| 49 |
bool SetController(const std::string &portAddress, const std::string &controllerId, bool bProvidesInput); |
| 50 |
void RemoveController(const std::string &portAddress); |
| 51 |
|
| 52 |
libretro_device_t TypeOverride(const std::string &portAddress, const std::string &controllerId) const; |
| 53 |
libretro_subclass_t SubclassOverride(const std::string &portAddress, const std::string &controllerId) const; |
| 54 |
|
| 55 |
int PlayerLimit() const { return m_playerLimit; } |
| 56 |
|
| 57 |
private: |
| 58 |
struct Port; |
| 59 |
using PortPtr = std::unique_ptr<Port>; |
| 60 |
|
| 61 |
struct Controller; |
| 62 |
using ControllerPtr = std::unique_ptr<Controller>; |
| 63 |
|
| 64 |
bool Deserialize(const TiXmlElement* pElement); |
| 65 |
PortPtr DeserializePort(const TiXmlElement* pElement); |
| 66 |
ControllerPtr DeserializeController(const TiXmlElement* pElement); |
| 67 |
|
| 68 |
static game_input_port *GetPorts(const std::vector<PortPtr> &portVec, unsigned int &portCount); |
| 69 |
static game_input_device *GetControllers(const std::vector<ControllerPtr> &controllerVec, unsigned int &deviceCount); |
| 70 |
static void FreeControllers(game_input_device *devices, unsigned int deviceCount); |
| 71 |
|
| 72 |
struct Port |
| 73 |
{ |
| 74 |
GAME_PORT_TYPE type; |
| 75 |
std::string portId; |
| 76 |
std::string connectionPort; // Empty if no connection port is specified in topology.xml |
| 77 |
bool forceConnected = false; |
| 78 |
std::vector<ControllerPtr> accepts; |
| 79 |
std::string activeId; // Empty if disconnected |
| 80 |
}; |
| 81 |
|
| 82 |
struct Controller |
| 83 |
{ |
| 84 |
std::string controllerId; |
| 85 |
std::vector<PortPtr> ports; |
| 86 |
bool bProvidesInput; |
| 87 |
libretro_device_t type = RETRO_DEVICE_NONE; |
| 88 |
libretro_subclass_t subclass = RETRO_SUBCLASS_NONE; |
| 89 |
}; |
| 90 |
|
| 91 |
static unsigned int GetPlayerCount(const PortPtr& port); |
| 92 |
static unsigned int GetPlayerCount(const ControllerPtr& controller); |
| 93 |
|
| 94 |
static int GetPortIndex(const PortPtr &port, const std::string &portAddress, unsigned int &playerCount); |
| 95 |
static int GetPortIndex(const ControllerPtr &controller, const std::string &portAddress, unsigned int &playerCount); |
| 96 |
|
| 97 |
static bool GetConnectionPortIndex(const PortPtr &port, const std::string &portAddress, int &connectionPort); |
| 98 |
static bool GetConnectionPortIndex(const ControllerPtr &controller, const std::string &portAddress, int &connectionPort); |
| 99 |
|
| 100 |
static std::string GetAddress(const PortPtr &port, unsigned int portIndex, unsigned int &playerCount); |
| 101 |
static std::string GetAddress(const ControllerPtr &controller, unsigned int portIndex, unsigned int &playerCount); |
| 102 |
|
| 103 |
static bool SetController(const PortPtr &port, const std::string &portAddress, const std::string &controllerId, bool bProvidesInput); |
| 104 |
static void RemoveController(const PortPtr &port, const std::string &portAddress); |
| 105 |
|
| 106 |
static bool SetController(const ControllerPtr &controller, const std::string &portAddress, const std::string &controllerId, bool bProvidesInput); |
| 107 |
static void RemoveController(const ControllerPtr &controller, const std::string &portAddress); |
| 108 |
|
| 109 |
static libretro_device_t TypeOverride(const std::vector<PortPtr>& ports, const std::string& controllerAddress); |
| 110 |
static libretro_subclass_t SubclassOverride(const std::vector<PortPtr>& ports, const std::string& controllerAddress); |
| 111 |
|
| 112 |
static libretro_device_t TypeOverride(const std::vector<ControllerPtr>& controllers, const std::string& controllerAddress); |
| 113 |
static libretro_subclass_t SubclassOverride(const std::vector<ControllerPtr>& controllers, const std::string &controllerAddress); |
| 114 |
|
| 115 |
static PortPtr CreateDefaultPort(const std::string &acceptedController); |
| 116 |
|
| 117 |
static const ControllerPtr& GetActiveController(const PortPtr& port); |
| 118 |
|
| 119 |
static void SplitAddress(const std::string &address, std::string &nodeId, std::string &remainingAddress); |
| 120 |
static std::string JoinAddress(const std::string& address, const std::string& nodeId); |
| 121 |
|
| 122 |
std::vector<PortPtr> m_ports; |
| 123 |
int m_playerLimit = -1; |
| 124 |
}; |
| 125 |
} |