cec: added poweroff devices parameter to the config (instead of a boolean parameter...
[deb_libcec.git] / src / lib / LibCEC.cpp
index 0efef00b2bb1ea4ef30b67845b0d7c8e7b9f5596..67e8b6018415396a0f667eadad684e3bb42ee9c8 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 "LibCEC.h"
 
-#include "AdapterCommunication.h"
-#include "AdapterDetection.h"
+#include "adapter/USBCECAdapterDetection.h"
 #include "CECProcessor.h"
 #include "devices/CECBusDevice.h"
-#include "util/StdString.h"
-#include "platform/timeutils.h"
+#include "platform/util/timeutils.h"
+#include "platform/util/StdString.h"
 
 using namespace std;
 using namespace CEC;
+using namespace PLATFORM;
 
-CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) :
+CLibCEC::CLibCEC(const char *strDeviceName, cec_device_type_list types, uint16_t iPhysicalAddress /* = 0 */) :
     m_iStartTime(GetTimeMs()),
     m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
-    m_buttontime(0)
+    m_buttontime(0),
+    m_callbacks(NULL),
+    m_cbParam(NULL)
 {
-  m_comm = new CAdapterCommunication(this);
-  m_cec = new CCECProcessor(this, m_comm, strDeviceName, iLogicalAddress, iPhysicalAddress);
+  m_cec = new CCECProcessor(this, strDeviceName, types, iPhysicalAddress, CEC_CLIENT_VERSION_PRE_1_5);
+}
+
+CLibCEC::CLibCEC(const libcec_configuration *configuration) :
+    m_iStartTime(GetTimeMs()),
+    m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
+    m_buttontime(0),
+    m_callbacks(configuration->callbacks),
+    m_cbParam(configuration->callbackParam)
+{
+  m_cec = new CCECProcessor(this, configuration);
 }
 
 CLibCEC::~CLibCEC(void)
 {
   Close();
   delete m_cec;
-  delete m_comm;
 }
 
 bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = 10000 */)
 {
-  if (!m_comm)
-  {
-    AddLog(CEC_LOG_ERROR, "no comm port");
-    return false;
-  }
-
-  if (m_comm->IsOpen())
+  if (m_cec->IsRunning())
   {
     AddLog(CEC_LOG_ERROR, "connection already open");
     return false;
   }
 
-  int64_t iNow = GetTimeMs();
-  int64_t iTarget = iNow + iTimeoutMs;
-
-  bool bOpened(false);
-  while (!bOpened && iNow < iTarget)
-  {
-    bOpened = m_comm->Open(strPort, 38400, iTimeoutMs);
-    iNow = GetTimeMs();
-  }
-
-  if (!bOpened)
-  {
-    AddLog(CEC_LOG_ERROR, "could not open a connection");
-    return false;
-  }
-
-  if (!m_cec->Start())
+  if (!m_cec->Start(strPort, 38400, iTimeoutMs))
   {
     AddLog(CEC_LOG_ERROR, "could not start CEC communications");
     return false;
@@ -100,9 +88,18 @@ bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = 10000 */)
 void CLibCEC::Close(void)
 {
   if (m_cec)
-    m_cec->StopThread();
-  if (m_comm)
-    m_comm->Close();
+    m_cec->Close();
+}
+
+bool CLibCEC::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks)
+{
+  CLockObject lock(m_mutex);
+  if (m_cec)
+  {
+    m_cbParam   = cbParam;
+    m_callbacks = callbacks;
+  }
+  return false;
 }
 
 int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
@@ -114,17 +111,17 @@ int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const ch
     strDebug.Format("trying to autodetect all CEC adapters");
   AddLog(CEC_LOG_DEBUG, strDebug);
 
-  return CAdapterDetection::FindAdapters(deviceList, iBufSize, strDevicePath);
+  return CUSBCECAdapterDetection::FindAdapters(deviceList, iBufSize, strDevicePath);
 }
 
 bool CLibCEC::PingAdapter(void)
 {
-  return m_comm ? m_comm->PingAdapter() : false;
+  return m_cec ? m_cec->PingAdapter() : false;
 }
 
 bool CLibCEC::StartBootloader(void)
 {
-  return m_comm ? m_comm->StartBootloader() : false;
+  return m_cec ? m_cec->StartBootloader() : false;
 }
 
 bool CLibCEC::GetNextLogMessage(cec_log_message *message)
@@ -152,19 +149,34 @@ bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress)
   return m_cec ? m_cec->SetLogicalAddress(iLogicalAddress) : false;
 }
 
-bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress)
+bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
 {
   return m_cec ? m_cec->SetPhysicalAddress(iPhysicalAddress) : false;
 }
 
