| 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 "CustomControllerTranslator.h" |
| 10 |
|
| 11 |
#include "input/WindowTranslator.h" //! @todo |
| 12 |
#include "input/actions/ActionIDs.h" |
| 13 |
#include "input/actions/ActionTranslator.h" |
| 14 |
#include "utils/log.h" |
| 15 |
|
| 16 |
#include <tinyxml2.h> |
| 17 |
|
| 18 |
using namespace KODI; |
| 19 |
using namespace KEYMAP; |
| 20 |
|
| 21 |
void CCustomControllerTranslator::MapActions(int windowID, |
| 22 |
const tinyxml2::XMLNode* pCustomController) |
| 23 |
{ |
| 24 |
CustomControllerButtonMap buttonMap; |
| 25 |
std::string controllerName; |
| 26 |
|
| 27 |
const auto* pController = pCustomController->ToElement(); |
| 28 |
if (pController != nullptr) |
| 29 |
{ |
| 30 |
// Transform loose name to new family, including altnames |
| 31 |
const char* name = pController->Attribute("name"); |
| 32 |
if (name != nullptr) |
| 33 |
controllerName = name; |
| 34 |
} |
| 35 |
|
| 36 |
if (controllerName.empty()) |
| 37 |
{ |
| 38 |
CLog::Log(LOGERROR, "Missing attribute \"name\" for tag \"customcontroller\""); |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
// Parse map |
| 43 |
const auto* pButton = pCustomController->FirstChildElement(); |
| 44 |
int id = 0; |
| 45 |
while (pButton != nullptr) |
| 46 |
{ |
| 47 |
std::string action; |
| 48 |
if (!pButton->NoChildren()) |
| 49 |
action = pButton->FirstChild()->Value(); |
| 50 |
|
| 51 |
if ((pButton->QueryIntAttribute("id", &id) == tinyxml2::XML_SUCCESS) && id >= 0) |
| 52 |
{ |
| 53 |
buttonMap[id] = action; |
| 54 |
} |
| 55 |
else |
| 56 |
CLog::Log(LOGERROR, "Error reading customController map element, Invalid id: {}", id); |
| 57 |
|
| 58 |
pButton = pButton->NextSiblingElement(); |
| 59 |
} |
| 60 |
|
| 61 |
// Add/overwrite button with mapped actions |
| 62 |
for (auto button : buttonMap) |
| 63 |
m_customControllersMap[controllerName][windowID][button.first] = std::move(button.second); |
| 64 |
} |
| 65 |
|
| 66 |
void CCustomControllerTranslator::Clear() |
| 67 |
{ |
| 68 |
m_customControllersMap.clear(); |
| 69 |
} |
| 70 |
|
| 71 |
bool CCustomControllerTranslator::TranslateCustomControllerString(int windowId, |
| 72 |
const std::string& controllerName, |
| 73 |
int buttonId, |
| 74 |
int& action, |
| 75 |
std::string& strAction) |
| 76 |
{ |
| 77 |
unsigned int actionId = ACTION_NONE; |
| 78 |
|
| 79 |
// handle virtual windows |
| 80 |
windowId = CWindowTranslator::GetVirtualWindow(windowId); |
| 81 |
|
| 82 |
// Try to get the action from the current window |
| 83 |
if (!TranslateString(windowId, controllerName, buttonId, actionId, strAction)) |
| 84 |
{ |
| 85 |
// if it's invalid, try to get it from fallback windows or the global map (windowId == -1) |
| 86 |
while (actionId == ACTION_NONE && windowId > -1) |
| 87 |
{ |
| 88 |
windowId = CWindowTranslator::GetFallbackWindow(windowId); |
| 89 |
TranslateString(windowId, controllerName, buttonId, actionId, strAction); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
action = actionId; |
| 94 |
return actionId != ACTION_NONE; |
| 95 |
} |
| 96 |
|
| 97 |
bool CCustomControllerTranslator::TranslateString(int windowId, |
| 98 |
const std::string& controllerName, |
| 99 |
int buttonId, |
| 100 |
unsigned int& actionId, |
| 101 |
std::string& strAction) |
| 102 |
{ |
| 103 |
// Resolve the correct custom controller |
| 104 |
auto it = m_customControllersMap.find(controllerName); |
| 105 |
if (it == m_customControllersMap.end()) |
| 106 |
return false; |
| 107 |
|
| 108 |
const CustomControllerWindowMap& windowMap = it->second; |
| 109 |
auto it2 = windowMap.find(windowId); |
| 110 |
if (it2 != windowMap.end()) |
| 111 |
{ |
| 112 |
const CustomControllerButtonMap& buttonMap = it2->second; |
| 113 |
auto it3 = buttonMap.find(buttonId); |
| 114 |
if (it3 != buttonMap.end()) |
| 115 |
{ |
| 116 |
strAction = it3->second; |
| 117 |
ACTION::CActionTranslator::TranslateString(strAction, actionId); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return actionId != ACTION_NONE; |
| 122 |
} |