Debian Codegraph @

xbmc/games/controllers/windows/GUIControllerWindow.cpp

raw
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 "GUIControllerWindow.h"
10
11 #include "GUIControllerDefines.h"
12 #include "GUIControllerList.h"
13 #include "GUIFeatureList.h"
14 #include "ServiceBroker.h"
15 #include "addons/AddonManager.h"
16 #include "addons/IAddon.h"
17 #include "addons/addoninfo/AddonType.h"
18 #include "addons/gui/GUIWindowAddonBrowser.h"
19 #include "cores/RetroPlayer/guibridge/GUIGameRenderManager.h"
20 #include "cores/RetroPlayer/guibridge/GUIGameSettingsHandle.h"
21 #include "games/addons/GameClient.h"
22 #include "games/controllers/dialogs/ControllerInstaller.h"
23 #include "games/controllers/dialogs/GUIDialogIgnoreInput.h"
24 #include "guilib/GUIButtonControl.h"
25 #include "guilib/GUIControl.h"
26 #include "guilib/GUIMessage.h"
27 #include "guilib/WindowIDs.h"
28 #include "messaging/helpers/DialogOKHelper.h"
29
30 // To enable button mapping support
31 #include "peripherals/Peripherals.h"
32
33 using namespace KODI;
34 using namespace GAME;
35
36 CGUIControllerWindow::CGUIControllerWindow(void)
37 : CGUIDialog(WINDOW_DIALOG_GAME_CONTROLLERS, "DialogGameControllers.xml"),
38 m_installer(new CControllerInstaller)
39 {
40 // initialize CGUIWindow
41 m_loadType = KEEP_IN_MEMORY;
42 }
43
44 CGUIControllerWindow::~CGUIControllerWindow(void)
45 {
46 delete m_controllerList;
47 delete m_featureList;
48 }
49
50 void CGUIControllerWindow::DoProcess(unsigned int currentTime, CDirtyRegionList& dirtyregions)
51 {
52 /*
53 * Apply the faded focus texture to the current controller when unfocused
54 */
55
56 CGUIControl* control = nullptr; // The controller button
57 bool bAlphaFaded = false; // True if the controller button has been focused and faded this frame
58
59 if (m_controllerList && m_controllerList->GetFocusedController() >= 0)
60 {
61 control = GetFirstFocusableControl(CONTROL_CONTROLLER_BUTTONS_START +
62 m_controllerList->GetFocusedController());
63 if (control && !control->HasFocus())
64 {
65 if (control->GetControlType() == CGUIControl::GUICONTROL_BUTTON)
66 {
67 control->SetFocus(true);
68 static_cast<CGUIButtonControl*>(control)->SetAlpha(0x80);
69 bAlphaFaded = true;
70 }
71 }
72 }
73
74 CGUIDialog::DoProcess(currentTime, dirtyregions);
75
76 if (control && bAlphaFaded)
77 {
78 control->SetFocus(false);
79 if (control->GetControlType() == CGUIControl::GUICONTROL_BUTTON)
80 static_cast<CGUIButtonControl*>(control)->SetAlpha(0xFF);
81 }
82 }
83
84 bool CGUIControllerWindow::OnMessage(CGUIMessage& message)
85 {
86 // Set to true to block the call to the super class
87 bool bHandled = false;
88
89 switch (message.GetMessage())
90 {
91 case GUI_MSG_WINDOW_INIT:
92 {
93 m_controllerId = message.GetStringParam();
94 break;
95 }
96 case GUI_MSG_CLICKED:
97 {
98 int controlId = message.GetSenderId();
99
100 if (controlId == CONTROL_CLOSE_BUTTON)
101 {
102 Close();
103 bHandled = true;
104 }
105 else if (controlId == CONTROL_GET_MORE)
106 {
107 GetMoreControllers();
108 bHandled = true;
109 }
110 else if (controlId == CONTROL_GET_ALL)
111 {
112 GetAllControllers();
113 bHandled = true;
114 }
115 else if (controlId == CONTROL_RESET_BUTTON)
116 {
117 ResetController();
118 bHandled = true;
119 }
120 else if (controlId == CONTROL_HELP_BUTTON)
121 {
122 ShowHelp();
123 bHandled = true;
124 }
125 else if (controlId == CONTROL_FIX_SKIPPING)
126 {
127 ShowButtonCaptureDialog();
128 }
129 else if (CONTROL_CONTROLLER_BUTTONS_START <= controlId &&
130 controlId < CONTROL_CONTROLLER_BUTTONS_END)
131 {
132 OnControllerSelected(controlId - CONTROL_CONTROLLER_BUTTONS_START);
133 bHandled = true;
134 }
135 else if (CONTROL_FEATURE_BUTTONS_START <= controlId &&
136 controlId < CONTROL_FEATURE_BUTTONS_END)
137 {
138 OnFeatureSelected(controlId - CONTROL_FEATURE_BUTTONS_START);
139 bHandled = true;
140 }
141 break;
142 }
143 case GUI_MSG_FOCUSED:
144 {
145 int controlId = message.GetControlId();
146
147 if (CONTROL_CONTROLLER_BUTTONS_START <= controlId &&
148 controlId < CONTROL_CONTROLLER_BUTTONS_END)
149 {
150 OnControllerFocused(controlId - CONTROL_CONTROLLER_BUTTONS_START);
151 }
152 else if (CONTROL_FEATURE_BUTTONS_START <= controlId &&
153 controlId < CONTROL_FEATURE_BUTTONS_END)
154 {
155 OnFeatureFocused(controlId - CONTROL_FEATURE_BUTTONS_START);
156 }
157 break;
158 }
159 case GUI_MSG_SETFOCUS:
160 {
161 int controlId = message.GetControlId();
162
163 if (CONTROL_CONTROLLER_BUTTONS_START <= controlId &&
164 controlId < CONTROL_CONTROLLER_BUTTONS_END)
165 {
166 OnControllerFocused(controlId - CONTROL_CONTROLLER_BUTTONS_START);
167 }
168 else if (CONTROL_FEATURE_BUTTONS_START <= controlId &&
169 controlId < CONTROL_FEATURE_BUTTONS_END)
170 {
171 OnFeatureFocused(controlId - CONTROL_FEATURE_BUTTONS_START);
172 }
173 break;
174 }
175 case GUI_MSG_REFRESH_LIST:
176 {
177 int controlId = message.GetControlId();
178
179 if (controlId == CONTROL_CONTROLLER_LIST)
180 {
181 const std::string controllerId = message.GetStringParam();
182 if (m_controllerList && m_controllerList->Refresh(controllerId))
183 {
184 CGUIDialog::OnMessage(message);
185 bHandled = true;
186 }
187 }
188 break;
189 }
190 default:
191 break;
192 }
193
194 if (!bHandled)
195 bHandled = CGUIDialog::OnMessage(message);
196
197 return bHandled;
198 }
199
200 void CGUIControllerWindow::OnEvent(const ADDON::CRepositoryUpdater::RepositoryUpdated& event)
201 {
202 UpdateButtons();
203 }
204
205 void CGUIControllerWindow::OnEvent(const ADDON::AddonEvent& event)
206 {
207 using namespace ADDON;
208
209 if (typeid(event) == typeid(AddonEvents::Enabled) || // also called on install,
210 typeid(event) == typeid(AddonEvents::Disabled) || // not called on uninstall
211 typeid(event) == typeid(AddonEvents::UnInstalled) ||
212 typeid(event) == typeid(AddonEvents::ReInstalled))
213 {
214 if (CServiceBroker::GetAddonMgr().HasType(event.addonId, AddonType::GAME_CONTROLLER))
215 {
216 UpdateButtons();
217 }
218 }
219 }
220
221 void CGUIControllerWindow::OnInitWindow(void)
222 {
223 // Get active game add-on
224 GameClientPtr gameClient;
225 {
226 auto gameSettingsHandle = CServiceBroker::GetGameRenderManager().RegisterGameSettingsDialog();
227 if (gameSettingsHandle)
228 {
229 ADDON::AddonPtr addon;
230 if (CServiceBroker::GetAddonMgr().GetAddon(gameSettingsHandle->GameClientID(), addon,
231 ADDON::AddonType::GAMEDLL,
232 ADDON::OnlyEnabled::CHOICE_YES))
233 gameClient = std::static_pointer_cast<CGameClient>(addon);
234 }
235 }
236 m_gameClient = std::move(gameClient);
237
238 CGUIDialog::OnInitWindow();
239
240 if (!m_featureList)
241 {
242 m_featureList = new CGUIFeatureList(this, m_gameClient);
243 if (!m_featureList->Initialize())
244 {
245 delete m_featureList;
246 m_featureList = nullptr;
247 }
248 }
249
250 if (!m_controllerList && m_featureList)
251 {
252 m_controllerList = new CGUIControllerList(this, m_featureList, m_gameClient, m_controllerId);
253 if (!m_controllerList->Initialize())
254 {
255 delete m_controllerList;
256 m_controllerList = nullptr;
257 }
258 }
259
260 // Focus the first controller so that the feature list is loaded properly
261 CGUIMessage msgFocus(GUI_MSG_SETFOCUS, GetID(), CONTROL_CONTROLLER_BUTTONS_START);
262 OnMessage(msgFocus);
263
264 // Enable button mapping support
265 CServiceBroker::GetPeripherals().EnableButtonMapping();
266
267 UpdateButtons();
268
269 // subscribe to events
270 CServiceBroker::GetRepositoryUpdater().Events().Subscribe(this, &CGUIControllerWindow::OnEvent);
271 CServiceBroker::GetAddonMgr().Events().Subscribe(this, &CGUIControllerWindow::OnEvent);
272 }
273
274 void CGUIControllerWindow::OnDeinitWindow(int nextWindowID)
275 {
276 CServiceBroker::GetRepositoryUpdater().Events().Unsubscribe(this);
277 CServiceBroker::GetAddonMgr().Events().Unsubscribe(this);
278
279 if (m_controllerList)
280 {
281 m_controllerList->Deinitialize();
282 delete m_controllerList;
283 m_controllerList = nullptr;
284 }
285
286 if (m_featureList)
287 {
288 m_featureList->Deinitialize();
289 delete m_featureList;
290 m_featureList = nullptr;
291 }
292
293 CGUIDialog::OnDeinitWindow(nextWindowID);
294
295 m_gameClient.reset();
296 }
297
298 void CGUIControllerWindow::OnControllerFocused(unsigned int controllerIndex)
299 {
300 if (m_controllerList)
301 m_controllerList->OnFocus(controllerIndex);
302 }
303
304 void CGUIControllerWindow::OnControllerSelected(unsigned int controllerIndex)
305 {
306 if (m_controllerList)
307 m_controllerList->OnSelect(controllerIndex);
308 }
309
310 void CGUIControllerWindow::OnFeatureFocused(unsigned int buttonIndex)
311 {
312 if (m_featureList)
313 m_featureList->OnFocus(buttonIndex);
314 }
315
316 void CGUIControllerWindow::OnFeatureSelected(unsigned int buttonIndex)
317 {
318 if (m_featureList)
319 m_featureList->OnSelect(buttonIndex);
320 }
321
322 void CGUIControllerWindow::UpdateButtons(void)
323 {
324 using namespace ADDON;
325
326 VECADDONS addons;
327 if (m_gameClient)
328 {
329 SET_CONTROL_HIDDEN(CONTROL_GET_MORE);
330 SET_CONTROL_HIDDEN(CONTROL_GET_ALL);
331 }
332 else
333 {
334 const bool bEnable = CServiceBroker::GetAddonMgr().GetInstallableAddons(
335 addons, ADDON::AddonType::GAME_CONTROLLER) &&
336 !addons.empty();
337 CONTROL_ENABLE_ON_CONDITION(CONTROL_GET_MORE, bEnable);
338 CONTROL_ENABLE_ON_CONDITION(CONTROL_GET_ALL, bEnable);
339 }
340 }
341
342 void CGUIControllerWindow::GetMoreControllers(void)
343 {
344 std::string strAddonId;
345 if (CGUIWindowAddonBrowser::SelectAddonID(ADDON::AddonType::GAME_CONTROLLER, strAddonId, false,
346 true, false, true, false) < 0)
347 {
348 // "Controller profiles"
349 // "All available controller profiles are installed."
350 MESSAGING::HELPERS::ShowOKDialogText(CVariant{35050}, CVariant{35062});
351 return;
352 }
353 }
354
355 void CGUIControllerWindow::GetAllControllers()
356 {
357 if (m_installer->IsRunning())
358 return;
359
360 m_installer->Create(false);
361 }
362
363 void CGUIControllerWindow::ResetController(void)
364 {
365 if (m_controllerList)
366 m_controllerList->ResetController();
367 }
368
369 void CGUIControllerWindow::ShowHelp(void)
370 {
371 // "Help"
372 // <help text>
373 MESSAGING::HELPERS::ShowOKDialogText(CVariant{10043}, CVariant{35055});
374 }
375
376 void CGUIControllerWindow::ShowButtonCaptureDialog(void)
377 {
378 CGUIDialogIgnoreInput dialog;
379 dialog.Show();
380 }