cec: clean up and fix the LG command handler
[deb_libcec.git] / src / lib / devices / CECBusDevice.cpp
index 1ea78d72fdf0d6418b9cd3d22f1903b479d770a1..c2704ae56bbbe79dc91bfc9649e30a39ad821c5a 100644 (file)
@@ -92,7 +92,6 @@ bool CCECBusDevice::HandleCommand(const cec_command &command)
   }
 
   /* handle the command */
-  ReplaceHandler(true);
   bHandled = m_handler->HandleCommand(command);
 
   /* change status to present */
@@ -157,11 +156,17 @@ bool CCECBusDevice::Standby(void)
 //@{
 cec_version CCECBusDevice::GetCecVersion(bool bUpdate /* = false */)
 {
-  CLockObject lock(&m_mutex);
-  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
-      (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN))
+  bool bRequestUpdate(false);
+  {
+    CLockObject lock(&m_mutex);
+    bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
+      (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN));
+  }
+
+  if (bRequestUpdate)
     RequestCecVersion();
 
+  CLockObject lock(&m_mutex);
   return m_cecVersion;
 }
 
@@ -187,11 +192,17 @@ const char* CCECBusDevice::GetLogicalAddressName(void) const
 
 cec_menu_language &CCECBusDevice::GetMenuLanguage(bool bUpdate /* = false */)
 {
-  CLockObject lock(&m_mutex);
-  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
-      (bUpdate || !strcmp(m_menuLanguage.language, "???")))
+  bool bRequestUpdate(false);
+  {
+    CLockObject lock(&m_mutex);
+    bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
+        (bUpdate || !strcmp(m_menuLanguage.language, "???")));
+  }
+
+  if (bRequestUpdate)
     RequestMenuLanguage();
 
+  CLockObject lock(&m_mutex);
   return m_menuLanguage;
 }
 
@@ -222,12 +233,18 @@ uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
 
 CStdString CCECBusDevice::GetOSDName(bool bUpdate /* = false */)
 {
-  CLockObject lock(&m_mutex);
-  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
-      (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) &&
-      m_type != CEC_DEVICE_TYPE_TV)
+  bool bRequestUpdate(false);
+  {
+    CLockObject lock(&m_mutex);
+    bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
+        (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) &&
+        m_type != CEC_DEVICE_TYPE_TV);
+  }
+
+  if (bRequestUpdate)
     RequestOSDName();
 
+  CLockObject lock(&m_mutex);
   return m_strDeviceName;
 }
 
@@ -248,14 +265,17 @@ bool CCECBusDevice::RequestOSDName(void)
 
 uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate /* = false */)
 {
-  CLockObject lock(&m_mutex);
-  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
-      (m_iPhysicalAddress == 0xFFFF || bUpdate))
+  bool bRequestUpdate(false);
   {
-    if (!RequestPhysicalAddress())
-      AddLog(CEC_LOG_ERROR, "failed to request the physical address");
+    CLockObject lock(&m_mutex);
+    bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
+        (m_iPhysicalAddress == 0xFFFF || bUpdate));
   }
 
+  if (bRequestUpdate && !RequestPhysicalAddress())
+    AddLog(CEC_LOG_ERROR, "failed to request the physical address (1)");
+
+  CLockObject lock(&m_mutex);
   return m_iPhysicalAddress;
 }
 
@@ -275,11 +295,17 @@ bool CCECBusDevice::RequestPhysicalAddress(void)
 
 cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */)
 {
-  CLockObject lock(&m_mutex);
-  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
-      (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN))
+  bool bRequestUpdate(false);
+  {
+    CLockObject lock(&m_mutex);
+    bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
+        (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN));
+  }
+
+  if (bRequestUpdate)
     RequestPowerStatus();
 
+  CLockObject lock(&m_mutex);
   return m_powerStatus;
 }
 
@@ -300,11 +326,17 @@ bool CCECBusDevice::RequestPowerStatus(void)
 
 cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */)
 {
-  CLockObject lock(&m_mutex);
-  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
-      (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN))
+  bool bRequestUpdate(false);
+  {
+    CLockObject lock(&m_mutex);
+    bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
+        (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN));
+  }
+
+  if (bRequestUpdate)
     RequestVendorId();
 
+  CLockObject lock(&m_mutex);
   return m_vendor;
 }
 
@@ -652,8 +684,8 @@ bool CCECBusDevice::TransmitActiveSource(void)
 
   if (bSendActiveSource)
   {
-    m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress);
     m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV);
+    m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress);
     return true;
   }
 
@@ -844,7 +876,6 @@ void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode)
 bool CCECBusDevice::InitHandler(void)
 {
   CLockObject lock(&m_mutex);
-  ReplaceHandler(false);
   return m_handler->InitHandler();
 }