| 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/DriverPrimitive.h" |
| 12 |
#include "input/joysticks/interfaces/IButtonMapCallback.h" |
| 13 |
#include "input/joysticks/interfaces/IDriverHandler.h" |
| 14 |
#include "input/keyboard/interfaces/IKeyboardDriverHandler.h" |
| 15 |
#include "input/mouse/MouseTypes.h" |
| 16 |
#include "input/mouse/interfaces/IMouseDriverHandler.h" |
| 17 |
|
| 18 |
#include <chrono> |
| 19 |
#include <map> |
| 20 |
#include <memory> |
| 21 |
#include <stdint.h> |
| 22 |
|
| 23 |
namespace KODI |
| 24 |
{ |
| 25 |
namespace KEYMAP |
| 26 |
{ |
| 27 |
class IKeymap; |
| 28 |
} // namespace KEYMAP |
| 29 |
|
| 30 |
namespace JOYSTICK |
| 31 |
{ |
| 32 |
class CButtonMapping; |
| 33 |
class IButtonMap; |
| 34 |
class IButtonMapper; |
| 35 |
|
| 36 |
/*! |
| 37 |
* \ingroup joystick |
| 38 |
* |
| 39 |
* \brief Detects and dispatches mapping events |
| 40 |
* |
| 41 |
* A mapping event usually occurs when a driver primitive is pressed or |
| 42 |
* exceeds a certain threshold. |
| 43 |
* |
| 44 |
* Detection can be quite complicated due to driver bugs, so each type of |
| 45 |
* driver primitive is given its own detector class inheriting from this one. |
| 46 |
*/ |
| 47 |
class CPrimitiveDetector |
| 48 |
{ |
| 49 |
protected: |
| 50 |
CPrimitiveDetector(CButtonMapping* buttonMapping); |
| 51 |
|
| 52 |
/*! |
| 53 |
* \brief Dispatch a mapping event |
| 54 |
* |
| 55 |
* \return True if the primitive was mapped, false otherwise |
| 56 |
*/ |
| 57 |
bool MapPrimitive(const CDriverPrimitive& primitive); |
| 58 |
|
| 59 |
private: |
| 60 |
CButtonMapping* const m_buttonMapping; |
| 61 |
}; |
| 62 |
|
| 63 |
/*! |
| 64 |
* \ingroup joystick |
| 65 |
* |
| 66 |
* \brief Detects when a button should be mapped |
| 67 |
*/ |
| 68 |
class CButtonDetector : public CPrimitiveDetector |
| 69 |
{ |
| 70 |
public: |
| 71 |
CButtonDetector(CButtonMapping* buttonMapping, unsigned int buttonIndex); |
| 72 |
|
| 73 |
/*! |
| 74 |
* \brief Button state has been updated |
| 75 |
* |
| 76 |
* \param bPressed The new state |
| 77 |
* |
| 78 |
* \return True if this press was handled, false if it should fall through |
| 79 |
* to the next driver handler |
| 80 |
*/ |
| 81 |
bool OnMotion(bool bPressed); |
| 82 |
|
| 83 |
private: |
| 84 |
// Construction parameters |
| 85 |
const unsigned int m_buttonIndex; |
| 86 |
}; |
| 87 |
|
| 88 |
/*! |
| 89 |
* \ingroup joystick |
| 90 |
* |
| 91 |
* \brief Detects when a D-pad direction should be mapped |
| 92 |
*/ |
| 93 |
class CHatDetector : public CPrimitiveDetector |
| 94 |
{ |
| 95 |
public: |
| 96 |
CHatDetector(CButtonMapping* buttonMapping, unsigned int hatIndex); |
| 97 |
|
| 98 |
/*! |
| 99 |
* \brief Hat state has been updated |
| 100 |
* |
| 101 |
* \param state The new state |
| 102 |
* |
| 103 |
* \return True if state is a cardinal direction, false otherwise |
| 104 |
*/ |
| 105 |
bool OnMotion(HAT_STATE state); |
| 106 |
|
| 107 |
private: |
| 108 |
// Construction parameters |
| 109 |
const unsigned int m_hatIndex; |
| 110 |
}; |
| 111 |
|
| 112 |
struct AxisConfiguration |
| 113 |
{ |
| 114 |
bool bKnown = false; |
| 115 |
int center = 0; |
| 116 |
unsigned int range = 1; |
| 117 |
bool bLateDiscovery = false; |
| 118 |
}; |
| 119 |
|
| 120 |
/*! |
| 121 |
* \ingroup joystick |
| 122 |
* |
| 123 |
* \brief Detects when an axis should be mapped |
| 124 |
*/ |
| 125 |
class CAxisDetector : public CPrimitiveDetector |
| 126 |
{ |
| 127 |
public: |
| 128 |
CAxisDetector(CButtonMapping* buttonMapping, |
| 129 |
unsigned int axisIndex, |
| 130 |
const AxisConfiguration& config); |
| 131 |
|
| 132 |
/*! |
| 133 |
* \brief Axis state has been updated |
| 134 |
* |
| 135 |
* \param position The new state |
| 136 |
* |
| 137 |
* \return Always true - axis motion events are always absorbed while button mapping |
| 138 |
*/ |
| 139 |
bool OnMotion(float position); |
| 140 |
|
| 141 |
/*! |
| 142 |
* \brief Called once per frame |
| 143 |
* |
| 144 |
* If an axis was activated, the button mapping command will be emitted |
| 145 |
* here. |
| 146 |
*/ |
| 147 |
void ProcessMotion(); |
| 148 |
|
| 149 |
/*! |
| 150 |
* \brief Check if the axis was mapped and is still in motion |
| 151 |
* |
| 152 |
* \return True between when the axis is mapped and when it crosses zero |
| 153 |
*/ |
| 154 |
bool IsMapping() const { return m_state == AXIS_STATE::MAPPED; } |
| 155 |
|
| 156 |
/*! |
| 157 |
* \brief Set the state such that this axis has generated a mapping event |
| 158 |
* |
| 159 |
* If an axis is mapped to the Select action, it may be pressed when button |
| 160 |
* mapping begins. This function is used to indicate that the axis shouldn't |
| 161 |
* be mapped until after it crosses zero again. |
| 162 |
*/ |
| 163 |
void SetEmitted(const CDriverPrimitive& activePrimitive); |
| 164 |
|
| 165 |
private: |
| 166 |
enum class AXIS_STATE |
| 167 |
{ |
| 168 |
/*! |
| 169 |
* \brief Axis is inactive (position is less than threshold) |
| 170 |
*/ |
| 171 |
INACTIVE, |
| 172 |
|
| 173 |
/*! |
| 174 |
* \brief Axis is activated (position has exceeded threshold) |
| 175 |
*/ |
| 176 |
ACTIVATED, |
| 177 |
|
| 178 |
/*! |
| 179 |
* \brief Axis has generated a mapping event, but has not been centered yet |
| 180 |
*/ |
| 181 |
MAPPED, |
| 182 |
}; |
| 183 |
|
| 184 |
enum class AXIS_TYPE |
| 185 |
{ |
| 186 |
/*! |
| 187 |
* \brief Axis type is initially unknown |
| 188 |
*/ |
| 189 |
UNKNOWN, |
| 190 |
|
| 191 |
/*! |
| 192 |
* \brief Axis is centered about 0 |
| 193 |
* |
| 194 |
* - If the axis is an analog stick, it can travel to -1 or +1. |
| 195 |
* - If the axis is a pressure-sensitive button or a normal trigger, |
| 196 |
* it can travel to +1. |
| 197 |
* - If the axis is a DirectInput trigger, then it is possible that two |
| 198 |
* triggers can be on the same axis in opposite directions. |
| 199 |
* - Normally, D-pads appear as a hat or four buttons. However, some |
| 200 |
* D-pads are reported as two axes that can have the discrete values |
| 201 |
* -1, 0 or 1. This is called a "discrete D-pad". |
| 202 |
*/ |
| 203 |
NORMAL, |
| 204 |
|
| 205 |
/*! |
| 206 |
* \brief Axis is centered about -1 or 1 |
| 207 |
* |
| 208 |
* - On OSX, with the cocoa driver, triggers are centered about -1 and |
| 209 |
* travel to +1. In this case, the range is 2 and the direction is |
| 210 |
* positive. |
| 211 |
* - The author of SDL has observed triggers centered at +1 and travel |
| 212 |
* to 0. In this case, the range is 1 and the direction is negative. |
| 213 |
*/ |
| 214 |
OFFSET, |
| 215 |
}; |
| 216 |
|
| 217 |
void DetectType(float position); |
| 218 |
|
| 219 |
// Construction parameters |
| 220 |
const unsigned int m_axisIndex; |
| 221 |
AxisConfiguration m_config; // mutable |
| 222 |
|
| 223 |
// State variables |
| 224 |
AXIS_STATE m_state = AXIS_STATE::INACTIVE; |
| 225 |
CDriverPrimitive m_activatedPrimitive; |
| 226 |
AXIS_TYPE m_type = AXIS_TYPE::UNKNOWN; |
| 227 |
bool m_initialPositionKnown = false; // set to true on first motion |
| 228 |
float m_initialPosition = 0.0f; // set to position of first motion |
| 229 |
bool m_initialPositionChanged = |
| 230 |
false; // set to true when position differs from the initial position |
| 231 |
std::chrono::time_point<std::chrono::steady_clock> |
| 232 |
m_activationTimeMs; // only used to delay anomalous trigger mapping to detect full range |
| 233 |
}; |
| 234 |
|
| 235 |
/*! |
| 236 |
* \ingroup joystick |
| 237 |
* |
| 238 |
* \brief Detects when a keyboard key should be mapped |
| 239 |
*/ |
| 240 |
class CKeyDetector : public CPrimitiveDetector |
| 241 |
{ |
| 242 |
public: |
| 243 |
CKeyDetector(CButtonMapping* buttonMapping, XBMCKey keycode); |
| 244 |
|
| 245 |
/*! |
| 246 |
* \brief Key state has been updated |
| 247 |
* |
| 248 |
* \param bPressed The new state |
| 249 |
* |
| 250 |
* \return True if this press was handled, false if it should fall through |
| 251 |
* to the next driver handler |
| 252 |
*/ |
| 253 |
bool OnMotion(bool bPressed); |
| 254 |
|
| 255 |
private: |
| 256 |
// Construction parameters |
| 257 |
const XBMCKey m_keycode; |
| 258 |
}; |
| 259 |
|
| 260 |
/*! |
| 261 |
* \ingroup joystick |
| 262 |
* |
| 263 |
* \brief Detects when a mouse button should be mapped |
| 264 |
*/ |
| 265 |
class CMouseButtonDetector : public CPrimitiveDetector |
| 266 |
{ |
| 267 |
public: |
| 268 |
CMouseButtonDetector(CButtonMapping* buttonMapping, MOUSE::BUTTON_ID buttonIndex); |
| 269 |
|
| 270 |
/*! |
| 271 |
* \brief Button state has been updated |
| 272 |
* |
| 273 |
* \param bPressed The new state |
| 274 |
* |
| 275 |
* \return True if this press was handled, false if it should fall through |
| 276 |
* to the next driver handler |
| 277 |
*/ |
| 278 |
bool OnMotion(bool bPressed); |
| 279 |
|
| 280 |
private: |
| 281 |
// Construction parameters |
| 282 |
const MOUSE::BUTTON_ID m_buttonIndex; |
| 283 |
}; |
| 284 |
|
| 285 |
/*! |
| 286 |
* \ingroup joystick |
| 287 |
* |
| 288 |
* \brief Detects when a mouse button should be mapped |
| 289 |
*/ |
| 290 |
class CPointerDetector : public CPrimitiveDetector |
| 291 |
{ |
| 292 |
public: |
| 293 |
CPointerDetector(CButtonMapping* buttonMapping); |
| 294 |
|
| 295 |
/*! |
| 296 |
* \brief Pointer position has been updated |
| 297 |
* |
| 298 |
* \param x The new x coordinate |
| 299 |
* \param y The new y coordinate |
| 300 |
* |
| 301 |
* \return Always true - pointer motion events are always absorbed while |
| 302 |
* button mapping |
| 303 |
*/ |
| 304 |
bool OnMotion(int x, int y); |
| 305 |
|
| 306 |
private: |
| 307 |
// Utility function |
| 308 |
static INPUT::INTERCARDINAL_DIRECTION GetPointerDirection(int x, int y); |
| 309 |
|
| 310 |
static const unsigned int MIN_FRAME_COUNT = 10; |
| 311 |
|
| 312 |
// State variables |
| 313 |
bool m_bStarted = false; |
| 314 |
int m_startX = 0; |
| 315 |
int m_startY = 0; |
| 316 |
unsigned int m_frameCount = 0; |
| 317 |
}; |
| 318 |
|
| 319 |
/*! |
| 320 |
* \ingroup joystick |
| 321 |
* |
| 322 |
* \brief Generic implementation of a class that provides button mapping by |
| 323 |
* translating driver events to button mapping commands |
| 324 |
* |
| 325 |
* Button mapping commands are invoked instantly for buttons and hats. |
| 326 |
* |
| 327 |
* Button mapping commands are deferred for a short while after an axis is |
| 328 |
* activated, and only one button mapping command will be invoked per |
| 329 |
* activation. |
| 330 |
*/ |
| 331 |
class CButtonMapping : public IDriverHandler, |
| 332 |
public KEYBOARD::IKeyboardDriverHandler, |
| 333 |
public MOUSE::IMouseDriverHandler, |
| 334 |
public IButtonMapCallback |
| 335 |
{ |
| 336 |
public: |
| 337 |
/*! |
| 338 |
* \brief Constructor for CButtonMapping |
| 339 |
* |
| 340 |
* \param buttonMapper Carries out button-mapping commands using <buttonMap> |
| 341 |
* \param buttonMap The button map given to <buttonMapper> on each command |
| 342 |
*/ |
| 343 |
CButtonMapping(IButtonMapper* buttonMapper, IButtonMap* buttonMap, KEYMAP::IKeymap* keymap); |
| 344 |
|
| 345 |
~CButtonMapping() override = default; |
| 346 |
|
| 347 |
// implementation of IDriverHandler |
| 348 |
bool OnButtonMotion(unsigned int buttonIndex, bool bPressed) override; |
| 349 |
bool OnHatMotion(unsigned int hatIndex, HAT_STATE state) override; |
| 350 |
bool OnAxisMotion(unsigned int axisIndex, |
| 351 |
float position, |
| 352 |
int center, |
| 353 |
unsigned int range) override; |
| 354 |
void OnInputFrame() override; |
| 355 |
|
| 356 |
// implementation of IKeyboardDriverHandler |
| 357 |
bool OnKeyPress(const CKey& key) override; |
| 358 |
void OnKeyRelease(const CKey& key) override {} |
| 359 |
|
| 360 |
// implementation of IMouseDriverHandler |
| 361 |
bool OnPosition(int x, int y) override; |
| 362 |
bool OnButtonPress(MOUSE::BUTTON_ID button) override; |
| 363 |
void OnButtonRelease(MOUSE::BUTTON_ID button) override; |
| 364 |
|
| 365 |
// implementation of IButtonMapCallback |
| 366 |
void SaveButtonMap() override; |
| 367 |
void ResetIgnoredPrimitives() override; |
| 368 |
void RevertButtonMap() override; |
| 369 |
|
| 370 |
/*! |
| 371 |
* \brief Process the primitive mapping command |
| 372 |
* |
| 373 |
* First, this function checks if the input should be dropped. This can |
| 374 |
* happen if the input is ignored or the cooldown period is active. If the |
| 375 |
* input is dropped, this returns true with no effect, effectively absorbing |
| 376 |
* the input. Otherwise, the mapping command is sent to m_buttonMapper. |
| 377 |
* |
| 378 |
* \param primitive The primitive being mapped |
| 379 |
* \return True if the mapping command was handled, false otherwise |
| 380 |
*/ |
| 381 |
bool MapPrimitive(const CDriverPrimitive& primitive); |
| 382 |
|
| 383 |
private: |
| 384 |
bool IsMapping() const; |
| 385 |
|
| 386 |
void OnLateDiscovery(unsigned int axisIndex); |
| 387 |
|
| 388 |
CButtonDetector& GetButton(unsigned int buttonIndex); |
| 389 |
CHatDetector& GetHat(unsigned int hatIndex); |
| 390 |
CAxisDetector& GetAxis(unsigned int axisIndex, |
| 391 |
float position, |
| 392 |
const AxisConfiguration& initialConfig = AxisConfiguration()); |
| 393 |
CKeyDetector& GetKey(XBMCKey keycode); |
| 394 |
CMouseButtonDetector& GetMouseButton(MOUSE::BUTTON_ID buttonIndex); |
| 395 |
CPointerDetector& GetPointer(); |
| 396 |
|
| 397 |
// Construction parameters |
| 398 |
IButtonMapper* const m_buttonMapper; |
| 399 |
IButtonMap* const m_buttonMap; |
| 400 |
KEYMAP::IKeymap* const m_keymap; |
| 401 |
|
| 402 |
std::map<unsigned int, CButtonDetector> m_buttons; |
| 403 |
std::map<unsigned int, CHatDetector> m_hats; |
| 404 |
std::map<unsigned int, CAxisDetector> m_axes; |
| 405 |
std::map<XBMCKey, CKeyDetector> m_keys; |
| 406 |
std::map<MOUSE::BUTTON_ID, CMouseButtonDetector> m_mouseButtons; |
| 407 |
std::unique_ptr<CPointerDetector> m_pointer; |
| 408 |
std::chrono::time_point<std::chrono::steady_clock> m_lastAction; |
| 409 |
uint64_t m_frameCount = 0; |
| 410 |
}; |
| 411 |
} // namespace JOYSTICK |
| 412 |
} // namespace KODI |