| 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 "input/joysticks/JoystickTypes.h" |
| 12 |
|
| 13 |
#include <map> |
| 14 |
#include <string> |
| 15 |
|
| 16 |
namespace KODI |
| 17 |
{ |
| 18 |
namespace KEYMAP |
| 19 |
{ |
| 20 |
class IKeymap; |
| 21 |
} // namespace KEYMAP |
| 22 |
|
| 23 |
namespace JOYSTICK |
| 24 |
{ |
| 25 |
class CDriverPrimitive; |
| 26 |
class IButtonMap; |
| 27 |
class IButtonMapCallback; |
| 28 |
|
| 29 |
/*! |
| 30 |
* \ingroup joystick |
| 31 |
* |
| 32 |
* \brief Button mapper interface to assign the driver's raw button/hat/axis |
| 33 |
* elements to physical joystick features using a provided button map. |
| 34 |
* |
| 35 |
* \sa IButtonMap |
| 36 |
*/ |
| 37 |
class IButtonMapper |
| 38 |
{ |
| 39 |
public: |
| 40 |
IButtonMapper() = default; |
| 41 |
|
| 42 |
virtual ~IButtonMapper() = default; |
| 43 |
|
| 44 |
/*! |
| 45 |
* \brief The add-on ID of the game controller associated with this button mapper |
| 46 |
* |
| 47 |
* \return The ID of the add-on extending kodi.game.controller |
| 48 |
*/ |
| 49 |
virtual std::string ControllerID(void) const = 0; |
| 50 |
|
| 51 |
/*! |
| 52 |
* \brief Return true if the button mapper wants a cooldown between button |
| 53 |
* mapping commands |
| 54 |
* |
| 55 |
* \return True to only send button mapping commands that occur after a small |
| 56 |
* timeout from the previous command. |
| 57 |
*/ |
| 58 |
virtual bool NeedsCooldown(void) const = 0; |
| 59 |
|
| 60 |
/*! |
| 61 |
* \brief Return true if the button mapper accepts primitives of the given type |
| 62 |
* |
| 63 |
* \param type The primitive type |
| 64 |
* |
| 65 |
* \return True if the button mapper can map the primitive type, false otherwise |
| 66 |
*/ |
| 67 |
virtual bool AcceptsPrimitive(PRIMITIVE_TYPE type) const = 0; |
| 68 |
|
| 69 |
/*! |
| 70 |
* \brief Handle button/hat press or axis threshold |
| 71 |
* |
| 72 |
* \param buttonMap The button map being manipulated |
| 73 |
* \param keymap An interface capable of translating features to Kodi actions |
| 74 |
* \param primitive The driver primitive |
| 75 |
* |
| 76 |
* Called in the same thread as \ref IButtonMapper::OnFrame. |
| 77 |
* |
| 78 |
* \return True if driver primitive was mapped to a feature |
| 79 |
*/ |
| 80 |
virtual bool MapPrimitive(IButtonMap* buttonMap, |
| 81 |
KEYMAP::IKeymap* keyMap, |
| 82 |
const CDriverPrimitive& primitive) = 0; |
| 83 |
|
| 84 |
/*! |
| 85 |
* \brief Called once per event frame to notify the implementation of motion status |
| 86 |
* |
| 87 |
* \param buttonMap The button map passed to MapPrimitive() (shall not be modified) |
| 88 |
* \param bMotion True if a previously-mapped axis is still in motion |
| 89 |
* |
| 90 |
* This allows the implementer to wait for an axis to be centered before |
| 91 |
* allowing it to be used as Kodi input. |
| 92 |
* |
| 93 |
* If mapping finishes on an axis, then the axis will still be pressed and |
| 94 |
* sending input every frame when the mapping ends. For example, when the |
| 95 |
* right analog stick is the last feature to be mapped, it is still pressed |
| 96 |
* when mapping ends and immediately sends Volume Down actions. |
| 97 |
* |
| 98 |
* The fix is to allow implementers to wait until all axes are motionless |
| 99 |
* before detaching themselves. |
| 100 |
* |
| 101 |
* Called in the same thread as \ref IButtonMapper::MapPrimitive. |
| 102 |
*/ |
| 103 |
virtual void OnEventFrame(const IButtonMap* buttonMap, bool bMotion) = 0; |
| 104 |
|
| 105 |
/*! |
| 106 |
* \brief Called when an axis has been detected after mapping began |
| 107 |
* |
| 108 |
* \param axisIndex The index of the axis being discovered |
| 109 |
* |
| 110 |
* Some joystick drivers don't report an initial value for analog axes. |
| 111 |
* |
| 112 |
* Called in the same thread as \ref IButtonMapper::MapPrimitive. |
| 113 |
*/ |
| 114 |
virtual void OnLateAxis(const IButtonMap* buttonMap, unsigned int axisIndex) = 0; |
| 115 |
|
| 116 |
// Button map callback interface |
| 117 |
void SetButtonMapCallback(const std::string& deviceLocation, IButtonMapCallback* callback) |
| 118 |
{ |
| 119 |
m_callbacks[deviceLocation] = callback; |
| 120 |
} |
| 121 |
void ResetButtonMapCallbacks(void) { m_callbacks.clear(); } |
| 122 |
std::map<std::string, IButtonMapCallback*>& ButtonMapCallbacks(void) { return m_callbacks; } |
| 123 |
|
| 124 |
private: |
| 125 |
std::map<std::string, IButtonMapCallback*> m_callbacks; // Device location -> callback |
| 126 |
}; |
| 127 |
} // namespace JOYSTICK |
| 128 |
} // namespace KODI |