X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2FLibCecSharp%2FLibCecSharp.cpp;h=76dd00624e7948d3fcbf9eedfcf7d73f156f1b86;hb=00a02d1d839c2155ee8abc3a37d10ab12b9881db;hp=92a4eee84e18116a847b11bae513b238013941a1;hpb=4ef3b314fa1f81ba7190cc5c7c3ee273ef7eb1eb;p=deb_libcec.git diff --git a/src/LibCecSharp/LibCecSharp.cpp b/src/LibCecSharp/LibCecSharp.cpp index 92a4eee..76dd006 100644 --- a/src/LibCecSharp/LibCecSharp.cpp +++ b/src/LibCecSharp/LibCecSharp.cpp @@ -43,32 +43,83 @@ namespace CecSharp public ref class LibCecSharp : public CecCallbackMethods { public: + LibCecSharp(LibCECConfiguration ^config) + { + m_callbacks = config->Callbacks; + CecCallbackMethods::EnableCallbacks(m_callbacks); + if (!InitialiseLibCec(config)) + throw gcnew Exception("Could not initialise LibCecSharp"); + } + LibCecSharp(String ^ strDeviceName, CecDeviceTypeList ^ deviceTypes) { + m_callbacks = gcnew CecCallbackMethods(); + LibCECConfiguration ^config = gcnew LibCECConfiguration(); + config->SetCallbacks(this); + config->DeviceName = strDeviceName; + config->DeviceTypes = deviceTypes; + if (!InitialiseLibCec(config)) + throw gcnew Exception("Could not initialise LibCecSharp"); + } + + ~LibCecSharp(void) + { + Close(); + m_libCec = NULL; + } + + private: + !LibCecSharp(void) + { + Close(); + m_libCec = NULL; + } + + bool InitialiseLibCec(LibCECConfiguration ^config) + { marshal_context ^ context = gcnew marshal_context(); - m_bHasCallbacks = false; - const char* strDeviceNameC = context->marshal_as(strDeviceName); + libcec_configuration libCecConfig; + ConvertConfiguration(context, config, libCecConfig); - cec_device_type_list types; - for (unsigned int iPtr = 0; iPtr < 5; iPtr++) - types.types[iPtr] = (cec_device_type)deviceTypes->Types[iPtr]; - m_libCec = (ICECAdapter *) CECInit(strDeviceNameC, types); + m_libCec = (ICECAdapter *) CECInitialise(&libCecConfig); delete context; + return m_libCec != NULL; + } + + void ConvertConfiguration(marshal_context ^context, LibCECConfiguration ^netConfig, CEC::libcec_configuration &config) + { + config.Clear(); + + _snprintf_s(config.strDeviceName, 13, context->marshal_as(netConfig->DeviceName)); + for (unsigned int iPtr = 0; iPtr < 5; iPtr++) + config.deviceTypes.types[iPtr] = (cec_device_type)netConfig->DeviceTypes->Types[iPtr]; + + config.bAutodetectAddress = netConfig->AutodetectAddress ? 1 : 0; + config.iPhysicalAddress = netConfig->PhysicalAddress; + config.baseDevice = (cec_logical_address)netConfig->BaseDevice; + config.iHDMIPort = netConfig->HDMIPort; + config.clientVersion = (cec_client_version)netConfig->ClientVersion; + config.bGetSettingsFromROM = netConfig->GetSettingsFromROM ? 1 : 0; + config.bActivateSource = netConfig->ActivateSource ? 1 : 0; + config.tvVendor = (cec_vendor_id)netConfig->TvVendor; + config.wakeDevices.Clear(); + for (int iPtr = 0; iPtr < 16; iPtr++) + { + if (netConfig->WakeDevices->IsSet((CecLogicalAddress)iPtr)) + config.wakeDevices.Set((cec_logical_address)iPtr); + } + config.powerOffDevices.Clear(); + for (int iPtr = 0; iPtr < 16; iPtr++) + { + if (netConfig->PowerOffDevices->IsSet((CecLogicalAddress)iPtr)) + config.powerOffDevices.Set((cec_logical_address)iPtr); + } + config.bPowerOffScreensaver = netConfig->PowerOffScreensaver ? 1 : 0; + config.bPowerOffOnStandby = netConfig->PowerOffOnStandby ? 1 : 0; + config.bSendInactiveSource = netConfig->SendInactiveSource ? 1 : 0; + config.callbacks = &g_cecCallbacks; } - - ~LibCecSharp(void) - { - Close(); - m_libCec = NULL; - } - - protected: - !LibCecSharp(void) - { - Close(); - m_libCec = NULL; - } public: array ^ FindAdapters(String ^ path) @@ -91,6 +142,8 @@ namespace CecSharp bool Open(String ^ strPort, int iTimeoutMs) { + CecCallbackMethods::EnableCallbacks(m_callbacks); + EnableCallbacks(m_callbacks); marshal_context ^ context = gcnew marshal_context(); const char* strPortC = context->marshal_as(strPort); bool bReturn = m_libCec->Open(strPortC, iTimeoutMs); @@ -100,9 +153,18 @@ namespace CecSharp void Close(void) { + DisableCallbacks(); m_libCec->Close(); } + virtual void DisableCallbacks(void) override + { + // delete the callbacks, since these might already have been destroyed in .NET + CecCallbackMethods::DisableCallbacks(); + if (m_libCec) + m_libCec->EnableCallbacks(NULL, NULL); + } + virtual bool EnableCallbacks(CecCallbackMethods ^ callbacks) override { if (m_libCec && CecCallbackMethods::EnableCallbacks(callbacks)) @@ -190,7 +252,7 @@ namespace CecSharp return m_libCec->SetLogicalAddress((cec_logical_address) logicalAddress); } - bool SetPhysicalAddress(int16_t physicalAddress) + bool SetPhysicalAddress(uint16_t physicalAddress) { return m_libCec->SetPhysicalAddress(physicalAddress); } @@ -277,6 +339,11 @@ namespace CecSharp return (CecPowerStatus) m_libCec->GetDevicePowerStatus((cec_logical_address) logicalAddress); } + void RescanActiveDevices(void) + { + m_libCec->RescanActiveDevices(); + } + CecLogicalAddresses ^ GetActiveDevices(void) { CecLogicalAddresses ^ retVal = gcnew CecLogicalAddresses(); @@ -352,6 +419,68 @@ namespace CecSharp return m_libCec->GetDevicePhysicalAddress((cec_logical_address)iAddress); } + bool SetStreamPath(CecLogicalAddress iAddress) + { + return m_libCec->SetStreamPath((cec_logical_address)iAddress); + } + + bool SetStreamPath(uint16_t iPhysicalAddress) + { + return m_libCec->SetStreamPath(iPhysicalAddress); + } + + CecLogicalAddresses ^GetLogicalAddresses(void) + { + CecLogicalAddresses ^addr = gcnew CecLogicalAddresses(); + cec_logical_addresses libAddr = m_libCec->GetLogicalAddresses(); + for (unsigned int iPtr = 0; iPtr < 16; iPtr++) + addr->Addresses[iPtr] = (CecLogicalAddress)libAddr.addresses[iPtr]; + addr->Primary = (CecLogicalAddress)libAddr.primary; + return addr; + } + + bool GetCurrentConfiguration(LibCECConfiguration ^configuration) + { + libcec_configuration config; + config.Clear(); + + if (m_libCec->GetCurrentConfiguration(&config)) + { + configuration->Update(config); + return true; + } + return false; + } + + bool CanPersistConfiguration(void) + { + return m_libCec->CanPersistConfiguration(); + } + + bool PersistConfiguration(LibCECConfiguration ^configuration) + { + marshal_context ^ context = gcnew marshal_context(); + libcec_configuration config; + ConvertConfiguration(context, configuration, config); + + bool bReturn = m_libCec->PersistConfiguration(&config); + + delete context; + return bReturn; + } + + bool SetConfiguration(LibCECConfiguration ^configuration) + { + marshal_context ^ context = gcnew marshal_context(); + libcec_configuration config; + ConvertConfiguration(context, configuration, config); + + bool bReturn = m_libCec->SetConfiguration(&config); + + delete context; + return bReturn; + } + String ^ ToString(CecLogicalAddress iAddress) { const char *retVal = m_libCec->ToString((cec_logical_address)iAddress); @@ -412,7 +541,20 @@ namespace CecSharp return gcnew String(retVal); } + String ^ ToString(CecClientVersion version) + { + const char *retVal = m_libCec->ToString((cec_client_version)version); + return gcnew String(retVal); + } + + String ^ ToString(CecServerVersion version) + { + const char *retVal = m_libCec->ToString((cec_server_version)version); + return gcnew String(retVal); + } + private: ICECAdapter * m_libCec; + CecCallbackMethods ^ m_callbacks; }; }