cec: sync win32
[deb_libcec.git] / src / lib / LibCEC.cpp
index 4156954e0acce84bf49f1d969e9c7cc06f9b0faf..69830fdf040b5367b062ffccc9cbb767f36affb1 100644 (file)
 
 #include "LibCEC.h"
 
-#include "AdapterCommunication.h"
-#include "AdapterDetection.h"
+#include "adapter/AdapterCommunication.h"
+#include "adapter/AdapterDetection.h"
 #include "CECProcessor.h"
 #include "devices/CECBusDevice.h"
-#include "util/StdString.h"
 #include "platform/timeutils.h"
 
 using namespace std;
 using namespace CEC;
+using namespace PLATFORM;
 
 CLibCEC::CLibCEC(const char *strDeviceName, cec_device_type_list types) :
     m_iStartTime(GetTimeMs()),
     m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
-    m_buttontime(0)
+    m_buttontime(0),
+    m_callbacks(NULL),
+    m_cbParam(NULL)
 {
   m_cec = new CCECProcessor(this, strDeviceName, types);
 }
@@ -53,7 +55,9 @@ CLibCEC::CLibCEC(const char *strDeviceName, cec_device_type_list types) :
 CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) :
     m_iStartTime(GetTimeMs()),
     m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
-    m_buttontime(0)
+    m_buttontime(0),
+    m_callbacks(NULL),
+    m_cbParam(NULL)
 {
   m_cec = new CCECProcessor(this, strDeviceName, iLogicalAddress, iPhysicalAddress);
 }
@@ -87,6 +91,17 @@ void CLibCEC::Close(void)
     m_cec->StopThread();
 }
 
+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 */)
 {
   CStdString strDebug;
@@ -267,54 +282,54 @@ cec_logical_addresses CLibCEC::GetActiveDevices(void)
 bool CLibCEC::IsActiveDevice(cec_logical_address iAddress)
 {
   if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
-    return m_cec->IsActiveDevice(iAddress);
+    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->IsActiveDeviceType(type);
+    return m_cec->IsPresentDeviceType(type);
   return false;
 }
 
-uint8_t CLibCEC::VolumeUp(bool bWait /* = true */)
+uint8_t CLibCEC::VolumeUp(bool bSendRelease /* = true */)
 {
   if (m_cec)
-    return m_cec->VolumeUp();
+    return m_cec->VolumeUp(bSendRelease);
   return 0;
 }
 
-uint8_t CLibCEC::VolumeDown(bool bWait /* = true */)
+uint8_t CLibCEC::VolumeDown(bool bSendRelease /* = true */)
 {
   if (m_cec)
-    return m_cec->VolumeDown();
+    return m_cec->VolumeDown(bSendRelease);
   return 0;
 }
 
 
-uint8_t CLibCEC::MuteAudio(bool bWait /* = true */)
+uint8_t CLibCEC::MuteAudio(bool bSendRelease /* = true */)
 {
   if (m_cec)
-    return m_cec->MuteAudio();
+    return m_cec->MuteAudio(bSendRelease);
   return 0;
 }
 
-bool CLibCEC::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = false */)
+bool CLibCEC::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
 {
   if (m_cec)
-    return m_cec->TransmitKeypress(iDestination, key);
+    return m_cec->TransmitKeypress(iDestination, key, bWait);
   return false;
 }
 
-bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = false */)
+bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
 {
   if (m_cec)
-    return m_cec->TransmitKeyRelease(iDestination);
+    return m_cec->TransmitKeyRelease(iDestination, bWait);
   return false;
 }
 
-cec_osd_name CLibCEC::GetOSDName(cec_logical_address iAddress)
+cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress)
 {
   cec_osd_name retVal;
   retVal.device = iAddress;
@@ -326,34 +341,54 @@ cec_osd_name CLibCEC::GetOSDName(cec_logical_address iAddress)
   return retVal;
 }
 
-void CLibCEC::AddLog(cec_log_level level, const string &strMessage)
+void CLibCEC::AddLog(cec_log_level level, const char *strFormat, ...)
 {
-  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);
-  }
+  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(cec_keypress &key)
 {
-  m_keyBuffer.Push(key);
+  CLockObject lock(m_mutex);
+  if (m_callbacks)
+    m_callbacks->CBCecKeyPress(m_cbParam, key);
+  else
+    m_keyBuffer.Push(key);
   m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
   m_buttontime = 0;
 }
 
 void CLibCEC::AddKey(void)
 {
+  CLockObject lock(m_mutex);
   if (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);
+
+    if (m_callbacks)
+      m_callbacks->CBCecKeyPress(m_cbParam, key);
+    else
+      m_keyBuffer.Push(key);
     m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
   }
   m_buttontime = 0;
@@ -361,7 +396,12 @@ void CLibCEC::AddKey(void)
 
 void CLibCEC::AddCommand(const cec_command &command)
 {
-  if (m_commandBuffer.Push(command))
+  CLockObject lock(m_mutex);
+  if (m_callbacks)
+  {
+    m_callbacks->CBCecCommand(m_cbParam, command);
+  }
+  else if (m_commandBuffer.Push(command))
   {
     CStdString strDebug;
     strDebug.Format("stored command '%2x' in the command buffer. buffer size = %d", command.opcode, m_commandBuffer.Size());
@@ -395,21 +435,36 @@ void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode)
   m_keyBuffer.Push(key);
 }
 
+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 * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
 {
-  return static_cast< void* > (new CLibCEC(strDeviceName, iLogicalAddress, iPhysicalAddress));
+  CLibCEC *lib = new CLibCEC(strDeviceName, iLogicalAddress, iPhysicalAddress);
+  CLibCEC::SetInstance(lib);
+  return static_cast< void* > (lib);
 }
 
 void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types)
 {
-  return static_cast< void* > (new CLibCEC(strDeviceName, types));
+  CLibCEC *lib = new CLibCEC(strDeviceName, types);
+  CLibCEC::SetInstance(lib);
+  return static_cast< void* > (lib);
 }
 
-void CECDestroy(CEC::ICECAdapter *instance)
+void CECDestroy(CEC::ICECAdapter *UNUSED(instance))
 {
-  CLibCEC *lib = static_cast< CLibCEC* > (instance);
-  if (lib)
-    delete lib;
+  CLibCEC::SetInstance(NULL);
 }
 
 const char *CLibCEC::ToString(const cec_menu_state state)