cec: don't send an active source command when the physical address couldn't be set...
[deb_libcec.git] / src / LibCecSharp / CecSharpTypes.h
index 55c756ef874e05cf88f3011b99f69c4e9d8cc3e8..d9fcc9d2a0fcc7cde1c312b685a2c6f071596ce7 100644 (file)
@@ -236,6 +236,7 @@ namespace CecSharp
                Onkyo     = 0x09B0,
                Yamaha    = 0xA0DE,
                Philips   = 0x903E,
+               Sony      = 0x080046,
                Unknown   = 0
        };
 
@@ -325,7 +326,21 @@ namespace CecSharp
        public enum class CecClientVersion
        {
                VersionPre1_5 = 0,
-               Version1_5_0  = 1
+               Version1_5_0  = 0x1500,
+    Version1_5_1  = 0x1501,
+    Version1_5_2  = 0x1502,
+    Version1_5_3  = 0x1503,
+    Version1_6_0  = 0x1600
+       };
+
+  public enum class CecServerVersion
+       {
+               VersionPre1_5 = 0,
+               Version1_5_0  = 0x1500,
+    Version1_5_1  = 0x1501,
+    Version1_5_2  = 0x1502,
+    Version1_5_3  = 0x1503,
+    Version1_6_0  = 0x1600
        };
 
        public ref class CecAdapter
@@ -360,15 +375,26 @@ namespace CecSharp
                CecLogicalAddresses(void)
                {
                        Addresses = gcnew array<CecLogicalAddress>(16);
+                       Clear();
+               }
+
+               void Clear(void)
+               {
                        for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
-                               Addresses[iPtr] = CecLogicalAddress::Unregistered;
+                               Addresses[iPtr] = CecLogicalAddress::Unknown;
                }
 
                bool IsSet(CecLogicalAddress iAddress)
                {
-                       return Addresses[(unsigned int)iAddress] != CecLogicalAddress::Unregistered;
+                       return Addresses[(unsigned int)iAddress] != CecLogicalAddress::Unknown;
+               }
+
+         void Set(CecLogicalAddress iAddress)
+               {
+                       Addresses[(unsigned int)iAddress] = iAddress;
                }
 
+               property CecLogicalAddress          Primary;
                property array<CecLogicalAddress> ^ Addresses;
        };
 
@@ -499,16 +525,136 @@ namespace CecSharp
                property int64_t         Time;
        };
 