+bool CLibCEC::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort /* = CEC_DEFAULT_HDMI_PORT */)
+{
+  return m_cec ? m_cec->SetHDMIPort(iBaseDevice, iPort) : false;
+}
+
+bool CLibCEC::EnablePhysicalAddressDetection(void)
+{
+  return m_cec ? m_cec->EnablePhysicalAddressDetection() : false;
+}
+
 bool CLibCEC::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
 {
-  return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->m_busDevices[(uint8_t)address]->PowerOn() : false;
+  return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->PowerOnDevices(address) : false;
 }
 
 bool CLibCEC::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
 {
-  return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->m_busDevices[(uint8_t)address]->Standby() : false;
+  return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->StandbyDevices(address) : false;
+}
+
+bool CLibCEC::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
+{
+  return m_cec ? m_cec->SetActiveSource(type) : false;
 }
 
 bool CLibCEC::SetActiveView(void)
@@ -172,14 +184,31 @@ bool CLibCEC::SetActiveView(void)
   return m_cec ? m_cec->SetActiveView() : false;
 }
 
+bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
+{
+  return m_cec ? m_cec->SetDeckControlMode(mode, bSendUpdate) : false;
+}
+
+bool CLibCEC::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
+{
+  return m_cec ? m_cec->SetDeckInfo(info, bSendUpdate) : false;
+}
+
 bool CLibCEC::SetInactiveView(void)
 {
-  return m_cec ? m_cec->SetInactiveView() : false;
+  return m_cec ? m_cec->TransmitInactiveSource() : false;
+}
+
+bool CLibCEC::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
+{
+  return m_cec ? m_cec->SetMenuState(state, bSendUpdate) : false;
 }
 
 bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress, cec_display_control duration, const char *strMessage)
 {
-  return m_cec && iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST ? m_cec->m_busDevices[(uint8_t)iLogicalAddress]->SetOSDString(duration, strMessage) : false;
+  return m_cec && iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST ?
+      m_cec->m_busDevices[m_cec->GetLogicalAddress()]->TransmitOSDString(iLogicalAddress, duration, strMessage) :
+      false;
 }
 
 bool CLibCEC::SwitchMonitoring(bool bEnable)
@@ -208,6 +237,25 @@ uint64_t CLibCEC::GetDeviceVendorId(cec_logical_address iAddress)
   return 0;
 }
 
+uint16_t CLibCEC::GetDevicePhysicalAddress(cec_logical_address iAddress)
+{
+  if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
+    return m_cec->GetDevicePhysicalAddress(iAddress);
+  return 0;
+}
+
+cec_logical_address CLibCEC::GetActiveSource(void)
+{
+  return m_cec ? m_cec->GetActiveSource() : CECDEVICE_UNKNOWN;
+}
+
+bool CLibCEC::IsActiveSource(cec_logical_address iAddress)
+{
+  if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
+    return m_cec->IsActiveSource(iAddress);
+  return false;
+}
+
 cec_power_status CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress)
 {
   if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
@@ -215,44 +263,178 @@ cec_power_status CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress)
   return CEC_POWER_STATUS_UNKNOWN;
 }
 
