cec: bump version to 1.6.3. check the client version, not the server version in libce...
[deb_libcec.git] / src / lib / LibCEC.cpp
index f8607004e50c257ed01a3ebc50c485ab2c89d2fb..b64cdc709ad6ab22f7a7eb17dac3c2d264e24896 100644 (file)
@@ -48,35 +48,43 @@ using namespace std;
 using namespace CEC;
 using namespace PLATFORM;
 
-CLibCEC::CLibCEC(void) :
-    m_client(NULL),
-    m_iStartTime(GetTimeMs())
+CLibCEC::CLibCEC(const char *UNUSED(strDeviceName), cec_device_type_list UNUSED(types), uint16_t UNUSED(iPhysicalAddress) /* = 0 */) :
+    m_iStartTime(GetTimeMs()),
+    m_client(NULL)
+{
+  m_cec = new CCECProcessor(this);
+}
+
+CLibCEC::CLibCEC(libcec_configuration *UNUSED(configuration)) :
+    m_iStartTime(GetTimeMs()),
+    m_client(NULL)
 {
   m_cec = new CCECProcessor(this);
 }
 
 CLibCEC::~CLibCEC(void)
 {
-  delete m_client;
-  m_client = NULL;
+  // unregister all clients client
+  UnregisterClients();
+
+  // delete the adapter connection
   delete m_cec;
   m_cec = NULL;
 }
 
 bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
 {
-  if (m_cec->IsRunning())
-  {
-    AddLog(CEC_LOG_ERROR, "connection already open");
+  if (!m_cec || !strPort)
     return false;
-  }
 
+  // open a new connection
   if (!m_cec->Start(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs))
   {
     AddLog(CEC_LOG_ERROR, "could not start CEC communications");
     return false;
   }
 
+  // register all clients
   for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
   {
     if (!m_cec->RegisterClient(*it))
@@ -91,18 +99,12 @@ bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = CEC_DEFAULT_CON
 
 void CLibCEC::Close(void)
 {
-  m_clients.clear();
+  // unregister all clients
+  UnregisterClients();
 
-  if (m_client)
-  {
-    delete m_client;
-    m_client = NULL;
-  }
+  // close the connection
   if (m_cec)
-  {
-    delete m_cec;
-    m_cec = NULL;
-  }
+    m_cec->Close();
 }
 
 int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
@@ -110,113 +112,50 @@ int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const ch
   return CUSBCECAdapterDetection::FindAdapters(deviceList, iBufSize, strDevicePath);
 }
 
-bool CLibCEC::PingAdapter(void)
-{
-  return m_cec ? m_cec->PingAdapter() : false;
-}
-
 bool CLibCEC::StartBootloader(void)
 {
   return m_cec ? m_cec->StartBootloader() : false;
 }
 
-bool CLibCEC::SwitchMonitoring(bool bEnable)
-{
-  return m_cec ? m_cec->SwitchMonitoring(bEnable) : false;
-}
-
-cec_logical_address CLibCEC::GetActiveSource(void)
-{
-  return m_cec ? m_cec->GetActiveSource() : CECDEVICE_UNKNOWN;
-}
-
-bool CLibCEC::IsActiveSource(cec_logical_address iAddress)
-{
-  return m_cec ? m_cec->IsActiveSource(iAddress) : false;
-}
-
-bool CLibCEC::PollDevice(cec_logical_address iAddress)
-{
-  return m_cec ? m_cec->PollDevice(iAddress) : false;
-}
-
-cec_logical_addresses CLibCEC::GetActiveDevices(void)
-{
-  CECDEVICEVEC activeDevices;
-  if (m_cec)
-    m_cec->GetDevices()->GetActive(activeDevices);
-  return CCECDeviceMap::ToLogicalAddresses(activeDevices);
-}
-
-bool CLibCEC::IsActiveDevice(cec_logical_address iAddress)
+bool CLibCEC::PingAdapter(void)
 {
-  cec_logical_addresses activeDevices = GetActiveDevices();
-  return activeDevices.IsSet(iAddress);
+  return m_client ? m_client->PingAdapter() : false;
 }
 
-bool CLibCEC::IsActiveDeviceType(cec_device_type type)
-{
-  CECDEVICEVEC activeDevices;
-  if (m_cec)
-    m_cec->GetDevices()->GetActive(activeDevices);
-  CCECDeviceMap::FilterType(type, activeDevices);
-  return !activeDevices.empty();
-}
-
-bool CLibCEC::SetStreamPath(cec_logical_address iAddress)
+bool CLibCEC::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks)
 {
-  uint16_t iPhysicalAddress = GetDevicePhysicalAddress(iAddress);
-  if (iPhysicalAddress != CEC_INVALID_PHYSICAL_ADDRESS)
-    return SetStreamPath(iPhysicalAddress);
-  return false;
+  return m_client ? m_client->EnableCallbacks(cbParam, callbacks) : false;
 }
 