+       ref class CecCallbackMethods; //forward
+       public ref class LibCECConfiguration
+       {
+       public:
+               LibCECConfiguration(void)
+               {
+                       DeviceName          = "";
+                       DeviceTypes         = gcnew CecDeviceTypeList();
+                       AutodetectAddress  = true;
+                       PhysicalAddress     = CEC_DEFAULT_PHYSICAL_ADDRESS;
+                       BaseDevice          = (CecLogicalAddress)CEC_DEFAULT_BASE_DEVICE;
+                       HDMIPort            = CEC_DEFAULT_HDMI_PORT;
+                       ClientVersion       = CecClientVersion::VersionPre1_5;
+                       ServerVersion       = CecServerVersion::VersionPre1_5;
+                       TvVendor            = CecVendorId::Unknown;
+
+                       GetSettingsFromROM  = false;
+                       UseTVMenuLanguage   = CEC_DEFAULT_SETTING_USE_TV_MENU_LANGUAGE == 1;
+                       ActivateSource      = CEC_DEFAULT_SETTING_ACTIVATE_SOURCE == 1;
+
+                       WakeDevices         = gcnew CecLogicalAddresses();
+                       if (CEC_DEFAULT_SETTING_ACTIVATE_SOURCE == 1)
+                               WakeDevices->Set(CecLogicalAddress::Tv);
+
+                       PowerOffDevices     = gcnew CecLogicalAddresses();
+                       if (CEC_DEFAULT_SETTING_POWER_OFF_SHUTDOWN == 1)
+                               PowerOffDevices->Set(CecLogicalAddress::Broadcast);
+
+                       PowerOffScreensaver = CEC_DEFAULT_SETTING_POWER_OFF_SCREENSAVER == 1;
+                       PowerOffOnStandby   = CEC_DEFAULT_SETTING_POWER_OFF_ON_STANDBY == 1;
+      SendInactiveSource  = CEC_DEFAULT_SETTING_SEND_INACTIVE_SOURCE == 1;
+      LogicalAddresses    = gcnew CecLogicalAddresses();
+      FirmwareVersion     = 1;
+      PowerOffDevicesOnStandby = CEC_DEFAULT_SETTING_POWER_OFF_DEVICES_STANDBY == 1;
+               }
+
+               void SetCallbacks(CecCallbackMethods ^callbacks)
+               {
+                       Callbacks = callbacks;
+               }
+
+               void Update(const CEC::libcec_configuration &config)
+               {
+                       DeviceName = gcnew System::String(config.strDeviceName);
+
+                       for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
+                               DeviceTypes->Types[iPtr] = (CecDeviceType)config.deviceTypes.types[iPtr];
+
+                       AutodetectAddress  = config.bAutodetectAddress == 1;
+                       PhysicalAddress    = config.iPhysicalAddress;
+                       BaseDevice         = (CecLogicalAddress)config.baseDevice;
+                       HDMIPort           = config.iHDMIPort;
+                       ClientVersion      = (CecClientVersion)config.clientVersion;
+                       ServerVersion      = (CecServerVersion)config.serverVersion;
+                       TvVendor           = (CecVendorId)config.tvVendor;
+
+                       // player specific settings
+                       GetSettingsFromROM = config.bGetSettingsFromROM == 1;
+                       UseTVMenuLanguage = config.bUseTVMenuLanguage == 1;
+                       ActivateSource = config.bActivateSource == 1;
+
+                       WakeDevices->Clear();
+                       for (uint8_t iPtr = 0; iPtr <= 16; iPtr++)
+                               if (config.wakeDevices[iPtr])
+                                       WakeDevices->Set((CecLogicalAddress)iPtr);
+
+                       PowerOffDevices->Clear();
+                       for (uint8_t iPtr = 0; iPtr <= 16; iPtr++)
+                               if (config.powerOffDevices[iPtr])
+                                       PowerOffDevices->Set((CecLogicalAddress)iPtr);
+
+                       PowerOffScreensaver = config.bPowerOffScreensaver == 1;
+                       PowerOffOnStandby = config.bPowerOffOnStandby == 1;
+
+      if (ServerVersion >= CecServerVersion::Version1_5_1)
+        SendInactiveSource = config.bSendInactiveSource == 1;
+
+      if (ServerVersion >= CecServerVersion::Version1_5_3)
+      {
+        LogicalAddresses->Clear();
+                         for (uint8_t iPtr = 0; iPtr <= 16; iPtr++)
+                                 if (config.logicalAddresses[iPtr])
+                                       LogicalAddresses->Set((CecLogicalAddress)iPtr);
+      }
+
+      if (ServerVersion >= CecServerVersion::Version1_6_0)
+      {
+        FirmwareVersion          = config.iFirmwareVersion;
+        PowerOffDevicesOnStandby = config.bPowerOffDevicesOnStandby == 1;
+      }
+               }
+
+               property System::String ^     DeviceName;
+               property CecDeviceTypeList ^  DeviceTypes;
+               property bool                 AutodetectAddress;
+               property uint16_t             PhysicalAddress;
+               property CecLogicalAddress    BaseDevice;
+               property uint8_t              HDMIPort;
+               property CecClientVersion     ClientVersion;
+               property CecServerVersion     ServerVersion;
+               property CecVendorId          TvVendor;
+
+               // player specific settings
+               property bool                 GetSettingsFromROM;
+               property bool                 UseTVMenuLanguage;
+               property bool                 ActivateSource;
+               property CecLogicalAddresses ^WakeDevices;
+               property CecLogicalAddresses ^PowerOffDevices;
+               property bool                 PowerOffScreensaver;
+               property bool                 PowerOffOnStandby;
+    property bool                 SendInactiveSource;
+    property CecLogicalAddresses ^LogicalAddresses;
+    property uint16_t             FirmwareVersion;
+    property bool                 PowerOffDevicesOnStandby;
+
+               property CecCallbackMethods ^ Callbacks;
+       };
+
        // the callback methods are called by unmanaged code, so we need some delegates for this
        #pragma unmanaged
        // unmanaged callback methods
        typedef int (__stdcall *LOGCB)    (const CEC::cec_log_message &message);
        typedef int (__stdcall *KEYCB)    (const CEC::cec_keypress &key);
        typedef int (__stdcall *COMMANDCB)(const CEC::cec_command &command);
+       typedef int (__stdcall *CONFIGCB) (const CEC::libcec_configuration &config);
 
        static LOGCB              g_logCB;
        static KEYCB              g_keyCB;
        static COMMANDCB          g_commandCB;
+       static CONFIGCB           g_configCB;
        static CEC::ICECCallbacks g_cecCallbacks;
 
        int CecLogMessageCB(void *cbParam, const CEC::cec_log_message &message)
@@ -532,41 +678,29 @@ namespace CecSharp
                return 0;
        }
 
