removed deprecated methods and fields
[deb_libcec.git] / src / lib / CECClient.cpp
index 24f196f49287008800a01c1b88f76ff4d0463259..9dc9bff85d107816abc55de7461feae9d8bb6137 100644 (file)
  *     http://www.pulse-eight.net/
  */
 
+#include "env.h"
 #include "CECClient.h"
+
 #include "CECProcessor.h"
 #include "LibCEC.h"
+#include "CECTypeUtils.h"
 #include "devices/CECPlaybackDevice.h"
 #include "devices/CECAudioSystem.h"
 #include "devices/CECTV.h"
+#include "implementations/CECCommandHandler.h"
 
 using namespace CEC;
 using namespace PLATFORM;
 
 #define LIB_CEC     m_processor->GetLib()
-#define ToString(x) LIB_CEC->ToString(x)
+#define ToString(x) CCECTypeUtils::ToString(x)
+
+#define COMBO_KEY        CEC_USER_CONTROL_CODE_STOP
+#define COMBO_TIMEOUT_MS 1000
 
 CCECClient::CCECClient(CCECProcessor *processor, const libcec_configuration &configuration) :
     m_processor(processor),
     m_bInitialised(false),
     m_bRegistered(false),
     m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
-    m_buttontime(0)
+    m_buttontime(0),
+    m_iPreventForwardingPowerOffCommand(0)
 {
+  m_configuration.Clear();
   // set the initial configuration
   SetConfiguration(configuration);
 }
@@ -119,12 +128,9 @@ bool CCECClient::OnRegister(void)
   // set the physical address
   SetPhysicalAddress(m_configuration);
 
-  // ensure that we know the vendor id of the TV, so we are using the correct handler
-  m_processor->GetTV()->GetVendorId(GetPrimaryLogicalAdddress());
-
   // make the primary device the active source if the option is set
   if (m_configuration.bActivateSource == 1)
-    GetPrimaryDevice()->ActivateSource();
+    GetPrimaryDevice()->ActivateSource(500);
 
   return true;
 }
@@ -138,7 +144,7 @@ bool CCECClient::SetHDMIPort(const cec_logical_address iBaseDevice, const uint8_
       iPort > CEC_MAX_HDMI_PORTNUMBER)
     return bReturn;
 
