| 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 "JoystickTypes.h" |
| 12 |
#include "input/keyboard/KeyboardTypes.h" |
| 13 |
#include "input/mouse/MouseTypes.h" |
| 14 |
|
| 15 |
#include <stdint.h> |
| 16 |
|
| 17 |
namespace KODI |
| 18 |
{ |
| 19 |
namespace JOYSTICK |
| 20 |
{ |
| 21 |
/*! |
| 22 |
* \ingroup joystick |
| 23 |
* |
| 24 |
* \brief Basic driver element associated with input events |
| 25 |
* |
| 26 |
* Driver input (bools, floats and enums) is split into primitives that better |
| 27 |
* map to the physical features on a joystick. |
| 28 |
* |
| 29 |
* A bool obviously only maps to a single feature, so it is a driver |
| 30 |
* primitive. Here, these are called "buttons". |
| 31 |
* |
| 32 |
* A hat enum encodes the state of the four hat directions. Each direction |
| 33 |
* can map to a different feature, so a hat enum consists of four driver |
| 34 |
* primitives called "hat directions". |
| 35 |
* |
| 36 |
* A float is a little trickier. Trivially, it can map to an analog stick or |
| 37 |
* trigger. However, DirectInput combines two triggers onto a single axis. |
| 38 |
* Therefore, the axis is split into two primitives called "semiaxes". |
| 39 |
* |
| 40 |
* The type determines the fields in use: |
| 41 |
* |
| 42 |
* Button: |
| 43 |
* - driver index |
| 44 |
* |
| 45 |
* Hat direction: |
| 46 |
* - driver index |
| 47 |
* - hat direction (up/right/down/left) |
| 48 |
* |
| 49 |
* Semiaxis: |
| 50 |
* - driver index |
| 51 |
* - center (-1, 0 or 1) |
| 52 |
* - semiaxis direction (positive/negative) |
| 53 |
* - range (1 or 2) |
| 54 |
* |
| 55 |
* Motor: |
| 56 |
* - driver index |
| 57 |
* |
| 58 |
* Key: |
| 59 |
* - keycode |
| 60 |
* |
| 61 |
* Mouse button: |
| 62 |
* - driver index |
| 63 |
* |
| 64 |
* Relative pointer: |
| 65 |
* - pointer direction |
| 66 |
* |
| 67 |
* For more info, see "Chapter 2. Joystick drivers" in the documentation |
| 68 |
* thread: http://forum.kodi.tv/showthread.php?tid=257764 |
| 69 |
*/ |
| 70 |
class CDriverPrimitive |
| 71 |
{ |
| 72 |
public: |
| 73 |
/*! |
| 74 |
* \brief Construct an invalid driver primitive |
| 75 |
*/ |
| 76 |
CDriverPrimitive(void); |
| 77 |
|
| 78 |
/*! |
| 79 |
* \brief Construct a driver primitive representing a button or motor |
| 80 |
*/ |
| 81 |
CDriverPrimitive(PRIMITIVE_TYPE type, unsigned int index); |
| 82 |
|
| 83 |
/*! |
| 84 |
* \brief Construct a driver primitive representing one of the four |
| 85 |
* direction arrows on a dpad |
| 86 |
*/ |
| 87 |
CDriverPrimitive(unsigned int hatIndex, HAT_DIRECTION direction); |
| 88 |
|
| 89 |
/*! |
| 90 |
* \brief Construct a driver primitive representing the positive or negative |
| 91 |
* half of an axis |
| 92 |
*/ |
| 93 |
CDriverPrimitive(unsigned int axisIndex, |
| 94 |
int center, |
| 95 |
SEMIAXIS_DIRECTION direction, |
| 96 |
unsigned int range); |
| 97 |
|
| 98 |
/*! |
| 99 |
* \brief Construct a driver primitive representing a key on a keyboard |
| 100 |
*/ |
| 101 |
CDriverPrimitive(KEYBOARD::XBMCKey keycode); |
| 102 |
|
| 103 |
/*! |
| 104 |
* \brief Construct a driver primitive representing a mouse button |
| 105 |
*/ |
| 106 |
CDriverPrimitive(MOUSE::BUTTON_ID index); |
| 107 |
|
| 108 |
/*! |
| 109 |
* \brief Construct a driver primitive representing a relative pointer |
| 110 |
*/ |
| 111 |
CDriverPrimitive(RELATIVE_POINTER_DIRECTION direction); |
| 112 |
|
| 113 |
bool operator==(const CDriverPrimitive& rhs) const; |
| 114 |
bool operator<(const CDriverPrimitive& rhs) const; |
| 115 |
|
| 116 |
bool operator!=(const CDriverPrimitive& rhs) const { return !operator==(rhs); } |
| 117 |
bool operator>(const CDriverPrimitive& rhs) const { return !(operator<(rhs) || operator==(rhs)); } |
| 118 |
bool operator<=(const CDriverPrimitive& rhs) const { return operator<(rhs) || operator==(rhs); } |
| 119 |
bool operator>=(const CDriverPrimitive& rhs) const { return !operator<(rhs); } |
| 120 |
|
| 121 |
/*! |
| 122 |
* \brief The type of driver primitive |
| 123 |
*/ |
| 124 |
PRIMITIVE_TYPE Type(void) const { return m_type; } |
| 125 |
|
| 126 |
/*! |
| 127 |
* \brief The index used by the joystick driver |
| 128 |
* |
| 129 |
* Valid for: |
| 130 |
* - buttons |
| 131 |
* - hats |
| 132 |
* - semiaxes |
| 133 |
* - motors |
| 134 |
*/ |
| 135 |
unsigned int Index(void) const { return m_driverIndex; } |
| 136 |
|
| 137 |
/*! |
| 138 |
* \brief The direction arrow (valid for hat directions) |
| 139 |
*/ |
| 140 |
HAT_DIRECTION HatDirection(void) const { return m_hatDirection; } |
| 141 |
|
| 142 |
/*! |
| 143 |
* \brief The location of the zero point of the semiaxis |
| 144 |
*/ |
| 145 |
int Center() const { return m_center; } |
| 146 |
|
| 147 |
/*! |
| 148 |
* \brief The semiaxis direction (valid for semiaxes) |
| 149 |
*/ |
| 150 |
SEMIAXIS_DIRECTION SemiAxisDirection(void) const { return m_semiAxisDirection; } |
| 151 |
|
| 152 |
/*! |
| 153 |
* \brief The distance between the center and the farthest valid value (valid for semiaxes) |
| 154 |
*/ |
| 155 |
unsigned int Range() const { return m_range; } |
| 156 |
|
| 157 |
/*! |
| 158 |
* \brief The keyboard symbol (valid for keys) |
| 159 |
*/ |
| 160 |
KEYBOARD::XBMCKey Keycode() const { return m_keycode; } |
| 161 |
|
| 162 |
/*! |
| 163 |
* \brief The mouse button ID (valid for mouse buttons) |
| 164 |
*/ |
| 165 |
MOUSE::BUTTON_ID MouseButton() const { return static_cast<MOUSE::BUTTON_ID>(m_driverIndex); } |
| 166 |
|
| 167 |
/*! |
| 168 |
* \brief The relative pointer direction (valid for relative pointers) |
| 169 |
*/ |
| 170 |
RELATIVE_POINTER_DIRECTION PointerDirection() const { return m_pointerDirection; } |
| 171 |
|
| 172 |
/*! |
| 173 |
* \brief Test if an driver primitive is valid |
| 174 |
* |
| 175 |
* A driver primitive is valid if it has a known type and: |
| 176 |
* |
| 177 |
* 1) for hats, it is a cardinal direction |
| 178 |
* 2) for semi-axes, it is a positive or negative direction |
| 179 |
* 3) for keys, the keycode is non-empty |
| 180 |
*/ |
| 181 |
bool IsValid(void) const; |
| 182 |
|
| 183 |
/*! |
| 184 |
* \brief Convert primitive to a string suitable for logging |
| 185 |
* |
| 186 |
* \return The primitive as described by a short string, or empty if invalid |
| 187 |
*/ |
| 188 |
std::string ToString() const; |
| 189 |
|
| 190 |
private: |
| 191 |
PRIMITIVE_TYPE m_type = PRIMITIVE_TYPE::UNKNOWN; |
| 192 |
unsigned int m_driverIndex = 0; |
| 193 |
HAT_DIRECTION m_hatDirection = HAT_DIRECTION::NONE; |
| 194 |
int m_center = 0; |
| 195 |
SEMIAXIS_DIRECTION m_semiAxisDirection = SEMIAXIS_DIRECTION::ZERO; |
| 196 |
unsigned int m_range = 1; |
| 197 |
KEYBOARD::XBMCKey m_keycode = KEYBOARD::XBMCKey::XBMCK_UNKNOWN; |
| 198 |
RELATIVE_POINTER_DIRECTION m_pointerDirection = RELATIVE_POINTER_DIRECTION::NONE; |
| 199 |
}; |
| 200 |
} // namespace JOYSTICK |
| 201 |
} // namespace KODI |