9dc9bff85d107816abc55de7461feae9d8bb6137
[deb_libcec.git] / CECClient.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33 #include "env.h"
34 #include "CECClient.h"
35
36 #include "CECProcessor.h"
37 #include "LibCEC.h"
38 #include "CECTypeUtils.h"
39 #include "devices/CECPlaybackDevice.h"
40 #include "devices/CECAudioSystem.h"
41 #include "devices/CECTV.h"
42 #include "implementations/CECCommandHandler.h"
43
44 using namespace CEC;
45 using namespace PLATFORM;
46
47 #define LIB_CEC m_processor->GetLib()
48 #define ToString(x) CCECTypeUtils::ToString(x)
49
50 #define COMBO_KEY CEC_USER_CONTROL_CODE_STOP
51 #define COMBO_TIMEOUT_MS 1000
52
53 CCECClient::CCECClient(CCECProcessor *processor, const libcec_configuration &configuration) :
54 m_processor(processor),
55 m_bInitialised(false),
56 m_bRegistered(false),
57 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
58 m_buttontime(0),
59 m_iPreventForwardingPowerOffCommand(0)
60 {
61 m_configuration.Clear();
62 // set the initial configuration
63 SetConfiguration(configuration);
64 }
65
66 CCECClient::~CCECClient(void)
67 {
68 // unregister the client
69 if (m_processor && IsRegistered())
70 m_processor->UnregisterClient(this);
71 }
72
73 bool CCECClient::IsInitialised(void)
74 {
75 CLockObject lock(m_mutex);
76 return m_bInitialised && m_processor;
77 }
78
79 void CCECClient::SetInitialised(bool bSetTo)
80 {
81 CLockObject lock(m_mutex);
82 m_bInitialised = bSetTo;
83 }
84
85 bool CCECClient::IsRegistered(void)
86 {
87 CLockObject lock(m_mutex);
88 return m_bRegistered && m_processor;
89 }
90
91 void CCECClient::SetRegistered(bool bSetTo)
92 {
93 CLockObject lock(m_mutex);
94 m_bRegistered = bSetTo;
95 }
96
97 bool CCECClient::OnRegister(void)
98 {
99 // return false if already initialised
100 if (IsInitialised())
101 return true;
102
103 // get all device we control
104 CECDEVICEVEC devices;
105 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
106
107 // return false when no devices were found
108 if (devices.empty())
109 {
110 LIB_CEC->AddLog(CEC_LOG_WARNING, "cannot find the primary device (logical address %x)", GetPrimaryLogicalAdddress());
111 return false;
112 }
113
114 // mark as initialised
115 SetInitialised(true);
116
117 // configure all devices
118 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
119 {
120 // only set our OSD name for the primary device
121 if ((*it)->GetLogicalAddress() == GetPrimaryLogicalAdddress())
122 (*it)->SetOSDName(m_configuration.strDeviceName);
123
124 // set the default menu language for devices we control
125 (*it)->SetMenuLanguage(m_configuration.strDeviceLanguage);
126 }
127
128 // set the physical address
129 SetPhysicalAddress(m_configuration);
130
131 // make the primary device the active source if the option is set
132 if (m_configuration.bActivateSource == 1)
133 GetPrimaryDevice()->ActivateSource(500);
134
135 return true;
136 }
137
138 bool CCECClient::SetHDMIPort(const cec_logical_address iBaseDevice, const uint8_t iPort, bool bForce /* = false */)
139 {
140 bool bReturn(false);
141
142 // limit the HDMI port range to 1-15
143 if (iPort < CEC_MIN_HDMI_PORTNUMBER ||
144 iPort > CEC_MAX_HDMI_PORTNUMBER)
145 return bReturn;
146
147 LIB_CEC->AddLog(CEC_LOG_NOTICE, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
148
149 // update the configuration
150 {
151 CLockObject lock(m_mutex);
152 m_configuration.baseDevice = iBaseDevice;
153 m_configuration.iHDMIPort = iPort;
154 }
155
156 // don't continue if the connection isn't opened
157 if (!m_processor->CECInitialised() && !bForce)
158 return true;
159
160 // get the PA of the base device
161 uint16_t iPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS);
162 CCECBusDevice *baseDevice = m_processor->GetDevice(iBaseDevice);
163 if (baseDevice)
164 iPhysicalAddress = baseDevice->GetPhysicalAddress(GetPrimaryLogicalAdddress());
165
166 // add our port number
167 if (iPhysicalAddress <= CEC_MAX_PHYSICAL_ADDRESS)
168 {
169 if (iPhysicalAddress == 0)
170 iPhysicalAddress += 0x1000 * iPort;
171 else if (iPhysicalAddress % 0x1000 == 0)
172 iPhysicalAddress += 0x100 * iPort;
173 else if (iPhysicalAddress % 0x100 == 0)
174 iPhysicalAddress += 0x10 * iPort;
175 else if (iPhysicalAddress % 0x10 == 0)
176 iPhysicalAddress += iPort;
177
178 bReturn = true;
179 }
180
181 // set the default address when something went wrong
182 if (!bReturn)
183 {
184 LIB_CEC->AddLog(CEC_LOG_WARNING, "failed to set the physical address to %04X, setting it to the default value %04X", iPhysicalAddress, CEC_DEFAULT_PHYSICAL_ADDRESS);
185 iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS;
186 }
187
188 // and set the address
189 SetDevicePhysicalAddress(iPhysicalAddress);
190
191 CallbackConfigurationChanged(m_configuration);
192
193 return bReturn;
194 }
195
196 void CCECClient::ResetPhysicalAddress(void)
197 {
198 SetPhysicalAddress(m_configuration);
199 }
200
201 void CCECClient::SetPhysicalAddress(const libcec_configuration &configuration)
202 {
203 bool bPASet(false);
204
205 // override the physical address from configuration.iPhysicalAddress if it's set
206 if (!bPASet && CLibCEC::IsValidPhysicalAddress(configuration.iPhysicalAddress))
207 bPASet = SetPhysicalAddress(configuration.iPhysicalAddress);
208
209 // try to autodetect the address
210 if (!bPASet && m_processor->CECInitialised())
211 {
212 bPASet = AutodetectPhysicalAddress();
213 m_configuration.bAutodetectAddress = bPASet ? 1 : 0;
214 }
215
216 // use the base device + hdmi port settings
217 if (!bPASet)
218 bPASet = SetHDMIPort(configuration.baseDevice, configuration.iHDMIPort);
219
220 // reset to defaults if something went wrong
221 if (!bPASet)
222 {
223 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - resetting HDMI port and base device to defaults", __FUNCTION__);
224 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
225 m_configuration.iHDMIPort = CEC_HDMI_PORTNUMBER_NONE;
226 }
227 }
228
229 bool CCECClient::SetPhysicalAddress(const uint16_t iPhysicalAddress)
230 {
231 // update the configuration
232 bool bChanged(true);
233 {
234 CLockObject lock(m_mutex);
235 if (m_configuration.iPhysicalAddress == iPhysicalAddress)
236 bChanged = false;
237 else
238 m_configuration.iPhysicalAddress = iPhysicalAddress;
239 }
240 if (!bChanged)
241 {
242 LIB_CEC->AddLog(CEC_LOG_DEBUG, "physical address unchanged (%04X)", iPhysicalAddress);
243 return true;
244 }
245
246 LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting physical address to '%04X'", iPhysicalAddress);
247
248 // set the physical address for each device
249 SetDevicePhysicalAddress(iPhysicalAddress);
250
251 // and send back the updated configuration
252 CallbackConfigurationChanged(m_configuration);
253
254 return true;
255 }
256
257 void CCECClient::SetSupportedDeviceTypes(void)
258 {
259 cec_device_type_list types;
260 types.Clear();
261
262 // get the command handler for the tv
263 CCECCommandHandler *tvHandler = m_processor->GetTV()->GetHandler();
264 if (!tvHandler)
265 return;
266
267 // check all device types
268 for (uint8_t iPtr = 0; iPtr < 5; iPtr++)
269 {
270 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
271 continue;
272
273 // get the supported device type. the handler will replace types it doesn't support by one it does support
274 cec_device_type type = tvHandler->GetReplacementDeviceType(m_configuration.deviceTypes.types[iPtr]);
275 if (!types.IsSet(type))
276 types.Add(type);
277 }
278 m_processor->GetTV()->MarkHandlerReady();
279
280 // set the new type list
281 m_configuration.deviceTypes = types;
282
283 // persist the new configuration
284 PersistConfiguration(m_configuration);
285 }
286
287 bool CCECClient::AllocateLogicalAddresses(void)
288 {
289 // reset all previous LAs that were set
290 m_configuration.logicalAddresses.Clear();
291
292 // get the supported device types from the command handler of the TV
293 SetSupportedDeviceTypes();
294
295 // display an error if no device types are set
296 if (m_configuration.deviceTypes.IsEmpty())
297 {
298 LIB_CEC->AddLog(CEC_LOG_ERROR, "no device types given");
299 return false;
300 }
301
302 // check each entry of the list
303 for (uint8_t iPtr = 0; iPtr < 5; iPtr++)
304 {
305 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
306 continue;
307
308 // find an LA for this type
309 cec_logical_address address(CECDEVICE_UNKNOWN);
310 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
311 address = AllocateLogicalAddressRecordingDevice();
312 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
313 address = AllocateLogicalAddressTuner();
314 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
315 address = AllocateLogicalAddressPlaybackDevice();
316 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
317 address = AllocateLogicalAddressAudioSystem();
318
319 // display an error if no LA could be allocated
320 if (address == CECDEVICE_UNKNOWN)
321 {
322 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - failed to allocate device '%d', type '%s'", __FUNCTION__, iPtr, ToString(m_configuration.deviceTypes.types[iPtr]));
323 return false;
324 }
325
326 // display the registered LA
327 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - device '%d', type '%s', LA '%X'", __FUNCTION__, iPtr, ToString(m_configuration.deviceTypes.types[iPtr]), address);
328 m_configuration.logicalAddresses.Set(address);
329 }
330
331 // persist the new configuration
332 PersistConfiguration(m_configuration);
333
334 return true;
335 }
336
337 cec_logical_address CCECClient::AllocateLogicalAddressRecordingDevice(void)
338 {
339 cec_logical_address retVal(CECDEVICE_UNKNOWN);
340
341 LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
342 if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1, m_configuration.cecVersion))
343 retVal = CECDEVICE_RECORDINGDEVICE1;
344 else if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2, m_configuration.cecVersion))
345 retVal = CECDEVICE_RECORDINGDEVICE2;
346 else if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3, m_configuration.cecVersion))
347 retVal = CECDEVICE_RECORDINGDEVICE3;
348
349 return retVal;
350 }
351
352 cec_logical_address CCECClient::AllocateLogicalAddressTuner(void)
353 {
354 cec_logical_address retVal(CECDEVICE_UNKNOWN);
355
356 LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
357 if (m_processor->TryLogicalAddress(CECDEVICE_TUNER1, m_configuration.cecVersion))
358 retVal = CECDEVICE_TUNER1;
359 else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER2, m_configuration.cecVersion))
360 retVal = CECDEVICE_TUNER2;
361 else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER3, m_configuration.cecVersion))
362 retVal = CECDEVICE_TUNER3;
363 else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER4, m_configuration.cecVersion))
364 retVal = CECDEVICE_TUNER4;
365
366 return retVal;
367 }
368
369 cec_logical_address CCECClient::AllocateLogicalAddressPlaybackDevice(void)
370 {
371 cec_logical_address retVal(CECDEVICE_UNKNOWN);
372
373 LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
374 if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1, m_configuration.cecVersion))
375 retVal = CECDEVICE_PLAYBACKDEVICE1;
376 else if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2, m_configuration.cecVersion))
377 retVal = CECDEVICE_PLAYBACKDEVICE2;
378 else if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3, m_configuration.cecVersion))
379 retVal = CECDEVICE_PLAYBACKDEVICE3;
380
381 return retVal;
382 }
383
384 cec_logical_address CCECClient::AllocateLogicalAddressAudioSystem(void)
385 {
386 cec_logical_address retVal(CECDEVICE_UNKNOWN);
387
388 LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audiosystem'");
389 if (m_processor->TryLogicalAddress(CECDEVICE_AUDIOSYSTEM, m_configuration.cecVersion))
390 retVal = CECDEVICE_AUDIOSYSTEM;
391
392 return retVal;
393 }
394
395 CCECBusDevice *CCECClient::GetDeviceByType(const cec_device_type type) const
396 {
397 // get all devices that match our logical addresses
398 CECDEVICEVEC devices;
399 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
400
401 // filter the type we need
402 CCECDeviceMap::FilterType(type, devices);
403
404 return devices.empty() ?
405 NULL :
406 *devices.begin();
407 }
408
409 bool CCECClient::ChangeDeviceType(const cec_device_type from, const cec_device_type to)
410 {
411 if (from == to)
412 return true;
413
414 LIB_CEC->AddLog(CEC_LOG_NOTICE, "changing device type '%s' into '%s'", ToString(from), ToString(to));
415
416 {
417 CLockObject lock(m_mutex);
418
419 // get the previous device that was allocated
420 CCECBusDevice *previousDevice = GetDeviceByType(from);
421 if (!previousDevice)
422 return false;
423
424 // change the type in the device type list
425 bool bChanged(false);
426 for (uint8_t iPtr = 0; iPtr < 5; iPtr++)
427 {
428 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
429 continue;
430
431 if (m_configuration.deviceTypes.types[iPtr] == from)
432 {
433 bChanged = true;
434 m_configuration.deviceTypes.types[iPtr] = to;
435 }
436 else if (m_configuration.deviceTypes.types[iPtr] == to && bChanged)
437 {
438 // ensure that dupes are removed
439 m_configuration.deviceTypes.types[iPtr] = CEC_DEVICE_TYPE_RESERVED;
440 }
441 }
442 }
443
444 // re-register the client to set the new ackmask
445 if (!m_processor->RegisterClient(this))
446 return false;
447
448 // persist the new configuration
449 PersistConfiguration(m_configuration);
450
451 return true;
452 }
453
454 bool CCECClient::SetLogicalAddress(const cec_logical_address iLogicalAddress)
455 {
456 bool bReturn(true);
457
458 if (GetPrimaryLogicalAdddress() != iLogicalAddress)
459 {
460 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< setting primary logical address to %1x", iLogicalAddress);
461 {
462 CLockObject lock(m_mutex);
463 m_configuration.logicalAddresses.primary = iLogicalAddress;
464 m_configuration.logicalAddresses.Set(iLogicalAddress);
465 }
466
467 bReturn = m_processor->RegisterClient(this);
468
469 // persist the new configuration
470 if (bReturn)
471 PersistConfiguration(m_configuration);
472 }
473
474 return bReturn;
475 }
476
477 bool CCECClient::Transmit(const cec_command &data, bool bIsReply)
478 {
479 return m_processor ? m_processor->Transmit(data, bIsReply) : false;
480 }
481
482 bool CCECClient::SendPowerOnDevices(const cec_logical_address address /* = CECDEVICE_TV */)
483 {
484 // if the broadcast address if set as destination and the client version >=1.5.0, read the wakeDevices setting
485 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
486 {
487 CECDEVICEVEC devices;
488 m_processor->GetDevices()->GetWakeDevices(m_configuration, devices);
489 return m_processor->PowerOnDevices(GetPrimaryLogicalAdddress(), devices);
490 }
491
492 return m_processor->PowerOnDevice(GetPrimaryLogicalAdddress(), address);
493 }
494
495 bool CCECClient::SendStandbyDevices(const cec_logical_address address /* = CECDEVICE_BROADCAST */)
496 {
497 // if the broadcast address if set as destination and the client version >=1.5.0, read the standbyDevices setting
498 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
499 {
500 CECDEVICEVEC devices;
501 m_processor->GetDevices()->GetPowerOffDevices(m_configuration, devices);
502 return m_processor->StandbyDevices(GetPrimaryLogicalAdddress(), devices);
503 }
504
505 return m_processor->StandbyDevice(GetPrimaryLogicalAdddress(), address);
506 }
507
508 bool CCECClient::SendSetActiveSource(const cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
509 {
510 // get the devices that are controlled by us
511 CECDEVICEVEC devices;
512 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
513
514 // filter out the device that matches the given type
515 if (type != CEC_DEVICE_TYPE_RESERVED)
516 CCECDeviceMap::FilterType(type, devices);
517
518 // no devices left, re-fetch the list of devices that are controlled by us
519 if (devices.empty())
520 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
521
522 if (!devices.empty())
523 {
524 // get the first device from the list
525 CCECBusDevice *device = *devices.begin();
526
527 // and activate it
528 if (!m_processor->CECInitialised())
529 device->MarkAsActiveSource();
530 else if (device->HasValidPhysicalAddress())
531 return device->ActivateSource();
532 }
533
534 return false;
535 }
536
537 CCECPlaybackDevice *CCECClient::GetPlaybackDevice(void)
538 {
539 CCECPlaybackDevice *device(NULL);
540 CECDEVICEVEC devices;
541
542 // get the playback devices
543 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
544 CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE, devices);
545
546 // no matches, get the recording devices
547 if (devices.empty())
548 {
549 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
550 CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_RECORDING_DEVICE, devices);
551 }
552
553 // get the first device that matches, and cast it to CCECPlaybackDevice
554 if (!devices.empty())
555 device = (*devices.begin())->AsPlaybackDevice();
556
557 return device;
558 }
559
560 cec_logical_address CCECClient::GetPrimaryLogicalAdddress(void)
561 {
562 CLockObject lock(m_mutex);
563 return m_configuration.logicalAddresses.primary;
564 }
565
566 CCECBusDevice *CCECClient::GetPrimaryDevice(void)
567 {
568 return m_processor->GetDevice(GetPrimaryLogicalAdddress());
569 }
570
571 bool CCECClient::SendSetDeckControlMode(const cec_deck_control_mode mode, bool bSendUpdate /* = true */)
572 {
573 // find a playback device that we control
574 CCECPlaybackDevice *device = GetPlaybackDevice();
575 if (device)
576 {
577 // and set the deck control mode if there is a match
578 device->SetDeckControlMode(mode);
579 if (bSendUpdate)
580 return device->TransmitDeckStatus(CECDEVICE_TV, false);
581 return true;
582 }
583
584 // no match
585 return false;
586 }
587
588 bool CCECClient::SendSetDeckInfo(const cec_deck_info info, bool bSendUpdate /* = true */)
589 {
590 // find a playback device that we control
591 CCECPlaybackDevice *device = GetPlaybackDevice();
592 if (device)
593 {
594 // and set the deck status if there is a match
595 device->SetDeckStatus(info);
596 if (bSendUpdate)
597 return device->AsPlaybackDevice()->TransmitDeckStatus(CECDEVICE_TV, false);
598 return true;
599 }
600
601 // no match
602 return false;
603 }
604
605 bool CCECClient::SendSetMenuState(const cec_menu_state state, bool bSendUpdate /* = true */)
606 {
607 CECDEVICEVEC devices;
608
609 // set the menu state for all devices that are controlled by us
610 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
611 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
612 {
613 (*it)->SetMenuState(state);
614 if (bSendUpdate)
615 (*it)->TransmitMenuState(CECDEVICE_TV, false);
616 }
617
618 return true;
619 }
620
621 bool CCECClient::SendSetInactiveView(void)
622 {
623 CECDEVICEVEC devices;
624
625 // mark all devices that are controlled by us as inactive source
626 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
627 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
628 {
629 if ((*it)->IsActiveSource())
630 {
631 (*it)->MarkAsInactiveSource();
632 return (*it)->TransmitInactiveSource();
633 }
634 }
635
636 return true;
637 }
638
639 bool CCECClient::SendSetOSDString(const cec_logical_address iLogicalAddress, const cec_display_control duration, const char *strMessage)
640 {
641 CCECBusDevice *primary = GetPrimaryDevice();
642 if (primary)
643 return primary->TransmitOSDString(iLogicalAddress, duration, strMessage, false);
644
645 return false;
646 }
647
648 cec_version CCECClient::GetDeviceCecVersion(const cec_logical_address iAddress)
649 {
650 CCECBusDevice *device = m_processor->GetDevice(iAddress);
651 if (device)
652 return device->GetCecVersion(GetPrimaryLogicalAdddress());
653 return CEC_VERSION_UNKNOWN;
654 }
655
656 bool CCECClient::GetDeviceMenuLanguage(const cec_logical_address iAddress, cec_menu_language &language)
657 {
658 CCECBusDevice *device = m_processor->GetDevice(iAddress);
659 if (device)
660 {
661 language = device->GetMenuLanguage(GetPrimaryLogicalAdddress());
662 return (strcmp(language.language, "???") != 0);
663 }
664 return false;
665 }
666
667 cec_osd_name CCECClient::GetDeviceOSDName(const cec_logical_address iAddress)
668 {
669 cec_osd_name retVal;
670 retVal.device = iAddress;
671 retVal.name[0] = 0;
672
673 CCECBusDevice *device = m_processor->GetDevice(iAddress);
674 if (device)
675 {
676 CStdString strOSDName = device->GetOSDName(GetPrimaryLogicalAdddress());
677 snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
678 retVal.device = iAddress;
679 }
680
681 return retVal;
682 }
683
684 uint16_t CCECClient::GetDevicePhysicalAddress(const cec_logical_address iAddress)
685 {
686 CCECBusDevice *device = m_processor->GetDevice(iAddress);
687 if (device)
688 return device->GetPhysicalAddress(GetPrimaryLogicalAdddress());
689 return CEC_INVALID_PHYSICAL_ADDRESS;
690 }
691
692 cec_power_status CCECClient::GetDevicePowerStatus(const cec_logical_address iAddress)
693 {
694 CCECBusDevice *device = m_processor->GetDevice(iAddress);
695 if (device)
696 return device->GetPowerStatus(GetPrimaryLogicalAdddress());
697 return CEC_POWER_STATUS_UNKNOWN;
698 }
699
700 uint64_t CCECClient::GetDeviceVendorId(const cec_logical_address iAddress)
701 {
702 CCECBusDevice *device = m_processor->GetDevice(iAddress);
703 if (device)
704 return device->GetVendorId(GetPrimaryLogicalAdddress());
705 return CEC_VENDOR_UNKNOWN;
706 }
707
708 uint8_t CCECClient::SendVolumeUp(bool bSendRelease /* = true */)
709 {
710 CCECBusDevice *device = GetPrimaryDevice();
711 CCECAudioSystem *audio = m_processor->GetAudioSystem();
712
713 return device && audio && audio->IsPresent() ?
714 audio->VolumeUp(device->GetLogicalAddress(), bSendRelease) :
715 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
716 }
717
718 uint8_t CCECClient::SendVolumeDown(bool bSendRelease /* = true */)
719 {
720 CCECBusDevice *device = GetPrimaryDevice();
721 CCECAudioSystem *audio = m_processor->GetAudioSystem();
722
723 return device && audio && audio->IsPresent() ?
724 audio->VolumeDown(device->GetLogicalAddress(), bSendRelease) :
725 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
726 }
727
728 uint8_t CCECClient::SendMuteAudio(void)
729 {
730 CCECBusDevice *device = GetPrimaryDevice();
731 CCECAudioSystem *audio = m_processor->GetAudioSystem();
732
733 return device && audio && audio->IsPresent() ?
734 audio->MuteAudio(device->GetLogicalAddress()) :
735 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
736 }
737
738 bool CCECClient::SendKeypress(const cec_logical_address iDestination, const cec_user_control_code key, bool bWait /* = true */)
739 {
740 CCECBusDevice *dest = m_processor->GetDevice(iDestination);
741
742 return dest ?
743 dest->TransmitKeypress(GetPrimaryLogicalAdddress(), key, bWait) :
744 false;
745 }
746
747 bool CCECClient::SendKeyRelease(const cec_logical_address iDestination, bool bWait /* = true */)
748 {
749 CCECBusDevice *dest = m_processor->GetDevice(iDestination);
750
751 return dest ?
752 dest->TransmitKeyRelease(GetPrimaryLogicalAdddress(), bWait) :
753 false;
754 }
755
756 bool CCECClient::GetCurrentConfiguration(libcec_configuration &configuration)
757 {
758 CLockObject lock(m_mutex);
759
760 // client version 1.5.0
761 snprintf(configuration.strDeviceName, 13, "%s", m_configuration.strDeviceName);
762 configuration.deviceTypes = m_configuration.deviceTypes;
763 configuration.bAutodetectAddress = m_configuration.bAutodetectAddress;
764 configuration.iPhysicalAddress = m_configuration.iPhysicalAddress;
765 configuration.baseDevice = m_configuration.baseDevice;
766 configuration.iHDMIPort = m_configuration.iHDMIPort;
767 configuration.clientVersion = m_configuration.clientVersion;
768 configuration.serverVersion = m_configuration.serverVersion;
769 configuration.tvVendor = m_configuration.tvVendor;
770
771 configuration.bGetSettingsFromROM = m_configuration.bGetSettingsFromROM;
772 configuration.bUseTVMenuLanguage = m_configuration.bUseTVMenuLanguage;
773 configuration.bActivateSource = m_configuration.bActivateSource;
774 configuration.wakeDevices = m_configuration.wakeDevices;
775 configuration.powerOffDevices = m_configuration.powerOffDevices;
776 configuration.bPowerOffScreensaver = m_configuration.bPowerOffScreensaver;
777 configuration.bPowerOffOnStandby = m_configuration.bPowerOffOnStandby;
778
779 // client version 1.5.1
780 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_1)
781 configuration.bSendInactiveSource = m_configuration.bSendInactiveSource;
782
783 // client version 1.5.3
784 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_3)
785 configuration.logicalAddresses = m_configuration.logicalAddresses;
786
787 // client version 1.6.0
788 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_0)
789 {
790 configuration.iFirmwareVersion = m_configuration.iFirmwareVersion;
791 configuration.bPowerOffDevicesOnStandby = m_configuration.bPowerOffDevicesOnStandby;
792 configuration.bShutdownOnStandby = m_configuration.bShutdownOnStandby;
793 }
794
795 // client version 1.6.2
796 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_2)
797 {
798 memcpy(configuration.strDeviceLanguage, m_configuration.strDeviceLanguage, 3);
799 configuration.iFirmwareBuildDate = m_configuration.iFirmwareBuildDate;
800 }
801
802 // client version 1.6.3
803 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3)
804 {
805 configuration.bMonitorOnly = m_configuration.bMonitorOnly;
806 }
807
808 // client version 1.8.0
809 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_8_0)
810 configuration.cecVersion = m_configuration.cecVersion;
811
812 // client version 1.8.2
813 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_8_2)
814 configuration.adapterType = m_configuration.adapterType;
815
816 return true;
817 }
818
819 bool CCECClient::SetConfiguration(const libcec_configuration &configuration)
820 {
821 bool bIsRunning(m_processor && m_processor->CECInitialised());
822 CCECBusDevice *primary = bIsRunning ? GetPrimaryDevice() : NULL;
823 uint16_t iPA = primary ? primary->GetCurrentPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS;
824
825 // update the callbacks
826 if (configuration.callbacks)
827 EnableCallbacks(configuration.callbackParam, configuration.callbacks);
828
829 // update the client version
830 SetClientVersion((cec_client_version)configuration.clientVersion);
831
832 // update the OSD name
833 CStdString strOSDName(configuration.strDeviceName);
834 SetOSDName(strOSDName);
835
836 // update the TV vendor override
837 SetTVVendorOverride((cec_vendor_id)configuration.tvVendor);
838
839 // just copy these
840 {
841 CLockObject lock(m_mutex);
842 m_configuration.bUseTVMenuLanguage = configuration.bUseTVMenuLanguage;
843 m_configuration.bActivateSource = configuration.bActivateSource;
844 m_configuration.bGetSettingsFromROM = configuration.bGetSettingsFromROM;
845 m_configuration.wakeDevices = configuration.wakeDevices;
846 m_configuration.powerOffDevices = configuration.powerOffDevices;
847 m_configuration.bPowerOffScreensaver = configuration.bPowerOffScreensaver;
848 m_configuration.bPowerOffOnStandby = configuration.bPowerOffOnStandby;
849
850 // client version 1.5.1
851 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_1)
852 m_configuration.bSendInactiveSource = configuration.bSendInactiveSource;
853
854 // client version 1.6.0
855 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_0)
856 {
857 m_configuration.bPowerOffDevicesOnStandby = configuration.bPowerOffDevicesOnStandby;
858 m_configuration.bShutdownOnStandby = configuration.bShutdownOnStandby;
859 }
860
861 // client version 1.6.2
862 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_2)
863 {
864 memcpy(m_configuration.strDeviceLanguage, configuration.strDeviceLanguage, 3);
865 }
866
867 // client version 1.6.3
868 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3)
869 {
870 m_configuration.bMonitorOnly = configuration.bMonitorOnly;
871 }
872
873 // client version 1.8.0
874 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_8_0)
875 m_configuration.cecVersion = configuration.cecVersion;
876
877 // client version 1.8.2
878 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_8_2)
879 m_configuration.adapterType = configuration.adapterType;
880
881 // ensure that there is at least 1 device type set
882 if (m_configuration.deviceTypes.IsEmpty())
883 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
884 }
885
886 bool bNeedReinit(false);
887
888 // device types
889 if (SetDeviceTypes(configuration.deviceTypes))
890 {
891 // the device type changed. just copy the rest, and re-register
892 {
893 CLockObject lock(m_mutex);
894 m_configuration.iPhysicalAddress = configuration.iPhysicalAddress;
895 m_configuration.baseDevice = configuration.baseDevice;
896 m_configuration.iHDMIPort = configuration.iHDMIPort;
897 bNeedReinit = true;
898 }
899 }
900 else
901 {
902 // set the physical address
903 SetPhysicalAddress(configuration);
904 }
905
906 // persist the new configuration
907 PersistConfiguration(m_configuration);
908
909 if (!primary)
910 primary = GetPrimaryDevice();
911
912 if (bNeedReinit || !primary || primary->GetCurrentPhysicalAddress() != iPA)
913 {
914 // PA or device type changed
915 m_processor->RegisterClient(this);
916 }
917 else if (primary && configuration.bActivateSource == 1 && bIsRunning && !primary->IsActiveSource())
918 {
919 // activate the source if we're not already the active source
920 primary->ActivateSource();
921 }
922
923 return true;
924 }
925
926 void CCECClient::AddCommand(const cec_command &command)
927 {
928 // don't forward the standby opcode more than once every 10 seconds
929 if (command.opcode == CEC_OPCODE_STANDBY)
930 {
931 CLockObject lock(m_mutex);
932 if (m_iPreventForwardingPowerOffCommand != 0 &&
933 m_iPreventForwardingPowerOffCommand > GetTimeMs())
934 return;
935 else
936 m_iPreventForwardingPowerOffCommand = GetTimeMs() + CEC_FORWARD_STANDBY_MIN_INTERVAL;
937 }
938
939 if (command.destination == CECDEVICE_BROADCAST || GetLogicalAddresses().IsSet(command.destination))
940 {
941 LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> %s (%X) -> %s (%X): %s (%2X)", ToString(command.initiator), command.initiator, ToString(command.destination), command.destination, ToString(command.opcode), command.opcode);
942 CallbackAddCommand(command);
943 }
944 }
945
946 int CCECClient::MenuStateChanged(const cec_menu_state newState)
947 {
948 LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> %s: %s", ToString(CEC_OPCODE_MENU_REQUEST), ToString(newState));
949 return CallbackMenuStateChanged(newState);
950 }
951
952 void CCECClient::AddKey(bool bSendComboKey /* = false */)
953 {
954 cec_keypress key;
955 key.keycode = CEC_USER_CONTROL_CODE_UNKNOWN;
956
957 {
958 CLockObject lock(m_mutex);
959 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
960 {
961 key.duration = (unsigned int) (GetTimeMs() - m_buttontime);
962
963 if (key.duration > COMBO_TIMEOUT_MS || m_iCurrentButton != COMBO_KEY || bSendComboKey)
964 {
965 key.keycode = m_iCurrentButton;
966
967 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
968 m_buttontime = 0;
969 }
970 }
971 }
972
973 if (key.keycode != CEC_USER_CONTROL_CODE_UNKNOWN)
974 {
975 LIB_CEC->AddLog(CEC_LOG_DEBUG, "key released: %s (%1x)", ToString(key.keycode), key.keycode);
976 CallbackAddKey(key);
977 }
978 }
979
980 void CCECClient::AddKey(const cec_keypress &key)
981 {
982 // send back the previous key if there is one
983 AddKey();
984
985 cec_keypress transmitKey(key);
986
987 {
988 CLockObject lock(m_mutex);
989 if (key.duration > 0 || key.keycode > CEC_USER_CONTROL_CODE_MAX)
990 {
991 transmitKey.keycode = CEC_USER_CONTROL_CODE_UNKNOWN;
992 }
993 else if (m_iCurrentButton == COMBO_KEY)
994 {
995 // stop + ok -> exit
996 if (key.keycode == CEC_USER_CONTROL_CODE_SELECT)
997 transmitKey.keycode = CEC_USER_CONTROL_CODE_EXIT;
998 // stop + pause -> root menu
999 else if (key.keycode == CEC_USER_CONTROL_CODE_ROOT_MENU)
1000 transmitKey.keycode = CEC_USER_CONTROL_CODE_ROOT_MENU;
1001 // stop + play -> dot (which is handled as context menu in xbmc)
1002 else if (key.keycode == CEC_USER_CONTROL_CODE_PLAY)
1003 transmitKey.keycode = CEC_USER_CONTROL_CODE_DOT;
1004 // default, send back the previous key
1005 else
1006 AddKey(true);
1007 }
1008
1009 m_iCurrentButton = transmitKey.keycode;
1010 m_buttontime = m_iCurrentButton == CEC_USER_CONTROL_CODE_UNKNOWN || key.duration > 0 ? 0 : GetTimeMs();
1011 }
1012
1013 LIB_CEC->AddLog(CEC_LOG_DEBUG, "key pressed: %s (%1x)", ToString(transmitKey.keycode), transmitKey.keycode);
1014 CallbackAddKey(transmitKey);
1015 }
1016
1017 void CCECClient::SetCurrentButton(const cec_user_control_code iButtonCode)
1018 {
1019 // push a keypress to the buffer with 0 duration and another with the duration set when released
1020 cec_keypress key;
1021 key.duration = 0;
1022 key.keycode = iButtonCode;
1023
1024 AddKey(key);
1025 }
1026
1027 void CCECClient::CheckKeypressTimeout(void)
1028 {
1029 cec_keypress key;
1030
1031 {
1032 CLockObject lock(m_mutex);
1033 uint64_t iNow = GetTimeMs();
1034
1035 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN &&
1036 ((m_iCurrentButton == COMBO_KEY && iNow - m_buttontime > COMBO_TIMEOUT_MS) ||
1037 (m_iCurrentButton != COMBO_KEY && iNow - m_buttontime > CEC_BUTTON_TIMEOUT)))
1038 {
1039 key.duration = (unsigned int) (iNow - m_buttontime);
1040 key.keycode = m_iCurrentButton;
1041
1042 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
1043 m_buttontime = 0;
1044 }
1045 else
1046 {
1047 return;
1048 }
1049 }
1050
1051 LIB_CEC->AddLog(CEC_LOG_DEBUG, "key auto-released: %s (%1x)", ToString(key.keycode), key.keycode);
1052 CallbackAddKey(key);
1053 }
1054
1055 bool CCECClient::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks)
1056 {
1057 CLockObject lock(m_cbMutex);
1058 m_configuration.callbackParam = cbParam;
1059 m_configuration.callbacks = callbacks;
1060 return true;
1061 }
1062
1063 bool CCECClient::PingAdapter(void)
1064 {
1065 return m_processor ? m_processor->PingAdapter() : false;
1066 }
1067
1068 std::string CCECClient::GetConnectionInfo(void)
1069 {
1070 CStdString strLog;
1071 strLog.Format("libCEC version = %s, client version = %s, firmware version = %d", ToString((cec_server_version)m_configuration.serverVersion), ToString((cec_client_version)m_configuration.clientVersion), m_configuration.iFirmwareVersion);
1072 if (m_configuration.iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN)
1073 {
1074 time_t buildTime = (time_t)m_configuration.iFirmwareBuildDate;
1075 strLog.AppendFormat(", firmware build date: %s", asctime(gmtime(&buildTime)));
1076 strLog = strLog.Left((int)strLog.length() - 1); // strip \n added by asctime
1077 strLog.append(" +0000");
1078 }
1079
1080 // log the addresses that are being used
1081 if (!m_configuration.logicalAddresses.IsEmpty())
1082 {
1083 strLog.append(", logical address(es) = ");
1084 CECDEVICEVEC devices;
1085 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
1086 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
1087 strLog.AppendFormat("%s (%X) ", (*it)->GetLogicalAddressName(), (*it)->GetLogicalAddress());
1088 }
1089
1090 if (!CLibCEC::IsValidPhysicalAddress(m_configuration.iPhysicalAddress))
1091 strLog.AppendFormat(", base device: %s (%X), HDMI port number: %d", ToString(m_configuration.baseDevice), m_configuration.baseDevice, m_configuration.iHDMIPort);
1092 else
1093 strLog.AppendFormat(", physical address: %04x", m_configuration.iPhysicalAddress);
1094
1095 strLog.AppendFormat(", %s", LIB_CEC->GetLibInfo());
1096
1097 std::string strReturn(strLog.c_str());
1098 return strReturn;
1099 }
1100
1101 void CCECClient::SetTVVendorOverride(const cec_vendor_id id)
1102 {
1103 {
1104 CLockObject lock(m_mutex);
1105 m_configuration.tvVendor = id;
1106 }
1107
1108 if (id != CEC_VENDOR_UNKNOWN)
1109 {
1110 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vendor id '%s'", __FUNCTION__, ToString(id));
1111
1112 CCECBusDevice *tv = m_processor ? m_processor->GetTV() : NULL;
1113 if (tv)
1114 tv->SetVendorId((uint64_t)id);
1115 }
1116
1117 // persist the new configuration
1118 PersistConfiguration(m_configuration);
1119 }
1120
1121 cec_vendor_id CCECClient::GetTVVendorOverride(void)
1122 {
1123 CLockObject lock(m_mutex);
1124 return (cec_vendor_id)m_configuration.tvVendor;
1125 }
1126
1127 void CCECClient::SetOSDName(const std::string &strDeviceName)
1128 {
1129 {
1130 CLockObject lock(m_mutex);
1131 snprintf(m_configuration.strDeviceName, 13, "%s", strDeviceName.c_str());
1132 }
1133
1134 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - using OSD name '%s'", __FUNCTION__, strDeviceName.c_str());
1135
1136 CCECBusDevice *primary = GetPrimaryDevice();
1137 if (primary && !primary->GetCurrentOSDName().Equals(strDeviceName.c_str()))
1138 {
1139 primary->SetOSDName(strDeviceName);
1140 if (m_processor && m_processor->CECInitialised())
1141 primary->TransmitOSDName(CECDEVICE_TV, false);
1142 }
1143
1144 // persist the new configuration
1145 PersistConfiguration(m_configuration);
1146 }
1147
1148 std::string CCECClient::GetOSDName(void)
1149 {
1150 CLockObject lock(m_mutex);
1151 std::string strOSDName(m_configuration.strDeviceName);
1152 return strOSDName;
1153 }
1154
1155 void CCECClient::SetWakeDevices(const cec_logical_addresses &addresses)
1156 {
1157 {
1158 CLockObject lock(m_mutex);
1159 m_configuration.wakeDevices = addresses;
1160 }
1161 // persist the new configuration
1162 PersistConfiguration(m_configuration);
1163 }
1164
1165 cec_logical_addresses CCECClient::GetWakeDevices(void)
1166 {
1167 CLockObject lock(m_mutex);
1168 return m_configuration.wakeDevices;
1169 }
1170
1171 bool CCECClient::AutodetectPhysicalAddress(void)
1172 {
1173 bool bPhysicalAutodetected(false);
1174 uint16_t iPhysicalAddress = m_processor ? m_processor->GetDetectedPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS;
1175
1176 if (CLibCEC::IsValidPhysicalAddress(iPhysicalAddress))
1177 {
1178 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - autodetected physical address '%04X'", __FUNCTION__, iPhysicalAddress);
1179
1180 CLockObject lock(m_mutex);
1181 m_configuration.iPhysicalAddress = iPhysicalAddress;
1182 m_configuration.iHDMIPort = CEC_HDMI_PORTNUMBER_NONE;
1183 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
1184 bPhysicalAutodetected = true;
1185 }
1186
1187 SetDevicePhysicalAddress(iPhysicalAddress);
1188
1189 return bPhysicalAutodetected;
1190 }
1191
1192 void CCECClient::SetClientVersion(const cec_client_version version)
1193 {
1194 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - using client version '%s'", __FUNCTION__, ToString(version));
1195
1196 CLockObject lock(m_mutex);
1197 m_configuration.clientVersion = (uint32_t)version;
1198 }
1199
1200 cec_client_version CCECClient::GetClientVersion(void)
1201 {
1202 CLockObject lock(m_mutex);
1203 return (cec_client_version)m_configuration.clientVersion;
1204 }
1205
1206 bool CCECClient::SetDeviceTypes(const cec_device_type_list &deviceTypes)
1207 {
1208 bool bNeedReinit(false);
1209
1210 {
1211 CLockObject lock(m_mutex);
1212 bNeedReinit = m_processor && m_processor->CECInitialised() &&
1213 (m_configuration.deviceTypes != deviceTypes);
1214 m_configuration.deviceTypes = deviceTypes;
1215 }
1216
1217 // persist the new configuration
1218 PersistConfiguration(m_configuration);
1219
1220 if (bNeedReinit)
1221 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - using primary device type '%s'", __FUNCTION__, ToString(deviceTypes[0]));
1222
1223 return bNeedReinit;
1224 }
1225
1226 cec_device_type_list CCECClient::GetDeviceTypes(void)
1227 {
1228 cec_device_type_list retVal;
1229 CLockObject lock(m_mutex);
1230 retVal = m_configuration.deviceTypes;
1231 return retVal;
1232 }
1233
1234 bool CCECClient::SetDevicePhysicalAddress(const uint16_t iPhysicalAddress)
1235 {
1236 if (!CLibCEC::IsValidPhysicalAddress(iPhysicalAddress))
1237 {
1238 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - not setting invalid physical address %04x", __FUNCTION__, iPhysicalAddress);
1239 return false;
1240 }
1241
1242 // reconfigure all devices
1243 cec_logical_address reactivateSource(CECDEVICE_UNKNOWN);
1244 CECDEVICEVEC devices;
1245 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
1246 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
1247 {
1248 // if this device was the active source, reactivate it afterwards
1249 if ((*it)->IsActiveSource())
1250 reactivateSource = (*it)->GetLogicalAddress();
1251
1252 // mark the device as inactive source
1253 if (IsInitialised())
1254 (*it)->MarkAsInactiveSource();
1255
1256 // set the new physical address
1257 (*it)->SetPhysicalAddress(iPhysicalAddress);
1258
1259 // and transmit it
1260 if (IsInitialised())
1261 (*it)->TransmitPhysicalAddress(false);
1262 }
1263
1264 // reactivate the previous active source
1265 if (reactivateSource != CECDEVICE_UNKNOWN &&
1266 m_processor->CECInitialised() &&
1267 IsInitialised())
1268 {
1269 CCECBusDevice *device = m_processor->GetDevice(reactivateSource);
1270 if (device)
1271 device->ActivateSource();
1272 }
1273
1274 // persist the new configuration
1275 PersistConfiguration(m_configuration);
1276
1277 return true;
1278 }
1279
1280 bool CCECClient::SwitchMonitoring(bool bEnable)
1281 {
1282 LIB_CEC->AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
1283
1284 if (m_processor)
1285 {
1286 m_processor->SwitchMonitoring(bEnable);
1287 m_configuration.bMonitorOnly = bEnable;
1288 return bEnable ? true: m_processor->RegisterClient(this);
1289 }
1290
1291 return false;
1292 }
1293
1294 bool CCECClient::PollDevice(const cec_logical_address iAddress)
1295 {
1296 // try to find the primary device
1297 CCECBusDevice *primary = GetPrimaryDevice();
1298 // poll the destination, with the primary as source
1299 if (primary)
1300 return primary->TransmitPoll(iAddress, false);
1301
1302 return m_processor ? m_processor->PollDevice(iAddress) : false;
1303 }
1304
1305 cec_logical_addresses CCECClient::GetActiveDevices(void)
1306 {
1307 CECDEVICEVEC activeDevices;
1308 if (m_processor)
1309 m_processor->GetDevices()->GetActive(activeDevices);
1310 return CCECDeviceMap::ToLogicalAddresses(activeDevices);
1311 }
1312
1313 bool CCECClient::IsActiveDevice(const cec_logical_address iAddress)
1314 {
1315 cec_logical_addresses activeDevices = GetActiveDevices();
1316 return activeDevices.IsSet(iAddress);
1317 }
1318
1319 bool CCECClient::IsActiveDeviceType(const cec_device_type type)
1320 {
1321 CECDEVICEVEC activeDevices;
1322 if (m_processor)
1323 m_processor->GetDevices()->GetActive(activeDevices);
1324 CCECDeviceMap::FilterType(type, activeDevices);
1325 return !activeDevices.empty();
1326 }
1327
1328 cec_logical_address CCECClient::GetActiveSource(void)
1329 {
1330 return m_processor ? m_processor->GetActiveSource() : CECDEVICE_UNKNOWN;
1331 }
1332
1333 bool CCECClient::IsActiveSource(const cec_logical_address iAddress)
1334 {
1335 return m_processor ? m_processor->IsActiveSource(iAddress) : false;
1336 }
1337
1338 bool CCECClient::SetStreamPath(const cec_logical_address iAddress)
1339 {
1340 uint16_t iPhysicalAddress = GetDevicePhysicalAddress(iAddress);
1341 if (iPhysicalAddress != CEC_INVALID_PHYSICAL_ADDRESS)
1342 return SetStreamPath(iPhysicalAddress);
1343 return false;
1344 }
1345
1346 bool CCECClient::SetStreamPath(const uint16_t iPhysicalAddress)
1347 {
1348 bool bReturn(false);
1349
1350 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_TV);
1351 if (device)
1352 {
1353 device->SetStreamPath(iPhysicalAddress);
1354 bReturn = device->GetHandler()->TransmitSetStreamPath(iPhysicalAddress, false);
1355 device->MarkHandlerReady();
1356 }
1357 else
1358 {
1359 LIB_CEC->AddLog(CEC_LOG_ERROR, "only the TV is allowed to send CEC_OPCODE_SET_STREAM_PATH");
1360 }
1361
1362 return bReturn;
1363 }
1364
1365 cec_logical_addresses CCECClient::GetLogicalAddresses(void)
1366 {
1367 cec_logical_addresses addresses;
1368 CLockObject lock(m_mutex);
1369 addresses = m_configuration.logicalAddresses;
1370 return addresses;
1371 }
1372
1373 bool CCECClient::CanPersistConfiguration(void)
1374 {
1375 return m_processor ? m_processor->CanPersistConfiguration() : false;
1376 }
1377
1378 bool CCECClient::PersistConfiguration(const libcec_configuration &configuration)
1379 {
1380 return m_processor && IsRegistered() ?
1381 m_processor->PersistConfiguration(configuration) :
1382 false;
1383 }
1384
1385 void CCECClient::RescanActiveDevices(void)
1386 {
1387 if (m_processor)
1388 m_processor->RescanActiveDevices();
1389 }
1390
1391 bool CCECClient::IsLibCECActiveSource(void)
1392 {
1393 bool bReturn(false);
1394 if (m_processor)
1395 {
1396 cec_logical_address activeSource = m_processor->GetActiveSource();
1397 CCECBusDevice *device = m_processor->GetDevice(activeSource);
1398 if (device)
1399 bReturn = device->IsHandledByLibCEC();
1400 }
1401 return bReturn;
1402 }
1403
1404 void CCECClient::SourceActivated(const cec_logical_address logicalAddress)
1405 {
1406 LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> source activated: %s (%x)", ToString(logicalAddress), logicalAddress);
1407 CallbackSourceActivated(true, logicalAddress);
1408 }
1409
1410 void CCECClient::SourceDeactivated(const cec_logical_address logicalAddress)
1411 {
1412 LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> source deactivated: %s (%x)", ToString(logicalAddress), logicalAddress);
1413 CallbackSourceActivated(false, logicalAddress);
1414 }
1415
1416 void CCECClient::CallbackAddCommand(const cec_command &command)
1417 {
1418 CLockObject lock(m_cbMutex);
1419 if (m_configuration.callbacks && m_configuration.callbacks->CBCecCommand)
1420 m_configuration.callbacks->CBCecCommand(m_configuration.callbackParam, command);
1421 }
1422
1423 void CCECClient::CallbackAddKey(const cec_keypress &key)
1424 {
1425 CLockObject lock(m_cbMutex);
1426 if (m_configuration.callbacks && m_configuration.callbacks->CBCecKeyPress)
1427 m_configuration.callbacks->CBCecKeyPress(m_configuration.callbackParam, key);
1428 }
1429
1430 void CCECClient::CallbackAddLog(const cec_log_message &message)
1431 {
1432 CLockObject lock(m_cbMutex);
1433 if (m_configuration.callbacks && m_configuration.callbacks->CBCecLogMessage)
1434 m_configuration.callbacks->CBCecLogMessage(m_configuration.callbackParam, message);
1435 }
1436
1437 void CCECClient::CallbackConfigurationChanged(const libcec_configuration &config)
1438 {
1439 CLockObject lock(m_cbMutex);
1440 if (m_configuration.callbacks &&
1441 m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0 &&
1442 m_configuration.callbacks->CBCecConfigurationChanged &&
1443 m_processor->CECInitialised())
1444 m_configuration.callbacks->CBCecConfigurationChanged(m_configuration.callbackParam, config);
1445 }
1446
1447 void CCECClient::CallbackSourceActivated(bool bActivated, const cec_logical_address logicalAddress)
1448 {
1449 CLockObject lock(m_cbMutex);
1450 if (m_configuration.callbacks &&
1451 m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_7_1 &&
1452 m_configuration.callbacks->CBCecSourceActivated)
1453 m_configuration.callbacks->CBCecSourceActivated(m_configuration.callbackParam, logicalAddress, bActivated ? 1 : 0);
1454 }
1455
1456 void CCECClient::CallbackAlert(const libcec_alert type, const libcec_parameter &param)
1457 {
1458 CLockObject lock(m_cbMutex);
1459 if (m_configuration.callbacks &&
1460 m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_0 &&
1461 m_configuration.callbacks->CBCecAlert)
1462 m_configuration.callbacks->CBCecAlert(m_configuration.callbackParam, type, param);
1463 }
1464
1465 int CCECClient::CallbackMenuStateChanged(const cec_menu_state newState)
1466 {
1467 CLockObject lock(m_cbMutex);
1468 if (m_configuration.callbacks &&
1469 m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_2 &&
1470 m_configuration.callbacks->CBCecMenuStateChanged)
1471 return m_configuration.callbacks->CBCecMenuStateChanged(m_configuration.callbackParam, newState);
1472 return 0;
1473 }