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 <string>
20 
21 class CCriticalSection;
22 struct game_input_event;
23 
24 namespace KODI
25 {
26 namespace JOYSTICK
27 {
28 class IInputProvider;
29 }
30 
31 namespace GAME
32 {
33 class CGameClient;
34 class CGameClientController;
35 class CGameClientHardware;
36 class CGameClientJoystick;
37 class CGameClientKeyboard;
38 class CGameClientMouse;
39 class CGameClientTopology;
40 class CPortManager;
41 class IGameInputCallback;
42 
44 {
45 public:
47  using PortAddress = std::string;
48  using JoystickMap = std::map<PortAddress, std::shared_ptr<CGameClientJoystick>>;
49 
50  CGameClientInput(CGameClient& gameClient,
51  AddonInstance_Game& addonStruct,
52  CCriticalSection& clientAccess);
53  ~CGameClientInput() override;
54 
55  void Initialize();
56  void Deinitialize();
57 
58  void Start(IGameInputCallback* input);
59  void Stop();
60 
61  // Input functions
62  bool HasFeature(const std::string& controllerId, const std::string& featureName) const;
63  bool AcceptsInput() const;
64  bool InputEvent(const game_input_event& event);
65 
66  // Topology functions
67  const CControllerTree& GetDefaultControllerTree() const;
68  const CControllerTree& GetActiveControllerTree() const;
69  bool SupportsKeyboard() const;
70  bool SupportsMouse() const;
71  int GetPlayerLimit() const;
72  bool ConnectController(const std::string& portAddress, const ControllerPtr& controller);
73  bool DisconnectController(const std::string& portAddress);
74  void SavePorts();
75  void ResetPorts();
76 
77  // Joystick functions
78  const JoystickMap& GetJoystickMap() const { return m_joysticks; }
79  void CloseJoysticks(PERIPHERALS::EventLockHandlePtr& inputHandlingLock);
80 
81  // Keyboard functions
82  bool OpenKeyboard(const ControllerPtr& controller, const PERIPHERALS::PeripheralPtr& keyboard);
83  bool IsKeyboardOpen() const;
84  void CloseKeyboard();
85 
86  // Mouse functions
87  bool OpenMouse(const ControllerPtr& controller, const PERIPHERALS::PeripheralPtr& mouse);
88  bool IsMouseOpen() const;
89  void CloseMouse();
90 
91  // Agent functions
92  bool HasAgent() const;
93 
94  // Hardware input functions
95  void HardwareReset();
96 
97  // Input callbacks
98  bool ReceiveInputEvent(const game_input_event& eventStruct);
99 
100 private:
101  // Private input helpers
102  void LoadTopology();
103  void SetControllerLayouts(const ControllerVector& controllers);
104  bool OpenJoystick(const std::string& portAddress, const ControllerPtr& controller);
105  void CloseJoysticks(const CPortNode& port, PERIPHERALS::EventLockHandlePtr& inputHandlingLock);
106  void CloseJoystick(const std::string& portAddress,
107  PERIPHERALS::EventLockHandlePtr& inputHandlingLock);
108 
109  // Private callback helpers
110  bool SetRumble(const std::string& portAddress, const std::string& feature, float magnitude);
111 
112  // Helper functions
113  static ControllerVector GetControllers(const CGameClient& gameClient);
114  static void ActivateControllers(CControllerHub& hub);
115 
116  // Input properties
117  IGameInputCallback* m_inputCallback = nullptr;
118  std::unique_ptr<CGameClientTopology> m_topology;
119  using ControllerLayoutMap = std::map<std::string, std::unique_ptr<CGameClientController>>;
120  ControllerLayoutMap m_controllerLayouts;
121 
132  JoystickMap m_joysticks;
133 
134  // TODO: Guard with a mutex
135  std::unique_ptr<CPortManager> m_portManager;
136 
142  std::unique_ptr<CGameClientKeyboard> m_keyboard;
143 
149  std::unique_ptr<CGameClientMouse> m_mouse;
150 
157  std::unique_ptr<CGameClientHardware> m_hardware;
158 };
159 } // namespace GAME
160 } // namespace KODI
Input callbacks.
Definition: GameClientCallbacks.h:20
Base class for game client subsystems.
Definition: GameClientSubsystem.h:37
Definition: GameClientInput.h:43
std::string PortAddress
Definition: GameClientInput.h:47
Collection of nodes that can be connected to this port.
Definition: PortNode.h:26
Interface between Kodi and Game add-ons.
Definition: GameClient.h:115
Controller configuration window.
Definition: AudioDecoder.h:18
A branch in the controller tree.
Definition: ControllerHub.h:23
Game instance.
Definition: game.h:1223
Definition: game.h:996
Definition: Observer.h:44