-  LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
+  LIB_CEC->AddLog(CEC_LOG_NOTICE, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
 
   // update the configuration
   {
@@ -182,7 +188,7 @@ bool CCECClient::SetHDMIPort(const cec_logical_address iBaseDevice, const uint8_
   // and set the address
   SetDevicePhysicalAddress(iPhysicalAddress);
 
-  ConfigurationChanged(m_configuration);
+  CallbackConfigurationChanged(m_configuration);
 
   return bReturn;
 }
@@ -194,15 +200,19 @@ void CCECClient::ResetPhysicalAddress(void)
 
 void CCECClient::SetPhysicalAddress(const libcec_configuration &configuration)
 {
-  // try to autodetect the address
   bool bPASet(false);
-  if (m_processor->CECInitialised() && configuration.bAutodetectAddress == 1)
-    bPASet = AutodetectPhysicalAddress();
 
-  // try to use physical address setting
+  // override the physical address from configuration.iPhysicalAddress if it's set
   if (!bPASet && CLibCEC::IsValidPhysicalAddress(configuration.iPhysicalAddress))
     bPASet = SetPhysicalAddress(configuration.iPhysicalAddress);
 
+  // try to autodetect the address
+  if (!bPASet && m_processor->CECInitialised())
+  {
+    bPASet = AutodetectPhysicalAddress();
+    m_configuration.bAutodetectAddress = bPASet ? 1 : 0;
+  }
+
   // use the base device + hdmi port settings
   if (!bPASet)
     bPASet = SetHDMIPort(configuration.baseDevice, configuration.iHDMIPort);
@@ -219,37 +229,69 @@ void CCECClient::SetPhysicalAddress(const libcec_configuration &configuration)
 bool CCECClient::SetPhysicalAddress(const uint16_t iPhysicalAddress)
 {
   // update the configuration
+  bool bChanged(true);
   {
     CLockObject lock(m_mutex);
     if (m_configuration.iPhysicalAddress == iPhysicalAddress)
-    {
-      LIB_CEC->AddLog(CEC_LOG_DEBUG, "physical address unchanged (%04X)", iPhysicalAddress);
-      return true;
-    }
+      bChanged = false;
     else
-    {
       m_configuration.iPhysicalAddress = iPhysicalAddress;
-      LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting physical address to '%04X'", iPhysicalAddress);
-    }
+  }
+  if (!bChanged)
+  {
+    LIB_CEC->AddLog(CEC_LOG_DEBUG, "physical address unchanged (%04X)", iPhysicalAddress);
+    return true;
   }
 
-  // persist the new configuration
-  m_processor->PersistConfiguration(m_configuration);
+  LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting physical address to '%04X'", iPhysicalAddress);
 
   // set the physical address for each device
   SetDevicePhysicalAddress(iPhysicalAddress);
 
   // and send back the updated configuration
-  ConfigurationChanged(m_configuration);
+  CallbackConfigurationChanged(m_configuration);
 
   return true;
 }
 
+void CCECClient::SetSupportedDeviceTypes(void)
+{
+  cec_device_type_list types;
+  types.Clear();
+
+  // get the command handler for the tv
+  CCECCommandHandler *tvHandler = m_processor->GetTV()->GetHandler();
+  if (!tvHandler)
+    return;
+
+  // check all device types
+  for (uint8_t iPtr = 0; iPtr < 5; iPtr++)
+  {
+    if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
+      continue;
+
+    // get the supported device type. the handler will replace types it doesn't support by one it does support
+    cec_device_type type = tvHandler->GetReplacementDeviceType(m_configuration.deviceTypes.types[iPtr]);
+    if (!types.IsSet(type))
+      types.Add(type);
+  }
+  m_processor->GetTV()->MarkHandlerReady();
+
+  // set the new type list
+  m_configuration.deviceTypes = types;
+
+  // persist the new configuration
+  PersistConfiguration(m_configuration);
+}
+
 bool CCECClient::AllocateLogicalAddresses(void)
 {
   // reset all previous LAs that were set
   m_configuration.logicalAddresses.Clear();
 
+  // get the supported device types from the command handler of the TV
+  SetSupportedDeviceTypes();
+
   // display an error if no device types are set
   if (m_configuration.deviceTypes.IsEmpty())
   {
@@ -286,6 +328,9 @@ bool CCECClient::AllocateLogicalAddresses(void)
     m_configuration.logicalAddresses.Set(address);
   }
 
+  // persist the new configuration
+  PersistConfiguration(m_configuration);
+
   return true;
 }
 
@@ -294,11 +339,11 @@ cec_logical_address CCECClient::AllocateLogicalAddressRecordingDevice(void)
   cec_logical_address retVal(CECDEVICE_UNKNOWN);
 
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
-  if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1))
+  if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1, m_configuration.cecVersion))
     retVal = CECDEVICE_RECORDINGDEVICE1;
-  else if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2))
+  else if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2, m_configuration.cecVersion))
     retVal = CECDEVICE_RECORDINGDEVICE2;
-  else if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3))
+  else if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3, m_configuration.cecVersion))
     retVal = CECDEVICE_RECORDINGDEVICE3;
 
   return retVal;
@@ -309,13 +354,13 @@ cec_logical_address CCECClient::AllocateLogicalAddressTuner(void)
   cec_logical_address retVal(CECDEVICE_UNKNOWN);
 
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
-  if (m_processor->TryLogicalAddress(CECDEVICE_TUNER1))
+  if (m_processor->TryLogicalAddress(CECDEVICE_TUNER1, m_configuration.cecVersion))
     retVal = CECDEVICE_TUNER1;
-  else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER2))
+  else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER2, m_configuration.cecVersion))
     retVal = CECDEVICE_TUNER2;
-  else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER3))
+  else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER3, m_configuration.cecVersion))
     retVal = CECDEVICE_TUNER3;
-  else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER4))
+  else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER4, m_configuration.cecVersion))
     retVal = CECDEVICE_TUNER4;
 
   return retVal;
@@ -326,11 +371,11 @@ cec_logical_address CCECClient::AllocateLogicalAddressPlaybackDevice(void)
   cec_logical_address retVal(CECDEVICE_UNKNOWN);
 
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
-  if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1))
+  if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1, m_configuration.cecVersion))
     retVal = CECDEVICE_PLAYBACKDEVICE1;
-  else if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2))
+  else if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2, m_configuration.cecVersion))
     retVal = CECDEVICE_PLAYBACKDEVICE2;
-  else if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3))
+  else if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3, m_configuration.cecVersion))
     retVal = CECDEVICE_PLAYBACKDEVICE3;
 
   return retVal;