-void CLibCEC::AddLog(cec_log_level level, const string &strMessage)
+bool CLibCEC::PollDevice(cec_logical_address iAddress)
 {
+  if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
+    return m_cec->PollDevice(iAddress);
+  return false;
+}
+
+cec_logical_addresses CLibCEC::GetActiveDevices(void)
+{
+  cec_logical_addresses addresses;
+  addresses.Clear();
   if (m_cec)
-  {
-    cec_log_message message;
-    message.level = level;
-    message.time = GetTimeMs() - m_iStartTime;
-    snprintf(message.message, sizeof(message.message), "%s", strMessage.c_str());
-    m_logBuffer.Push(message);
-  }
+    addresses = m_cec->GetActiveDevices();
+  return addresses;
+}
+
+bool CLibCEC::IsActiveDevice(cec_logical_address iAddress)
+{
+  if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
+    return m_cec->IsPresentDevice(iAddress);
+  return false;
+}
+
+bool CLibCEC::IsActiveDeviceType(cec_device_type type)
+{
+  if (m_cec && type >= CEC_DEVICE_TYPE_TV && type <= CEC_DEVICE_TYPE_AUDIO_SYSTEM)
+    return m_cec->IsPresentDeviceType(type);
+  return false;
+}
+
+uint8_t CLibCEC::VolumeUp(bool bSendRelease /* = true */)
+{
+  if (m_cec)
+    return m_cec->VolumeUp(bSendRelease);
+  return 0;
+}
+
+uint8_t CLibCEC::VolumeDown(bool bSendRelease /* = true */)
+{
+  if (m_cec)
+    return m_cec->VolumeDown(bSendRelease);
+  return 0;
+}
+
+
+uint8_t CLibCEC::MuteAudio(bool bSendRelease /* = true */)
+{
+  if (m_cec)
+    return m_cec->MuteAudio(bSendRelease);
+  return 0;
+}
+
+bool CLibCEC::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
+{
+  if (m_cec)
+    return m_cec->TransmitKeypress(iDestination, key, bWait);
+  return false;
+}
+
+bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
+{
+  if (m_cec)
+    return m_cec->TransmitKeyRelease(iDestination, bWait);
+  return false;
+}
+
+cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress)
+{
+  cec_osd_name retVal;
+  retVal.device = iAddress;
+  retVal.name[0] = 0;
+
+  if (m_cec)
+    retVal = m_cec->GetDeviceOSDName(iAddress);
+
+  return retVal;
+}
+
+void CLibCEC::AddLog(const cec_log_level level, const char *strFormat, ...)
+{
+  CStdString strLog;
+
+  va_list argList;
+  va_start(argList, strFormat);
+  strLog.FormatV(strFormat, argList);
+  va_end(argList);
+
+  CLibCEC *instance = CLibCEC::GetInstance();
+  CLockObject lock(instance->m_mutex);
+
+  cec_log_message message;
+  message.level = level;
+  message.time = GetTimeMs() - instance->m_iStartTime;
+  snprintf(message.message, sizeof(message.message), "%s", strLog.c_str());
+
+  if (instance->m_callbacks)
+    instance->m_callbacks->CBCecLogMessage(instance->m_cbParam, message);
+  else
+    instance->m_logBuffer.Push(message);
+}
+
+void CLibCEC::AddKey(const cec_keypress &key)
+{
+  CLibCEC *instance = CLibCEC::GetInstance();
+  CLockObject lock(instance->m_mutex);
+
+  AddLog(CEC_LOG_DEBUG, "key pressed: %1x", key.keycode);
+
+  if (instance->m_callbacks)
+    instance->m_callbacks->CBCecKeyPress(instance->m_cbParam, key);
+  else
+    instance->m_keyBuffer.Push(key);
+
+  instance->m_iCurrentButton = key.duration > 0 ? CEC_USER_CONTROL_CODE_UNKNOWN : key.keycode;
+  instance->m_buttontime = key.duration > 0 ? 0 : GetTimeMs();
+}
+
+void CLibCEC::ConfigurationChanged(const libcec_configuration &config)
+{
+  CLibCEC *instance = CLibCEC::GetInstance();
+  CLockObject lock(instance->m_mutex);
+
+  if (instance->m_callbacks &&
+      config.clientVersion >= CEC_CLIENT_VERSION_1_5_0 &&
+      instance->m_callbacks->CBCecConfigurationChanged != NULL)
+    instance->m_callbacks->CBCecConfigurationChanged(instance->m_cbParam, config);
+}
+
+void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode)
+{
+  /* push keypress to the keybuffer with 0 duration.
+     push another press to the keybuffer with the duration set when the button is released */
+  cec_keypress key;
+  key.duration = 0;
+  key.keycode = iButtonCode;
+
+  AddKey(key);
 }
 
 void CLibCEC::AddKey(void)
 {
-  if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
+  CLibCEC *instance = CLibCEC::GetInstance();
+  CLockObject lock(instance->m_mutex);
+
+  if (instance->m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
   {
     cec_keypress key;
 
-    key.duration = (unsigned int) (GetTimeMs() - m_buttontime);
-    key.keycode = m_iCurrentButton;
-    m_keyBuffer.Push(key);
-    m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
+    key.duration = (unsigned int) (GetTimeMs() - instance->m_buttontime);
+    key.keycode = instance->m_iCurrentButton;
+    AddLog(CEC_LOG_DEBUG, "key released: %1x", key.keycode);
+
+    if (instance->m_callbacks)
+      instance->m_callbacks->CBCecKeyPress(instance->m_cbParam, key);
+    else
+      instance->m_keyBuffer.Push(key);
+    instance->m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
   }
-  m_buttontime = 0;
+  instance->m_buttontime = 0;
 }
 
 void CLibCEC::AddCommand(const cec_command &command)
 {
-  if (m_commandBuffer.Push(command))
-  {
-    CStdString strDebug;
-    strDebug.Format("stored command '%2x' in the command buffer. buffer size = %d", command.opcode, m_commandBuffer.Size());
-    AddLog(CEC_LOG_DEBUG, strDebug);
-  }
-  else
-  {
+  CLibCEC *instance = CLibCEC::GetInstance();
+  CLockObject lock(instance->m_mutex);
+
+  AddLog(CEC_LOG_NOTICE, ">> %s (%X) -> %s (%X): %s (%2X)", instance->m_cec->ToString(command.initiator), command.initiator, instance->m_cec->ToString(command.destination), command.destination, instance->m_cec->ToString(command.opcode), command.opcode);
+
+  if (instance->m_callbacks)
+    instance->m_callbacks->CBCecCommand(instance->m_cbParam, command);
+  else if (!instance->m_commandBuffer.Push(command))
     AddLog(CEC_LOG_WARNING, "command buffer is full");
-  }
 }
 
 void CLibCEC::CheckKeypressTimeout(void)
@@ -264,27 +446,128 @@ void CLibCEC::CheckKeypressTimeout(void)
   }
 }
 
