| 1 |
/* |
| 2 |
* Copyright (C) 2022-2023 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 "GUIGameControllerProvider.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "games/GameServices.h" |
| 14 |
#include "games/controllers/Controller.h" |
| 15 |
#include "games/controllers/ControllerLayout.h" |
| 16 |
#include "games/ports/windows/GUIPortDefines.h" |
| 17 |
#include "guilib/GUIFont.h" |
| 18 |
#include "guilib/GUIListItem.h" |
| 19 |
#include "utils/Variant.h" |
| 20 |
|
| 21 |
using namespace KODI; |
| 22 |
using namespace GAME; |
| 23 |
|
| 24 |
namespace |
| 25 |
{ |
| 26 |
// Prepend a "disconnected" icon to the list of ports |
| 27 |
constexpr unsigned int ITEM_COUNT = MAX_PORT_COUNT + 1; |
| 28 |
} // namespace |
| 29 |
|
| 30 |
CGUIGameControllerProvider::CGUIGameControllerProvider(unsigned int portCount, |
| 31 |
int portIndex, |
| 32 |
const std::string& peripheralLocation, |
| 33 |
uint32_t alignment, |
| 34 |
int parentID) |
| 35 |
: IListProvider(parentID), |
| 36 |
m_portCount(portCount), |
| 37 |
m_portIndex(portIndex), |
| 38 |
m_peripheralLocation(peripheralLocation), |
| 39 |
m_alignment(alignment) |
| 40 |
{ |
| 41 |
InitializeItems(); |
| 42 |
} |
| 43 |
|
| 44 |
CGUIGameControllerProvider::CGUIGameControllerProvider(const CGUIGameControllerProvider& other) |
| 45 |
: IListProvider(other.m_parentID), |
| 46 |
m_portCount(other.m_portCount), |
| 47 |
m_portIndex(other.m_portIndex), |
| 48 |
m_peripheralLocation(other.m_peripheralLocation), |
| 49 |
m_alignment(other.m_alignment) |
| 50 |
{ |
| 51 |
InitializeItems(); |
| 52 |
} |
| 53 |
|
| 54 |
CGUIGameControllerProvider::~CGUIGameControllerProvider() = default; |
| 55 |
|
| 56 |
std::unique_ptr<IListProvider> CGUIGameControllerProvider::Clone() |
| 57 |
{ |
| 58 |
return std::make_unique<CGUIGameControllerProvider>(*this); |
| 59 |
} |
| 60 |
|
| 61 |
bool CGUIGameControllerProvider::Update(bool forceRefresh) |
| 62 |
{ |
| 63 |
bool bDirty = false; |
| 64 |
std::swap(bDirty, m_bDirty); |
| 65 |
return bDirty; |
| 66 |
} |
| 67 |
|
| 68 |
void CGUIGameControllerProvider::Fetch(std::vector<std::shared_ptr<CGUIListItem>>& items) |
| 69 |
{ |
| 70 |
items = m_items; |
| 71 |
} |
| 72 |
|
| 73 |
void CGUIGameControllerProvider::SetControllerProfile(ControllerPtr controllerProfile) |
| 74 |
{ |
| 75 |
const std::string oldControllerId = m_controllerProfile ? m_controllerProfile->ID() : ""; |
| 76 |
const std::string newControllerId = controllerProfile ? controllerProfile->ID() : ""; |
| 77 |
|
| 78 |
if (oldControllerId != newControllerId) |
| 79 |
{ |
| 80 |
m_controllerProfile = std::move(controllerProfile); |
| 81 |
UpdateItems(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void CGUIGameControllerProvider::SetPortCount(unsigned int portCount) |
| 86 |
{ |
| 87 |
if (m_portCount != portCount) |
| 88 |
{ |
| 89 |
m_portCount = portCount; |
| 90 |
UpdateItems(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void CGUIGameControllerProvider::SetPortIndex(int portIndex) |
| 95 |
{ |
| 96 |
if (m_portIndex != portIndex) |
| 97 |
{ |
| 98 |
m_portIndex = portIndex; |
| 99 |
UpdateItems(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void CGUIGameControllerProvider::SetPeripheralLocation(const std::string& peripheralLocation) |
| 104 |
{ |
| 105 |
if (m_peripheralLocation != peripheralLocation) |
| 106 |
{ |
| 107 |
m_peripheralLocation = peripheralLocation; |
| 108 |
UpdateItems(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void CGUIGameControllerProvider::InitializeItems() |
| 113 |
{ |
| 114 |
m_items.resize(ITEM_COUNT); |
| 115 |
for (auto& item : m_items) |
| 116 |
item = std::make_shared<CGUIListItem>(); |
| 117 |
|
| 118 |
UpdateItems(); |
| 119 |
} |
| 120 |
|
| 121 |
void CGUIGameControllerProvider::UpdateItems() |
| 122 |
{ |
| 123 |
ControllerPtr controller = m_controllerProfile; |
| 124 |
|
| 125 |
int portIndex = -1; |
| 126 |
for (unsigned int i = 0; i < static_cast<unsigned int>(m_items.size()); ++i) |
| 127 |
{ |
| 128 |
std::shared_ptr<CGUIListItem>& guiItem = m_items.at(i); |
| 129 |
|
| 130 |
// Pad list if aligning to the right |
| 131 |
if (m_alignment == XBFONT_RIGHT && i + m_portCount < MAX_PORT_COUNT) |
| 132 |
{ |
| 133 |
// Fully reset item state. Simply resetting the individual properties |
| 134 |
// is not enough, as other properties may also be set. |
| 135 |
guiItem = std::make_shared<CGUIListItem>(); |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
CFileItemPtr fileItem = std::make_shared<CFileItem>(); |
| 140 |
|
| 141 |
// Set the item state for the current port index |
| 142 |
if (portIndex++ == m_portIndex && controller) |
| 143 |
{ |
| 144 |
fileItem->SetLabel(controller->Layout().Label()); |
| 145 |
fileItem->SetPath(m_peripheralLocation); |
| 146 |
fileItem->SetProperty("Addon.ID", controller->ID()); |
| 147 |
fileItem->SetArt("icon", controller->Layout().ImagePath()); |
| 148 |
} |
| 149 |
|
| 150 |
guiItem = std::move(fileItem); |
| 151 |
} |
| 152 |
|
| 153 |
m_bDirty = true; |
| 154 |
} |