@@ -341,7 +386,7 @@ cec_logical_address CCECClient::AllocateLogicalAddressAudioSystem(void)
   cec_logical_address retVal(CECDEVICE_UNKNOWN);
 
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audiosystem'");
-  if (m_processor->TryLogicalAddress(CECDEVICE_AUDIOSYSTEM))
+  if (m_processor->TryLogicalAddress(CECDEVICE_AUDIOSYSTEM, m_configuration.cecVersion))
     retVal = CECDEVICE_AUDIOSYSTEM;
 
   return retVal;
@@ -363,31 +408,36 @@ CCECBusDevice *CCECClient::GetDeviceByType(const cec_device_type type) const
 
 bool CCECClient::ChangeDeviceType(const cec_device_type from, const cec_device_type to)
 {
-  LIB_CEC->AddLog(CEC_LOG_NOTICE, "changing device type '%s' into '%s'", ToString(from), ToString(to));
-
-  CLockObject lock(m_mutex);
+  if (from == to)
+    return true;
 
-  // get the previous device that was allocated
-  CCECBusDevice *previousDevice = GetDeviceByType(from);
-  if (!previousDevice)
-    return false;
+  LIB_CEC->AddLog(CEC_LOG_NOTICE, "changing device type '%s' into '%s'", ToString(from), ToString(to));
 
-  // change the type in the device type list
-  bool bChanged(false);
-  for (uint8_t iPtr = 0; iPtr < 5; iPtr++)
   {
-    if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
-      continue;
+    CLockObject lock(m_mutex);
 
-    if (m_configuration.deviceTypes.types[iPtr] == from)
-    {
-      bChanged = true;
-      m_configuration.deviceTypes.types[iPtr] = to;
-    }
-    else if (m_configuration.deviceTypes.types[iPtr] == to && bChanged)
+    // get the previous device that was allocated
+    CCECBusDevice *previousDevice = GetDeviceByType(from);
+    if (!previousDevice)
+      return false;
+
+    // change the type in the device type list
+    bool bChanged(false);
+    for (uint8_t iPtr = 0; iPtr < 5; iPtr++)
     {
-      // ensure that dupes are removed
-      m_configuration.deviceTypes.types[iPtr] = CEC_DEVICE_TYPE_RESERVED;
+      if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
+        continue;
+
+      if (m_configuration.deviceTypes.types[iPtr] == from)
+      {
+        bChanged = true;
+        m_configuration.deviceTypes.types[iPtr] = to;
+      }
+      else if (m_configuration.deviceTypes.types[iPtr] == to && bChanged)
+      {
+        // ensure that dupes are removed
+        m_configuration.deviceTypes.types[iPtr] = CEC_DEVICE_TYPE_RESERVED;
+      }
     }
   }
 
@@ -395,29 +445,38 @@ bool CCECClient::ChangeDeviceType(const cec_device_type from, const cec_device_t
   if (!m_processor->RegisterClient(this))
     return false;
 
+  // persist the new configuration
+  PersistConfiguration(m_configuration);
+
   return true;
 }
 
 bool CCECClient::SetLogicalAddress(const cec_logical_address iLogicalAddress)
 {
-  CLockObject lock(m_mutex);
+  bool bReturn(true);
+
   if (GetPrimaryLogicalAdddress() != iLogicalAddress)
   {
+    LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< setting primary logical address to %1x", iLogicalAddress);
     {
       CLockObject lock(m_mutex);
-      LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< setting primary logical address to %1x", iLogicalAddress);
       m_configuration.logicalAddresses.primary = iLogicalAddress;
       m_configuration.logicalAddresses.Set(iLogicalAddress);
     }
-    return m_processor->RegisterClient(this);
+
+    bReturn = m_processor->RegisterClient(this);
+
+    // persist the new configuration
+    if (bReturn)
+      PersistConfiguration(m_configuration);
   }
 
-  return true;
+  return bReturn;
 }
 
-bool CCECClient::Transmit(const cec_command &data)
+bool CCECClient::Transmit(const cec_command &data, bool bIsReply)
 {
-  return m_processor ? m_processor->Transmit(data) : false;
+  return m_processor ? m_processor->Transmit(data, bIsReply) : false;
 }
 
 bool CCECClient::SendPowerOnDevices(const cec_logical_address address /* = CECDEVICE_TV */)
@@ -518,7 +577,7 @@ bool CCECClient::SendSetDeckControlMode(const cec_deck_control_mode mode, bool b
     // and set the deck control mode if there is a match
     device->SetDeckControlMode(mode);
     if (bSendUpdate)
-      return device->TransmitDeckStatus(CECDEVICE_TV);
+      return device->TransmitDeckStatus(CECDEVICE_TV, false);
     return true;
   }
 
@@ -535,7 +594,7 @@ bool CCECClient::SendSetDeckInfo(const cec_deck_info info, bool bSendUpdate /* =
     // and set the deck status if there is a match
     device->SetDeckStatus(info);
     if (bSendUpdate)
-      return device->AsPlaybackDevice()->TransmitDeckStatus(CECDEVICE_TV);
+      return device->AsPlaybackDevice()->TransmitDeckStatus(CECDEVICE_TV, false);
     return true;
   }
 
@@ -553,7 +612,7 @@ bool CCECClient::SendSetMenuState(const cec_menu_state state, bool bSendUpdate /
   {
     (*it)->SetMenuState(state);
     if (bSendUpdate)
-      (*it)->TransmitMenuState(CECDEVICE_TV);
+      (*it)->TransmitMenuState(CECDEVICE_TV, false);
   }
 
   return true;
@@ -581,7 +640,7 @@ bool CCECClient::SendSetOSDString(const cec_logical_address iLogicalAddress, con
 {
   CCECBusDevice *primary = GetPrimaryDevice();
   if (primary)
-    return primary->TransmitOSDString(iLogicalAddress, duration, strMessage);
+    return primary->TransmitOSDString(iLogicalAddress, duration, strMessage, false);
 
   return false;
 }
@@ -678,21 +737,19 @@ uint8_t CCECClient::SendMuteAudio(void)
 
 bool CCECClient::SendKeypress(const cec_logical_address iDestination, const cec_user_control_code key, bool bWait /* = true */)
 {
-  CCECBusDevice *device = GetPrimaryDevice();
   CCECBusDevice *dest = m_processor->GetDevice(iDestination);
 
-  return device && dest ?
-      device->TransmitKeypress(GetPrimaryLogicalAdddress(), key, bWait) :
+  return dest ?
+      dest->TransmitKeypress(GetPrimaryLogicalAdddress(), key, bWait) :
       false;
 }
 
 bool CCECClient::SendKeyRelease(const cec_logical_address iDestination, bool bWait /* = true */)
 {
-  CCECBusDevice *device = GetPrimaryDevice();
   CCECBusDevice *dest = m_processor->GetDevice(iDestination);
 
-  return device && dest ?
-      device->TransmitKeyRelease(GetPrimaryLogicalAdddress(), bWait) :
+  return dest ?
+      dest->TransmitKeyRelease(GetPrimaryLogicalAdddress(), bWait) :
       false;
 }
 
@@ -748,6 +805,14 @@ bool CCECClient::GetCurrentConfiguration(libcec_configuration &configuration)
     configuration.bMonitorOnly            = m_configuration.bMonitorOnly;
   }
 
