3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
36 #include <msclr/marshal.h>
37 #include "../../include/cec.h"
43 public enum class CecDeviceType
53 public enum class CecLogLevel
64 public enum class CecLogicalAddress
66 Unknown
= -1, //not a valid logical address
86 public enum class CecPowerStatus
90 InTransitionStandbyToOn
= 0x02,
91 InTransitionOnToStandby
= 0x03,
95 public enum class CecVersion
105 public enum class CecDisplayControl
107 DisplayForDefaultTime
= 0x00,
108 DisplayUntilCleared
= 0x40,
109 ClearPreviousMessage
= 0x80,
110 ReservedForFutureUse
= 0xC0
113 public enum class CecMenuState
119 public enum class CecDeckControlMode
122 SkipReverseRewind
= 2,
127 public enum class CecDeckInfo
139 SkipForwardWind
= 0x1B,
140 SkipReverseRewind
= 0x1C,
141 IndexSearchForward
= 0x1D,
142 IndexSearchReverse
= 0x1E,
146 public enum class CecUserControlCode
178 PreviousChannel
= 0x32,
181 DisplayInformation
= 0x35,
202 VideoOnDemand
= 0x52,
203 ElectronicProgramGuide
= 0x53,
204 TimerProgramming
= 0x54,
205 InitialConfiguration
= 0x55,
207 PausePlayFunction
= 0x61,
208 RecordFunction
= 0x62,
209 PauseRecordFunction
= 0x63,
212 RestoreVolumeFunction
= 0x66,
214 SelectMediaFunction
= 0x68,
215 SelectAVInputFunction
= 0x69,
216 SelectAudioInputFunction
= 0x6A,
217 PowerToggleFunction
= 0x6B,
218 PowerOffFunction
= 0x6C,
219 PowerOnFunction
= 0x6D,
230 public enum class CecVendorId
243 public enum class CecAudioStatus
245 MuteStatusMask
= 0x80,
246 VolumeStatusMask
= 0x7F,
249 VolumeStatusUnknown
= 0x7F
252 public enum class CecOpcode
257 InactiveSource
= 0x9D,
258 RequestActiveSource
= 0x85,
259 RoutingChange
= 0x80,
260 RoutingInformation
= 0x81,
261 SetStreamPath
= 0x86,
266 RecordTvScreen
= 0x0F,
267 ClearAnalogueTimer
= 0x33,
268 ClearDigitalTimer
= 0x99,
269 ClearExternalTimer
= 0xA1,
270 SetAnalogueTimer
= 0x34,
271 SetDigitalTimer
= 0x97,
272 SetExternalTimer
= 0xA2,
273 SetTimerProgramTitle
= 0x67,
274 TimerClearedStatus
= 0x43,
277 GetCecVersion
= 0x9F,
278 GivePhysicalAddress
= 0x83,
279 GetMenuLanguage
= 0x91,
280 ReportPhysicalAddress
= 0x84,
281 SetMenuLanguage
= 0x32,
284 GiveDeckStatus
= 0x1A,
286 GiveTunerDeviceStatus
= 0x08,
287 SelectAnalogueService
= 0x92,
288 SelectDigtalService
= 0x93,
289 TunerDeviceStatus
= 0x07,
290 TunerStepDecrement
= 0x06,
291 TunerStepIncrement
= 0x05,
292 DeviceVendorId
= 0x87,
293 GiveDeviceVendorId
= 0x8C,
294 VendorCommand
= 0x89,
295 VendorCommandWithId
= 0xA0,
296 VendorRemoteButtonDown
= 0x8A,
297 VendorRemoteButtonUp
= 0x8B,
303 UserControlPressed
= 0x44,
304 UserControlRelease
= 0x45,
305 GiveDevicePowerStatus
= 0x8F,
306 ReportPowerStatus
= 0x90,
309 GiveAudioStatus
= 0x71,
310 GiveSystemAudioMode
= 0x7D,
311 ReportAudioStatus
= 0x7A,
312 SetSystemAudioMode
= 0x72,
313 SystemAudioModeRequest
= 0x70,
314 SystemAudioModeStatus
= 0x7E,
316 /* when this opcode is set, no opcode will be sent to the device. this is one of the reserved numbers */
320 public enum class CecSystemAudioStatus
326 public enum class CecClientVersion
329 Version1_5_0
= 0x1500,
330 Version1_5_1
= 0x1501,
331 Version1_5_2
= 0x1502,
332 Version1_5_3
= 0x1503,
333 Version1_6_0
= 0x1600
336 public enum class CecServerVersion
339 Version1_5_0
= 0x1500,
340 Version1_5_1
= 0x1501,
341 Version1_5_2
= 0x1502,
342 Version1_5_3
= 0x1503,
343 Version1_6_0
= 0x1600
346 public ref
class CecAdapter
349 CecAdapter(System::String
^ strPath
, System::String
^ strComPort
)
352 ComPort
= strComPort
;
355 property
System::String
^ Path
;
356 property
System::String
^ ComPort
;
359 public ref
class CecDeviceTypeList
362 CecDeviceTypeList(void)
364 Types
= gcnew array
<CecDeviceType
>(5);
365 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
366 Types
[iPtr
] = CecDeviceType::Reserved
;
369 property array
<CecDeviceType
> ^ Types
;
372 public ref
class CecLogicalAddresses
375 CecLogicalAddresses(void)
377 Addresses
= gcnew array
<CecLogicalAddress
>(16);
383 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
384 Addresses
[iPtr
] = CecLogicalAddress::Unknown
;
387 bool IsSet(CecLogicalAddress iAddress
)
389 return Addresses
[(unsigned int)iAddress
] != CecLogicalAddress::Unknown
;
392 void Set(CecLogicalAddress iAddress
)
394 Addresses
[(unsigned int)iAddress
] = iAddress
;
397 property CecLogicalAddress Primary
;
398 property array
<CecLogicalAddress
> ^ Addresses
;
401 public ref
class CecDatapacket
406 Data
= gcnew array
<uint8_t>(100);
410 void PushBack(uint8_t data
)
419 property array
<uint8_t> ^ Data
;
420 property
uint8_t Size
;
423 public ref
class CecCommand
426 CecCommand(CecLogicalAddress iInitiator
, CecLogicalAddress iDestination
, bool bAck
, bool bEom
, CecOpcode iOpcode
, int32_t iTransmitTimeout
)
428 Initiator
= iInitiator
;
429 Destination
= iDestination
;
434 TransmitTimeout
= iTransmitTimeout
;
435 Parameters
= gcnew CecDatapacket
;
441 Initiator
= CecLogicalAddress::Unknown
;
442 Destination
= CecLogicalAddress::Unknown
;
445 Opcode
= CecOpcode::None
;
448 Parameters
= gcnew CecDatapacket
;
452 void PushBack(uint8_t data
)
454 if (Initiator
== CecLogicalAddress::Unknown
&& Destination
== CecLogicalAddress::Unknown
)
456 Initiator
= (CecLogicalAddress
) (data
>> 4);
457 Destination
= (CecLogicalAddress
) (data
& 0xF);
462 Opcode
= (CecOpcode
)data
;
466 Parameters
->PushBack(data
);
471 property CecLogicalAddress Initiator
;
472 property CecLogicalAddress Destination
;
475 property CecOpcode Opcode
;
476 property CecDatapacket
^ Parameters
;
477 property
bool OpcodeSet
;
478 property
int32_t TransmitTimeout
;
481 public ref
class CecKeypress
484 CecKeypress(int iKeycode
, unsigned int iDuration
)
487 Duration
= iDuration
;
499 property
int Keycode
;
500 property
unsigned int Duration
;
503 public ref
class CecLogMessage
506 CecLogMessage(System::String
^ strMessage
, CecLogLevel iLevel
, int64_t iTime
)
508 Message
= strMessage
;
517 Level
= CecLogLevel::None
;
523 property
System::String
^Message
;
524 property CecLogLevel Level
;
525 property
int64_t Time
;
528 ref
class CecCallbackMethods
; //forward
529 public ref
class LibCECConfiguration
532 LibCECConfiguration(void)
535 DeviceTypes
= gcnew
CecDeviceTypeList();
536 AutodetectAddress
= true;
537 PhysicalAddress
= CEC_DEFAULT_PHYSICAL_ADDRESS
;
538 BaseDevice
= (CecLogicalAddress
)CEC_DEFAULT_BASE_DEVICE
;
539 HDMIPort
= CEC_DEFAULT_HDMI_PORT
;
540 ClientVersion
= CecClientVersion::VersionPre1_5
;
541 ServerVersion
= CecServerVersion::VersionPre1_5
;
542 TvVendor
= CecVendorId::Unknown
;
544 GetSettingsFromROM
= false;
545 UseTVMenuLanguage
= CEC_DEFAULT_SETTING_USE_TV_MENU_LANGUAGE
== 1;
546 ActivateSource
= CEC_DEFAULT_SETTING_ACTIVATE_SOURCE
== 1;
548 WakeDevices
= gcnew
CecLogicalAddresses();
549 if (CEC_DEFAULT_SETTING_ACTIVATE_SOURCE
== 1)
550 WakeDevices
->Set(CecLogicalAddress::Tv
);
552 PowerOffDevices
= gcnew
CecLogicalAddresses();
553 if (CEC_DEFAULT_SETTING_POWER_OFF_SHUTDOWN
== 1)
554 PowerOffDevices
->Set(CecLogicalAddress::Broadcast
);
556 PowerOffScreensaver
= CEC_DEFAULT_SETTING_POWER_OFF_SCREENSAVER
== 1;
557 PowerOffOnStandby
= CEC_DEFAULT_SETTING_POWER_OFF_ON_STANDBY
== 1;
558 SendInactiveSource
= CEC_DEFAULT_SETTING_SEND_INACTIVE_SOURCE
== 1;
559 LogicalAddresses
= gcnew
CecLogicalAddresses();
561 PowerOffDevicesOnStandby
= CEC_DEFAULT_SETTING_POWER_OFF_DEVICES_STANDBY
== 1;
564 void SetCallbacks(CecCallbackMethods
^callbacks
)
566 Callbacks
= callbacks
;
569 void Update(const CEC::libcec_configuration
&config
)
571 DeviceName
= gcnew
System::String(config
.strDeviceName
);
573 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
574 DeviceTypes
->Types
[iPtr
] = (CecDeviceType
)config
.deviceTypes
.types
[iPtr
];
576 AutodetectAddress
= config
.bAutodetectAddress
== 1;
577 PhysicalAddress
= config
.iPhysicalAddress
;
578 BaseDevice
= (CecLogicalAddress
)config
.baseDevice
;
579 HDMIPort
= config
.iHDMIPort
;
580 ClientVersion
= (CecClientVersion
)config
.clientVersion
;
581 ServerVersion
= (CecServerVersion
)config
.serverVersion
;
582 TvVendor
= (CecVendorId
)config
.tvVendor
;
584 // player specific settings
585 GetSettingsFromROM
= config
.bGetSettingsFromROM
== 1;
586 UseTVMenuLanguage
= config
.bUseTVMenuLanguage
== 1;
587 ActivateSource
= config
.bActivateSource
== 1;
589 WakeDevices
->Clear();
590 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
591 if (config
.wakeDevices
[iPtr
])
592 WakeDevices
->Set((CecLogicalAddress
)iPtr
);
594 PowerOffDevices
->Clear();
595 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
596 if (config
.powerOffDevices
[iPtr
])
597 PowerOffDevices
->Set((CecLogicalAddress
)iPtr
);
599 PowerOffScreensaver
= config
.bPowerOffScreensaver
== 1;
600 PowerOffOnStandby
= config
.bPowerOffOnStandby
== 1;
602 if (ServerVersion
>= CecServerVersion::Version1_5_1
)
603 SendInactiveSource
= config
.bSendInactiveSource
== 1;
605 if (ServerVersion
>= CecServerVersion::Version1_5_3
)
607 LogicalAddresses
->Clear();
608 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
609 if (config
.logicalAddresses
[iPtr
])
610 LogicalAddresses
->Set((CecLogicalAddress
)iPtr
);
613 if (ServerVersion
>= CecServerVersion::Version1_6_0
)
615 FirmwareVersion
= config
.iFirmwareVersion
;
616 PowerOffDevicesOnStandby
= config
.bPowerOffDevicesOnStandby
== 1;
620 property
System::String
^ DeviceName
;
621 property CecDeviceTypeList
^ DeviceTypes
;
622 property
bool AutodetectAddress
;
623 property
uint16_t PhysicalAddress
;
624 property CecLogicalAddress BaseDevice
;
625 property
uint8_t HDMIPort
;
626 property CecClientVersion ClientVersion
;
627 property CecServerVersion ServerVersion
;
628 property CecVendorId TvVendor
;
630 // player specific settings
631 property
bool GetSettingsFromROM
;
632 property
bool UseTVMenuLanguage
;
633 property
bool ActivateSource
;
634 property CecLogicalAddresses
^WakeDevices
;
635 property CecLogicalAddresses
^PowerOffDevices
;
636 property
bool PowerOffScreensaver
;
637 property
bool PowerOffOnStandby
;
638 property
bool SendInactiveSource
;
639 property CecLogicalAddresses
^LogicalAddresses
;
640 property
uint16_t FirmwareVersion
;
641 property
bool PowerOffDevicesOnStandby
;
643 property CecCallbackMethods
^ Callbacks
;
646 // the callback methods are called by unmanaged code, so we need some delegates for this
648 // unmanaged callback methods
649 typedef int (__stdcall
*LOGCB
) (const CEC::cec_log_message
&message
);
650 typedef int (__stdcall
*KEYCB
) (const CEC::cec_keypress
&key
);
651 typedef int (__stdcall
*COMMANDCB
)(const CEC::cec_command
&command
);
652 typedef int (__stdcall
*CONFIGCB
) (const CEC::libcec_configuration
&config
);
654 static LOGCB g_logCB
;
655 static KEYCB g_keyCB
;
656 static COMMANDCB g_commandCB
;
657 static CONFIGCB g_configCB
;
658 static CEC::ICECCallbacks g_cecCallbacks
;
660 int CecLogMessageCB(void *cbParam
, const CEC::cec_log_message
&message
)
663 return g_logCB(message
);
667 int CecKeyPressCB(void *cbParam
, const CEC::cec_keypress
&key
)
674 int CecCommandCB(void *cbParam
, const CEC::cec_command
&command
)
677 return g_commandCB(command
);
681 int CecConfigCB(void *cbParam
, const CEC::libcec_configuration
&config
)
684 return g_configCB(config
);
689 // delegates for the unmanaged callback methods
690 public delegate
int CecLogMessageManagedDelegate(const CEC::cec_log_message
&);
691 public delegate
int CecKeyPressManagedDelegate(const CEC::cec_keypress
&);
692 public delegate
int CecCommandManagedDelegate(const CEC::cec_command
&);
693 public delegate
int CecConfigManagedDelegate(const CEC::libcec_configuration
&);
695 // callback method interface
696 public ref
class CecCallbackMethods
699 CecCallbackMethods(void)
701 m_bHasCallbacks
= false;
702 m_bDelegatesCreated
= false;
705 ~CecCallbackMethods(void)
711 !CecCallbackMethods(void)
717 virtual void DisableCallbacks(void)
722 virtual bool EnableCallbacks(CecCallbackMethods
^ callbacks
)
725 if (!m_bHasCallbacks
)
727 m_bHasCallbacks
= true;
728 m_callbacks
= callbacks
;
735 virtual int ReceiveLogMessage(CecLogMessage
^ message
)
740 virtual int ReceiveKeypress(CecKeypress
^ key
)
745 virtual int ReceiveCommand(CecCommand
^ command
)
750 virtual int ConfigurationChanged(LibCECConfiguration
^ config
)
755 // managed callback methods
756 int CecLogMessageManaged(const CEC::cec_log_message
&message
)
760 iReturn
= m_callbacks
->ReceiveLogMessage(gcnew
CecLogMessage(gcnew
System::String(message
.message
), (CecLogLevel
)message
.level
, message
.time
));
764 int CecKeyPressManaged(const CEC::cec_keypress
&key
)
768 iReturn
= m_callbacks
->ReceiveKeypress(gcnew
CecKeypress(key
.keycode
, key
.duration
));
772 int CecCommandManaged(const CEC::cec_command
&command
)
777 CecCommand
^ newCommand
= gcnew
CecCommand((CecLogicalAddress
)command
.initiator
, (CecLogicalAddress
)command
.destination
, command
.ack
== 1 ? true : false, command
.eom
== 1 ? true : false, (CecOpcode
)command
.opcode
, command
.transmit_timeout
);
778 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
779 newCommand
->Parameters
->PushBack(command
.parameters
[iPtr
]);
780 iReturn
= m_callbacks
->ReceiveCommand(newCommand
);
785 int CecConfigManaged(const CEC::libcec_configuration
&config
)
790 LibCECConfiguration
^netConfig
= gcnew
LibCECConfiguration();
791 netConfig
->Update(config
);
792 iReturn
= m_callbacks
->ConfigurationChanged(netConfig
);
797 void DestroyDelegates()
799 m_bHasCallbacks
= false;
800 if (m_bDelegatesCreated
)
802 m_bDelegatesCreated
= false;
803 m_logMessageGCHandle
.Free();
804 m_keypressGCHandle
.Free();
805 m_commandGCHandle
.Free();
809 void CreateDelegates()
813 if (!m_bDelegatesCreated
)
815 msclr::interop::marshal_context
^ context
= gcnew
msclr::interop::marshal_context();
817 // create the delegate method for the log message callback
818 m_logMessageDelegate
= gcnew
CecLogMessageManagedDelegate(this, &CecCallbackMethods::CecLogMessageManaged
);
819 m_logMessageGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_logMessageDelegate
);
820 g_logCB
= static_cast<LOGCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_logMessageDelegate
).ToPointer());
821 g_cecCallbacks
.CBCecLogMessage
= CecLogMessageCB
;
823 // create the delegate method for the keypress callback
824 m_keypressDelegate
= gcnew
CecKeyPressManagedDelegate(this, &CecCallbackMethods::CecKeyPressManaged
);
825 m_keypressGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_keypressDelegate
);
826 g_keyCB
= static_cast<KEYCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_keypressDelegate
).ToPointer());
827 g_cecCallbacks
.CBCecKeyPress
= CecKeyPressCB
;
829 // create the delegate method for the command callback
830 m_commandDelegate
= gcnew
CecCommandManagedDelegate(this, &CecCallbackMethods::CecCommandManaged
);
831 m_commandGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_commandDelegate
);
832 g_commandCB
= static_cast<COMMANDCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_commandDelegate
).ToPointer());
833 g_cecCallbacks
.CBCecCommand
= CecCommandCB
;
835 // create the delegate method for the configuration change callback
836 m_configDelegate
= gcnew
CecConfigManagedDelegate(this, &CecCallbackMethods::CecConfigManaged
);
837 m_configGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_configDelegate
);
838 g_configCB
= static_cast<CONFIGCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_configDelegate
).ToPointer());
839 g_cecCallbacks
.CBCecConfigurationChanged
= CecConfigCB
;
842 m_bDelegatesCreated
= true;
846 CecLogMessageManagedDelegate
^ m_logMessageDelegate
;
847 static System::Runtime::InteropServices::GCHandle m_logMessageGCHandle
;
848 LOGCB m_logMessageCallback
;
850 CecKeyPressManagedDelegate
^ m_keypressDelegate
;
851 static System::Runtime::InteropServices::GCHandle m_keypressGCHandle
;
852 KEYCB m_keypressCallback
;
854 CecCommandManagedDelegate
^ m_commandDelegate
;
855 static System::Runtime::InteropServices::GCHandle m_commandGCHandle
;
856 COMMANDCB m_commandCallback
;
858 CecConfigManagedDelegate
^ m_configDelegate
;
859 static System::Runtime::InteropServices::GCHandle m_configGCHandle
;
860 CONFIGCB m_configCallback
;
862 CecCallbackMethods
^ m_callbacks
;
863 bool m_bHasCallbacks
;
864 bool m_bDelegatesCreated
;