+  int CecConfigCB(void *cbParam, const CEC::libcec_configuration &config)
+       {
+               if (g_configCB)
+                       return g_configCB(config);
+               return 0;
+       }
+
        #pragma managed
        // delegates for the unmanaged callback methods
        public delegate int CecLogMessageManagedDelegate(const CEC::cec_log_message &);
        public delegate int CecKeyPressManagedDelegate(const CEC::cec_keypress &);
        public delegate int CecCommandManagedDelegate(const CEC::cec_command &);
+       public delegate int CecConfigManagedDelegate(const CEC::libcec_configuration &);
 
        // callback method interface
        public ref class CecCallbackMethods
        {
        public:
-               CecCallbackMethods(void)
-               {
-                       m_bHasCallbacks = false;
-                       msclr::interop::marshal_context ^ context = gcnew msclr::interop::marshal_context();
-
-                       // create the delegate method for the log message callback
-                       m_logMessageDelegate           = gcnew CecLogMessageManagedDelegate(this, &CecCallbackMethods::CecLogMessageManaged);
-                       m_logMessageGCHandle           = System::Runtime::InteropServices::GCHandle::Alloc(m_logMessageDelegate);
-                       g_logCB                        = static_cast<LOGCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_logMessageDelegate).ToPointer());
-                       g_cecCallbacks.CBCecLogMessage = CecLogMessageCB;
-
-                       // create the delegate method for the keypress callback
-                       m_keypressDelegate           = gcnew CecKeyPressManagedDelegate(this, &CecCallbackMethods::CecKeyPressManaged);
-                       m_keypressGCHandle           = System::Runtime::InteropServices::GCHandle::Alloc(m_keypressDelegate);
-                       g_keyCB                      = static_cast<KEYCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_keypressDelegate).ToPointer());
-                       g_cecCallbacks.CBCecKeyPress = CecKeyPressCB;
-
-                       // create the delegate method for the command callback
-                       m_commandDelegate           = gcnew CecCommandManagedDelegate(this, &CecCallbackMethods::CecCommandManaged);
-                       m_commandGCHandle           = System::Runtime::InteropServices::GCHandle::Alloc(m_commandDelegate);
-                       g_commandCB                 = static_cast<COMMANDCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_commandDelegate).ToPointer());
-                       g_cecCallbacks.CBCecCommand = CecCommandCB;
-
-                       delete context;
-               }
+    CecCallbackMethods(void)
+    {
+      m_bHasCallbacks = false;
+      m_bDelegatesCreated = false;
+    }
 
        ~CecCallbackMethods(void)
    {
@@ -580,8 +714,14 @@ namespace CecSharp
    }
 
        public:
+               virtual void DisableCallbacks(void)
+               {
+                       DestroyDelegates();
+               }
+
                virtual bool EnableCallbacks(CecCallbackMethods ^ callbacks)
                {
+      CreateDelegates();
                        if (!m_bHasCallbacks)
                        {
                                m_bHasCallbacks = true;
@@ -607,6 +747,10 @@ namespace CecSharp
                        return 0;
                }
 
+               virtual int ConfigurationChanged(LibCECConfiguration ^ config)
+               {
+                       return 0;
+               }
        protected:
                // managed callback methods
                int CecLogMessageManaged(const CEC::cec_log_message &message)
@@ -638,15 +782,67 @@ namespace CecSharp
                        return iReturn;
                }
 
+               int CecConfigManaged(const CEC::libcec_configuration &config)
+               {
+                       int iReturn(0);
+                       if (m_bHasCallbacks)
+                       {
+                               LibCECConfiguration ^netConfig = gcnew LibCECConfiguration();
+                               netConfig->Update(config);
+                               iReturn = m_callbacks->ConfigurationChanged(netConfig);
+                       }
+                       return iReturn;
+               }
+
                void DestroyDelegates()
                {
-                       m_bHasCallbacks = false;
-                       delete m_callbacks;
-                       m_logMessageGCHandle.Free();
-                       m_keypressGCHandle.Free();
-                       m_commandGCHandle.Free();
+      m_bHasCallbacks = false;
+                       if (m_bDelegatesCreated)
+                       {
+        m_bDelegatesCreated = false;
+                               m_logMessageGCHandle.Free();
+                               m_keypressGCHandle.Free();
+                               m_commandGCHandle.Free();
+                       }
                }
 