+  // client version 1.8.0
+  if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_8_0)
+    configuration.cecVersion              = m_configuration.cecVersion;
+
+  // client version 1.8.2
+  if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_8_2)
+    configuration.adapterType             = m_configuration.adapterType;
+
   return true;
 }
 
@@ -805,6 +870,14 @@ bool CCECClient::SetConfiguration(const libcec_configuration &configuration)
       m_configuration.bMonitorOnly = configuration.bMonitorOnly;
     }
 
+    // client version 1.8.0
+    if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_8_0)
+      m_configuration.cecVersion   = configuration.cecVersion;
+
+    // client version 1.8.2
+    if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_8_2)
+      m_configuration.adapterType  = configuration.adapterType;
+
     // ensure that there is at least 1 device type set
     if (m_configuration.deviceTypes.IsEmpty())
       m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
@@ -830,7 +903,8 @@ bool CCECClient::SetConfiguration(const libcec_configuration &configuration)
     SetPhysicalAddress(configuration);
   }
 
-  m_processor->PersistConfiguration(m_configuration);
+  // persist the new configuration
+  PersistConfiguration(m_configuration);
 
   if (!primary)
     primary = GetPrimaryDevice();
@@ -851,84 +925,93 @@ bool CCECClient::SetConfiguration(const libcec_configuration &configuration)
 
 void CCECClient::AddCommand(const cec_command &command)
 {
-  CLockObject lock(m_mutex);
-
-  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);
+  // don't forward the standby opcode more than once every 10 seconds
+  if (command.opcode == CEC_OPCODE_STANDBY)
+  {
+    CLockObject lock(m_mutex);
+    if (m_iPreventForwardingPowerOffCommand != 0 &&
+        m_iPreventForwardingPowerOffCommand > GetTimeMs())
+      return;
+    else
+      m_iPreventForwardingPowerOffCommand = GetTimeMs() + CEC_FORWARD_STANDBY_MIN_INTERVAL;
+  }
 
