Debian Codegraph @

xbmc/games/agents/input/AgentInput.h

raw
1 /*
2 * Copyright (C) 2017-2024 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 "AgentController.h"
11 #include "games/GameTypes.h"
12 #include "input/keyboard/interfaces/IKeyboardDriverHandler.h"
13 #include "input/mouse/interfaces/IMouseDriverHandler.h"
14 #include "peripherals/PeripheralTypes.h"
15 #include "utils/Observer.h"
16
17 #include <map>
18 #include <memory>
19 #include <mutex>
20 #include <set>
21 #include <string>
22 #include <vector>
23
24 class CInputManager;
25
26 namespace PERIPHERALS
27 {
28 class CPeripherals;
29 } // namespace PERIPHERALS
30
31 namespace KODI
32 {
33 namespace JOYSTICK
34 {
35 class IInputProvider;
36 }
37 namespace KEYBOARD
38 {
39 class IKeyboardInputProvider;
40 }
41 namespace MOUSE
42 {
43 class IMouseInputProvider;
44 }
45
46 namespace GAME
47 {
48 class CGameClient;
49 class CGameClientJoystick;
50
51 /*!
52 * \ingroup games
53 *
54 * \brief Class to manage game-playing agents for a running game client
55 *
56 * Currently, port mapping is controller-based and does not take into account
57 * the human belonging to the controller. In the future, humans and possibly
58 * bots will be managed here.
59 *
60 * To map ports to controllers, a list of controllers is retrieved in
61 * ProcessJoysticks(). After expired controllers are removed, the port mapping
62 * occurs in the static function MapJoysticks(). The strategy is to simply
63 * sort controllers by heuristics and greedily assign to game ports.
64 */
65 class CAgentInput : public Observable,
66 public Observer,
67 KEYBOARD::IKeyboardDriverHandler,
68 MOUSE::IMouseDriverHandler
69 {
70 public:
71 CAgentInput(PERIPHERALS::CPeripherals& peripheralManager, CInputManager& inputManager);
72
73 virtual ~CAgentInput();
74
75 // Lifecycle functions
76 void Start(GameClientPtr gameClient);
77 void Stop();
78 void Refresh();
79
80 // Implementation of Observer
81 void Notify(const Observable& obs, const ObservableMessage msg) override;
82
83 // Implementation of IKeyboardDriverHandler
84 bool OnKeyPress(const CKey& key) override;
85 void OnKeyRelease(const CKey& key) override {}
86
87 // Implementation of IMouseDriverHandler
88 bool OnPosition(int x, int y) override;
89 bool OnButtonPress(MOUSE::BUTTON_ID button) override;
90 void OnButtonRelease(MOUSE::BUTTON_ID button) override {}
91
92 // Public interface
93 std::vector<std::shared_ptr<const CAgentController>> GetControllers() const;
94 std::string GetPortAddress(JOYSTICK::IInputProvider* inputProvider) const;
95 std::string GetKeyboardAddress(KEYBOARD::IKeyboardInputProvider* inputProvider) const;
96 std::string GetMouseAddress(MOUSE::IMouseInputProvider* inputProvider) const;
97 std::vector<std::string> GetGameInputPorts() const;
98 float GetGamePortActivation(const std::string& address) const;
99 float GetPeripheralActivation(const std::string& peripheralLocation) const;
100
101 private:
102 //! @todo De-duplicate these types
103 using PortAddress = std::string;
104 using JoystickMap = std::map<PortAddress, std::shared_ptr<CGameClientJoystick>>;
105 using PortMap = std::map<JOYSTICK::IInputProvider*, std::shared_ptr<CGameClientJoystick>>;
106
107 using PeripheralLocation = std::string;
108 using CurrentPortMap = std::map<PortAddress, PeripheralLocation>;
109 using CurrentPeripheralMap = std::map<PeripheralLocation, PortAddress>;
110
111 using ControllerAddress = std::string;
112 using PeripheralMap = std::map<ControllerAddress, PERIPHERALS::PeripheralPtr>;
113
114 // Internal interface
115 void ProcessJoysticks(PERIPHERALS::EventLockHandlePtr& inputHandlingLock);
116 void ProcessKeyboard();
117 void ProcessMouse();
118
119 // Internal helpers
120 void ProcessAgentControllers(const PERIPHERALS::PeripheralVector& peripherals,
121 PERIPHERALS::EventLockHandlePtr& inputHandlingLock,
122 bool updateJoysticks);
123 void UpdateExpiredJoysticks(const PERIPHERALS::PeripheralVector& joysticks,
124 PERIPHERALS::EventLockHandlePtr& inputHandlingLock);
125 void UpdateConnectedJoysticks(const PERIPHERALS::PeripheralVector& joysticks,
126 const PortMap& newPortMap,
127 PERIPHERALS::EventLockHandlePtr& inputHandlingLock,
128 std::set<PERIPHERALS::PeripheralPtr>& disconnectedJoysticks);
129
130 // Static functionals
131 static PortMap MapJoysticks(const PERIPHERALS::PeripheralVector& peripheralJoysticks,
132 const JoystickMap& gameClientjoysticks,
133 CurrentPortMap& currentPorts,
134 CurrentPeripheralMap& currentPeripherals,
135 int playerLimit);
136 static void MapJoystick(PERIPHERALS::PeripheralPtr peripheralJoystick,
137 std::shared_ptr<CGameClientJoystick> gameClientJoystick,
138 PortMap& result);
139 static void LogPeripheralMap(const PeripheralMap& peripheralMap,
140 const std::set<PERIPHERALS::PeripheralPtr>& disconnectedPeripherals);
141
142 // Construction parameters
143 PERIPHERALS::CPeripherals& m_peripheralManager;
144 CInputManager& m_inputManager;
145
146 // State parameters
147 GameClientPtr m_gameClient;
148 bool m_bHasKeyboard = false;
149 bool m_bHasMouse = false;
150 int m_initialMouseX{-1};
151 int m_initialMouseY{-1};
152 std::vector<std::shared_ptr<CAgentController>> m_controllers;
153
154 // Synchronization parameters
155 mutable std::mutex m_controllerMutex;
156
157 /*!
158 * \brief Map of joystick input provider to joystick handler
159 *
160 * The input provider is a handle to agent input.
161 *
162 * The joystick handler connects to joystick input of the game client.
163 *
164 * This property remembers which joysticks are actually being controlled by
165 * agents.
166 *
167 * Not exposed to the game.
168 */
169 PortMap m_portMap;
170
171 /*!
172 * \brief Map of keyboard input provider to keyboard port address
173 */
174 std::map<KEYBOARD::IKeyboardInputProvider*, PortAddress> m_keyboardPort;
175
176 /*!
177 * \brief Map of mouse input provider to mouse port address
178 */
179 std::map<MOUSE::IMouseInputProvider*, PortAddress> m_mousePort;
180
181 /*!
182 * \brief Map of the current ports to their peripheral
183 *
184 * This allows attempt to preserve player numbers.
185 */
186 CurrentPortMap m_currentPorts;
187
188 /*!
189 * \brief Map of the current peripherals to their port
190 *
191 * This allows attempt to preserve player numbers.
192 */
193 CurrentPeripheralMap m_currentPeripherals;
194
195 /*!
196 * Map of controller address to source peripheral
197 *
198 * Source peripherals are not exposed to the game.
199 */
200 PeripheralMap m_peripheralMap;
201
202 /*!
203 * Collection of disconnected joysticks
204 *
205 * Source peripherals are not exposed to the game.
206 */
207 std::set<PERIPHERALS::PeripheralPtr> m_disconnectedPeripherals;
208 };
209 } // namespace GAME
210 } // namespace KODI