Debian Codegraph @

xbmc/peripherals/devices/Peripheral.cpp

raw
1 /*
2 * Copyright (C) 2005-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 "Peripheral.h"
10
11 #include "ServiceBroker.h"
12 #include "Util.h"
13 #include "XBDateTime.h"
14 #include "games/controllers/Controller.h"
15 #include "games/controllers/ControllerLayout.h"
16 #include "games/controllers/ControllerManager.h"
17 #include "guilib/LocalizeStrings.h"
18 #include "input/joysticks/interfaces/IInputHandler.h"
19 #include "input/keyboard/generic/DefaultKeyboardHandling.h"
20 #include "input/mouse/generic/DefaultMouseHandling.h"
21 #include "peripherals/Peripherals.h"
22 #include "peripherals/addons/AddonButtonMapping.h"
23 #include "peripherals/addons/AddonInputHandling.h"
24 #include "peripherals/addons/PeripheralAddon.h"
25 #include "peripherals/bus/PeripheralBus.h"
26 #include "peripherals/bus/virtual/PeripheralBusAddon.h"
27 #include "settings/SettingAddon.h"
28 #include "settings/lib/Setting.h"
29 #include "utils/FileUtils.h"
30 #include "utils/StringUtils.h"
31 #include "utils/XBMCTinyXML2.h"
32 #include "utils/XMLUtils.h"
33 #include "utils/log.h"
34
35 #include <utility>
36
37 using namespace KODI;
38 using namespace JOYSTICK;
39 using namespace PERIPHERALS;
40
41 namespace
42 {
43 // Settings for peripherals
44 constexpr std::string_view SETTING_APPEARANCE = "appearance";
45 constexpr std::string_view SETTING_LAST_ACTIVE = "last_active";
46
47 struct SortBySettingsOrder
48 {
49 bool operator()(const PeripheralDeviceSetting& left, const PeripheralDeviceSetting& right)
50 {
51 return left.m_order < right.m_order;
52 }
53 };
54 } // namespace
55
56 CPeripheral::CPeripheral(CPeripherals& manager,
57 const PeripheralScanResult& scanResult,
58 CPeripheralBus* bus)
59 : m_manager(manager),
60 m_type(scanResult.m_mappedType),
61 m_busType(scanResult.m_busType),
62 m_mappedBusType(scanResult.m_mappedBusType),
63 m_strLocation(scanResult.m_strLocation),
64 m_strDeviceName(scanResult.m_strDeviceName),
65 m_iVendorId(scanResult.m_iVendorId),
66 m_iProductId(scanResult.m_iProductId),
67 m_bus(bus)
68 {
69 PeripheralTypeTranslator::FormatHexString(scanResult.m_iVendorId, m_strVendorId);
70 PeripheralTypeTranslator::FormatHexString(scanResult.m_iProductId, m_strProductId);
71 if (scanResult.m_iSequence > 0)
72 {
73 m_strFileLocation =
74 StringUtils::Format("peripherals://{}/{}_{}.dev",
75 PeripheralTypeTranslator::BusTypeToString(scanResult.m_busType),
76 scanResult.m_strLocation, scanResult.m_iSequence);
77 }
78 else
79 {
80 m_strFileLocation = StringUtils::Format(
81 "peripherals://{}/{}.dev", PeripheralTypeTranslator::BusTypeToString(scanResult.m_busType),
82 scanResult.m_strLocation);
83 }
84 }
85
86 CPeripheral::~CPeripheral(void)
87 {
88 PersistSettings(true);
89
90 m_subDevices.clear();
91
92 ClearSettings();
93 }
94
95 bool CPeripheral::operator==(const CPeripheral& right) const
96 {
97 return m_type == right.m_type && m_strLocation == right.m_strLocation &&
98 m_iVendorId == right.m_iVendorId && m_iProductId == right.m_iProductId;
99 }
100
101 bool CPeripheral::operator!=(const CPeripheral& right) const
102 {
103 return !(*this == right);
104 }
105
106 bool CPeripheral::HasFeature(const PeripheralFeature feature) const
107 {
108 bool bReturn(false);
109
110 for (unsigned int iFeaturePtr = 0; iFeaturePtr < m_features.size(); iFeaturePtr++)
111 {
112 if (m_features.at(iFeaturePtr) == feature)
113 {
114 bReturn = true;
115 break;
116 }
117 }
118
119 if (!bReturn)
120 {
121 for (unsigned int iSubdevicePtr = 0; iSubdevicePtr < m_subDevices.size(); iSubdevicePtr++)
122 {
123 if (m_subDevices.at(iSubdevicePtr)->HasFeature(feature))
124 {
125 bReturn = true;
126 break;
127 }
128 }
129 }
130
131 return bReturn;
132 }
133
134 void CPeripheral::GetFeatures(std::vector<PeripheralFeature>& features) const
135 {
136 for (unsigned int iFeaturePtr = 0; iFeaturePtr < m_features.size(); iFeaturePtr++)
137 features.push_back(m_features.at(iFeaturePtr));
138
139 for (unsigned int iSubdevicePtr = 0; iSubdevicePtr < m_subDevices.size(); iSubdevicePtr++)
140 m_subDevices.at(iSubdevicePtr)->GetFeatures(features);
141 }
142
143 bool CPeripheral::Initialise(void)
144 {
145 bool bReturn(false);
146
147 if (m_bError)
148 return bReturn;
149
150 bReturn = true;
151 if (m_bInitialised)
152 return bReturn;
153
154 m_manager.GetSettingsFromMapping(*this);
155
156 std::string safeDeviceName = m_strDeviceName;
157 StringUtils::Replace(safeDeviceName, ' ', '_');
158
159 if (m_iVendorId == 0x0000 && m_iProductId == 0x0000)
160 {
161 m_strSettingsFile = StringUtils::Format(
162 "special://profile/peripheral_data/{}_{}.xml",
163 PeripheralTypeTranslator::BusTypeToString(m_mappedBusType),
164 CUtil::MakeLegalFileName(std::move(safeDeviceName), LEGAL_WIN32_COMPAT));
165 }
166 else
167 {
168 // Backwards compatibility - old settings files didn't include the device name
169 m_strSettingsFile = StringUtils::Format(
170 "special://profile/peripheral_data/{}_{}_{}.xml",
171 PeripheralTypeTranslator::BusTypeToString(m_mappedBusType), m_strVendorId, m_strProductId);
172
173 if (!CFileUtils::Exists(m_strSettingsFile))
174 m_strSettingsFile = StringUtils::Format(
175 "special://profile/peripheral_data/{}_{}_{}_{}.xml",
176 PeripheralTypeTranslator::BusTypeToString(m_mappedBusType), m_strVendorId, m_strProductId,
177 CUtil::MakeLegalFileName(std::move(safeDeviceName), LEGAL_WIN32_COMPAT));
178 }
179
180 // Load settings and initialize state
181 LoadPersistedSettings();
182
183 // Initialize features
184 for (unsigned int iFeaturePtr = 0; iFeaturePtr < m_features.size(); iFeaturePtr++)
185 {
186 PeripheralFeature feature = m_features.at(iFeaturePtr);
187 bReturn &= InitialiseFeature(feature);
188 }
189
190 for (unsigned int iSubdevicePtr = 0; iSubdevicePtr < m_subDevices.size(); iSubdevicePtr++)
191 bReturn &= m_subDevices.at(iSubdevicePtr)->Initialise();
192
193 if (bReturn)
194 {
195 CLog::Log(LOGDEBUG, "{} - initialised peripheral on '{}' with {} features and {} sub devices",
196 __FUNCTION__, m_strLocation, (int)m_features.size(), (int)m_subDevices.size());
197 m_bInitialised = true;
198 }
199
200 return bReturn;
201 }
202
203 void CPeripheral::GetSubdevices(PeripheralVector& subDevices) const
204 {
205 subDevices = m_subDevices;
206 }
207
208 bool CPeripheral::IsMultiFunctional(void) const
209 {
210 return m_subDevices.size() > 0;
211 }
212
213 std::vector<std::shared_ptr<CSetting>> CPeripheral::GetSettings(void) const
214 {
215 std::vector<PeripheralDeviceSetting> tmpSettings;
216 tmpSettings.reserve(m_settings.size());
217 for (const auto& it : m_settings)
218 tmpSettings.push_back(it.second);
219 sort(tmpSettings.begin(), tmpSettings.end(), SortBySettingsOrder());
220
221 std::vector<std::shared_ptr<CSetting>> settings;
222 settings.reserve(tmpSettings.size());
223 for (const auto& it : tmpSettings)
224 settings.push_back(it.m_setting);
225 return settings;
226 }
227
228 void CPeripheral::AddSetting(const std::string& strKey, const SettingConstPtr& setting, int order)
229 {
230 if (!setting)
231 {
232 CLog::Log(LOGERROR, "{} - invalid setting", __FUNCTION__);
233 return;
234 }
235
236 if (!HasSetting(strKey))
237 {
238 PeripheralDeviceSetting deviceSetting = {NULL, order};
239 switch (setting->GetType())
240 {
241 case SettingType::Boolean:
242 {
243 std::shared_ptr<const CSettingBool> mappedSetting =
244 std::static_pointer_cast<const CSettingBool>(setting);
245 std::shared_ptr<CSettingBool> boolSetting =
246 std::make_shared<CSettingBool>(strKey, *mappedSetting);
247 if (boolSetting)
248 {
249 boolSetting->SetVisible(mappedSetting->IsVisible());
250 deviceSetting.m_setting = boolSetting;
251 }
252 }
253 break;
254 case SettingType::Integer:
255 {
256 std::shared_ptr<const CSettingInt> mappedSetting =
257 std::static_pointer_cast<const CSettingInt>(setting);
258 std::shared_ptr<CSettingInt> intSetting =
259 std::make_shared<CSettingInt>(strKey, *mappedSetting);
260 if (intSetting)
261 {
262 intSetting->SetVisible(mappedSetting->IsVisible());
263 deviceSetting.m_setting = intSetting;
264 }
265 }
266 break;
267 case SettingType::Number:
268 {
269 std::shared_ptr<const CSettingNumber> mappedSetting =
270 std::static_pointer_cast<const CSettingNumber>(setting);
271 std::shared_ptr<CSettingNumber> floatSetting =
272 std::make_shared<CSettingNumber>(strKey, *mappedSetting);
273 if (floatSetting)
274 {
275 floatSetting->SetVisible(mappedSetting->IsVisible());
276 deviceSetting.m_setting = floatSetting;
277 }
278 }
279 break;
280 case SettingType::String:
281 {
282 if (std::dynamic_pointer_cast<const CSettingAddon>(setting))
283 {
284 std::shared_ptr<const CSettingAddon> mappedSetting =
285 std::static_pointer_cast<const CSettingAddon>(setting);
286 std::shared_ptr<CSettingAddon> addonSetting =
287 std::make_shared<CSettingAddon>(strKey, *mappedSetting);
288 addonSetting->SetVisible(mappedSetting->IsVisible());
289 deviceSetting.m_setting = addonSetting;
290
291 // Handle default settings
292 if (strKey == SETTING_APPEARANCE)
293 {
294 const std::string& controllerId = addonSetting->GetValue();
295 if (!controllerId.empty())
296 {
297 GAME::ControllerPtr controllerProfile =
298 CServiceBroker::GetGameControllerManager().GetController(controllerId);
299 if (controllerProfile)
300 SetControllerProfile(controllerProfile);
301 }
302 }
303 }
304 else
305 {
306 std::shared_ptr<const CSettingString> mappedSetting =
307 std::static_pointer_cast<const CSettingString>(setting);
308 std::shared_ptr<CSettingString> stringSetting =
309 std::make_shared<CSettingString>(strKey, *mappedSetting);
310 stringSetting->SetVisible(mappedSetting->IsVisible());
311 deviceSetting.m_setting = stringSetting;
312 }
313 }
314 break;
315 default:
316 //! @todo add more types if needed
317 break;
318 }
319
320 if (deviceSetting.m_setting != NULL)
321 m_settings.insert(make_pair(strKey, deviceSetting));
322 }
323 }
324
325 bool CPeripheral::HasSetting(const std::string& strKey) const
326 {
327 std::map<std::string, PeripheralDeviceSetting>::const_iterator it = m_settings.find(strKey);
328 return it != m_settings.end();
329 }
330
331 bool CPeripheral::HasSettings(void) const
332 {
333 return !m_settings.empty();
334 }
335
336 bool CPeripheral::HasConfigurableSettings(void) const
337 {
338 bool bReturn(false);
339 std::map<std::string, PeripheralDeviceSetting>::const_iterator it = m_settings.begin();
340 while (it != m_settings.end() && !bReturn)
341 {
342 if ((*it).second.m_setting->IsVisible())
343 {
344 bReturn = true;
345 break;
346 }
347
348 ++it;
349 }
350
351 return bReturn;
352 }
353
354 bool CPeripheral::GetSettingBool(const std::string& strKey) const
355 {
356 std::map<std::string, PeripheralDeviceSetting>::const_iterator it = m_settings.find(strKey);
357 if (it != m_settings.end() && (*it).second.m_setting->GetType() == SettingType::Boolean)
358 {
359 std::shared_ptr<CSettingBool> boolSetting =
360 std::static_pointer_cast<CSettingBool>((*it).second.m_setting);
361 if (boolSetting)
362 return boolSetting->GetValue();
363 }
364
365 return false;
366 }
367
368 int CPeripheral::GetSettingInt(const std::string& strKey) const
369 {
370 std::map<std::string, PeripheralDeviceSetting>::const_iterator it = m_settings.find(strKey);
371 if (it != m_settings.end() && (*it).second.m_setting->GetType() == SettingType::Integer)
372 {
373 std::shared_ptr<CSettingInt> intSetting =
374 std::static_pointer_cast<CSettingInt>((*it).second.m_setting);
375 if (intSetting)
376 return intSetting->GetValue();
377 }
378
379 return 0;
380 }
381
382 float CPeripheral::GetSettingFloat(const std::string& strKey) const
383 {
384 std::map<std::string, PeripheralDeviceSetting>::const_iterator it = m_settings.find(strKey);
385 if (it != m_settings.end() && (*it).second.m_setting->GetType() == SettingType::Number)
386 {
387 std::shared_ptr<CSettingNumber> floatSetting =
388 std::static_pointer_cast<CSettingNumber>((*it).second.m_setting);
389 if (floatSetting)
390 return (float)floatSetting->GetValue();
391 }
392
393 return 0;
394 }
395
396 const std::string CPeripheral::GetSettingString(const std::string& strKey) const
397 {
398 std::map<std::string, PeripheralDeviceSetting>::const_iterator it = m_settings.find(strKey);
399 if (it != m_settings.end() && (*it).second.m_setting->GetType() == SettingType::String)
400 {
401 std::shared_ptr<CSettingString> stringSetting =
402 std::static_pointer_cast<CSettingString>((*it).second.m_setting);
403 if (stringSetting)
404 return stringSetting->GetValue();
405 }
406
407 return "";
408 }
409
410 bool CPeripheral::SetSetting(const std::string& strKey, bool bValue)
411 {
412 bool bChanged(false);
413 std::map<std::string, PeripheralDeviceSetting>::iterator it = m_settings.find(strKey);
414 if (it != m_settings.end() && (*it).second.m_setting->GetType() == SettingType::Boolean)
415 {
416 std::shared_ptr<CSettingBool> boolSetting =
417 std::static_pointer_cast<CSettingBool>((*it).second.m_setting);
418 if (boolSetting)
419 {
420 bChanged = boolSetting->GetValue() != bValue;
421 boolSetting->SetValue(bValue);
422 if (bChanged && m_bInitialised)
423 m_changedSettings.insert(strKey);
424 }
425 }
426 return bChanged;
427 }
428
429 bool CPeripheral::SetSetting(const std::string& strKey, int iValue)
430 {
431 bool bChanged(false);
432 std::map<std::string, PeripheralDeviceSetting>::iterator it = m_settings.find(strKey);
433 if (it != m_settings.end() && (*it).second.m_setting->GetType() == SettingType::Integer)
434 {
435 std::shared_ptr<CSettingInt> intSetting =
436 std::static_pointer_cast<CSettingInt>((*it).second.m_setting);
437 if (intSetting)
438 {
439 bChanged = intSetting->GetValue() != iValue;
440 intSetting->SetValue(iValue);
441 if (bChanged && m_bInitialised)
442 m_changedSettings.insert(strKey);
443 }
444 }
445 return bChanged;
446 }
447
448 bool CPeripheral::SetSetting(const std::string& strKey, float fValue)
449 {
450 bool bChanged(false);
451 std::map<std::string, PeripheralDeviceSetting>::iterator it = m_settings.find(strKey);
452 if (it != m_settings.end() && (*it).second.m_setting->GetType() == SettingType::Number)
453 {
454 std::shared_ptr<CSettingNumber> floatSetting =
455 std::static_pointer_cast<CSettingNumber>((*it).second.m_setting);
456 if (floatSetting)
457 {
458 bChanged = floatSetting->GetValue() != static_cast<double>(fValue);
459 floatSetting->SetValue(static_cast<double>(fValue));
460 if (bChanged && m_bInitialised)
461 m_changedSettings.insert(strKey);
462 }
463 }
464 return bChanged;
465 }
466
467 void CPeripheral::SetSettingVisible(const std::string& strKey, bool bSetTo)
468 {
469 std::map<std::string, PeripheralDeviceSetting>::iterator it = m_settings.find(strKey);
470 if (it != m_settings.end())
471 (*it).second.m_setting->SetVisible(bSetTo);
472 }
473
474 bool CPeripheral::IsSettingVisible(const std::string& strKey) const
475 {
476 std::map<std::string, PeripheralDeviceSetting>::const_iterator it = m_settings.find(strKey);
477 if (it != m_settings.end())
478 return (*it).second.m_setting->IsVisible();
479 return false;
480 }
481
482 bool CPeripheral::SetSetting(const std::string& strKey, const std::string& strValue)
483 {
484 bool bChanged(false);
485 std::map<std::string, PeripheralDeviceSetting>::iterator it = m_settings.find(strKey);
486 if (it != m_settings.end())
487 {
488 if ((*it).second.m_setting->GetType() == SettingType::String)
489 {
490 std::shared_ptr<CSettingString> stringSetting =
491 std::static_pointer_cast<CSettingString>((*it).second.m_setting);
492 if (stringSetting)
493 {
494 bChanged = !StringUtils::EqualsNoCase(stringSetting->GetValue(), strValue);
495 stringSetting->SetValue(strValue);
496 if (bChanged && m_bInitialised)
497 m_changedSettings.insert(strKey);
498
499 if (strKey == SETTING_LAST_ACTIVE && !strValue.empty())
500 {
501 CDateTime lastActive;
502 lastActive.SetFromW3CDateTime(strValue, false);
503 SetLastActive(lastActive);
504 }
505 }
506 }
507 else if ((*it).second.m_setting->GetType() == SettingType::Integer)
508 bChanged = SetSetting(strKey, strValue.empty() ? 0 : atoi(strValue.c_str()));
509 else if ((*it).second.m_setting->GetType() == SettingType::Number)
510 bChanged = SetSetting(strKey, (float)(strValue.empty() ? 0 : atof(strValue.c_str())));
511 else if ((*it).second.m_setting->GetType() == SettingType::Boolean)
512 bChanged = SetSetting(strKey, strValue == "1" || StringUtils::EqualsNoCase(strValue, "true"));
513 }
514 return bChanged;
515 }
516
517 void CPeripheral::PersistSettings(bool bExiting /* = false */)
518 {
519 CXBMCTinyXML2 doc;
520 auto* node = doc.NewElement("settings");
521 if (node == nullptr)
522 return;
523
524 doc.InsertEndChild(node);
525 for (const auto& itr : m_settings)
526 {
527 auto* nodeSetting = doc.NewElement("setting");
528 if (nodeSetting == nullptr)
529 continue;
530
531 nodeSetting->SetAttribute("id", itr.first.c_str());
532 std::string strValue;
533 switch (itr.second.m_setting->GetType())
534 {
535 case SettingType::String:
536 {
537 std::shared_ptr<CSettingString> stringSetting =
538 std::static_pointer_cast<CSettingString>(itr.second.m_setting);
539 if (stringSetting)
540 strValue = stringSetting->GetValue();
541 }
542 break;
543 case SettingType::Integer:
544 {
545 std::shared_ptr<CSettingInt> intSetting =
546 std::static_pointer_cast<CSettingInt>(itr.second.m_setting);
547 if (intSetting)
548 strValue = std::to_string(intSetting->GetValue());
549 }
550 break;
551 case SettingType::Number:
552 {
553 std::shared_ptr<CSettingNumber> floatSetting =
554 std::static_pointer_cast<CSettingNumber>(itr.second.m_setting);
555 if (floatSetting)
556 strValue = StringUtils::Format("{:.2f}", floatSetting->GetValue());
557 }
558 break;
559 case SettingType::Boolean:
560 {
561 std::shared_ptr<CSettingBool> boolSetting =
562 std::static_pointer_cast<CSettingBool>(itr.second.m_setting);
563 if (boolSetting)
564 strValue = std::to_string(boolSetting->GetValue() ? 1 : 0);
565 }
566 break;
567 default:
568 break;
569 }
570 nodeSetting->SetAttribute("value", strValue.c_str());
571 doc.RootElement()->InsertEndChild(nodeSetting);
572 }
573
574 doc.SaveFile(m_strSettingsFile);
575
576 if (!bExiting)
577 {
578 for (const auto& it : m_changedSettings)
579 OnSettingChanged(it);
580 }
581 m_changedSettings.clear();
582 }
583
584 void CPeripheral::LoadPersistedSettings(void)
585 {
586 CXBMCTinyXML2 doc;
587 if (doc.LoadFile(m_strSettingsFile))
588 {
589 const auto* setting = doc.RootElement()->FirstChildElement("setting");
590 while (setting != nullptr)
591 {
592 std::string strId = XMLUtils::GetAttribute(setting, "id");
593 std::string strValue = XMLUtils::GetAttribute(setting, "value");
594 SetSetting(strId, strValue);
595
596 setting = setting->NextSiblingElement("setting");
597 }
598 }
599 }
600
601 void CPeripheral::ResetDefaultSettings(void)
602 {
603 ClearSettings();
604 m_manager.GetSettingsFromMapping(*this);
605
606 std::map<std::string, PeripheralDeviceSetting>::iterator it = m_settings.begin();
607 while (it != m_settings.end())
608 {
609 m_changedSettings.insert((*it).first);
610 ++it;
611 }
612
613 PersistSettings();
614 }
615
616 void CPeripheral::ClearSettings(void)
617 {
618 m_settings.clear();
619 }
620
621 void CPeripheral::RegisterInputHandler(IInputHandler* handler, bool bPromiscuous)
622 {
623 auto it = m_inputHandlers.find(handler);
624 if (it == m_inputHandlers.end())
625 {
626 PeripheralAddonPtr addon = m_manager.GetAddonWithButtonMap(this);
627 if (addon)
628 {
629 std::unique_ptr<CAddonInputHandling> addonInput = std::make_unique<CAddonInputHandling>(
630 m_manager, this, std::move(addon), handler, GetDriverReceiver());
631 if (addonInput->Load())
632 {
633 RegisterJoystickDriverHandler(addonInput.get(), bPromiscuous);
634 m_inputHandlers[handler] = std::move(addonInput);
635 }
636 }
637 else
638 {
639 CLog::Log(LOGDEBUG, "Failed to locate add-on for \"{}\"", m_strLocation);
640 }
641 }
642 }
643
644 void CPeripheral::UnregisterInputHandler(IInputHandler* handler)
645 {
646 handler->ResetInputReceiver();
647
648 auto it = m_inputHandlers.find(handler);
649 if (it != m_inputHandlers.end())
650 {
651 UnregisterJoystickDriverHandler(it->second.get());
652 m_inputHandlers.erase(it);
653 }
654 }
655
656 void CPeripheral::RegisterKeyboardHandler(KEYBOARD::IKeyboardInputHandler* handler,
657 bool bPromiscuous,
658 bool forceDefaultMap)
659 {
660 auto it = m_keyboardHandlers.find(handler);
661 if (it == m_keyboardHandlers.end())
662 {
663 std::unique_ptr<KODI::KEYBOARD::IKeyboardDriverHandler> keyboardDriverHandler;
664
665 if (!forceDefaultMap)
666 {
667 PeripheralAddonPtr addon = m_manager.GetAddonWithButtonMap(this);
668 if (addon)
669 {
670 std::unique_ptr<CAddonInputHandling> addonInput =
671 std::make_unique<CAddonInputHandling>(m_manager, this, std::move(addon), handler);
672 if (addonInput->Load())
673 keyboardDriverHandler = std::move(addonInput);
674 }
675 else
676 {
677 CLog::Log(LOGDEBUG, "Failed to locate add-on for \"{}\"", m_strLocation);
678 }
679 }
680
681 if (!keyboardDriverHandler)
682 {
683 std::unique_ptr<KODI::KEYBOARD::CDefaultKeyboardHandling> defaultInput =
684 std::make_unique<KODI::KEYBOARD::CDefaultKeyboardHandling>(this, handler);
685 if (defaultInput->Load())
686 keyboardDriverHandler = std::move(defaultInput);
687 }
688
689 if (keyboardDriverHandler)
690 {
691 RegisterKeyboardDriverHandler(keyboardDriverHandler.get(), bPromiscuous);
692 m_keyboardHandlers[handler] = std::move(keyboardDriverHandler);
693 }
694 }
695 }
696
697 void CPeripheral::UnregisterKeyboardHandler(KEYBOARD::IKeyboardInputHandler* handler)
698 {
699 auto it = m_keyboardHandlers.find(handler);
700 if (it != m_keyboardHandlers.end())
701 {
702 UnregisterKeyboardDriverHandler(it->second.get());
703 m_keyboardHandlers.erase(it);
704 }
705 }
706
707 void CPeripheral::RegisterMouseHandler(MOUSE::IMouseInputHandler* handler,
708 bool bPromiscuous,
709 bool forceDefaultMap)
710 {
711 auto it = m_mouseHandlers.find(handler);
712 if (it == m_mouseHandlers.end())
713 {
714 std::unique_ptr<KODI::MOUSE::IMouseDriverHandler> mouseDriverHandler;
715
716 if (!forceDefaultMap)
717 {
718 PeripheralAddonPtr addon = m_manager.GetAddonWithButtonMap(this);
719 if (addon)
720 {
721 std::unique_ptr<CAddonInputHandling> addonInput =
722 std::make_unique<CAddonInputHandling>(m_manager, this, std::move(addon), handler);
723 if (addonInput->Load())
724 mouseDriverHandler = std::move(addonInput);
725 }
726 else
727 {
728 CLog::Log(LOGDEBUG, "Failed to locate add-on for \"{}\"", m_strLocation);
729 }
730 }
731
732 if (!mouseDriverHandler)
733 {
734 std::unique_ptr<KODI::MOUSE::CDefaultMouseHandling> defaultInput =
735 std::make_unique<KODI::MOUSE::CDefaultMouseHandling>(this, handler);
736 if (defaultInput->Load())
737 mouseDriverHandler = std::move(defaultInput);
738 }
739
740 if (mouseDriverHandler)
741 {
742 RegisterMouseDriverHandler(mouseDriverHandler.get(), bPromiscuous);
743 m_mouseHandlers[handler] = std::move(mouseDriverHandler);
744 }
745 }
746 }
747
748 void CPeripheral::UnregisterMouseHandler(MOUSE::IMouseInputHandler* handler)
749 {
750 auto it = m_mouseHandlers.find(handler);
751 if (it != m_mouseHandlers.end())
752 {
753 UnregisterMouseDriverHandler(it->second.get());
754 m_mouseHandlers.erase(it);
755 }
756 }
757
758 void CPeripheral::RegisterJoystickButtonMapper(IButtonMapper* mapper)
759 {
760 auto it = m_buttonMappers.find(mapper);
761 if (it == m_buttonMappers.end())
762 {
763 std::unique_ptr<CAddonButtonMapping> addonMapping(
764 new CAddonButtonMapping(m_manager, this, mapper));
765
766 RegisterJoystickDriverHandler(addonMapping.get(), false);
767 RegisterKeyboardDriverHandler(addonMapping.get(), false);
768 RegisterMouseDriverHandler(addonMapping.get(), false);
769
770 m_buttonMappers[mapper] = std::move(addonMapping);
771 }
772 }
773
774 void CPeripheral::UnregisterJoystickButtonMapper(IButtonMapper* mapper)
775 {
776 auto it = m_buttonMappers.find(mapper);
777 if (it != m_buttonMappers.end())
778 {
779 UnregisterMouseDriverHandler(it->second.get());
780 UnregisterKeyboardDriverHandler(it->second.get());
781 UnregisterJoystickDriverHandler(it->second.get());
782
783 m_buttonMappers.erase(it);
784 }
785 }
786
787 std::string CPeripheral::GetIcon() const
788 {
789 std::string icon;
790
791 // Try controller profile
792 const GAME::ControllerPtr controller = ControllerProfile();
793 if (controller)
794 icon = controller->Layout().ImagePath();
795
796 // Try add-on
797 if (icon.empty() && m_busType == PERIPHERAL_BUS_ADDON)
798 {
799 CPeripheralBusAddon* bus = static_cast<CPeripheralBusAddon*>(m_bus);
800
801 PeripheralAddonPtr addon;
802 unsigned int index;
803 if (bus->SplitLocation(m_strLocation, addon, index))
804 {
805 std::string addonIcon = addon->Icon();
806 if (!addonIcon.empty())
807 icon = std::move(addonIcon);
808 }
809 }
810
811 // Fallback
812 if (icon.empty())
813 icon = "DefaultAddon.png";
814
815 return icon;
816 }
817
818 bool CPeripheral::operator==(const PeripheralScanResult& right) const
819 {
820 return StringUtils::EqualsNoCase(m_strLocation, right.m_strLocation);
821 }
822
823 bool CPeripheral::operator!=(const PeripheralScanResult& right) const
824 {
825 return !(*this == right);
826 }
827
828 CDateTime CPeripheral::LastActive() const
829 {
830 // By default, peripherals are fully-activated
831 return CDateTime::GetCurrentDateTime();
832 }
833
834 void CPeripheral::SetLastActive(const CDateTime& lastActive)
835 {
836 // Update last active setting
837 const std::string strKey{SETTING_LAST_ACTIVE};
838
839 auto it = m_settings.find(strKey);
840 if (it != m_settings.end() && it->second.m_setting->GetType() == SettingType::String)
841 {
842 std::shared_ptr<CSettingString> stringSetting =
843 std::static_pointer_cast<CSettingString>(it->second.m_setting);
844
845 const bool wasActive = !stringSetting->GetValue().empty();
846
847 const std::string lastActiveStr = lastActive.IsValid() ? lastActive.GetAsW3CDateTime() : "";
848
849 stringSetting->SetValue(lastActiveStr);
850
851 // Notify listeners if a peripheral was activated for the first time
852 if (!wasActive & lastActive.IsValid())
853 {
854 m_manager.SetChanged(true);
855 m_manager.NotifyObservers(ObservableMessagePeripheralsChanged);
856 PersistSettings();
857 }
858 }
859 }