xbmc
GameClientInput.h
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 
48 {
49 public:
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 input helpers
107  void LoadTopology();
108  void SetControllerLayouts(const ControllerVector& controllers);
109  bool OpenJoystick(const std::string& portAddress, const ControllerPtr& controller);
110  void CloseJoysticks(const CPortNode& port, PERIPHERALS::EventLockHandlePtr& inputHandlingLock);
111  void CloseJoystick(const std::string& portAddress,
112  PERIPHERALS::EventLockHandlePtr& inputHandlingLock);
113 
114  // Private callback helpers
115  bool SetRumble(const std::string& portAddress, const std::string& feature, float magnitude);
116 
117  // Helper functions
118  static ControllerVector GetControllers(const CGameClient& gameClient);
119  static void ActivateControllers(CControllerHub& hub);
120 
121  // Input properties
122  IGameInputCallback* m_inputCallback = nullptr;
123  std::unique_ptr<CGameClientTopology> m_topology;
124  using ControllerLayoutMap = std::map<std::string, std::unique_ptr<CGameClientController>>;
125  ControllerLayoutMap m_controllerLayouts;
126 
137  JoystickMap m_joysticks;
138 
142  std::unique_ptr<CPortManager> m_portManager;
143 
150  mutable std::recursive_mutex m_portMutex;
151 
157  std::unique_ptr<CGameClientKeyboard> m_keyboard;
158 
164  std::unique_ptr<CGameClientMouse> m_mouse;
165 
172  std::unique_ptr<CGameClientHardware> m_hardware;
173 };
174 } // namespace GAME
175 } // namespace KODI
Input callbacks.
Definition: GameClientCallbacks.h:22
Base class for game client subsystems.
Definition: GameClientSubsystem.h:39
Definition: GameClientInput.h:47
std::string PortAddress
Definition: GameClientInput.h:51
Collection of nodes that can be connected to this port.
Definition: PortNode.h:28
Interface between Kodi and Game add-ons.
Definition: GameClient.h:116
Definition: AudioDecoder.h:18
std::shared_ptr< CController > ControllerPtr
Smart pointer to a game controller (CController)
Definition: ControllerTypes.h:25
A branch in the controller tree.
Definition: ControllerHub.h:25
Game instance.
Definition: game.h:1228
Definition: game.h:996
Definition: Observer.h:44
std::vector< ControllerPtr > ControllerVector
Vector of smart pointers to a game controller (CController)
Definition: ControllerTypes.h:32