kodi
KeyHandler.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 
9 #pragma once
10 
11 #include "input/actions/Action.h"
12 #include "input/keymaps/KeymapTypes.h"
13 #include "input/keymaps/interfaces/IKeyHandler.h"
14 
15 #include <map>
16 #include <string>
17 #include <vector>
18 
19 class CAction;
20 
21 namespace KODI
22 {
23 namespace ACTION
24 {
25 class IActionListener;
26 } // namespace ACTION
27 
28 namespace KEYMAP
29 {
30 class IKeymapHandler;
31 class IKeymap;
32 
36 class CKeyHandler : public IKeyHandler
37 {
38 public:
39  CKeyHandler(const std::string& keyName,
40  ACTION::IActionListener* actionHandler,
41  const IKeymap* keymap,
42  IKeymapHandler* keymapHandler);
43 
44  ~CKeyHandler() override = default;
45 
46  // implementation of IKeyHandler
47  bool IsPressed() const override { return m_bHeld; }
48  bool OnDigitalMotion(bool bPressed, unsigned int holdTimeMs) override;
49  bool OnAnalogMotion(float magnitude, unsigned int motionTimeMs) override;
50 
51 private:
52  void Reset();
53 
64  CAction ProcessActions(std::vector<const KeymapAction*> actions,
65  int windowId,
66  float magnitude,
67  unsigned int holdTimeMs);
68 
77  CAction ProcessRelease(std::vector<const KeymapAction*> actions, int windowId);
78 
89  CAction ProcessAction(const KeymapAction& action,
90  int windowId,
91  float magnitude,
92  unsigned int holdTimeMs);
93 
94  // Check criteria for sending a repeat action
95  bool SendRepeatAction(unsigned int holdTimeMs);
96 
97  // Helper function
98  static bool IsPressed(float magnitude);
99 
100  // Construction parameters
101  const std::string m_keyName;
102  ACTION::IActionListener* const m_actionHandler;
103  const IKeymap* const m_keymap;
104  IKeymapHandler* const m_keymapHandler;
105 
106  // State variables
107  bool m_bHeld;
108  float m_magnitude;
109  unsigned int m_holdStartTimeMs;
110  unsigned int m_lastHoldTimeMs;
111  bool m_bActionSent;
112  unsigned int m_lastActionMs;
113  int m_activeWindowId = -1; // Window that activated the key
114  CAction m_lastAction;
115 };
116 } // namespace KEYMAP
117 } // namespace KODI
Action entry in joystick.xml.
Definition: KeymapTypes.h:23
Interface for handling keymap keys.
Definition: IKeyHandler.h:23
Class encapsulating information regarding a particular user action to be sent to windows and controls...
Definition: Action.h:21
Definition: AudioDecoder.h:18
Definition: KeyHandler.h:36
Interface for a class working with a keymap.
Definition: IKeymapHandler.h:23
Interface defining methods to handle GUI actions.
Definition: IActionListener.h:22
Definition: actions.py:1
Interface for mapping buttons to Kodi actions.
Definition: IKeymap.h:28
bool IsPressed() const override
Return true if the key is "pressed" (has a magnitude greater than 0.5)
Definition: KeyHandler.h:47