-bool CLibCEC::SetStreamPath(uint16_t iPhysicalAddress)
+bool CLibCEC::GetCurrentConfiguration(libcec_configuration *configuration)
 {
-  return m_cec->SetStreamPath(iPhysicalAddress);
+  return m_client ? m_client->GetCurrentConfiguration(*configuration) : false;
 }
 
-bool CLibCEC::IsLibCECActiveSource(void)
+bool CLibCEC::SetConfiguration(const libcec_configuration *configuration)
 {
-  bool bReturn(false);
-  if (m_cec)
-  {
-    cec_logical_address activeSource = m_cec->GetActiveSource();
-    CCECBusDevice *device = m_cec->GetDevice(activeSource);
-    if (device)
-      bReturn = device->IsHandledByLibCEC();
-  }
-  return bReturn;
+  return m_client ? m_client->SetConfiguration(*configuration) : false;
 }
 
 bool CLibCEC::CanPersistConfiguration(void)
 {
-  return m_cec->CanPersistConfiguration();
+  return m_client ? m_client->CanPersistConfiguration() : false;
 }
 
 bool CLibCEC::PersistConfiguration(libcec_configuration *configuration)
 {
-  return m_cec->PersistConfiguration(configuration);
+  return m_client ? m_client->PersistConfiguration(*configuration) : false;
 }
 
 void CLibCEC::RescanActiveDevices(void)
 {
-  return m_cec->RescanActiveDevices();
-}
-
-bool CLibCEC::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks)
-{
-  return m_client ? m_client->EnableCallbacks(cbParam, callbacks) : false;
-}
-
-bool CLibCEC::GetCurrentConfiguration(libcec_configuration *configuration)
-{
-  return m_client ? m_client->GetCurrentConfiguration(configuration) : false;
+  if (m_client)
+    m_client->RescanActiveDevices();
 }
 
-bool CLibCEC::SetConfiguration(const libcec_configuration *configuration)
+bool CLibCEC::IsLibCECActiveSource(void)
 {
-  return m_client ? m_client->SetConfiguration(configuration) : false;
+  return m_client ? m_client->IsLibCECActiveSource() : false;
 }
 
 bool CLibCEC::Transmit(const cec_command &data)
@@ -279,6 +218,11 @@ bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress, cec_display_cont
   return m_client ? m_client->SendSetOSDString(iLogicalAddress, duration, strMessage) : false;
 }
 
+bool CLibCEC::SwitchMonitoring(bool bEnable)
+{
+  return m_client ? m_client->SwitchMonitoring(bEnable) : false;
+}
+
 cec_version CLibCEC::GetDeviceCecVersion(cec_logical_address iAddress)
 {
   return m_client ? m_client->GetDeviceCecVersion(iAddress) : CEC_VERSION_UNKNOWN;
@@ -286,7 +230,7 @@ cec_version CLibCEC::GetDeviceCecVersion(cec_logical_address iAddress)
 
 bool CLibCEC::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
 {
-  return m_client ? m_client->GetDeviceMenuLanguage(iAddress, language) : false;
+  return m_client ? m_client->GetDeviceMenuLanguage(iAddress, *language) : false;
 }
 
 uint64_t CLibCEC::GetDeviceVendorId(cec_logical_address iAddress)
@@ -304,6 +248,30 @@ cec_power_status CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress)
   return m_client ? m_client->GetDevicePowerStatus(iAddress) : CEC_POWER_STATUS_UNKNOWN;
 }
 
+bool CLibCEC::PollDevice(cec_logical_address iAddress)
+{
+  return m_client ? m_client->PollDevice(iAddress) : false;
+}
+
+cec_logical_addresses CLibCEC::GetActiveDevices(void)
+{
+  cec_logical_addresses addresses;
+  addresses.Clear();
+  if (m_client)
+    addresses = m_client->GetActiveDevices();
+  return addresses;
+}
+
+bool CLibCEC::IsActiveDevice(cec_logical_address iAddress)
+{
+  return m_client ? m_client->IsActiveDevice(iAddress) : false;
+}
+
+bool CLibCEC::IsActiveDeviceType(cec_device_type type)
+{
+  return m_client ? m_client->IsActiveDeviceType(type) : false;
+}
+
 uint8_t CLibCEC::VolumeUp(bool bSendRelease /* = true */)
 {
   return m_client ? m_client->SendVolumeUp(bSendRelease) : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
@@ -332,16 +300,39 @@ bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = t
 cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress)
 {
   cec_osd_name retVal;
+  retVal.device = CECDEVICE_UNKNOWN;
+  memset(retVal.name, 0, 14);
+
   if (m_client)
     retVal = m_client->GetDeviceOSDName(iAddress);
   return retVal;
 }
 
