kodi
AgentInput.h
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 
65 class CAgentInput : public Observable,
66  public Observer,
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:
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& joysticks,
121  PERIPHERALS::EventLockHandlePtr& inputHandlingLock);
122  void UpdateExpiredJoysticks(const PERIPHERALS::PeripheralVector& joysticks,
123  PERIPHERALS::EventLockHandlePtr& inputHandlingLock);
124  void UpdateConnectedJoysticks(const PERIPHERALS::PeripheralVector& joysticks,
125  const PortMap& newPortMap,
126  PERIPHERALS::EventLockHandlePtr& inputHandlingLock,
127  std::set<PERIPHERALS::PeripheralPtr>& disconnectedJoysticks);
128 
129  // Static functionals
130  static PortMap MapJoysticks(const PERIPHERALS::PeripheralVector& peripheralJoysticks,
131  const JoystickMap& gameClientjoysticks,
132  CurrentPortMap& currentPorts,
133  CurrentPeripheralMap& currentPeripherals,
134  int playerLimit);
135  static void MapJoystick(PERIPHERALS::PeripheralPtr peripheralJoystick,
136  std::shared_ptr<CGameClientJoystick> gameClientJoystick,
137  PortMap& result);
138  static void LogPeripheralMap(const PeripheralMap& peripheralMap,
139  const std::set<PERIPHERALS::PeripheralPtr>& disconnectedPeripherals);
140 
141  // Construction parameters
142  PERIPHERALS::CPeripherals& m_peripheralManager;
143  CInputManager& m_inputManager;
144 
145  // State parameters
146  GameClientPtr m_gameClient;
147  bool m_bHasKeyboard = false;
148  bool m_bHasMouse = false;
149  int m_initialMouseX{-1};
150  int m_initialMouseY{-1};
151  std::vector<std::shared_ptr<CAgentController>> m_controllers;
152 
153  // Synchronization parameters
154  mutable std::mutex m_controllerMutex;
155 
168  PortMap m_portMap;
169 
173  std::map<KEYBOARD::IKeyboardInputProvider*, PortAddress> m_keyboardPort;
174 
178  std::map<MOUSE::IMouseInputProvider*, PortAddress> m_mousePort;
179 
185  CurrentPortMap m_currentPorts;
186 
192  CurrentPeripheralMap m_currentPeripherals;
193 
199  PeripheralMap m_peripheralMap;
200 
206  std::set<PERIPHERALS::PeripheralPtr> m_disconnectedPeripherals;
207 };
208 } // namespace GAME
209 } // namespace KODI
Class to manage game-playing agents for a running game client.
Definition: AgentInput.h:65
Interface for classes that can provide mouse input.
Definition: IMouseInputProvider.h:22
Main input processing class.
Definition: InputManager.h:68
BUTTON_ID
Buttons on a mouse.
Definition: MouseTypes.h:26
Interface for classes that can provide input.
Definition: IInputProvider.h:22
Definition: RetroPlayerInput.h:15
void OnButtonRelease(MOUSE::BUTTON_ID button) override
A mouse button has been released.
Definition: AgentInput.h:90
Definition: AudioDecoder.h:18
Interface for classes that can provide keyboard input.
Definition: IKeyboardInputProvider.h:22
Definition: Observer.h:31
std::shared_ptr< CGameClient > GameClientPtr
Smart pointer to a game client (CGameClient)
Definition: GameTypes.h:29
void OnKeyRelease(const CKey &key) override
A key has been released.
Definition: AgentInput.h:85
Interface for handling mouse driver events.
Definition: IMouseDriverHandler.h:22
Interface for handling keyboard events.
Definition: IKeyboardDriverHandler.h:22
Definition: Observer.h:44
Definition: Key.h:17
Definition: Peripherals.h:56