| 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 "IRTranslator.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "input/remote/IRRemote.h" |
| 13 |
#include "profiles/ProfileManager.h" |
| 14 |
#include "settings/SettingsComponent.h" |
| 15 |
#include "utils/FileUtils.h" |
| 16 |
#include "utils/StringUtils.h" |
| 17 |
#include "utils/URIUtils.h" |
| 18 |
#include "utils/XBMCTinyXML2.h" |
| 19 |
#include "utils/log.h" |
| 20 |
|
| 21 |
#include <cstring> |
| 22 |
#include <memory> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
using namespace KODI; |
| 27 |
using namespace KEYMAP; |
| 28 |
|
| 29 |
void CIRTranslator::Load(const std::string& irMapName) |
| 30 |
{ |
| 31 |
if (irMapName.empty()) |
| 32 |
return; |
| 33 |
|
| 34 |
Clear(); |
| 35 |
|
| 36 |
bool success = false; |
| 37 |
|
| 38 |
std::string irMapPath = URIUtils::AddFileToFolder("special://xbmc/system/", irMapName); |
| 39 |
if (CFileUtils::Exists(irMapPath)) |
| 40 |
success |= LoadIRMap(irMapPath); |
| 41 |
else |
| 42 |
CLog::Log(LOGDEBUG, "CIRTranslator::Load - no system {} found, skipping", irMapName); |
| 43 |
|
| 44 |
irMapPath = |
| 45 |
CServiceBroker::GetSettingsComponent()->GetProfileManager()->GetUserDataItem(irMapName); |
| 46 |
if (CFileUtils::Exists(irMapPath)) |
| 47 |
success |= LoadIRMap(irMapPath); |
| 48 |
else |
| 49 |
CLog::Log(LOGDEBUG, "CIRTranslator::Load - no userdata {} found, skipping", irMapName); |
| 50 |
|
| 51 |
if (!success) |
| 52 |
CLog::Log(LOGERROR, "CIRTranslator::Load - unable to load remote map {}", irMapName); |
| 53 |
} |
| 54 |
|
| 55 |
bool CIRTranslator::LoadIRMap(const std::string& irMapPath) |
| 56 |
{ |
| 57 |
std::string remoteMapTag = URIUtils::GetFileName(irMapPath); |
| 58 |
size_t lastindex = remoteMapTag.find_last_of('.'); |
| 59 |
if (lastindex != std::string::npos) |
| 60 |
remoteMapTag.resize(lastindex); |
| 61 |
StringUtils::ToLower(remoteMapTag); |
| 62 |
|
| 63 |
// Load our xml file, and fill up our mapping tables |
| 64 |
CXBMCTinyXML2 xmlDoc; |
| 65 |
|
| 66 |
// Load the config file |
| 67 |
CLog::Log(LOGINFO, "Loading {}", irMapPath); |
| 68 |
if (!xmlDoc.LoadFile(irMapPath)) |
| 69 |
{ |
| 70 |
CLog::Log(LOGERROR, "{}, Line {}\n{}", irMapPath, xmlDoc.ErrorLineNum(), xmlDoc.ErrorStr()); |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
auto* pRoot = xmlDoc.RootElement(); |
| 75 |
if (pRoot == nullptr) |
| 76 |
return false; |
| 77 |
|
| 78 |
std::string strValue = pRoot->Value(); |
| 79 |
if (strValue != remoteMapTag) |
| 80 |
{ |
| 81 |
CLog::Log(LOGERROR, "{} Doesn't contain <{}>", irMapPath, remoteMapTag); |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
// Run through our window groups |
| 86 |
auto* pRemote = pRoot->FirstChild(); |
| 87 |
while (pRemote != nullptr) |
| 88 |
{ |
| 89 |
if (pRemote->ToElement()) |
| 90 |
{ |
| 91 |
const char* szRemote = pRemote->Value(); |
| 92 |
if (szRemote != nullptr) |
| 93 |
{ |
| 94 |
auto* pAttr = pRemote->ToElement()->FirstAttribute(); |
| 95 |
if (pAttr != nullptr) |
| 96 |
MapRemote(pRemote, pAttr->Value()); |
| 97 |
} |
| 98 |
} |
| 99 |
pRemote = pRemote->NextSibling(); |
| 100 |
} |
| 101 |
|
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
void CIRTranslator::MapRemote(tinyxml2::XMLNode* pRemote, const std::string& szDevice) |
| 106 |
{ |
| 107 |
CLog::Log(LOGINFO, "* Adding remote mapping for device '{}'", szDevice); |
| 108 |
|
| 109 |
std::vector<std::string> remoteNames; |
| 110 |
|
| 111 |
auto it = m_irRemotesMap.find(szDevice); |
| 112 |
if (it == m_irRemotesMap.end()) |
| 113 |
m_irRemotesMap[szDevice] = std::make_shared<IRButtonMap>(); |
| 114 |
|
| 115 |
const std::shared_ptr<IRButtonMap>& buttons = m_irRemotesMap[szDevice]; |
| 116 |
|
| 117 |
auto* pButton = pRemote->FirstChildElement(); |
| 118 |
while (pButton != nullptr) |
| 119 |
{ |
| 120 |
if (!pButton->NoChildren()) |
| 121 |
{ |
| 122 |
if (std::strcmp(pButton->Value(), "altname") == 0) |
| 123 |
remoteNames.emplace_back(pButton->FirstChild()->Value()); |
| 124 |
else |
| 125 |
(*buttons)[pButton->FirstChild()->Value()] = pButton->Value(); |
| 126 |
} |
| 127 |
pButton = pButton->NextSiblingElement(); |
| 128 |
} |
| 129 |
|
| 130 |
for (const auto& remoteName : remoteNames) |
| 131 |
{ |
| 132 |
CLog::Log(LOGINFO, "* Linking remote mapping for '{}' to '{}'", szDevice, remoteName); |
| 133 |
m_irRemotesMap[remoteName] = buttons; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void CIRTranslator::Clear() |
| 138 |
{ |
| 139 |
m_irRemotesMap.clear(); |
| 140 |
} |
| 141 |
|
| 142 |
unsigned int CIRTranslator::TranslateButton(const std::string& szDevice, |
| 143 |
const std::string& szButton) |
| 144 |
{ |
| 145 |
// Find the device |
| 146 |
auto it = m_irRemotesMap.find(szDevice); |
| 147 |
if (it == m_irRemotesMap.end()) |
| 148 |
return 0; |
| 149 |
|
| 150 |
// Find the button |
| 151 |
auto it2 = (*it).second->find(szButton); |
| 152 |
if (it2 == (*it).second->end()) |
| 153 |
return 0; |
| 154 |
|
| 155 |
// Convert the button to code |
| 156 |
if (StringUtils::CompareNoCase((*it2).second, "obc", 3) == 0) |
| 157 |
return TranslateUniversalRemoteString((*it2).second); |
| 158 |
|
| 159 |
return TranslateString((*it2).second); |
| 160 |
} |
| 161 |
|
| 162 |
uint32_t CIRTranslator::TranslateString(std::string strButton) |
| 163 |
{ |
| 164 |
if (strButton.empty()) |
| 165 |
return 0; |
| 166 |
|
| 167 |
uint32_t buttonCode = 0; |
| 168 |
|
| 169 |
StringUtils::ToLower(strButton); |
| 170 |
|
| 171 |
if (strButton == "left") |
| 172 |
buttonCode = XINPUT_IR_REMOTE_LEFT; |
| 173 |
else if (strButton == "right") |
| 174 |
buttonCode = XINPUT_IR_REMOTE_RIGHT; |
| 175 |
else if (strButton == "up") |
| 176 |
buttonCode = XINPUT_IR_REMOTE_UP; |
| 177 |
else if (strButton == "down") |
| 178 |
buttonCode = XINPUT_IR_REMOTE_DOWN; |
| 179 |
else if (strButton == "select") |
| 180 |
buttonCode = XINPUT_IR_REMOTE_SELECT; |
| 181 |
else if (strButton == "back") |
| 182 |
buttonCode = XINPUT_IR_REMOTE_BACK; |
| 183 |
else if (strButton == "menu") |
| 184 |
buttonCode = XINPUT_IR_REMOTE_MENU; |
| 185 |
else if (strButton == "info") |
| 186 |
buttonCode = XINPUT_IR_REMOTE_INFO; |
| 187 |
else if (strButton == "display") |
| 188 |
buttonCode = XINPUT_IR_REMOTE_DISPLAY; |
| 189 |
else if (strButton == "title") |
| 190 |
buttonCode = XINPUT_IR_REMOTE_TITLE; |
| 191 |
else if (strButton == "play") |
| 192 |
buttonCode = XINPUT_IR_REMOTE_PLAY; |
| 193 |
else if (strButton == "pause") |
| 194 |
buttonCode = XINPUT_IR_REMOTE_PAUSE; |
| 195 |
else if (strButton == "reverse") |
| 196 |
buttonCode = XINPUT_IR_REMOTE_REVERSE; |
| 197 |
else if (strButton == "forward") |
| 198 |
buttonCode = XINPUT_IR_REMOTE_FORWARD; |
| 199 |
else if (strButton == "skipplus") |
| 200 |
buttonCode = XINPUT_IR_REMOTE_SKIP_PLUS; |
| 201 |
else if (strButton == "skipminus") |
| 202 |
buttonCode = XINPUT_IR_REMOTE_SKIP_MINUS; |
| 203 |
else if (strButton == "stop") |
| 204 |
buttonCode = XINPUT_IR_REMOTE_STOP; |
| 205 |
else if (strButton == "zero") |
| 206 |
buttonCode = XINPUT_IR_REMOTE_0; |
| 207 |
else if (strButton == "one") |
| 208 |
buttonCode = XINPUT_IR_REMOTE_1; |
| 209 |
else if (strButton == "two") |
| 210 |
buttonCode = XINPUT_IR_REMOTE_2; |
| 211 |
else if (strButton == "three") |
| 212 |
buttonCode = XINPUT_IR_REMOTE_3; |
| 213 |
else if (strButton == "four") |
| 214 |
buttonCode = XINPUT_IR_REMOTE_4; |
| 215 |
else if (strButton == "five") |
| 216 |
buttonCode = XINPUT_IR_REMOTE_5; |
| 217 |
else if (strButton == "six") |
| 218 |
buttonCode = XINPUT_IR_REMOTE_6; |
| 219 |
else if (strButton == "seven") |
| 220 |
buttonCode = XINPUT_IR_REMOTE_7; |
| 221 |
else if (strButton == "eight") |
| 222 |
buttonCode = XINPUT_IR_REMOTE_8; |
| 223 |
else if (strButton == "nine") |
| 224 |
buttonCode = XINPUT_IR_REMOTE_9; |
| 225 |
// Additional keys from the media center extender for xbox remote |
| 226 |
else if (strButton == "power") |
| 227 |
buttonCode = XINPUT_IR_REMOTE_POWER; |
| 228 |
else if (strButton == "mytv") |
| 229 |
buttonCode = XINPUT_IR_REMOTE_MY_TV; |
| 230 |
else if (strButton == "mymusic") |
| 231 |
buttonCode = XINPUT_IR_REMOTE_MY_MUSIC; |
| 232 |
else if (strButton == "mypictures") |
| 233 |
buttonCode = XINPUT_IR_REMOTE_MY_PICTURES; |
| 234 |
else if (strButton == "myvideo") |
| 235 |
buttonCode = XINPUT_IR_REMOTE_MY_VIDEOS; |
| 236 |
else if (strButton == "record") |
| 237 |
buttonCode = XINPUT_IR_REMOTE_RECORD; |
| 238 |
else if (strButton == "start") |
| 239 |
buttonCode = XINPUT_IR_REMOTE_START; |
| 240 |
else if (strButton == "volumeplus") |
| 241 |
buttonCode = XINPUT_IR_REMOTE_VOLUME_PLUS; |
| 242 |
else if (strButton == "volumeminus") |
| 243 |
buttonCode = XINPUT_IR_REMOTE_VOLUME_MINUS; |
| 244 |
else if (strButton == "channelplus") |
| 245 |
buttonCode = XINPUT_IR_REMOTE_CHANNEL_PLUS; |
| 246 |
else if (strButton == "channelminus") |
| 247 |
buttonCode = XINPUT_IR_REMOTE_CHANNEL_MINUS; |
| 248 |
else if (strButton == "pageplus") |
| 249 |
buttonCode = XINPUT_IR_REMOTE_CHANNEL_PLUS; |
| 250 |
else if (strButton == "pageminus") |
| 251 |
buttonCode = XINPUT_IR_REMOTE_CHANNEL_MINUS; |
| 252 |
else if (strButton == "mute") |
| 253 |
buttonCode = XINPUT_IR_REMOTE_MUTE; |
| 254 |
else if (strButton == "recordedtv") |
| 255 |
buttonCode = XINPUT_IR_REMOTE_RECORDED_TV; |
| 256 |
else if (strButton == "guide") |
| 257 |
buttonCode = XINPUT_IR_REMOTE_GUIDE; |
| 258 |
else if (strButton == "livetv") |
| 259 |
buttonCode = XINPUT_IR_REMOTE_LIVE_TV; |
| 260 |
else if (strButton == "liveradio") |
| 261 |
buttonCode = XINPUT_IR_REMOTE_LIVE_RADIO; |
| 262 |
else if (strButton == "epgsearch") |
| 263 |
buttonCode = XINPUT_IR_REMOTE_EPG_SEARCH; |
| 264 |
else if (strButton == "star") |
| 265 |
buttonCode = XINPUT_IR_REMOTE_STAR; |
| 266 |
else if (strButton == "hash") |
| 267 |
buttonCode = XINPUT_IR_REMOTE_HASH; |
| 268 |
else if (strButton == "clear") |
| 269 |
buttonCode = XINPUT_IR_REMOTE_CLEAR; |
| 270 |
else if (strButton == "enter") |
| 271 |
buttonCode = XINPUT_IR_REMOTE_ENTER; |
| 272 |
else if (strButton == "xbox") |
| 273 |
buttonCode = XINPUT_IR_REMOTE_DISPLAY; // Same as display |
| 274 |
else if (strButton == "playlist") |
| 275 |
buttonCode = XINPUT_IR_REMOTE_PLAYLIST; |
| 276 |
else if (strButton == "teletext") |
| 277 |
buttonCode = XINPUT_IR_REMOTE_TELETEXT; |
| 278 |
else if (strButton == "red") |
| 279 |
buttonCode = XINPUT_IR_REMOTE_RED; |
| 280 |
else if (strButton == "green") |
| 281 |
buttonCode = XINPUT_IR_REMOTE_GREEN; |
| 282 |
else if (strButton == "yellow") |
| 283 |
buttonCode = XINPUT_IR_REMOTE_YELLOW; |
| 284 |
else if (strButton == "blue") |
| 285 |
buttonCode = XINPUT_IR_REMOTE_BLUE; |
| 286 |
else if (strButton == "subtitle") |
| 287 |
buttonCode = XINPUT_IR_REMOTE_SUBTITLE; |
| 288 |
else if (strButton == "language") |
| 289 |
buttonCode = XINPUT_IR_REMOTE_LANGUAGE; |
| 290 |
else if (strButton == "eject") |
| 291 |
buttonCode = XINPUT_IR_REMOTE_EJECT; |
| 292 |
else if (strButton == "contentsmenu") |
| 293 |
buttonCode = XINPUT_IR_REMOTE_CONTENTS_MENU; |
| 294 |
else if (strButton == "rootmenu") |
| 295 |
buttonCode = XINPUT_IR_REMOTE_ROOT_MENU; |
| 296 |
else if (strButton == "topmenu") |
| 297 |
buttonCode = XINPUT_IR_REMOTE_TOP_MENU; |
| 298 |
else if (strButton == "dvdmenu") |
| 299 |
buttonCode = XINPUT_IR_REMOTE_DVD_MENU; |
| 300 |
else if (strButton == "print") |
| 301 |
buttonCode = XINPUT_IR_REMOTE_PRINT; |
| 302 |
else |
| 303 |
CLog::Log(LOGERROR, "Remote Translator: Can't find button {}", strButton); |
| 304 |
return buttonCode; |
| 305 |
} |
| 306 |
|
| 307 |
uint32_t CIRTranslator::TranslateUniversalRemoteString(const std::string& szButton) |
| 308 |
{ |
| 309 |
if (szButton.empty() || szButton.length() < 4 || StringUtils::CompareNoCase(szButton, "obc", 3)) |
| 310 |
return 0; |
| 311 |
|
| 312 |
const char* szCode = szButton.c_str() + 3; |
| 313 |
|
| 314 |
// Button Code is 255 - OBC (Original Button Code) of the button |
| 315 |
uint32_t buttonCode = 255 - atol(szCode); |
| 316 |
if (buttonCode > 255) |
| 317 |
buttonCode = 0; |
| 318 |
|
| 319 |
return buttonCode; |
| 320 |
} |