Debian Codegraph @

xbmc/peripherals/devices/PeripheralCecAdapter.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 "PeripheralCecAdapter.h"
10
11 #include "ServiceBroker.h"
12 #include "application/ApplicationComponents.h"
13 #include "application/ApplicationEnums.h"
14 #include "application/ApplicationPlayer.h"
15 #include "application/ApplicationPowerHandling.h"
16 #include "application/ApplicationVolumeHandling.h"
17 #include "dialogs/GUIDialogKaiToast.h"
18 #include "guilib/GUIComponent.h"
19 #include "guilib/GUIWindowManager.h"
20 #include "guilib/LocalizeStrings.h"
21 #include "input/actions/Action.h"
22 #include "input/actions/ActionIDs.h"
23 #include "input/remote/IRRemote.h"
24 #include "messaging/ApplicationMessenger.h"
25 #include "pictures/GUIWindowSlideShow.h"
26 #include "utils/JobManager.h"
27 #include "utils/Variant.h"
28 #include "utils/log.h"
29 #include "xbmc/interfaces/AnnouncementManager.h"
30
31 #include <mutex>
32
33 #include <libcec/cec.h>
34
35 using namespace PERIPHERALS;
36 using namespace CEC;
37 using namespace ANNOUNCEMENT;
38 using namespace std::chrono_literals;
39
40 #define CEC_LIB_SUPPORTED_VERSION LIBCEC_VERSION_TO_UINT(4, 0, 0)
41
42 /* time in seconds to ignore standby commands from devices after the screensaver has been activated
43 */
44 #define SCREENSAVER_TIMEOUT 20
45 #define VOLUME_CHANGE_TIMEOUT 250
46 #define VOLUME_REFRESH_TIMEOUT 100
47
48 #define LOCALISED_ID_TV 36037
49 #define LOCALISED_ID_AVR 36038
50 #define LOCALISED_ID_TV_AVR 36039
51 #define LOCALISED_ID_STOP 36044
52 #define LOCALISED_ID_PAUSE 36045
53 #define LOCALISED_ID_POWEROFF 13005
54 #define LOCALISED_ID_SUSPEND 13011
55 #define LOCALISED_ID_HIBERNATE 13010
56 #define LOCALISED_ID_QUIT 13009
57 #define LOCALISED_ID_IGNORE 36028
58 #define LOCALISED_ID_RECORDING_DEVICE 36051
59 #define LOCALISED_ID_PLAYBACK_DEVICE 36052
60 #define LOCALISED_ID_TUNER_DEVICE 36053
61
62 #define LOCALISED_ID_NONE 231
63
64 /* time in seconds to suppress source activation after receiving OnStop */
65 #define CEC_SUPPRESS_ACTIVATE_SOURCE_AFTER_ON_STOP 2
66
67 CPeripheralCecAdapter::CPeripheralCecAdapter(CPeripherals& manager,
68 const PeripheralScanResult& scanResult,
69 CPeripheralBus* bus)
70 : CPeripheralHID(manager, scanResult, bus), CThread("CECAdapter"), m_cecAdapter(NULL)
71 {
72 ResetMembers();
73 m_features.push_back(FEATURE_CEC);
74 m_strComPort = scanResult.m_strLocation;
75 }
76
77 CPeripheralCecAdapter::~CPeripheralCecAdapter(void)
78 {
79 {
80 std::unique_lock<CCriticalSection> lock(m_critSection);
81 CServiceBroker::GetAnnouncementManager()->RemoveAnnouncer(this);
82 m_bStop = true;
83 }
84
85 StopThread(true);
86 delete m_queryThread;
87
88 if (m_cecAdapter)
89 {
90 CECDestroy(m_cecAdapter);
91 m_cecAdapter = NULL;
92 }
93 }
94
95 void CPeripheralCecAdapter::ResetMembers(void)
96 {
97 if (m_cecAdapter)
98 CECDestroy(m_cecAdapter);
99 m_cecAdapter = NULL;
100 m_bStarted = false;
101 m_bHasButton = false;
102 m_bIsReady = false;
103 m_bHasConnectedAudioSystem = false;
104 m_strMenuLanguage = "???";
105 m_lastKeypress = {};
106 m_lastChange = VOLUME_CHANGE_NONE;
107 m_iExitCode = EXITCODE_QUIT;
108
109 //! @todo fetch the correct initial value when system audiostatus is
110 //! implemented in libCEC
111 m_bIsMuted = false;
112
113 m_bGoingToStandby = false;
114 m_bIsRunning = false;
115 m_bDeviceRemoved = false;
116 m_bActiveSourcePending = false;
117 m_bStandbyPending = false;
118 m_bActiveSourceBeforeStandby = false;
119 m_bOnPlayReceived = false;
120 m_bPlaybackPaused = false;
121 m_queryThread = NULL;
122 m_bPowerOnScreensaver = false;
123 m_bUseTVMenuLanguage = false;
124 m_bSendInactiveSource = false;
125 m_bPowerOffScreensaver = false;
126 m_bShutdownOnStandby = false;
127
128 m_currentButton.iButton = 0;
129 m_currentButton.iDuration = 0;
130 m_standbySent.SetValid(false);
131 m_configuration.Clear();
132 }
133
134 void CPeripheralCecAdapter::Announce(ANNOUNCEMENT::AnnouncementFlag flag,
135 const std::string& sender,
136 const std::string& message,
137 const CVariant& data)
138 {
139 if (flag == ANNOUNCEMENT::System && sender == CAnnouncementManager::ANNOUNCEMENT_SENDER &&
140 message == "OnQuit" && m_bIsReady)
141 {
142 std::unique_lock<CCriticalSection> lock(m_critSection);
143 m_iExitCode = static_cast<int>(data["exitcode"].asInteger(EXITCODE_QUIT));
144 CServiceBroker::GetAnnouncementManager()->RemoveAnnouncer(this);
145 StopThread(false);
146 }
147 else if (flag == ANNOUNCEMENT::GUI && sender == CAnnouncementManager::ANNOUNCEMENT_SENDER &&
148 message == "OnScreensaverDeactivated" && m_bIsReady)
149 {
150 bool bIgnoreDeactivate(false);
151 if (data["shuttingdown"].isBoolean())
152 {
153 // don't respond to the deactivation if we are just going to suspend/shutdown anyway
154 // the tv will not have time to switch on before being told to standby and
155 // may not action the standby command.
156 bIgnoreDeactivate = data["shuttingdown"].asBoolean();
157 if (bIgnoreDeactivate)
158 CLog::Log(LOGDEBUG, "{} - ignoring OnScreensaverDeactivated for power action",
159 __FUNCTION__);
160 }
161 if (m_bPowerOnScreensaver && !bIgnoreDeactivate && m_configuration.bActivateSource)
162 {
163 ActivateSource();
164 }
165 }
166 else if (flag == ANNOUNCEMENT::GUI && sender == CAnnouncementManager::ANNOUNCEMENT_SENDER &&
167 message == "OnScreensaverActivated" && m_bIsReady)
168 {
169 // Don't put devices to standby if application is currently playing
170 const auto& components = CServiceBroker::GetAppComponents();
171 const auto appPlayer = components.GetComponent<CApplicationPlayer>();
172 if (!appPlayer->IsPlaying() && m_bPowerOffScreensaver)
173 {
174 // only power off when we're the active source
175 if (m_cecAdapter->IsLibCECActiveSource())
176 StandbyDevices();
177 }
178 }
179 else if (flag == ANNOUNCEMENT::System && sender == CAnnouncementManager::ANNOUNCEMENT_SENDER &&
180 message == "OnSleep")
181 {
182 // this will also power off devices when we're the active source
183 {
184 std::unique_lock<CCriticalSection> lock(m_critSection);
185 m_bGoingToStandby = true;
186 }
187 StopThread();
188 }
189 else if (flag == ANNOUNCEMENT::System && sender == CAnnouncementManager::ANNOUNCEMENT_SENDER &&
190 message == "OnWake")
191 {
192 CLog::Log(LOGDEBUG, "{} - reconnecting to the CEC adapter after standby mode", __FUNCTION__);
193 if (ReopenConnection())
194 {
195 bool bActivate(false);
196 {
197 std::unique_lock<CCriticalSection> lock(m_critSection);
198 bActivate = m_bActiveSourceBeforeStandby;
199 m_bActiveSourceBeforeStandby = false;
200 }
201 if (bActivate)
202 ActivateSource();
203 }
204 }
205 else if (flag == ANNOUNCEMENT::Player && sender == CAnnouncementManager::ANNOUNCEMENT_SENDER &&
206 message == "OnStop")
207 {
208 std::unique_lock<CCriticalSection> lock(m_critSection);
209 m_preventActivateSourceOnPlay = CDateTime::GetCurrentDateTime();
210 m_bOnPlayReceived = false;
211 }
212 else if (flag == ANNOUNCEMENT::Player && sender == CAnnouncementManager::ANNOUNCEMENT_SENDER &&
213 (message == "OnPlay" || message == "OnResume"))
214 {
215 // activate the source when playback started, and the option is enabled
216 bool bActivateSource(false);
217 {
218 std::unique_lock<CCriticalSection> lock(m_critSection);
219 bActivateSource = (m_configuration.bActivateSource && !m_bOnPlayReceived &&
220 !m_cecAdapter->IsLibCECActiveSource() &&
221 (!m_preventActivateSourceOnPlay.IsValid() ||
222 CDateTime::GetCurrentDateTime() - m_preventActivateSourceOnPlay >
223 CDateTimeSpan(0, 0, 0, CEC_SUPPRESS_ACTIVATE_SOURCE_AFTER_ON_STOP)));
224 m_bOnPlayReceived = true;
225 }
226 if (bActivateSource)
227 ActivateSource();
228 }
229 }
230
231 bool CPeripheralCecAdapter::InitialiseFeature(const PeripheralFeature feature)
232 {
233 if (feature == FEATURE_CEC && !m_bStarted && GetSettingBool("enabled"))
234 {
235 // hide settings that have an override set
236 if (!GetSettingString("wake_devices_advanced").empty())
237 SetSettingVisible("wake_devices", false);
238 if (!GetSettingString("standby_devices_advanced").empty())
239 SetSettingVisible("standby_devices", false);
240
241 SetConfigurationFromSettings();
242 m_callbacks.Clear();
243 m_callbacks.logMessage = &CecLogMessage;
244 m_callbacks.keyPress = &CecKeyPress;
245 m_callbacks.commandReceived = &CecCommand;
246 m_callbacks.configurationChanged = &CecConfiguration;
247 m_callbacks.alert = &CecAlert;
248 m_callbacks.sourceActivated = &CecSourceActivated;
249 m_configuration.callbackParam = this;
250 m_configuration.callbacks = &m_callbacks;
251
252 m_cecAdapter = CECInitialise(&m_configuration);
253
254 if (m_configuration.serverVersion < CEC_LIB_SUPPORTED_VERSION)
255 {
256 /* unsupported libcec version */
257 CLog::Log(
258 LOGERROR,
259 "Detected version of libCEC interface ({0:x}) is lower than the supported version {1:x}",
260 m_cecAdapter ? m_configuration.serverVersion : -1, CEC_LIB_SUPPORTED_VERSION);
261
262 // display warning: incompatible libCEC
263 std::string strMessage = StringUtils::Format(
264 g_localizeStrings.Get(36040), m_cecAdapter ? m_configuration.serverVersion : -1,
265 CEC_LIB_SUPPORTED_VERSION);
266 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Error, g_localizeStrings.Get(36000),
267 strMessage);
268 m_bError = true;
269 if (m_cecAdapter)
270 CECDestroy(m_cecAdapter);
271 m_cecAdapter = NULL;
272
273 m_features.clear();
274 return false;
275 }
276 else
277 {
278 CLog::Log(LOGDEBUG, "{} - using libCEC v{}", __FUNCTION__,
279 m_cecAdapter->VersionToString(m_configuration.serverVersion));
280 SetVersionInfo(m_configuration);
281 }
282
283 m_bStarted = true;
284 Create();
285 }
286
287 return CPeripheral::InitialiseFeature(feature);
288 }
289
290 void CPeripheralCecAdapter::SetVersionInfo(const libcec_configuration& configuration)
291 {
292 m_strVersionInfo = StringUtils::Format("libCEC {} - firmware v{}",
293 m_cecAdapter->VersionToString(configuration.serverVersion),
294 configuration.iFirmwareVersion);
295
296 // append firmware build date
297 if (configuration.iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN)
298 {
299 CDateTime dt((time_t)configuration.iFirmwareBuildDate);
300 m_strVersionInfo += StringUtils::Format(" ({})", dt.GetAsDBDate());
301 }
302 }
303
304 bool CPeripheralCecAdapter::OpenConnection(void)
305 {
306 bool bIsOpen(false);
307
308 if (!GetSettingBool("enabled"))
309 {
310 CLog::Log(LOGDEBUG, "{} - CEC adapter is disabled in peripheral settings", __FUNCTION__);
311 m_bStarted = false;
312 return bIsOpen;
313 }
314
315 // open the CEC adapter
316 CLog::Log(LOGDEBUG, "{} - opening a connection to the CEC adapter: {}", __FUNCTION__,
317 m_strComPort);
318
319 // scanning the CEC bus takes about 5 seconds, so display a notification to inform users that
320 // we're busy
321 std::string strMessage =
322 StringUtils::Format(g_localizeStrings.Get(21336), g_localizeStrings.Get(36000));
323 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(36000),
324 strMessage);
325
326 bool bConnectionFailedDisplayed(false);
327
328 while (!m_bStop && !bIsOpen)
329 {
330 if ((bIsOpen = m_cecAdapter->Open(m_strComPort.c_str(), 10000)) == false)
331 {
332 // display warning: couldn't initialise libCEC
333 CLog::Log(LOGERROR, "{} - could not opening a connection to the CEC adapter", __FUNCTION__);
334 if (!bConnectionFailedDisplayed)
335 CGUIDialogKaiToast::QueueNotification(
336 CGUIDialogKaiToast::Error, g_localizeStrings.Get(36000), g_localizeStrings.Get(36012));
337 bConnectionFailedDisplayed = true;
338
339 CThread::Sleep(10000ms);
340 }
341 }
342
343 if (bIsOpen)
344 {
345 CLog::Log(LOGDEBUG, "{} - connection to the CEC adapter opened", __FUNCTION__);
346
347 // read the configuration
348 libcec_configuration config;
349 if (m_cecAdapter->GetCurrentConfiguration(&config))
350 {
351 // update the local configuration
352 std::unique_lock<CCriticalSection> lock(m_critSection);
353 SetConfigurationFromLibCEC(config);
354 }
355 }
356
357 return bIsOpen;
358 }
359
360 void CPeripheralCecAdapter::Process(void)
361 {
362 if (!OpenConnection())
363 return;
364
365 {
366 std::unique_lock<CCriticalSection> lock(m_critSection);
367 m_iExitCode = EXITCODE_QUIT;
368 m_bGoingToStandby = false;
369 m_bIsRunning = true;
370 m_bActiveSourceBeforeStandby = false;
371 }
372
373 CServiceBroker::GetAnnouncementManager()->AddAnnouncer(this);
374
375 m_queryThread = new CPeripheralCecAdapterUpdateThread(this, &m_configuration);
376 m_queryThread->Create(false);
377
378 while (!m_bStop)
379 {
380 if (!m_bStop)
381 ProcessVolumeChange();
382
383 if (!m_bStop)
384 ProcessActivateSource();
385
386 if (!m_bStop)
387 ProcessStandbyDevices();
388
389 if (!m_bStop)
390 CThread::Sleep(5ms);
391 }
392
393 m_queryThread->StopThread(true);
394
395 bool bSendStandbyCommands(false);
396 {
397 std::unique_lock<CCriticalSection> lock(m_critSection);
398 bSendStandbyCommands = m_iExitCode != EXITCODE_REBOOT && m_iExitCode != EXITCODE_RESTARTAPP &&
399 !m_bDeviceRemoved &&
400 (!m_bGoingToStandby || GetSettingBool("standby_tv_on_pc_standby")) &&
401 GetSettingBool("enabled");
402
403 if (m_bGoingToStandby)
404 m_bActiveSourceBeforeStandby = m_cecAdapter->IsLibCECActiveSource();
405 }
406
407 if (bSendStandbyCommands)
408 {
409 if (m_cecAdapter->IsLibCECActiveSource())
410 {
411 if (!m_configuration.powerOffDevices.IsEmpty())
412 {
413 CLog::Log(LOGDEBUG, "{} - sending standby commands", __FUNCTION__);
414 m_standbySent = CDateTime::GetCurrentDateTime();
415 m_cecAdapter->StandbyDevices();
416 }
417 else if (m_bSendInactiveSource)
418 {
419 CLog::Log(LOGDEBUG, "{} - sending inactive source commands", __FUNCTION__);
420 m_cecAdapter->SetInactiveView();
421 }
422 }
423 else
424 {
425 CLog::Log(LOGDEBUG, "{} - XBMC is not the active source, not sending any standby commands",
426 __FUNCTION__);
427 }
428 }
429
430 m_cecAdapter->Close();
431
432 CLog::Log(LOGDEBUG, "{} - CEC adapter processor thread ended", __FUNCTION__);
433
434 {
435 std::unique_lock<CCriticalSection> lock(m_critSection);
436 m_bStarted = false;
437 m_bIsRunning = false;
438 }
439 }
440
441 bool CPeripheralCecAdapter::HasAudioControl(void)
442 {
443 std::unique_lock<CCriticalSection> lock(m_critSection);
444 return m_bHasConnectedAudioSystem;
445 }
446
447 void CPeripheralCecAdapter::SetAudioSystemConnected(bool bSetTo)
448 {
449 std::unique_lock<CCriticalSection> lock(m_critSection);
450 m_bHasConnectedAudioSystem = bSetTo;
451 }
452
453 void CPeripheralCecAdapter::ProcessVolumeChange(void)
454 {
455 bool bSendRelease(false);
456 CecVolumeChange pendingVolumeChange = VOLUME_CHANGE_NONE;
457 {
458 std::unique_lock<CCriticalSection> lock(m_critSection);
459 auto now = std::chrono::steady_clock::now();
460 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_lastKeypress);
461 if (!m_volumeChangeQueue.empty())
462 {
463 /* get the first change from the queue */
464 pendingVolumeChange = m_volumeChangeQueue.front();
465 m_volumeChangeQueue.pop();
466
467 /* remove all dupe entries */
468 while (!m_volumeChangeQueue.empty() && m_volumeChangeQueue.front() == pendingVolumeChange)
469 m_volumeChangeQueue.pop();
470
471 /* send another keypress after VOLUME_REFRESH_TIMEOUT ms */
472
473 bool bRefresh(duration.count() > VOLUME_REFRESH_TIMEOUT);
474
475 /* only send the keypress when it hasn't been sent yet */
476 if (pendingVolumeChange != m_lastChange)
477 {
478 m_lastKeypress = std::chrono::steady_clock::now();
479 m_lastChange = pendingVolumeChange;
480 }
481 else if (bRefresh)
482 {
483 m_lastKeypress = std::chrono::steady_clock::now();
484 pendingVolumeChange = m_lastChange;
485 }
486 else
487 pendingVolumeChange = VOLUME_CHANGE_NONE;
488 }
489 else if (m_lastKeypress.time_since_epoch().count() > 0 &&
490 duration.count() > VOLUME_CHANGE_TIMEOUT)
491 {
492 /* send a key release */
493 m_lastKeypress = {};
494 bSendRelease = true;
495 m_lastChange = VOLUME_CHANGE_NONE;
496 }
497 }
498
499 switch (pendingVolumeChange)
500 {
501 case VOLUME_CHANGE_UP:
502 m_cecAdapter->SendKeypress(CECDEVICE_AUDIOSYSTEM, CEC_USER_CONTROL_CODE_VOLUME_UP, false);
503 break;
504 case VOLUME_CHANGE_DOWN:
505 m_cecAdapter->SendKeypress(CECDEVICE_AUDIOSYSTEM, CEC_USER_CONTROL_CODE_VOLUME_DOWN, false);
506 break;
507 case VOLUME_CHANGE_MUTE:
508 m_cecAdapter->SendKeypress(CECDEVICE_AUDIOSYSTEM, CEC_USER_CONTROL_CODE_MUTE, false);
509 {
510 std::unique_lock<CCriticalSection> lock(m_critSection);
511 m_bIsMuted = !m_bIsMuted;
512 }
513 break;
514 case VOLUME_CHANGE_NONE:
515 if (bSendRelease)
516 m_cecAdapter->SendKeyRelease(CECDEVICE_AUDIOSYSTEM, false);
517 break;
518 }
519 }
520
521 void CPeripheralCecAdapter::VolumeUp(void)
522 {
523 if (HasAudioControl())
524 {
525 std::unique_lock<CCriticalSection> lock(m_critSection);
526 m_volumeChangeQueue.push(VOLUME_CHANGE_UP);
527 }
528 }
529
530 void CPeripheralCecAdapter::VolumeDown(void)
531 {
532 if (HasAudioControl())
533 {
534 std::unique_lock<CCriticalSection> lock(m_critSection);
535 m_volumeChangeQueue.push(VOLUME_CHANGE_DOWN);
536 }
537 }
538
539 void CPeripheralCecAdapter::ToggleMute(void)
540 {
541 if (HasAudioControl())
542 {
543 std::unique_lock<CCriticalSection> lock(m_critSection);
544 m_volumeChangeQueue.push(VOLUME_CHANGE_MUTE);
545 }
546 }
547
548 bool CPeripheralCecAdapter::IsMuted(void)
549 {
550 if (HasAudioControl())
551 {
552 std::unique_lock<CCriticalSection> lock(m_critSection);
553 return m_bIsMuted;
554 }
555 return false;
556 }
557
558 void CPeripheralCecAdapter::SetMenuLanguage(const char* strLanguage)
559 {
560 if (StringUtils::EqualsNoCase(m_strMenuLanguage, strLanguage))
561 return;
562
563 std::string strGuiLanguage;
564
565 if (!strcmp(strLanguage, "bul"))
566 strGuiLanguage = "bg_bg";
567 else if (!strcmp(strLanguage, "hrv"))
568 strGuiLanguage = "hr_hr";
569 else if (!strcmp(strLanguage, "cze"))
570 strGuiLanguage = "cs_cz";
571 else if (!strcmp(strLanguage, "dan"))
572 strGuiLanguage = "da_dk";
573 else if (!strcmp(strLanguage, "deu"))
574 strGuiLanguage = "de_de";
575 else if (!strcmp(strLanguage, "dut"))
576 strGuiLanguage = "nl_nl";
577 else if (!strcmp(strLanguage, "eng"))
578 strGuiLanguage = "en_gb";
579 else if (!strcmp(strLanguage, "fin"))
580 strGuiLanguage = "fi_fi";
581 else if (!strcmp(strLanguage, "fre"))
582 strGuiLanguage = "fr_fr";
583 else if (!strcmp(strLanguage, "ger"))
584 strGuiLanguage = "de_de";
585 else if (!strcmp(strLanguage, "gre"))
586 strGuiLanguage = "el_gr";
587 else if (!strcmp(strLanguage, "hun"))
588 strGuiLanguage = "hu_hu";
589 else if (!strcmp(strLanguage, "ita"))
590 strGuiLanguage = "it_it";
591 else if (!strcmp(strLanguage, "nor"))
592 strGuiLanguage = "nb_no";
593 else if (!strcmp(strLanguage, "pol"))
594 strGuiLanguage = "pl_pl";
595 else if (!strcmp(strLanguage, "por"))
596 strGuiLanguage = "pt_pt";
597 else if (!strcmp(strLanguage, "rum"))
598 strGuiLanguage = "ro_ro";
599 else if (!strcmp(strLanguage, "rus"))
600 strGuiLanguage = "ru_ru";
601 else if (!strcmp(strLanguage, "srp"))
602 strGuiLanguage = "sr_rs@latin";
603 else if (!strcmp(strLanguage, "slo"))
604 strGuiLanguage = "sk_sk";
605 else if (!strcmp(strLanguage, "slv"))
606 strGuiLanguage = "sl_si";
607 else if (!strcmp(strLanguage, "spa"))
608 strGuiLanguage = "es_es";
609 else if (!strcmp(strLanguage, "swe"))
610 strGuiLanguage = "sv_se";
611 else if (!strcmp(strLanguage, "tur"))
612 strGuiLanguage = "tr_tr";
613
614 if (!strGuiLanguage.empty())
615 {
616 strGuiLanguage = "resource.language." + strGuiLanguage;
617 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_SETLANGUAGE, -1, -1, nullptr, strGuiLanguage);
618 CLog::Log(LOGDEBUG, "{} - language set to '{}'", __FUNCTION__, strGuiLanguage);
619 }
620 else
621 CLog::Log(LOGWARNING, "{} - TV menu language set to unknown value '{}'", __FUNCTION__,
622 strLanguage);
623 }
624
625 void CPeripheralCecAdapter::OnTvStandby(void)
626 {
627 int iActionOnTvStandby = GetSettingInt("standby_pc_on_tv_standby");
628 switch (iActionOnTvStandby)
629 {
630 case LOCALISED_ID_POWEROFF:
631 m_bStarted = false;
632 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_SYSTEM_POWERDOWN, TMSG_SHUTDOWN);
633 break;
634 case LOCALISED_ID_SUSPEND:
635 m_bStarted = false;
636 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_SYSTEM_POWERDOWN, TMSG_SUSPEND);
637 break;
638 case LOCALISED_ID_HIBERNATE:
639 m_bStarted = false;
640 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_SYSTEM_POWERDOWN, TMSG_HIBERNATE);
641 break;
642 case LOCALISED_ID_QUIT:
643 m_bStarted = false;
644 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_QUIT);
645 break;
646 case LOCALISED_ID_PAUSE:
647 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_MEDIA_PAUSE_IF_PLAYING);
648 break;
649 case LOCALISED_ID_STOP:
650 {
651 const auto& components = CServiceBroker::GetAppComponents();
652 const auto appPlayer = components.GetComponent<CApplicationPlayer>();
653 if (appPlayer->IsPlaying())
654 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_MEDIA_STOP);
655 break;
656 }
657 case LOCALISED_ID_IGNORE:
658 break;
659 default:
660 CLog::Log(LOGERROR, "{} - Unexpected [standby_pc_on_tv_standby] setting value", __FUNCTION__);
661 break;
662 }
663 }
664
665 void CPeripheralCecAdapter::CecCommand(void* cbParam, const cec_command* command)
666 {
667 CPeripheralCecAdapter* adapter = static_cast<CPeripheralCecAdapter*>(cbParam);
668 if (!adapter)
669 return;
670
671 if (adapter->m_bIsReady)
672 {
673 switch (command->opcode)
674 {
675 case CEC_OPCODE_STANDBY:
676 if (command->initiator == CECDEVICE_TV &&
677 (!adapter->m_standbySent.IsValid() ||
678 CDateTime::GetCurrentDateTime() - adapter->m_standbySent >
679 CDateTimeSpan(0, 0, 0, SCREENSAVER_TIMEOUT)))
680 {
681 adapter->OnTvStandby();
682 }
683 break;
684 case CEC_OPCODE_SET_MENU_LANGUAGE:
685 if (adapter->m_bUseTVMenuLanguage == 1 && command->initiator == CECDEVICE_TV &&
686 command->parameters.size == 3)
687 {
688 char strNewLanguage[4];
689 for (int iPtr = 0; iPtr < 3; iPtr++)
690 strNewLanguage[iPtr] = command->parameters[iPtr];
691 strNewLanguage[3] = 0;
692 adapter->SetMenuLanguage(strNewLanguage);
693 }
694 break;
695 case CEC_OPCODE_DECK_CONTROL:
696 if (command->initiator == CECDEVICE_TV && command->parameters.size == 1 &&
697 command->parameters[0] == CEC_DECK_CONTROL_MODE_STOP)
698 {
699 cec_keypress key;
700 key.duration = 500;
701 key.keycode = CEC_USER_CONTROL_CODE_STOP;
702 adapter->PushCecKeypress(key);
703 }
704 break;
705 case CEC_OPCODE_PLAY:
706 if (command->initiator == CECDEVICE_TV && command->parameters.size == 1)
707 {
708 if (command->parameters[0] == CEC_PLAY_MODE_PLAY_FORWARD)
709 {
710 cec_keypress key;
711 key.duration = 500;
712 key.keycode = CEC_USER_CONTROL_CODE_PLAY;
713 adapter->PushCecKeypress(key);
714 }
715 else if (command->parameters[0] == CEC_PLAY_MODE_PLAY_STILL)
716 {
717 cec_keypress key;
718 key.duration = 500;
719 key.keycode = CEC_USER_CONTROL_CODE_PAUSE;
720 adapter->PushCecKeypress(key);
721 }
722 }
723 break;
724 default:
725 break;
726 }
727 }
728 }
729
730 void CPeripheralCecAdapter::CecConfiguration(void* cbParam, const libcec_configuration* config)
731 {
732 CPeripheralCecAdapter* adapter = static_cast<CPeripheralCecAdapter*>(cbParam);
733 if (!adapter)
734 return;
735
736 std::unique_lock<CCriticalSection> lock(adapter->m_critSection);
737 adapter->SetConfigurationFromLibCEC(*config);
738 }
739
740 void CPeripheralCecAdapter::CecAlert(void* cbParam,
741 const libcec_alert alert,
742 const libcec_parameter data)
743 {
744 CPeripheralCecAdapter* adapter = static_cast<CPeripheralCecAdapter*>(cbParam);
745 if (!adapter)
746 return;
747
748 bool bReopenConnection(false);
749 int iAlertString(0);
750 switch (alert)
751 {
752 case CEC_ALERT_SERVICE_DEVICE:
753 iAlertString = 36027;
754 break;
755 case CEC_ALERT_CONNECTION_LOST:
756 bReopenConnection = true;
757 iAlertString = 36030;
758 break;
759 #if defined(CEC_ALERT_PERMISSION_ERROR)
760 case CEC_ALERT_PERMISSION_ERROR:
761 bReopenConnection = true;
762 iAlertString = 36031;
763 break;
764 case CEC_ALERT_PORT_BUSY:
765 bReopenConnection = true;
766 iAlertString = 36032;
767 break;
768 #endif
769 default:
770 break;
771 }
772
773 // display the alert
774 if (iAlertString)
775 {
776 std::string strLog(g_localizeStrings.Get(iAlertString));
777 if (data.paramType == CEC_PARAMETER_TYPE_STRING && data.paramData)
778 strLog += StringUtils::Format(" - {}", (const char*)data.paramData);
779 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(36000),
780 strLog);
781 }
782
783 if (bReopenConnection)
784 {
785 // Reopen the connection asynchronously. Otherwise a deadlock may occur.
786 // Reconnect means destruction and recreation of our libcec instance, but libcec
787 // calls this callback function synchronously and must not be destroyed meanwhile.
788 adapter->ReopenConnection(true);
789 }
790 }
791
792 void CPeripheralCecAdapter::CecKeyPress(void* cbParam, const cec_keypress* key)
793 {
794 CPeripheralCecAdapter* adapter = static_cast<CPeripheralCecAdapter*>(cbParam);
795 if (!!adapter)
796 adapter->PushCecKeypress(*key);
797 }
798
799 void CPeripheralCecAdapter::GetNextKey(void)
800 {
801 std::unique_lock<CCriticalSection> lock(m_critSection);
802 m_bHasButton = false;
803 if (m_bIsReady)
804 {
805 std::vector<CecButtonPress>::iterator it = m_buttonQueue.begin();
806 if (it != m_buttonQueue.end())
807 {
808 m_currentButton = (*it);
809 m_buttonQueue.erase(it);
810 m_bHasButton = true;
811 }
812 }
813 }
814
815 void CPeripheralCecAdapter::PushCecKeypress(const CecButtonPress& key)
816 {
817 CLog::Log(LOGDEBUG, "{} - received key {:2x} duration {}", __FUNCTION__, key.iButton,
818 key.iDuration);
819
820 std::unique_lock<CCriticalSection> lock(m_critSection);
821 // avoid the queue getting too long
822 if (m_configuration.iButtonRepeatRateMs && m_buttonQueue.size() > 5)
823 return;
824 if (m_configuration.iButtonRepeatRateMs == 0 && key.iDuration > 0)
825 {
826 if (m_currentButton.iButton == key.iButton && m_currentButton.iDuration == 0)
827 {
828 // update the duration
829 if (m_bHasButton)
830 m_currentButton.iDuration = key.iDuration;
831 // ignore this one, since it's already been handled by xbmc
832 return;
833 }
834 // if we received a keypress with a duration set, try to find the same one without a duration
835 // set, and replace it
836 for (std::vector<CecButtonPress>::reverse_iterator it = m_buttonQueue.rbegin();
837 it != m_buttonQueue.rend(); ++it)
838 {
839 if ((*it).iButton == key.iButton)
840 {
841 if ((*it).iDuration == 0)
842 {
843 // replace this entry
844 (*it).iDuration = key.iDuration;
845 return;
846 }
847 // add a new entry
848 break;
849 }
850 }
851 }
852
853 m_buttonQueue.push_back(key);
854 }
855
856 void CPeripheralCecAdapter::PushCecKeypress(const cec_keypress& key)
857 {
858 CecButtonPress xbmcKey;
859 xbmcKey.iDuration = key.duration;
860
861 switch (key.keycode)
862 {
863 case CEC_USER_CONTROL_CODE_SELECT:
864 xbmcKey.iButton = XINPUT_IR_REMOTE_SELECT;
865 PushCecKeypress(xbmcKey);
866 break;
867 case CEC_USER_CONTROL_CODE_UP:
868 xbmcKey.iButton = XINPUT_IR_REMOTE_UP;
869 PushCecKeypress(xbmcKey);
870 break;
871 case CEC_USER_CONTROL_CODE_DOWN:
872 xbmcKey.iButton = XINPUT_IR_REMOTE_DOWN;
873 PushCecKeypress(xbmcKey);
874 break;
875 case CEC_USER_CONTROL_CODE_LEFT:
876 xbmcKey.iButton = XINPUT_IR_REMOTE_LEFT;
877 PushCecKeypress(xbmcKey);
878 break;
879 case CEC_USER_CONTROL_CODE_LEFT_UP:
880 xbmcKey.iButton = XINPUT_IR_REMOTE_LEFT;
881 PushCecKeypress(xbmcKey);
882 xbmcKey.iButton = XINPUT_IR_REMOTE_UP;
883 PushCecKeypress(xbmcKey);
884 break;
885 case CEC_USER_CONTROL_CODE_LEFT_DOWN:
886 xbmcKey.iButton = XINPUT_IR_REMOTE_LEFT;
887 PushCecKeypress(xbmcKey);
888 xbmcKey.iButton = XINPUT_IR_REMOTE_DOWN;
889 PushCecKeypress(xbmcKey);
890 break;
891 case CEC_USER_CONTROL_CODE_RIGHT:
892 xbmcKey.iButton = XINPUT_IR_REMOTE_RIGHT;
893 PushCecKeypress(xbmcKey);
894 break;
895 case CEC_USER_CONTROL_CODE_RIGHT_UP:
896 xbmcKey.iButton = XINPUT_IR_REMOTE_RIGHT;
897 PushCecKeypress(xbmcKey);
898 xbmcKey.iButton = XINPUT_IR_REMOTE_UP;
899 PushCecKeypress(xbmcKey);
900 break;
901 case CEC_USER_CONTROL_CODE_RIGHT_DOWN:
902 xbmcKey.iButton = XINPUT_IR_REMOTE_RIGHT;
903 PushCecKeypress(xbmcKey);
904 xbmcKey.iButton = XINPUT_IR_REMOTE_DOWN;
905 PushCecKeypress(xbmcKey);
906 break;
907 case CEC_USER_CONTROL_CODE_SETUP_MENU:
908 xbmcKey.iButton = XINPUT_IR_REMOTE_TITLE;
909 PushCecKeypress(xbmcKey);
910 break;
911 case CEC_USER_CONTROL_CODE_CONTENTS_MENU:
912 xbmcKey.iButton = XINPUT_IR_REMOTE_CONTENTS_MENU;
913 PushCecKeypress(xbmcKey);
914 break;
915 case CEC_USER_CONTROL_CODE_ROOT_MENU:
916 xbmcKey.iButton = XINPUT_IR_REMOTE_ROOT_MENU;
917 PushCecKeypress(xbmcKey);
918 break;
919 case CEC_USER_CONTROL_CODE_TOP_MENU:
920 xbmcKey.iButton = XINPUT_IR_REMOTE_TOP_MENU;
921 PushCecKeypress(xbmcKey);
922 break;
923 case CEC_USER_CONTROL_CODE_DVD_MENU:
924 xbmcKey.iButton = XINPUT_IR_REMOTE_DVD_MENU;
925 PushCecKeypress(xbmcKey);
926 break;
927 case CEC_USER_CONTROL_CODE_FAVORITE_MENU:
928 xbmcKey.iButton = XINPUT_IR_REMOTE_MENU;
929 PushCecKeypress(xbmcKey);
930 break;
931 case CEC_USER_CONTROL_CODE_EXIT:
932 xbmcKey.iButton = XINPUT_IR_REMOTE_BACK;
933 PushCecKeypress(xbmcKey);
934 break;
935 case CEC_USER_CONTROL_CODE_ENTER:
936 xbmcKey.iButton = XINPUT_IR_REMOTE_ENTER;
937 PushCecKeypress(xbmcKey);
938 break;
939 case CEC_USER_CONTROL_CODE_CHANNEL_DOWN:
940 xbmcKey.iButton = XINPUT_IR_REMOTE_CHANNEL_MINUS;
941 PushCecKeypress(xbmcKey);
942 break;
943 case CEC_USER_CONTROL_CODE_CHANNEL_UP:
944 xbmcKey.iButton = XINPUT_IR_REMOTE_CHANNEL_PLUS;
945 PushCecKeypress(xbmcKey);
946 break;
947 case CEC_USER_CONTROL_CODE_PREVIOUS_CHANNEL:
948 xbmcKey.iButton = XINPUT_IR_REMOTE_TELETEXT;
949 PushCecKeypress(xbmcKey);
950 break;
951 case CEC_USER_CONTROL_CODE_SOUND_SELECT:
952 xbmcKey.iButton = XINPUT_IR_REMOTE_LANGUAGE;
953 PushCecKeypress(xbmcKey);
954 break;
955 case CEC_USER_CONTROL_CODE_POWER:
956 case CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION:
957 case CEC_USER_CONTROL_CODE_POWER_OFF_FUNCTION:
958 xbmcKey.iButton = XINPUT_IR_REMOTE_POWER;
959 PushCecKeypress(xbmcKey);
960 break;
961 case CEC_USER_CONTROL_CODE_VOLUME_UP:
962 xbmcKey.iButton = XINPUT_IR_REMOTE_VOLUME_PLUS;
963 PushCecKeypress(xbmcKey);
964 break;
965 case CEC_USER_CONTROL_CODE_VOLUME_DOWN:
966 xbmcKey.iButton = XINPUT_IR_REMOTE_VOLUME_MINUS;
967 PushCecKeypress(xbmcKey);
968 break;
969 case CEC_USER_CONTROL_CODE_MUTE:
970 case CEC_USER_CONTROL_CODE_MUTE_FUNCTION:
971 case CEC_USER_CONTROL_CODE_RESTORE_VOLUME_FUNCTION:
972 xbmcKey.iButton = XINPUT_IR_REMOTE_MUTE;
973 PushCecKeypress(xbmcKey);
974 break;
975 case CEC_USER_CONTROL_CODE_PLAY:
976 xbmcKey.iButton = XINPUT_IR_REMOTE_PLAY;
977 PushCecKeypress(xbmcKey);
978 break;
979 case CEC_USER_CONTROL_CODE_STOP:
980 xbmcKey.iButton = XINPUT_IR_REMOTE_STOP;
981 PushCecKeypress(xbmcKey);
982 break;
983 case CEC_USER_CONTROL_CODE_PAUSE:
984 xbmcKey.iButton = XINPUT_IR_REMOTE_PAUSE;
985 PushCecKeypress(xbmcKey);
986 break;
987 case CEC_USER_CONTROL_CODE_REWIND:
988 xbmcKey.iButton = XINPUT_IR_REMOTE_REVERSE;
989 PushCecKeypress(xbmcKey);
990 break;
991 case CEC_USER_CONTROL_CODE_FAST_FORWARD:
992 xbmcKey.iButton = XINPUT_IR_REMOTE_FORWARD;
993 PushCecKeypress(xbmcKey);
994 break;
995 case CEC_USER_CONTROL_CODE_NUMBER0:
996 xbmcKey.iButton = XINPUT_IR_REMOTE_0;
997 PushCecKeypress(xbmcKey);
998 break;
999 case CEC_USER_CONTROL_CODE_NUMBER1:
1000 xbmcKey.iButton = XINPUT_IR_REMOTE_1;
1001 PushCecKeypress(xbmcKey);
1002 break;
1003 case CEC_USER_CONTROL_CODE_NUMBER2:
1004 xbmcKey.iButton = XINPUT_IR_REMOTE_2;
1005 PushCecKeypress(xbmcKey);
1006 break;
1007 case CEC_USER_CONTROL_CODE_NUMBER3:
1008 xbmcKey.iButton = XINPUT_IR_REMOTE_3;
1009 PushCecKeypress(xbmcKey);
1010 break;
1011 case CEC_USER_CONTROL_CODE_NUMBER4:
1012 xbmcKey.iButton = XINPUT_IR_REMOTE_4;
1013 PushCecKeypress(xbmcKey);
1014 break;
1015 case CEC_USER_CONTROL_CODE_NUMBER5:
1016 xbmcKey.iButton = XINPUT_IR_REMOTE_5;
1017 PushCecKeypress(xbmcKey);
1018 break;
1019 case CEC_USER_CONTROL_CODE_NUMBER6:
1020 xbmcKey.iButton = XINPUT_IR_REMOTE_6;
1021 PushCecKeypress(xbmcKey);
1022 break;
1023 case CEC_USER_CONTROL_CODE_NUMBER7:
1024 xbmcKey.iButton = XINPUT_IR_REMOTE_7;
1025 PushCecKeypress(xbmcKey);
1026 break;
1027 case CEC_USER_CONTROL_CODE_NUMBER8:
1028 xbmcKey.iButton = XINPUT_IR_REMOTE_8;
1029 PushCecKeypress(xbmcKey);
1030 break;
1031 case CEC_USER_CONTROL_CODE_NUMBER9:
1032 xbmcKey.iButton = XINPUT_IR_REMOTE_9;
1033 PushCecKeypress(xbmcKey);
1034 break;
1035 case CEC_USER_CONTROL_CODE_RECORD:
1036 xbmcKey.iButton = XINPUT_IR_REMOTE_RECORD;
1037 PushCecKeypress(xbmcKey);
1038 break;
1039 case CEC_USER_CONTROL_CODE_CLEAR:
1040 xbmcKey.iButton = XINPUT_IR_REMOTE_CLEAR;
1041 PushCecKeypress(xbmcKey);
1042 break;
1043 case CEC_USER_CONTROL_CODE_DISPLAY_INFORMATION:
1044 xbmcKey.iButton = XINPUT_IR_REMOTE_INFO;
1045 PushCecKeypress(xbmcKey);
1046 break;
1047 case CEC_USER_CONTROL_CODE_PAGE_UP:
1048 xbmcKey.iButton = XINPUT_IR_REMOTE_CHANNEL_PLUS;
1049 PushCecKeypress(xbmcKey);
1050 break;
1051 case CEC_USER_CONTROL_CODE_PAGE_DOWN:
1052 xbmcKey.iButton = XINPUT_IR_REMOTE_CHANNEL_MINUS;
1053 PushCecKeypress(xbmcKey);
1054 break;
1055 case CEC_USER_CONTROL_CODE_FORWARD:
1056 xbmcKey.iButton = XINPUT_IR_REMOTE_SKIP_PLUS;
1057 PushCecKeypress(xbmcKey);
1058 break;
1059 case CEC_USER_CONTROL_CODE_BACKWARD:
1060 xbmcKey.iButton = XINPUT_IR_REMOTE_SKIP_MINUS;
1061 PushCecKeypress(xbmcKey);
1062 break;
1063 case CEC_USER_CONTROL_CODE_F1_BLUE:
1064 xbmcKey.iButton = XINPUT_IR_REMOTE_BLUE;
1065 PushCecKeypress(xbmcKey);
1066 break;
1067 case CEC_USER_CONTROL_CODE_F2_RED:
1068 xbmcKey.iButton = XINPUT_IR_REMOTE_RED;
1069 PushCecKeypress(xbmcKey);
1070 break;
1071 case CEC_USER_CONTROL_CODE_F3_GREEN:
1072 xbmcKey.iButton = XINPUT_IR_REMOTE_GREEN;
1073 PushCecKeypress(xbmcKey);
1074 break;
1075 case CEC_USER_CONTROL_CODE_F4_YELLOW:
1076 xbmcKey.iButton = XINPUT_IR_REMOTE_YELLOW;
1077 PushCecKeypress(xbmcKey);
1078 break;
1079 case CEC_USER_CONTROL_CODE_ELECTRONIC_PROGRAM_GUIDE:
1080 xbmcKey.iButton = XINPUT_IR_REMOTE_GUIDE;
1081 PushCecKeypress(xbmcKey);
1082 break;
1083 case CEC_USER_CONTROL_CODE_AN_CHANNELS_LIST:
1084 xbmcKey.iButton = XINPUT_IR_REMOTE_LIVE_TV;
1085 PushCecKeypress(xbmcKey);
1086 break;
1087 case CEC_USER_CONTROL_CODE_NEXT_FAVORITE:
1088 case CEC_USER_CONTROL_CODE_DOT:
1089 case CEC_USER_CONTROL_CODE_AN_RETURN:
1090 xbmcKey.iButton = XINPUT_IR_REMOTE_TITLE; // context menu
1091 PushCecKeypress(xbmcKey);
1092 break;
1093 case CEC_USER_CONTROL_CODE_DATA:
1094 xbmcKey.iButton = XINPUT_IR_REMOTE_TELETEXT;
1095 PushCecKeypress(xbmcKey);
1096 break;
1097 case CEC_USER_CONTROL_CODE_SUB_PICTURE:
1098 xbmcKey.iButton = XINPUT_IR_REMOTE_SUBTITLE;
1099 PushCecKeypress(xbmcKey);
1100 break;
1101 case CEC_USER_CONTROL_CODE_EJECT:
1102 xbmcKey.iButton = XINPUT_IR_REMOTE_EJECT;
1103 PushCecKeypress(xbmcKey);
1104 break;
1105 case CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION:
1106 case CEC_USER_CONTROL_CODE_INPUT_SELECT:
1107 case CEC_USER_CONTROL_CODE_INITIAL_CONFIGURATION:
1108 case CEC_USER_CONTROL_CODE_HELP:
1109 case CEC_USER_CONTROL_CODE_STOP_RECORD:
1110 case CEC_USER_CONTROL_CODE_PAUSE_RECORD:
1111 case CEC_USER_CONTROL_CODE_ANGLE:
1112 case CEC_USER_CONTROL_CODE_VIDEO_ON_DEMAND:
1113 case CEC_USER_CONTROL_CODE_TIMER_PROGRAMMING:
1114 case CEC_USER_CONTROL_CODE_PLAY_FUNCTION:
1115 case CEC_USER_CONTROL_CODE_PAUSE_PLAY_FUNCTION:
1116 case CEC_USER_CONTROL_CODE_RECORD_FUNCTION:
1117 case CEC_USER_CONTROL_CODE_PAUSE_RECORD_FUNCTION:
1118 case CEC_USER_CONTROL_CODE_STOP_FUNCTION:
1119 case CEC_USER_CONTROL_CODE_TUNE_FUNCTION:
1120 case CEC_USER_CONTROL_CODE_SELECT_MEDIA_FUNCTION:
1121 case CEC_USER_CONTROL_CODE_SELECT_AV_INPUT_FUNCTION:
1122 case CEC_USER_CONTROL_CODE_SELECT_AUDIO_INPUT_FUNCTION:
1123 case CEC_USER_CONTROL_CODE_F5:
1124 case CEC_USER_CONTROL_CODE_NUMBER_ENTRY_MODE:
1125 case CEC_USER_CONTROL_CODE_NUMBER11:
1126 case CEC_USER_CONTROL_CODE_NUMBER12:
1127 case CEC_USER_CONTROL_CODE_SELECT_BROADCAST_TYPE:
1128 case CEC_USER_CONTROL_CODE_SELECT_SOUND_PRESENTATION:
1129 case CEC_USER_CONTROL_CODE_UNKNOWN:
1130 default:
1131 break;
1132 }
1133 }
1134
1135 int CPeripheralCecAdapter::GetButton(void)
1136 {
1137 std::unique_lock<CCriticalSection> lock(m_critSection);
1138 if (!m_bHasButton)
1139 GetNextKey();
1140
1141 return m_bHasButton ? m_currentButton.iButton : 0;
1142 }
1143
1144 unsigned int CPeripheralCecAdapter::GetHoldTime(void)
1145 {
1146 std::unique_lock<CCriticalSection> lock(m_critSection);
1147 if (!m_bHasButton)
1148 GetNextKey();
1149
1150 return m_bHasButton ? m_currentButton.iDuration : 0;
1151 }
1152
1153 void CPeripheralCecAdapter::ResetButton(void)
1154 {
1155 std::unique_lock<CCriticalSection> lock(m_critSection);
1156 m_bHasButton = false;
1157
1158 // wait for the key release if the duration isn't 0
1159 if (m_currentButton.iDuration > 0)
1160 {
1161 m_currentButton.iButton = 0;
1162 m_currentButton.iDuration = 0;
1163 }
1164 }
1165
1166 void CPeripheralCecAdapter::OnSettingChanged(const std::string& strChangedSetting)
1167 {
1168 if (StringUtils::EqualsNoCase(strChangedSetting, "enabled"))
1169 {
1170 bool bEnabled(GetSettingBool("enabled"));
1171 if (!bEnabled && IsRunning())
1172 {
1173 CLog::Log(LOGDEBUG, "{} - closing the CEC connection", __FUNCTION__);
1174 StopThread(true);
1175 }
1176 else if (bEnabled && !IsRunning())
1177 {
1178 CLog::Log(LOGDEBUG, "{} - starting the CEC connection", __FUNCTION__);
1179 SetConfigurationFromSettings();
1180 InitialiseFeature(FEATURE_CEC);
1181 }
1182 }
1183 else if (IsRunning())
1184 {
1185 if (m_queryThread->IsRunning())
1186 {
1187 CLog::Log(LOGDEBUG, "{} - sending the updated configuration to libCEC", __FUNCTION__);
1188 SetConfigurationFromSettings();
1189 m_queryThread->UpdateConfiguration(&m_configuration);
1190 }
1191 }
1192 else
1193 {
1194 CLog::Log(LOGDEBUG, "{} - restarting the CEC connection", __FUNCTION__);
1195 SetConfigurationFromSettings();
1196 InitialiseFeature(FEATURE_CEC);
1197 }
1198 }
1199
1200 void CPeripheralCecAdapter::CecSourceActivated(void* cbParam,
1201 const CEC::cec_logical_address address,
1202 const uint8_t activated)
1203 {
1204 CPeripheralCecAdapter* adapter = static_cast<CPeripheralCecAdapter*>(cbParam);
1205 if (!adapter)
1206 return;
1207
1208 // wake up the screensaver, so the user doesn't switch to a black screen
1209 if (activated == 1)
1210 {
1211 auto& components = CServiceBroker::GetAppComponents();
1212 const auto appPower = components.GetComponent<CApplicationPowerHandling>();
1213 appPower->WakeUpScreenSaverAndDPMS();
1214 }
1215
1216 if (adapter->GetSettingInt("pause_or_stop_playback_on_deactivate") != LOCALISED_ID_NONE)
1217 {
1218 bool bShowingSlideshow =
1219 (CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow() == WINDOW_SLIDESHOW);
1220 CGUIWindowSlideShow* pSlideShow =
1221 bShowingSlideshow
1222 ? CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIWindowSlideShow>(
1223 WINDOW_SLIDESHOW)
1224 : NULL;
1225
1226 const auto& components = CServiceBroker::GetAppComponents();
1227 const auto appPlayer = components.GetComponent<CApplicationPlayer>();
1228
1229 bool bPlayingAndDeactivated = activated == 0 && ((pSlideShow && pSlideShow->IsPlaying()) ||
1230 !appPlayer->IsPausedPlayback());
1231 bool bPausedAndActivated =
1232 activated == 1 && adapter->m_bPlaybackPaused &&
1233 ((pSlideShow && pSlideShow->IsPaused()) || (appPlayer && appPlayer->IsPausedPlayback()));
1234 if (bPlayingAndDeactivated)
1235 adapter->m_bPlaybackPaused = true;
1236 else if (bPausedAndActivated)
1237 adapter->m_bPlaybackPaused = false;
1238
1239 if ((bPlayingAndDeactivated || bPausedAndActivated) &&
1240 adapter->GetSettingInt("pause_or_stop_playback_on_deactivate") == LOCALISED_ID_PAUSE)
1241 {
1242 if (pSlideShow)
1243 // pause/resume slideshow
1244 pSlideShow->OnAction(CAction(ACTION_PAUSE));
1245 else
1246 // pause/resume player
1247 CServiceBroker::GetAppMessenger()->SendMsg(TMSG_MEDIA_PAUSE);
1248 }
1249 else if (bPlayingAndDeactivated &&
1250 adapter->GetSettingInt("pause_or_stop_playback_on_deactivate") == LOCALISED_ID_STOP)
1251 {
1252 if (pSlideShow)
1253 pSlideShow->OnAction(CAction(ACTION_STOP));
1254 else
1255 CServiceBroker::GetAppMessenger()->SendMsg(TMSG_MEDIA_STOP);
1256 }
1257 }
1258 }
1259
1260 void CPeripheralCecAdapter::CecLogMessage(void* cbParam, const cec_log_message* message)
1261 {
1262 CPeripheralCecAdapter* adapter = static_cast<CPeripheralCecAdapter*>(cbParam);
1263 if (!adapter)
1264 return;
1265
1266 int iLevel = -1;
1267 switch (message->level)
1268 {
1269 case CEC_LOG_ERROR:
1270 iLevel = LOGERROR;
1271 break;
1272 case CEC_LOG_WARNING:
1273 iLevel = LOGWARNING;
1274 break;
1275 case CEC_LOG_NOTICE:
1276 iLevel = LOGDEBUG;
1277 break;
1278 case CEC_LOG_TRAFFIC:
1279 case CEC_LOG_DEBUG:
1280 iLevel = LOGDEBUG;
1281 break;
1282 default:
1283 break;
1284 }
1285
1286 if (iLevel >= CEC_LOG_NOTICE ||
1287 (iLevel >= 0 && CServiceBroker::GetLogging().IsLogLevelLogged(LOGDEBUG)))
1288 CLog::Log(iLevel, LOGCEC, "{} - {}", __FUNCTION__, message->message);
1289 }
1290
1291 void CPeripheralCecAdapter::SetConfigurationFromLibCEC(const CEC::libcec_configuration& config)
1292 {
1293 bool bChanged(false);
1294
1295 // set the primary device type
1296 m_configuration.deviceTypes.Clear();
1297 m_configuration.deviceTypes.Add(config.deviceTypes[0]);
1298
1299 // hide the "connected device" and "hdmi port number" settings when the PA was autodetected
1300 bool bPAAutoDetected(config.bAutodetectAddress == 1);
1301
1302 SetSettingVisible("connected_device", !bPAAutoDetected);
1303 SetSettingVisible("cec_hdmi_port", !bPAAutoDetected);
1304
1305 // set the connected device
1306 m_configuration.baseDevice = config.baseDevice;
1307 bChanged |=
1308 SetSetting("connected_device",
1309 config.baseDevice == CECDEVICE_AUDIOSYSTEM ? LOCALISED_ID_AVR : LOCALISED_ID_TV);
1310
1311 // set the HDMI port number
1312 m_configuration.iHDMIPort = config.iHDMIPort;
1313 bChanged |= SetSetting("cec_hdmi_port", config.iHDMIPort);
1314
1315 // set the physical address, when baseDevice or iHDMIPort are not set
1316 std::string strPhysicalAddress("0");
1317 if (!bPAAutoDetected && (m_configuration.baseDevice == CECDEVICE_UNKNOWN ||
1318 m_configuration.iHDMIPort < CEC_MIN_HDMI_PORTNUMBER ||
1319 m_configuration.iHDMIPort > CEC_MAX_HDMI_PORTNUMBER))
1320 {
1321 m_configuration.iPhysicalAddress = config.iPhysicalAddress;
1322 strPhysicalAddress = StringUtils::Format("{:x}", config.iPhysicalAddress);
1323 }
1324 bChanged |= SetSetting("physical_address", strPhysicalAddress);
1325
1326 // set the devices to wake when starting
1327 m_configuration.wakeDevices = config.wakeDevices;
1328 bChanged |= WriteLogicalAddresses(config.wakeDevices, "wake_devices", "wake_devices_advanced");
1329
1330 // set the devices to power off when stopping
1331 m_configuration.powerOffDevices = config.powerOffDevices;
1332 bChanged |=
1333 WriteLogicalAddresses(config.powerOffDevices, "standby_devices", "standby_devices_advanced");
1334
1335 // set the boolean settings
1336 m_configuration.bActivateSource = config.bActivateSource;
1337 bChanged |= SetSetting("activate_source", m_configuration.bActivateSource == 1);
1338
1339 m_configuration.iDoubleTapTimeoutMs = config.iDoubleTapTimeoutMs;
1340 bChanged |= SetSetting("double_tap_timeout_ms", (int)m_configuration.iDoubleTapTimeoutMs);
1341
1342 m_configuration.iButtonRepeatRateMs = config.iButtonRepeatRateMs;
1343 bChanged |= SetSetting("button_repeat_rate_ms", (int)m_configuration.iButtonRepeatRateMs);
1344
1345 m_configuration.iButtonReleaseDelayMs = config.iButtonReleaseDelayMs;
1346 bChanged |= SetSetting("button_release_delay_ms", (int)m_configuration.iButtonReleaseDelayMs);
1347
1348 m_configuration.bPowerOffOnStandby = config.bPowerOffOnStandby;
1349
1350 m_configuration.iFirmwareVersion = config.iFirmwareVersion;
1351
1352 memcpy(m_configuration.strDeviceLanguage, config.strDeviceLanguage, 3);
1353 m_configuration.iFirmwareBuildDate = config.iFirmwareBuildDate;
1354
1355 SetVersionInfo(m_configuration);
1356
1357 if (bChanged)
1358 CLog::Log(LOGDEBUG, "SetConfigurationFromLibCEC - settings updated by libCEC");
1359 }
1360
1361 void CPeripheralCecAdapter::SetConfigurationFromSettings(void)
1362 {
1363 // client version matches the version of libCEC that we originally used the API from
1364 m_configuration.clientVersion = LIBCEC_VERSION_TO_UINT(4, 0, 0);
1365
1366 // device name 'XBMC'
1367 snprintf(m_configuration.strDeviceName, 13, "%s", GetSettingString("device_name").c_str());
1368
1369 // set the primary device type
1370 m_configuration.deviceTypes.Clear();
1371 switch (GetSettingInt("device_type"))
1372 {
1373 case LOCALISED_ID_PLAYBACK_DEVICE:
1374 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
1375 break;
1376 case LOCALISED_ID_TUNER_DEVICE:
1377 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_TUNER);
1378 break;
1379 case LOCALISED_ID_RECORDING_DEVICE:
1380 default:
1381 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
1382 break;
1383 }
1384
1385 // always try to autodetect the address.
1386 // when the firmware supports this, it will override the physical address, connected device and
1387 // hdmi port settings
1388 m_configuration.bAutodetectAddress = CEC_DEFAULT_SETTING_AUTODETECT_ADDRESS;
1389
1390 // set the physical address
1391 // when set, it will override the connected device and hdmi port settings
1392 std::string strPhysicalAddress = GetSettingString("physical_address");
1393 int iPhysicalAddress;
1394 if (sscanf(strPhysicalAddress.c_str(), "%x", &iPhysicalAddress) &&
1395 iPhysicalAddress >= CEC_PHYSICAL_ADDRESS_TV && iPhysicalAddress <= CEC_MAX_PHYSICAL_ADDRESS)
1396 m_configuration.iPhysicalAddress = iPhysicalAddress;
1397 else
1398 m_configuration.iPhysicalAddress = CEC_PHYSICAL_ADDRESS_TV;
1399
1400 // set the connected device
1401 int iConnectedDevice = GetSettingInt("connected_device");
1402 if (iConnectedDevice == LOCALISED_ID_AVR)
1403 m_configuration.baseDevice = CECDEVICE_AUDIOSYSTEM;
1404 else if (iConnectedDevice == LOCALISED_ID_TV)
1405 m_configuration.baseDevice = CECDEVICE_TV;
1406
1407 // set the HDMI port number
1408 int iHDMIPort = GetSettingInt("cec_hdmi_port");
1409 if (iHDMIPort >= CEC_MIN_HDMI_PORTNUMBER && iHDMIPort <= CEC_MAX_HDMI_PORTNUMBER)
1410 m_configuration.iHDMIPort = iHDMIPort;
1411
1412 // set the tv vendor override
1413 int iVendor = GetSettingInt("tv_vendor");
1414 if (iVendor >= CEC_MIN_VENDORID && iVendor <= CEC_MAX_VENDORID)
1415 m_configuration.tvVendor = iVendor;
1416
1417 // read the devices to wake when starting
1418 std::string strWakeDevices = GetSettingString("wake_devices_advanced");
1419 StringUtils::Trim(strWakeDevices);
1420 m_configuration.wakeDevices.Clear();
1421 if (!strWakeDevices.empty())
1422 ReadLogicalAddresses(strWakeDevices, m_configuration.wakeDevices);
1423 else
1424 ReadLogicalAddresses(GetSettingInt("wake_devices"), m_configuration.wakeDevices);
1425
1426 // read the devices to power off when stopping
1427 std::string strStandbyDevices = GetSettingString("standby_devices_advanced");
1428 StringUtils::Trim(strStandbyDevices);
1429 m_configuration.powerOffDevices.Clear();
1430 if (!strStandbyDevices.empty())
1431 ReadLogicalAddresses(strStandbyDevices, m_configuration.powerOffDevices);
1432 else
1433 ReadLogicalAddresses(GetSettingInt("standby_devices"), m_configuration.powerOffDevices);
1434
1435 // read the boolean settings
1436 m_bUseTVMenuLanguage = GetSettingBool("use_tv_menu_language");
1437 m_configuration.bActivateSource = GetSettingBool("activate_source") ? 1 : 0;
1438 m_bPowerOffScreensaver = GetSettingBool("cec_standby_screensaver");
1439 m_bPowerOnScreensaver = GetSettingBool("cec_wake_screensaver");
1440 m_bSendInactiveSource = GetSettingBool("send_inactive_source");
1441 m_configuration.bAutoWakeAVR = GetSettingBool("power_avr_on_as") ? 1 : 0;
1442
1443 // read the mutually exclusive boolean settings
1444 int iStandbyAction(GetSettingInt("standby_pc_on_tv_standby"));
1445 m_configuration.bPowerOffOnStandby =
1446 (iStandbyAction == LOCALISED_ID_SUSPEND || iStandbyAction == LOCALISED_ID_HIBERNATE) ? 1 : 0;
1447 m_bShutdownOnStandby = iStandbyAction == LOCALISED_ID_POWEROFF;
1448
1449 // double tap prevention timeout in ms
1450 m_configuration.iDoubleTapTimeoutMs = GetSettingInt("double_tap_timeout_ms");
1451 m_configuration.iButtonRepeatRateMs = GetSettingInt("button_repeat_rate_ms");
1452 m_configuration.iButtonReleaseDelayMs = GetSettingInt("button_release_delay_ms");
1453
1454 if (GetSettingBool("pause_playback_on_deactivate"))
1455 {
1456 SetSetting("pause_or_stop_playback_on_deactivate", LOCALISED_ID_PAUSE);
1457 SetSetting("pause_playback_on_deactivate", false);
1458 }
1459 }
1460
1461 void CPeripheralCecAdapter::ReadLogicalAddresses(const std::string& strString,
1462 cec_logical_addresses& addresses)
1463 {
1464 for (size_t iPtr = 0; iPtr < strString.size(); iPtr++)
1465 {
1466 std::string strDevice = strString.substr(iPtr, 1);
1467 StringUtils::Trim(strDevice);
1468 if (!strDevice.empty())
1469 {
1470 int iDevice(0);
1471 if (sscanf(strDevice.c_str(), "%x", &iDevice) == 1 && iDevice >= 0 && iDevice <= 0xF)
1472 addresses.Set((cec_logical_address)iDevice);
1473 }
1474 }
1475 }
1476
1477 void CPeripheralCecAdapter::ReadLogicalAddresses(int iLocalisedId, cec_logical_addresses& addresses)
1478 {
1479 addresses.Clear();
1480 switch (iLocalisedId)
1481 {
1482 case LOCALISED_ID_TV:
1483 addresses.Set(CECDEVICE_TV);
1484 break;
1485 case LOCALISED_ID_AVR:
1486 addresses.Set(CECDEVICE_AUDIOSYSTEM);
1487 break;
1488 case LOCALISED_ID_TV_AVR:
1489 addresses.Set(CECDEVICE_TV);
1490 addresses.Set(CECDEVICE_AUDIOSYSTEM);
1491 break;
1492 case LOCALISED_ID_NONE:
1493 default:
1494 break;
1495 }
1496 }
1497
1498 bool CPeripheralCecAdapter::WriteLogicalAddresses(const cec_logical_addresses& addresses,
1499 const std::string& strSettingName,
1500 const std::string& strAdvancedSettingName)
1501 {
1502 bool bChanged(false);
1503
1504 // only update the advanced setting if it was set by the user
1505 if (!GetSettingString(strAdvancedSettingName).empty())
1506 {
1507 std::string strPowerOffDevices;
1508 for (unsigned int iPtr = CECDEVICE_TV; iPtr <= CECDEVICE_BROADCAST; iPtr++)
1509 if (addresses[iPtr])
1510 strPowerOffDevices += StringUtils::Format(" {:X}", iPtr);
1511 StringUtils::Trim(strPowerOffDevices);
1512 bChanged = SetSetting(strAdvancedSettingName, strPowerOffDevices);
1513 }
1514
1515 int iSettingPowerOffDevices = LOCALISED_ID_NONE;
1516 if (addresses[CECDEVICE_TV] && addresses[CECDEVICE_AUDIOSYSTEM])
1517 iSettingPowerOffDevices = LOCALISED_ID_TV_AVR;
1518 else if (addresses[CECDEVICE_TV])
1519 iSettingPowerOffDevices = LOCALISED_ID_TV;
1520 else if (addresses[CECDEVICE_AUDIOSYSTEM])
1521 iSettingPowerOffDevices = LOCALISED_ID_AVR;
1522 return SetSetting(strSettingName, iSettingPowerOffDevices) || bChanged;
1523 }
1524
1525 CPeripheralCecAdapterUpdateThread::CPeripheralCecAdapterUpdateThread(
1526 CPeripheralCecAdapter* adapter, libcec_configuration* configuration)
1527 : CThread("CECAdapterUpdate"), m_adapter(adapter), m_configuration(*configuration)
1528 {
1529 m_nextConfiguration.Clear();
1530 m_event.Reset();
1531 }
1532
1533 CPeripheralCecAdapterUpdateThread::~CPeripheralCecAdapterUpdateThread(void)
1534 {
1535 StopThread(false);
1536 m_event.Set();
1537 StopThread(true);
1538 }
1539
1540 void CPeripheralCecAdapterUpdateThread::Signal(void)
1541 {
1542 m_event.Set();
1543 }
1544
1545 bool CPeripheralCecAdapterUpdateThread::UpdateConfiguration(libcec_configuration* configuration)
1546 {
1547 std::unique_lock<CCriticalSection> lock(m_critSection);
1548 if (!configuration)
1549 return false;
1550
1551 if (m_bIsUpdating)
1552 {
1553 m_bNextConfigurationScheduled = true;
1554 m_nextConfiguration = *configuration;
1555 }
1556 else
1557 {
1558 m_configuration = *configuration;
1559 m_event.Set();
1560 }
1561 return true;
1562 }
1563
1564 bool CPeripheralCecAdapterUpdateThread::WaitReady(void)
1565 {
1566 // don't wait if we're not powering up anything
1567 if (m_configuration.wakeDevices.IsEmpty() && m_configuration.bActivateSource == 0)
1568 return true;
1569
1570 // wait for the TV if we're configured to become the active source.
1571 // wait for the first device in the wake list otherwise.
1572 cec_logical_address waitFor =
1573 (m_configuration.bActivateSource == 1) ? CECDEVICE_TV : m_configuration.wakeDevices.primary;
1574
1575 cec_power_status powerStatus(CEC_POWER_STATUS_UNKNOWN);
1576 bool bContinue(true);
1577 while (bContinue && !m_adapter->m_bStop && !m_bStop && powerStatus != CEC_POWER_STATUS_ON)
1578 {
1579 powerStatus = m_adapter->m_cecAdapter->GetDevicePowerStatus(waitFor);
1580 if (powerStatus != CEC_POWER_STATUS_ON)
1581 bContinue = !m_event.Wait(1000ms);
1582 }
1583
1584 return powerStatus == CEC_POWER_STATUS_ON;
1585 }
1586
1587 void CPeripheralCecAdapterUpdateThread::UpdateMenuLanguage(void)
1588 {
1589 // request the menu language of the TV
1590 if (m_adapter->m_bUseTVMenuLanguage == 1)
1591 {
1592 CLog::Log(LOGDEBUG, "{} - requesting the menu language of the TV", __FUNCTION__);
1593 std::string language(m_adapter->m_cecAdapter->GetDeviceMenuLanguage(CECDEVICE_TV));
1594 m_adapter->SetMenuLanguage(language.c_str());
1595 }
1596 else
1597 {
1598 CLog::Log(LOGDEBUG, "{} - using TV menu language is disabled", __FUNCTION__);
1599 }
1600 }
1601
1602 std::string CPeripheralCecAdapterUpdateThread::UpdateAudioSystemStatus(void)
1603 {
1604 std::string strAmpName;
1605
1606 /* disable the mute setting when an amp is found, because the amp handles the mute setting and
1607 set PCM output to 100% */
1608 if (m_adapter->m_cecAdapter->IsActiveDeviceType(CEC_DEVICE_TYPE_AUDIO_SYSTEM))
1609 {
1610 // request the OSD name of the amp
1611 std::string ampName(m_adapter->m_cecAdapter->GetDeviceOSDName(CECDEVICE_AUDIOSYSTEM));
1612 CLog::Log(LOGDEBUG,
1613 "{} - CEC capable amplifier found ({}). volume will be controlled on the amp",
1614 __FUNCTION__, ampName);
1615 strAmpName += ampName;
1616
1617 // set amp present
1618 m_adapter->SetAudioSystemConnected(true);
1619 auto& components = CServiceBroker::GetAppComponents();
1620 const auto appVolume = components.GetComponent<CApplicationVolumeHandling>();
1621 appVolume->SetMute(false);
1622 appVolume->SetVolume(CApplicationVolumeHandling::VOLUME_MAXIMUM, false);
1623 }
1624 else
1625 {
1626 // set amp present
1627 CLog::Log(LOGDEBUG, "{} - no CEC capable amplifier found", __FUNCTION__);
1628 m_adapter->SetAudioSystemConnected(false);
1629 }
1630
1631 return strAmpName;
1632 }
1633
1634 bool CPeripheralCecAdapterUpdateThread::SetInitialConfiguration(void)
1635 {
1636 // the option to make XBMC the active source is set
1637 if (m_configuration.bActivateSource == 1)
1638 m_adapter->m_cecAdapter->SetActiveSource();
1639
1640 // devices to wake are set
1641 cec_logical_addresses tvOnly;
1642 tvOnly.Clear();
1643 tvOnly.Set(CECDEVICE_TV);
1644 if (!m_configuration.wakeDevices.IsEmpty() &&
1645 (m_configuration.wakeDevices != tvOnly || m_configuration.bActivateSource == 0))
1646 m_adapter->m_cecAdapter->PowerOnDevices(CECDEVICE_BROADCAST);
1647
1648 // wait until devices are powered up
1649 if (!WaitReady())
1650 return false;
1651
1652 UpdateMenuLanguage();
1653
1654 // request the OSD name of the TV
1655 std::string strNotification;
1656 std::string tvName(m_adapter->m_cecAdapter->GetDeviceOSDName(CECDEVICE_TV));
1657 strNotification = StringUtils::Format("{}: {}", g_localizeStrings.Get(36016), tvName);
1658
1659 std::string strAmpName = UpdateAudioSystemStatus();
1660 if (!strAmpName.empty())
1661 strNotification += StringUtils::Format("- {}", strAmpName);
1662
1663 m_adapter->m_bIsReady = true;
1664
1665 // and let the gui know that we're done
1666 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(36000),
1667 strNotification);
1668
1669 std::unique_lock<CCriticalSection> lock(m_critSection);
1670 m_bIsUpdating = false;
1671 return true;
1672 }
1673
1674 bool CPeripheralCecAdapter::IsRunning(void) const
1675 {
1676 std::unique_lock<CCriticalSection> lock(m_critSection);
1677 return m_bIsRunning;
1678 }
1679
1680 void CPeripheralCecAdapterUpdateThread::Process(void)
1681 {
1682 // set the initial configuration
1683 if (!SetInitialConfiguration())
1684 return;
1685
1686 // and wait for updates
1687 bool bUpdate(false);
1688 while (!m_bStop)
1689 {
1690 // update received
1691 if (bUpdate || m_event.Wait(500ms))
1692 {
1693 if (m_bStop)
1694 return;
1695 // set the new configuration
1696 libcec_configuration configuration;
1697 {
1698 std::unique_lock<CCriticalSection> lock(m_critSection);
1699 configuration = m_configuration;
1700 m_bIsUpdating = false;
1701 }
1702
1703 CLog::Log(LOGDEBUG, "{} - updating the configuration", __FUNCTION__);
1704 bool bConfigSet(m_adapter->m_cecAdapter->SetConfiguration(&configuration));
1705 // display message: config updated / failed to update
1706 if (!bConfigSet)
1707 CLog::Log(LOGERROR, "{} - libCEC couldn't set the new configuration", __FUNCTION__);
1708 else
1709 {
1710 UpdateMenuLanguage();
1711 UpdateAudioSystemStatus();
1712 }
1713
1714 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(36000),
1715 g_localizeStrings.Get(bConfigSet ? 36023 : 36024));
1716
1717 {
1718 std::unique_lock<CCriticalSection> lock(m_critSection);
1719 if ((bUpdate = m_bNextConfigurationScheduled) == true)
1720 {
1721 // another update is scheduled
1722 m_bNextConfigurationScheduled = false;
1723 m_configuration = m_nextConfiguration;
1724 }
1725 else
1726 {
1727 // nothing left to do, wait for updates
1728 m_bIsUpdating = false;
1729 m_event.Reset();
1730 }
1731 }
1732 }
1733 }
1734 }
1735
1736 void CPeripheralCecAdapter::OnDeviceRemoved(void)
1737 {
1738 std::unique_lock<CCriticalSection> lock(m_critSection);
1739 m_bDeviceRemoved = true;
1740 }
1741
1742 namespace PERIPHERALS
1743 {
1744
1745 class CPeripheralCecAdapterReopenJob : public CJob
1746 {
1747 public:
1748 CPeripheralCecAdapterReopenJob(CPeripheralCecAdapter* adapter) : m_adapter(adapter) {}
1749 ~CPeripheralCecAdapterReopenJob() override = default;
1750
1751 bool DoWork(void) override { return m_adapter->ReopenConnection(false); }
1752
1753 private:
1754 CPeripheralCecAdapter* m_adapter;
1755 };
1756
1757 }; // namespace PERIPHERALS
1758
1759 bool CPeripheralCecAdapter::ReopenConnection(bool bAsync /* = false */)
1760 {
1761 if (bAsync)
1762 {
1763 CServiceBroker::GetJobManager()->AddJob(new CPeripheralCecAdapterReopenJob(this), nullptr,
1764 CJob::PRIORITY_NORMAL);
1765 return true;
1766 }
1767
1768 // stop running thread
1769 {
1770 std::unique_lock<CCriticalSection> lock(m_critSection);
1771 m_iExitCode = EXITCODE_RESTARTAPP;
1772 CServiceBroker::GetAnnouncementManager()->RemoveAnnouncer(this);
1773 StopThread(false);
1774 }
1775 StopThread();
1776
1777 // reset all members to their defaults
1778 ResetMembers();
1779
1780 // reopen the connection
1781 return InitialiseFeature(FEATURE_CEC);
1782 }
1783
1784 void CPeripheralCecAdapter::ActivateSource(void)
1785 {
1786 std::unique_lock<CCriticalSection> lock(m_critSection);
1787 m_bActiveSourcePending = true;
1788 }
1789
1790 void CPeripheralCecAdapter::ProcessActivateSource(void)
1791 {
1792 bool bActivate(false);
1793
1794 {
1795 std::unique_lock<CCriticalSection> lock(m_critSection);
1796 bActivate = m_bActiveSourcePending;
1797 m_bActiveSourcePending = false;
1798 }
1799
1800 if (bActivate)
1801 m_cecAdapter->SetActiveSource();
1802 }
1803
1804 void CPeripheralCecAdapter::StandbyDevices(void)
1805 {
1806 std::unique_lock<CCriticalSection> lock(m_critSection);
1807 m_bStandbyPending = true;
1808 }
1809
1810 void CPeripheralCecAdapter::ProcessStandbyDevices(void)
1811 {
1812 bool bStandby(false);
1813
1814 {
1815 std::unique_lock<CCriticalSection> lock(m_critSection);
1816 bStandby = m_bStandbyPending;
1817 m_bStandbyPending = false;
1818 if (bStandby)
1819 m_bGoingToStandby = true;
1820 }
1821
1822 if (bStandby)
1823 {
1824 if (!m_configuration.powerOffDevices.IsEmpty())
1825 {
1826 m_standbySent = CDateTime::GetCurrentDateTime();
1827 m_cecAdapter->StandbyDevices(CECDEVICE_BROADCAST);
1828 }
1829 else if (m_bSendInactiveSource == 1)
1830 {
1831 CLog::Log(LOGDEBUG, "{} - sending inactive source commands", __FUNCTION__);
1832 m_cecAdapter->SetInactiveView();
1833 }
1834 }
1835 }
1836
1837 bool CPeripheralCecAdapter::ToggleDeviceState(CecStateChange mode /*= STATE_SWITCH_TOGGLE */,
1838 bool forceType /*= false */)
1839 {
1840 if (!IsRunning())
1841 return false;
1842 if (m_cecAdapter->IsLibCECActiveSource() &&
1843 (mode == STATE_SWITCH_TOGGLE || mode == STATE_STANDBY))
1844 {
1845 CLog::Log(LOGDEBUG, "{} - putting CEC device on standby...", __FUNCTION__);
1846 StandbyDevices();
1847 return false;
1848 }
1849 else if (mode == STATE_SWITCH_TOGGLE || mode == STATE_ACTIVATE_SOURCE)
1850 {
1851 CLog::Log(LOGDEBUG, "{} - waking up CEC device...", __FUNCTION__);
1852 ActivateSource();
1853 return true;
1854 }
1855
1856 return false;
1857 }