Debian Codegraph @

xbmc/input/keymaps/keyboard/KeyboardTranslator.cpp

raw
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 "KeyboardTranslator.h"
10
11 #include "input/keyboard/Key.h"
12 #include "input/keyboard/KeyIDs.h"
13 #include "input/keyboard/XBMC_keytable.h"
14 #include "utils/StringUtils.h"
15 #include "utils/log.h"
16
17 #include <string>
18 #include <vector>
19
20 #include <tinyxml2.h>
21
22 using namespace KODI;
23 using namespace KEYMAP;
24
25 uint32_t CKeyboardTranslator::TranslateButton(const tinyxml2::XMLElement* pButton)
26 {
27 uint32_t button_id = 0;
28 const char* szButton = pButton->Value();
29
30 if (szButton == nullptr)
31 return 0;
32
33 const std::string strKey = szButton;
34 if (strKey == "key")
35 {
36 const char* strID;
37 if (pButton->QueryStringAttribute("id", &strID) == tinyxml2::XML_SUCCESS)
38 {
39 const char* str = strID;
40 char* endptr;
41 long int id = strtol(str, &endptr, 0);
42 if (endptr - str != (int)strlen(str) || id <= 0 || id > 0x00FFFFFF)
43 CLog::Log(LOGDEBUG, "{} - invalid key id {}", __FUNCTION__, strID);
44 else
45 button_id = (uint32_t)id;
46 }
47 else
48 CLog::Log(LOGERROR, "Keyboard Translator: `key' button has no id");
49 }
50 else
51 button_id = TranslateString(szButton);
52
53 // Process the ctrl/shift/alt modifiers
54 const char* cstrMod;
55 if (pButton->QueryStringAttribute("mod", &cstrMod) == tinyxml2::XML_SUCCESS)
56 {
57 const std::string strMod = StringUtils::ToLower(cstrMod);
58
59 std::vector<std::string> modArray = StringUtils::Split(strMod, ",");
60 for (auto substr : modArray)
61 {
62 StringUtils::Trim(substr);
63
64 if (substr == "ctrl" || substr == "control")
65 button_id |= CKey::MODIFIER_CTRL;
66 else if (substr == "shift")
67 button_id |= CKey::MODIFIER_SHIFT;
68 else if (substr == "alt")
69 button_id |= CKey::MODIFIER_ALT;
70 else if (substr == "super" || substr == "win")
71 button_id |= CKey::MODIFIER_SUPER;
72 else if (substr == "meta" || substr == "cmd")
73 button_id |= CKey::MODIFIER_META;
74 else if (substr == "longpress")
75 button_id |= CKey::MODIFIER_LONG;
76 else
77 CLog::Log(LOGERROR, "Keyboard Translator: Unknown key modifier {} in {}", substr, strMod);
78 }
79 }
80
81 return button_id;
82 }
83
84 uint32_t CKeyboardTranslator::TranslateString(const std::string& szButton)
85 {
86 uint32_t buttonCode = 0;
87 KEYBOARD::XBMCKEYTABLE keytable;
88
89 // Look up the key name
90 if (KEYBOARD::KeyTable::LookupName(szButton, &keytable))
91 {
92 buttonCode = keytable.vkey;
93 }
94 else
95 {
96 // The lookup failed i.e. the key name wasn't found
97 CLog::Log(LOGERROR, "Keyboard Translator: Can't find button {}", szButton);
98 }
99
100 buttonCode |= KEY_VKEY;
101
102 return buttonCode;
103 }