+    void CreateDelegates()
+    {
+      DestroyDelegates();
+
+      if (!m_bDelegatesCreated)
+      {
+        msclr::interop::marshal_context ^ context = gcnew msclr::interop::marshal_context();
+
+        // create the delegate method for the log message callback
+        m_logMessageDelegate           = gcnew CecLogMessageManagedDelegate(this, &CecCallbackMethods::CecLogMessageManaged);
+        m_logMessageGCHandle           = System::Runtime::InteropServices::GCHandle::Alloc(m_logMessageDelegate);
+        g_logCB                        = static_cast<LOGCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_logMessageDelegate).ToPointer());
+        g_cecCallbacks.CBCecLogMessage = CecLogMessageCB;
+
+        // create the delegate method for the keypress callback
+        m_keypressDelegate           = gcnew CecKeyPressManagedDelegate(this, &CecCallbackMethods::CecKeyPressManaged);
+        m_keypressGCHandle           = System::Runtime::InteropServices::GCHandle::Alloc(m_keypressDelegate);
+        g_keyCB                      = static_cast<KEYCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_keypressDelegate).ToPointer());
+        g_cecCallbacks.CBCecKeyPress = CecKeyPressCB;
+
+        // create the delegate method for the command callback
+        m_commandDelegate           = gcnew CecCommandManagedDelegate(this, &CecCallbackMethods::CecCommandManaged);
+        m_commandGCHandle           = System::Runtime::InteropServices::GCHandle::Alloc(m_commandDelegate);
+        g_commandCB                 = static_cast<COMMANDCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_commandDelegate).ToPointer());
+        g_cecCallbacks.CBCecCommand = CecCommandCB;
+
+        // create the delegate method for the configuration change callback
+        m_configDelegate            = gcnew CecConfigManagedDelegate(this, &CecCallbackMethods::CecConfigManaged);
+        m_configGCHandle            = System::Runtime::InteropServices::GCHandle::Alloc(m_configDelegate);
+        g_configCB                  = static_cast<CONFIGCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_configDelegate).ToPointer());
+        g_cecCallbacks.CBCecConfigurationChanged = CecConfigCB;
+
+        delete context;
+        m_bDelegatesCreated = true;
+      }
+    }
+
                CecLogMessageManagedDelegate ^                    m_logMessageDelegate;
                static System::Runtime::InteropServices::GCHandle m_logMessageGCHandle;
                LOGCB                                             m_logMessageCallback;
@@ -659,46 +855,12 @@ namespace CecSharp
                static System::Runtime::InteropServices::GCHandle m_commandGCHandle;
                COMMANDCB                                         m_commandCallback;
 
+         CecConfigManagedDelegate ^                        m_configDelegate;
+               static System::Runtime::InteropServices::GCHandle m_configGCHandle;
+               CONFIGCB                                          m_configCallback;
+
                CecCallbackMethods ^ m_callbacks;
          bool                 m_bHasCallbacks;
-       };
-
-       public ref class LibCECConfiguration
-       {
-       public:
-               LibCECConfiguration(CecCallbackMethods ^callbacks)
-               {
-                       DeviceName          = "";
-                       PhysicalAddress     = CEC_DEFAULT_PHYSICAL_ADDRESS;
-                       BaseDevice          = (CecLogicalAddress)CEC_DEFAULT_BASE_DEVICE;
-                       HDMIPort            = CEC_DEFAULT_HDMI_PORT;
-                       ClientVersion       = CecClientVersion::VersionPre1_5;
-
-                       GetSettingsFromROM  = false;
-                       UseTVMenuLanguage   = CEC_DEFAULT_SETTING_USE_TV_MENU_LANGUAGE ? true : false;
-                       PowerOnStartup      = CEC_DEFAULT_SETTING_POWER_ON_STARTUP ? true : false;
-                       PowerOffShutdown    = CEC_DEFAULT_SETTING_POWER_OFF_SHUTDOWN ? true : false;
-                       PowerOffScreensaver = CEC_DEFAULT_SETTING_POWER_OFF_SCREENSAVER ? true : false;
-                       PowerOffOnStandby   = CEC_DEFAULT_SETTING_POWER_OFF_ON_STANDBY ? true : false;
-
-                       Callbacks           = callbacks;
-               }
-
-               property System::String ^    DeviceName;
-               property CecDeviceTypeList ^ DeviceTypes;
-               property uint16_t            PhysicalAddress;
-               property CecLogicalAddress   BaseDevice;
-               property uint8_t             HDMIPort;
-               property CecClientVersion    ClientVersion;
-
-               // player specific settings
-               property bool                GetSettingsFromROM;
-               property bool                UseTVMenuLanguage;
-               property bool                PowerOnStartup;
-               property bool                PowerOffShutdown;
-               property bool                PowerOffScreensaver;
-               property bool                PowerOffOnStandby;
-
-               property CecCallbackMethods ^Callbacks;
+    bool                 m_bDelegatesCreated;
        };
 }