| 1 |
/* |
| 2 |
* Copyright (C) 2021 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 "GUIPortList.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "GUIPortDefines.h" |
| 13 |
#include "GUIPortWindow.h" |
| 14 |
#include "ServiceBroker.h" |
| 15 |
#include "addons/AddonManager.h" |
| 16 |
#include "games/GameServices.h" |
| 17 |
#include "games/addons/GameClient.h" |
| 18 |
#include "games/addons/input/GameClientInput.h" |
| 19 |
#include "games/controllers/Controller.h" |
| 20 |
#include "games/controllers/ControllerLayout.h" |
| 21 |
#include "games/controllers/types/ControllerHub.h" |
| 22 |
#include "games/controllers/types/ControllerTree.h" |
| 23 |
#include "games/ports/types/PortNode.h" |
| 24 |
#include "guilib/GUIMessage.h" |
| 25 |
#include "guilib/GUIWindow.h" |
| 26 |
#include "guilib/LocalizeStrings.h" |
| 27 |
#include "messaging/ApplicationMessenger.h" |
| 28 |
#include "messaging/helpers/DialogOKHelper.h" |
| 29 |
#include "utils/StringUtils.h" |
| 30 |
#include "utils/log.h" |
| 31 |
#include "view/GUIViewControl.h" |
| 32 |
#include "view/ViewState.h" |
| 33 |
|
| 34 |
using namespace KODI; |
| 35 |
using namespace GAME; |
| 36 |
|
| 37 |
CGUIPortList::CGUIPortList(CGUIWindow& window) |
| 38 |
: m_guiWindow(window), |
| 39 |
m_viewControl(std::make_unique<CGUIViewControl>()), |
| 40 |
m_vecItems(std::make_unique<CFileItemList>()) |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
CGUIPortList::~CGUIPortList() |
| 45 |
{ |
| 46 |
Deinitialize(); |
| 47 |
} |
| 48 |
|
| 49 |
void CGUIPortList::OnWindowLoaded() |
| 50 |
{ |
| 51 |
m_viewControl->Reset(); |
| 52 |
m_viewControl->SetParentWindow(m_guiWindow.GetID()); |
| 53 |
m_viewControl->AddView(m_guiWindow.GetControl(CONTROL_PORT_LIST)); |
| 54 |
} |
| 55 |
|
| 56 |
void CGUIPortList::OnWindowUnload() |
| 57 |
{ |
| 58 |
m_viewControl->Reset(); |
| 59 |
} |
| 60 |
|
| 61 |
bool CGUIPortList::Initialize(GameClientPtr gameClient) |
| 62 |
{ |
| 63 |
// Validate parameters |
| 64 |
if (!gameClient) |
| 65 |
return false; |
| 66 |
|
| 67 |
// Initialize state |
| 68 |
m_gameClient = std::move(gameClient); |
| 69 |
m_viewControl->SetCurrentView(DEFAULT_VIEW_LIST); |
| 70 |
|
| 71 |
// Initialize GUI |
| 72 |
Refresh(); |
| 73 |
|
| 74 |
CServiceBroker::GetAddonMgr().Events().Subscribe(this, &CGUIPortList::OnEvent); |
| 75 |
|
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
void CGUIPortList::Deinitialize() |
| 80 |
{ |
| 81 |
CServiceBroker::GetAddonMgr().Events().Unsubscribe(this); |
| 82 |
|
| 83 |
// Deinitialize GUI |
| 84 |
CleanupItems(); |
| 85 |
|
| 86 |
// Reset state |
| 87 |
m_gameClient.reset(); |
| 88 |
} |
| 89 |
|
| 90 |
bool CGUIPortList::HasControl(int controlId) |
| 91 |
{ |
| 92 |
return m_viewControl->HasControl(controlId); |
| 93 |
} |
| 94 |
|
| 95 |
int CGUIPortList::GetCurrentControl() |
| 96 |
{ |
| 97 |
return m_viewControl->GetCurrentControl(); |
| 98 |
} |
| 99 |
|
| 100 |
void CGUIPortList::Refresh() |
| 101 |
{ |
| 102 |
// Send a synchronous message to clear the view control |
| 103 |
m_viewControl->Clear(); |
| 104 |
|
| 105 |
CleanupItems(); |
| 106 |
|
| 107 |
if (m_gameClient) |
| 108 |
{ |
| 109 |
CControllerTree controllerTree = m_gameClient->Input().GetActiveControllerTree(); |
| 110 |
|
| 111 |
unsigned int itemIndex = 0; |
| 112 |
for (const CPortNode& port : controllerTree.GetPorts()) |
| 113 |
AddItems(port, itemIndex, GetLabel(port)); |
| 114 |
|
| 115 |
m_viewControl->SetItems(*m_vecItems); |
| 116 |
|
| 117 |
// Try to restore focus to the previously focused port |
| 118 |
if (!m_focusedPort.empty() && m_addressToItem.find(m_focusedPort) != m_addressToItem.end()) |
| 119 |
{ |
| 120 |
const unsigned int itemIndex = m_addressToItem[m_focusedPort]; |
| 121 |
m_viewControl->SetSelectedItem(itemIndex); |
| 122 |
OnItemFocus(itemIndex); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
void CGUIPortList::FrameMove() |
| 128 |
{ |
| 129 |
const int itemIndex = m_viewControl->GetSelectedItem(); |
| 130 |
if (itemIndex != m_currentItem) |
| 131 |
{ |
| 132 |
m_currentItem = itemIndex; |
| 133 |
if (itemIndex >= 0) |
| 134 |
OnItemFocus(static_cast<unsigned int>(itemIndex)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void CGUIPortList::SetFocused() |
| 139 |
{ |
| 140 |
m_viewControl->SetFocused(); |
| 141 |
} |
| 142 |
|
| 143 |
bool CGUIPortList::OnSelect() |
| 144 |
{ |
| 145 |
const int itemIndex = m_viewControl->GetSelectedItem(); |
| 146 |
if (itemIndex >= 0) |
| 147 |
{ |
| 148 |
OnItemSelect(static_cast<unsigned int>(itemIndex)); |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
void CGUIPortList::ResetPorts() |
| 156 |
{ |
| 157 |
if (m_gameClient) |
| 158 |
{ |
| 159 |
// Update the game client |
| 160 |
m_gameClient->Input().ResetPorts(); |
| 161 |
m_gameClient->Input().SavePorts(); |
| 162 |
|
| 163 |
// Refresh the GUI |
| 164 |
CGUIMessage msg(GUI_MSG_REFRESH_LIST, m_guiWindow.GetID(), CONTROL_PORT_LIST); |
| 165 |
CServiceBroker::GetAppMessenger()->SendGUIMessage(msg, m_guiWindow.GetID()); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
void CGUIPortList::OnEvent(const ADDON::AddonEvent& event) |
| 170 |
{ |
| 171 |
if (typeid(event) == typeid(ADDON::AddonEvents::Enabled) || // Also called on install |
| 172 |
typeid(event) == typeid(ADDON::AddonEvents::Disabled) || // Not called on uninstall |
| 173 |
typeid(event) == typeid(ADDON::AddonEvents::ReInstalled) || |
| 174 |
typeid(event) == typeid(ADDON::AddonEvents::UnInstalled)) |
| 175 |
{ |
| 176 |
CGUIMessage msg(GUI_MSG_REFRESH_LIST, m_guiWindow.GetID(), CONTROL_PORT_LIST); |
| 177 |
msg.SetStringParam(event.addonId); |
| 178 |
CServiceBroker::GetAppMessenger()->SendGUIMessage(msg, m_guiWindow.GetID()); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
bool CGUIPortList::AddItems(const CPortNode& port, |
| 183 |
unsigned int& itemId, |
| 184 |
const std::string& itemLabel) |
| 185 |
{ |
| 186 |
// Validate parameters |
| 187 |
if (itemLabel.empty()) |
| 188 |
return false; |
| 189 |
|
| 190 |
// Record the port address so that we can decode item indexes later |
| 191 |
m_itemToAddress[itemId] = port.GetAddress(); |
| 192 |
m_addressToItem[port.GetAddress()] = itemId; |
| 193 |
|
| 194 |
if (port.IsConnected()) |
| 195 |
{ |
| 196 |
const CControllerNode& controllerNode = port.GetActiveController(); |
| 197 |
const ControllerPtr& controller = controllerNode.GetController(); |
| 198 |
|
| 199 |
// Create the list item |
| 200 |
CFileItemPtr item = std::make_shared<CFileItem>(itemLabel); |
| 201 |
item->SetLabel2(controller->Layout().Label()); |
| 202 |
item->SetPath(port.GetAddress()); |
| 203 |
item->SetArt("icon", controller->Layout().ImagePath()); |
| 204 |
m_vecItems->Add(std::move(item)); |
| 205 |
++itemId; |
| 206 |
|
| 207 |
// Handle items for child ports |
| 208 |
const PortVec& ports = controllerNode.GetHub().GetPorts(); |
| 209 |
for (const CPortNode& childPort : ports) |
| 210 |
{ |
| 211 |
std::ostringstream childItemLabel; |
| 212 |
childItemLabel << " - "; |
| 213 |
childItemLabel << controller->Layout().Label(); |
| 214 |
childItemLabel << " - "; |
| 215 |
childItemLabel << GetLabel(childPort); |
| 216 |
|
| 217 |
if (!AddItems(childPort, itemId, childItemLabel.str())) |
| 218 |
return false; |
| 219 |
} |
| 220 |
} |
| 221 |
else |
| 222 |
{ |
| 223 |
// Create the list item |
| 224 |
CFileItemPtr item = std::make_shared<CFileItem>(itemLabel); |
| 225 |
item->SetLabel2(g_localizeStrings.Get(13298)); // "Disconnected" |
| 226 |
item->SetPath(port.GetAddress()); |
| 227 |
item->SetArt("icon", "DefaultAddonNone.png"); |
| 228 |
m_vecItems->Add(std::move(item)); |
| 229 |
++itemId; |
| 230 |
} |
| 231 |
|
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
void CGUIPortList::CleanupItems() |
| 236 |
{ |
| 237 |
m_vecItems->Clear(); |
| 238 |
m_itemToAddress.clear(); |
| 239 |
m_addressToItem.clear(); |
| 240 |
} |
| 241 |
|
| 242 |
void CGUIPortList::OnItemFocus(unsigned int itemIndex) |
| 243 |
{ |
| 244 |
m_focusedPort = m_itemToAddress[itemIndex]; |
| 245 |
} |
| 246 |
|
| 247 |
void CGUIPortList::OnItemSelect(unsigned int itemIndex) |
| 248 |
{ |
| 249 |
if (m_gameClient) |
| 250 |
{ |
| 251 |
const auto it = m_itemToAddress.find(itemIndex); |
| 252 |
if (it == m_itemToAddress.end()) |
| 253 |
return; |
| 254 |
|
| 255 |
const std::string& portAddress = it->second; |
| 256 |
if (portAddress.empty()) |
| 257 |
return; |
| 258 |
|
| 259 |
CPortNode port = m_gameClient->Input().GetActiveControllerTree().GetPort(portAddress); |
| 260 |
|
| 261 |
ControllerVector controllers; |
| 262 |
for (const CControllerNode& controllerNode : port.GetCompatibleControllers()) |
| 263 |
controllers.emplace_back(controllerNode.GetController()); |
| 264 |
|
| 265 |
// Get current controller to give initial focus |
| 266 |
ControllerPtr controller = port.GetActiveController().GetController(); |
| 267 |
|
| 268 |
// Check if we should show a "disconnect" option |
| 269 |
const bool showDisconnect = !port.IsForceConnected(); |
| 270 |
|
| 271 |
auto callback = [this, port = std::move(port)](const ControllerPtr& controller) |
| 272 |
{ OnControllerSelected(port, controller); }; |
| 273 |
|
| 274 |
m_controllerSelectDialog.Initialize(std::move(controllers), std::move(controller), |
| 275 |
showDisconnect, callback); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
void CGUIPortList::OnControllerSelected(const CPortNode& port, const ControllerPtr& controller) |
| 280 |
{ |
| 281 |
if (m_gameClient) |
| 282 |
{ |
| 283 |
// Translate parameter |
| 284 |
const bool bConnected = static_cast<bool>(controller); |
| 285 |
|
| 286 |
// Update the game client |
| 287 |
const bool bSuccess = |
| 288 |
bConnected ? m_gameClient->Input().ConnectController(port.GetAddress(), controller) |
| 289 |
: m_gameClient->Input().DisconnectController(port.GetAddress()); |
| 290 |
|
| 291 |
if (bSuccess) |
| 292 |
{ |
| 293 |
m_gameClient->Input().SavePorts(); |
| 294 |
} |
| 295 |
else |
| 296 |
{ |
| 297 |
// "Failed to change controller" |
| 298 |
// "The emulator "%s" had an internal error." |
| 299 |
MESSAGING::HELPERS::ShowOKDialogText( |
| 300 |
CVariant{35114}, |
| 301 |
CVariant{StringUtils::Format(g_localizeStrings.Get(35213), m_gameClient->Name())}); |
| 302 |
} |
| 303 |
|
| 304 |
// Send a GUI message to reload the port list |
| 305 |
CGUIMessage msg(GUI_MSG_REFRESH_LIST, m_guiWindow.GetID(), CONTROL_PORT_LIST); |
| 306 |
CServiceBroker::GetAppMessenger()->SendGUIMessage(msg, m_guiWindow.GetID()); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
std::string CGUIPortList::GetLabel(const CPortNode& port) |
| 311 |
{ |
| 312 |
const PORT_TYPE portType = port.GetPortType(); |
| 313 |
switch (portType) |
| 314 |
{ |
| 315 |
case PORT_TYPE::KEYBOARD: |
| 316 |
{ |
| 317 |
// "Keyboard" |
| 318 |
return g_localizeStrings.Get(35150); |
| 319 |
} |
| 320 |
case PORT_TYPE::MOUSE: |
| 321 |
{ |
| 322 |
// "Mouse" |
| 323 |
return g_localizeStrings.Get(35171); |
| 324 |
} |
| 325 |
case PORT_TYPE::CONTROLLER: |
| 326 |
{ |
| 327 |
const std::string& portId = port.GetPortID(); |
| 328 |
if (portId.empty()) |
| 329 |
{ |
| 330 |
CLog::Log(LOGERROR, "Controller port with address \"{}\" doesn't have a port ID", |
| 331 |
port.GetAddress()); |
| 332 |
} |
| 333 |
else |
| 334 |
{ |
| 335 |
// "Port {0:s}" |
| 336 |
const std::string& portString = g_localizeStrings.Get(35112); |
| 337 |
return StringUtils::Format(portString, portId); |
| 338 |
} |
| 339 |
break; |
| 340 |
} |
| 341 |
default: |
| 342 |
break; |
| 343 |
} |
| 344 |
|
| 345 |
return ""; |
| 346 |
} |