aboutsummaryrefslogtreecommitdiff
path: root/src/input/KeyboardLayoutConfiguration.cpp
blob: 916a2e559dfb3d94f29b4d920989f0405808e4ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 *      Copyright (C) 2005-2013 Team XBMC
 *      http://xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include "KeyboardLayoutConfiguration.h"
#include "utils/CharsetConverter.h"
#include "utils/XBMCTinyXML.h"
#include "utils/XMLUtils.h"

using namespace std;
CKeyboardLayoutConfiguration g_keyboardLayoutConfiguration;

CKeyboardLayoutConfiguration::CKeyboardLayoutConfiguration()
{
  SetDefaults();
}


CKeyboardLayoutConfiguration::~CKeyboardLayoutConfiguration()
{
  SetDefaults();
}

void CKeyboardLayoutConfiguration::SetDefaults()
{
  m_changeXbmcCharRegardlessModifiers.clear();
  m_changeXbmcCharWithRalt.clear();
  m_deriveXbmcCharFromVkeyRegardlessModifiers.clear();
  m_deriveXbmcCharFromVkeyWithShift.clear();
  m_deriveXbmcCharFromVkeyWithRalt.clear();
}

bool CKeyboardLayoutConfiguration::Load(const std::string& strFileName)
{
  SetDefaults();

  CXBMCTinyXML xmlDoc;
  if (!xmlDoc.LoadFile(strFileName))
  {
    CLog::Log(LOGINFO, "unable to load %s: %s at line %d", strFileName.c_str(), xmlDoc.ErrorDesc(), xmlDoc.ErrorRow());
    return false;
  }

  TiXmlElement* pRootElement = xmlDoc.RootElement();
  std::string strValue = pRootElement->Value();
  if (strValue != "keyboard_layout")
  {
    CLog::Log(LOGERROR, "%s Doesn't contain <keyboard_layout>", strFileName.c_str());
    return false;
  }

  CLog::Log(LOGDEBUG, "reading char2char ");
  const TiXmlElement* pMapChangeXbmcCharRegardlessModifiers = pRootElement->FirstChildElement("char2char");
  readCharMapFromXML(pMapChangeXbmcCharRegardlessModifiers, m_changeXbmcCharRegardlessModifiers, ("char2char"));

  CLog::Log(LOGDEBUG, "reading char2char_ralt ");
  const TiXmlElement* pMapChangeXbmcCharWithRalt = pRootElement->FirstChildElement("char2char_ralt");
  readCharMapFromXML(pMapChangeXbmcCharWithRalt, m_changeXbmcCharWithRalt, ("char2char_ralt"));

  CLog::Log(LOGDEBUG, "reading vkey2char ");
  const TiXmlElement* pMapDeriveXbmcCharFromVkeyRegardlessModifiers = pRootElement->FirstChildElement("vkey2char");
  readByteMapFromXML(pMapDeriveXbmcCharFromVkeyRegardlessModifiers, m_deriveXbmcCharFromVkeyRegardlessModifiers, ("vkey2char"));

  CLog::Log(LOGDEBUG, "reading vkey2char_shift ");
  const TiXmlElement* pMapDeriveXbmcCharFromVkeyWithShift = pRootElement->FirstChildElement("vkey2char_shift");
  readByteMapFromXML(pMapDeriveXbmcCharFromVkeyWithShift, m_deriveXbmcCharFromVkeyWithShift, ("vkey2char_shift"));

  CLog::Log(LOGDEBUG, "reading vkey2char_ralt ");
  const TiXmlElement* pMapDeriveXbmcCharFromVkeyWithRalt = pRootElement->FirstChildElement("vkey2char_ralt");
  readByteMapFromXML(pMapDeriveXbmcCharFromVkeyWithRalt, m_deriveXbmcCharFromVkeyWithRalt, ("vkey2char_ralt"));

  return true;
}

