Debian Codegraph @

xbmc/games/controllers/guicontrols/GUIGameControllerList.cpp

raw
1 /*
2 * Copyright (C) 2022-2023 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 "GUIGameControllerList.h"
10
11 #include "FileItem.h"
12 #include "ServiceBroker.h"
13 #include "games/GameServices.h"
14 #include "games/addons/GameClient.h"
15 #include "games/addons/input/GameClientInput.h"
16 #include "games/addons/input/GameClientJoystick.h"
17 #include "games/addons/input/GameClientTopology.h"
18 #include "games/agents/input/AgentController.h"
19 #include "games/agents/input/AgentInput.h"
20 #include "games/controllers/Controller.h"
21 #include "games/controllers/listproviders/GUIGameControllerProvider.h"
22 #include "guilib/GUIListItem.h"
23 #include "guilib/GUIMessage.h"
24 #include "peripherals/devices/Peripheral.h"
25 #include "utils/Variant.h"
26
27 #include <algorithm>
28 #include <cstdlib>
29
30 using namespace KODI;
31 using namespace GAME;
32
33 CGUIGameControllerList::CGUIGameControllerList(int parentID,
34 int controlID,
35 float posX,
36 float posY,
37 float width,
38 float height,
39 ORIENTATION orientation,
40 uint32_t alignment,
41 const CScroller& scroller)
42 : CGUIListContainer(parentID, controlID, posX, posY, width, height, orientation, scroller, 0),
43 m_alignment(alignment)
44 {
45 // Initialize CGUIControl
46 ControlType = GUICONTROL_GAMECONTROLLERLIST;
47 }
48
49 CGUIGameControllerList::CGUIGameControllerList(const CGUIGameControllerList& other)
50 : CGUIListContainer(other), m_alignment(other.m_alignment)
51 {
52 // Initialize CGUIControl
53 ControlType = GUICONTROL_GAMECONTROLLERLIST;
54 }
55
56 CGUIGameControllerList* CGUIGameControllerList::Clone(void) const
57 {
58 return new CGUIGameControllerList(*this);
59 }
60
61 void CGUIGameControllerList::UpdateInfo(const CGUIListItem* item)
62 {
63 CGUIListContainer::UpdateInfo(item);
64
65 if (item == nullptr)
66 return;
67
68 CAgentInput& agentInput = CServiceBroker::GetGameServices().AgentInput();
69
70 // Update port count
71 const std::vector<std::string> inputPorts = agentInput.GetGameInputPorts();
72 m_portCount = inputPorts.size();
73
74 // Update port index
75 UpdatePort(item->GetCurrentItem(), inputPorts);
76
77 bool bUpdateListProvider = false;
78
79 // Update CGUIListContainer
80 if (!m_listProvider)
81 {
82 m_listProvider = std::make_unique<CGUIGameControllerProvider>(
83 m_portCount, m_portIndex, m_peripheralLocation, m_alignment, GetParentID());
84 bUpdateListProvider = true;
85 }
86
87 // Update controller provider
88 CGUIGameControllerProvider* controllerProvider =
89 dynamic_cast<CGUIGameControllerProvider*>(m_listProvider.get());
90 if (controllerProvider != nullptr)
91 {
92 // Update port count
93 if (controllerProvider->GetPortCount() != m_portCount)
94 {
95 controllerProvider->SetPortCount(m_portCount);
96 bUpdateListProvider = true;
97 }
98
99 // Update player count
100 if (controllerProvider->GetPortIndex() != m_portIndex)
101 {
102 controllerProvider->SetPortIndex(m_portIndex);
103 bUpdateListProvider = true;
104 }
105
106 // Update current controller
107 const std::string newControllerId = item->GetProperty("Addon.ID").asString();
108 if (!newControllerId.empty())
109 {
110 std::string currentControllerId;
111 if (controllerProvider->GetControllerProfile())
112 currentControllerId = controllerProvider->GetControllerProfile()->ID();
113
114 if (currentControllerId != newControllerId)
115 {
116 CGameServices& gameServices = CServiceBroker::GetGameServices();
117 ControllerPtr controller = gameServices.GetController(newControllerId);
118 controllerProvider->SetControllerProfile(std::move(controller));
119 bUpdateListProvider = true;
120 }
121 }
122
123 // Update peripheral location
124 if (controllerProvider->GetPeripheralLocation() != m_peripheralLocation)
125 {
126 controllerProvider->SetPeripheralLocation(m_peripheralLocation);
127 bUpdateListProvider = true;
128 }
129 }
130
131 if (bUpdateListProvider)
132 UpdateListProvider(true);
133 }
134
135 void CGUIGameControllerList::SetGameClient(GAME::GameClientPtr gameClient)
136 {
137 m_gameClient = std::move(gameClient);
138 }
139
140 void CGUIGameControllerList::ClearGameClient()
141 {
142 m_gameClient.reset();
143 }
144
145 void CGUIGameControllerList::UpdatePort(int itemNumber, const std::vector<std::string>& inputPorts)
146 {
147 // Item numbers start from 1
148 if (itemNumber < 1)
149 return;
150
151 const unsigned int controllerIndex = static_cast<unsigned int>(itemNumber - 1);
152
153 CAgentInput& agentInput = CServiceBroker::GetGameServices().AgentInput();
154
155 std::vector<std::shared_ptr<const CAgentController>> agentControllers =
156 agentInput.GetControllers();
157 if (controllerIndex < static_cast<unsigned int>(agentControllers.size()))
158 {
159 const std::shared_ptr<const CAgentController>& agentController =
160 agentControllers.at(controllerIndex);
161 UpdatePortIndex(agentController->GetPeripheral(), inputPorts);
162 UpdatePeripheral(agentController->GetPeripheral());
163 }
164 }
165
166 void CGUIGameControllerList::UpdatePortIndex(const PERIPHERALS::PeripheralPtr& agentPeripheral,
167 const std::vector<std::string>& inputPorts)
168 {
169 const std::string portAddress = GetPortAddress(agentPeripheral);
170
171 m_portIndex = -1;
172
173 if (portAddress.empty())
174 return;
175
176 // Search ports for input provider's address
177 for (size_t i = 0; i < inputPorts.size(); ++i)
178 {
179 if (inputPorts.at(i) == portAddress)
180 {
181 // Found port address, record index
182 m_portIndex = i;
183 break;
184 }
185 }
186 }
187
188 void CGUIGameControllerList::UpdatePeripheral(const PERIPHERALS::PeripheralPtr& agentPeripheral)
189 {
190 m_peripheralLocation = agentPeripheral->Location();
191 }
192
193 std::string CGUIGameControllerList::GetPortAddress(
194 const PERIPHERALS::PeripheralPtr& agentPeripheral)
195 {
196 CAgentInput& agentInput = CServiceBroker::GetGameServices().AgentInput();
197
198 // Upcast peripheral to input providers
199 KEYBOARD::IKeyboardInputProvider* const keyboardInputProvider =
200 static_cast<KEYBOARD::IKeyboardInputProvider*>(agentPeripheral.get());
201 MOUSE::IMouseInputProvider* const mouseInputProvider =
202 static_cast<MOUSE::IMouseInputProvider*>(agentPeripheral.get());
203 JOYSTICK::IInputProvider* const joystickInputProvider =
204 static_cast<JOYSTICK::IInputProvider*>(agentPeripheral.get());
205
206 // See if the keyboard input provider has a port address
207 std::string keyboardAddress = agentInput.GetKeyboardAddress(keyboardInputProvider);
208 if (!keyboardAddress.empty())
209 return keyboardAddress;
210
211 // See if the mouse input provider has a port address
212 std::string mouseAddress = agentInput.GetMouseAddress(mouseInputProvider);
213 if (!mouseAddress.empty())
214 return mouseAddress;
215
216 // See if the joystick input provider has a port address
217 std::string joystickAddress = agentInput.GetPortAddress(joystickInputProvider);
218 if (!joystickAddress.empty())
219 return joystickAddress;
220
221 return "";
222 }