-  if (m_configuration.callbacks && m_configuration.callbacks->CBCecCommand)
-    m_configuration.callbacks->CBCecCommand(m_configuration.callbackParam, command);
-  else if (!m_commandBuffer.Push(command))
-    LIB_CEC->AddLog(CEC_LOG_WARNING, "command buffer is full");
+  if (command.destination == CECDEVICE_BROADCAST || GetLogicalAddresses().IsSet(command.destination))
+  {
+    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);
+    CallbackAddCommand(command);
+  }
 }
 
 int CCECClient::MenuStateChanged(const cec_menu_state newState)
 {
-  CLockObject lock(m_mutex);
-
   LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> %s: %s", ToString(CEC_OPCODE_MENU_REQUEST), ToString(newState));
-
-  if (m_configuration.callbacks &&
-      m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_2 &&
-      m_configuration.callbacks->CBCecMenuStateChanged)
-    return m_configuration.callbacks->CBCecMenuStateChanged(m_configuration.callbackParam, newState);
-
-  return 0;
-}
-
-void CCECClient::Alert(const libcec_alert type, const libcec_parameter &param)
-{
-  CLockObject lock(m_mutex);
-
-  if (m_configuration.callbacks &&
-      m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_0 &&
-      m_configuration.callbacks->CBCecAlert)
-    m_configuration.callbacks->CBCecAlert(m_configuration.callbackParam, type, param);
+  return CallbackMenuStateChanged(newState);
 }
 
-void CCECClient::AddLog(const cec_log_message &message)
+void CCECClient::AddKey(bool bSendComboKey /* = false */)
 {
-  CLockObject lock(m_logMutex);
-  if (m_configuration.callbacks && m_configuration.callbacks->CBCecLogMessage)
-    m_configuration.callbacks->CBCecLogMessage(m_configuration.callbackParam, message);
-  else
-    m_logBuffer.Push(message);
-}
-
-void CCECClient::AddKey(void)
-{
-  CLockObject lock(m_mutex);
+  cec_keypress key;
+  key.keycode = CEC_USER_CONTROL_CODE_UNKNOWN;
 
-  if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
   {
-    cec_keypress key;
+    CLockObject lock(m_mutex);
+    if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
+    {
+      key.duration = (unsigned int) (GetTimeMs() - m_buttontime);
 
-    key.duration = (unsigned int) (GetTimeMs() - m_buttontime);
-    key.keycode = m_iCurrentButton;
-    LIB_CEC->AddLog(CEC_LOG_DEBUG, "key released: %1x", key.keycode);
+      if (key.duration > COMBO_TIMEOUT_MS || m_iCurrentButton != COMBO_KEY || bSendComboKey)
+      {
+        key.keycode = m_iCurrentButton;
 
-    if (m_configuration.callbacks && m_configuration.callbacks->CBCecKeyPress)
-      m_configuration.callbacks->CBCecKeyPress(m_configuration.callbackParam, key);
-    else
-      m_keyBuffer.Push(key);
-    m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
+        m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
+        m_buttontime = 0;
+      }
+    }
   }
 
-  m_buttontime = 0;
+  if (key.keycode != CEC_USER_CONTROL_CODE_UNKNOWN)
+  {
+    LIB_CEC->AddLog(CEC_LOG_DEBUG, "key released: %s (%1x)", ToString(key.keycode), key.keycode);
+    CallbackAddKey(key);
+  }
 }
 
 void CCECClient::AddKey(const cec_keypress &key)
 {
-  CLockObject lock(m_mutex);
+  // send back the previous key if there is one
+  AddKey();
 
-  LIB_CEC->AddLog(CEC_LOG_DEBUG, "key pressed: %1x", key.keycode);
+  cec_keypress transmitKey(key);
 
-  if (m_configuration.callbacks && m_configuration.callbacks->CBCecKeyPress)
-    m_configuration.callbacks->CBCecKeyPress(m_configuration.callbackParam, key);
-  else
-    m_keyBuffer.Push(key);
+  {
+    CLockObject lock(m_mutex);
+    if (key.duration > 0 || key.keycode > CEC_USER_CONTROL_CODE_MAX)
+    {
+      transmitKey.keycode = CEC_USER_CONTROL_CODE_UNKNOWN;
+    }
+    else if (m_iCurrentButton == COMBO_KEY)
+    {
+      // stop + ok -> exit
+      if (key.keycode == CEC_USER_CONTROL_CODE_SELECT)
+        transmitKey.keycode = CEC_USER_CONTROL_CODE_EXIT;
+      // stop + pause -> root menu
+      else if (key.keycode == CEC_USER_CONTROL_CODE_ROOT_MENU)
+        transmitKey.keycode = CEC_USER_CONTROL_CODE_ROOT_MENU;
+      // stop + play -> dot (which is handled as context menu in xbmc)
+      else if (key.keycode == CEC_USER_CONTROL_CODE_PLAY)
+        transmitKey.keycode = CEC_USER_CONTROL_CODE_DOT;
+      // default, send back the previous key
+      else
+        AddKey(true);
+    }
 
-  m_iCurrentButton = key.duration > 0 ? CEC_USER_CONTROL_CODE_UNKNOWN : key.keycode;
-  m_buttontime = key.duration > 0 ? 0 : GetTimeMs();
+    m_iCurrentButton = transmitKey.keycode;
+    m_buttontime = m_iCurrentButton == CEC_USER_CONTROL_CODE_UNKNOWN || key.duration > 0 ? 0 : GetTimeMs();
+  }
+
+  LIB_CEC->AddLog(CEC_LOG_DEBUG, "key pressed: %s (%1x)", ToString(transmitKey.keycode), transmitKey.keycode);
+  CallbackAddKey(transmitKey);
 }
 
 void CCECClient::SetCurrentButton(const cec_user_control_code iButtonCode)
