cec: more LG loop fix hacks
[deb_libcec.git] / src / lib / implementations / SLCommandHandler.cpp
index 31376e59b1c12cbc66c1b34d86c7e80285db1d6b..2a8beab45226c85ceea6ce08b718681d4519e1e8 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the libCEC(R) library.
  *
- * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited.  All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited.  All rights reserved.
  * libCEC(R) is an original work, containing original code.
  *
  * libCEC(R) is a trademark of Pulse-Eight Limited.
@@ -34,8 +34,6 @@
 #include "../devices/CECBusDevice.h"
 #include "../devices/CECPlaybackDevice.h"
 #include "../CECProcessor.h"
-#include "../platform/timeutils.h"
-#include "../platform/threads.h"
 
 using namespace CEC;
 
@@ -50,16 +48,16 @@ using namespace CEC;
 
 CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
     CCECCommandHandler(busDevice),
-    m_bAwaitingReceiveFailed(false),
     m_bSLEnabled(false),
-    m_bPowerStateReset(false)
+    m_bPowerStateReset(false),
+    m_bActiveSourceSent(false)
 {
+  m_vendorId = CEC_VENDOR_LG;
   CCECBusDevice *primary = m_processor->GetPrimaryDevice();
 
   /* imitate LG devices */
-  if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
+  if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
     primary->SetVendorId(CEC_VENDOR_LG);
-  SetLGDeckStatus();
 
   /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
   if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
@@ -72,24 +70,6 @@ CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
   m_busDevice->SetMenuLanguage(lang);
 }
 
-
-void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
-{
-  CCECCommandHandler::HandlePoll(iInitiator, iDestination);
-  m_bAwaitingReceiveFailed = true;
-}
-
-bool CSLCommandHandler::HandleReceiveFailed(void)
-{
-  if (m_bAwaitingReceiveFailed)
-  {
-    m_bAwaitingReceiveFailed = false;
-    return false;
-  }
-
-  return true;
-}
-
 bool CSLCommandHandler::InitHandler(void)
 {
   if (m_bHandlerInited)
@@ -101,12 +81,16 @@ bool CSLCommandHandler::InitHandler(void)
   if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
     primary->TransmitVendorID(CECDEVICE_TV, false);
 
-  primary->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
+  primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
   return true;
 }
 
 bool CSLCommandHandler::ActivateSource(void)
 {
+  if (m_bActiveSourceSent)
+    return false;
+  m_bActiveSourceSent = true;
+
   CCECBusDevice *primary = m_processor->GetPrimaryDevice();
   primary->SetActiveSource();
   primary->TransmitActiveSource();
@@ -126,13 +110,24 @@ bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
   return true;
 }
 
+bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
+{
+  SetVendorId(command);
+
+  cec_command response;
+  cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
+  return Transmit(response);
+}
+
 bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
 {
   CCECBusDevice *primary = m_processor->GetPrimaryDevice();
   if (primary->GetPowerStatus(false) == CEC_POWER_STATUS_ON && !m_bPowerStateReset && !m_bSLEnabled)
   {
+    // reset to standby->on while SL hasn't been initialised
     m_bPowerStateReset = true;
-    primary->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
+    m_bActiveSourceSent = false;
+    primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
   }
 
   return CCECCommandHandler::HandleFeatureAbort(command);
@@ -140,11 +135,11 @@ bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
 
 bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
 {
-  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
   {
     CCECBusDevice *device = GetDevice(command.destination);
     if (device)
-      return device->TransmitPhysicalAddress();
+      return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on
   }
 
   return false;
@@ -182,6 +177,7 @@ bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
 
 void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
 {
+  m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
   TransmitVendorCommand0205(command.destination, command.initiator);
 }
 
@@ -206,7 +202,6 @@ void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
     device->TransmitPowerState(command.initiator);
     device->SetPowerStatus(CEC_POWER_STATUS_ON);
 
-    SetLGDeckStatus();
     device->SetActiveSource();
     TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
   }
@@ -225,12 +220,9 @@ void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &comm
 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
 {
   m_bSLEnabled = true;
-  SetLGDeckStatus();
-
   CCECBusDevice *primary = m_processor->GetPrimaryDevice();
+  primary->SetPowerStatus(CEC_POWER_STATUS_ON);
 
-  primary->SetActiveSource();
-  TransmitImageViewOn(primary->GetLogicalAddress(), command.initiator);
   TransmitVendorCommand05(primary->GetLogicalAddress(), command.initiator);
 }
 
@@ -239,18 +231,51 @@ void CSLCommandHandler::TransmitVendorCommand05(const cec_logical_address iSourc
   cec_command response;
   cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
   response.PushBack(SL_COMMAND_CONNECT_ACCEPT);
-  response.PushBack((uint8_t)iSource);
+  response.PushBack((uint8_t)m_processor->m_busDevices[iSource]->GetType());
   Transmit(response, false);
 }
 
-void CSLCommandHandler::SetLGDeckStatus(void)
+bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
 {
-  /* LG TVs only route keypresses when the deck status is set to 0x20 */
-  CCECBusDevice *device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
-  if (device)
-    ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
+  if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
+  {
+    CCECBusDevice *device = GetDevice(command.destination);
+    if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
+    {
+      if (command.parameters.size > 0)
+      {
+        ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
+        if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
+        {
+          return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator) &&
+              device->TransmitImageViewOn() &&
+              device->TransmitPhysicalAddress();
+        }
+        else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
+        {
+          return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
+        }
+      }
+    }
+    return CCECCommandHandler::HandleGiveDeckStatus(command);
+  }
 
-  device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_RECORDING_DEVICE);
-  if (device)
-    ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
+  return false;
+}
+
+bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
+{
+  if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
+  {
+    CCECBusDevice *device = GetDevice(command.destination);
+    if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON)
+      return device->TransmitPowerState(command.initiator);
+    else if (!ActivateSource())
+    {
+      // assume that we've bugged out, reset
+      device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
+    }
+  }
+
+  return false;
 }