-void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode)
+bool CLibCEC::SetStreamPath(cec_logical_address iAddress)
 {
-  m_iCurrentButton = iButtonCode;
-  m_buttontime = GetTimeMs();
+  uint16_t iPhysicalAddress = GetDevicePhysicalAddress(iAddress);
+  if (iPhysicalAddress != 0xFFFF)
+    return SetStreamPath(iPhysicalAddress);
+  return false;
+}
 
-  /* push keypress to the keybuffer with 0 duration.
-     push another press to the keybuffer with the duration set when the button is released */
-  cec_keypress key;
-  key.duration = 0;
-  key.keycode = m_iCurrentButton;
-  m_keyBuffer.Push(key);
+bool CLibCEC::SetStreamPath(uint16_t iPhysicalAddress)
+{
+  return m_cec->SetStreamPath(iPhysicalAddress);
+}
+
+cec_logical_addresses CLibCEC::GetLogicalAddresses(void)
+{
+  cec_logical_addresses addr = m_cec->GetLogicalAddresses();
+  return addr;
+}
+
+static CLibCEC *g_libCEC_instance(NULL);
+CLibCEC *CLibCEC::GetInstance(void)
+{
+  return g_libCEC_instance;
+}
+
+void CLibCEC::SetInstance(CLibCEC *instance)
+{
+  if (g_libCEC_instance)
+    delete g_libCEC_instance;
+  g_libCEC_instance = instance;
+}
+
+void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types, uint16_t iPhysicalAddress /* = 0 */)
+{
+  CLibCEC *lib = new CLibCEC(strDeviceName, types);
+  CLibCEC::SetInstance(lib);
+  return static_cast< void* > (lib);
+}
+
+void * CECInitialise(const libcec_configuration *configuration)
+{
+  CLibCEC *lib = new CLibCEC(configuration);
+  CLibCEC::SetInstance(lib);
+  return static_cast< void* > (lib);
+}
+
+void CECDestroy(CEC::ICECAdapter *UNUSED(instance))
+{
+  CLibCEC::SetInstance(NULL);
+}
+
+const char *CLibCEC::ToString(const cec_menu_state state)
+{
+  return m_cec->ToString(state);
+}
+
+const char *CLibCEC::ToString(const cec_version version)
+{
+  return m_cec->ToString(version);
+}
+
+const char *CLibCEC::ToString(const cec_power_status status)
+{
+  return m_cec->ToString(status);
+}
+
+const char *CLibCEC::ToString(const cec_logical_address address)
+{
+  return m_cec->ToString(address);
+}
+
+const char *CLibCEC::ToString(const cec_deck_control_mode mode)
+{
+  return m_cec->ToString(mode);
+}
+
+const char *CLibCEC::ToString(const cec_deck_info status)
+{
+  return m_cec->ToString(status);
+}
+
+const char *CLibCEC::ToString(const cec_opcode opcode)
+{
+  return m_cec->ToString(opcode);
+}
+
+const char *CLibCEC::ToString(const cec_system_audio_status mode)
+{
+  return m_cec->ToString(mode);
+}
+
+const char *CLibCEC::ToString(const cec_audio_status status)
+{
+  return m_cec->ToString(status);
+}
+
+const char *CLibCEC::ToString(const cec_vendor_id vendor)
+{
+  return m_cec->ToString(vendor);
+}
+
+const char *CLibCEC::ToString(const cec_client_version version)
+{
+  return m_cec->ToString(version);
+}
+
+bool CLibCEC::GetCurrentConfiguration(libcec_configuration *configuration)
+{
+  return m_cec->GetCurrentConfiguration(configuration);
+}
+
+bool CLibCEC::SetConfiguration(const libcec_configuration *configuration)
+{
+  return m_cec->SetConfiguration(configuration);
 }
 
-void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
+bool CLibCEC::CanPersistConfiguration(void)
 {
-  return static_cast< void* > (new CLibCEC(strDeviceName, iLogicalAddress, iPhysicalAddress));
+  return m_cec->CanPersistConfiguration();
 }
 
-void CECDestroy(CEC::ICECAdapter *instance)
+bool CLibCEC::PersistConfiguration(libcec_configuration *configuration)
 {
-  CLibCEC *lib = static_cast< CLibCEC* > (instance);
-  if (lib)
-    delete lib;
+  return m_cec->PersistConfiguration(configuration);
 }