| 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 <string> |
| 14 |
|
| 15 |
namespace KODI |
| 16 |
{ |
| 17 |
namespace JOYSTICK |
| 18 |
{ |
| 19 |
class IInputReceiver; |
| 20 |
|
| 21 |
/*! |
| 22 |
* \ingroup joystick |
| 23 |
* |
| 24 |
* \brief Interface for handling input events for game controllers |
| 25 |
*/ |
| 26 |
class IInputHandler |
| 27 |
{ |
| 28 |
public: |
| 29 |
virtual ~IInputHandler() = default; |
| 30 |
|
| 31 |
/*! |
| 32 |
* \brief The add-on ID of the game controller associated with this input handler |
| 33 |
* |
| 34 |
* \return The ID of the add-on extending kodi.game.controller |
| 35 |
*/ |
| 36 |
virtual std::string ControllerID(void) const = 0; |
| 37 |
|
| 38 |
/*! |
| 39 |
* \brief Return true if the input handler accepts the given feature |
| 40 |
* |
| 41 |
* \param feature A feature belonging to the controller specified by ControllerID() |
| 42 |
* |
| 43 |
* \return True if the feature is used for input, false otherwise |
| 44 |
*/ |
| 45 |
virtual bool HasFeature(const FeatureName& feature) const = 0; |
| 46 |
|
| 47 |
/*! |
| 48 |
* \brief Return true if the input handler is currently accepting input for the |
| 49 |
* given feature |
| 50 |
* |
| 51 |
* \param feature A feature belonging to the controller specified by ControllerID() |
| 52 |
* |
| 53 |
* \return True if the feature is currently accepting input, false otherwise |
| 54 |
* |
| 55 |
* This does not prevent the input events from being called, but can return |
| 56 |
* false to indicate that input wasn't handled for the specified feature. |
| 57 |
*/ |
| 58 |
virtual bool AcceptsInput(const FeatureName& feature) const = 0; |
| 59 |
|
| 60 |
/*! |
| 61 |
* \brief A digital button has been pressed or released |
| 62 |
* |
| 63 |
* \param feature The feature being pressed |
| 64 |
* \param bPressed True if pressed, false if released |
| 65 |
* |
| 66 |
* \return True if the event was handled otherwise false |
| 67 |
*/ |
| 68 |
virtual bool OnButtonPress(const FeatureName& feature, bool bPressed) = 0; |
| 69 |
|
| 70 |
/*! |
| 71 |
* \brief A digital button has been pressed for more than one event frame |
| 72 |
* |
| 73 |
* \param feature The feature being held |
| 74 |
* \param holdTimeMs The time elapsed since the initial press (ms) |
| 75 |
* |
| 76 |
* If OnButtonPress() returns true for the initial press, then this callback |
| 77 |
* is invoked on subsequent frames until the button is released. |
| 78 |
*/ |
| 79 |
virtual void OnButtonHold(const FeatureName& feature, unsigned int holdTimeMs) = 0; |
| 80 |
|
| 81 |
/*! |
| 82 |
* \brief An analog button (trigger or a pressure-sensitive button) has changed state |
| 83 |
* |
| 84 |
* \param feature The feature changing state |
| 85 |
* \param magnitude The button pressure or trigger travel distance in the |
| 86 |
* closed interval [0, 1] |
| 87 |
* \param motionTimeMs The time elapsed since the magnitude was 0 |
| 88 |
* |
| 89 |
* \return True if the event was handled otherwise false |
| 90 |
*/ |
| 91 |
virtual bool OnButtonMotion(const FeatureName& feature, |
| 92 |
float magnitude, |
| 93 |
unsigned int motionTimeMs) = 0; |
| 94 |
|
| 95 |
/*! |
| 96 |
* \brief An analog stick has moved |
| 97 |
* |
| 98 |
* \param feature The analog stick being moved |
| 99 |
* \param x The x coordinate in the closed interval [-1, 1] |
| 100 |
* \param y The y coordinate in the closed interval [-1, 1] |
| 101 |
* \param motionTimeMs The time elapsed since this analog stick was centered, |
| 102 |
* or 0 if the analog stick is centered |
| 103 |
* |
| 104 |
* \return True if the event was handled otherwise false |
| 105 |
*/ |
| 106 |
virtual bool OnAnalogStickMotion(const FeatureName& feature, |
| 107 |
float x, |
| 108 |
float y, |
| 109 |
unsigned int motionTimeMs) = 0; |
| 110 |
|
| 111 |
/*! |
| 112 |
* \brief An accelerometer's state has changed |
| 113 |
* |
| 114 |
* \param feature The accelerometer being accelerated |
| 115 |
* \param x The x coordinate in the closed interval [-1, 1] |
| 116 |
* \param y The y coordinate in the closed interval [-1, 1] |
| 117 |
* \param z The z coordinate in the closed interval [-1, 1] |
| 118 |
* |
| 119 |
* \return True if the event was handled otherwise false |
| 120 |
*/ |
| 121 |
virtual bool OnAccelerometerMotion(const FeatureName& feature, float x, float y, float z) |
| 122 |
{ |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
/*! |
| 127 |
* \brief A wheel has changed state |
| 128 |
* |
| 129 |
* Left is negative position, right is positive position |
| 130 |
* |
| 131 |
* \param feature The wheel changing state |
| 132 |
* \param position The position in the closed interval [-1, 1] |
| 133 |
* \param motionTimeMs The time elapsed since the position was 0 |
| 134 |
* |
| 135 |
* \return True if the event was handled otherwise false |
| 136 |
*/ |
| 137 |
virtual bool OnWheelMotion(const FeatureName& feature, |
| 138 |
float position, |
| 139 |
unsigned int motionTimeMs) = 0; |
| 140 |
|
| 141 |
/*! |
| 142 |
* \brief A throttle has changed state |
| 143 |
* |
| 144 |
* Up is positive position, down is negative position. |
| 145 |
* |
| 146 |
* \param feature The wheel changing state |
| 147 |
* \param position The position in the closed interval [-1, 1] |
| 148 |
* \param motionTimeMs The time elapsed since the position was 0 |
| 149 |
* |
| 150 |
* \return True if the event was handled otherwise false |
| 151 |
*/ |
| 152 |
virtual bool OnThrottleMotion(const FeatureName& feature, |
| 153 |
float position, |
| 154 |
unsigned int motionTimeMs) = 0; |
| 155 |
|
| 156 |
/*! |
| 157 |
* \brief Called at the end of the frame that provided input |
| 158 |
*/ |
| 159 |
virtual void OnInputFrame() = 0; |
| 160 |
|
| 161 |
// Input receiver interface |
| 162 |
void SetInputReceiver(IInputReceiver* receiver) { m_receiver = receiver; } |
| 163 |
void ResetInputReceiver(void) { m_receiver = nullptr; } |
| 164 |
IInputReceiver* InputReceiver(void) { return m_receiver; } |
| 165 |
|
| 166 |
private: |
| 167 |
IInputReceiver* m_receiver = nullptr; |
| 168 |
}; |
| 169 |
} // namespace JOYSTICK |
| 170 |
} // namespace KODI |