cec: added callback methods to libCEC. enable them by calling EnableCallbacks(ICECCal...
[deb_libcec.git] / src / lib / LibCEC.cpp
index 1366ff6cbcec92b6567ab0648e8a32cdeac3f87a..a8b8c70e5af975830c1415823a091e1990f416cb 100644 (file)
@@ -45,7 +45,8 @@ using namespace CEC;
 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_cec = new CCECProcessor(this, strDeviceName, types);
 }
@@ -53,7 +54,8 @@ 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_cec = new CCECProcessor(this, strDeviceName, iLogicalAddress, iPhysicalAddress);
 }
@@ -87,6 +89,14 @@ void CLibCEC::Close(void)
     m_cec->StopThread();
 }
 
+bool CLibCEC::EnableCallbacks(ICECCallbacks *callbacks)
+{
+  CLockObject lock(&m_mutex);
+  if (m_cec)
+    m_callbacks = callbacks;
+  return false;
+}
+
 int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
 {
   CStdString strDebug;
@@ -328,32 +338,46 @@ cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress)
 
 void CLibCEC::AddLog(cec_log_level level, const string &strMessage)
 {
+  CLockObject lock(&m_mutex);
   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);
+
+    if (m_callbacks)
+      m_callbacks->CecLogMessage(message);
+    else
+      m_logBuffer.Push(message);
   }
 }
 
 void CLibCEC::AddKey(cec_keypress &key)
 {
-  m_keyBuffer.Push(key);
+  CLockObject lock(&m_mutex);
+  if (m_callbacks)
+    m_callbacks->CecKeyPress(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->CecKeyPress(key);
+    else
+      m_keyBuffer.Push(key);
     m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
   }
   m_buttontime = 0;
@@ -361,7 +385,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->CecCommand(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());