| 1 |
/* |
| 2 |
* Copyright (C) 2022-2024 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 "GUIAgentControllerList.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "GUIAgentDefines.h" |
| 13 |
#include "GUIAgentWindow.h" |
| 14 |
#include "ServiceBroker.h" |
| 15 |
#include "addons/AddonEvents.h" |
| 16 |
#include "addons/AddonManager.h" |
| 17 |
#include "games/GameServices.h" |
| 18 |
#include "games/addons/GameClient.h" |
| 19 |
#include "games/addons/input/GameClientInput.h" |
| 20 |
#include "games/agents/input/AgentController.h" |
| 21 |
#include "games/agents/input/AgentInput.h" |
| 22 |
#include "games/controllers/Controller.h" |
| 23 |
#include "games/controllers/ControllerLayout.h" |
| 24 |
#include "guilib/GUIBaseContainer.h" |
| 25 |
#include "guilib/GUIComponent.h" |
| 26 |
#include "guilib/GUIMessage.h" |
| 27 |
#include "guilib/GUIWindow.h" |
| 28 |
#include "guilib/GUIWindowManager.h" |
| 29 |
#include "guilib/LocalizeStrings.h" |
| 30 |
#include "messaging/ApplicationMessenger.h" |
| 31 |
#include "messaging/helpers/DialogOKHelper.h" |
| 32 |
#include "peripherals/Peripherals.h" |
| 33 |
#include "peripherals/devices/Peripheral.h" |
| 34 |
#include "peripherals/dialogs/GUIDialogPeripheralSettings.h" |
| 35 |
#include "utils/log.h" |
| 36 |
#include "view/GUIViewControl.h" |
| 37 |
#include "view/ViewState.h" |
| 38 |
|
| 39 |
using namespace KODI; |
| 40 |
using namespace GAME; |
| 41 |
|
| 42 |
CGUIAgentControllerList::CGUIAgentControllerList(CGUIWindow& window) |
| 43 |
: m_guiWindow(window), |
| 44 |
m_viewControl(std::make_unique<CGUIViewControl>()), |
| 45 |
m_vecItems(std::make_unique<CFileItemList>()) |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
CGUIAgentControllerList::~CGUIAgentControllerList() |
| 50 |
{ |
| 51 |
Deinitialize(); |
| 52 |
} |
| 53 |
|
| 54 |
void CGUIAgentControllerList::OnWindowLoaded() |
| 55 |
{ |
| 56 |
m_viewControl->Reset(); |
| 57 |
m_viewControl->SetParentWindow(m_guiWindow.GetID()); |
| 58 |
m_viewControl->AddView(m_guiWindow.GetControl(CONTROL_AGENT_CONTROLLER_LIST)); |
| 59 |
} |
| 60 |
|
| 61 |
void CGUIAgentControllerList::OnWindowUnload() |
| 62 |
{ |
| 63 |
m_viewControl->Reset(); |
| 64 |
} |
| 65 |
|
| 66 |
bool CGUIAgentControllerList::Initialize(GameClientPtr gameClient) |
| 67 |
{ |
| 68 |
// Validate parameters |
| 69 |
if (!gameClient) |
| 70 |
return false; |
| 71 |
|
| 72 |
// Initialize state |
| 73 |
m_gameClient = std::move(gameClient); |
| 74 |
m_viewControl->SetCurrentView(DEFAULT_VIEW_LIST); |
| 75 |
|
| 76 |
// Initialize GUI |
| 77 |
Refresh(); |
| 78 |
m_viewControl->SetSelectedItem(0); |
| 79 |
|
| 80 |
// Register observers |
| 81 |
if (m_gameClient) |
| 82 |
m_gameClient->Input().RegisterObserver(this); |
| 83 |
CServiceBroker::GetAddonMgr().Events().Subscribe(this, &CGUIAgentControllerList::OnEvent); |
| 84 |
if (CServiceBroker::IsServiceManagerUp()) |
| 85 |
CServiceBroker::GetGameServices().AgentInput().RegisterObserver(this); |
| 86 |
|
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
void CGUIAgentControllerList::Deinitialize() |
| 91 |
{ |
| 92 |
// Unregister observers in reverse order |
| 93 |
if (CServiceBroker::IsServiceManagerUp()) |
| 94 |
CServiceBroker::GetGameServices().AgentInput().UnregisterObserver(this); |
| 95 |
CServiceBroker::GetAddonMgr().Events().Unsubscribe(this); |
| 96 |
if (m_gameClient) |
| 97 |
m_gameClient->Input().UnregisterObserver(this); |
| 98 |
|
| 99 |
// Deinitialize GUI |
| 100 |
CleanupItems(); |
| 101 |
|
| 102 |
// Reset state |
| 103 |
m_gameClient.reset(); |
| 104 |
} |
| 105 |
|
| 106 |
bool CGUIAgentControllerList::HasControl(int controlId) const |
| 107 |
{ |
| 108 |
return m_viewControl->HasControl(controlId); |
| 109 |
} |
| 110 |
|
| 111 |
int CGUIAgentControllerList::GetCurrentControl() const |
| 112 |
{ |
| 113 |
return m_viewControl->GetCurrentControl(); |
| 114 |
} |
| 115 |
|
| 116 |
void CGUIAgentControllerList::FrameMove() |
| 117 |
{ |
| 118 |
CGUIBaseContainer* thumbs = |
| 119 |
dynamic_cast<CGUIBaseContainer*>(m_guiWindow.GetControl(CONTROL_AGENT_CONTROLLER_LIST)); |
| 120 |
if (thumbs != nullptr) |
| 121 |
{ |
| 122 |
const int selectedItem = thumbs->GetSelectedItem(); |
| 123 |
if (0 <= selectedItem && selectedItem < m_vecItems->Size()) |
| 124 |
{ |
| 125 |
const unsigned int focusedItem = static_cast<unsigned int>(selectedItem); |
| 126 |
if (focusedItem != m_currentItem) |
| 127 |
OnItemFocus(focusedItem); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
void CGUIAgentControllerList::Refresh() |
| 133 |
{ |
| 134 |
// Send a synchronous message to clear the view control |
| 135 |
m_viewControl->Clear(); |
| 136 |
|
| 137 |
CleanupItems(); |
| 138 |
|
| 139 |
CAgentInput& agentInput = CServiceBroker::GetGameServices().AgentInput(); |
| 140 |
|
| 141 |
std::vector<std::shared_ptr<const CAgentController>> agentControllers = |
| 142 |
agentInput.GetControllers(); |
| 143 |
for (const std::shared_ptr<const CAgentController>& agentController : agentControllers) |
| 144 |
AddItem(*agentController); |
| 145 |
|
| 146 |
// Add a "No controllers connected" item if no agents are available |
| 147 |
if (m_vecItems->IsEmpty()) |
| 148 |
{ |
| 149 |
CFileItemPtr item = |
| 150 |
std::make_shared<CFileItem>(g_localizeStrings.Get(35173)); // "No controllers connected" |
| 151 |
m_vecItems->Add(std::move(item)); |
| 152 |
} |
| 153 |
|
| 154 |
// Update items |
| 155 |
m_viewControl->SetItems(*m_vecItems); |
| 156 |
|
| 157 |
// Try to restore focus to the previously focused agent |
| 158 |
for (unsigned int currentItem = 0; static_cast<int>(currentItem) < m_vecItems->Size(); |
| 159 |
++currentItem) |
| 160 |
{ |
| 161 |
CFileItemPtr item = m_vecItems->Get(currentItem); |
| 162 |
if (item && item->GetPath() == m_currentAgent) |
| 163 |
{ |
| 164 |
m_viewControl->SetSelectedItem(currentItem); |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
void CGUIAgentControllerList::SetFocused() |
| 171 |
{ |
| 172 |
m_viewControl->SetFocused(); |
| 173 |
} |
| 174 |
|
| 175 |
void CGUIAgentControllerList::OnSelect() |
| 176 |
{ |
| 177 |
const int itemIndex = m_viewControl->GetSelectedItem(); |
| 178 |
if (itemIndex >= 0) |
| 179 |
OnItemSelect(static_cast<unsigned int>(itemIndex)); |
| 180 |
} |
| 181 |
|
| 182 |
void CGUIAgentControllerList::Notify(const Observable& obs, const ObservableMessage msg) |
| 183 |
{ |
| 184 |
switch (msg) |
| 185 |
{ |
| 186 |
case ObservableMessageAgentControllersChanged: |
| 187 |
case ObservableMessageGamePortsChanged: |
| 188 |
{ |
| 189 |
CGUIMessage msg(GUI_MSG_REFRESH_LIST, m_guiWindow.GetID(), CONTROL_AGENT_CONTROLLER_LIST); |
| 190 |
CServiceBroker::GetAppMessenger()->SendGUIMessage(msg, m_guiWindow.GetID()); |
| 191 |
break; |
| 192 |
} |
| 193 |
default: |
| 194 |
break; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void CGUIAgentControllerList::OnEvent(const ADDON::AddonEvent& event) |
| 199 |
{ |
| 200 |
if (typeid(event) == typeid(ADDON::AddonEvents::Enabled) || // Also called on install |
| 201 |
typeid(event) == typeid(ADDON::AddonEvents::Disabled) || // Not called on uninstall |
| 202 |
typeid(event) == typeid(ADDON::AddonEvents::ReInstalled) || |
| 203 |
typeid(event) == typeid(ADDON::AddonEvents::UnInstalled)) |
| 204 |
{ |
| 205 |
CGUIMessage msg(GUI_MSG_REFRESH_LIST, m_guiWindow.GetID(), CONTROL_AGENT_CONTROLLER_LIST); |
| 206 |
msg.SetStringParam(event.addonId); |
| 207 |
CServiceBroker::GetAppMessenger()->SendGUIMessage(msg, m_guiWindow.GetID()); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
void CGUIAgentControllerList::AddItem(const CAgentController& agentController) |
| 212 |
{ |
| 213 |
// Create the list item from agent properties |
| 214 |
const std::string label = agentController.GetPeripheralName(); |
| 215 |
const ControllerPtr controller = agentController.GetController(); |
| 216 |
const std::string& path = agentController.GetPeripheralLocation(); |
| 217 |
|
| 218 |
CFileItemPtr item = std::make_shared<CFileItem>(label); |
| 219 |
item->SetPath(path); |
| 220 |
if (controller) |
| 221 |
{ |
| 222 |
item->SetProperty("Addon.ID", controller->ID()); |
| 223 |
item->SetArt("icon", controller->Layout().ImagePath()); |
| 224 |
} |
| 225 |
m_vecItems->Add(std::move(item)); |
| 226 |
} |
| 227 |
|
| 228 |
void CGUIAgentControllerList::CleanupItems() |
| 229 |
{ |
| 230 |
m_vecItems->Clear(); |
| 231 |
} |
| 232 |
|
| 233 |
void CGUIAgentControllerList::OnItemFocus(unsigned int itemIndex) |
| 234 |
{ |
| 235 |
// Remember the focused item |
| 236 |
m_currentItem = itemIndex; |
| 237 |
|
| 238 |
// Handle the focused agent |
| 239 |
CFileItemPtr item = m_vecItems->Get(m_currentItem); |
| 240 |
if (item) |
| 241 |
OnControllerFocus(item->GetPath()); |
| 242 |
} |
| 243 |
|
| 244 |
void CGUIAgentControllerList::OnControllerFocus(const std::string& focusedAgent) |
| 245 |
{ |
| 246 |
if (!focusedAgent.empty()) |
| 247 |
{ |
| 248 |
// Remember the focused agent |
| 249 |
m_currentAgent = focusedAgent; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
void CGUIAgentControllerList::OnItemSelect(unsigned int itemIndex) |
| 254 |
{ |
| 255 |
// Handle the selected agent |
| 256 |
CFileItemPtr item = m_vecItems->Get(itemIndex); |
| 257 |
if (item) |
| 258 |
OnControllerSelect(*item); |
| 259 |
} |
| 260 |
|
| 261 |
void CGUIAgentControllerList::OnControllerSelect(const CFileItem& selectedAgentItem) |
| 262 |
{ |
| 263 |
CAgentInput& agentInput = CServiceBroker::GetGameServices().AgentInput(); |
| 264 |
|
| 265 |
std::vector<std::shared_ptr<const CAgentController>> agentControllers = |
| 266 |
agentInput.GetControllers(); |
| 267 |
for (const std::shared_ptr<const CAgentController>& agentController : agentControllers) |
| 268 |
{ |
| 269 |
PERIPHERALS::PeripheralPtr peripheral = agentController->GetPeripheral(); |
| 270 |
if (peripheral && peripheral->Location() == selectedAgentItem.GetPath()) |
| 271 |
{ |
| 272 |
if (!peripheral->HasConfigurableSettings()) |
| 273 |
{ |
| 274 |
// Show an error if the peripheral doesn't have any settings |
| 275 |
CLog::Log(LOGERROR, "Peripheral has no settings"); |
| 276 |
|
| 277 |
// "Peripherals" |
| 278 |
// "There are no settings available for this peripheral" |
| 279 |
MESSAGING::HELPERS::ShowOKDialogText(CVariant{35000}, CVariant{35004}); |
| 280 |
} |
| 281 |
else |
| 282 |
{ |
| 283 |
ShowControllerDialog(*agentController); |
| 284 |
} |
| 285 |
break; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
void CGUIAgentControllerList::ShowControllerDialog(const CAgentController& agentController) |
| 291 |
{ |
| 292 |
// Get the dialog |
| 293 |
PERIPHERALS::CGUIDialogPeripheralSettings* pSettingsDialog = |
| 294 |
CServiceBroker::GetGUI() |
| 295 |
->GetWindowManager() |
| 296 |
.GetWindow<PERIPHERALS::CGUIDialogPeripheralSettings>(WINDOW_DIALOG_PERIPHERAL_SETTINGS); |
| 297 |
if (pSettingsDialog == nullptr) |
| 298 |
return; |
| 299 |
|
| 300 |
const PERIPHERALS::PeripheralPtr peripheral = agentController.GetPeripheral(); |
| 301 |
|
| 302 |
// Get a file item for the peripheral settings dialog |
| 303 |
CFileItemPtr peripheralItem; |
| 304 |
|
| 305 |
CFileItemList peripherals; |
| 306 |
CServiceBroker::GetPeripherals().GetDirectory("peripherals://all/", peripherals); |
| 307 |
for (int i = 0; i < peripherals.Size(); ++i) |
| 308 |
{ |
| 309 |
if (peripherals[i]->GetProperty("location").asString() == peripheral->Location()) |
| 310 |
{ |
| 311 |
peripheralItem = peripherals[i]; |
| 312 |
break; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
if (!peripheralItem) |
| 317 |
{ |
| 318 |
CLog::Log(LOGERROR, "Failed to get peripheral for location {}", peripheral->Location()); |
| 319 |
if (peripherals.IsEmpty()) |
| 320 |
CLog::Log(LOGERROR, "No peripherals available"); |
| 321 |
else |
| 322 |
{ |
| 323 |
CLog::Log(LOGERROR, "Available peripherals are:"); |
| 324 |
for (int i = 0; i < peripherals.Size(); ++i) |
| 325 |
CLog::Log(LOGERROR, " - \"{}\" ({})", peripherals[i]->GetProperty("location").asString(), |
| 326 |
peripherals[i]->GetPath()); |
| 327 |
} |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
// Remember controller profile to detect changes |
| 332 |
const std::string oldControllerId = |
| 333 |
peripheral->ControllerProfile() ? peripheral->ControllerProfile()->ID() : ""; |
| 334 |
|
| 335 |
// Pass peripheral item properties to settings dialog so skin authors |
| 336 |
// can use it to show more detailed information about the device |
| 337 |
pSettingsDialog->SetProperty("vendor", peripheralItem->GetProperty("vendor")); |
| 338 |
pSettingsDialog->SetProperty("product", peripheralItem->GetProperty("product")); |
| 339 |
pSettingsDialog->SetProperty("bus", peripheralItem->GetProperty("bus")); |
| 340 |
pSettingsDialog->SetProperty("location", peripheralItem->GetProperty("location")); |
| 341 |
pSettingsDialog->SetProperty("class", peripheralItem->GetProperty("class")); |
| 342 |
pSettingsDialog->SetProperty("version", peripheralItem->GetProperty("version")); |
| 343 |
|
| 344 |
// Open settings dialog |
| 345 |
pSettingsDialog->SetFileItem(peripheralItem.get()); |
| 346 |
pSettingsDialog->RegisterPeripheralManager(CServiceBroker::GetPeripherals()); |
| 347 |
pSettingsDialog->Open(); |
| 348 |
pSettingsDialog->UnregisterPeripheralManager(); |
| 349 |
|
| 350 |
// Check for changes in the controller profile |
| 351 |
const std::string newControllerId = |
| 352 |
peripheral->ControllerProfile() ? peripheral->ControllerProfile()->ID() : ""; |
| 353 |
|
| 354 |
if (oldControllerId != newControllerId) |
| 355 |
{ |
| 356 |
CServiceBroker::GetPeripherals().SetChanged(true); |
| 357 |
CServiceBroker::GetPeripherals().NotifyObservers(ObservableMessagePeripheralsChanged); |
| 358 |
} |
| 359 |
} |