Debian Codegraph @

xbmc/input/keyboard/KeyboardLayout.cpp

raw
1 /*
2 * Copyright (C) 2005-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 "KeyboardLayout.h"
10
11 #include "guilib/LocalizeStrings.h"
12 #include "input/InputCodingTableFactory.h"
13 #include "utils/CharsetConverter.h"
14 #include "utils/StringUtils.h"
15 #include "utils/log.h"
16
17 #include <algorithm>
18 #include <set>
19
20 #include <tinyxml2.h>
21
22 using namespace KODI;
23 using namespace KEYBOARD;
24
25 CKeyboardLayout::~CKeyboardLayout() = default;
26
27 bool CKeyboardLayout::Load(const tinyxml2::XMLElement* element)
28 {
29 const char* language = element->Attribute("language");
30 if (language == nullptr)
31 {
32 CLog::Log(LOGWARNING, "CKeyboardLayout: invalid \"language\" attribute");
33 return false;
34 }
35
36 m_language = language;
37 if (m_language.empty())
38 {
39 CLog::Log(LOGWARNING, "CKeyboardLayout: empty \"language\" attribute");
40 return false;
41 }
42
43 const char* layout = element->Attribute("layout");
44 if (layout == nullptr)
45 {
46 CLog::Log(LOGWARNING, "CKeyboardLayout: invalid \"layout\" attribute");
47 return false;
48 }
49
50 m_layout = layout;
51 if (m_layout.empty())
52 {
53 CLog::Log(LOGWARNING, "CKeyboardLayout: empty \"layout\" attribute");
54 return false;
55 }
56
57 const auto* keyboard = element->FirstChildElement("keyboard");
58 if (element->Attribute("codingtable"))
59 m_codingtable = IInputCodingTablePtr(
60 CInputCodingTableFactory::CreateCodingTable(element->Attribute("codingtable")));
61 else
62 m_codingtable = NULL;
63 while (keyboard != nullptr)
64 {
65 // parse modifiers keys
66 std::set<unsigned int> modifierKeysSet;
67
68 const char* strModifiers = keyboard->Attribute("modifiers");
69 if (strModifiers != NULL)
70 {
71 std::string modifiers = strModifiers;
72 StringUtils::ToLower(modifiers);
73
74 std::vector<std::string> variants = StringUtils::Split(modifiers, ",");
75 for (const auto& itv : variants)
76 {
77 unsigned int iKeys = ModifierKeyNone;
78 std::vector<std::string> keys = StringUtils::Split(itv, "+");
79 for (const std::string& strKey : keys)
80 {
81 if (strKey == "shift")
82 iKeys |= ModifierKeyShift;
83 else if (strKey == "symbol")
84 iKeys |= ModifierKeySymbol;
85 }
86
87 modifierKeysSet.insert(iKeys);
88 }
89 }
90
91 // parse keyboard rows
92 const auto* row = keyboard->FirstChildElement("row");
93 while (row != nullptr)
94 {
95 if (!row->NoChildren())
96 {
97 std::string strRow = row->FirstChild()->Value();
98 std::vector<std::string> chars = BreakCharacters(strRow);
99 if (!modifierKeysSet.empty())
100 {
101 for (const auto& it : modifierKeysSet)
102 m_keyboards[it].push_back(chars);
103 }
104 else
105 m_keyboards[ModifierKeyNone].push_back(chars);
106 }
107
108 row = row->NextSiblingElement();
109 }
110
111 keyboard = keyboard->NextSiblingElement();
112 }
113
114 if (m_keyboards.empty())
115 {
116 CLog::Log(LOGWARNING, "CKeyboardLayout: no keyboard layout found");
117 return false;
118 }
119
120 return true;
121 }
122
123 std::string CKeyboardLayout::GetIdentifier() const
124 {
125 return StringUtils::Format("{} {}", m_language, m_layout);
126 }
127
128 std::string CKeyboardLayout::GetName() const
129 {
130 return StringUtils::Format(g_localizeStrings.Get(311), m_language, m_layout);
131 }
132
133 std::string CKeyboardLayout::GetCharAt(unsigned int row,
134 unsigned int column,
135 unsigned int modifiers) const
136 {
137 Keyboards::const_iterator mod = m_keyboards.find(modifiers);
138 if (modifiers != ModifierKeyNone && mod != m_keyboards.end() && mod->second.empty())
139 {
140 // fallback to basic keyboard
141 mod = m_keyboards.find(ModifierKeyNone);
142 }
143
144 if (mod != m_keyboards.end())
145 {
146 if (row < mod->second.size() && column < mod->second[row].size())
147 {
148 std::string ch = mod->second[row][column];
149 if (ch != " ")
150 return ch;
151 }
152 }
153
154 return "";
155 }
156
157 std::vector<std::string> CKeyboardLayout::BreakCharacters(const std::string& chars)
158 {
159 std::vector<std::string> result;
160 // break into utf8 characters
161 std::u32string chars32 = g_charsetConverter.utf8ToUtf32(chars);
162 for (const auto& it : chars32)
163 {
164 std::u32string char32(1, it);
165 result.push_back(g_charsetConverter.utf32ToUtf8(char32));
166 }
167
168 return result;
169 }