cec: added SendKeypress()/cec_send_keypress() and SendKeyRelease()/cec_send_key_release()
[deb_libcec.git] / src / lib / devices / CECBusDevice.cpp
index a23c4e07187c80620fbf5f5791697b5730876a8b..5c38f635060be704a9e7cb55121980d235bbbdf1 100644 (file)
@@ -214,6 +214,40 @@ uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
   return m_processor->GetPhysicalAddress();
 }
 
+uint16_t CCECBusDevice::GetPhysicalAddress(bool bRefresh /* = true */)
+{
+  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT)
+  {
+    CLockObject lock(&m_mutex);
+    if (m_iPhysicalAddress == 0xFFFF || bRefresh)
+    {
+      lock.Leave();
+      RequestPhysicalAddress();
+      lock.Lock();
+    }
+  }
+
+  CLockObject lock(&m_mutex);
+  return m_iPhysicalAddress;
+}
+
+bool CCECBusDevice::RequestPhysicalAddress(void)
+{
+  bool bReturn(false);
+  if (!MyLogicalAddressContains(m_iLogicalAddress))
+  {
+    CStdString strLog;
+    strLog.Format("<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
+    AddLog(CEC_LOG_NOTICE, strLog);
+    cec_command command;
+    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
+    CLockObject lock(&m_transmitMutex);
+    if (m_processor->Transmit(command))
+      bReturn = m_condition.Wait(&m_transmitMutex, 1000);
+  }
+  return bReturn;
+}
+
 cec_power_status CCECBusDevice::GetPowerStatus(void)
 {
   CLockObject lock(&m_mutex);
@@ -423,7 +457,7 @@ void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus)
 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
 {
   CLockObject lock(&m_mutex);
-  if (iNewAddress > 0)
+  if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress)
   {
     CStdString strLog;
     strLog.Format(">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
@@ -714,4 +748,43 @@ bool CCECBusDevice::TransmitVendorID(cec_logical_address dest)
     return m_processor->Transmit(command);
   }
 }
+
+bool CCECBusDevice::SendKeypress(cec_user_control_code key, bool bWait /* = false */)
+{
+  {
+    CLockObject lock(&m_transmitMutex);
+    cec_command command;
+    cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_PRESSED);
+    command.parameters.PushBack(key);
+
+    if (bWait)
+    {
+      if (m_processor->Transmit(command))
+        return m_condition.Wait(&m_transmitMutex, 1000);
+      return false;
+    }
+
+    return m_processor->Transmit(command);
+  }
+}
+
+bool CCECBusDevice::SendKeyRelease(bool bWait /* = false */)
+{
+  {
+    CLockObject lock(&m_transmitMutex);
+    cec_command command;
+    cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_RELEASE);
+
+    if (bWait)
+    {
+      if (m_processor->Transmit(command))
+        return m_condition.Wait(&m_transmitMutex, 1000);
+      return false;
+    }
+    else
+    {
+      return m_processor->Transmit(command);
+    }
+  }
+}
 //@}