| 1 |
/* |
| 2 |
* Copyright (C) 2014-2024 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 "PeripheralBusAddon.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "addons/AddonEvents.h" |
| 13 |
#include "addons/AddonManager.h" |
| 14 |
#include "addons/addoninfo/AddonInfo.h" |
| 15 |
#include "addons/addoninfo/AddonType.h" |
| 16 |
#include "messaging/helpers/DialogHelper.h" |
| 17 |
#include "peripherals/Peripherals.h" |
| 18 |
#include "peripherals/addons/PeripheralAddon.h" |
| 19 |
#include "peripherals/devices/PeripheralJoystick.h" |
| 20 |
#include "threads/SingleLock.h" |
| 21 |
#include "utils/log.h" |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <memory> |
| 25 |
#include <mutex> |
| 26 |
|
| 27 |
using namespace KODI; |
| 28 |
using namespace PERIPHERALS; |
| 29 |
|
| 30 |
CPeripheralBusAddon::CPeripheralBusAddon(CPeripherals& manager) |
| 31 |
: CPeripheralBus("PeripBusAddon", manager, PERIPHERAL_BUS_ADDON) |
| 32 |
{ |
| 33 |
using namespace ADDON; |
| 34 |
|
| 35 |
CServiceBroker::GetAddonMgr().Events().Subscribe(this, &CPeripheralBusAddon::OnEvent); |
| 36 |
|
| 37 |
UpdateAddons(); |
| 38 |
} |
| 39 |
|
| 40 |
CPeripheralBusAddon::~CPeripheralBusAddon() |
| 41 |
{ |
| 42 |
using namespace ADDON; |
| 43 |
|
| 44 |
CServiceBroker::GetAddonMgr().Events().Unsubscribe(this); |
| 45 |
|
| 46 |
// stop everything before destroying any (loaded) addons |
| 47 |
Clear(); |
| 48 |
|
| 49 |
// destroy any (loaded) addons |
| 50 |
for (const auto& addon : m_addons) |
| 51 |
addon->DestroyAddon(); |
| 52 |
|
| 53 |
m_failedAddons.clear(); |
| 54 |
m_addons.clear(); |
| 55 |
} |
| 56 |
|
| 57 |
bool CPeripheralBusAddon::GetAddonWithButtonMap(PeripheralAddonPtr& addon) const |
| 58 |
{ |
| 59 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 60 |
|
| 61 |
auto it = std::find_if(m_addons.begin(), m_addons.end(), |
| 62 |
[](const PeripheralAddonPtr& addon) { return addon->HasButtonMaps(); }); |
| 63 |
|
| 64 |
if (it != m_addons.end()) |
| 65 |
{ |
| 66 |
addon = *it; |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
bool CPeripheralBusAddon::GetAddonWithButtonMap(const CPeripheral* device, |
| 74 |
PeripheralAddonPtr& addon) const |
| 75 |
{ |
| 76 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 77 |
|
| 78 |
// If device is from an add-on, try to use that add-on |
| 79 |
if (device && device->GetBusType() == PERIPHERAL_BUS_ADDON) |
| 80 |
{ |
| 81 |
PeripheralAddonPtr addonWithButtonMap; |
| 82 |
unsigned int index; |
| 83 |
if (SplitLocation(device->Location(), addonWithButtonMap, index)) |
| 84 |
{ |
| 85 |
if (addonWithButtonMap->HasButtonMaps()) |
| 86 |
addon = std::move(addonWithButtonMap); |
| 87 |
else |
| 88 |
CLog::Log(LOGDEBUG, "Add-on {} doesn't provide button maps for its controllers", |
| 89 |
addonWithButtonMap->ID()); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if (!addon) |
| 94 |
{ |
| 95 |
auto it = std::find_if(m_addons.begin(), m_addons.end(), |
| 96 |
[](const PeripheralAddonPtr& addon) { return addon->HasButtonMaps(); }); |
| 97 |
|
| 98 |
if (it != m_addons.end()) |
| 99 |
addon = *it; |
| 100 |
} |
| 101 |
|
| 102 |
return addon.get() != nullptr; |
| 103 |
} |
| 104 |
|
| 105 |
bool CPeripheralBusAddon::PerformDeviceScan(PeripheralScanResults& results) |
| 106 |
{ |
| 107 |
PeripheralAddonVector addons; |
| 108 |
{ |
| 109 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 110 |
addons = m_addons; |
| 111 |
} |
| 112 |
|
| 113 |
for (const auto& addon : addons) |
| 114 |
addon->PerformDeviceScan(results); |
| 115 |
|
| 116 |
// Scan during bus initialization must return true or bus gets deleted |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
bool CPeripheralBusAddon::InitializeProperties(CPeripheral& peripheral) |
| 121 |
{ |
| 122 |
if (!CPeripheralBus::InitializeProperties(peripheral)) |
| 123 |
return false; |
| 124 |
|
| 125 |
bool bSuccess = false; |
| 126 |
|
| 127 |
PeripheralAddonPtr addon; |
| 128 |
unsigned int index; |
| 129 |
|
| 130 |
if (SplitLocation(peripheral.Location(), addon, index)) |
| 131 |
{ |
| 132 |
switch (peripheral.Type()) |
| 133 |
{ |
| 134 |
case PERIPHERAL_JOYSTICK: |
| 135 |
bSuccess = |
| 136 |
addon->GetJoystickProperties(index, static_cast<CPeripheralJoystick&>(peripheral)); |
| 137 |
break; |
| 138 |
|
| 139 |
default: |
| 140 |
break; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return bSuccess; |
| 145 |
} |
| 146 |
|
| 147 |
bool CPeripheralBusAddon::SendRumbleEvent(const std::string& strLocation, |
| 148 |
unsigned int motorIndex, |
| 149 |
float magnitude) |
| 150 |
{ |
| 151 |
bool bHandled = false; |
| 152 |
|
| 153 |
PeripheralAddonPtr addon; |
| 154 |
unsigned int peripheralIndex; |
| 155 |
if (SplitLocation(strLocation, addon, peripheralIndex)) |
| 156 |
bHandled = addon->SendRumbleEvent(peripheralIndex, motorIndex, magnitude); |
| 157 |
|
| 158 |
return bHandled; |
| 159 |
} |
| 160 |
|
| 161 |
void CPeripheralBusAddon::ProcessEvents(void) |
| 162 |
{ |
| 163 |
PeripheralAddonVector addons; |
| 164 |
|
| 165 |
{ |
| 166 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 167 |
addons = m_addons; |
| 168 |
} |
| 169 |
|
| 170 |
for (const auto& addon : addons) |
| 171 |
addon->ProcessEvents(); |
| 172 |
} |
| 173 |
|
| 174 |
void CPeripheralBusAddon::EnableButtonMapping() |
| 175 |
{ |
| 176 |
using namespace ADDON; |
| 177 |
|
| 178 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 179 |
|
| 180 |
PeripheralAddonPtr dummy; |
| 181 |
|
| 182 |
if (!GetAddonWithButtonMap(dummy)) |
| 183 |
{ |
| 184 |
std::vector<AddonInfoPtr> disabledAddons; |
| 185 |
CServiceBroker::GetAddonMgr().GetDisabledAddonInfos(disabledAddons, AddonType::PERIPHERALDLL); |
| 186 |
if (!disabledAddons.empty()) |
| 187 |
PromptEnableAddons(disabledAddons); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void CPeripheralBusAddon::PowerOff(const std::string& strLocation) |
| 192 |
{ |
| 193 |
PeripheralAddonPtr addon; |
| 194 |
unsigned int peripheralIndex; |
| 195 |
if (SplitLocation(strLocation, addon, peripheralIndex)) |
| 196 |
addon->PowerOffJoystick(peripheralIndex); |
| 197 |
} |
| 198 |
|
| 199 |
void CPeripheralBusAddon::UnregisterRemovedDevices(const PeripheralScanResults& results) |
| 200 |
{ |
| 201 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 202 |
|
| 203 |
PeripheralVector removedPeripherals; |
| 204 |
|
| 205 |
for (const auto& addon : m_addons) |
| 206 |
addon->UnregisterRemovedDevices(results, removedPeripherals); |
| 207 |
|
| 208 |
for (const auto& peripheral : removedPeripherals) |
| 209 |
m_manager.OnDeviceDeleted(*this, *peripheral); |
| 210 |
} |
| 211 |
|
| 212 |
void CPeripheralBusAddon::Register(const PeripheralPtr& peripheral) |
| 213 |
{ |
| 214 |
if (!peripheral) |
| 215 |
return; |
| 216 |
|
| 217 |
PeripheralAddonPtr addon; |
| 218 |
unsigned int peripheralIndex; |
| 219 |
|
| 220 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 221 |
|
| 222 |
if (SplitLocation(peripheral->Location(), addon, peripheralIndex)) |
| 223 |
{ |
| 224 |
if (addon->Register(peripheralIndex, peripheral)) |
| 225 |
m_manager.OnDeviceAdded(*this, *peripheral); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
void CPeripheralBusAddon::GetFeatures(std::vector<PeripheralFeature>& features) const |
| 230 |
{ |
| 231 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 232 |
for (const auto& addon : m_addons) |
| 233 |
addon->GetFeatures(features); |
| 234 |
} |
| 235 |
|
| 236 |
bool CPeripheralBusAddon::HasFeature(const PeripheralFeature feature) const |
| 237 |
{ |
| 238 |
bool bReturn(false); |
| 239 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 240 |
for (const auto& addon : m_addons) |
| 241 |
bReturn = bReturn || addon->HasFeature(feature); |
| 242 |
return bReturn; |
| 243 |
} |
| 244 |
|
| 245 |
PeripheralPtr CPeripheralBusAddon::GetPeripheral(const std::string& strLocation) const |
| 246 |
{ |
| 247 |
PeripheralPtr peripheral; |
| 248 |
PeripheralAddonPtr addon; |
| 249 |
unsigned int peripheralIndex; |
| 250 |
|
| 251 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 252 |
|
| 253 |
if (SplitLocation(strLocation, addon, peripheralIndex)) |
| 254 |
peripheral = addon->GetPeripheral(peripheralIndex); |
| 255 |
|
| 256 |
return peripheral; |
| 257 |
} |
| 258 |
|
| 259 |
PeripheralPtr CPeripheralBusAddon::GetByPath(const std::string& strPath) const |
| 260 |
{ |
| 261 |
PeripheralPtr result; |
| 262 |
|
| 263 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 264 |
|
| 265 |
for (const auto& addon : m_addons) |
| 266 |
{ |
| 267 |
PeripheralPtr peripheral = addon->GetByPath(strPath); |
| 268 |
if (peripheral) |
| 269 |
{ |
| 270 |
result = peripheral; |
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
return result; |
| 276 |
} |
| 277 |
|
| 278 |
bool CPeripheralBusAddon::SupportsFeature(PeripheralFeature feature) const |
| 279 |
{ |
| 280 |
bool bSupportsFeature = false; |
| 281 |
|
| 282 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 283 |
for (const auto& addon : m_addons) |
| 284 |
bSupportsFeature |= addon->SupportsFeature(feature); |
| 285 |
|
| 286 |
return bSupportsFeature; |
| 287 |
} |
| 288 |
|
| 289 |
unsigned int CPeripheralBusAddon::GetPeripheralsWithFeature(PeripheralVector& results, |
| 290 |
const PeripheralFeature feature) const |
| 291 |
{ |
| 292 |
unsigned int iReturn = 0; |
| 293 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 294 |
for (const auto& addon : m_addons) |
| 295 |
iReturn += addon->GetPeripheralsWithFeature(results, feature); |
| 296 |
return iReturn; |
| 297 |
} |
| 298 |
|
| 299 |
unsigned int CPeripheralBusAddon::GetNumberOfPeripherals(void) const |
| 300 |
{ |
| 301 |
unsigned int iReturn = 0; |
| 302 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 303 |
for (const auto& addon : m_addons) |
| 304 |
iReturn += addon->GetNumberOfPeripherals(); |
| 305 |
return iReturn; |
| 306 |
} |
| 307 |
|
| 308 |
unsigned int CPeripheralBusAddon::GetNumberOfPeripheralsWithId(const int iVendorId, |
| 309 |
const int iProductId) const |
| 310 |
{ |
| 311 |
unsigned int iReturn = 0; |
| 312 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 313 |
for (const auto& addon : m_addons) |
| 314 |
iReturn += addon->GetNumberOfPeripheralsWithId(iVendorId, iProductId); |
| 315 |
return iReturn; |
| 316 |
} |
| 317 |
|
| 318 |
void CPeripheralBusAddon::GetDirectory(const std::string& strPath, CFileItemList& items) const |
| 319 |
{ |
| 320 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 321 |
for (const auto& addon : m_addons) |
| 322 |
addon->GetDirectory(strPath, items); |
| 323 |
} |
| 324 |
|
| 325 |
void CPeripheralBusAddon::OnEvent(const ADDON::AddonEvent& event) |
| 326 |
{ |
| 327 |
if (typeid(event) == typeid(ADDON::AddonEvents::Enabled) || |
| 328 |
typeid(event) == typeid(ADDON::AddonEvents::ReInstalled)) |
| 329 |
{ |
| 330 |
if (CServiceBroker::GetAddonMgr().HasType(event.addonId, ADDON::AddonType::PERIPHERALDLL)) |
| 331 |
UpdateAddons(); |
| 332 |
} |
| 333 |
else if (typeid(event) == typeid(ADDON::AddonEvents::Disabled)) |
| 334 |
{ |
| 335 |
if (CServiceBroker::GetAddonMgr().HasType(event.addonId, ADDON::AddonType::PERIPHERALDLL)) |
| 336 |
UnRegisterAddon(event.addonId); |
| 337 |
} |
| 338 |
else if (typeid(event) == typeid(ADDON::AddonEvents::UnInstalled)) |
| 339 |
{ |
| 340 |
UnRegisterAddon(event.addonId); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
bool CPeripheralBusAddon::SplitLocation(const std::string& strLocation, |
| 345 |
PeripheralAddonPtr& addon, |
| 346 |
unsigned int& peripheralIndex) const |
| 347 |
{ |
| 348 |
std::vector<std::string> parts = StringUtils::Split(strLocation, "/"); |
| 349 |
if (parts.size() == 2) |
| 350 |
{ |
| 351 |
addon.reset(); |
| 352 |
|
| 353 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 354 |
|
| 355 |
const std::string& strAddonId = parts[0]; |
| 356 |
for (const auto& addonIt : m_addons) |
| 357 |
{ |
| 358 |
if (addonIt->ID() == strAddonId) |
| 359 |
{ |
| 360 |
addon = addonIt; |
| 361 |
break; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
if (addon) |
| 366 |
{ |
| 367 |
const char* strJoystickIndex = parts[1].c_str(); |
| 368 |
char* p = NULL; |
| 369 |
peripheralIndex = strtol(strJoystickIndex, &p, 10); |
| 370 |
if (strJoystickIndex != p) |
| 371 |
return true; |
| 372 |
} |
| 373 |
} |
| 374 |
return false; |
| 375 |
} |
| 376 |
|
| 377 |
void CPeripheralBusAddon::UpdateAddons(void) |
| 378 |
{ |
| 379 |
using namespace ADDON; |
| 380 |
|
| 381 |
auto GetPeripheralAddonID = [](const PeripheralAddonPtr& addon) { return addon->ID(); }; |
| 382 |
auto GetAddonID = [](const AddonInfoPtr& addon) { return addon->ID(); }; |
| 383 |
|
| 384 |
std::set<std::string> currentIds; |
| 385 |
std::set<std::string> newIds; |
| 386 |
|
| 387 |
std::set<std::string> added; |
| 388 |
std::set<std::string> removed; |
| 389 |
|
| 390 |
// Get new add-ons |
| 391 |
std::vector<AddonInfoPtr> newAddons; |
| 392 |
CServiceBroker::GetAddonMgr().GetAddonInfos(newAddons, true, AddonType::PERIPHERALDLL); |
| 393 |
std::transform(newAddons.begin(), newAddons.end(), std::inserter(newIds, newIds.end()), |
| 394 |
GetAddonID); |
| 395 |
|
| 396 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 397 |
|
| 398 |
// Get current add-ons |
| 399 |
std::transform(m_addons.begin(), m_addons.end(), std::inserter(currentIds, currentIds.end()), |
| 400 |
GetPeripheralAddonID); |
| 401 |
std::transform(m_failedAddons.begin(), m_failedAddons.end(), |
| 402 |
std::inserter(currentIds, currentIds.end()), GetPeripheralAddonID); |
| 403 |
|
| 404 |
// Differences |
| 405 |
std::set_difference(newIds.begin(), newIds.end(), currentIds.begin(), currentIds.end(), |
| 406 |
std::inserter(added, added.end())); |
| 407 |
std::set_difference(currentIds.begin(), currentIds.end(), newIds.begin(), newIds.end(), |
| 408 |
std::inserter(removed, removed.end())); |
| 409 |
|
| 410 |
// Register new add-ons |
| 411 |
for (const std::string& addonId : added) |
| 412 |
{ |
| 413 |
CLog::Log(LOGDEBUG, "Add-on bus: Registering add-on {}", addonId); |
| 414 |
|
| 415 |
auto GetAddon = [&addonId](const AddonInfoPtr& addon) { return addon->ID() == addonId; }; |
| 416 |
|
| 417 |
auto it = std::find_if(newAddons.begin(), newAddons.end(), GetAddon); |
| 418 |
if (it != newAddons.end()) |
| 419 |
{ |
| 420 |
PeripheralAddonPtr newAddon = std::make_shared<CPeripheralAddon>(*it, m_manager); |
| 421 |
if (newAddon) |
| 422 |
{ |
| 423 |
bool bCreated; |
| 424 |
|
| 425 |
{ |
| 426 |
CSingleExit exit(m_critSection); |
| 427 |
bCreated = newAddon->CreateAddon(); |
| 428 |
} |
| 429 |
|
| 430 |
if (bCreated) |
| 431 |
m_addons.emplace_back(std::move(newAddon)); |
| 432 |
else |
| 433 |
m_failedAddons.emplace_back(std::move(newAddon)); |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
// Destroy removed add-ons |
| 439 |
for (const std::string& addonId : removed) |
| 440 |
{ |
| 441 |
UnRegisterAddon(addonId); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
void CPeripheralBusAddon::UnRegisterAddon(const std::string& addonId) |
| 446 |
{ |
| 447 |
PeripheralAddonPtr erased; |
| 448 |
auto ErasePeripheralAddon = [&addonId, &erased](const PeripheralAddonPtr& addon) |
| 449 |
{ |
| 450 |
if (addon->ID() == addonId) |
| 451 |
{ |
| 452 |
erased = addon; |
| 453 |
return true; |
| 454 |
} |
| 455 |
return false; |
| 456 |
}; |
| 457 |
|
| 458 |
m_addons.erase(std::remove_if(m_addons.begin(), m_addons.end(), ErasePeripheralAddon), |
| 459 |
m_addons.end()); |
| 460 |
if (!erased) |
| 461 |
m_failedAddons.erase( |
| 462 |
std::remove_if(m_failedAddons.begin(), m_failedAddons.end(), ErasePeripheralAddon), |
| 463 |
m_failedAddons.end()); |
| 464 |
|
| 465 |
if (erased) |
| 466 |
{ |
| 467 |
CLog::Log(LOGDEBUG, "Add-on bus: Unregistered add-on {}", addonId); |
| 468 |
CSingleExit exit(m_critSection); |
| 469 |
erased->DestroyAddon(); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
void CPeripheralBusAddon::PromptEnableAddons( |
| 474 |
const std::vector<std::shared_ptr<ADDON::CAddonInfo>>& disabledAddons) |
| 475 |
{ |
| 476 |
using namespace ADDON; |
| 477 |
using namespace MESSAGING::HELPERS; |
| 478 |
|
| 479 |
// True if the user confirms enabling the disabled peripheral add-on |
| 480 |
bool bAccepted = false; |
| 481 |
|
| 482 |
auto itAddon = std::find_if(disabledAddons.begin(), disabledAddons.end(), |
| 483 |
[](const AddonInfoPtr& addonInfo) |
| 484 |
{ return CPeripheralAddon::ProvidesJoysticks(addonInfo); }); |
| 485 |
|
| 486 |
if (itAddon != disabledAddons.end()) |
| 487 |
{ |
| 488 |
// "Unable to configure controllers" |
| 489 |
// "Controller configuration depends on a disabled add-on. Would you like to enable it?" |
| 490 |
bAccepted = |
| 491 |
(ShowYesNoDialogLines(CVariant{35017}, CVariant{35018}) == DialogResponse::CHOICE_YES); |
| 492 |
} |
| 493 |
|
| 494 |
if (bAccepted) |
| 495 |
{ |
| 496 |
for (const auto& addonInfo : disabledAddons) |
| 497 |
{ |
| 498 |
if (CPeripheralAddon::ProvidesJoysticks(addonInfo)) |
| 499 |
CServiceBroker::GetAddonMgr().EnableAddon(addonInfo->ID()); |
| 500 |
} |
| 501 |
} |
| 502 |
} |