+cec_logical_address CLibCEC::GetActiveSource(void)
+{
+  return m_client ? m_client->GetActiveSource() : CECDEVICE_UNKNOWN;
+}
+
+bool CLibCEC::IsActiveSource(cec_logical_address iAddress)
+{
+  return m_client ? m_client->IsActiveSource(iAddress) : false;
+}
+bool CLibCEC::SetStreamPath(cec_logical_address iAddress)
+{
+  return m_client ? m_client->SetStreamPath(iAddress) : false;
+}
+
+bool CLibCEC::SetStreamPath(uint16_t iPhysicalAddress)
+{
+  return m_client ? m_client->SetStreamPath(iPhysicalAddress) : false;
+}
+
 cec_logical_addresses CLibCEC::GetLogicalAddresses(void)
 {
   cec_logical_addresses addresses;
-  if (m_cec)
-    addresses = m_cec->GetLogicalAddresses();
+  addresses.Clear();
+  if (m_client)
+    m_client->GetLogicalAddresses();
   return addresses;
 }
 
@@ -832,6 +823,8 @@ const char *CLibCEC::ToString(const cec_client_version version)
     return "1.6.1";
   case CEC_CLIENT_VERSION_1_6_2:
     return "1.6.2";
+  case CEC_CLIENT_VERSION_1_6_3:
+    return "1.6.3";
   default:
     return "Unknown";
   }
@@ -857,6 +850,8 @@ const char *CLibCEC::ToString(const cec_server_version version)
     return "1.6.1";
   case CEC_SERVER_VERSION_1_6_2:
     return "1.6.2";
+  case CEC_SERVER_VERSION_1_6_3:
+    return "1.6.3";
   default:
     return "Unknown";
   }
@@ -873,6 +868,7 @@ void CLibCEC::AddLog(const cec_log_level level, const char *strFormat, ...)
 {
   CStdString strLog;
 
+  // format the message
   va_list argList;
   va_start(argList, strFormat);
   strLog.FormatV(strFormat, argList);
@@ -907,28 +903,35 @@ bool CLibCEC::EnablePhysicalAddressDetection(void)
   return true;
 }
 
-CCECClient *CLibCEC::RegisterClient(libcec_configuration *configuration)
+CCECClient *CLibCEC::RegisterClient(libcec_configuration &configuration)
 {
   if (!m_cec)
     return NULL;
 
+  // create a new client instance
   CCECClient *newClient = new CCECClient(m_cec, configuration);
   if (!newClient)
     return NULL;
-
   m_clients.push_back(newClient);
+
+  // if the default client isn't set, set it
   if (!m_client)
     m_client = newClient;
 
-  if (m_cec->IsRunning())
+  // register the new client
+  if (m_cec->CECInitialised())
     m_cec->RegisterClient(newClient);
 
-  return m_client;
+  return newClient;
 }
 
 void CLibCEC::UnregisterClients(void)
 {
+  if (m_cec)
+    m_cec->UnregisterClients();
+
   m_clients.clear();
+
   delete m_client;
   m_client = NULL;
 }
@@ -938,9 +941,20 @@ void * CECInitialise(libcec_configuration *configuration)
   if (!configuration)
     return NULL;
 
-  CLibCEC *lib = new CLibCEC;
-  if (lib)
-    lib->RegisterClient(configuration);
+  // create a new libCEC instance
+  CLibCEC *lib = new CLibCEC(NULL);
+
+  // register a new client
+  CCECClient *client(NULL);
+  if (lib && configuration)
+    client = lib->RegisterClient(*configuration);
+
+  // update the current configuration
+  if (client)
+    client->GetCurrentConfiguration(*configuration);
+
+  // ensure that the correct server version is set
+  configuration->serverVersion = LIBCEC_VERSION_CURRENT;
 
   return static_cast< void* > (lib);
 }
@@ -948,7 +962,6 @@ void * CECInitialise(libcec_configuration *configuration)
 void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types, uint16_t iPhysicalAddress /* = 0 */)
 {
   libcec_configuration configuration;
-  configuration.serverVersion = LIBCEC_VERSION_CURRENT;
 
   // client version < 1.5.0
   snprintf(configuration.strDeviceName, 13, "%s", strDeviceName);
@@ -993,3 +1006,11 @@ bool CLibCEC::GetDeviceInformation(const char *strPort, libcec_configuration *co
 
   return m_cec->GetDeviceInformation(strPort, config, iTimeoutMs);
 }
+
+// no longer being used
+void CLibCEC::AddKey(const cec_keypress &UNUSED(key)) {}
+void CLibCEC::AddCommand(const cec_command &UNUSED(command)) {}
+void CLibCEC::ConfigurationChanged(const libcec_configuration &UNUSED(config)) {}
+void CLibCEC::SetCurrentButton(cec_user_control_code UNUSED(iButtonCode)) {}
+CLibCEC *CLibCEC::GetInstance(void) { return NULL; }
+void CLibCEC::SetInstance(CLibCEC *UNUSED(instance)) {}