cec: ensure that the command handler of the tv is replaced before registering a clien...
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 71e5c6960ac8d643f5ba98de374843685dcafacf..db6f8a16876bd1c8a2c2330be4dc78280c00b5c0 100644 (file)
@@ -660,12 +660,18 @@ bool CCECProcessor::RegisterClient(CCECClient *client)
   if (client->IsRegistered())
     UnregisterClient(client);
 
+  // ensure that controlled mode is enabled
+  m_communication->SetControlledMode(true);
+
   // ensure that we know the vendor id of the TV
   CCECBusDevice *tv = GetTV();
+  cec_vendor_id tvVendor = CEC_VENDOR_UNKNOWN;
   if (m_communication->SupportsSourceLogicalAddress(CECDEVICE_UNREGISTERED))
-    tv->GetVendorId(CECDEVICE_UNREGISTERED);
+    tvVendor = tv->GetVendorId(CECDEVICE_UNREGISTERED);
   else if (m_communication->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE))
-    tv->GetVendorId(CECDEVICE_FREEUSE);
+    tvVendor = tv->GetVendorId(CECDEVICE_FREEUSE);
+  if (tvVendor != CEC_VENDOR_UNKNOWN)
+    tv->ReplaceHandler(false);
 
   // get the configuration from the client
   m_libcec->AddLog(CEC_LOG_NOTICE, "registering new CEC client - v%s", ToString((cec_client_version)configuration.clientVersion));
@@ -784,7 +790,17 @@ bool CCECProcessor::UnregisterClient(CCECClient *client)
   }
 
   // set the new ackmask
-  return SetLogicalAddresses(GetLogicalAddresses());;
+  cec_logical_addresses addresses = GetLogicalAddresses();
+  if (SetLogicalAddresses(addresses))
+  {
+    // no more clients left, disable controlled mode
+    if (addresses.IsEmpty())
+      m_communication->SetControlledMode(false);
+
+    return true;
+  }
+
+  return false;
 }
 
 void CCECProcessor::UnregisterClients(void)