summaryrefslogtreecommitdiffstats
path: root/xbmc/input/joysticks/interfaces/IButtonMapper.h
blob: d0784df107f117512311dbc7cecbe0b72b7e399e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 *  Copyright (C) 2014-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "input/joysticks/JoystickTypes.h"

#include <map>
#include <string>

class IKeymap;

namespace KODI
{
namespace JOYSTICK
{
class CDriverPrimitive;
class IButtonMap;
class IButtonMapCallback;

/*!
 * \ingroup joystick
 * \brief Button mapper interface to assign the driver's raw button/hat/axis
 *        elements to physical joystick features using a provided button map.
 *
 * \sa IButtonMap
 */
class IButtonMapper
{
public:
  IButtonMapper() = default;

  virtual ~IButtonMapper() = default;

  /*!
   * \brief The add-on ID of the game controller associated with this button mapper
   *
   * \return The ID of the add-on extending kodi.game.controller
   */
  virtual std::string ControllerID(void) const = 0;

  /*!
   * \brief Return true if the button mapper wants a cooldown between button
   *        mapping commands
   *
   * \return True to only send button mapping commands that occur after a small
   *         timeout from the previous command.
   */
  virtual bool NeedsCooldown(void) const = 0;

  /*!
   * \brief Return true if the button mapper accepts primitives of the given type
   *
   * \param type The primitive type
   *
   * \return True if the button mapper can map the primitive type, false otherwise
   */
  virtual bool AcceptsPrimitive(PRIMITIVE_TYPE type) const = 0;

  /*!
   * \brief Handle button/hat press or axis threshold
   *
   * \param buttonMap  The button map being manipulated
   * \param keymap     An interface capable of translating features to Kodi actions
   * \param primitive  The driver primitive
   *
   * Called in the same thread as \ref IButtonMapper::OnFrame.
   *
   * \return True if driver primitive was mapped to a feature
   */
  virtual bool MapPrimitive(IButtonMap* buttonMap,
                            IKeymap* keyMap,
                            const CDriverPrimitive& primitive) = 0;

  /*!
   * \brief Called once per event frame to notify the implementation of motion status
   *
   * \param buttonMap The button map passed to MapPrimitive() (shall not be modified)
   * \param bMotion True if a previously-mapped axis is still in motion
   *
   * This allows the implementer to wait for an axis to be centered before
   * allowing it to be used as Kodi input.
   *
   * If mapping finishes on an axis, then the axis will still be pressed and
   * sending input every frame when the mapping ends. For example, when the
   * right analog stick is the last feature to be mapped, it is still pressed
   * when mapping ends and immediately sends Volume Down actions.
   *
   * The fix is to allow implementers to wait until all axes are motionless
   * before detaching themselves.
   *
   * Called in the same thread as \ref IButtonMapper::MapPrimitive.
   */
  virtual void OnEventFrame(const IButtonMap* buttonMap, bool bMotion) = 0;

  /*!
   * \brief Called when an axis has been detected after mapping began
   *
   * \param axisIndex The index of the axis being discovered
   *
   * Some joystick drivers don't report an initial value for analog axes.
   *
   * Called in the same thread as \ref IButtonMapper::MapPrimitive.
   */
  virtual void OnLateAxis(const IButtonMap* buttonMap, unsigned int axisIndex) = 0;

  // Button map callback interface
  void SetButtonMapCallback(const std::string& deviceLocation, IButtonMapCallback* callback)
  {
    m_callbacks[deviceLocation] = callback;
  }
  void ResetButtonMapCallbacks(void) { m_callbacks.clear(); }
  std::map<std::string, IButtonMapCallback*>& ButtonMapCallbacks(void) { return m_callbacks; }

private:
  std::map<std::string, IButtonMapCallback*> m_callbacks; // Device location -> callback
};
} // namespace JOYSTICK
} // namespace KODI