| 1 |
/* |
| 2 |
* Copyright (C) 2021-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 "GUIActivePortList.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "addons/AddonEvents.h" |
| 14 |
#include "addons/AddonManager.h" |
| 15 |
#include "games/addons/GameClient.h" |
| 16 |
#include "games/addons/input/GameClientInput.h" |
| 17 |
#include "games/agents/windows/GUIAgentDefines.h" |
| 18 |
#include "games/controllers/Controller.h" |
| 19 |
#include "games/controllers/ControllerLayout.h" |
| 20 |
#include "games/controllers/guicontrols/GUIGameControllerList.h" |
| 21 |
#include "games/controllers/input/PhysicalTopology.h" |
| 22 |
#include "games/controllers/types/ControllerHub.h" |
| 23 |
#include "games/controllers/types/ControllerTree.h" |
| 24 |
#include "games/ports/windows/GUIPortDefines.h" |
| 25 |
#include "guilib/GUIFont.h" |
| 26 |
#include "guilib/GUIMessage.h" |
| 27 |
#include "guilib/GUIWindow.h" |
| 28 |
#include "messaging/ApplicationMessenger.h" |
| 29 |
|
| 30 |
using namespace KODI; |
| 31 |
using namespace GAME; |
| 32 |
|
| 33 |
CGUIActivePortList::CGUIActivePortList(CGUIWindow& window, int controlId, bool showInputDisabled) |
| 34 |
: m_guiWindow(window), |
| 35 |
m_controlId(controlId), |
| 36 |
m_showInputDisabled(showInputDisabled), |
| 37 |
m_vecItems(std::make_unique<CFileItemList>()) |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
CGUIActivePortList::~CGUIActivePortList() |
| 42 |
{ |
| 43 |
Deinitialize(); |
| 44 |
} |
| 45 |
|
| 46 |
bool CGUIActivePortList::Initialize(GameClientPtr gameClient) |
| 47 |
{ |
| 48 |
// Validate parameters |
| 49 |
if (!gameClient) |
| 50 |
return false; |
| 51 |
|
| 52 |
// Initialize state |
| 53 |
m_gameClient = std::move(gameClient); |
| 54 |
|
| 55 |
// Initialize GUI |
| 56 |
InitializeGUI(); |
| 57 |
|
| 58 |
// Register observers |
| 59 |
m_gameClient->Input().RegisterObserver(this); |
| 60 |
CServiceBroker::GetAddonMgr().Events().Subscribe(this, &CGUIActivePortList::OnEvent); |
| 61 |
|
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
void CGUIActivePortList::Deinitialize() |
| 66 |
{ |
| 67 |
// Unregister observers in reverse order |
| 68 |
CServiceBroker::GetAddonMgr().Events().Unsubscribe(this); |
| 69 |
if (m_gameClient) |
| 70 |
m_gameClient->Input().UnregisterObserver(this); |
| 71 |
|
| 72 |
// Deinitialize GUI |
| 73 |
DeinitializeGUI(); |
| 74 |
|
| 75 |
// Reset state |
| 76 |
m_gameClient.reset(); |
| 77 |
} |
| 78 |
|
| 79 |
void CGUIActivePortList::Refresh() |
| 80 |
{ |
| 81 |
CleanupItems(); |
| 82 |
|
| 83 |
// Add input disabled icon |
| 84 |
if (m_showInputDisabled) |
| 85 |
AddInputDisabled(); |
| 86 |
|
| 87 |
// Add controllers of active ports |
| 88 |
if (m_gameClient) |
| 89 |
{ |
| 90 |
CControllerTree controllerTree = m_gameClient->Input().GetActiveControllerTree(); |
| 91 |
AddItems(controllerTree.GetPorts()); |
| 92 |
} |
| 93 |
|
| 94 |
// Add padding if right-aligned |
| 95 |
if (m_alignment == XBFONT_RIGHT) |
| 96 |
AddPadding(); |
| 97 |
|
| 98 |
// Update the GUI |
| 99 |
CGUIMessage msg(GUI_MSG_LABEL_BIND, m_guiWindow.GetID(), m_controlId, 0, 0, m_vecItems.get()); |
| 100 |
m_guiWindow.OnMessage(msg); |
| 101 |
} |
| 102 |
|
| 103 |
void CGUIActivePortList::Notify(const Observable& obs, const ObservableMessage msg) |
| 104 |
{ |
| 105 |
switch (msg) |
| 106 |
{ |
| 107 |
case ObservableMessageGamePortsChanged: |
| 108 |
{ |
| 109 |
CGUIMessage msg(GUI_MSG_REFRESH_LIST, m_guiWindow.GetID(), m_controlId); |
| 110 |
CServiceBroker::GetAppMessenger()->SendGUIMessage(msg, m_guiWindow.GetID()); |
| 111 |
} |
| 112 |
break; |
| 113 |
default: |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
void CGUIActivePortList::OnEvent(const ADDON::AddonEvent& event) |
| 119 |
{ |
| 120 |
if (typeid(event) == typeid(ADDON::AddonEvents::Enabled) || // Also called on install |
| 121 |
typeid(event) == typeid(ADDON::AddonEvents::Disabled) || // Not called on uninstall |
| 122 |
typeid(event) == typeid(ADDON::AddonEvents::ReInstalled) || |
| 123 |
typeid(event) == typeid(ADDON::AddonEvents::UnInstalled)) |
| 124 |
{ |
| 125 |
CGUIMessage msg(GUI_MSG_REFRESH_LIST, m_guiWindow.GetID(), m_controlId); |
| 126 |
msg.SetStringParam(event.addonId); |
| 127 |
CServiceBroker::GetAppMessenger()->SendGUIMessage(msg, m_guiWindow.GetID()); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void CGUIActivePortList::InitializeGUI() |
| 132 |
{ |
| 133 |
CGUIGameControllerList* activePortList = |
| 134 |
dynamic_cast<CGUIGameControllerList*>(m_guiWindow.GetControl(m_controlId)); |
| 135 |
|
| 136 |
if (activePortList != nullptr) |
| 137 |
{ |
| 138 |
m_alignment = activePortList->GetAlignment(); |
| 139 |
activePortList->SetGameClient(m_gameClient); |
| 140 |
} |
| 141 |
|
| 142 |
Refresh(); |
| 143 |
} |
| 144 |
|
| 145 |
void CGUIActivePortList::DeinitializeGUI() |
| 146 |
{ |
| 147 |
CleanupItems(); |
| 148 |
|
| 149 |
CGUIGameControllerList* activePortList = |
| 150 |
dynamic_cast<CGUIGameControllerList*>(m_guiWindow.GetControl(m_controlId)); |
| 151 |
|
| 152 |
if (activePortList != nullptr) |
| 153 |
activePortList->ClearGameClient(); |
| 154 |
} |
| 155 |
|
| 156 |
void CGUIActivePortList::AddInputDisabled() |
| 157 |
{ |
| 158 |
CFileItem item; |
| 159 |
item.SetArt("icon", "DefaultAddonNone.png"); |
| 160 |
m_vecItems->Add(std::move(item)); |
| 161 |
} |
| 162 |
|
| 163 |
void CGUIActivePortList::AddItems(const PortVec& ports) |
| 164 |
{ |
| 165 |
for (const CPortNode& port : ports) |
| 166 |
{ |
| 167 |
// Add controller |
| 168 |
ControllerPtr controller = port.GetActiveController().GetController(); |
| 169 |
const std::string& controllerAddress = port.GetActiveController().GetControllerAddress(); |
| 170 |
AddItem(controller, controllerAddress); |
| 171 |
|
| 172 |
// Add child ports |
| 173 |
AddItems(port.GetActiveController().GetHub().GetPorts()); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
void CGUIActivePortList::AddItem(const ControllerPtr& controller, |
| 178 |
const std::string& controllerAddress) |
| 179 |
{ |
| 180 |
// Check if a controller is connected that provides input |
| 181 |
if (controller && controller->Topology().ProvidesInput()) |
| 182 |
{ |
| 183 |
// Add GUI item |
| 184 |
CFileItemPtr item = std::make_shared<CFileItem>(controller->Layout().Label()); |
| 185 |
item->SetArt("icon", controller->Layout().ImagePath()); |
| 186 |
item->SetPath(controllerAddress); |
| 187 |
m_vecItems->Add(std::move(item)); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void CGUIActivePortList::AddPadding() |
| 192 |
{ |
| 193 |
unsigned int itemCount = MAX_PORT_COUNT; |
| 194 |
if (m_showInputDisabled) |
| 195 |
itemCount++; |
| 196 |
|
| 197 |
while (m_vecItems->Size() < static_cast<int>(itemCount)) |
| 198 |
m_vecItems->AddFront(std::make_shared<CFileItem>(), 0); |
| 199 |
} |
| 200 |
|
| 201 |
void CGUIActivePortList::CleanupItems() |
| 202 |
{ |
| 203 |
m_vecItems->Clear(); |
| 204 |
} |