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
242 public enum class CecAudioStatus
244 MuteStatusMask
= 0x80,
245 VolumeStatusMask
= 0x7F,
248 VolumeStatusUnknown
= 0x7F
251 public enum class CecOpcode
256 InactiveSource
= 0x9D,
257 RequestActiveSource
= 0x85,
258 RoutingChange
= 0x80,
259 RoutingInformation
= 0x81,
260 SetStreamPath
= 0x86,
265 RecordTvScreen
= 0x0F,
266 ClearAnalogueTimer
= 0x33,
267 ClearDigitalTimer
= 0x99,
268 ClearExternalTimer
= 0xA1,
269 SetAnalogueTimer
= 0x34,
270 SetDigitalTimer
= 0x97,
271 SetExternalTimer
= 0xA2,
272 SetTimerProgramTitle
= 0x67,
273 TimerClearedStatus
= 0x43,
276 GetCecVersion
= 0x9F,
277 GivePhysicalAddress
= 0x83,
278 GetMenuLanguage
= 0x91,
279 ReportPhysicalAddress
= 0x84,
280 SetMenuLanguage
= 0x32,
283 GiveDeckStatus
= 0x1A,
285 GiveTunerDeviceStatus
= 0x08,
286 SelectAnalogueService
= 0x92,
287 SelectDigtalService
= 0x93,
288 TunerDeviceStatus
= 0x07,
289 TunerStepDecrement
= 0x06,
290 TunerStepIncrement
= 0x05,
291 DeviceVendorId
= 0x87,
292 GiveDeviceVendorId
= 0x8C,
293 VendorCommand
= 0x89,
294 VendorCommandWithId
= 0xA0,
295 VendorRemoteButtonDown
= 0x8A,
296 VendorRemoteButtonUp
= 0x8B,
302 UserControlPressed
= 0x44,
303 UserControlRelease
= 0x45,
304 GiveDevicePowerStatus
= 0x8F,
305 ReportPowerStatus
= 0x90,
308 GiveAudioStatus
= 0x71,
309 GiveSystemAudioMode
= 0x7D,
310 ReportAudioStatus
= 0x7A,
311 SetSystemAudioMode
= 0x72,
312 SystemAudioModeRequest
= 0x70,
313 SystemAudioModeStatus
= 0x7E,
315 /* when this opcode is set, no opcode will be sent to the device. this is one of the reserved numbers */
319 public enum class CecSystemAudioStatus
325 public enum class CecClientVersion
328 Version1_5_0
= 0x1500
331 public ref
class CecAdapter
334 CecAdapter(System::String
^ strPath
, System::String
^ strComPort
)
337 ComPort
= strComPort
;
340 property
System::String
^ Path
;
341 property
System::String
^ ComPort
;
344 public ref
class CecDeviceTypeList
347 CecDeviceTypeList(void)
349 Types
= gcnew array
<CecDeviceType
>(5);
350 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
351 Types
[iPtr
] = CecDeviceType::Reserved
;
354 property array
<CecDeviceType
> ^ Types
;
357 public ref
class CecLogicalAddresses
360 CecLogicalAddresses(void)
362 Addresses
= gcnew array
<CecLogicalAddress
>(16);
368 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
369 Addresses
[iPtr
] = CecLogicalAddress::Unregistered
;
372 bool IsSet(CecLogicalAddress iAddress
)
374 return Addresses
[(unsigned int)iAddress
] != CecLogicalAddress::Unregistered
;
377 void Set(CecLogicalAddress iAddress
)
379 Addresses
[(unsigned int)iAddress
] = iAddress
;
382 property CecLogicalAddress Primary
;
383 property array
<CecLogicalAddress
> ^ Addresses
;
386 public ref
class CecDatapacket
391 Data
= gcnew array
<uint8_t>(100);
395 void PushBack(uint8_t data
)
404 property array
<uint8_t> ^ Data
;
405 property
uint8_t Size
;
408 public ref
class CecCommand
411 CecCommand(CecLogicalAddress iInitiator
, CecLogicalAddress iDestination
, bool bAck
, bool bEom
, CecOpcode iOpcode
, int32_t iTransmitTimeout
)
413 Initiator
= iInitiator
;
414 Destination
= iDestination
;
419 TransmitTimeout
= iTransmitTimeout
;
420 Parameters
= gcnew CecDatapacket
;
426 Initiator
= CecLogicalAddress::Unknown
;
427 Destination
= CecLogicalAddress::Unknown
;
430 Opcode
= CecOpcode::None
;
433 Parameters
= gcnew CecDatapacket
;
437 void PushBack(uint8_t data
)
439 if (Initiator
== CecLogicalAddress::Unknown
&& Destination
== CecLogicalAddress::Unknown
)
441 Initiator
= (CecLogicalAddress
) (data
>> 4);
442 Destination
= (CecLogicalAddress
) (data
& 0xF);
447 Opcode
= (CecOpcode
)data
;
451 Parameters
->PushBack(data
);
456 property CecLogicalAddress Initiator
;
457 property CecLogicalAddress Destination
;
460 property CecOpcode Opcode
;
461 property CecDatapacket
^ Parameters
;
462 property
bool OpcodeSet
;
463 property
int32_t TransmitTimeout
;
466 public ref
class CecKeypress
469 CecKeypress(int iKeycode
, unsigned int iDuration
)
472 Duration
= iDuration
;
484 property
int Keycode
;
485 property
unsigned int Duration
;
488 public ref
class CecLogMessage
491 CecLogMessage(System::String
^ strMessage
, CecLogLevel iLevel
, int64_t iTime
)
493 Message
= strMessage
;
502 Level
= CecLogLevel::None
;
508 property
System::String
^Message
;
509 property CecLogLevel Level
;
510 property
int64_t Time
;
513 ref
class CecCallbackMethods
; //forward
514 public ref
class LibCECConfiguration
517 LibCECConfiguration(void)
520 DeviceTypes
= gcnew
CecDeviceTypeList();
521 PhysicalAddress
= CEC_DEFAULT_PHYSICAL_ADDRESS
;
522 BaseDevice
= (CecLogicalAddress
)CEC_DEFAULT_BASE_DEVICE
;
523 HDMIPort
= CEC_DEFAULT_HDMI_PORT
;
524 ClientVersion
= CecClientVersion::VersionPre1_5
;
525 TvVendor
= CecVendorId::Unknown
;
527 GetSettingsFromROM
= false;
528 UseTVMenuLanguage
= CEC_DEFAULT_SETTING_USE_TV_MENU_LANGUAGE
== 1;
529 ActivateSource
= CEC_DEFAULT_SETTING_ACTIVATE_SOURCE
== 1;
531 WakeDevices
= gcnew
CecLogicalAddresses();
532 if (CEC_DEFAULT_SETTING_ACTIVATE_SOURCE
== 1)
533 WakeDevices
->Set(CecLogicalAddress::Tv
);
535 PowerOffDevices
= gcnew
CecLogicalAddresses();
536 if (CEC_DEFAULT_SETTING_POWER_OFF_SHUTDOWN
== 1)
537 PowerOffDevices
->Set(CecLogicalAddress::Broadcast
);
539 PowerOffScreensaver
= CEC_DEFAULT_SETTING_POWER_OFF_SCREENSAVER
== 1;
540 PowerOffOnStandby
= CEC_DEFAULT_SETTING_POWER_OFF_ON_STANDBY
== 1;
543 void SetCallbacks(CecCallbackMethods
^callbacks
)
545 Callbacks
= callbacks
;
548 property
System::String
^ DeviceName
;
549 property CecDeviceTypeList
^ DeviceTypes
;
550 property
uint16_t PhysicalAddress
;
551 property CecLogicalAddress BaseDevice
;
552 property
uint8_t HDMIPort
;
553 property CecClientVersion ClientVersion
;
554 property CecVendorId TvVendor
;
556 // player specific settings
557 property
bool GetSettingsFromROM
;
558 property
bool UseTVMenuLanguage
;
559 property
bool ActivateSource
;
560 property CecLogicalAddresses
^WakeDevices
;
561 property CecLogicalAddresses
^PowerOffDevices
;
562 property
bool PowerOffScreensaver
;
563 property
bool PowerOffOnStandby
;
565 property CecCallbackMethods
^ Callbacks
;
568 // the callback methods are called by unmanaged code, so we need some delegates for this
570 // unmanaged callback methods
571 typedef int (__stdcall
*LOGCB
) (const CEC::cec_log_message
&message
);
572 typedef int (__stdcall
*KEYCB
) (const CEC::cec_keypress
&key
);
573 typedef int (__stdcall
*COMMANDCB
)(const CEC::cec_command
&command
);
574 typedef int (__stdcall
*CONFIGCB
) (const CEC::libcec_configuration
&config
);
576 static LOGCB g_logCB
;
577 static KEYCB g_keyCB
;
578 static COMMANDCB g_commandCB
;
579 static CONFIGCB g_configCB
;
580 static CEC::ICECCallbacks g_cecCallbacks
;
582 int CecLogMessageCB(void *cbParam
, const CEC::cec_log_message
&message
)
585 return g_logCB(message
);
589 int CecKeyPressCB(void *cbParam
, const CEC::cec_keypress
&key
)
596 int CecCommandCB(void *cbParam
, const CEC::cec_command
&command
)
599 return g_commandCB(command
);
603 int CecConfigCB(void *cbParam
, const CEC::libcec_configuration
&config
)
606 return g_configCB(config
);
611 // delegates for the unmanaged callback methods
612 public delegate
int CecLogMessageManagedDelegate(const CEC::cec_log_message
&);
613 public delegate
int CecKeyPressManagedDelegate(const CEC::cec_keypress
&);
614 public delegate
int CecCommandManagedDelegate(const CEC::cec_command
&);
615 public delegate
int CecConfigManagedDelegate(const CEC::libcec_configuration
&);
617 // callback method interface
618 public ref
class CecCallbackMethods
621 CecCallbackMethods(void)
623 m_bHasCallbacks
= false;
624 msclr::interop::marshal_context
^ context
= gcnew
msclr::interop::marshal_context();
626 // create the delegate method for the log message callback
627 m_logMessageDelegate
= gcnew
CecLogMessageManagedDelegate(this, &CecCallbackMethods::CecLogMessageManaged
);
628 m_logMessageGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_logMessageDelegate
);
629 g_logCB
= static_cast<LOGCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_logMessageDelegate
).ToPointer());
630 g_cecCallbacks
.CBCecLogMessage
= CecLogMessageCB
;
632 // create the delegate method for the keypress callback
633 m_keypressDelegate
= gcnew
CecKeyPressManagedDelegate(this, &CecCallbackMethods::CecKeyPressManaged
);
634 m_keypressGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_keypressDelegate
);
635 g_keyCB
= static_cast<KEYCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_keypressDelegate
).ToPointer());
636 g_cecCallbacks
.CBCecKeyPress
= CecKeyPressCB
;
638 // create the delegate method for the command callback
639 m_commandDelegate
= gcnew
CecCommandManagedDelegate(this, &CecCallbackMethods::CecCommandManaged
);
640 m_commandGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_commandDelegate
);
641 g_commandCB
= static_cast<COMMANDCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_commandDelegate
).ToPointer());
642 g_cecCallbacks
.CBCecCommand
= CecCommandCB
;
644 // create the delegate method for the configuration change callback
645 m_configDelegate
= gcnew
CecConfigManagedDelegate(this, &CecCallbackMethods::CecConfigManaged
);
646 m_configGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_configDelegate
);
647 g_configCB
= static_cast<CONFIGCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_configDelegate
).ToPointer());
648 g_cecCallbacks
.CBCecConfigurationChanged
= CecConfigCB
;
653 ~CecCallbackMethods(void)
659 !CecCallbackMethods(void)
665 virtual bool EnableCallbacks(CecCallbackMethods
^ callbacks
)
667 if (!m_bHasCallbacks
)
669 m_bHasCallbacks
= true;
670 m_callbacks
= callbacks
;
677 virtual int ReceiveLogMessage(CecLogMessage
^ message
)
682 virtual int ReceiveKeypress(CecKeypress
^ key
)
687 virtual int ReceiveCommand(CecCommand
^ command
)
692 virtual int ConfigurationChanged(LibCECConfiguration
^ config
)
697 // managed callback methods
698 int CecLogMessageManaged(const CEC::cec_log_message
&message
)
702 iReturn
= m_callbacks
->ReceiveLogMessage(gcnew
CecLogMessage(gcnew
System::String(message
.message
), (CecLogLevel
)message
.level
, message
.time
));
706 int CecKeyPressManaged(const CEC::cec_keypress
&key
)
710 iReturn
= m_callbacks
->ReceiveKeypress(gcnew
CecKeypress(key
.keycode
, key
.duration
));
714 int CecCommandManaged(const CEC::cec_command
&command
)
719 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
);
720 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
721 newCommand
->Parameters
->PushBack(command
.parameters
[iPtr
]);
722 iReturn
= m_callbacks
->ReceiveCommand(newCommand
);
727 int CecConfigManaged(const CEC::libcec_configuration
&config
)
732 LibCECConfiguration
^netConfig
= gcnew
LibCECConfiguration();
733 netConfig
->DeviceName
= gcnew
System::String(config
.strDeviceName
);
734 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
735 netConfig
->DeviceTypes
->Types
[iPtr
] = (CecDeviceType
)config
.deviceTypes
.types
[iPtr
];
737 netConfig
->PhysicalAddress
= config
.iPhysicalAddress
;
738 netConfig
->BaseDevice
= (CecLogicalAddress
)config
.baseDevice
;
739 netConfig
->HDMIPort
= config
.iHDMIPort
;
740 netConfig
->ClientVersion
= (CecClientVersion
)config
.clientVersion
;
741 netConfig
->GetSettingsFromROM
= config
.bGetSettingsFromROM
== 1;
742 netConfig
->ActivateSource
= config
.bActivateSource
== 1;
744 netConfig
->WakeDevices
->Clear();
746 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
747 if (config
.wakeDevices
[iPtr
])
748 netConfig
->WakeDevices
->Addresses
[iDevices
++] = (CecLogicalAddress
)iPtr
;
750 netConfig
->PowerOffDevices
->Clear();
752 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
753 if (config
.powerOffDevices
[iPtr
])
754 netConfig
->PowerOffDevices
->Addresses
[iDevices
++] = (CecLogicalAddress
)iPtr
;
756 netConfig
->PowerOffScreensaver
= config
.bPowerOffScreensaver
== 1;
757 netConfig
->PowerOffOnStandby
= config
.bPowerOffOnStandby
== 1;
759 iReturn
= m_callbacks
->ConfigurationChanged(netConfig
);
764 void DestroyDelegates()
768 m_bHasCallbacks
= false;
770 m_logMessageGCHandle
.Free();
771 m_keypressGCHandle
.Free();
772 m_commandGCHandle
.Free();
776 CecLogMessageManagedDelegate
^ m_logMessageDelegate
;
777 static System::Runtime::InteropServices::GCHandle m_logMessageGCHandle
;
778 LOGCB m_logMessageCallback
;
780 CecKeyPressManagedDelegate
^ m_keypressDelegate
;
781 static System::Runtime::InteropServices::GCHandle m_keypressGCHandle
;
782 KEYCB m_keypressCallback
;
784 CecCommandManagedDelegate
^ m_commandDelegate
;
785 static System::Runtime::InteropServices::GCHandle m_commandGCHandle
;
786 COMMANDCB m_commandCallback
;
788 CecConfigManagedDelegate
^ m_configDelegate
;
789 static System::Runtime::InteropServices::GCHandle m_configGCHandle
;
790 CONFIGCB m_configCallback
;
792 CecCallbackMethods
^ m_callbacks
;
793 bool m_bHasCallbacks
;