/
GUIControllerWindow.cpp
324 lines (282 loc) · 8.84 KB
/
GUIControllerWindow.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Copyright (C) 2014-2016 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this Program; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "GUIControllerWindow.h"
#include "GUIControllerDefines.h"
#include "GUIControllerList.h"
#include "GUIFeatureList.h"
#include "IConfigurationWindow.h"
#include "addons/GUIWindowAddonBrowser.h"
#include "addons/IAddon.h"
#include "addons/AddonManager.h"
#include "guilib/GUIButtonControl.h"
#include "guilib/GUIControl.h"
#include "guilib/GUIMessage.h"
#include "guilib/WindowIDs.h"
// To check for button mapping support
#include "dialogs/GUIDialogOK.h"
#include "peripherals/bus/virtual/PeripheralBusAddon.h"
#include "peripherals/Peripherals.h"
#include "utils/log.h"
// To check for installable controllers
#include "addons/AddonDatabase.h"
#include "addons/AddonManager.h"
using namespace GAME;
CGUIControllerWindow::CGUIControllerWindow(void) :
CGUIDialog(WINDOW_DIALOG_GAME_CONTROLLERS, "DialogGameControllers.xml"),
m_controllerList(nullptr),
m_featureList(nullptr)
{
// initialize CGUIWindow
m_loadType = KEEP_IN_MEMORY;
}
CGUIControllerWindow::~CGUIControllerWindow(void)
{
delete m_controllerList;
delete m_featureList;
}
void CGUIControllerWindow::DoProcess(unsigned int currentTime, CDirtyRegionList &dirtyregions)
{
/*
* Apply the faded focus texture to the current controller when unfocused
*/
CGUIControl* control = nullptr; // The controller button
bool bAlphaFaded = false; // True if the controller button has been focused and faded this frame
if (m_controllerList && m_controllerList->GetFocusedController() >= 0)
{
control = GetFirstFocusableControl(CONTROL_CONTROLLER_BUTTONS_START + m_controllerList->GetFocusedController());
if (control && !control->HasFocus())
{
if (control->GetControlType() == CGUIControl::GUICONTROL_BUTTON)
{
control->SetFocus(true);
static_cast<CGUIButtonControl*>(control)->SetAlpha(0x80);
bAlphaFaded = true;
}
}
}
CGUIDialog::DoProcess(currentTime, dirtyregions);
if (control && bAlphaFaded)
{
control->SetFocus(false);
if (control->GetControlType() == CGUIControl::GUICONTROL_BUTTON)
static_cast<CGUIButtonControl*>(control)->SetAlpha(0xFF);
}
}
bool CGUIControllerWindow::OnMessage(CGUIMessage& message)
{
switch (message.GetMessage())
{
case GUI_MSG_WINDOW_INIT:
{
//! @todo Process parameter
//std::string strParam = message.GetStringParam();
break;
}
case GUI_MSG_CLICKED:
{
int controlId = message.GetSenderId();
if (controlId == CONTROL_CLOSE_BUTTON)
{
Close();
return true;
}
else if (controlId == CONTROL_GET_MORE)
{
GetMoreControllers();
return true;
}
else if (controlId == CONTROL_RESET_BUTTON)
{
ResetController();
return true;
}
else if (controlId == CONTROL_HELP_BUTTON)
{
ShowHelp();
return true;
}
else if (CONTROL_CONTROLLER_BUTTONS_START <= controlId && controlId < CONTROL_CONTROLLER_BUTTONS_END)
{
OnControllerSelected(controlId - CONTROL_CONTROLLER_BUTTONS_START);
return true;
}
else if (CONTROL_FEATURE_BUTTONS_START <= controlId && controlId < CONTROL_FEATURE_BUTTONS_END)
{
OnFeatureSelected(controlId - CONTROL_FEATURE_BUTTONS_START);
return true;
}
break;
}
case GUI_MSG_FOCUSED:
{
int controlId = message.GetControlId();
if (CONTROL_CONTROLLER_BUTTONS_START <= controlId && controlId < CONTROL_CONTROLLER_BUTTONS_END)
{
OnControllerFocused(controlId - CONTROL_CONTROLLER_BUTTONS_START);
}
else if (CONTROL_FEATURE_BUTTONS_START <= controlId && controlId < CONTROL_FEATURE_BUTTONS_END)
{
OnFeatureFocused(controlId - CONTROL_FEATURE_BUTTONS_START);
}
break;
}
case GUI_MSG_SETFOCUS:
{
int controlId = message.GetControlId();
if (CONTROL_CONTROLLER_BUTTONS_START <= controlId && controlId < CONTROL_CONTROLLER_BUTTONS_END)
{
OnControllerFocused(controlId - CONTROL_CONTROLLER_BUTTONS_START);
}
else if (CONTROL_FEATURE_BUTTONS_START <= controlId && controlId < CONTROL_FEATURE_BUTTONS_END)
{
OnFeatureFocused(controlId - CONTROL_FEATURE_BUTTONS_START);
}
break;
}
case GUI_MSG_REFRESH_LIST:
{
int controlId = message.GetControlId();
if (controlId == CONTROL_CONTROLLER_LIST)
{
if (m_controllerList && m_controllerList->Refresh())
{
CGUIDialog::OnMessage(message);
return true;
}
}
break;
}
default:
break;
}
return CGUIDialog::OnMessage(message);
}
void CGUIControllerWindow::OnEvent(const ADDON::CRepositoryUpdater::RepositoryUpdated& event)
{
UpdateButtons();
}
void CGUIControllerWindow::OnInitWindow(void)
{
using namespace PERIPHERALS;
CGUIDialog::OnInitWindow();
if (!m_featureList)
{
m_featureList = new CGUIFeatureList(this);
if (!m_featureList->Initialize())
{
delete m_featureList;
m_featureList = nullptr;
}
}
if (!m_controllerList && m_featureList)
{
m_controllerList = new CGUIControllerList(this, m_featureList);
if (!m_controllerList->Initialize())
{
delete m_controllerList;
m_controllerList = nullptr;
}
}
// Focus the first controller so that the feature list is loaded properly
CGUIMessage msgFocus(GUI_MSG_SETFOCUS, GetID(), CONTROL_CONTROLLER_BUTTONS_START);
OnMessage(msgFocus);
// Check for button mapping support
//! @todo remove this
PeripheralBusAddonPtr bus = std::static_pointer_cast<CPeripheralBusAddon>(g_peripherals.GetBusByType(PERIPHERAL_BUS_ADDON));
if (bus && !bus->HasFeature(FEATURE_JOYSTICK))
{
//! @todo Move the XML implementation of button map storage from add-on to
//! Kodi while keeping support for add-on button-mapping
CLog::Log(LOGERROR, "Joystick support not found");
// "Joystick support not found"
// "Controller configuration is disabled. Install the proper joystick support add-on."
CGUIDialogOK::ShowAndGetInput(CVariant{35056}, CVariant{35057});
// close the window as there's nothing that can be done
Close();
}
// FIXME: not thread safe
// ADDON::CRepositoryUpdater::GetInstance().Events().Subscribe(this, &CGUIControllerWindow::OnEvent);
UpdateButtons();
}
void CGUIControllerWindow::OnDeinitWindow(int nextWindowID)
{
ADDON::CRepositoryUpdater::GetInstance().Events().Unsubscribe(this);
if (m_controllerList)
{
m_controllerList->Deinitialize();
delete m_controllerList;
m_controllerList = nullptr;
}
if (m_featureList)
{
m_featureList->Deinitialize();
delete m_featureList;
m_featureList = nullptr;
}
CGUIDialog::OnDeinitWindow(nextWindowID);
}
void CGUIControllerWindow::OnControllerFocused(unsigned int controllerIndex)
{
if (m_controllerList)
m_controllerList->OnFocus(controllerIndex);
}
void CGUIControllerWindow::OnControllerSelected(unsigned int controllerIndex)
{
if (m_controllerList)
m_controllerList->OnSelect(controllerIndex);
}
void CGUIControllerWindow::OnFeatureFocused(unsigned int featureIndex)
{
if (m_featureList)
m_featureList->OnFocus(featureIndex);
}
void CGUIControllerWindow::OnFeatureSelected(unsigned int featureIndex)
{
if (m_featureList)
m_featureList->OnSelect(featureIndex);
}
void CGUIControllerWindow::UpdateButtons(void)
{
using namespace ADDON;
VECADDONS addons;
CONTROL_ENABLE_ON_CONDITION(CONTROL_GET_MORE, CAddonMgr::GetInstance().GetInstallableAddons(addons, ADDON::ADDON_GAME_CONTROLLER) && !addons.empty());
}
void CGUIControllerWindow::GetMoreControllers(void)
{
std::string strAddonId;
if (CGUIWindowAddonBrowser::SelectAddonID(ADDON::ADDON_GAME_CONTROLLER, strAddonId, false, true, false, true, false) < 0)
{
// "Controller profiles"
// "All available controller profiles are installed."
CGUIDialogOK::ShowAndGetInput(CVariant{ 35050 }, CVariant{ 35062 });
return;
}
}
void CGUIControllerWindow::ResetController(void)
{
if (m_controllerList)
m_controllerList->ResetController();
}
void CGUIControllerWindow::ShowHelp(void)
{
// "Help"
// <help text>
CGUIDialogOK::ShowAndGetInput(CVariant{10043}, CVariant{35055});
}