void CKeyboardLayoutConfiguration::readCharMapFromXML(const TiXmlElement* pXMLMap, map<WCHAR, WCHAR>& charToCharMap, const char* mapRootElement)
{
  if (pXMLMap && !pXMLMap->NoChildren())
  { // map keys
    const TiXmlElement* pEntry = pXMLMap->FirstChildElement();
    while (pEntry)
    {
      std::string strInChar = XMLUtils::GetAttribute(pEntry, "inchar");
      std::string strOutChar = XMLUtils::GetAttribute(pEntry, "outchar");
      if (!strInChar.empty() && !strOutChar.empty())
      {
        std::wstring fromStr;
        g_charsetConverter.utf8ToW(strInChar, fromStr);
        std::wstring toStr;
        g_charsetConverter.utf8ToW(strOutChar, toStr);
        if (fromStr.size()==1 && toStr.size()==1)
        {
          charToCharMap.insert(pair<WCHAR, WCHAR>(fromStr[0], toStr[0]));
          CLog::Log(LOGDEBUG, "insert map entry from %c to %c ", fromStr[0], toStr[0]);
        }
        else
        {
          CLog::Log(LOGERROR, "String from %ls or to %ls does not have the expected length of 1", fromStr.c_str(), toStr.c_str());
        }
      }
      else
      {
        CLog::Log(LOGERROR, "map entry misses attribute <inchar> or <outchar> or content of them");
      }
      pEntry = pEntry->NextSiblingElement();
    }
  }
  else
  {
    CLog::Log(LOGDEBUG, "XML-Configuration doesn't contain expected map root element %s", mapRootElement);
  }
}

void CKeyboardLayoutConfiguration::readByteMapFromXML(const TiXmlElement* pXMLMap, map<BYTE, WCHAR>& charToCharMap, const char* mapRootElement)
{
  if (pXMLMap && !pXMLMap->NoChildren())
  { // map keys
    const TiXmlElement* pEntry = pXMLMap->FirstChildElement();
    while (pEntry)
    {
      std::string strInHex = XMLUtils::GetAttribute(pEntry, "inhex");
      std::string strOutChar = XMLUtils::GetAttribute(pEntry, "outchar");
      if (!strInHex.empty() && !strOutChar.empty())
      {
        std::string hexValue = strInHex;
        std::wstring toStr;
        g_charsetConverter.utf8ToW(strOutChar, toStr);

        int from;
        if (sscanf(hexValue.c_str(), "%x", (unsigned int *)&from))
        {
          if (from != 0) // eats nearly any typing error as 0: catch it:
          {
            if (from < 256)
            {
              if (toStr.size()==1)
              {
                    charToCharMap.insert(pair<BYTE, WCHAR>(from, toStr[0]));
                    CLog::Log(LOGDEBUG, "insert map entry from %d to %c ", from, toStr[0]);
              }
              else
              {
                CLog::Log(LOGERROR, "String to %ls does not have the expected length of >=1", toStr.c_str());
              }
            }
            else
            {
              CLog::Log(LOGERROR, "From value %d was greater than 255! ", from);
            }
          }
          else
          {
            CLog::Log(LOGERROR, "Scanned from-value %d as 0 probably a (typing?) error! ", from);
          }
        }
        else
        {
            CLog::Log(LOGERROR, "Could not scan from-value %s (was no valid hex value) ", hexValue.c_str());
        }
      }
      else
      {
        CLog::Log(LOGERROR, "map entry misses attribute <inhex> or <outchar> or content of them");
      }
      pEntry = pEntry->NextSiblingElement();
    }
  }
  else
  {
    CLog::Log(LOGERROR, "XML-Configuration doesn't contain expected map root element %s", mapRootElement);
  }
}

bool CKeyboardLayoutConfiguration::containsChangeXbmcCharRegardlessModifiers(WCHAR key)
{
  bool result = m_changeXbmcCharRegardlessModifiers.find(key) != m_changeXbmcCharRegardlessModifiers.end();
#ifdef DEBUG_KEYBOARD_GETCHAR
  CLog::Log(LOGDEBUG, "found containsChangeXbmcCharRegardlessModifiers key char %c: bool: %d ", key, result);
#endif
  return result;
}

bool CKeyboardLayoutConfiguration::containsChangeXbmcCharWithRalt(WCHAR key)
{
  bool result = m_changeXbmcCharWithRalt.find(key) != m_changeXbmcCharWithRalt.end();
#ifdef DEBUG_KEYBOARD_GETCHAR
  CLog::Log(LOGDEBUG, "found containsChangeXbmcCharWithRalt key char %c: bool: %d ", key, result);
#endif
  return result;
}

