| 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 "ControllerSelect.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "dialogs/GUIDialogSelect.h" |
| 14 |
#include "games/controllers/Controller.h" |
| 15 |
#include "games/controllers/ControllerLayout.h" |
| 16 |
#include "games/controllers/ControllerTypes.h" |
| 17 |
#include "guilib/GUIComponent.h" |
| 18 |
#include "guilib/GUIWindowManager.h" |
| 19 |
#include "guilib/LocalizeStrings.h" |
| 20 |
#include "guilib/WindowIDs.h" |
| 21 |
#include "utils/Variant.h" |
| 22 |
#include "utils/log.h" |
| 23 |
|
| 24 |
using namespace KODI; |
| 25 |
using namespace GAME; |
| 26 |
|
| 27 |
CControllerSelect::CControllerSelect() : CThread("ControllerSelect") |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
CControllerSelect::~CControllerSelect() = default; |
| 32 |
|
| 33 |
void CControllerSelect::Initialize(ControllerVector controllers, |
| 34 |
ControllerPtr defaultController, |
| 35 |
bool showDisconnect, |
| 36 |
const std::function<void(ControllerPtr)>& callback) |
| 37 |
{ |
| 38 |
// Validate parameters |
| 39 |
if (!callback) |
| 40 |
return; |
| 41 |
|
| 42 |
// Stop thread and reset state |
| 43 |
Deinitialize(); |
| 44 |
|
| 45 |
// Initialize state |
| 46 |
m_controllers = std::move(controllers); |
| 47 |
m_defaultController = std::move(defaultController); |
| 48 |
m_showDisconnect = showDisconnect; |
| 49 |
m_callback = callback; |
| 50 |
|
| 51 |
// Create thread |
| 52 |
Create(false); |
| 53 |
} |
| 54 |
|
| 55 |
void CControllerSelect::Deinitialize() |
| 56 |
{ |
| 57 |
// Stop thread |
| 58 |
StopThread(true); |
| 59 |
|
| 60 |
// Reset state |
| 61 |
m_controllers.clear(); |
| 62 |
m_defaultController.reset(); |
| 63 |
m_showDisconnect = true; |
| 64 |
m_callback = nullptr; |
| 65 |
} |
| 66 |
|
| 67 |
void CControllerSelect::Process() |
| 68 |
{ |
| 69 |
// Select first controller by default |
| 70 |
unsigned int initialSelected = 0; |
| 71 |
|
| 72 |
CGUIComponent* gui = CServiceBroker::GetGUI(); |
| 73 |
if (gui == nullptr) |
| 74 |
return; |
| 75 |
|
| 76 |
CGUIWindowManager& windowManager = gui->GetWindowManager(); |
| 77 |
|
| 78 |
auto pSelectDialog = windowManager.GetWindow<CGUIDialogSelect>(WINDOW_DIALOG_SELECT); |
| 79 |
if (pSelectDialog == nullptr) |
| 80 |
return; |
| 81 |
|
| 82 |
CLog::Log(LOGDEBUG, "Controller select: Showing dialog for {} controllers", m_controllers.size()); |
| 83 |
|
| 84 |
CFileItemList items; |
| 85 |
for (const ControllerPtr& controller : m_controllers) |
| 86 |
{ |
| 87 |
CFileItemPtr item(new CFileItem(controller->Layout().Label())); |
| 88 |
item->SetArt("icon", controller->Layout().ImagePath()); |
| 89 |
items.Add(std::move(item)); |
| 90 |
|
| 91 |
// Check if a specified controller should be selected by default |
| 92 |
if (m_defaultController && m_defaultController->ID() == controller->ID()) |
| 93 |
initialSelected = items.Size() - 1; |
| 94 |
} |
| 95 |
|
| 96 |
if (m_showDisconnect) |
| 97 |
{ |
| 98 |
// Add a button to disconnect the port |
| 99 |
CFileItemPtr item(new CFileItem(g_localizeStrings.Get(13298))); // "Disconnected" |
| 100 |
item->SetArt("icon", "DefaultAddonNone.png"); |
| 101 |
items.Add(std::move(item)); |
| 102 |
|
| 103 |
// Check if the disconnect button should be selected by default |
| 104 |
if (!m_defaultController) |
| 105 |
initialSelected = items.Size() - 1; |
| 106 |
} |
| 107 |
|
| 108 |
pSelectDialog->Reset(); |
| 109 |
pSelectDialog->SetHeading(CVariant{35113}); // "Select a Controller" |
| 110 |
pSelectDialog->SetUseDetails(true); |
| 111 |
pSelectDialog->EnableButton(false, 186); // "OK"" |
| 112 |
pSelectDialog->SetButtonFocus(false); |
| 113 |
for (const auto& it : items) |
| 114 |
pSelectDialog->Add(*it); |
| 115 |
pSelectDialog->SetSelected(static_cast<int>(initialSelected)); |
| 116 |
pSelectDialog->Open(); |
| 117 |
|
| 118 |
// If the thread was stopped, exit early |
| 119 |
if (m_bStop) |
| 120 |
return; |
| 121 |
|
| 122 |
if (pSelectDialog->IsConfirmed()) |
| 123 |
{ |
| 124 |
ControllerPtr resultController; |
| 125 |
|
| 126 |
const int selected = pSelectDialog->GetSelectedItem(); |
| 127 |
if (0 <= selected && selected < static_cast<int>(m_controllers.size())) |
| 128 |
resultController = m_controllers.at(selected); |
| 129 |
|
| 130 |
// Fire a callback with the result |
| 131 |
m_callback(resultController); |
| 132 |
} |
| 133 |
} |