| 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 "GUIGameController.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "games/GameServices.h" |
| 14 |
#include "games/addons/input/GameClientTopology.h" |
| 15 |
#include "games/agents/input/AgentInput.h" |
| 16 |
#include "games/controllers/Controller.h" |
| 17 |
#include "games/controllers/ControllerLayout.h" |
| 18 |
#include "guilib/GUIListItem.h" |
| 19 |
#include "guilib/GUITexture.h" |
| 20 |
#include "utils/log.h" |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <mutex> |
| 24 |
#include <tuple> |
| 25 |
|
| 26 |
using namespace KODI; |
| 27 |
using namespace GAME; |
| 28 |
|
| 29 |
CGUIGameController::CGUIGameController(int parentID, |
| 30 |
int controlID, |
| 31 |
float posX, |
| 32 |
float posY, |
| 33 |
float width, |
| 34 |
float height, |
| 35 |
const CTextureInfo& texture) |
| 36 |
: CGUIImage(parentID, controlID, posX, posY, width, height, texture) |
| 37 |
{ |
| 38 |
// Initialize CGUIControl |
| 39 |
ControlType = GUICONTROL_GAMECONTROLLER; |
| 40 |
|
| 41 |
// Initialize CGUIImage |
| 42 |
SetAspectRatio(CAspectRatio::AR_KEEP); |
| 43 |
} |
| 44 |
|
| 45 |
CGUIGameController::CGUIGameController(const CGUIGameController& from) |
| 46 |
: CGUIImage(from), |
| 47 |
m_controllerIdInfo(from.m_controllerIdInfo), |
| 48 |
m_controllerAddressInfo(from.m_controllerAddressInfo), |
| 49 |
m_controllerDiffuse(from.m_controllerDiffuse), |
| 50 |
m_portAddressInfo(from.m_portAddressInfo), |
| 51 |
m_peripheralLocationInfo(from.m_peripheralLocationInfo), |
| 52 |
m_currentController(from.m_currentController), |
| 53 |
m_portAddress(from.m_portAddress), |
| 54 |
m_peripheralLocation(from.m_peripheralLocation) |
| 55 |
{ |
| 56 |
// Initialize CGUIControl |
| 57 |
ControlType = GUICONTROL_GAMECONTROLLER; |
| 58 |
|
| 59 |
// Initialize CGUIImage |
| 60 |
SetAspectRatio(CAspectRatio::AR_KEEP); |
| 61 |
} |
| 62 |
|
| 63 |
CGUIGameController* CGUIGameController::Clone(void) const |
| 64 |
{ |
| 65 |
return new CGUIGameController(*this); |
| 66 |
} |
| 67 |
|
| 68 |
void CGUIGameController::DoProcess(unsigned int currentTime, CDirtyRegionList& dirtyregions) |
| 69 |
{ |
| 70 |
std::string portAddress; |
| 71 |
std::string peripheralLocation; |
| 72 |
|
| 73 |
{ |
| 74 |
std::lock_guard<std::mutex> lock(m_mutex); |
| 75 |
portAddress = m_portAddress; |
| 76 |
peripheralLocation = m_peripheralLocation; |
| 77 |
} |
| 78 |
|
| 79 |
const GAME::CAgentInput& agentInput = CServiceBroker::GetGameServices().AgentInput(); |
| 80 |
|
| 81 |
// Highlight the controller if it is active |
| 82 |
float activation = 0.0f; |
| 83 |
|
| 84 |
if (!portAddress.empty()) |
| 85 |
activation = agentInput.GetGamePortActivation(portAddress); |
| 86 |
|
| 87 |
if (!peripheralLocation.empty()) |
| 88 |
activation = std::max(agentInput.GetPeripheralActivation(peripheralLocation), activation); |
| 89 |
|
| 90 |
SetActivation(activation); |
| 91 |
|
| 92 |
CGUIImage::DoProcess(currentTime, dirtyregions); |
| 93 |
} |
| 94 |
|
| 95 |
void CGUIGameController::UpdateInfo(const CGUIListItem* item /* = nullptr */) |
| 96 |
{ |
| 97 |
CGUIImage::UpdateInfo(item); |
| 98 |
|
| 99 |
m_controllerDiffuse.Update(item); |
| 100 |
|
| 101 |
if (item != nullptr) |
| 102 |
{ |
| 103 |
std::string controllerId; |
| 104 |
std::string portAddress; |
| 105 |
std::string peripheralLocation; |
| 106 |
|
| 107 |
if (item->HasProperty("Addon.ID")) |
| 108 |
controllerId = item->GetProperty("Addon.ID").asString(); |
| 109 |
|
| 110 |
if (controllerId.empty()) |
| 111 |
controllerId = m_controllerIdInfo.GetItemLabel(item); |
| 112 |
|
| 113 |
portAddress = m_portAddressInfo.GetItemLabel(item); |
| 114 |
peripheralLocation = m_peripheralLocationInfo.GetItemLabel(item); |
| 115 |
|
| 116 |
std::string controllerAddress = m_controllerAddressInfo.GetItemLabel(item); |
| 117 |
if (!controllerAddress.empty()) |
| 118 |
std::tie(portAddress, controllerId) = CGameClientTopology::SplitAddress(controllerAddress); |
| 119 |
|
| 120 |
std::lock_guard<std::mutex> lock(m_mutex); |
| 121 |
|
| 122 |
if (!controllerId.empty()) |
| 123 |
ActivateController(controllerId); |
| 124 |
if (!portAddress.empty()) |
| 125 |
m_portAddress = portAddress; |
| 126 |
if (!peripheralLocation.empty()) |
| 127 |
m_peripheralLocation = peripheralLocation; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void CGUIGameController::SetControllerID(const GUILIB::GUIINFO::CGUIInfoLabel& controllerId) |
| 132 |
{ |
| 133 |
m_controllerIdInfo = controllerId; |
| 134 |
|
| 135 |
// Check if a controller ID is available without a listitem |
| 136 |
static const CFileItem empty; |
| 137 |
const std::string strControllerId = m_controllerIdInfo.GetItemLabel(&empty); |
| 138 |
if (!strControllerId.empty()) |
| 139 |
{ |
| 140 |
std::lock_guard<std::mutex> lock(m_mutex); |
| 141 |
ActivateController(strControllerId); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
void CGUIGameController::SetControllerAddress( |
| 146 |
const GUILIB::GUIINFO::CGUIInfoLabel& controllerAddress) |
| 147 |
{ |
| 148 |
m_controllerAddressInfo = controllerAddress; |
| 149 |
|
| 150 |
// Check if a controller address is available without a listitem |
| 151 |
static const CFileItem empty; |
| 152 |
const std::string strControllerAddress = m_controllerAddressInfo.GetItemLabel(&empty); |
| 153 |
if (!strControllerAddress.empty()) |
| 154 |
{ |
| 155 |
std::string controllerId; |
| 156 |
std::string portAddress; |
| 157 |
std::tie(portAddress, controllerId) = CGameClientTopology::SplitAddress(strControllerAddress); |
| 158 |
|
| 159 |
std::lock_guard<std::mutex> lock(m_mutex); |
| 160 |
ActivateController(controllerId); |
| 161 |
m_portAddress = portAddress; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
void CGUIGameController::SetControllerDiffuse(const GUILIB::GUIINFO::CGUIInfoColor& color) |
| 166 |
{ |
| 167 |
m_controllerDiffuse = color; |
| 168 |
} |
| 169 |
|
| 170 |
void CGUIGameController::SetPortAddress(const GUILIB::GUIINFO::CGUIInfoLabel& portAddress) |
| 171 |
{ |
| 172 |
m_portAddressInfo = portAddress; |
| 173 |
|
| 174 |
// Check if a port address is available without a listitem |
| 175 |
static const CFileItem empty; |
| 176 |
const std::string strPortAddress = m_portAddressInfo.GetItemLabel(&empty); |
| 177 |
if (!strPortAddress.empty()) |
| 178 |
{ |
| 179 |
std::lock_guard<std::mutex> lock(m_mutex); |
| 180 |
m_portAddress = strPortAddress; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
void CGUIGameController::SetPeripheralLocation( |
| 185 |
const GUILIB::GUIINFO::CGUIInfoLabel& peripheralLocation) |
| 186 |
{ |
| 187 |
m_peripheralLocationInfo = peripheralLocation; |
| 188 |
|
| 189 |
// Check if a port address is available without a listitem |
| 190 |
static const CFileItem empty; |
| 191 |
const std::string strPeripheralLocation = m_peripheralLocationInfo.GetItemLabel(&empty); |
| 192 |
if (!strPeripheralLocation.empty()) |
| 193 |
{ |
| 194 |
std::lock_guard<std::mutex> lock(m_mutex); |
| 195 |
m_peripheralLocation = strPeripheralLocation; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
void CGUIGameController::ActivateController(const std::string& controllerId) |
| 200 |
{ |
| 201 |
CGameServices& gameServices = CServiceBroker::GetGameServices(); |
| 202 |
|
| 203 |
ControllerPtr controller = gameServices.GetController(controllerId); |
| 204 |
|
| 205 |
ActivateController(controller); |
| 206 |
} |
| 207 |
|
| 208 |
void CGUIGameController::ActivateController(const ControllerPtr& controller) |
| 209 |
{ |
| 210 |
m_currentController = controller; |
| 211 |
|
| 212 |
if (m_currentController) |
| 213 |
SetFileName(m_currentController->Layout().ImagePath()); |
| 214 |
else |
| 215 |
SetFileName(""); |
| 216 |
} |
| 217 |
|
| 218 |
std::string CGUIGameController::GetPortAddress() |
| 219 |
{ |
| 220 |
std::lock_guard<std::mutex> lock(m_mutex); |
| 221 |
return m_portAddress; |
| 222 |
} |
| 223 |
|
| 224 |
std::string CGUIGameController::GetPeripheralLocation() |
| 225 |
{ |
| 226 |
std::lock_guard<std::mutex> lock(m_mutex); |
| 227 |
return m_peripheralLocation; |
| 228 |
} |
| 229 |
|
| 230 |
void CGUIGameController::SetActivation(float activation) |
| 231 |
{ |
| 232 |
// Validate parameters |
| 233 |
if (activation < 0.0f) |
| 234 |
activation = 0.0f; |
| 235 |
if (activation > 1.0f) |
| 236 |
activation = 1.0f; |
| 237 |
|
| 238 |
// Get diffuse color parts |
| 239 |
const uint8_t alpha = (m_controllerDiffuse >> 24) & 0xff; |
| 240 |
const uint8_t red = (m_controllerDiffuse >> 16) & 0xff; |
| 241 |
const uint8_t green = (m_controllerDiffuse >> 8) & 0xff; |
| 242 |
const uint8_t blue = m_controllerDiffuse & 0xff; |
| 243 |
|
| 244 |
// Merge the diffuse color with white as a portion of the activation |
| 245 |
const uint8_t newAlpha = static_cast<uint8_t>(0xff - (0xff - alpha) * activation); |
| 246 |
const uint8_t newRed = static_cast<uint8_t>(0xff - (0xff - red) * activation); |
| 247 |
const uint8_t newGreen = static_cast<uint8_t>(0xff - (0xff - green) * activation); |
| 248 |
const uint8_t newBlue = static_cast<uint8_t>(0xff - (0xff - blue) * activation); |
| 249 |
|
| 250 |
const UTILS::COLOR::Color activationColor = |
| 251 |
(newAlpha << 24) | (newRed << 16) | (newGreen << 8) | newBlue; |
| 252 |
|
| 253 |
if (CGUIImage::SetColorDiffuse(activationColor)) |
| 254 |
CGUIImage::UpdateDiffuseColor(nullptr); |
| 255 |
} |