kodi
KeymapHandler.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/joysticks/JoystickTypes.h"
12 #include "input/joysticks/interfaces/IButtonSequence.h"
13 #include "input/joysticks/interfaces/IInputHandler.h"
14 #include "input/keymaps/interfaces/IKeymapHandler.h"
15 
16 #include <map>
17 #include <memory>
18 #include <string>
19 
20 namespace KODI
21 {
22 namespace ACTION
23 {
24 class IActionListener;
25 } // namespace ACTION
26 
27 namespace KEYMAP
28 {
29 class IKeyHandler;
30 class IKeymap;
31 
36 {
37 public:
38  CKeymapHandler(ACTION::IActionListener* actionHandler, const IKeymap* keymap);
39 
40  ~CKeymapHandler() override = default;
41 
42  // implementation of IKeymapHandler
43  bool HotkeysPressed(const std::set<std::string>& keyNames) const override;
44  std::string GetLastPressed() const override { return m_lastPressed; }
45  void OnPress(const std::string& keyName) override { m_lastPressed = keyName; }
46 
47  // implementation of IInputHandler
48  std::string ControllerID() const override;
49  bool HasFeature(const JOYSTICK::FeatureName& feature) const override { return true; }
50  bool AcceptsInput(const JOYSTICK::FeatureName& feature) const override;
51  bool OnButtonPress(const JOYSTICK::FeatureName& feature, bool bPressed) override;
52  void OnButtonHold(const JOYSTICK::FeatureName& feature, unsigned int holdTimeMs) override;
53  bool OnButtonMotion(const JOYSTICK::FeatureName& feature,
54  float magnitude,
55  unsigned int motionTimeMs) override;
56  bool OnAnalogStickMotion(const JOYSTICK::FeatureName& feature,
57  float x,
58  float y,
59  unsigned int motionTimeMs) override;
60  bool OnAccelerometerMotion(const JOYSTICK::FeatureName& feature,
61  float x,
62  float y,
63  float z) override;
64  bool OnWheelMotion(const JOYSTICK::FeatureName& feature,
65  float position,
66  unsigned int motionTimeMs) override;
67  bool OnThrottleMotion(const JOYSTICK::FeatureName& feature,
68  float position,
69  unsigned int motionTimeMs) override;
70  void OnInputFrame() override {}
71 
72 protected:
73  // Keep track of cheat code presses
74  std::unique_ptr<JOYSTICK::IButtonSequence> m_easterEgg;
75 
76 private:
77  // Analog stick helper functions
78  bool ActivateDirection(const JOYSTICK::FeatureName& feature,
79  float magnitude,
80  JOYSTICK::ANALOG_STICK_DIRECTION dir,
81  unsigned int motionTimeMs);
82  void DeactivateDirection(const JOYSTICK::FeatureName& feature,
83  JOYSTICK::ANALOG_STICK_DIRECTION dir);
84 
85  // Wheel helper functions
86  bool ActivateDirection(const JOYSTICK::FeatureName& feature,
87  float magnitude,
88  JOYSTICK::WHEEL_DIRECTION dir,
89  unsigned int motionTimeMs);
90  void DeactivateDirection(const JOYSTICK::FeatureName& feature, JOYSTICK::WHEEL_DIRECTION dir);
91 
92  // Throttle helper functions
93  bool ActivateDirection(const JOYSTICK::FeatureName& feature,
94  float magnitude,
95  JOYSTICK::THROTTLE_DIRECTION dir,
96  unsigned int motionTimeMs);
97  void DeactivateDirection(const JOYSTICK::FeatureName& feature, JOYSTICK::THROTTLE_DIRECTION dir);
98 
99  // Helper functions
100  IKeyHandler* GetKeyHandler(const std::string& keyName);
101  bool HasAction(const std::string& keyName) const;
102 
103  // Construction parameters
104  ACTION::IActionListener* const m_actionHandler;
105  const IKeymap* const m_keymap;
106 
107  // Handlers for individual keys
108  std::map<std::string, std::unique_ptr<IKeyHandler>> m_keyHandlers; // Key name -> handler
109 
110  // Last pressed key
111  std::string m_lastPressed;
112 };
113 } // namespace KEYMAP
114 } // namespace KODI
void OnInputFrame() override
Called at the end of the frame that provided input.
Definition: KeymapHandler.h:70
Definition: KeymapHandler.h:35
Interface for handling keymap keys.
Definition: IKeyHandler.h:23
Definition: AudioDecoder.h:18
Interface for a class working with a keymap.
Definition: IKeymapHandler.h:23
Interface for handling input events for game controllers.
Definition: IInputHandler.h:26
Interface defining methods to handle GUI actions.
Definition: IActionListener.h:22
std::string GetLastPressed() const override
Get the key name of the last button pressed.
Definition: KeymapHandler.h:44
bool HasFeature(const JOYSTICK::FeatureName &feature) const override
Return true if the input handler accepts the given feature.
Definition: KeymapHandler.h:49
void OnPress(const std::string &keyName) override
Called when a key has emitted an action after bring pressed.
Definition: KeymapHandler.h:45
Interface for mapping buttons to Kodi actions.
Definition: IKeymap.h:28