summaryrefslogtreecommitdiffstats
path: root/xbmc/input/joysticks/keymaps/KeyHandler.h
blob: 3b5a46cf0641784a6326c0018dba8df9a63fc944 (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
/*
 *  Copyright (C) 2017-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/actions/Action.h"
#include "input/joysticks/JoystickTypes.h"
#include "input/joysticks/interfaces/IKeyHandler.h"

#include <map>
#include <string>
#include <vector>

class CAction;
class IActionListener;
class IKeymap;

namespace KODI
{
namespace JOYSTICK
{
class IKeymapHandler;

/*!
 * \ingroup joystick
 * \brief
 */
class CKeyHandler : public IKeyHandler
{
public:
  CKeyHandler(const std::string& keyName,
              IActionListener* actionHandler,
              const IKeymap* keymap,
              IKeymapHandler* keymapHandler);

  ~CKeyHandler() override = default;

  // implementation of IKeyHandler
  bool IsPressed() const override { return m_bHeld; }
  bool OnDigitalMotion(bool bPressed, unsigned int holdTimeMs) override;
  bool OnAnalogMotion(float magnitude, unsigned int motionTimeMs) override;

private:
  void Reset();

  /*!
   * \brief Process actions to see if an action should be dispatched
   *
   * \param actions All actions from the keymap defined for the current window
   * \param windowId The current window ID
   * \param magnitude The magnitude or distance of the feature being handled
   * \param holdTimeMs The time which the feature has been past the hold threshold
   *
   * \return The action to dispatch, or action with ID ACTION_NONE if no action should be dispatched
   */
  CAction ProcessActions(std::vector<const KeymapAction*> actions,
                         int windowId,
                         float magnitude,
                         unsigned int holdTimeMs);

  /*!
   * \brief Process actions after release event to see if an action should be dispatched
   *
   * \param actions All actions from the keymap defined for the current window
   * \param windowId The current window ID
   *
   * \return The action to dispatch, or action with ID ACTION_NONE if no action should be dispatched
   */
  CAction ProcessRelease(std::vector<const KeymapAction*> actions, int windowId);

  /*!
   * \brief Process an action to see if it should be dispatched
   *
   * \param action The action chosen to be dispatched
   * \param windowId The current window ID
   * \param magnitude The magnitude or distance of the feature being handled
   * \param holdTimeMs The time which the feature has been past the hold threshold
   *
   * \return The action to dispatch, or action with ID ACTION_NONE if no action should be dispatched
   */
  CAction ProcessAction(const KeymapAction& action,
                        int windowId,
                        float magnitude,
                        unsigned int holdTimeMs);

  // Check criteria for sending a repeat action
  bool SendRepeatAction(unsigned int holdTimeMs);

  // Helper function
  static bool IsPressed(float magnitude);

  // Construction parameters
  const std::string m_keyName;
  IActionListener* const m_actionHandler;
  const IKeymap* const m_keymap;
  IKeymapHandler* const m_keymapHandler;

  // State variables
  bool m_bHeld;
  float m_magnitude;
  unsigned int m_holdStartTimeMs;
  unsigned int m_lastHoldTimeMs;
  bool m_bActionSent;
  unsigned int m_lastActionMs;
  int m_activeWindowId = -1; // Window that activated the key
  CAction m_lastAction;
};
} // namespace JOYSTICK
} // namespace KODI