summaryrefslogtreecommitdiffstats
path: root/xbmc/games/controllers/ControllerLayout.cpp
blob: 85816c1c7169589633c25ed11b33fdc18205075b (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
/*
 *  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 "ControllerLayout.h"

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

#include <sstream>

using namespace KODI;
using namespace GAME;

CControllerLayout::CControllerLayout() : m_topology(new CPhysicalTopology)
{
}

CControllerLayout::CControllerLayout(const CControllerLayout& other)
  : m_controller(other.m_controller),
    m_labelId(other.m_labelId),
    m_icon(other.m_icon),
    m_strImage(other.m_strImage),
    m_topology(new CPhysicalTopology(*other.m_topology))
{
}

CControllerLayout::~CControllerLayout() = default;

void CControllerLayout::Reset(void)
{
  m_controller = nullptr;
  m_labelId = -1;
  m_icon.clear();
  m_strImage.clear();
  m_topology->Reset();
}

bool CControllerLayout::IsValid(bool bLog) const
{
  if (m_labelId < 0)
  {
    if (bLog)
      CLog::Log(LOGERROR, "<{}> tag has no \"{}\" attribute", LAYOUT_XML_ROOT,
                LAYOUT_XML_ATTR_LAYOUT_LABEL);
    return false;
  }

  if (m_strImage.empty())
  {
    if (bLog)
      CLog::Log(LOGDEBUG, "<{}> tag has no \"{}\" attribute", LAYOUT_XML_ROOT,
                LAYOUT_XML_ATTR_LAYOUT_IMAGE);
    return false;
  }

  return true;
}

std::string CControllerLayout::Label(void) const
{
  std::string label;

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

  return label;
}

std::string CControllerLayout::ImagePath(void) const
{
  std::string path;

  if (!m_strImage.empty() && m_controller != nullptr)
    return URIUtils::AddFileToFolder(URIUtils::GetDirectory(m_controller->LibPath()), m_strImage);

  return path;
}

void CControllerLayout::Deserialize(const TiXmlElement* pElement,
                                    const CController* controller,
                                    std::vector<CPhysicalFeature>& features)
{
  if (pElement == nullptr || controller == nullptr)
    return;

  // Controller (used for string lookup and path translation)
  m_controller = controller;

  // Label
  std::string strLabel = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_LAYOUT_LABEL);
  if (!strLabel.empty())
    std::istringstream(strLabel) >> m_labelId;

  // Icon
  std::string icon = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_LAYOUT_ICON);
  if (!icon.empty())
    m_icon = icon;

  // Fallback icon, use add-on icon
  if (m_icon.empty())
    m_icon = controller->Icon();

  // Image
  std::string image = XMLUtils::GetAttribute(pElement, LAYOUT_XML_ATTR_LAYOUT_IMAGE);
  if (!image.empty())
    m_strImage = image;

  for (const TiXmlElement* pChild = pElement->FirstChildElement(); pChild != nullptr;
       pChild = pChild->NextSiblingElement())
  {
    if (pChild->ValueStr() == LAYOUT_XML_ELM_CATEGORY)
    {
      // Category
      std::string strCategory = XMLUtils::GetAttribute(pChild, LAYOUT_XML_ATTR_CATEGORY_NAME);
      JOYSTICK::FEATURE_CATEGORY category =
          CControllerTranslator::TranslateFeatureCategory(strCategory);

      // Category label
      int categoryLabelId = -1;

      std::string strCategoryLabelId =
          XMLUtils::GetAttribute(pChild, LAYOUT_XML_ATTR_CATEGORY_LABEL);
      if (!strCategoryLabelId.empty())
        std::istringstream(strCategoryLabelId) >> categoryLabelId;

      // Features
      for (const TiXmlElement* pFeature = pChild->FirstChildElement(); pFeature != nullptr;
           pFeature = pFeature->NextSiblingElement())
      {
        CPhysicalFeature feature;

        if (feature.Deserialize(pFeature, controller, category, categoryLabelId))
          features.push_back(feature);
      }
    }
    else if (pChild->ValueStr() == LAYOUT_XML_ELM_TOPOLOGY)
    {
      // Topology
      CPhysicalTopology topology;
      if (topology.Deserialize(pChild))
        *m_topology = std::move(topology);
    }
    else
    {
      CLog::Log(LOGDEBUG, "Ignoring <{}> tag", pChild->ValueStr());
    }
  }
}