@@ -943,27 +1026,35 @@ void CCECClient::SetCurrentButton(const cec_user_control_code iButtonCode)
 
 void CCECClient::CheckKeypressTimeout(void)
 {
-  if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && GetTimeMs() - m_buttontime > CEC_BUTTON_TIMEOUT)
+  cec_keypress key;
+
   {
-    AddKey();
-    m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
-  }
-}
+    CLockObject lock(m_mutex);
+    uint64_t iNow = GetTimeMs();
 
-void CCECClient::ConfigurationChanged(const libcec_configuration &config)
-{
-  CLockObject lock(m_mutex);
+    if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN &&
+          ((m_iCurrentButton == COMBO_KEY && iNow - m_buttontime > COMBO_TIMEOUT_MS) ||
+          (m_iCurrentButton != COMBO_KEY && iNow - m_buttontime > CEC_BUTTON_TIMEOUT)))
+    {
+      key.duration = (unsigned int) (iNow - m_buttontime);
+      key.keycode = m_iCurrentButton;
 
-  if (m_configuration.callbacks &&
-      m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0 &&
-      m_configuration.callbacks->CBCecConfigurationChanged &&
-      m_processor->CECInitialised())
-    m_configuration.callbacks->CBCecConfigurationChanged(m_configuration.callbackParam, config);
+      m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
+      m_buttontime = 0;
+    }
+    else
+    {
+      return;
+    }
+  }
+
+  LIB_CEC->AddLog(CEC_LOG_DEBUG, "key auto-released: %s (%1x)", ToString(key.keycode), key.keycode);
+  CallbackAddKey(key);
 }
 
 bool CCECClient::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks)
 {
-  CLockObject lock(m_mutex);
+  CLockObject lock(m_cbMutex);
   m_configuration.callbackParam = cbParam;
   m_configuration.callbacks     = callbacks;
   return true;
@@ -974,22 +1065,7 @@ bool CCECClient::PingAdapter(void)
   return m_processor ? m_processor->PingAdapter() : false;
 }
 
-bool CCECClient::GetNextLogMessage(cec_log_message *message)
-{
-  return (m_logBuffer.Pop(*message));
-}
-
-bool CCECClient::GetNextKeypress(cec_keypress *key)
-{
-  return m_keyBuffer.Pop(*key);
-}
-
-bool CCECClient::GetNextCommand(cec_command *command)
-{
-  return m_commandBuffer.Pop(*command);
-}
-
-CStdString CCECClient::GetConnectionInfo(void)
+std::string CCECClient::GetConnectionInfo(void)
 {
   CStdString strLog;
   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);
@@ -1016,7 +1092,10 @@ CStdString CCECClient::GetConnectionInfo(void)
   else
     strLog.AppendFormat(", physical address: %04x", m_configuration.iPhysicalAddress);
 
-  return strLog;
+  strLog.AppendFormat(", %s", LIB_CEC->GetLibInfo());
+
+  std::string strReturn(strLog.c_str());
+  return strReturn;
 }
 
 void CCECClient::SetTVVendorOverride(const cec_vendor_id id)
@@ -1034,6 +1113,9 @@ void CCECClient::SetTVVendorOverride(const cec_vendor_id id)
     if (tv)
       tv->SetVendorId((uint64_t)id);
   }
+
+  // persist the new configuration
+  PersistConfiguration(m_configuration);
 }
 
 cec_vendor_id CCECClient::GetTVVendorOverride(void)
