xbmc
KeymapHandler.h
1 /*
2  * Copyright (C) 2017-2018 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 
12 #include "input/joysticks/interfaces/IButtonSequence.h"
13 #include "input/joysticks/interfaces/IInputHandler.h"
14 #include "input/joysticks/interfaces/IKeymapHandler.h"
15 
16 #include <map>
17 #include <memory>
18 #include <string>
19 
20 class IActionListener;
21 class IKeymap;
22 
23 namespace KODI
24 {
25 namespace JOYSTICK
26 {
27 class IKeyHandler;
28 
34 {
35 public:
36  CKeymapHandler(IActionListener* actionHandler, const IKeymap* keymap);
37 
38  ~CKeymapHandler() override = default;
39 
40  // implementation of IKeymapHandler
41  bool HotkeysPressed(const std::set<std::string>& keyNames) const override;
42  std::string GetLastPressed() const override { return m_lastPressed; }
43  void OnPress(const std::string& keyName) override { m_lastPressed = keyName; }
44 
45  // implementation of IInputHandler
46  std::string ControllerID() const override;
47  bool HasFeature(const FeatureName& feature) const override { return true; }
48  bool AcceptsInput(const FeatureName& feature) const override;
49  bool OnButtonPress(const FeatureName& feature, bool bPressed) override;
50  void OnButtonHold(const FeatureName& feature, unsigned int holdTimeMs) override;
51  bool OnButtonMotion(const FeatureName& feature,
52  float magnitude,
53  unsigned int motionTimeMs) override;
54  bool OnAnalogStickMotion(const FeatureName& feature,
55  float x,
56  float y,
57  unsigned int motionTimeMs) override;
58  bool OnAccelerometerMotion(const FeatureName& feature, float x, float y, float z) override;
59  bool OnWheelMotion(const FeatureName& feature,
60  float position,
61  unsigned int motionTimeMs) override;
62  bool OnThrottleMotion(const FeatureName& feature,
63  float position,
64  unsigned int motionTimeMs) override;
65  void OnInputFrame() override {}
66 
67 protected:
68  // Keep track of cheat code presses
69  std::unique_ptr<IButtonSequence> m_easterEgg;
70 
71 private:
72  // Analog stick helper functions
73  bool ActivateDirection(const FeatureName& feature,
74  float magnitude,
75  ANALOG_STICK_DIRECTION dir,
76  unsigned int motionTimeMs);
77  void DeactivateDirection(const FeatureName& feature, ANALOG_STICK_DIRECTION dir);
78 
79  // Wheel helper functions
80  bool ActivateDirection(const FeatureName& feature,
81  float magnitude,
82  WHEEL_DIRECTION dir,
83  unsigned int motionTimeMs);
84  void DeactivateDirection(const FeatureName& feature, WHEEL_DIRECTION dir);
85 
86  // Throttle helper functions
87  bool ActivateDirection(const FeatureName& feature,
88  float magnitude,
90  unsigned int motionTimeMs);
91  void DeactivateDirection(const FeatureName& feature, THROTTLE_DIRECTION dir);
92 
93  // Helper functions
94  IKeyHandler* GetKeyHandler(const std::string& keyName);
95  bool HasAction(const std::string& keyName) const;
96 
97  // Construction parameters
98  IActionListener* const m_actionHandler;
99  const IKeymap* const m_keymap;
100 
101  // Handlers for individual keys
102  std::map<std::string, std::unique_ptr<IKeyHandler>> m_keyHandlers; // Key name -> handler
103 
104  // Last pressed key
105  std::string m_lastPressed;
106 };
107 } // namespace JOYSTICK
108 } // namespace KODI
bool OnAnalogStickMotion(const FeatureName &feature, float x, float y, unsigned int motionTimeMs) override
An analog stick has moved.
Definition: KeymapHandler.cpp:107
bool OnAccelerometerMotion(const FeatureName &feature, float x, float y, float z) override
An accelerometer&#39;s state has changed.
Definition: KeymapHandler.cpp:188
THROTTLE_DIRECTION
Directions on a throttle.
Definition: JoystickTypes.h:130
bool AcceptsInput(const FeatureName &feature) const override
Return true if the input handler is currently accepting input for the given feature.
Definition: KeymapHandler.cpp:61
void OnButtonHold(const FeatureName &feature, unsigned int holdTimeMs) override
A digital button has been pressed for more than one event frame.
Definition: KeymapHandler.cpp:86
bool OnThrottleMotion(const FeatureName &feature, float position, unsigned int motionTimeMs) override
A throttle has changed state.
Definition: KeymapHandler.cpp:162
Interface for handling keymap keys.
Definition: IKeyHandler.h:22
bool HasFeature(const FeatureName &feature) const override
Return true if the input handler accepts the given feature.
Definition: KeymapHandler.h:47
std::string GetLastPressed() const override
Get the key name of the last button pressed.
Definition: KeymapHandler.h:42
Interface for mapping buttons to Kodi actions.
Definition: IKeymap.h:22
bool HotkeysPressed(const std::set< std::string > &keyNames) const override
Get the pressed state of the given keys.
Definition: KeymapHandler.cpp:39
std::string ControllerID() const override
The add-on ID of the game controller associated with this input handler.
Definition: KeymapHandler.cpp:56
void OnPress(const std::string &keyName) override
Called when a key has emitted an action after bring pressed.
Definition: KeymapHandler.h:43
Interface for a class working with a keymap.
Definition: IKeymapHandler.h:22
WHEEL_DIRECTION
Directions on a wheel.
Definition: JoystickTypes.h:120
void OnInputFrame() override
Called at the end of the frame that provided input.
Definition: KeymapHandler.h:65
Controller configuration window.
Definition: AudioDecoder.h:18
bool OnWheelMotion(const FeatureName &feature, float position, unsigned int motionTimeMs) override
A wheel has changed state.
Definition: KeymapHandler.cpp:136
Definition: IActionListener.h:13
Interface for handling input events for game controllers.
Definition: IInputHandler.h:25
bool OnButtonPress(const FeatureName &feature, bool bPressed) override
A digital button has been pressed or released.
Definition: KeymapHandler.cpp:75
std::string FeatureName
Name of a physical feature belonging to the joystick.
Definition: JoystickTypes.h:28
Definition: KeymapHandler.h:33
bool OnButtonMotion(const FeatureName &feature, float magnitude, unsigned int motionTimeMs) override
An analog button (trigger or a pressure-sensitive button) has changed state.
Definition: KeymapHandler.cpp:97