xbmc
GameAgentManager.h
1 /*
2  * Copyright (C) 2017-2022 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 #pragma once
9 
10 #include "games/GameTypes.h"
11 #include "input/keyboard/interfaces/IKeyboardDriverHandler.h"
12 #include "input/mouse/interfaces/IMouseDriverHandler.h"
13 #include "peripherals/PeripheralTypes.h"
14 #include "utils/Observer.h"
15 
16 #include <map>
17 #include <memory>
18 #include <mutex>
19 #include <set>
20 #include <string>
21 
22 class CInputManager;
23 
24 namespace PERIPHERALS
25 {
26 class CPeripherals;
27 } // namespace PERIPHERALS
28 
29 namespace KODI
30 {
31 namespace JOYSTICK
32 {
33 class IInputProvider;
34 }
35 
36 namespace GAME
37 {
38 class CGameClient;
39 class CGameClientJoystick;
40 
56  public Observer,
59 {
60 public:
61  CGameAgentManager(PERIPHERALS::CPeripherals& peripheralManager, CInputManager& inputManager);
62 
63  virtual ~CGameAgentManager();
64 
65  // Lifecycle functions
66  void Start(GameClientPtr gameClient);
67  void Stop();
68  void Refresh();
69 
70  // Implementation of Observer
71  void Notify(const Observable& obs, const ObservableMessage msg) override;
72 
73  // Implementation of IKeyboardDriverHandler
74  bool OnKeyPress(const CKey& key) override;
75  void OnKeyRelease(const CKey& key) override {}
76 
77  // Implementation of IMouseDriverHandler
78  bool OnPosition(int x, int y) override;
79  bool OnButtonPress(MOUSE::BUTTON_ID button) override;
80  void OnButtonRelease(MOUSE::BUTTON_ID button) override {}
81 
82  // Public interface
83  GameAgentVec GetAgents() const;
84  std::string GetPortAddress(JOYSTICK::IInputProvider* inputProvider) const;
85  std::vector<std::string> GetInputPorts() const;
86  float GetPortActivation(const std::string& address) const;
87  float GetPeripheralActivation(const std::string& peripheralLocation) const;
88 
89 private:
91  using PortAddress = std::string;
92  using JoystickMap = std::map<PortAddress, std::shared_ptr<CGameClientJoystick>>;
93  using PortMap = std::map<JOYSTICK::IInputProvider*, std::shared_ptr<CGameClientJoystick>>;
94 
95  using PeripheralLocation = std::string;
96  using CurrentPortMap = std::map<PortAddress, PeripheralLocation>;
97  using CurrentPeripheralMap = std::map<PeripheralLocation, PortAddress>;
98 
99  using ControllerAddress = std::string;
100  using PeripheralMap = std::map<ControllerAddress, PERIPHERALS::PeripheralPtr>;
101 
102  // Internal interface
103  void ProcessJoysticks(PERIPHERALS::EventLockHandlePtr& inputHandlingLock);
104  void ProcessKeyboard();
105  void ProcessMouse();
106 
107  // Internal helpers
108  void ProcessAgents(const PERIPHERALS::PeripheralVector& joysticks,
109  PERIPHERALS::EventLockHandlePtr& inputHandlingLock);
110  void UpdateExpiredJoysticks(const PERIPHERALS::PeripheralVector& joysticks,
111  PERIPHERALS::EventLockHandlePtr& inputHandlingLock);
112  void UpdateConnectedJoysticks(const PERIPHERALS::PeripheralVector& joysticks,
113  const PortMap& newPortMap,
114  PERIPHERALS::EventLockHandlePtr& inputHandlingLock,
115  std::set<PERIPHERALS::PeripheralPtr>& disconnectedJoysticks);
116 
117  // Static functionals
118  static PortMap MapJoysticks(const PERIPHERALS::PeripheralVector& peripheralJoysticks,
119  const JoystickMap& gameClientjoysticks,
120  CurrentPortMap& currentPorts,
121  CurrentPeripheralMap& currentPeripherals,
122  int playerLimit);
123  static void MapJoystick(PERIPHERALS::PeripheralPtr peripheralJoystick,
124  std::shared_ptr<CGameClientJoystick> gameClientJoystick,
125  PortMap& result);
126  static void LogPeripheralMap(const PeripheralMap& peripheralMap,
127  const std::set<PERIPHERALS::PeripheralPtr>& disconnectedPeripherals);
128 
129  // Construction parameters
130  PERIPHERALS::CPeripherals& m_peripheralManager;
131  CInputManager& m_inputManager;
132 
133  // State parameters
134  GameClientPtr m_gameClient;
135  bool m_bHasKeyboard = false;
136  bool m_bHasMouse = false;
137  GameAgentVec m_agents;
138 
139  // Synchronization parameters
140  mutable std::mutex m_agentMutex;
141 
154  PortMap m_portMap;
155 
161  CurrentPortMap m_currentPorts;
162 
168  CurrentPeripheralMap m_currentPeripherals;
169 
175  PeripheralMap m_peripheralMap;
176 
182  std::set<PERIPHERALS::PeripheralPtr> m_disconnectedPeripherals;
183 };
184 } // namespace GAME
185 } // namespace KODI
std::vector< GameAgentPtr > GameAgentVec
Vector of smart pointers to game-playing agents (CGameAgent)
Definition: GameTypes.h:78
Main input processing class.
Definition: InputManager.h:63
Interface for classes that can provide input.
Definition: IInputProvider.h:21
Definition: RetroPlayerInput.h:15
Class to manage game-playing agents for a running game client.
Definition: GameAgentManager.h:55
void OnKeyRelease(const CKey &key) override
A key has been released.
Definition: GameAgentManager.h:75
Definition: AudioDecoder.h:18
Definition: Observer.h:31
std::shared_ptr< CGameClient > GameClientPtr
Smart pointer to a game client (CGameClient)
Definition: GameTypes.h:29
Interface for handling mouse driver events.
Definition: IMouseDriverHandler.h:21
void OnButtonRelease(MOUSE::BUTTON_ID button) override
A mouse button has been released.
Definition: GameAgentManager.h:80
Interface for handling keyboard events.
Definition: IKeyboardDriverHandler.h:21
Definition: Observer.h:44
Definition: Key.h:135
Definition: Peripherals.h:53