cec: added VolumeUp()/cec_volume_up(), VolumeDown()/cec_volume_down(), MuteAudio...
[deb_libcec.git] / src / lib / devices / CECAudioSystem.cpp
index 87406948f86d038b21d81974b9e6ba1c50b1ea29..4a4f11fbe2434a898df529176772f57749f5c0f3 100644 (file)
@@ -32,6 +32,7 @@
 
 #include "CECAudioSystem.h"
 #include "../CECProcessor.h"
+#include "../implementations/CECCommandHandler.h"
 
 using namespace CEC;
 
@@ -40,15 +41,44 @@ CCECAudioSystem::CCECAudioSystem(CCECProcessor *processor, cec_logical_address a
     m_systemAudioStatus(CEC_SYSTEM_AUDIO_STATUS_ON),
     m_audioStatus(CEC_AUDIO_MUTE_STATUS_MASK)
 {
-  m_type          = CEC_DEVICE_TYPE_AUDIO_SYSTEM;
-  m_strDeviceName = "Audio";
+  m_type = CEC_DEVICE_TYPE_AUDIO_SYSTEM;
+}
+
+bool CCECAudioSystem::SetAudioStatus(uint8_t status)
+{
+  if (m_audioStatus != status)
+  {
+    CStdString strLog;
+    strLog.Format(">> %s (%X): audio status changed from %2x to %2x", GetLogicalAddressName(), m_iLogicalAddress, m_audioStatus, status);
+    AddLog(CEC_LOG_DEBUG, strLog.c_str());
+
+    m_audioStatus = status;
+    return true;
+  }
+
+  return false;
+}
+
+bool CCECAudioSystem::SetSystemAudioMode(const cec_system_audio_status mode)
+{
+  if (m_systemAudioStatus != mode)
+  {
+    CStdString strLog;
+    strLog.Format(">> %s (%X): system audio mode changed from %s to %s", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(m_systemAudioStatus), CCECCommandHandler::ToString(mode));
+    AddLog(CEC_LOG_DEBUG, strLog.c_str());
+
+    m_systemAudioStatus = mode;
+    return true;
+  }
+
+  return false;
 }
 
 bool CCECAudioSystem::SetSystemAudioMode(const cec_command &command)
 {
-  m_systemAudioStatus = (command.parameters.size == 0) ?
+  SetSystemAudioMode((command.parameters.size == 0) ?
     CEC_SYSTEM_AUDIO_STATUS_OFF :
-  CEC_SYSTEM_AUDIO_STATUS_ON;
+  CEC_SYSTEM_AUDIO_STATUS_ON);
 
   return TransmitAudioStatus(command.initiator);
 }
@@ -60,8 +90,8 @@ bool CCECAudioSystem::TransmitAudioStatus(cec_logical_address dest)
   AddLog(CEC_LOG_NOTICE, strLog);
 
   cec_command command;
-  cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_REPORT_AUDIO_STATUS);
-  command.parameters.push_back((uint8_t) m_audioStatus);
+  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_REPORT_AUDIO_STATUS);
+  command.parameters.PushBack(m_audioStatus);
 
   return m_processor->Transmit(command);
 }
@@ -69,12 +99,45 @@ bool CCECAudioSystem::TransmitAudioStatus(cec_logical_address dest)
 bool CCECAudioSystem::TransmitSystemAudioModeStatus(cec_logical_address dest)
 {
   CStdString strLog;
-  strLog.Format("<< %x -> %x: system audio mode '%2x'", m_iLogicalAddress, dest, m_systemAudioStatus);
+  strLog.Format("<< %x -> %x: system audio mode '%s'", m_iLogicalAddress, dest, CCECCommandHandler::ToString(m_systemAudioStatus));
   AddLog(CEC_LOG_NOTICE, strLog);
 
   cec_command command;
-  cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
-  command.parameters.push_back((uint8_t) m_systemAudioStatus);
+  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
+  command.parameters.PushBack((uint8_t) m_systemAudioStatus);
 
   return m_processor->Transmit(command);
 }
+
+uint8_t CCECAudioSystem::VolumeUp(void)
+{
+  return SendKey(CEC_USER_CONTROL_CODE_VOLUME_UP);
+}
+
+uint8_t CCECAudioSystem::VolumeDown(void)
+{
+  return SendKey(CEC_USER_CONTROL_CODE_VOLUME_DOWN);
+}
+
+uint8_t CCECAudioSystem::MuteAudio(void)
+{
+  return SendKey(CEC_USER_CONTROL_CODE_MUTE);
+}
+
+uint8_t CCECAudioSystem::SendKey(cec_user_control_code key)
+{
+  {
+    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);
+    m_processor->Transmit(command);
+
+    cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_RELEASE);
+    if (m_processor->Transmit(command))
+      m_condition.Wait(&m_transmitMutex, 1000);
+  }
+
+  CLockObject lock(&m_mutex);
+  return m_audioStatus;
+}