| 1 |
/* |
| 2 |
* Copyright (C) 2017-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 |
#include "KeyHandler.h" |
| 10 |
|
| 11 |
#include "input/actions/ActionIDs.h" |
| 12 |
#include "input/actions/ActionTranslator.h" |
| 13 |
#include "input/actions/interfaces/IActionListener.h" |
| 14 |
#include "input/joysticks/JoystickUtils.h" |
| 15 |
#include "input/keymaps/interfaces/IKeymap.h" |
| 16 |
#include "input/keymaps/interfaces/IKeymapHandler.h" |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
#include <assert.h> |
| 20 |
|
| 21 |
using namespace KODI; |
| 22 |
using namespace KEYMAP; |
| 23 |
|
| 24 |
#define DIGITAL_ANALOG_THRESHOLD 0.5f |
| 25 |
|
| 26 |
#define HOLD_TIMEOUT_MS 500 |
| 27 |
#define REPEAT_TIMEOUT_MS 50 |
| 28 |
|
| 29 |
CKeyHandler::CKeyHandler(const std::string& keyName, |
| 30 |
ACTION::IActionListener* actionHandler, |
| 31 |
const IKeymap* keymap, |
| 32 |
IKeymapHandler* keymapHandler) |
| 33 |
: m_keyName(keyName), |
| 34 |
m_actionHandler(actionHandler), |
| 35 |
m_keymap(keymap), |
| 36 |
m_keymapHandler(keymapHandler) |
| 37 |
{ |
| 38 |
assert(m_actionHandler != nullptr); |
| 39 |
assert(m_keymap != nullptr); |
| 40 |
assert(m_keymapHandler != nullptr); |
| 41 |
|
| 42 |
Reset(); |
| 43 |
} |
| 44 |
|
| 45 |
void CKeyHandler::Reset() |
| 46 |
{ |
| 47 |
m_bHeld = false; |
| 48 |
m_magnitude = 0.0f; |
| 49 |
m_holdStartTimeMs = 0; |
| 50 |
m_lastHoldTimeMs = 0; |
| 51 |
m_bActionSent = false; |
| 52 |
m_lastActionMs = 0; |
| 53 |
m_activeWindowId = -1; |
| 54 |
m_lastAction = CAction(); |
| 55 |
} |
| 56 |
|
| 57 |
bool CKeyHandler::OnDigitalMotion(bool bPressed, unsigned int holdTimeMs) |
| 58 |
{ |
| 59 |
return OnAnalogMotion(bPressed ? 1.0f : 0.0f, holdTimeMs); |
| 60 |
} |
| 61 |
|
| 62 |
bool CKeyHandler::OnAnalogMotion(float magnitude, unsigned int motionTimeMs) |
| 63 |
{ |
| 64 |
// Don't send deactivation event more than once |
| 65 |
if (m_magnitude == 0.0f && magnitude == 0.0f) |
| 66 |
return false; |
| 67 |
|
| 68 |
// Get actions for the key |
| 69 |
const auto& actionGroup = m_keymap->GetActions(m_keyName); |
| 70 |
const int windowId = actionGroup.windowId; |
| 71 |
const auto& actions = actionGroup.actions; |
| 72 |
|
| 73 |
// Calculate press state |
| 74 |
const bool bPressed = IsPressed(magnitude); |
| 75 |
const bool bJustPressed = bPressed && !m_bHeld; |
| 76 |
|
| 77 |
if (bJustPressed) |
| 78 |
{ |
| 79 |
// Reset key if just pressed |
| 80 |
Reset(); |
| 81 |
|
| 82 |
// Record hold start time if just pressed |
| 83 |
m_holdStartTimeMs = motionTimeMs; |
| 84 |
|
| 85 |
// Record window ID |
| 86 |
if (windowId >= 0) |
| 87 |
m_activeWindowId = windowId; |
| 88 |
} |
| 89 |
|
| 90 |
// Calculate holdtime relative to when magnitude crossed the threshold |
| 91 |
unsigned int holdTimeMs = 0; |
| 92 |
if (bPressed) |
| 93 |
holdTimeMs = motionTimeMs - m_holdStartTimeMs; |
| 94 |
|
| 95 |
// Give priority to actions with hotkeys |
| 96 |
std::vector<const KeymapAction*> actionsWithHotkeys; |
| 97 |
|
| 98 |
for (const auto& action : actions) |
| 99 |
{ |
| 100 |
if (!action.hotkeys.empty()) |
| 101 |
actionsWithHotkeys.emplace_back(&action); |
| 102 |
} |
| 103 |
|
| 104 |
CAction dispatchAction = |
| 105 |
ProcessActions(std::move(actionsWithHotkeys), windowId, magnitude, holdTimeMs); |
| 106 |
|
| 107 |
// If that failed, try again with all actions |
| 108 |
if (dispatchAction.GetID() == ACTION_NONE) |
| 109 |
{ |
| 110 |
std::vector<const KeymapAction*> allActions; |
| 111 |
|
| 112 |
allActions.reserve(actions.size()); |
| 113 |
for (const auto& action : actions) |
| 114 |
allActions.emplace_back(&action); |
| 115 |
|
| 116 |
dispatchAction = ProcessActions(std::move(allActions), windowId, magnitude, holdTimeMs); |
| 117 |
} |
| 118 |
|
| 119 |
// If specific action was sent last frame but not this one, send a release event |
| 120 |
if (dispatchAction.GetID() != m_lastAction.GetID()) |
| 121 |
{ |
| 122 |
if (ACTION::CActionTranslator::IsAnalog(m_lastAction.GetID()) && |
| 123 |
m_lastAction.GetAmount() > 0.0f) |
| 124 |
{ |
| 125 |
m_lastAction.ClearAmount(); |
| 126 |
m_actionHandler->OnAction(m_lastAction); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// Dispatch action |
| 131 |
bool bHandled = false; |
| 132 |
if (dispatchAction.GetID() != ACTION_NONE) |
| 133 |
{ |
| 134 |
m_actionHandler->OnAction(dispatchAction); |
| 135 |
bHandled = true; |
| 136 |
} |
| 137 |
|
| 138 |
m_bHeld = bPressed; |
| 139 |
m_magnitude = magnitude; |
| 140 |
m_lastHoldTimeMs = holdTimeMs; |
| 141 |
m_lastAction = dispatchAction; |
| 142 |
|
| 143 |
return bHandled; |
| 144 |
} |
| 145 |
|
| 146 |
CAction CKeyHandler::ProcessActions(std::vector<const KeymapAction*> actions, |
| 147 |
int windowId, |
| 148 |
float magnitude, |
| 149 |
unsigned int holdTimeMs) |
| 150 |
{ |
| 151 |
CAction dispatchAction; |
| 152 |
|
| 153 |
// Filter out actions without pressed hotkeys |
| 154 |
actions.erase(std::remove_if(actions.begin(), actions.end(), |
| 155 |
[this](const KeymapAction* action) |
| 156 |
{ return !m_keymapHandler->HotkeysPressed(action->hotkeys); }), |
| 157 |
actions.end()); |
| 158 |
|
| 159 |
if (actions.empty()) |
| 160 |
return false; |
| 161 |
|
| 162 |
// Actions are sorted by holdtime, so the final action is the one with the |
| 163 |
// greatest holdtime |
| 164 |
const KeymapAction& finalAction = **actions.rbegin(); |
| 165 |
const unsigned int maxHoldTimeMs = finalAction.holdTimeMs; |
| 166 |
|
| 167 |
const bool bHasDelay = (maxHoldTimeMs > 0); |
| 168 |
if (!bHasDelay) |
| 169 |
{ |
| 170 |
dispatchAction = ProcessAction(finalAction, windowId, magnitude, holdTimeMs); |
| 171 |
} |
| 172 |
else |
| 173 |
{ |
| 174 |
// If holdtime has exceeded the last action, execute it now |
| 175 |
if (holdTimeMs >= finalAction.holdTimeMs) |
| 176 |
{ |
| 177 |
// Force holdtime to zero for the initial press |
| 178 |
if (!m_bActionSent) |
| 179 |
holdTimeMs = 0; |
| 180 |
else |
| 181 |
holdTimeMs -= finalAction.holdTimeMs; |
| 182 |
|
| 183 |
dispatchAction = ProcessAction(finalAction, windowId, magnitude, holdTimeMs); |
| 184 |
} |
| 185 |
else |
| 186 |
{ |
| 187 |
// Calculate press state |
| 188 |
const bool bPressed = IsPressed(magnitude); |
| 189 |
const bool bJustReleased = m_bHeld && !bPressed; |
| 190 |
|
| 191 |
// If button was just released, send a release action |
| 192 |
if (bJustReleased) |
| 193 |
dispatchAction = ProcessRelease(actions, windowId); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return dispatchAction; |
| 198 |
} |
| 199 |
|
| 200 |
CAction CKeyHandler::ProcessRelease(std::vector<const KeymapAction*> actions, int windowId) |
| 201 |
{ |
| 202 |
CAction dispatchAction; |
| 203 |
|
| 204 |
// Use previous holdtime from before button release |
| 205 |
const unsigned int holdTimeMs = m_lastHoldTimeMs; |
| 206 |
|
| 207 |
// Send an action on release if one occurs before the holdtime |
| 208 |
for (auto it = actions.begin(); it != actions.end();) |
| 209 |
{ |
| 210 |
const KeymapAction& action = **it; |
| 211 |
|
| 212 |
unsigned int thisHoldTime = (*it)->holdTimeMs; |
| 213 |
|
| 214 |
++it; |
| 215 |
if (it == actions.end()) |
| 216 |
break; |
| 217 |
|
| 218 |
unsigned int nextHoldTime = (*it)->holdTimeMs; |
| 219 |
|
| 220 |
if (thisHoldTime <= holdTimeMs && holdTimeMs < nextHoldTime) |
| 221 |
{ |
| 222 |
dispatchAction = ProcessAction(action, windowId, 1.0f, 0); |
| 223 |
break; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
return dispatchAction; |
| 228 |
} |
| 229 |
|
| 230 |
CAction CKeyHandler::ProcessAction(const KeymapAction& action, |
| 231 |
int windowId, |
| 232 |
float magnitude, |
| 233 |
unsigned int holdTimeMs) |
| 234 |
{ |
| 235 |
CAction dispatchAction; |
| 236 |
|
| 237 |
bool bSendAction = false; |
| 238 |
|
| 239 |
if (windowId != m_activeWindowId) |
| 240 |
{ |
| 241 |
// Don't send actions if the window has changed since being pressed |
| 242 |
} |
| 243 |
else if (ACTION::CActionTranslator::IsAnalog(action.actionId)) |
| 244 |
{ |
| 245 |
bSendAction = true; |
| 246 |
} |
| 247 |
else if (IsPressed(magnitude)) |
| 248 |
{ |
| 249 |
// Dispatch action if button was pressed this frame |
| 250 |
if (holdTimeMs == 0) |
| 251 |
bSendAction = true; |
| 252 |
else |
| 253 |
bSendAction = SendRepeatAction(holdTimeMs); |
| 254 |
} |
| 255 |
|
| 256 |
if (bSendAction) |
| 257 |
{ |
| 258 |
const CAction guiAction(action.actionId, magnitude, 0.0f, action.actionString, holdTimeMs); |
| 259 |
m_keymapHandler->OnPress(m_keyName); |
| 260 |
m_bActionSent = true; |
| 261 |
m_lastActionMs = holdTimeMs; |
| 262 |
dispatchAction = guiAction; |
| 263 |
} |
| 264 |
|
| 265 |
return dispatchAction; |
| 266 |
} |
| 267 |
|
| 268 |
bool CKeyHandler::SendRepeatAction(unsigned int holdTimeMs) |
| 269 |
{ |
| 270 |
bool bSendRepeat = true; |
| 271 |
|
| 272 |
// Don't send a repeat action if the last key has changed |
| 273 |
if (m_keymapHandler->GetLastPressed() != m_keyName) |
| 274 |
bSendRepeat = false; |
| 275 |
|
| 276 |
// Ensure initial timeout has elapsed |
| 277 |
else if (holdTimeMs < HOLD_TIMEOUT_MS) |
| 278 |
bSendRepeat = false; |
| 279 |
|
| 280 |
// Ensure repeat timeout has elapsed |
| 281 |
else if (holdTimeMs < m_lastActionMs + REPEAT_TIMEOUT_MS) |
| 282 |
bSendRepeat = false; |
| 283 |
|
| 284 |
return bSendRepeat; |
| 285 |
} |
| 286 |
|
| 287 |
bool CKeyHandler::IsPressed(float magnitude) |
| 288 |
{ |
| 289 |
return magnitude >= DIGITAL_ANALOG_THRESHOLD; |
| 290 |
} |