/
PeripheralCecAdapter.cpp
1748 lines (1558 loc) · 53.6 KB
/
PeripheralCecAdapter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2005-2012 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "system.h"
#if defined(HAVE_LIBCEC)
#include "PeripheralCecAdapter.h"
#include "input/XBIRRemote.h"
#include "Application.h"
#include "ApplicationMessenger.h"
#include "DynamicDll.h"
#include "threads/SingleLock.h"
#include "dialogs/GUIDialogKaiToast.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/LocalizeStrings.h"
#include "peripherals/Peripherals.h"
#include "peripherals/bus/PeripheralBus.h"
#include "pictures/GUIWindowSlideShow.h"
#include "settings/GUISettings.h"
#include "settings/Settings.h"
#include "utils/log.h"
#include "utils/Variant.h"
#include <libcec/cec.h>
using namespace PERIPHERALS;
using namespace ANNOUNCEMENT;
using namespace CEC;
using namespace std;
#define CEC_LIB_SUPPORTED_VERSION 0x2000
/* time in seconds to ignore standby commands from devices after the screensaver has been activated */
#define SCREENSAVER_TIMEOUT 10
#define VOLUME_CHANGE_TIMEOUT 250
#define VOLUME_REFRESH_TIMEOUT 100
#define LOCALISED_ID_TV 36037
#define LOCALISED_ID_AVR 36038
#define LOCALISED_ID_TV_AVR 36039
#define LOCALISED_ID_NONE 231
/* time in seconds to suppress source activation after receiving OnStop */
#define CEC_SUPPRESS_ACTIVATE_SOURCE_AFTER_ON_STOP 2
class DllLibCECInterface
{
public:
virtual ~DllLibCECInterface() {}
virtual ICECAdapter* CECInitialise(libcec_configuration *configuration)=0;
virtual void* CECDestroy(ICECAdapter *adapter)=0;
};
class DllLibCEC : public DllDynamic, DllLibCECInterface
{
DECLARE_DLL_WRAPPER(DllLibCEC, DLL_PATH_LIBCEC)
DEFINE_METHOD1(ICECAdapter*, CECInitialise, (libcec_configuration *p1))
DEFINE_METHOD1(void* , CECDestroy, (ICECAdapter *p1))
BEGIN_METHOD_RESOLVE()
RESOLVE_METHOD_RENAME(CECInitialise, CECInitialise)
RESOLVE_METHOD_RENAME(CECDestroy, CECDestroy)
END_METHOD_RESOLVE()
};
CPeripheralCecAdapter::CPeripheralCecAdapter(const PeripheralType type, const PeripheralBusType busType, const CStdString &strLocation, const CStdString &strDeviceName, int iVendorId, int iProductId) :
CPeripheralHID(type, busType, strLocation, strDeviceName, iVendorId, iProductId),
CThread("CEC Adapter"),
m_dll(NULL),
m_cecAdapter(NULL)
{
ResetMembers();
m_features.push_back(FEATURE_CEC);
}
CPeripheralCecAdapter::~CPeripheralCecAdapter(void)
{
{
CSingleLock lock(m_critSection);
CAnnouncementManager::RemoveAnnouncer(this);
m_bStop = true;
}
StopThread(true);
if (m_dll && m_cecAdapter)
{
m_dll->CECDestroy(m_cecAdapter);
m_cecAdapter = NULL;
delete m_dll;
m_dll = NULL;
}
}
void CPeripheralCecAdapter::ResetMembers(void)
{
if (m_cecAdapter && m_dll)
m_dll->CECDestroy(m_cecAdapter);
m_cecAdapter = NULL;
delete m_dll;
m_dll = NULL;
m_bStarted = false;
m_bHasButton = false;
m_bIsReady = false;
m_bHasConnectedAudioSystem = false;
m_strMenuLanguage = "???";
m_lastKeypress = 0;
m_lastChange = VOLUME_CHANGE_NONE;
m_iExitCode = 0;
m_bIsMuted = false; // TODO fetch the correct initial value when system audiostatus is implemented in libCEC
m_bGoingToStandby = false;
m_bIsRunning = false;
m_bDeviceRemoved = false;
m_bActiveSourcePending = false;
m_bStandbyPending = false;
m_bActiveSourceBeforeStandby = false;
m_bOnPlayReceived = false;
m_currentButton.iButton = 0;
m_currentButton.iDuration = 0;
m_screensaverLastActivated.SetValid(false);
m_configuration.Clear();
}
void CPeripheralCecAdapter::Announce(AnnouncementFlag flag, const char *sender, const char *message, const CVariant &data)
{
if (flag == System && !strcmp(sender, "xbmc") && !strcmp(message, "OnQuit") && m_bIsReady)
{
CSingleLock lock(m_critSection);
m_iExitCode = (int)data.asInteger(0);
CAnnouncementManager::RemoveAnnouncer(this);
StopThread(false);
}
else if (flag == GUI && !strcmp(sender, "xbmc") && !strcmp(message, "OnScreensaverDeactivated") && m_bIsReady)
{
bool bIgnoreDeactivate(false);
if (data.isBoolean())
{
// don't respond to the deactivation if we are just going to suspend/shutdown anyway
// the tv will not have time to switch on before being told to standby and
// may not action the standby command.
bIgnoreDeactivate = data.asBoolean();
if (bIgnoreDeactivate)
CLog::Log(LOGDEBUG, "%s - ignoring OnScreensaverDeactivated for power action", __FUNCTION__);
}
if (m_configuration.bPowerOffScreensaver == 1 && !bIgnoreDeactivate &&
m_configuration.bActivateSource == 1)
{
ActivateSource();
}
}
else if (flag == GUI && !strcmp(sender, "xbmc") && !strcmp(message, "OnScreensaverActivated") && m_bIsReady)
{
// Don't put devices to standby if application is currently playing
if ((!g_application.IsPlaying() || g_application.IsPaused()) && m_configuration.bPowerOffScreensaver == 1)
{
m_screensaverLastActivated = CDateTime::GetCurrentDateTime();
// only power off when we're the active source
if (m_cecAdapter->IsLibCECActiveSource())
StandbyDevices();
}
}
else if (flag == System && !strcmp(sender, "xbmc") && !strcmp(message, "OnSleep"))
{
// this will also power off devices when we're the active source
{
CSingleLock lock(m_critSection);
m_bGoingToStandby = true;
}
StopThread();
}
else if (flag == System && !strcmp(sender, "xbmc") && !strcmp(message, "OnWake"))
{
CLog::Log(LOGDEBUG, "%s - reconnecting to the CEC adapter after standby mode", __FUNCTION__);
if (ReopenConnection())
{
bool bActivate(false);
{
CSingleLock lock(m_critSection);
bActivate = m_bActiveSourceBeforeStandby;
m_bActiveSourceBeforeStandby = false;
}
if (bActivate)
ActivateSource();
}
}
else if (flag == Player && !strcmp(sender, "xbmc") && !strcmp(message, "OnStop"))
{
CSingleLock lock(m_critSection);
m_preventActivateSourceOnPlay = CDateTime::GetCurrentDateTime();
m_bOnPlayReceived = false;
}
else if (flag == Player && !strcmp(sender, "xbmc") && !strcmp(message, "OnPlay"))
{
// activate the source when playback started, and the option is enabled
bool bActivateSource(false);
{
CSingleLock lock(m_critSection);
bActivateSource = (m_configuration.bActivateSource &&
!m_bOnPlayReceived &&
(!m_preventActivateSourceOnPlay.IsValid() || CDateTime::GetCurrentDateTime() - m_preventActivateSourceOnPlay > CDateTimeSpan(0, 0, 0, CEC_SUPPRESS_ACTIVATE_SOURCE_AFTER_ON_STOP)));
m_bOnPlayReceived = true;
}
if (bActivateSource)
ActivateSource();
}
}
bool CPeripheralCecAdapter::InitialiseFeature(const PeripheralFeature feature)
{
if (feature == FEATURE_CEC && !m_bStarted && GetSettingBool("enabled"))
{
// hide settings that have an override set
if (!GetSettingString("wake_devices_advanced").IsEmpty())
SetSettingVisible("wake_devices", false);
if (!GetSettingString("standby_devices_advanced").IsEmpty())
SetSettingVisible("standby_devices", false);
SetConfigurationFromSettings();
m_callbacks.Clear();
m_callbacks.CBCecLogMessage = &CecLogMessage;
m_callbacks.CBCecKeyPress = &CecKeyPress;
m_callbacks.CBCecCommand = &CecCommand;
m_callbacks.CBCecConfigurationChanged = &CecConfiguration;
m_callbacks.CBCecAlert = &CecAlert;
m_callbacks.CBCecSourceActivated = &CecSourceActivated;
m_configuration.callbackParam = this;
m_configuration.callbacks = &m_callbacks;
m_dll = new DllLibCEC;
if (m_dll->Load() && m_dll->IsLoaded())
m_cecAdapter = m_dll->CECInitialise(&m_configuration);
else
{
// display warning: libCEC could not be loaded
CLog::Log(LOGERROR, "%s", g_localizeStrings.Get(36017).c_str());
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Error, g_localizeStrings.Get(36000), g_localizeStrings.Get(36029));
delete m_dll;
m_dll = NULL;
m_features.clear();
return false;
}
if (m_configuration.serverVersion < CEC_LIB_SUPPORTED_VERSION)
{
/* unsupported libcec version */
CLog::Log(LOGERROR, g_localizeStrings.Get(36040).c_str(), m_cecAdapter ? m_configuration.serverVersion : -1, CEC_LIB_SUPPORTED_VERSION);
// display warning: incompatible libCEC
CStdString strMessage;
strMessage.Format(g_localizeStrings.Get(36040).c_str(), m_cecAdapter ? m_configuration.serverVersion : -1, CEC_LIB_SUPPORTED_VERSION);
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Error, g_localizeStrings.Get(36000), strMessage);
m_bError = true;
if (m_cecAdapter)
m_dll->CECDestroy(m_cecAdapter);
m_cecAdapter = NULL;
m_features.clear();
return false;
}
else
{
CLog::Log(LOGDEBUG, "%s - using libCEC v%s", __FUNCTION__, m_cecAdapter->ToString((cec_server_version)m_configuration.serverVersion));
SetVersionInfo(m_configuration);
}
m_bStarted = true;
Create();
}
return CPeripheral::InitialiseFeature(feature);
}
void CPeripheralCecAdapter::SetVersionInfo(const libcec_configuration &configuration)
{
m_strVersionInfo.Format("libCEC %s - firmware v%d", m_cecAdapter->ToString((cec_server_version)configuration.serverVersion), configuration.iFirmwareVersion);
// append firmware build date
if (configuration.iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN)
{
CDateTime dt((time_t)configuration.iFirmwareBuildDate);
m_strVersionInfo.AppendFormat(" (%s)", dt.GetAsDBDate().c_str());
}
}
CStdString CPeripheralCecAdapter::GetComPort(void)
{
CStdString strPort = GetSettingString("port");
if (strPort.IsEmpty())
{
strPort = m_strFileLocation;
cec_adapter deviceList[10];
TranslateComPort(strPort);
uint8_t iFound = m_cecAdapter->FindAdapters(deviceList, 10, strPort.c_str());
if (iFound <= 0)
{
CLog::Log(LOGWARNING, "%s - no CEC adapters found on %s", __FUNCTION__, strPort.c_str());
// display warning: couldn't set up com port
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Error, g_localizeStrings.Get(36000), g_localizeStrings.Get(36011));
strPort = "";
}
else
{
cec_adapter *dev = &deviceList[0];
if (iFound > 1)
CLog::Log(LOGDEBUG, "%s - multiple com ports found for device. taking the first one", __FUNCTION__);
else
CLog::Log(LOGDEBUG, "%s - autodetect com port '%s'", __FUNCTION__, dev->comm);
strPort = dev->comm;
}
}
return strPort;
}
bool CPeripheralCecAdapter::OpenConnection(void)
{
bool bIsOpen(false);
if (!GetSettingBool("enabled"))
{
CLog::Log(LOGDEBUG, "%s - CEC adapter is disabled in peripheral settings", __FUNCTION__);
m_bStarted = false;
return bIsOpen;
}
CStdString strPort = GetComPort();
if (strPort.empty())
return bIsOpen;
// open the CEC adapter
CLog::Log(LOGDEBUG, "%s - opening a connection to the CEC adapter: %s", __FUNCTION__, strPort.c_str());
// scanning the CEC bus takes about 5 seconds, so display a notification to inform users that we're busy
CStdString strMessage;
strMessage.Format(g_localizeStrings.Get(21336), g_localizeStrings.Get(36000));
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(36000), strMessage);
bool bConnectionFailedDisplayed(false);
while (!m_bStop && !bIsOpen)
{
if ((bIsOpen = m_cecAdapter->Open(strPort.c_str(), 10000)) == false)
{
// display warning: couldn't initialise libCEC
CLog::Log(LOGERROR, "%s - could not opening a connection to the CEC adapter", __FUNCTION__);
if (!bConnectionFailedDisplayed)
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Error, g_localizeStrings.Get(36000), g_localizeStrings.Get(36012));
bConnectionFailedDisplayed = true;
Sleep(10000);
}
}
if (bIsOpen)
{
CLog::Log(LOGDEBUG, "%s - connection to the CEC adapter opened", __FUNCTION__);
// read the configuration
libcec_configuration config;
if (m_cecAdapter->GetCurrentConfiguration(&config))
{
// wake devices
for (uint8_t iDevice = CECDEVICE_TV; iDevice < CECDEVICE_BROADCAST; iDevice++)
{
if ((config.bActivateSource == 0 || iDevice != CECDEVICE_TV) && config.wakeDevices.IsSet((cec_logical_address)iDevice))
m_cecAdapter->PowerOnDevices((cec_logical_address)iDevice);
}
// update the local configuration
CSingleLock lock(m_critSection);
SetConfigurationFromLibCEC(config);
}
}
return bIsOpen;
}
void CPeripheralCecAdapter::Process(void)
{
if (!OpenConnection())
return;
{
CSingleLock lock(m_critSection);
m_iExitCode = EXITCODE_QUIT;
m_bGoingToStandby = false;
m_bIsRunning = true;
m_bActiveSourceBeforeStandby = false;
}
CAnnouncementManager::AddAnnouncer(this);
m_queryThread = new CPeripheralCecAdapterUpdateThread(this, &m_configuration);
m_queryThread->Create(false);
while (!m_bStop)
{
if (!m_bStop)
ProcessVolumeChange();
if (!m_bStop)
ProcessActivateSource();
if (!m_bStop)
ProcessStandbyDevices();
if (!m_bStop)
Sleep(5);
}
delete m_queryThread;
m_queryThread = NULL;
bool bSendStandbyCommands(false);
{
CSingleLock lock(m_critSection);
bSendStandbyCommands = m_iExitCode != EXITCODE_REBOOT &&
m_iExitCode != EXITCODE_RESTARTAPP &&
!m_bDeviceRemoved &&
(!m_bGoingToStandby || GetSettingBool("standby_tv_on_pc_standby")) &&
GetSettingBool("enabled");
if (m_bGoingToStandby)
m_bActiveSourceBeforeStandby = m_cecAdapter->IsLibCECActiveSource();
}
if (bSendStandbyCommands)
{
if (m_cecAdapter->IsLibCECActiveSource())
{
if (!m_configuration.powerOffDevices.IsEmpty())
{
CLog::Log(LOGDEBUG, "%s - sending standby commands", __FUNCTION__);
m_cecAdapter->StandbyDevices();
}
else if (m_configuration.bSendInactiveSource == 1)
{
CLog::Log(LOGDEBUG, "%s - sending inactive source commands", __FUNCTION__);
m_cecAdapter->SetInactiveView();
}
}
else
{
CLog::Log(LOGDEBUG, "%s - XBMC is not the active source, not sending any standby commands", __FUNCTION__);
}
}
m_cecAdapter->Close();
CLog::Log(LOGDEBUG, "%s - CEC adapter processor thread ended", __FUNCTION__);
{
CSingleLock lock(m_critSection);
m_bStarted = false;
m_bIsRunning = false;
}
}
bool CPeripheralCecAdapter::HasConnectedAudioSystem(void)
{
CSingleLock lock(m_critSection);
return m_bHasConnectedAudioSystem;
}
void CPeripheralCecAdapter::SetAudioSystemConnected(bool bSetTo)
{
CSingleLock lock(m_critSection);
m_bHasConnectedAudioSystem = bSetTo;
}
void CPeripheralCecAdapter::ScheduleVolumeUp(void)
{
{
CSingleLock lock(m_critSection);
m_volumeChangeQueue.push(VOLUME_CHANGE_UP);
}
Sleep(5);
}
void CPeripheralCecAdapter::ScheduleVolumeDown(void)
{
{
CSingleLock lock(m_critSection);
m_volumeChangeQueue.push(VOLUME_CHANGE_DOWN);
}
Sleep(5);
}
void CPeripheralCecAdapter::ScheduleMute(void)
{
{
CSingleLock lock(m_critSection);
m_volumeChangeQueue.push(VOLUME_CHANGE_MUTE);
}
Sleep(5);
}
void CPeripheralCecAdapter::ProcessVolumeChange(void)
{
bool bSendRelease(false);
CecVolumeChange pendingVolumeChange = VOLUME_CHANGE_NONE;
{
CSingleLock lock(m_critSection);
if (m_volumeChangeQueue.size() > 0)
{
/* get the first change from the queue */
pendingVolumeChange = m_volumeChangeQueue.front();
m_volumeChangeQueue.pop();
/* remove all dupe entries */
while (m_volumeChangeQueue.size() > 0 && m_volumeChangeQueue.front() == pendingVolumeChange)
m_volumeChangeQueue.pop();
/* send another keypress after VOLUME_REFRESH_TIMEOUT ms */
bool bRefresh(m_lastKeypress + VOLUME_REFRESH_TIMEOUT < XbmcThreads::SystemClockMillis());
/* only send the keypress when it hasn't been sent yet */
if (pendingVolumeChange != m_lastChange)
{
m_lastKeypress = XbmcThreads::SystemClockMillis();
m_lastChange = pendingVolumeChange;
}
else if (bRefresh)
{
m_lastKeypress = XbmcThreads::SystemClockMillis();
pendingVolumeChange = m_lastChange;
}
else
pendingVolumeChange = VOLUME_CHANGE_NONE;
}
else if (m_lastKeypress > 0 && m_lastKeypress + VOLUME_CHANGE_TIMEOUT < XbmcThreads::SystemClockMillis())
{
/* send a key release */
m_lastKeypress = 0;
bSendRelease = true;
m_lastChange = VOLUME_CHANGE_NONE;
}
}
switch (pendingVolumeChange)
{
case VOLUME_CHANGE_UP:
m_cecAdapter->SendKeypress(CECDEVICE_AUDIOSYSTEM, CEC_USER_CONTROL_CODE_VOLUME_UP, false);
break;
case VOLUME_CHANGE_DOWN:
m_cecAdapter->SendKeypress(CECDEVICE_AUDIOSYSTEM, CEC_USER_CONTROL_CODE_VOLUME_DOWN, false);
break;
case VOLUME_CHANGE_MUTE:
m_cecAdapter->SendKeypress(CECDEVICE_AUDIOSYSTEM, CEC_USER_CONTROL_CODE_MUTE, false);
{
CSingleLock lock(m_critSection);
m_bIsMuted = !m_bIsMuted;
}
break;
case VOLUME_CHANGE_NONE:
if (bSendRelease)
m_cecAdapter->SendKeyRelease(CECDEVICE_AUDIOSYSTEM, false);
break;
}
}
void CPeripheralCecAdapter::VolumeUp(void)
{
if (HasConnectedAudioSystem())
{
CSingleLock lock(m_critSection);
m_volumeChangeQueue.push(VOLUME_CHANGE_UP);
}
}
void CPeripheralCecAdapter::VolumeDown(void)
{
if (HasConnectedAudioSystem())
{
CSingleLock lock(m_critSection);
m_volumeChangeQueue.push(VOLUME_CHANGE_DOWN);
}
}
void CPeripheralCecAdapter::Mute(void)
{
if (HasConnectedAudioSystem())
{
CSingleLock lock(m_critSection);
m_volumeChangeQueue.push(VOLUME_CHANGE_MUTE);
}
}
bool CPeripheralCecAdapter::IsMuted(void)
{
if (HasConnectedAudioSystem())
{
CSingleLock lock(m_critSection);
return m_bIsMuted;
}
return false;
}
void CPeripheralCecAdapter::SetMenuLanguage(const char *strLanguage)
{
if (m_strMenuLanguage.Equals(strLanguage))
return;
CStdString strGuiLanguage;
if (!strcmp(strLanguage, "bul"))
strGuiLanguage = "Bulgarian";
else if (!strcmp(strLanguage, "hrv"))
strGuiLanguage = "Croatian";
else if (!strcmp(strLanguage, "cze"))
strGuiLanguage = "Czech";
else if (!strcmp(strLanguage, "dan"))
strGuiLanguage = "Danish";
else if (!strcmp(strLanguage, "dut"))
strGuiLanguage = "Dutch";
else if (!strcmp(strLanguage, "eng"))
strGuiLanguage = "English";
else if (!strcmp(strLanguage, "fin"))
strGuiLanguage = "Finnish";
else if (!strcmp(strLanguage, "fre"))
strGuiLanguage = "French";
else if (!strcmp(strLanguage, "ger"))
strGuiLanguage = "German";
else if (!strcmp(strLanguage, "gre"))
strGuiLanguage = "Greek";
else if (!strcmp(strLanguage, "hun"))
strGuiLanguage = "Hungarian";
else if (!strcmp(strLanguage, "ita"))
strGuiLanguage = "Italian";
else if (!strcmp(strLanguage, "nor"))
strGuiLanguage = "Norwegian";
else if (!strcmp(strLanguage, "pol"))
strGuiLanguage = "Polish";
else if (!strcmp(strLanguage, "por"))
strGuiLanguage = "Portuguese";
else if (!strcmp(strLanguage, "rum"))
strGuiLanguage = "Romanian";
else if (!strcmp(strLanguage, "rus"))
strGuiLanguage = "Russian";
else if (!strcmp(strLanguage, "srp"))
strGuiLanguage = "Serbian";
else if (!strcmp(strLanguage, "slo"))
strGuiLanguage = "Slovenian";
else if (!strcmp(strLanguage, "spa"))
strGuiLanguage = "Spanish";
else if (!strcmp(strLanguage, "swe"))
strGuiLanguage = "Swedish";
else if (!strcmp(strLanguage, "tur"))
strGuiLanguage = "Turkish";
if (!strGuiLanguage.IsEmpty())
{
CApplicationMessenger::Get().SetGUILanguage(strGuiLanguage);
CLog::Log(LOGDEBUG, "%s - language set to '%s'", __FUNCTION__, strGuiLanguage.c_str());
}
else
CLog::Log(LOGWARNING, "%s - TV menu language set to unknown value '%s'", __FUNCTION__, strLanguage);
}
int CPeripheralCecAdapter::CecCommand(void *cbParam, const cec_command command)
{
CPeripheralCecAdapter *adapter = (CPeripheralCecAdapter *)cbParam;
if (!adapter)
return 0;
if (adapter->m_bIsReady)
{
switch (command.opcode)
{
case CEC_OPCODE_STANDBY:
/* a device was put in standby mode */
if (command.initiator == CECDEVICE_TV &&
(adapter->m_configuration.bPowerOffOnStandby == 1 || adapter->m_configuration.bShutdownOnStandby == 1) &&
(!adapter->m_screensaverLastActivated.IsValid() || CDateTime::GetCurrentDateTime() - adapter->m_screensaverLastActivated > CDateTimeSpan(0, 0, 0, SCREENSAVER_TIMEOUT)))
{
adapter->m_bStarted = false;
if (adapter->m_configuration.bPowerOffOnStandby == 1)
CApplicationMessenger::Get().Suspend();
else if (adapter->m_configuration.bShutdownOnStandby == 1)
CApplicationMessenger::Get().Shutdown();
}
break;
case CEC_OPCODE_SET_MENU_LANGUAGE:
if (adapter->m_configuration.bUseTVMenuLanguage == 1 && command.initiator == CECDEVICE_TV && command.parameters.size == 3)
{
char strNewLanguage[4];
for (int iPtr = 0; iPtr < 3; iPtr++)
strNewLanguage[iPtr] = command.parameters[iPtr];
strNewLanguage[3] = 0;
adapter->SetMenuLanguage(strNewLanguage);
}
break;
case CEC_OPCODE_DECK_CONTROL:
if (command.initiator == CECDEVICE_TV &&
command.parameters.size == 1 &&
command.parameters[0] == CEC_DECK_CONTROL_MODE_STOP)
{
cec_keypress key;
key.duration = 500;
key.keycode = CEC_USER_CONTROL_CODE_STOP;
adapter->PushCecKeypress(key);
}
break;
case CEC_OPCODE_PLAY:
if (command.initiator == CECDEVICE_TV &&
command.parameters.size == 1)
{
if (command.parameters[0] == CEC_PLAY_MODE_PLAY_FORWARD)
{
cec_keypress key;
key.duration = 500;
key.keycode = CEC_USER_CONTROL_CODE_PLAY;
adapter->PushCecKeypress(key);
}
else if (command.parameters[0] == CEC_PLAY_MODE_PLAY_STILL)
{
cec_keypress key;
key.duration = 500;
key.keycode = CEC_USER_CONTROL_CODE_PAUSE;
adapter->PushCecKeypress(key);
}
}
break;
default:
break;
}
}
return 1;
}
int CPeripheralCecAdapter::CecConfiguration(void *cbParam, const libcec_configuration config)
{
CPeripheralCecAdapter *adapter = (CPeripheralCecAdapter *)cbParam;
if (!adapter)
return 0;
CSingleLock lock(adapter->m_critSection);
adapter->SetConfigurationFromLibCEC(config);
return 1;
}
int CPeripheralCecAdapter::CecAlert(void *cbParam, const libcec_alert alert, const libcec_parameter data)
{
CPeripheralCecAdapter *adapter = (CPeripheralCecAdapter *)cbParam;
if (!adapter)
return 0;
bool bReopenConnection(false);
int iAlertString(0);
switch (alert)
{
case CEC_ALERT_SERVICE_DEVICE:
iAlertString = 36027;
break;
case CEC_ALERT_CONNECTION_LOST:
iAlertString = 36030;
break;
#if defined(CEC_ALERT_PERMISSION_ERROR)
case CEC_ALERT_PERMISSION_ERROR:
bReopenConnection = true;
iAlertString = 36031;
break;
case CEC_ALERT_PORT_BUSY:
bReopenConnection = true;
iAlertString = 36032;
break;
#endif
default:
break;
}
// display the alert
if (iAlertString)
{
CStdString strLog(g_localizeStrings.Get(iAlertString));
if (data.paramType == CEC_PARAMETER_TYPE_STRING && data.paramData)
strLog.AppendFormat(" - %s", (const char *)data.paramData);
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(36000), strLog);
}
if (bReopenConnection)
adapter->ReopenConnection();
return 1;
}
int CPeripheralCecAdapter::CecKeyPress(void *cbParam, const cec_keypress key)
{
CPeripheralCecAdapter *adapter = (CPeripheralCecAdapter *)cbParam;
if (!adapter)
return 0;
adapter->PushCecKeypress(key);
return 1;
}
void CPeripheralCecAdapter::GetNextKey(void)
{
CSingleLock lock(m_critSection);
m_bHasButton = false;
if (m_bIsReady)
{
vector<CecButtonPress>::iterator it = m_buttonQueue.begin();
if (it != m_buttonQueue.end())
{
m_currentButton = (*it);
m_buttonQueue.erase(it);
m_bHasButton = true;
}
}
}
void CPeripheralCecAdapter::PushCecKeypress(const CecButtonPress &key)
{
CLog::Log(LOGDEBUG, "%s - received key %2x duration %d", __FUNCTION__, key.iButton, key.iDuration);
CSingleLock lock(m_critSection);
if (key.iDuration > 0)
{
if (m_currentButton.iButton == key.iButton && m_currentButton.iDuration == 0)
{
// update the duration
if (m_bHasButton)
m_currentButton.iDuration = key.iDuration;
// ignore this one, since it's already been handled by xbmc
return;
}
// if we received a keypress with a duration set, try to find the same one without a duration set, and replace it
for (vector<CecButtonPress>::reverse_iterator it = m_buttonQueue.rbegin(); it != m_buttonQueue.rend(); it++)
{
if ((*it).iButton == key.iButton)
{
if ((*it).iDuration == 0)
{
// replace this entry
(*it).iDuration = key.iDuration;
return;
}
// add a new entry
break;
}
}
}
m_buttonQueue.push_back(key);
}
void CPeripheralCecAdapter::PushCecKeypress(const cec_keypress &key)
{
CecButtonPress xbmcKey;
xbmcKey.iDuration = key.duration;
switch (key.keycode)
{
case CEC_USER_CONTROL_CODE_SELECT:
xbmcKey.iButton = XINPUT_IR_REMOTE_SELECT;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_UP:
xbmcKey.iButton = XINPUT_IR_REMOTE_UP;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_DOWN:
xbmcKey.iButton = XINPUT_IR_REMOTE_DOWN;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_LEFT:
xbmcKey.iButton = XINPUT_IR_REMOTE_LEFT;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_LEFT_UP:
xbmcKey.iButton = XINPUT_IR_REMOTE_LEFT;
PushCecKeypress(xbmcKey);
xbmcKey.iButton = XINPUT_IR_REMOTE_UP;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_LEFT_DOWN:
xbmcKey.iButton = XINPUT_IR_REMOTE_LEFT;
PushCecKeypress(xbmcKey);
xbmcKey.iButton = XINPUT_IR_REMOTE_DOWN;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_RIGHT:
xbmcKey.iButton = XINPUT_IR_REMOTE_RIGHT;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_RIGHT_UP:
xbmcKey.iButton = XINPUT_IR_REMOTE_RIGHT;
PushCecKeypress(xbmcKey);
xbmcKey.iButton = XINPUT_IR_REMOTE_UP;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_RIGHT_DOWN:
xbmcKey.iButton = XINPUT_IR_REMOTE_RIGHT;
PushCecKeypress(xbmcKey);
xbmcKey.iButton = XINPUT_IR_REMOTE_DOWN;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_SETUP_MENU:
xbmcKey.iButton = XINPUT_IR_REMOTE_TITLE;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_CONTENTS_MENU:
case CEC_USER_CONTROL_CODE_FAVORITE_MENU:
case CEC_USER_CONTROL_CODE_ROOT_MENU:
xbmcKey.iButton = XINPUT_IR_REMOTE_MENU;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_EXIT:
xbmcKey.iButton = XINPUT_IR_REMOTE_BACK;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_ENTER:
xbmcKey.iButton = XINPUT_IR_REMOTE_ENTER;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_CHANNEL_DOWN:
xbmcKey.iButton = XINPUT_IR_REMOTE_CHANNEL_MINUS;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_CHANNEL_UP:
xbmcKey.iButton = XINPUT_IR_REMOTE_CHANNEL_PLUS;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_PREVIOUS_CHANNEL:
xbmcKey.iButton = XINPUT_IR_REMOTE_TELETEXT;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_SOUND_SELECT:
xbmcKey.iButton = XINPUT_IR_REMOTE_LANGUAGE;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_POWER:
case CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION:
case CEC_USER_CONTROL_CODE_POWER_OFF_FUNCTION:
xbmcKey.iButton = XINPUT_IR_REMOTE_POWER;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_VOLUME_UP:
xbmcKey.iButton = XINPUT_IR_REMOTE_VOLUME_PLUS;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_VOLUME_DOWN:
xbmcKey.iButton = XINPUT_IR_REMOTE_VOLUME_MINUS;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_MUTE:
case CEC_USER_CONTROL_CODE_MUTE_FUNCTION:
case CEC_USER_CONTROL_CODE_RESTORE_VOLUME_FUNCTION:
xbmcKey.iButton = XINPUT_IR_REMOTE_MUTE;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_PLAY:
xbmcKey.iButton = XINPUT_IR_REMOTE_PLAY;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_STOP:
xbmcKey.iButton = XINPUT_IR_REMOTE_STOP;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_PAUSE:
xbmcKey.iButton = XINPUT_IR_REMOTE_PAUSE;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_REWIND:
xbmcKey.iButton = XINPUT_IR_REMOTE_REVERSE;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_FAST_FORWARD:
xbmcKey.iButton = XINPUT_IR_REMOTE_FORWARD;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_NUMBER0:
xbmcKey.iButton = XINPUT_IR_REMOTE_0;
PushCecKeypress(xbmcKey);
break;
case CEC_USER_CONTROL_CODE_NUMBER1:
xbmcKey.iButton = XINPUT_IR_REMOTE_1;
PushCecKeypress(xbmcKey);
break;