cosmetics
[deb_libcec.git] / src / lib / implementations / VLCommandHandler.cpp
index 89f5b2eadebc5668436040c68b841e4c4b798f0d..87a3037e432a4bde4f872b7a820ee7c2e04bcf93 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.
 
 #include "VLCommandHandler.h"
 #include "../devices/CECBusDevice.h"
-#include "../util/StdString.h"
+#include "../CECProcessor.h"
+#include "../LibCEC.h"
+
+#define VL_POWER_CHANGE 0x20
+#define VL_POWERED_UP   0x00
+#define VL_POWERED_DOWN 0x01
 
 using namespace CEC;
+using namespace PLATFORM;
 
 CVLCommandHandler::CVLCommandHandler(CCECBusDevice *busDevice) :
-    CCECCommandHandler(busDevice)
+    CCECCommandHandler(busDevice),
+    m_bActiveSourcePending(false),
+    m_bPowerUpEventReceived(false)
 {
+  m_vendorId = CEC_VENDOR_PANASONIC;
 }
 
-bool CVLCommandHandler::HandleSetStreamPath(const cec_command &command)
+bool CVLCommandHandler::InitHandler(void)
 {
-  if (command.parameters.size >= 2)
+  CCECBusDevice *primary = m_processor->GetPrimaryDevice();
+  if (primary->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
+    return m_processor->ChangeDeviceType(CEC_DEVICE_TYPE_RECORDING_DEVICE, CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
+
+  return CCECCommandHandler::InitHandler();
+}
+
+bool CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
+{
+  if (command.initiator == CECDEVICE_TV &&
+      command.destination == CECDEVICE_BROADCAST &&
+      command.parameters.At(3) == VL_POWER_CHANGE)
   {
-    int streamaddr = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
-    CStdString strLog;
-    strLog.Format(">> %i requests stream path from physical address %04x", command.initiator, streamaddr);
-    m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
-    if (streamaddr == m_busDevice->GetMyPhysicalAddress())
+    if (command.parameters.At(4) == VL_POWERED_UP)
     {
-      CCECBusDevice *device = GetThisDevice();
-      CCECBusDevice *initiatorDevice = GetDevice(command.initiator);
-      if (device && initiatorDevice)
+      CLibCEC::AddLog(CEC_LOG_DEBUG, "TV powered up");
       {
-        return device->BroadcastActiveSource() &&
-               device->BroadcastActiveView() &&
-               initiatorDevice->ReportMenuState();
+        CLockObject lock(m_mutex);
+        m_bPowerUpEventReceived = true;
       }
-      return false;
+      m_processor->TransmitPendingActiveSourceCommands();
+    }
+    else if (command.parameters.At(4) == VL_POWERED_DOWN)
+      CLibCEC::AddLog(CEC_LOG_DEBUG, "TV powered down");
+    else if (command.parameters.At(4) == VL_POWERED_DOWN)
+      CLibCEC::AddLog(CEC_LOG_DEBUG, "unknown vendor command");
+
+    return true;
+  }
+
+  return CCECCommandHandler::HandleDeviceVendorCommandWithId(command);
+}
+
+bool CVLCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
+{
+  bool bPowerUpEventReceived(false);
+
+  {
+    CLockObject lock(m_mutex);
+    if (!m_bPowerUpEventReceived)
+    {
+      // just assume it's been sent when the tv is powered on
+      cec_power_status powerStatus = m_processor->m_busDevices[CECDEVICE_TV]->GetPowerStatus();
+      m_bPowerUpEventReceived = (powerStatus == CEC_POWER_STATUS_ON);
     }
+    bPowerUpEventReceived = m_bPowerUpEventReceived;
+  }
+
+  if (!bPowerUpEventReceived)
+  {
+    CLockObject lock(m_mutex);
+    // wait until we received the event
+    m_bActiveSourcePending = true;
+    return true;
+  }
+  else
+  {
+    // transmit standard active source message
+    return CCECCommandHandler::TransmitActiveSource(iInitiator, iPhysicalAddress);
+  }
+}
+
+bool CVLCommandHandler::TransmitPendingActiveSourceCommands(void)
+{
+  bool bTransmitCommand(false);
+  {
+    CLockObject lock(m_mutex);
+    bTransmitCommand = m_bActiveSourcePending;
+    m_bActiveSourcePending = false;
+  }
+
+  if (bTransmitCommand)
+  {
+    CLibCEC::AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command");
+    return CCECCommandHandler::TransmitActiveSource(m_busDevice->GetLogicalAddress(), m_busDevice->GetPhysicalAddress(false, true));
   }
   return true;
 }