bool CKeyboardLayoutConfiguration::containsDeriveXbmcCharFromVkeyRegardlessModifiers(BYTE key)
{
  bool result = m_deriveXbmcCharFromVkeyRegardlessModifiers.find(key) != m_deriveXbmcCharFromVkeyRegardlessModifiers.end();
#ifdef DEBUG_KEYBOARD_GETCHAR
  CLog::Log(LOGDEBUG, "found containsDeriveXbmcCharFromVkeyRegardlessModifiers key vkey %d: bool: %d ", key, result);
#endif
  return result;
}

bool CKeyboardLayoutConfiguration::containsDeriveXbmcCharFromVkeyWithShift(BYTE key)
{
  bool result = m_deriveXbmcCharFromVkeyWithShift.find(key) != m_deriveXbmcCharFromVkeyWithShift.end();
#ifdef DEBUG_KEYBOARD_GETCHAR
  CLog::Log(LOGDEBUG, "found containsDeriveXbmcCharFromVkeyWithShift key vkey %d: bool: %d ", key, result);
#endif
  return result;
}

bool CKeyboardLayoutConfiguration::containsDeriveXbmcCharFromVkeyWithRalt(BYTE key)
{
  bool result = m_deriveXbmcCharFromVkeyWithRalt.find(key) != m_deriveXbmcCharFromVkeyWithRalt.end();
#ifdef DEBUG_KEYBOARD_GETCHAR
  CLog::Log(LOGDEBUG, "found containsDeriveXbmcCharFromVkeyWithRalt key vkey %d: bool: %d ", key, result);
#endif
  return result;
}

WCHAR CKeyboardLayoutConfiguration::valueOfChangeXbmcCharRegardlessModifiers(WCHAR key)
{
  WCHAR result = (m_changeXbmcCharRegardlessModifiers.find(key))->second;
#ifdef DEBUG_KEYBOARD_GETCHAR
  CLog::Log(LOGDEBUG, "found valueOfChangeXbmcCharRegardlessModifiers for char key %c: char %c ", key, result);
#endif
  return result;
}

WCHAR CKeyboardLayoutConfiguration::valueOfChangeXbmcCharWithRalt(WCHAR key)
{
  WCHAR result = (m_changeXbmcCharWithRalt.find(key))->second;
#ifdef DEBUG_KEYBOARD_GETCHAR
  CLog::Log(LOGDEBUG, "found valueOfChangeXbmcCharWithRalt for char key %c: char %c ", key, result);
#endif
  return result;
}

WCHAR CKeyboardLayoutConfiguration::valueOfDeriveXbmcCharFromVkeyRegardlessModifiers(BYTE key)
{
  WCHAR result = (m_deriveXbmcCharFromVkeyRegardlessModifiers.find(key))->second;
#ifdef DEBUG_KEYBOARD_GETCHAR
  CLog::Log(LOGDEBUG, "found valueOfDeriveXbmcCharFromVkeyRegardlessModifiers for key vkey %d: char %c ", key, result);
#endif
  return result;
}

WCHAR CKeyboardLayoutConfiguration::valueOfDeriveXbmcCharFromVkeyWithShift(BYTE key)
{
  WCHAR result = (m_deriveXbmcCharFromVkeyWithShift.find(key))->second;
#ifdef DEBUG_KEYBOARD_GETCHAR
  CLog::Log(LOGDEBUG, "found valueOfDeriveXbmcCharFromVkeyWithShift for key vkey %d: char %c ", key, result);
#endif
  return result;
}

WCHAR CKeyboardLayoutConfiguration::valueOfDeriveXbmcCharFromVkeyWithRalt(BYTE key)
{
  WCHAR result = (m_deriveXbmcCharFromVkeyWithRalt.find(key))->second;
#ifdef DEBUG_KEYBOARD_GETCHAR
  CLog::Log(LOGDEBUG, "found valueOfDeriveXbmcCharFromVkeyWithRalt for key vkey %d: char %c ", key, result);
#endif
  return result;
}