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