| 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 |
#pragma once |
| 10 |
|
| 11 |
#include "games/addons/GameClientSubsystem.h" |
| 12 |
#include "games/controllers/ControllerTypes.h" |
| 13 |
#include "games/controllers/types/ControllerTree.h" |
| 14 |
#include "peripherals/PeripheralTypes.h" |
| 15 |
#include "utils/Observer.h" |
| 16 |
|
| 17 |
#include <map> |
| 18 |
#include <memory> |
| 19 |
#include <mutex> |
| 20 |
#include <string> |
| 21 |
|
| 22 |
class CCriticalSection; |
| 23 |
struct game_input_event; |
| 24 |
|
| 25 |
namespace KODI |
| 26 |
{ |
| 27 |
namespace JOYSTICK |
| 28 |
{ |
| 29 |
class IInputProvider; |
| 30 |
} |
| 31 |
|
| 32 |
namespace GAME |
| 33 |
{ |
| 34 |
class CGameClient; |
| 35 |
class CGameClientController; |
| 36 |
class CGameClientHardware; |
| 37 |
class CGameClientJoystick; |
| 38 |
class CGameClientKeyboard; |
| 39 |
class CGameClientMouse; |
| 40 |
class CGameClientTopology; |
| 41 |
class CPortManager; |
| 42 |
class IGameInputCallback; |
| 43 |
|
| 44 |
/*! |
| 45 |
* \ingroup games |
| 46 |
*/ |
| 47 |
class CGameClientInput : protected CGameClientSubsystem, public Observable |
| 48 |
{ |
| 49 |
public: |
| 50 |
//! @todo de-duplicate |
| 51 |
using PortAddress = std::string; |
| 52 |
using JoystickMap = std::map<PortAddress, std::shared_ptr<CGameClientJoystick>>; |
| 53 |
|
| 54 |
CGameClientInput(CGameClient& gameClient, |
| 55 |
AddonInstance_Game& addonStruct, |
| 56 |
CCriticalSection& clientAccess); |
| 57 |
~CGameClientInput() override; |
| 58 |
|
| 59 |
void Initialize(); |
| 60 |
void Deinitialize(); |
| 61 |
|
| 62 |
void Start(IGameInputCallback* input); |
| 63 |
void Stop(); |
| 64 |
|
| 65 |
// Input functions |
| 66 |
bool HasFeature(const std::string& controllerId, const std::string& featureName) const; |
| 67 |
bool AcceptsInput() const; |
| 68 |
bool InputEvent(const game_input_event& event); |
| 69 |
float GetPortActivation(const std::string& portAddress); |
| 70 |
|
| 71 |
// Topology functions |
| 72 |
const CControllerTree& GetDefaultControllerTree() const; |
| 73 |
CControllerTree GetActiveControllerTree() const; |
| 74 |
bool SupportsKeyboard() const; |
| 75 |
bool SupportsMouse() const; |
| 76 |
int GetPlayerLimit() const; |
| 77 |
bool ConnectController(const std::string& portAddress, const ControllerPtr& controller); |
| 78 |
bool DisconnectController(const std::string& portAddress); |
| 79 |
void SavePorts(); |
| 80 |
void ResetPorts(); |
| 81 |
|
| 82 |
// Joystick functions |
| 83 |
const JoystickMap& GetJoystickMap() const { return m_joysticks; } |
| 84 |
void CloseJoysticks(PERIPHERALS::EventLockHandlePtr& inputHandlingLock); |
| 85 |
|
| 86 |
// Keyboard functions |
| 87 |
bool OpenKeyboard(const ControllerPtr& controller, const PERIPHERALS::PeripheralPtr& keyboard); |
| 88 |
bool IsKeyboardOpen() const; |
| 89 |
void CloseKeyboard(); |
| 90 |
|
| 91 |
// Mouse functions |
| 92 |
bool OpenMouse(const ControllerPtr& controller, const PERIPHERALS::PeripheralPtr& mouse); |
| 93 |
bool IsMouseOpen() const; |
| 94 |
void CloseMouse(); |
| 95 |
|
| 96 |
// Agent functions |
| 97 |
bool HasAgent() const; |
| 98 |
|
| 99 |
// Hardware input functions |
| 100 |
void HardwareReset(); |
| 101 |
|
| 102 |
// Input callbacks |
| 103 |
bool ReceiveInputEvent(const game_input_event& eventStruct); |
| 104 |
|
| 105 |
private: |
| 106 |
// Private topology helpers |
| 107 |
void LoadTopology(); |
| 108 |
void SetControllerLayouts(const ControllerVector& controllers); |
| 109 |
bool DisconnectControllerRecursive(const std::string& portAddress); |
| 110 |
|
| 111 |
// Private input helpers |
| 112 |
bool OpenJoystick(const std::string& portAddress, const ControllerPtr& controller); |
| 113 |
void CloseJoysticks(const CPortNode& port, PERIPHERALS::EventLockHandlePtr& inputHandlingLock); |
| 114 |
void CloseJoystick(const std::string& portAddress, |
| 115 |
PERIPHERALS::EventLockHandlePtr& inputHandlingLock); |
| 116 |
|
| 117 |
// Private callback helpers |
| 118 |
bool SetRumble(const std::string& portAddress, const std::string& feature, float magnitude); |
| 119 |
|
| 120 |
// Helper functions |
| 121 |
static ControllerVector GetControllers(const CGameClient& gameClient); |
| 122 |
static void ActivateControllers(CControllerHub& hub); |
| 123 |
void ResetPorts(const PortVec& port); |
| 124 |
|
| 125 |
// Input properties |
| 126 |
IGameInputCallback* m_inputCallback = nullptr; |
| 127 |
std::unique_ptr<CGameClientTopology> m_topology; |
| 128 |
using ControllerLayoutMap = std::map<std::string, std::unique_ptr<CGameClientController>>; |
| 129 |
ControllerLayoutMap m_controllerLayouts; |
| 130 |
|
| 131 |
/*! |
| 132 |
* \brief Map of port address to joystick handler |
| 133 |
* |
| 134 |
* The port address is a string that identifies the adress of the port. |
| 135 |
* |
| 136 |
* The joystick handler connects to joystick input of the game client. |
| 137 |
* |
| 138 |
* This property is always populated with the default joystick configuration |
| 139 |
* (i.e. all ports are connected to the first controller they accept). |
| 140 |
*/ |
| 141 |
JoystickMap m_joysticks; |
| 142 |
|
| 143 |
/*! |
| 144 |
* \brief Serializable port state |
| 145 |
*/ |
| 146 |
std::unique_ptr<CPortManager> m_portManager; |
| 147 |
|
| 148 |
/*! |
| 149 |
* \brief Mutex for port state |
| 150 |
* |
| 151 |
* Mutex is recursive to allow for management of several ports within the |
| 152 |
* function ResetPorts(). |
| 153 |
*/ |
| 154 |
mutable std::recursive_mutex m_portMutex; |
| 155 |
|
| 156 |
/*! |
| 157 |
* \brief Keyboard handler |
| 158 |
* |
| 159 |
* This connects to the keyboard input of the game client. |
| 160 |
*/ |
| 161 |
std::unique_ptr<CGameClientKeyboard> m_keyboard; |
| 162 |
|
| 163 |
/*! |
| 164 |
* \brief Mouse handler |
| 165 |
* |
| 166 |
* This connects to the mouse input of the game client. |
| 167 |
*/ |
| 168 |
std::unique_ptr<CGameClientMouse> m_mouse; |
| 169 |
|
| 170 |
/*! |
| 171 |
* \brief Hardware input handler |
| 172 |
* |
| 173 |
* This connects to input from game console hardware belonging to the game |
| 174 |
* client. |
| 175 |
*/ |
| 176 |
std::unique_ptr<CGameClientHardware> m_hardware; |
| 177 |
}; |
| 178 |
} // namespace GAME |
| 179 |
} // namespace KODI |