@@ -1042,7 +1124,7 @@ cec_vendor_id CCECClient::GetTVVendorOverride(void)
   return (cec_vendor_id)m_configuration.tvVendor;
 }
 
-void CCECClient::SetOSDName(const CStdString &strDeviceName)
+void CCECClient::SetOSDName(const std::string &strDeviceName)
 {
   {
     CLockObject lock(m_mutex);
@@ -1052,25 +1134,32 @@ void CCECClient::SetOSDName(const CStdString &strDeviceName)
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - using OSD name '%s'", __FUNCTION__, strDeviceName.c_str());
 
   CCECBusDevice *primary = GetPrimaryDevice();
-  if (primary && !primary->GetCurrentOSDName().Equals(strDeviceName))
+  if (primary && !primary->GetCurrentOSDName().Equals(strDeviceName.c_str()))
   {
     primary->SetOSDName(strDeviceName);
     if (m_processor && m_processor->CECInitialised())
-      primary->TransmitOSDName(CECDEVICE_TV);
+      primary->TransmitOSDName(CECDEVICE_TV, false);
   }
+
+  // persist the new configuration
+  PersistConfiguration(m_configuration);
 }
 
-CStdString CCECClient::GetOSDName(void)
+std::string CCECClient::GetOSDName(void)
 {
   CLockObject lock(m_mutex);
-  CStdString strOSDName(m_configuration.strDeviceName);
+  std::string strOSDName(m_configuration.strDeviceName);
   return strOSDName;
 }
 
 void CCECClient::SetWakeDevices(const cec_logical_addresses &addresses)
 {
-  CLockObject lock(m_mutex);
-  m_configuration.wakeDevices = addresses;
+  {
+    CLockObject lock(m_mutex);
+    m_configuration.wakeDevices = addresses;
+  }
+  // persist the new configuration
+  PersistConfiguration(m_configuration);
 }
 
 cec_logical_addresses CCECClient::GetWakeDevices(void)
@@ -1125,6 +1214,9 @@ bool CCECClient::SetDeviceTypes(const cec_device_type_list &deviceTypes)
     m_configuration.deviceTypes = deviceTypes;
   }
 
+  // persist the new configuration
+  PersistConfiguration(m_configuration);
+
   if (bNeedReinit)
     LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - using primary device type '%s'", __FUNCTION__, ToString(deviceTypes[0]));
 
@@ -1142,7 +1234,10 @@ cec_device_type_list CCECClient::GetDeviceTypes(void)
 bool CCECClient::SetDevicePhysicalAddress(const uint16_t iPhysicalAddress)
 {
   if (!CLibCEC::IsValidPhysicalAddress(iPhysicalAddress))
+  {
+    LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - not setting invalid physical address %04x", __FUNCTION__, iPhysicalAddress);
     return false;
+  }
 
   // reconfigure all devices
   cec_logical_address reactivateSource(CECDEVICE_UNKNOWN);
@@ -1163,7 +1258,7 @@ bool CCECClient::SetDevicePhysicalAddress(const uint16_t iPhysicalAddress)
 
     // and transmit it
     if (IsInitialised())
-      (*it)->TransmitPhysicalAddress();
+      (*it)->TransmitPhysicalAddress(false);
   }
 
   // reactivate the previous active source
@@ -1176,6 +1271,9 @@ bool CCECClient::SetDevicePhysicalAddress(const uint16_t iPhysicalAddress)
       device->ActivateSource();
   }
 
+  // persist the new configuration
+  PersistConfiguration(m_configuration);
+
   return true;
 }
 
@@ -1185,13 +1283,9 @@ bool CCECClient::SwitchMonitoring(bool bEnable)
 
   if (m_processor)
   {
-    if (bEnable)
-      return m_processor->UnregisterClient(this);
-    else
-    {
-      m_configuration.bMonitorOnly = false;
-      return m_processor->RegisterClient(this);
-    }
+    m_processor->SwitchMonitoring(bEnable);
+    m_configuration.bMonitorOnly = bEnable;
+    return bEnable ? true: m_processor->RegisterClient(this);
   }
 
   return false;
@@ -1203,7 +1297,7 @@ bool CCECClient::PollDevice(const cec_logical_address iAddress)
   CCECBusDevice *primary = GetPrimaryDevice();
   // poll the destination, with the primary as source
   if (primary)
-    return primary->TransmitPoll(iAddress);
+    return primary->TransmitPoll(iAddress, false);
 
   return m_processor ? m_processor->PollDevice(iAddress) : false;
 }
