Debian Codegraph @

xbmc/input/InputManager.h

raw
1 /*
2 * Copyright (C) 2005-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
9 #pragma once
10
11 #include "input/actions/Action.h"
12 #include "input/actions/interfaces/IActionListener.h"
13 #include "input/keyboard/KeyboardStat.h"
14 #include "input/keymaps/ButtonStat.h"
15 #include "input/mouse/MouseStat.h"
16 #include "input/mouse/interfaces/IMouseInputProvider.h"
17 #include "settings/lib/ISettingCallback.h"
18 #include "threads/CriticalSection.h"
19 #include "utils/Observer.h"
20 #include "windowing/XBMC_events.h"
21
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <vector>
26
27 class CKey;
28 class CProfileManager;
29
30 namespace KODI
31 {
32
33 namespace KEYBOARD
34 {
35 class IKeyboardDriverHandler;
36 }
37
38 namespace KEYMAP
39 {
40 class CButtonTranslator;
41 class CCustomControllerTranslator;
42 class CJoystickMapper;
43 class CTouchTranslator;
44 class IKeymapEnvironment;
45 class IWindowKeymap;
46 } // namespace KEYMAP
47
48 namespace MOUSE
49 {
50 class IMouseDriverHandler;
51 }
52 } // namespace KODI
53
54 /// \addtogroup input
55 /// \{
56
57 /*!
58 * \ingroup input keyboard mouse touch joystick keymap
59 *
60 * \brief Main input processing class.
61 *
62 * This class consolidates all input generated from different sources such as
63 * mouse, keyboard, joystick or touch (in \ref OnEvent).
64 *
65 * \copydoc keyboard
66 * \copydoc mouse
67 */
68 class CInputManager : public ISettingCallback,
69 public KODI::ACTION::IActionListener,
70 public Observable
71 {
72 public:
73 CInputManager();
74 CInputManager(const CInputManager&) = delete;
75 CInputManager const& operator=(CInputManager const&) = delete;
76 ~CInputManager() override;
77
78 /*! \brief decode a mouse event and reset idle timers.
79 *
80 * \param windowId Currently active window
81 * \return true if event is handled, false otherwise
82 */
83 bool ProcessMouse(int windowId);
84
85 /*! \brief decode an event from the event service, this can be mouse, key, joystick, reset idle
86 * timers.
87 *
88 * \param windowId Currently active window
89 * \param frameTime Time in seconds since last call
90 * \return true if event is handled, false otherwise
91 */
92 bool ProcessEventServer(int windowId, float frameTime);
93
94 /*! \brief decode an event from peripherals.
95 *
96 * \param frameTime Time in seconds since last call
97 * \return true if event is handled, false otherwise
98 */
99 bool ProcessPeripherals(float frameTime);
100
101 /*! \brief Process all inputs
102 *
103 * \param windowId Currently active window
104 * \param frameTime Time in seconds since last call
105 * \return true on success, false otherwise
106 */
107 bool Process(int windowId, float frameTime);
108
109 /*!
110 * \brief Call once during application startup to initialize peripherals that need it
111 */
112 void InitializeInputs();
113
114 /*!
115 * \brief Deinitialize input and keymaps
116 */
117 void Deinitialize();
118
119 /*! \brief Handle an input event
120 *
121 * \param newEvent event details
122 * \return true on successfully handled event
123 * \sa XBMC_Event
124 */
125 bool OnEvent(XBMC_Event& newEvent);
126
127 /*! \brief Control if the mouse is actively used or not
128 *
129 * \param[in] active sets mouse active or inactive
130 */
131 void SetMouseActive(bool active = true);
132
133 /*! \brief Control if we should use a mouse or not
134 *
135 * \param[in] mouseEnabled sets mouse enabled or disabled
136 */
137 void SetMouseEnabled(bool mouseEnabled = true);
138
139 /*! \brief Set the current state of the mouse such as click, drag operation
140 *
141 * \param[in] mouseState which state the mouse should be set to
142 * \sa MOUSE_STATE
143 */
144 void SetMouseState(MOUSE_STATE mouseState);
145
146 /*! \brief Check if the mouse is currently active
147 *
148 * \return true if active, false otherwise
149 */
150 bool IsMouseActive();
151
152 /*! \brief Get the current state of the mouse, such as click or drag operation
153 *
154 * \return the current state of the mouse as a value from MOUSE_STATE
155 * \sa MOUSE_STATE
156 */
157 MOUSE_STATE GetMouseState();
158
159 /*! \brief Get the current mouse positions x and y coordinates
160 *
161 * \return a struct containing the x and y coordinates
162 * \sa MousePosition
163 */
164 MousePosition GetMousePosition();
165
166 /*! \brief Set the current screen resolution and pointer speed
167 *
168 * \param[in] maxX screen width
169 * \param[in] maxY screen height
170 * \param[in] speedX mouse speed in x dimension
171 * \param[in] speedY mouse speed in y dimension
172 * \return
173 */
174 void SetMouseResolution(int maxX, int maxY, float speedX, float speedY);
175
176 /*! \brief Get the status of the controller-enable setting
177 * \return True if controller input is enabled for the UI, false otherwise
178 */
179 bool IsControllerEnabled() const;
180
181 /*! \brief Returns whether or not we can handle a given built-in command.
182 *
183 */
184 bool HasBuiltin(const std::string& command);
185
186 /*! \brief Parse a builtin command and execute any input action
187 * currently only LIRC commands implemented
188 *
189 * \param[in] execute Command to execute
190 * \param[in] params parameters that was passed to the command
191 * \return 0 on success, -1 on failure
192 */
193 int ExecuteBuiltin(const std::string& execute, const std::vector<std::string>& params);
194
195 // Button translation
196 bool LoadKeymaps();
197 bool ReloadKeymaps();
198 void ClearKeymaps();
199 void AddKeymap(const std::string& keymap);
200 void RemoveKeymap(const std::string& keymap);
201
202 const KODI::KEYMAP::IKeymapEnvironment* KeymapEnvironment() const;
203
204 /*! \brief Obtain the action configured for a given window and key
205 *
206 * \param window the window id
207 * \param key the key to query the action for
208 * \param fallback if no action is directly configured for the given window, obtain the action
209 * from fallback window, if exists or from global config as last resort
210 *
211 * \return the action matching the key
212 */
213 CAction GetAction(int window, const CKey& key, bool fallback = true);
214
215 bool TranslateCustomControllerString(int windowId,
216 const std::string& controllerName,
217 int buttonId,
218 int& action,
219 std::string& strAction);
220
221 bool TranslateTouchAction(
222 int windowId, int touchAction, int touchPointers, int& action, std::string& actionString);
223
224 std::vector<std::shared_ptr<const KODI::KEYMAP::IWindowKeymap>> GetJoystickKeymaps() const;
225
226 /*!
227 * \brief Queue an action to be processed on the next call to Process()
228 */
229 void QueueAction(const CAction& action);
230
231 // implementation of ISettingCallback
232 void OnSettingChanged(const std::shared_ptr<const CSetting>& setting) override;
233
234 // implementation of IActionListener
235 bool OnAction(const CAction& action) override;
236
237 void RegisterKeyboardDriverHandler(KODI::KEYBOARD::IKeyboardDriverHandler* handler);
238 void UnregisterKeyboardDriverHandler(KODI::KEYBOARD::IKeyboardDriverHandler* handler);
239
240 virtual void RegisterMouseDriverHandler(KODI::MOUSE::IMouseDriverHandler* handler);
241 virtual void UnregisterMouseDriverHandler(KODI::MOUSE::IMouseDriverHandler* handler);
242
243 private:
244 /*! \brief Process keyboard event and translate into an action
245 *
246 * \param key keypress details
247 * \return true on successfully handled event
248 * \sa CKey
249 */
250 bool OnKey(const CKey& key);
251
252 /*! \brief Process key up event
253 *
254 * \param key details of released key
255 * \sa CKey
256 */
257 void OnKeyUp(const CKey& key);
258
259 /*! \brief Handle keypress
260 *
261 * \param key keypress details
262 * \return true on successfully handled event
263 */
264 bool HandleKey(const CKey& key);
265
266 /*! \brief Determine if an action should be processed or just
267 * cancel the screensaver
268 *
269 * \param action Action that is about to be processed
270 * \return true on any poweractions such as shutdown/reboot/sleep/suspend, false otherwise
271 * \sa CAction
272 */
273 bool AlwaysProcess(const CAction& action);
274
275 /*! \brief Send the Action to CApplication for further handling,
276 * play a sound before or after sending the action.
277 *
278 * \param action Action to send to CApplication
279 * \return result from CApplication::OnAction
280 * \sa CAction
281 */
282 bool ExecuteInputAction(const CAction& action);
283
284 /*! \brief Dispatch actions queued since the last call to Process()
285 */
286 void ProcessQueuedActions();
287
288 KODI::KEYBOARD::CKeyboardStat m_Keyboard;
289 KODI::KEYMAP::CButtonStat m_buttonStat;
290 CMouseStat m_Mouse;
291 CKey m_LastKey;
292
293 std::map<std::string, std::map<int, float>> m_lastAxisMap;
294
295 std::vector<CAction> m_queuedActions;
296 CCriticalSection m_actionMutex;
297
298 // Button translation
299 std::unique_ptr<KODI::KEYMAP::IKeymapEnvironment> m_keymapEnvironment;
300 std::unique_ptr<KODI::KEYMAP::CButtonTranslator> m_buttonTranslator;
301 std::unique_ptr<KODI::KEYMAP::CCustomControllerTranslator> m_customControllerTranslator;
302 std::unique_ptr<KODI::KEYMAP::CTouchTranslator> m_touchTranslator;
303 std::unique_ptr<KODI::KEYMAP::CJoystickMapper> m_joystickTranslator;
304
305 std::vector<KODI::KEYBOARD::IKeyboardDriverHandler*> m_keyboardHandlers;
306 std::vector<KODI::MOUSE::IMouseDriverHandler*> m_mouseHandlers;
307
308 std::unique_ptr<KODI::KEYBOARD::IKeyboardDriverHandler> m_keyboardEasterEgg;
309
310 // Input state
311 bool m_enableController = true;
312
313 // Settings
314 static const std::string SETTING_INPUT_ENABLE_CONTROLLER;
315 };
316
317 /// \}