summaryrefslogtreecommitdiffstats
path: root/xbmc/games/controllers/dialogs/ControllerSelect.cpp
blob: 2ef9941dea983ef8012777d71df9701d130d692a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 *  Copyright (C) 2021 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "ControllerSelect.h"

#include "FileItem.h"
#include "ServiceBroker.h"
#include "dialogs/GUIDialogSelect.h"
#include "games/controllers/Controller.h"
#include "games/controllers/ControllerLayout.h"
#include "games/controllers/ControllerTypes.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/LocalizeStrings.h"
#include "guilib/WindowIDs.h"
#include "utils/Variant.h"
#include "utils/log.h"

using namespace KODI;
using namespace GAME;

CControllerSelect::CControllerSelect() : CThread("ControllerSelect")
{
}

CControllerSelect::~CControllerSelect() = default;

void CControllerSelect::Initialize(ControllerVector controllers,
                                   ControllerPtr defaultController,
                                   bool showDisconnect,
                                   const std::function<void(ControllerPtr)>& callback)
{
  // Validate parameters
  if (!callback)
    return;

  // Stop thread and reset state
  Deinitialize();

  // Initialize state
  m_controllers = std::move(controllers);
  m_defaultController = std::move(defaultController);
  m_showDisconnect = showDisconnect;
  m_callback = callback;

  // Create thread
  Create(false);
}

void CControllerSelect::Deinitialize()
{
  // Stop thread
  StopThread(true);

  // Reset state
  m_controllers.clear();
  m_defaultController.reset();
  m_showDisconnect = true;
  m_callback = nullptr;
}

void CControllerSelect::Process()
{
  // Select first controller by default
  unsigned int initialSelected = 0;

  CGUIComponent* gui = CServiceBroker::GetGUI();
  if (gui == nullptr)
    return;

  CGUIWindowManager& windowManager = gui->GetWindowManager();

  auto pSelectDialog = windowManager.GetWindow<CGUIDialogSelect>(WINDOW_DIALOG_SELECT);
  if (pSelectDialog == nullptr)
    return;

  CLog::Log(LOGDEBUG, "Controller select: Showing dialog for {} controllers", m_controllers.size());

  CFileItemList items;
  for (const ControllerPtr& controller : m_controllers)
  {
    CFileItemPtr item(new CFileItem(controller->Layout().Label()));
    item->SetArt("icon", controller->Layout().ImagePath());
    items.Add(std::move(item));

    // Check if a specified controller should be selected by default
    if (m_defaultController && m_defaultController->ID() == controller->ID())
      initialSelected = items.Size() - 1;
  }

  if (m_showDisconnect)
  {
    // Add a button to disconnect the port
    CFileItemPtr item(new CFileItem(g_localizeStrings.Get(13298))); // "Disconnected"
    item->SetArt("icon", "DefaultAddonNone.png");
    items.Add(std::move(item));

    // Check if the disconnect button should be selected by default
    if (!m_defaultController)
      initialSelected = items.Size() - 1;
  }

  pSelectDialog->Reset();
  pSelectDialog->SetHeading(CVariant{35113}); // "Select a Controller"
  pSelectDialog->SetUseDetails(true);
  pSelectDialog->EnableButton(false, 186); // "OK""
  pSelectDialog->SetButtonFocus(false);
  for (const auto& it : items)
    pSelectDialog->Add(*it);
  pSelectDialog->SetSelected(static_cast<int>(initialSelected));
  pSelectDialog->Open();

  // If the thread was stopped, exit early
  if (m_bStop)
    return;

  if (pSelectDialog->IsConfirmed())
  {
    ControllerPtr resultController;

    const int selected = pSelectDialog->GetSelectedItem();
    if (0 <= selected && selected < static_cast<int>(m_controllers.size()))
      resultController = m_controllers.at(selected);

    // Fire a callback with the result
    m_callback(resultController);
  }
}