summaryrefslogtreecommitdiffstats
path: root/xbmc/input/joysticks/keymaps/KeyHandler.cpp
blob: cd0f30aafde56887dc546929d4fe6e6cb0d99d95 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 *  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.
 */

#include "KeyHandler.h"

#include "input/IKeymap.h"
#include "input/actions/ActionIDs.h"
#include "input/actions/ActionTranslator.h"
#include "input/joysticks/JoystickUtils.h"
#include "input/joysticks/interfaces/IKeymapHandler.h"
#include "interfaces/IActionListener.h"

#include <algorithm>
#include <assert.h>

using namespace KODI;
using namespace JOYSTICK;

#define DIGITAL_ANALOG_THRESHOLD 0.5f

#define HOLD_TIMEOUT_MS 500
#define REPEAT_TIMEOUT_MS 50

CKeyHandler::CKeyHandler(const std::string& keyName,
                         IActionListener* actionHandler,
                         const IKeymap* keymap,
                         IKeymapHandler* keymapHandler)
  : m_keyName(keyName),
    m_actionHandler(actionHandler),
    m_keymap(keymap),
    m_keymapHandler(keymapHandler)
{
  assert(m_actionHandler != nullptr);
  assert(m_keymap != nullptr);
  assert(m_keymapHandler != nullptr);

  Reset();
}

void CKeyHandler::Reset()
{
  m_bHeld = false;
  m_magnitude = 0.0f;
  m_holdStartTimeMs = 0;
  m_lastHoldTimeMs = 0;
  m_bActionSent = false;
  m_lastActionMs = 0;
  m_activeWindowId = -1;
  m_lastAction = CAction();
}

bool CKeyHandler::OnDigitalMotion(bool bPressed, unsigned int holdTimeMs)
{
  return OnAnalogMotion(bPressed ? 1.0f : 0.0f, holdTimeMs);
}

bool CKeyHandler::OnAnalogMotion(float magnitude, unsigned int motionTimeMs)
{
  // Don't send deactivation event more than once
  if (m_magnitude == 0.0f && magnitude == 0.0f)
    return false;

  // Get actions for the key
  const auto& actionGroup = m_keymap->GetActions(m_keyName);
  const int windowId = actionGroup.windowId;
  const auto& actions = actionGroup.actions;

  // Calculate press state
  const bool bPressed = IsPressed(magnitude);
  const bool bJustPressed = bPressed && !m_bHeld;

  if (bJustPressed)
  {
    // Reset key if just pressed
    Reset();

    // Record hold start time if just pressed
    m_holdStartTimeMs = motionTimeMs;

    // Record window ID
    if (windowId >= 0)
      m_activeWindowId = windowId;
  }

  // Calculate holdtime relative to when magnitude crossed the threshold
  unsigned int holdTimeMs = 0;
  if (bPressed)
    holdTimeMs = motionTimeMs - m_holdStartTimeMs;

  // Give priority to actions with hotkeys
  std::vector<const KeymapAction*> actionsWithHotkeys;

  for (const auto& action : actions)
  {
    if (!action.hotkeys.empty())
      actionsWithHotkeys.emplace_back(&action);
  }

  CAction dispatchAction =
      ProcessActions(std::move(actionsWithHotkeys), windowId, magnitude, holdTimeMs);

  // If that failed, try again with all actions
  if (dispatchAction.GetID() == ACTION_NONE)
  {
    std::vector<const KeymapAction*> allActions;

    allActions.reserve(actions.size());
    for (const auto& action : actions)
      allActions.emplace_back(&action);

    dispatchAction = ProcessActions(std::move(allActions), windowId, magnitude, holdTimeMs);
  }

  // If specific action was sent last frame but not this one, send a release event
  if (dispatchAction.GetID() != m_lastAction.GetID())
  {
    if (CActionTranslator::IsAnalog(m_lastAction.GetID()) && m_lastAction.GetAmount() > 0.0f)
    {
      m_lastAction.ClearAmount();
      m_actionHandler->OnAction(m_lastAction);
    }
  }

  // Dispatch action
  bool bHandled = false;
  if (dispatchAction.GetID() != ACTION_NONE)
  {
    m_actionHandler->OnAction(dispatchAction);
    bHandled = true;
  }

  m_bHeld = bPressed;
  m_magnitude = magnitude;
  m_lastHoldTimeMs = holdTimeMs;
  m_lastAction = dispatchAction;

  return bHandled;
}

