| 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 "TouchTranslator.h" |
| 10 |
|
| 11 |
#include "input/WindowTranslator.h" //! @todo |
| 12 |
#include "input/actions/ActionIDs.h" |
| 13 |
#include "input/actions/ActionTranslator.h" |
| 14 |
#include "utils/StringUtils.h" |
| 15 |
#include "utils/log.h" |
| 16 |
|
| 17 |
#include <map> |
| 18 |
|
| 19 |
#include <tinyxml2.h> |
| 20 |
|
| 21 |
using namespace KODI; |
| 22 |
using namespace KEYMAP; |
| 23 |
|
| 24 |
namespace |
| 25 |
{ |
| 26 |
using ActionName = std::string; |
| 27 |
using TouchCommandID = unsigned int; |
| 28 |
} // namespace |
| 29 |
|
| 30 |
#define TOUCH_COMMAND_NONE 0 |
| 31 |
|
| 32 |
static const std::map<ActionName, TouchCommandID> TouchCommands = { |
| 33 |
{"tap", ACTION_TOUCH_TAP}, |
| 34 |
{"longpress", ACTION_TOUCH_LONGPRESS}, |
| 35 |
{"pan", ACTION_GESTURE_PAN}, |
| 36 |
{"zoom", ACTION_GESTURE_ZOOM}, |
| 37 |
{"rotate", ACTION_GESTURE_ROTATE}, |
| 38 |
{"swipeleft", ACTION_GESTURE_SWIPE_LEFT}, |
| 39 |
{"swiperight", ACTION_GESTURE_SWIPE_RIGHT}, |
| 40 |
{"swipeup", ACTION_GESTURE_SWIPE_UP}, |
| 41 |
{"swipedown", ACTION_GESTURE_SWIPE_DOWN}}; |
| 42 |
|
| 43 |
void CTouchTranslator::MapActions(int windowID, const tinyxml2::XMLNode* pTouch) |
| 44 |
{ |
| 45 |
if (pTouch == nullptr) |
| 46 |
return; |
| 47 |
|
| 48 |
TouchActionMap map; |
| 49 |
|
| 50 |
// Check if there already is a touch map for the window ID |
| 51 |
auto it = m_touchMap.find(windowID); |
| 52 |
if (it != m_touchMap.end()) |
| 53 |
{ |
| 54 |
// Get the existing touch map and remove it from the window mapping as it |
| 55 |
// will be inserted later on |
| 56 |
map = std::move(it->second); |
| 57 |
m_touchMap.erase(it); |
| 58 |
} |
| 59 |
|
| 60 |
const auto* pTouchElem = pTouch->ToElement(); |
| 61 |
if (pTouchElem == nullptr) |
| 62 |
return; |
| 63 |
|
| 64 |
const auto* pButton = pTouchElem->FirstChildElement(); |
| 65 |
while (pButton != nullptr) |
| 66 |
{ |
| 67 |
CTouchAction action; |
| 68 |
unsigned int touchActionKey = TranslateTouchCommand(pButton, action); |
| 69 |
if (touchActionKey != ACTION_NONE) |
| 70 |
{ |
| 71 |
// check if there already is a mapping for the parsed action |
| 72 |
// and remove it if necessary |
| 73 |
TouchActionMap::iterator actionIt = map.find(touchActionKey); |
| 74 |
if (actionIt != map.end()) |
| 75 |
map.erase(actionIt); |
| 76 |
|
| 77 |
map.insert(std::make_pair(touchActionKey, std::move(action))); |
| 78 |
} |
| 79 |
|
| 80 |
pButton = pButton->NextSiblingElement(); |
| 81 |
} |
| 82 |
|
| 83 |
// add the modified touch map with the window ID |
| 84 |
if (!map.empty()) |
| 85 |
m_touchMap.insert(std::make_pair(windowID, std::move(map))); |
| 86 |
} |
| 87 |
|
| 88 |
void CTouchTranslator::Clear() |
| 89 |
{ |
| 90 |
m_touchMap.clear(); |
| 91 |
} |
| 92 |
|
| 93 |
bool CTouchTranslator::TranslateTouchAction( |
| 94 |
int window, int touchAction, int touchPointers, int& action, std::string& actionString) |
| 95 |
{ |
| 96 |
if (touchAction < 0) |
| 97 |
return false; |
| 98 |
|
| 99 |
unsigned int actionId = ACTION_NONE; |
| 100 |
|
| 101 |
// handle virtual windows |
| 102 |
window = CWindowTranslator::GetVirtualWindow(window); |
| 103 |
|
| 104 |
if (!TranslateAction(window, touchAction, touchPointers, actionId, actionString)) |
| 105 |
{ |
| 106 |
// if it's invalid, try to get it from fallback windows or the global map (window == -1) |
| 107 |
while (actionId == ACTION_NONE && window > -1) |
| 108 |
{ |
| 109 |
window = CWindowTranslator::GetFallbackWindow(window); |
| 110 |
TranslateAction(window, touchAction, touchPointers, actionId, actionString); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
action = actionId; |
| 115 |
return actionId != ACTION_NONE; |
| 116 |
} |
| 117 |
|
| 118 |
bool CTouchTranslator::TranslateAction(int window, |
| 119 |
unsigned int touchCommand, |
| 120 |
int touchPointers, |
| 121 |
unsigned int& actionId, |
| 122 |
std::string& actionString) |
| 123 |
{ |
| 124 |
unsigned int touchActionKey = GetTouchActionKey(touchCommand, touchPointers); |
| 125 |
|
| 126 |
actionId = GetActionID(window, touchActionKey, actionString); |
| 127 |
|
| 128 |
return actionId != ACTION_NONE; |
| 129 |
} |
| 130 |
|
| 131 |
unsigned int CTouchTranslator::GetActionID(WindowID window, |
| 132 |
TouchActionKey touchActionKey, |
| 133 |
std::string& actionString) |
| 134 |
{ |
| 135 |
auto windowIt = m_touchMap.find(window); |
| 136 |
if (windowIt == m_touchMap.end()) |
| 137 |
return ACTION_NONE; |
| 138 |
|
| 139 |
auto touchIt = windowIt->second.find(touchActionKey); |
| 140 |
if (touchIt == windowIt->second.end()) |
| 141 |
return ACTION_NONE; |
| 142 |
|
| 143 |
actionString = touchIt->second.strAction; |
| 144 |
return touchIt->second.actionId; |
| 145 |
} |
| 146 |
|
| 147 |
unsigned int CTouchTranslator::TranslateTouchCommand(const tinyxml2::XMLElement* pButton, |
| 148 |
CTouchAction& action) |
| 149 |
{ |
| 150 |
const char* szButton = pButton->Value(); |
| 151 |
if (szButton == nullptr || pButton->FirstChild() == nullptr) |
| 152 |
return ACTION_NONE; |
| 153 |
|
| 154 |
const char* szAction = pButton->FirstChild()->Value(); |
| 155 |
if (szAction == nullptr) |
| 156 |
return ACTION_NONE; |
| 157 |
|
| 158 |
std::string strTouchCommand = szButton; |
| 159 |
StringUtils::ToLower(strTouchCommand); |
| 160 |
|
| 161 |
// Handle direction |
| 162 |
const char* attrVal = pButton->Attribute("direction"); |
| 163 |
if (attrVal != nullptr) |
| 164 |
strTouchCommand += attrVal; |
| 165 |
|
| 166 |
// Lookup command |
| 167 |
unsigned int touchCommandId = TOUCH_COMMAND_NONE; |
| 168 |
auto it = TouchCommands.find(strTouchCommand); |
| 169 |
if (it != TouchCommands.end()) |
| 170 |
touchCommandId = it->second; |
| 171 |
|
| 172 |
if (touchCommandId == TOUCH_COMMAND_NONE) |
| 173 |
{ |
| 174 |
CLog::Log(LOGERROR, "{}: Can't find touch command {}", __FUNCTION__, szButton); |
| 175 |
return ACTION_NONE; |
| 176 |
} |
| 177 |
|
| 178 |
// Handle pointers |
| 179 |
int pointers = 1; |
| 180 |
attrVal = pButton->Attribute("pointers"); |
| 181 |
if (attrVal != nullptr) |
| 182 |
pointers = (int)strtol(attrVal, nullptr, 0); |
| 183 |
|
| 184 |
unsigned int touchActionKey = GetTouchActionKey(touchCommandId, pointers); |
| 185 |
|
| 186 |
action.strAction = szAction; |
| 187 |
if (!ACTION::CActionTranslator::TranslateString(action.strAction, action.actionId) || |
| 188 |
action.actionId == ACTION_NONE) |
| 189 |
return ACTION_NONE; |
| 190 |
|
| 191 |
return touchActionKey; |
| 192 |
} |
| 193 |
|
| 194 |
unsigned int CTouchTranslator::GetTouchActionKey(unsigned int touchCommandId, int touchPointers) |
| 195 |
{ |
| 196 |
if (touchPointers <= 0) |
| 197 |
touchPointers = 1; |
| 198 |
|
| 199 |
return touchCommandId + touchPointers - 1; |
| 200 |
} |