kodi
AddonButtonMap.h
1 /*
2  * Copyright (C) 2014-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 "PeripheralAddon.h" // for FeatureMap
12 #include "input/joysticks/DriverPrimitive.h"
13 #include "input/joysticks/JoystickTypes.h"
14 #include "input/joysticks/interfaces/IButtonMap.h"
15 #include "peripherals/PeripheralTypes.h"
16 #include "threads/CriticalSection.h"
17 
18 namespace PERIPHERALS
19 {
20 class CPeripheral;
21 
26 {
27 public:
29  const std::weak_ptr<CPeripheralAddon>& addon,
30  const std::string& strControllerId);
31 
32  ~CAddonButtonMap(void) override;
33 
34  // Implementation of IButtonMap
35  std::string ControllerID(void) const override { return m_strControllerId; }
36 
37  std::string Location(void) const override;
38 
39  bool Load(void) override;
40 
41  void Reset(void) override;
42 
43  bool IsEmpty(void) const override;
44 
45  std::string GetAppearance() const override;
46 
47  bool SetAppearance(const std::string& controllerId) const override;
48 
49  bool GetFeature(const KODI::JOYSTICK::CDriverPrimitive& primitive,
50  KODI::JOYSTICK::FeatureName& feature) override;
51 
52  KODI::JOYSTICK::FEATURE_TYPE GetFeatureType(const KODI::JOYSTICK::FeatureName& feature) override;
53 
54  bool GetScalar(const KODI::JOYSTICK::FeatureName& feature,
55  KODI::JOYSTICK::CDriverPrimitive& primitive) override;
56 
57  void AddScalar(const KODI::JOYSTICK::FeatureName& feature,
58  const KODI::JOYSTICK::CDriverPrimitive& primitive) override;
59 
60  bool GetAnalogStick(const KODI::JOYSTICK::FeatureName& feature,
61  KODI::JOYSTICK::ANALOG_STICK_DIRECTION direction,
62  KODI::JOYSTICK::CDriverPrimitive& primitive) override;
63 
64  void AddAnalogStick(const KODI::JOYSTICK::FeatureName& feature,
65  KODI::JOYSTICK::ANALOG_STICK_DIRECTION direction,
66  const KODI::JOYSTICK::CDriverPrimitive& primitive) override;
67 
68  bool GetRelativePointer(const KODI::JOYSTICK::FeatureName& feature,
69  KODI::JOYSTICK::RELATIVE_POINTER_DIRECTION direction,
70  KODI::JOYSTICK::CDriverPrimitive& primitive) override;
71 
72  void AddRelativePointer(const KODI::JOYSTICK::FeatureName& feature,
73  KODI::JOYSTICK::RELATIVE_POINTER_DIRECTION direction,
74  const KODI::JOYSTICK::CDriverPrimitive& primitive) override;
75 
76  bool GetAccelerometer(const KODI::JOYSTICK::FeatureName& feature,
79  KODI::JOYSTICK::CDriverPrimitive& positiveZ) override;
80 
81  void AddAccelerometer(const KODI::JOYSTICK::FeatureName& feature,
82  const KODI::JOYSTICK::CDriverPrimitive& positiveX,
83  const KODI::JOYSTICK::CDriverPrimitive& positiveY,
84  const KODI::JOYSTICK::CDriverPrimitive& positiveZ) override;
85 
86  bool GetWheel(const KODI::JOYSTICK::FeatureName& feature,
87  KODI::JOYSTICK::WHEEL_DIRECTION direction,
88  KODI::JOYSTICK::CDriverPrimitive& primitive) override;
89 
90  void AddWheel(const KODI::JOYSTICK::FeatureName& feature,
91  KODI::JOYSTICK::WHEEL_DIRECTION direction,
92  const KODI::JOYSTICK::CDriverPrimitive& primitive) override;
93 
94  bool GetThrottle(const KODI::JOYSTICK::FeatureName& feature,
95  KODI::JOYSTICK::THROTTLE_DIRECTION direction,
96  KODI::JOYSTICK::CDriverPrimitive& primitive) override;
97 
98  void AddThrottle(const KODI::JOYSTICK::FeatureName& feature,
99  KODI::JOYSTICK::THROTTLE_DIRECTION direction,
100  const KODI::JOYSTICK::CDriverPrimitive& primitive) override;
101 
102  bool GetKey(const KODI::JOYSTICK::FeatureName& feature,
103  KODI::JOYSTICK::CDriverPrimitive& primitive) override;
104 
105  void AddKey(const KODI::JOYSTICK::FeatureName& feature,
106  const KODI::JOYSTICK::CDriverPrimitive& primitive) override;
107 
109  const std::vector<KODI::JOYSTICK::CDriverPrimitive>& primitives) override;
110 
111  bool IsIgnored(const KODI::JOYSTICK::CDriverPrimitive& primitive) override;
112 
113  bool GetAxisProperties(unsigned int axisIndex, int& center, unsigned int& range) override;
114 
115  void SaveButtonMap() override;
116 
117  void RevertButtonMap() override;
118 
119 private:
120  typedef std::map<KODI::JOYSTICK::CDriverPrimitive, KODI::JOYSTICK::FeatureName> DriverMap;
121  typedef std::vector<KODI::JOYSTICK::CDriverPrimitive> JoystickPrimitiveVector;
122 
123  // Utility functions
124  static DriverMap CreateLookupTable(const FeatureMap& features);
125 
126  static JOYSTICK_FEATURE_PRIMITIVE GetAnalogStickIndex(KODI::JOYSTICK::ANALOG_STICK_DIRECTION dir);
127  static JOYSTICK_FEATURE_PRIMITIVE GetRelativePointerIndex(
128  KODI::JOYSTICK::RELATIVE_POINTER_DIRECTION dir);
129  static JOYSTICK_FEATURE_PRIMITIVE GetPrimitiveIndex(KODI::JOYSTICK::WHEEL_DIRECTION dir);
130  static JOYSTICK_FEATURE_PRIMITIVE GetPrimitiveIndex(KODI::JOYSTICK::THROTTLE_DIRECTION dir);
131 
132  // Construction parameters
133  CPeripheral* const m_device;
134  const std::weak_ptr<CPeripheralAddon> m_addon;
135  const std::string m_strControllerId;
136 
137  // Button map state
138  std::string m_controllerAppearance;
139  FeatureMap m_features;
140  DriverMap m_driverMap;
141  JoystickPrimitiveVector m_ignoredPrimitives;
142 
143  // Synchronization parameters
144  mutable CCriticalSection m_mutex;
145 };
146 } // namespace PERIPHERALS
bool GetFeature(const KODI::JOYSTICK::CDriverPrimitive &primitive, KODI::JOYSTICK::FeatureName &feature) override
Get the feature associated with a driver primitive.
Definition: AddonButtonMap.cpp:112
bool GetWheel(const KODI::JOYSTICK::FeatureName &feature, KODI::JOYSTICK::WHEEL_DIRECTION direction, KODI::JOYSTICK::CDriverPrimitive &primitive) override
Get a wheel direction from the button map.
Definition: AddonButtonMap.cpp:338
std::string Location(void) const override
The Location of the peripheral associated with this button map.
Definition: AddonButtonMap.cpp:42
JOYSTICK_FEATURE_PRIMITIVE
Definition: peripheral.h:538
void Reset(void) override
Reset the button map to its defaults, or clear button map if no defaults.
Definition: AddonButtonMap.cpp:84
std::string GetAppearance() const override
Get the ID of the controller profile that best represents the appearance of the peripheral.
Definition: AddonButtonMap.cpp:97
bool IsEmpty(void) const override
Check if the button map is empty.
Definition: AddonButtonMap.cpp:90
void AddWheel(const KODI::JOYSTICK::FeatureName &feature, KODI::JOYSTICK::WHEEL_DIRECTION direction, const KODI::JOYSTICK::CDriverPrimitive &primitive) override
Add or update a wheel direction.
Definition: AddonButtonMap.cpp:362
Definition: RetroPlayerInput.h:15
void AddKey(const KODI::JOYSTICK::FeatureName &feature, const KODI::JOYSTICK::CDriverPrimitive &primitive) override
Add or update a key.
Definition: AddonButtonMap.cpp:474
void SaveButtonMap() override
Save the button map.
Definition: AddonButtonMap.cpp:520
void AddAnalogStick(const KODI::JOYSTICK::FeatureName &feature, KODI::JOYSTICK::ANALOG_STICK_DIRECTION direction, const KODI::JOYSTICK::CDriverPrimitive &primitive) override
Add or update an analog stick direction.
Definition: AddonButtonMap.cpp:199
Button map interface to translate between the driver&#39;s raw button/hat/axis elements and physical joys...
Definition: IButtonMap.h:29
std::string ControllerID(void) const override
The add-on ID of the game controller associated with this button map.
Definition: AddonButtonMap.h:35
void RevertButtonMap() override
Revert changes to the button map since the last time it was loaded or committed to disk...
Definition: AddonButtonMap.cpp:526
void AddAccelerometer(const KODI::JOYSTICK::FeatureName &feature, const KODI::JOYSTICK::CDriverPrimitive &positiveX, const KODI::JOYSTICK::CDriverPrimitive &positiveY, const KODI::JOYSTICK::CDriverPrimitive &positiveZ) override
Get or update an accelerometer.
Definition: AddonButtonMap.cpp:318
void AddRelativePointer(const KODI::JOYSTICK::FeatureName &feature, KODI::JOYSTICK::RELATIVE_POINTER_DIRECTION direction, const KODI::JOYSTICK::CDriverPrimitive &primitive) override
Add or update a relative pointer direction.
Definition: AddonButtonMap.cpp:256
bool SetAppearance(const std::string &controllerId) const override
Set the ID of the controller that best represents the appearance of the peripheral.
Definition: AddonButtonMap.cpp:104
void AddThrottle(const KODI::JOYSTICK::FeatureName &feature, KODI::JOYSTICK::THROTTLE_DIRECTION direction, const KODI::JOYSTICK::CDriverPrimitive &primitive) override
Add or update a throttle direction.
Definition: AddonButtonMap.cpp:419
KODI::JOYSTICK::FEATURE_TYPE GetFeatureType(const KODI::JOYSTICK::FeatureName &feature) override
Get the type of the feature for the given name.
Definition: AddonButtonMap.cpp:126
void AddScalar(const KODI::JOYSTICK::FeatureName &feature, const KODI::JOYSTICK::CDriverPrimitive &primitive) override
Add or update a scalar feature.
Definition: AddonButtonMap.cpp:162
bool GetKey(const KODI::JOYSTICK::FeatureName &feature, KODI::JOYSTICK::CDriverPrimitive &primitive) override
Get the driver primitive for a keyboard key.
Definition: AddonButtonMap.cpp:452
bool GetAnalogStick(const KODI::JOYSTICK::FeatureName &feature, KODI::JOYSTICK::ANALOG_STICK_DIRECTION direction, KODI::JOYSTICK::CDriverPrimitive &primitive) override
Get an analog stick direction from the button map.
Definition: AddonButtonMap.cpp:175
bool GetAxisProperties(unsigned int axisIndex, int &center, unsigned int &range) override
Get the properties of an axis.
Definition: AddonButtonMap.cpp:498
bool Load(void) override
Load the button map into memory.
Definition: AddonButtonMap.cpp:47
bool GetScalar(const KODI::JOYSTICK::FeatureName &feature, KODI::JOYSTICK::CDriverPrimitive &primitive) override
Get the driver primitive for a scalar feature.
Definition: AddonButtonMap.cpp:139
bool GetRelativePointer(const KODI::JOYSTICK::FeatureName &feature, KODI::JOYSTICK::RELATIVE_POINTER_DIRECTION direction, KODI::JOYSTICK::CDriverPrimitive &primitive) override
Get a relative pointer direction from the button map.
Definition: AddonButtonMap.cpp:232
bool IsIgnored(const KODI::JOYSTICK::CDriverPrimitive &primitive) override
Check if a primitive is in the list of primitives to be ignored.
Definition: AddonButtonMap.cpp:492
Basic driver element associated with input events.
Definition: DriverPrimitive.h:70
bool GetThrottle(const KODI::JOYSTICK::FeatureName &feature, KODI::JOYSTICK::THROTTLE_DIRECTION direction, KODI::JOYSTICK::CDriverPrimitive &primitive) override
Get a throttle direction from the button map.
Definition: AddonButtonMap.cpp:395
Definition: Peripheral.h:71
Definition: AddonButtonMap.h:25
void SetIgnoredPrimitives(const std::vector< KODI::JOYSTICK::CDriverPrimitive > &primitives) override
Set a list of driver primitives to be ignored.
Definition: AddonButtonMap.cpp:484
bool GetAccelerometer(const KODI::JOYSTICK::FeatureName &feature, KODI::JOYSTICK::CDriverPrimitive &positiveX, KODI::JOYSTICK::CDriverPrimitive &positiveY, KODI::JOYSTICK::CDriverPrimitive &positiveZ) override
Get an accelerometer from the button map.
Definition: AddonButtonMap.cpp:289