CAction CKeyHandler::ProcessActions(std::vector<const KeymapAction*> actions,
                                    int windowId,
                                    float magnitude,
                                    unsigned int holdTimeMs)
{
  CAction dispatchAction;

  // Filter out actions without pressed hotkeys
  actions.erase(std::remove_if(actions.begin(), actions.end(),
                               [this](const KeymapAction* action) {
                                 return !m_keymapHandler->HotkeysPressed(action->hotkeys);
                               }),
                actions.end());

  if (actions.empty())
    return false;

  // Actions are sorted by holdtime, so the final action is the one with the
  // greatest holdtime
  const KeymapAction& finalAction = **actions.rbegin();
  const unsigned int maxHoldTimeMs = finalAction.holdTimeMs;

  const bool bHasDelay = (maxHoldTimeMs > 0);
  if (!bHasDelay)
  {
    dispatchAction = ProcessAction(finalAction, windowId, magnitude, holdTimeMs);
  }
  else
  {
    // If holdtime has exceeded the last action, execute it now
    if (holdTimeMs >= finalAction.holdTimeMs)
    {
      // Force holdtime to zero for the initial press
      if (!m_bActionSent)
        holdTimeMs = 0;
      else
        holdTimeMs -= finalAction.holdTimeMs;

      dispatchAction = ProcessAction(finalAction, windowId, magnitude, holdTimeMs);
    }
    else
    {
      // Calculate press state
      const bool bPressed = IsPressed(magnitude);
      const bool bJustReleased = m_bHeld && !bPressed;

      // If button was just released, send a release action
      if (bJustReleased)
        dispatchAction = ProcessRelease(actions, windowId);
    }
  }

  return dispatchAction;
}

CAction CKeyHandler::ProcessRelease(std::vector<const KeymapAction*> actions, int windowId)
{
  CAction dispatchAction;

  // Use previous holdtime from before button release
  const unsigned int holdTimeMs = m_lastHoldTimeMs;

  // Send an action on release if one occurs before the holdtime
  for (auto it = actions.begin(); it != actions.end();)
  {
    const KeymapAction& action = **it;

    unsigned int thisHoldTime = (*it)->holdTimeMs;

    ++it;
    if (it == actions.end())
      break;

    unsigned int nextHoldTime = (*it)->holdTimeMs;

    if (thisHoldTime <= holdTimeMs && holdTimeMs < nextHoldTime)
    {
      dispatchAction = ProcessAction(action, windowId, 1.0f, 0);
      break;
    }
  }

  return dispatchAction;
}

CAction CKeyHandler::ProcessAction(const KeymapAction& action,
                                   int windowId,
                                   float magnitude,
                                   unsigned int holdTimeMs)
{
  CAction dispatchAction;

  bool bSendAction = false;

  if (windowId != m_activeWindowId)
  {
    // Don't send actions if the window has changed since being pressed
  }
  else if (CActionTranslator::IsAnalog(action.actionId))
  {
    bSendAction = true;
  }
  else if (IsPressed(magnitude))
  {
    // Dispatch action if button was pressed this frame
    if (holdTimeMs == 0)
      bSendAction = true;
    else
      bSendAction = SendRepeatAction(holdTimeMs);
  }

  if (bSendAction)
  {
    const CAction guiAction(action.actionId, magnitude, 0.0f, action.actionString, holdTimeMs);
    m_keymapHandler->OnPress(m_keyName);
    m_bActionSent = true;
    m_lastActionMs = holdTimeMs;
    dispatchAction = guiAction;
  }

  return dispatchAction;
}

bool CKeyHandler::SendRepeatAction(unsigned int holdTimeMs)
{
  bool bSendRepeat = true;

  // Don't send a repeat action if the last key has changed
  if (m_keymapHandler->GetLastPressed() != m_keyName)
    bSendRepeat = false;

  // Ensure initial timeout has elapsed
  else if (holdTimeMs < HOLD_TIMEOUT_MS)
    bSendRepeat = false;

  // Ensure repeat timeout has elapsed
  else if (holdTimeMs < m_lastActionMs + REPEAT_TIMEOUT_MS)
    bSendRepeat = false;

  return bSendRepeat;
}

bool CKeyHandler::IsPressed(float magnitude)
{
  return magnitude >= DIGITAL_ANALOG_THRESHOLD;
}