@@ -1251,7 +1345,21 @@ bool CCECClient::SetStreamPath(const cec_logical_address iAddress)
 
 bool CCECClient::SetStreamPath(const uint16_t iPhysicalAddress)
 {
-  return m_processor ? m_processor->SetStreamPath(iPhysicalAddress) : false;
+  bool bReturn(false);
+
+  CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_TV);
+  if (device)
+  {
+    device->SetStreamPath(iPhysicalAddress);
+    bReturn = device->GetHandler()->TransmitSetStreamPath(iPhysicalAddress, false);
+    device->MarkHandlerReady();
+  }
+  else
+  {
+    LIB_CEC->AddLog(CEC_LOG_ERROR, "only the TV is allowed to send CEC_OPCODE_SET_STREAM_PATH");
+  }
+
+  return bReturn;
 }
 
 cec_logical_addresses CCECClient::GetLogicalAddresses(void)
@@ -1269,7 +1377,9 @@ bool CCECClient::CanPersistConfiguration(void)
 
 bool CCECClient::PersistConfiguration(const libcec_configuration &configuration)
 {
-  return m_processor ? m_processor->PersistConfiguration(configuration) : false;
+  return m_processor && IsRegistered() ?
+      m_processor->PersistConfiguration(configuration) :
+      false;
 }
 
 void CCECClient::RescanActiveDevices(void)
@@ -1290,3 +1400,74 @@ bool CCECClient::IsLibCECActiveSource(void)
   }
   return bReturn;
 }
+
+void CCECClient::SourceActivated(const cec_logical_address logicalAddress)
+{
+  LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> source activated: %s (%x)", ToString(logicalAddress), logicalAddress);
+  CallbackSourceActivated(true, logicalAddress);
+}
+
+void CCECClient::SourceDeactivated(const cec_logical_address logicalAddress)
+{
+  LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> source deactivated: %s (%x)", ToString(logicalAddress), logicalAddress);
+  CallbackSourceActivated(false, logicalAddress);
+}
+
+void CCECClient::CallbackAddCommand(const cec_command &command)
+{
+  CLockObject lock(m_cbMutex);
+  if (m_configuration.callbacks && m_configuration.callbacks->CBCecCommand)
+    m_configuration.callbacks->CBCecCommand(m_configuration.callbackParam, command);
+}
+
+void CCECClient::CallbackAddKey(const cec_keypress &key)
+{
+  CLockObject lock(m_cbMutex);
+  if (m_configuration.callbacks && m_configuration.callbacks->CBCecKeyPress)
+    m_configuration.callbacks->CBCecKeyPress(m_configuration.callbackParam, key);
+}
+
+void CCECClient::CallbackAddLog(const cec_log_message &message)
+{
+  CLockObject lock(m_cbMutex);
+  if (m_configuration.callbacks && m_configuration.callbacks->CBCecLogMessage)
+    m_configuration.callbacks->CBCecLogMessage(m_configuration.callbackParam, message);
+}
+
+void CCECClient::CallbackConfigurationChanged(const libcec_configuration &config)
+{
+  CLockObject lock(m_cbMutex);
+  if (m_configuration.callbacks &&
+      m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0 &&
+      m_configuration.callbacks->CBCecConfigurationChanged &&
+      m_processor->CECInitialised())
+    m_configuration.callbacks->CBCecConfigurationChanged(m_configuration.callbackParam, config);
+}
+
+void CCECClient::CallbackSourceActivated(bool bActivated, const cec_logical_address logicalAddress)
+{
+  CLockObject lock(m_cbMutex);
+  if (m_configuration.callbacks &&
+      m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_7_1 &&
+      m_configuration.callbacks->CBCecSourceActivated)
+    m_configuration.callbacks->CBCecSourceActivated(m_configuration.callbackParam, logicalAddress, bActivated ? 1 : 0);
+}
+
+void CCECClient::CallbackAlert(const libcec_alert type, const libcec_parameter &param)
+{
+  CLockObject lock(m_cbMutex);
+  if (m_configuration.callbacks &&
+      m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_0 &&
+      m_configuration.callbacks->CBCecAlert)
+    m_configuration.callbacks->CBCecAlert(m_configuration.callbackParam, type, param);
+}
+
+int CCECClient::CallbackMenuStateChanged(const cec_menu_state newState)
+{
+  CLockObject lock(m_cbMutex);
+  if (m_configuration.callbacks &&
+      m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_2 &&
+      m_configuration.callbacks->CBCecMenuStateChanged)
+    return m_configuration.callbacks->CBCecMenuStateChanged(m_configuration.callbackParam, newState);
+  return 0;
+}