summaryrefslogtreecommitdiffstats
path: root/xbmc/games/controllers/input/PhysicalFeature.cpp
blob: 24e0c410306ff412a1c0273bf954559a7ae50d44 (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
/*
 *  Copyright (C) 2015-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "PhysicalFeature.h"

#include "games/controllers/Controller.h"
#include "games/controllers/ControllerDefinitions.h"
#include "games/controllers/ControllerTranslator.h"
#include "guilib/LocalizeStrings.h"
#include "utils/XMLUtils.h"
#include "utils/log.h"

#include <sstream>

using namespace KODI;
using namespace GAME;
using namespace JOYSTICK;

CPhysicalFeature::CPhysicalFeature(int labelId)
{
  Reset();
  m_labelId = labelId;
}

void CPhysicalFeature::Reset(void)
{
  *this = CPhysicalFeature();
}

CPhysicalFeature& CPhysicalFeature::operator=(const CPhysicalFeature& rhs)
{
  if (this != &rhs)
  {
    m_controller = rhs.m_controller;
    m_type = rhs.m_type;
    m_category = rhs.m_category;
    m_categoryLabelId = rhs.m_categoryLabelId;
    m_strName = rhs.m_strName;
    m_labelId = rhs.m_labelId;
    m_inputType = rhs.m_inputType;
    m_keycode = rhs.m_keycode;
  }
  return *this;
}

std::string CPhysicalFeature::CategoryLabel() const
{
  std::string categoryLabel;

  if (m_categoryLabelId >= 0 && m_controller != nullptr)
    categoryLabel = g_localizeStrings.GetAddonString(m_controller->ID(), m_categoryLabelId);

  if (categoryLabel.empty())
    categoryLabel = g_localizeStrings.Get(m_categoryLabelId);

  return categoryLabel;
}

std::string CPhysicalFeature::Label() const
{
  std::string label;

  if (m_labelId >= 0 && m_controller != nullptr)
    label = g_localizeStrings.GetAddonString(m_controller->ID(), m_labelId);

  if (label.empty())
    label = g_localizeStrings.Get(m_labelId);

  return label;
}

bool CPhysicalFeature::Deserialize(const TiXmlElement* pElement,
                                   const CController* controller,
                                   FEATURE_CATEGORY category,
                                   int categoryLabelId)
{
  Reset();

  if (!pElement)
    return false;

  std::string strType(pElement->Value());

  // Type
  m_type = CControllerTranslator::TranslateFeatureType(strType);
  if (m_type == FEATURE_TYPE::UNKNOWN)
  {
    CLog::Log(LOGDEBUG, "Invalid feature: <{}> ", pElement->Value());
    return false;
  }

  // Cagegory was obtained from parent XML node
  m_category = category;
  m_categoryLabelId = categoryLabelId;

  // Name
  m_strName = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_FEATURE_NAME);
  if (m_strName.empty())
  {
    CLog::Log(LOGERROR, "<{}> tag has no \"{}\" attribute", strType, LAYOUT_XML_ATTR_FEATURE_NAME);
    return false;
  }

  // Label ID
  std::string strLabel = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_FEATURE_LABEL);
  if (strLabel.empty())
    CLog::Log(LOGDEBUG, "<{}> tag has no \"{}\" attribute", strType, LAYOUT_XML_ATTR_FEATURE_LABEL);
  else
    std::istringstream(strLabel) >> m_labelId;

  // Input type
  if (m_type == FEATURE_TYPE::SCALAR)
  {
    std::string strInputType = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_INPUT_TYPE);
    if (strInputType.empty())
    {
      CLog::Log(LOGERROR, "<{}> tag has no \"{}\" attribute", strType, LAYOUT_XML_ATTR_INPUT_TYPE);
      return false;
    }
    else
    {
      m_inputType = CControllerTranslator::TranslateInputType(strInputType);
      if (m_inputType == INPUT_TYPE::UNKNOWN)
      {
        CLog::Log(LOGERROR, "<{}> tag - attribute \"{}\" is invalid: \"{}\"", strType,
                  LAYOUT_XML_ATTR_INPUT_TYPE, strInputType);
        return false;
      }
    }
  }

  // Keycode
  if (m_type == FEATURE_TYPE::KEY)
  {
    std::string strSymbol = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_KEY_SYMBOL);
    if (strSymbol.empty())
    {
      CLog::Log(LOGERROR, "<{}> tag has no \"{}\" attribute", strType, LAYOUT_XML_ATTR_KEY_SYMBOL);
      return false;
    }
    else
    {
      m_keycode = CControllerTranslator::TranslateKeysym(strSymbol);
      if (m_keycode == XBMCK_UNKNOWN)
      {
        CLog::Log(LOGERROR, "<{}> tag - attribute \"{}\" is invalid: \"{}\"", strType,
                  LAYOUT_XML_ATTR_KEY_SYMBOL, strSymbol);
        return false;
      }
    }
  }

  // Save controller for string translation
  m_controller = controller;

  return true;
}