| 1 |
/* |
| 2 |
* Copyright (C) 2014-2018 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 "GUIControllerList.h" |
| 10 |
|
| 11 |
#include "GUIControllerDefines.h" |
| 12 |
#include "GUIControllerWindow.h" |
| 13 |
#include "GUIFeatureList.h" |
| 14 |
#include "ServiceBroker.h" |
| 15 |
#include "addons/AddonManager.h" |
| 16 |
#include "dialogs/GUIDialogYesNo.h" |
| 17 |
#include "games/GameServices.h" |
| 18 |
#include "games/addons/GameClient.h" |
| 19 |
#include "games/addons/input/GameClientInput.h" |
| 20 |
#include "games/controllers/Controller.h" |
| 21 |
#include "games/controllers/ControllerIDs.h" |
| 22 |
#include "games/controllers/ControllerLayout.h" |
| 23 |
#include "games/controllers/guicontrols/GUIControllerButton.h" |
| 24 |
#include "games/controllers/guicontrols/GUIGameController.h" |
| 25 |
#include "games/controllers/types/ControllerTree.h" |
| 26 |
#include "guilib/GUIButtonControl.h" |
| 27 |
#include "guilib/GUIControlGroupList.h" |
| 28 |
#include "guilib/GUIMessage.h" |
| 29 |
#include "guilib/GUIWindow.h" |
| 30 |
#include "messaging/ApplicationMessenger.h" |
| 31 |
#include "peripherals/Peripherals.h" |
| 32 |
#include "utils/StringUtils.h" |
| 33 |
|
| 34 |
#include <algorithm> |
| 35 |
#include <assert.h> |
| 36 |
#include <iterator> |
| 37 |
|
| 38 |
using namespace KODI; |
| 39 |
using namespace GAME; |
| 40 |
|
| 41 |
CGUIControllerList::CGUIControllerList(CGUIWindow* window, |
| 42 |
IFeatureList* featureList, |
| 43 |
GameClientPtr gameClient, |
| 44 |
std::string controllerId) |
| 45 |
: m_guiWindow(window), |
| 46 |
m_featureList(featureList), |
| 47 |
m_gameClient(std::move(gameClient)), |
| 48 |
m_controllerId(std::move(controllerId)) |
| 49 |
{ |
| 50 |
assert(m_featureList != nullptr); |
| 51 |
} |
| 52 |
|
| 53 |
bool CGUIControllerList::Initialize(void) |
| 54 |
{ |
| 55 |
m_controllerList = |
| 56 |
dynamic_cast<CGUIControlGroupList*>(m_guiWindow->GetControl(CONTROL_CONTROLLER_LIST)); |
| 57 |
m_controllerButton = |
| 58 |
dynamic_cast<CGUIButtonControl*>(m_guiWindow->GetControl(CONTROL_CONTROLLER_BUTTON_TEMPLATE)); |
| 59 |
|
| 60 |
if (m_controllerButton) |
| 61 |
m_controllerButton->SetVisible(false); |
| 62 |
|
| 63 |
CServiceBroker::GetAddonMgr().Events().Subscribe(this, &CGUIControllerList::OnEvent); |
| 64 |
Refresh(""); |
| 65 |
|
| 66 |
return m_controllerList != nullptr && m_controllerButton != nullptr; |
| 67 |
} |
| 68 |
|
| 69 |
void CGUIControllerList::Deinitialize(void) |
| 70 |
{ |
| 71 |
CServiceBroker::GetAddonMgr().Events().Unsubscribe(this); |
| 72 |
|
| 73 |
CleanupButtons(); |
| 74 |
|
| 75 |
m_controllerList = nullptr; |
| 76 |
m_controllerButton = nullptr; |
| 77 |
} |
| 78 |
|
| 79 |
bool CGUIControllerList::Refresh(const std::string& controllerId) |
| 80 |
{ |
| 81 |
// Focus specified controller after refresh |
| 82 |
std::string focusController = controllerId; |
| 83 |
|
| 84 |
if (focusController.empty() && m_focusedController >= 0) |
| 85 |
{ |
| 86 |
// If controller ID wasn't provided, focus current controller |
| 87 |
focusController = m_controllers[m_focusedController]->ID(); |
| 88 |
} |
| 89 |
|
| 90 |
if (!RefreshControllers()) |
| 91 |
return false; |
| 92 |
|
| 93 |
CleanupButtons(); |
| 94 |
|
| 95 |
if (m_controllerList) |
| 96 |
{ |
| 97 |
unsigned int buttonId = 0; |
| 98 |
for (const auto& controller : m_controllers) |
| 99 |
{ |
| 100 |
CGUIButtonControl* pButton = |
| 101 |
new CGUIControllerButton(*m_controllerButton, controller->Layout().Label(), buttonId++); |
| 102 |
m_controllerList->AddControl(pButton); |
| 103 |
|
| 104 |
if (!focusController.empty() && controller->ID() == focusController) |
| 105 |
{ |
| 106 |
CGUIMessage msg(GUI_MSG_SETFOCUS, m_guiWindow->GetID(), pButton->GetID()); |
| 107 |
m_guiWindow->OnMessage(msg); |
| 108 |
} |
| 109 |
|
| 110 |
// Just in case |
| 111 |
if (buttonId >= MAX_CONTROLLER_COUNT) |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
void CGUIControllerList::OnFocus(unsigned int controllerIndex) |
| 120 |
{ |
| 121 |
if (controllerIndex < m_controllers.size()) |
| 122 |
{ |
| 123 |
m_focusedController = controllerIndex; |
| 124 |
|
| 125 |
const ControllerPtr& controller = m_controllers[controllerIndex]; |
| 126 |
m_featureList->Load(controller); |
| 127 |
|
| 128 |
//! @todo Activate controller for all game controller controls |
| 129 |
CGUIGameController* pController = |
| 130 |
dynamic_cast<CGUIGameController*>(m_guiWindow->GetControl(CONTROL_GAME_CONTROLLER)); |
| 131 |
if (pController) |
| 132 |
pController->ActivateController(controller); |
| 133 |
|
| 134 |
// Update controller description |
| 135 |
CGUIMessage msg(GUI_MSG_LABEL_SET, m_guiWindow->GetID(), CONTROL_CONTROLLER_DESCRIPTION); |
| 136 |
msg.SetLabel(controller->Description()); |
| 137 |
m_guiWindow->OnMessage(msg); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void CGUIControllerList::OnSelect(unsigned int controllerIndex) |
| 142 |
{ |
| 143 |
m_featureList->OnSelect(0); |
| 144 |
} |
| 145 |
|
| 146 |
void CGUIControllerList::ResetController(void) |
| 147 |
{ |
| 148 |
if (0 <= m_focusedController && m_focusedController < (int)m_controllers.size()) |
| 149 |
{ |
| 150 |
const std::string strControllerId = m_controllers[m_focusedController]->ID(); |
| 151 |
|
| 152 |
//! @todo Choose peripheral |
| 153 |
// For now, ask the user if they would like to reset all peripherals |
| 154 |
// "Reset controller profile" |
| 155 |
// "Would you like to reset this controller profile for all devices?" |
| 156 |
if (!CGUIDialogYesNo::ShowAndGetInput(35060, 35061)) |
| 157 |
return; |
| 158 |
|
| 159 |
CServiceBroker::GetPeripherals().ResetButtonMaps(strControllerId); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
void CGUIControllerList::OnEvent(const ADDON::AddonEvent& event) |
| 164 |
{ |
| 165 |
if (typeid(event) == typeid(ADDON::AddonEvents::Enabled) || // also called on install, |
| 166 |
typeid(event) == typeid(ADDON::AddonEvents::Disabled) || // not called on uninstall |
| 167 |
typeid(event) == typeid(ADDON::AddonEvents::ReInstalled) || |
| 168 |
typeid(event) == typeid(ADDON::AddonEvents::UnInstalled)) |
| 169 |
{ |
| 170 |
CGUIMessage msg(GUI_MSG_REFRESH_LIST, m_guiWindow->GetID(), CONTROL_CONTROLLER_LIST); |
| 171 |
|
| 172 |
// Focus installed add-on |
| 173 |
if (typeid(event) == typeid(ADDON::AddonEvents::Enabled) || |
| 174 |
typeid(event) == typeid(ADDON::AddonEvents::ReInstalled)) |
| 175 |
msg.SetStringParam(event.addonId); |
| 176 |
|
| 177 |
CServiceBroker::GetAppMessenger()->SendGUIMessage(msg, m_guiWindow->GetID()); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
bool CGUIControllerList::RefreshControllers(void) |
| 182 |
{ |
| 183 |
// Get current controllers |
| 184 |
CGameServices& gameServices = CServiceBroker::GetGameServices(); |
| 185 |
ControllerVector newControllers = gameServices.GetControllers(); |
| 186 |
|
| 187 |
// Filter by specified controller ID |
| 188 |
if (!m_controllerId.empty()) |
| 189 |
{ |
| 190 |
newControllers.erase(std::remove_if(newControllers.begin(), newControllers.end(), |
| 191 |
[this](const ControllerPtr& controller) |
| 192 |
{ return controller->ID() != m_controllerId; }), |
| 193 |
newControllers.end()); |
| 194 |
} |
| 195 |
// Filter by current game add-on |
| 196 |
else if (m_gameClient) |
| 197 |
{ |
| 198 |
const CControllerTree& controllers = m_gameClient->Input().GetDefaultControllerTree(); |
| 199 |
|
| 200 |
auto ControllerNotAccepted = [&controllers](const ControllerPtr& controller) |
| 201 |
{ return !controllers.IsControllerAccepted(controller->ID()); }; |
| 202 |
|
| 203 |
if (!std::all_of(newControllers.begin(), newControllers.end(), ControllerNotAccepted)) |
| 204 |
newControllers.erase( |
| 205 |
std::remove_if(newControllers.begin(), newControllers.end(), ControllerNotAccepted), |
| 206 |
newControllers.end()); |
| 207 |
} |
| 208 |
|
| 209 |
if (newControllers.empty()) |
| 210 |
newControllers.emplace_back(gameServices.GetDefaultController()); |
| 211 |
|
| 212 |
// Check for changes |
| 213 |
std::set<std::string> oldControllerIds; |
| 214 |
std::set<std::string> newControllerIds; |
| 215 |
|
| 216 |
auto GetControllerID = [](const ControllerPtr& controller) { return controller->ID(); }; |
| 217 |
|
| 218 |
std::transform(m_controllers.begin(), m_controllers.end(), |
| 219 |
std::inserter(oldControllerIds, oldControllerIds.begin()), GetControllerID); |
| 220 |
std::transform(newControllers.begin(), newControllers.end(), |
| 221 |
std::inserter(newControllerIds, newControllerIds.begin()), GetControllerID); |
| 222 |
|
| 223 |
const bool bChanged = (oldControllerIds != newControllerIds); |
| 224 |
if (bChanged) |
| 225 |
{ |
| 226 |
m_controllers = std::move(newControllers); |
| 227 |
|
| 228 |
// Sort add-ons, with default controller first |
| 229 |
std::sort(m_controllers.begin(), m_controllers.end(), |
| 230 |
[](const ControllerPtr& i, const ControllerPtr& j) |
| 231 |
{ |
| 232 |
if (i->ID() == DEFAULT_CONTROLLER_ID && j->ID() != DEFAULT_CONTROLLER_ID) |
| 233 |
return true; |
| 234 |
if (i->ID() != DEFAULT_CONTROLLER_ID && j->ID() == DEFAULT_CONTROLLER_ID) |
| 235 |
return false; |
| 236 |
|
| 237 |
return StringUtils::CompareNoCase(i->Layout().Label(), j->Layout().Label()) < 0; |
| 238 |
}); |
| 239 |
} |
| 240 |
|
| 241 |
return bChanged; |
| 242 |
} |
| 243 |
|
| 244 |
void CGUIControllerList::CleanupButtons(void) |
| 245 |
{ |
| 246 |
if (m_controllerList) |
| 247 |
m_controllerList->ClearAll(); |
| 248 |
} |