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/
34 #include "../lib/platform/threads/mutex.h"
36 #include <msclr/marshal.h>
37 #include "../../include/cec.h"
44 public enum class CecDeviceType
54 public enum class CecLogLevel
65 public enum class CecLogicalAddress
67 Unknown
= -1, //not a valid logical address
87 public enum class CecAlert
92 public enum class CecParameterType
94 ParameterTypeString
= 1
97 public ref
class CecParameter
100 CecParameter(CecParameterType type
, System::String
^ strData
)
106 property CecParameterType Type
;
107 property
System::String
^ Data
;
110 public enum class CecPowerStatus
114 InTransitionStandbyToOn
= 0x02,
115 InTransitionOnToStandby
= 0x03,
119 public enum class CecVersion
129 public enum class CecDisplayControl
131 DisplayForDefaultTime
= 0x00,
132 DisplayUntilCleared
= 0x40,
133 ClearPreviousMessage
= 0x80,
134 ReservedForFutureUse
= 0xC0
137 public enum class CecMenuState
143 public enum class CecDeckControlMode
146 SkipReverseRewind
= 2,
151 public enum class CecDeckInfo
163 SkipForwardWind
= 0x1B,
164 SkipReverseRewind
= 0x1C,
165 IndexSearchForward
= 0x1D,
166 IndexSearchReverse
= 0x1E,
170 public enum class CecUserControlCode
202 PreviousChannel
= 0x32,
205 DisplayInformation
= 0x35,
226 VideoOnDemand
= 0x52,
227 ElectronicProgramGuide
= 0x53,
228 TimerProgramming
= 0x54,
229 InitialConfiguration
= 0x55,
231 PausePlayFunction
= 0x61,
232 RecordFunction
= 0x62,
233 PauseRecordFunction
= 0x63,
236 RestoreVolumeFunction
= 0x66,
238 SelectMediaFunction
= 0x68,
239 SelectAVInputFunction
= 0x69,
240 SelectAudioInputFunction
= 0x6A,
241 PowerToggleFunction
= 0x6B,
242 PowerOffFunction
= 0x6C,
243 PowerOnFunction
= 0x6D,
251 SamsungReturn
= 0x91,
255 public enum class CecVendorId
259 Panasonic
= 0x008045,
276 public enum class CecAudioStatus
278 MuteStatusMask
= 0x80,
279 VolumeStatusMask
= 0x7F,
282 VolumeStatusUnknown
= 0x7F
285 public enum class CecOpcode
290 InactiveSource
= 0x9D,
291 RequestActiveSource
= 0x85,
292 RoutingChange
= 0x80,
293 RoutingInformation
= 0x81,
294 SetStreamPath
= 0x86,
299 RecordTvScreen
= 0x0F,
300 ClearAnalogueTimer
= 0x33,
301 ClearDigitalTimer
= 0x99,
302 ClearExternalTimer
= 0xA1,
303 SetAnalogueTimer
= 0x34,
304 SetDigitalTimer
= 0x97,
305 SetExternalTimer
= 0xA2,
306 SetTimerProgramTitle
= 0x67,
307 TimerClearedStatus
= 0x43,
310 GetCecVersion
= 0x9F,
311 GivePhysicalAddress
= 0x83,
312 GetMenuLanguage
= 0x91,
313 ReportPhysicalAddress
= 0x84,
314 SetMenuLanguage
= 0x32,
317 GiveDeckStatus
= 0x1A,
319 GiveTunerDeviceStatus
= 0x08,
320 SelectAnalogueService
= 0x92,
321 SelectDigtalService
= 0x93,
322 TunerDeviceStatus
= 0x07,
323 TunerStepDecrement
= 0x06,
324 TunerStepIncrement
= 0x05,
325 DeviceVendorId
= 0x87,
326 GiveDeviceVendorId
= 0x8C,
327 VendorCommand
= 0x89,
328 VendorCommandWithId
= 0xA0,
329 VendorRemoteButtonDown
= 0x8A,
330 VendorRemoteButtonUp
= 0x8B,
336 UserControlPressed
= 0x44,
337 UserControlRelease
= 0x45,
338 GiveDevicePowerStatus
= 0x8F,
339 ReportPowerStatus
= 0x90,
342 GiveAudioStatus
= 0x71,
343 GiveSystemAudioMode
= 0x7D,
344 ReportAudioStatus
= 0x7A,
345 SetSystemAudioMode
= 0x72,
346 SystemAudioModeRequest
= 0x70,
347 SystemAudioModeStatus
= 0x7E,
349 /* when this opcode is set, no opcode will be sent to the device. this is one of the reserved numbers */
353 public enum class CecSystemAudioStatus
359 public enum class CecClientVersion
362 Version1_5_0
= 0x1500,
363 Version1_5_1
= 0x1501,
364 Version1_5_2
= 0x1502,
365 Version1_5_3
= 0x1503,
366 Version1_6_0
= 0x1600,
367 Version1_6_1
= 0x1601,
368 Version1_6_2
= 0x1602,
369 Version1_6_3
= 0x1603,
370 Version1_7_0
= 0x1700,
371 Version1_7_1
= 0x1701,
372 Version1_7_2
= 0x1702,
373 Version1_8_0
= 0x1800,
374 Version1_8_1
= 0x1801,
375 Version1_8_2
= 0x1802,
376 Version1_9_0
= 0x1900
379 public enum class CecServerVersion
382 Version1_5_0
= 0x1500,
383 Version1_5_1
= 0x1501,
384 Version1_5_2
= 0x1502,
385 Version1_5_3
= 0x1503,
386 Version1_6_0
= 0x1600,
387 Version1_6_1
= 0x1601,
388 Version1_6_2
= 0x1602,
389 Version1_6_3
= 0x1603,
390 Version1_7_0
= 0x1700,
391 Version1_7_1
= 0x1701,
392 Version1_7_2
= 0x1702,
393 Version1_8_0
= 0x1800,
394 Version1_8_1
= 0x1801,
395 Version1_8_2
= 0x1802,
396 Version1_9_0
= 0x1900
399 public enum class CecAdapterType
402 PulseEightExternal
= 0x1,
403 PulseEightDaughterboard
= 0x2,
408 /// Descriptor of a CEC adapter,
410 public ref
class CecAdapter
414 /// Create a new CEC adapter descriptor
416 /// <param name="path"> The path descriptor for this CEC adapter</param>
417 /// <param name="comPort">The COM port of this CEC adapter</param>
418 CecAdapter(System::String
^ path
, System::String
^ comPort
)
425 /// The path descriptor for this CEC adapter
427 property
System::String
^ Path
;
430 /// The COM port of this CEC adapter
432 property
System::String
^ ComPort
;
436 /// A list of CEC device types
438 public ref
class CecDeviceTypeList
442 /// Create a new empty list of CEC device types
444 CecDeviceTypeList(void)
446 Types
= gcnew array
<CecDeviceType
>(5);
447 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
448 Types
[iPtr
] = CecDeviceType::Reserved
;
452 /// The array with CecDeviceType instances in this list.
454 property array
<CecDeviceType
> ^ Types
;
458 /// A list of logical addresses
460 public ref
class CecLogicalAddresses
464 /// Create a new empty list of logical addresses
466 CecLogicalAddresses(void)
468 Addresses
= gcnew array
<CecLogicalAddress
>(16);
477 Primary
= CecLogicalAddress::Unknown
;
478 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
479 Addresses
[iPtr
] = CecLogicalAddress::Unknown
;
483 /// Checks whether a logical address is set in this list.
485 /// <param name="address">The address to check.</param>
486 /// <returns>True when set, false otherwise</returns>
487 bool IsSet(CecLogicalAddress address
)
489 return Addresses
[(unsigned int)address
] != CecLogicalAddress::Unknown
;
493 /// Add a logical address to this list (if it's not set already)
495 /// <param name="address">The address to add.</param>
496 void Set(CecLogicalAddress address
)
498 Addresses
[(unsigned int)address
] = address
;
499 if (Primary
== CecLogicalAddress::Unknown
)
504 /// The primary (first) address in this list
506 property CecLogicalAddress Primary
;
509 /// The list of addresses
511 property array
<CecLogicalAddress
> ^ Addresses
;
516 /// Byte array used for CEC command parameters
518 public ref
class CecDatapacket
522 /// Create a new byte array with maximum size 100
526 Data
= gcnew array
<uint8_t>(100);
531 /// Adds a byte to this byte array
533 /// <param name="data">The byte to add.</param>
534 void PushBack(uint8_t data
)
546 property array
<uint8_t> ^ Data
;
549 /// Current data size
551 property
uint8_t Size
;
555 /// A CEC command that is received or transmitted over the CEC bus
557 public ref
class CecCommand
561 /// Create a new CEC command instance
563 /// <param name="initiator">The initiator of the command</param>
564 /// <param name="destination">The receiver of the command</param>
565 /// <param name="ack">True when the ack bit is set, false otherwise</param>
566 /// <param name="eom">True when the eom bit is set, false otherwise</param>
567 /// <param name="opcode">The CEC opcode of this command</param>
568 /// <param name="transmitTimeout">The timeout to use when transmitting a command</param>
569 CecCommand(CecLogicalAddress initiator
, CecLogicalAddress destination
, bool ack
, bool eom
, CecOpcode opcode
, int32_t transmitTimeout
)
571 Initiator
= initiator
;
572 Destination
= destination
;
577 TransmitTimeout
= transmitTimeout
;
578 Parameters
= gcnew CecDatapacket
;
583 /// Create a new empty CEC command instance
587 Initiator
= CecLogicalAddress::Unknown
;
588 Destination
= CecLogicalAddress::Unknown
;
591 Opcode
= CecOpcode::None
;
594 Parameters
= gcnew CecDatapacket
;
599 /// Pushes a byte of data to this CEC command
601 /// <param name="data">The byte to add</param>
602 void PushBack(uint8_t data
)
604 if (Initiator
== CecLogicalAddress::Unknown
&& Destination
== CecLogicalAddress::Unknown
)
606 Initiator
= (CecLogicalAddress
) (data
>> 4);
607 Destination
= (CecLogicalAddress
) (data
& 0xF);
612 Opcode
= (CecOpcode
)data
;
616 Parameters
->PushBack(data
);
621 /// True when this command is empty, false otherwise.
625 /// The initiator of the command
627 property CecLogicalAddress Initiator
;
629 /// The destination of the command
631 property CecLogicalAddress Destination
;
633 /// True when the ack bit is set, false otherwise
637 /// True when the eom bit is set, false otherwise
641 /// The CEC opcode of the command
643 property CecOpcode Opcode
;
645 /// The parameters of this command
647 property CecDatapacket
^ Parameters
;
649 /// True when an opcode is set, false otherwise (poll message)
651 property
bool OpcodeSet
;
653 /// The timeout to use when transmitting a command
655 property
int32_t TransmitTimeout
;
659 /// A key press that was received
661 public ref
class CecKeypress
665 /// Create a new key press instance
667 /// <param name="keycode">The key code of this key press</param>
668 /// <param name="duration">The duration of this key press in milliseconds</param>
669 CecKeypress(CecUserControlCode keycode
, unsigned int duration
)
677 /// Create a new empty key press instance
681 Keycode
= CecUserControlCode::Unknown
;
687 /// True when empty, false otherwise
691 /// The key code of this key press
693 property CecUserControlCode Keycode
;
695 /// The duration of this key press in milliseconds
697 property
unsigned int Duration
;
701 /// A log message that libCEC generated
703 public ref
class CecLogMessage
707 /// Create a new log message
709 /// <param name="message">The actual message</param>
710 /// <param name="level">The log level, so the application can choose what type information to display</param>
711 /// <param name="time">The timestamp of this message, in milliseconds after connecting</param>
712 CecLogMessage(System::String
^ message
, CecLogLevel level
, int64_t time
)
721 /// Create a new empty log message
726 Level
= CecLogLevel::None
;
732 /// True when empty, false otherwise.
736 /// The actual message
738 property
System::String
^Message
;
740 /// The log level, so the application can choose what type information to display
742 property CecLogLevel Level
;
744 /// The timestamp of this message, in milliseconds after connecting
746 property
int64_t Time
;
749 ref
class CecCallbackMethods
; //forward declaration
752 /// The configuration that libCEC uses.
754 public ref
class LibCECConfiguration
758 /// Create a new configuration instance with default settings.
760 LibCECConfiguration(void)
763 DeviceTypes
= gcnew
CecDeviceTypeList();
764 AutodetectAddress
= true;
765 PhysicalAddress
= CEC_DEFAULT_PHYSICAL_ADDRESS
;
766 BaseDevice
= (CecLogicalAddress
)CEC_DEFAULT_BASE_DEVICE
;
767 HDMIPort
= CEC_DEFAULT_HDMI_PORT
;
768 ClientVersion
= CecClientVersion::VersionPre1_5
;
769 ServerVersion
= CecServerVersion::VersionPre1_5
;
770 TvVendor
= CecVendorId::Unknown
;
772 GetSettingsFromROM
= false;
773 UseTVMenuLanguage
= CEC_DEFAULT_SETTING_USE_TV_MENU_LANGUAGE
== 1;
774 ActivateSource
= CEC_DEFAULT_SETTING_ACTIVATE_SOURCE
== 1;
776 WakeDevices
= gcnew
CecLogicalAddresses();
777 if (CEC_DEFAULT_SETTING_ACTIVATE_SOURCE
== 1)
778 WakeDevices
->Set(CecLogicalAddress::Tv
);
780 PowerOffDevices
= gcnew
CecLogicalAddresses();
781 if (CEC_DEFAULT_SETTING_POWER_OFF_SHUTDOWN
== 1)
782 PowerOffDevices
->Set(CecLogicalAddress::Broadcast
);
784 PowerOffScreensaver
= CEC_DEFAULT_SETTING_POWER_OFF_SCREENSAVER
== 1;
785 PowerOffOnStandby
= CEC_DEFAULT_SETTING_POWER_OFF_ON_STANDBY
== 1;
787 SendInactiveSource
= CEC_DEFAULT_SETTING_SEND_INACTIVE_SOURCE
== 1;
788 LogicalAddresses
= gcnew
CecLogicalAddresses();
790 PowerOffDevicesOnStandby
= CEC_DEFAULT_SETTING_POWER_OFF_DEVICES_STANDBY
== 1;
791 ShutdownOnStandby
= CEC_DEFAULT_SETTING_SHUTDOWN_ON_STANDBY
== 1;
793 FirmwareBuildDate
= gcnew
System::DateTime(1970,1,1,0,0,0,0);
794 CECVersion
= (CecVersion
)CEC_DEFAULT_SETTING_CEC_VERSION
;
795 AdapterType
= CecAdapterType::Unknown
;
799 /// Change the callback method pointers in this configuration instance.
801 /// <param name="callbacks">The new callbacks</param>
802 void SetCallbacks(CecCallbackMethods
^callbacks
)
804 Callbacks
= callbacks
;
808 /// Update this configuration with data received from libCEC
810 /// <param name="config">The configuration that was received from libCEC</param>
811 void Update(const CEC::libcec_configuration
&config
)
813 DeviceName
= gcnew
System::String(config
.strDeviceName
);
815 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
816 DeviceTypes
->Types
[iPtr
] = (CecDeviceType
)config
.deviceTypes
.types
[iPtr
];
818 AutodetectAddress
= config
.bAutodetectAddress
== 1;
819 PhysicalAddress
= config
.iPhysicalAddress
;
820 BaseDevice
= (CecLogicalAddress
)config
.baseDevice
;
821 HDMIPort
= config
.iHDMIPort
;
822 ClientVersion
= (CecClientVersion
)config
.clientVersion
;
823 ServerVersion
= (CecServerVersion
)config
.serverVersion
;
824 TvVendor
= (CecVendorId
)config
.tvVendor
;
826 // player specific settings
827 GetSettingsFromROM
= config
.bGetSettingsFromROM
== 1;
828 UseTVMenuLanguage
= config
.bUseTVMenuLanguage
== 1;
829 ActivateSource
= config
.bActivateSource
== 1;
831 WakeDevices
->Clear();
832 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
833 if (config
.wakeDevices
[iPtr
])
834 WakeDevices
->Set((CecLogicalAddress
)iPtr
);
836 PowerOffDevices
->Clear();
837 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
838 if (config
.powerOffDevices
[iPtr
])
839 PowerOffDevices
->Set((CecLogicalAddress
)iPtr
);
841 PowerOffScreensaver
= config
.bPowerOffScreensaver
== 1;
842 PowerOffOnStandby
= config
.bPowerOffOnStandby
== 1;
844 if (ServerVersion
>= CecServerVersion::Version1_5_1
)
845 SendInactiveSource
= config
.bSendInactiveSource
== 1;
847 if (ServerVersion
>= CecServerVersion::Version1_5_3
)
849 LogicalAddresses
->Clear();
850 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
851 if (config
.logicalAddresses
[iPtr
])
852 LogicalAddresses
->Set((CecLogicalAddress
)iPtr
);
855 if (ServerVersion
>= CecServerVersion::Version1_6_0
)
857 FirmwareVersion
= config
.iFirmwareVersion
;
858 PowerOffDevicesOnStandby
= config
.bPowerOffDevicesOnStandby
== 1;
859 ShutdownOnStandby
= config
.bShutdownOnStandby
== 1;
862 if (ServerVersion
>= CecServerVersion::Version1_6_2
)
864 DeviceLanguage
= gcnew
System::String(config
.strDeviceLanguage
);
865 FirmwareBuildDate
= gcnew
System::DateTime(1970,1,1,0,0,0,0);
866 FirmwareBuildDate
= FirmwareBuildDate
->AddSeconds(config
.iFirmwareBuildDate
);
869 if (ServerVersion
>= CecServerVersion::Version1_6_3
)
870 MonitorOnlyClient
= config
.bMonitorOnly
== 1;
872 if (ServerVersion
>= CecServerVersion::Version1_8_0
)
873 CECVersion
= (CecVersion
)config
.cecVersion
;
875 if (ServerVersion
>= CecServerVersion::Version1_8_2
)
876 AdapterType
= (CecAdapterType
)config
.adapterType
;
880 /// The device name to use on the CEC bus
882 property
System::String
^ DeviceName
;
885 /// The device type(s) to use on the CEC bus for libCEC
887 property CecDeviceTypeList
^ DeviceTypes
;
890 /// True to try to autodetect the physical address, false otherwise
892 property
bool AutodetectAddress
;
895 /// The physical address that libCEC uses on the CEC bus
897 property
uint16_t PhysicalAddress
;
900 /// The logical address of the device to which the CEC adapter is connected, only used when PhysicalAddress isn't set
902 property CecLogicalAddress BaseDevice
;
905 /// The hdmi port number on the device to which the CEC adapter is connected, only used when PhysicalAddress isn't set
907 property
uint8_t HDMIPort
;
910 /// The client API version to use
912 property CecClientVersion ClientVersion
;
915 /// The version of libCEC
917 property CecServerVersion ServerVersion
;
920 /// Override the vendor ID of the TV when set (for quirks mode)
922 property CecVendorId TvVendor
;
925 /// True to read the settings from the EEPROM, which possibly override the settings passed here
927 property
bool GetSettingsFromROM
;
930 /// Use the language setting of the TV in the client application. Must be implemented by the client application.
932 property
bool UseTVMenuLanguage
;
935 /// Make libCEC the active source when starting the client application
937 property
bool ActivateSource
;
940 /// List of devices to send a power on command to when starting the client application
942 property CecLogicalAddresses
^WakeDevices
;
945 /// List of devices to send a standby command to when exiting the client application
947 property CecLogicalAddresses
^PowerOffDevices
;
950 /// Send standby commands when the client application activates the screensaver. Must be implemented by the client application.
952 property
bool PowerOffScreensaver
;
955 /// Power off the PC when the TV powers off. Must be implemented by the client application.
957 property
bool PowerOffOnStandby
;
960 /// Send an inactive source message when exiting the client application.
962 property
bool SendInactiveSource
;
965 /// The list of logical addresses that libCEC is using
967 property CecLogicalAddresses
^LogicalAddresses
;
970 /// The firmware version of the adapter to which libCEC is connected
972 property
uint16_t FirmwareVersion
;
975 /// Send standby commands when the client application activates standby mode (S3). Must be implemented by the client application.
977 property
bool PowerOffDevicesOnStandby
;
978 property
bool ShutdownOnStandby
;
981 /// True to start a monitor-only client, false to start a standard client.
983 property
bool MonitorOnlyClient
;
986 /// The language code of the menu language that libCEC reports to other devices.
988 property
System::String
^ DeviceLanguage
;
991 /// The callback methods to use.
993 property CecCallbackMethods
^ Callbacks
;
996 /// The build date of the firmware.
998 property
System::DateTime
^ FirmwareBuildDate
;
1001 /// The CEC version that libCEC uses.
1003 property CecVersion CECVersion
;
1006 /// The type of adapter that libCEC is connected to.
1008 property CecAdapterType AdapterType
;
1011 // the callback methods are called by unmanaged code, so we need some delegates for this
1013 // unmanaged callback methods
1014 typedef int (__stdcall
*LOGCB
) (const CEC::cec_log_message
&message
);
1015 typedef int (__stdcall
*KEYCB
) (const CEC::cec_keypress
&key
);
1016 typedef int (__stdcall
*COMMANDCB
)(const CEC::cec_command
&command
);
1017 typedef int (__stdcall
*CONFIGCB
) (const CEC::libcec_configuration
&config
);
1018 typedef int (__stdcall
*ALERTCB
) (const CEC::libcec_alert
, const CEC::libcec_parameter
&data
);
1019 typedef int (__stdcall
*MENUCB
) (const CEC::cec_menu_state newVal
);
1020 typedef void (__stdcall
*ACTICB
) (const CEC::cec_logical_address logicalAddress
, const uint8_t bActivated
);
1026 COMMANDCB commandCB
;
1030 ACTICB sourceActivatedCB
;
1031 } UnmanagedCecCallbacks
;
1033 static PLATFORM::CMutex g_callbackMutex
;
1034 static std::vector
<UnmanagedCecCallbacks
> g_unmanagedCallbacks
;
1035 static CEC::ICECCallbacks g_cecCallbacks
;
1037 int CecLogMessageCB(void *cbParam
, const CEC::cec_log_message
&message
)
1041 size_t iPtr
= (size_t)cbParam
;
1042 PLATFORM::CLockObject
lock(g_callbackMutex
);
1043 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1044 return g_unmanagedCallbacks
[iPtr
].logCB(message
);
1049 int CecKeyPressCB(void *cbParam
, const CEC::cec_keypress
&key
)
1053 size_t iPtr
= (size_t)cbParam
;
1054 PLATFORM::CLockObject
lock(g_callbackMutex
);
1055 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1056 return g_unmanagedCallbacks
[iPtr
].keyCB(key
);
1061 int CecCommandCB(void *cbParam
, const CEC::cec_command
&command
)
1065 size_t iPtr
= (size_t)cbParam
;
1066 PLATFORM::CLockObject
lock(g_callbackMutex
);
1067 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1068 return g_unmanagedCallbacks
[iPtr
].commandCB(command
);
1073 int CecConfigCB(void *cbParam
, const CEC::libcec_configuration
&config
)
1077 size_t iPtr
= (size_t)cbParam
;
1078 PLATFORM::CLockObject
lock(g_callbackMutex
);
1079 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1080 return g_unmanagedCallbacks
[iPtr
].configCB(config
);
1085 int CecAlertCB(void *cbParam
, const CEC::libcec_alert alert
, const CEC::libcec_parameter
&data
)
1089 size_t iPtr
= (size_t)cbParam
;
1090 PLATFORM::CLockObject
lock(g_callbackMutex
);
1091 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1092 return g_unmanagedCallbacks
[iPtr
].alertCB(alert
, data
);
1097 int CecMenuCB(void *cbParam
, const CEC::cec_menu_state newVal
)
1101 size_t iPtr
= (size_t)cbParam
;
1102 PLATFORM::CLockObject
lock(g_callbackMutex
);
1103 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1104 return g_unmanagedCallbacks
[iPtr
].menuCB(newVal
);
1109 void CecSourceActivatedCB(void *cbParam
, const CEC::cec_logical_address logicalAddress
, const uint8_t bActivated
)
1113 size_t iPtr
= (size_t)cbParam
;
1114 PLATFORM::CLockObject
lock(g_callbackMutex
);
1115 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1116 g_unmanagedCallbacks
[iPtr
].sourceActivatedCB(logicalAddress
, bActivated
);
1121 // delegates for the unmanaged callback methods
1122 public delegate
int CecLogMessageManagedDelegate(const CEC::cec_log_message
&);
1123 public delegate
int CecKeyPressManagedDelegate(const CEC::cec_keypress
&);
1124 public delegate
int CecCommandManagedDelegate(const CEC::cec_command
&);
1125 public delegate
int CecConfigManagedDelegate(const CEC::libcec_configuration
&);
1126 public delegate
int CecAlertManagedDelegate(const CEC::libcec_alert
, const CEC::libcec_parameter
&);
1127 public delegate
int CecMenuManagedDelegate(const CEC::cec_menu_state newVal
);
1128 public delegate
void CecSourceActivatedManagedDelegate(const CEC::cec_logical_address logicalAddress
, const uint8_t bActivated
);
1130 void AssignCallbacks()
1132 g_cecCallbacks
.CBCecLogMessage
= CecLogMessageCB
;
1133 g_cecCallbacks
.CBCecKeyPress
= CecKeyPressCB
;
1134 g_cecCallbacks
.CBCecCommand
= CecCommandCB
;
1135 g_cecCallbacks
.CBCecConfigurationChanged
= CecConfigCB
;
1136 g_cecCallbacks
.CBCecAlert
= CecAlertCB
;
1137 g_cecCallbacks
.CBCecMenuStateChanged
= CecMenuCB
;
1138 g_cecCallbacks
.CBCecSourceActivated
= CecSourceActivatedCB
;
1142 /// The callback methods that libCEC uses
1144 public ref
class CecCallbackMethods
1147 CecCallbackMethods(void)
1149 m_iCallbackPtr
= -1;
1151 m_bHasCallbacks
= false;
1152 m_bDelegatesCreated
= false;
1155 ~CecCallbackMethods(void)
1160 size_t GetCallbackPtr(void)
1162 PLATFORM::CLockObject
lock(g_callbackMutex
);
1163 return m_iCallbackPtr
;
1167 !CecCallbackMethods(void)
1173 virtual void DisableCallbacks(void)
1178 virtual bool EnableCallbacks(CecCallbackMethods
^ callbacks
)
1181 if (!m_bHasCallbacks
)
1183 m_bHasCallbacks
= true;
1184 m_callbacks
= callbacks
;
1192 /// Called by libCEC to send back a log message to the application
1194 /// <param name="message">The log message</param>
1195 virtual int ReceiveLogMessage(CecLogMessage
^ message
)
1201 /// Called by libCEC to send back a key press to the application
1203 /// <param name="key">The key press command that libCEC received</param>
1204 virtual int ReceiveKeypress(CecKeypress
^ key
)
1210 /// Called by libCEC to send back raw CEC data to the application
1212 /// <param name="command">The raw CEC data</param>
1213 virtual int ReceiveCommand(CecCommand
^ command
)
1219 /// Called by libCEC to send back an updated configuration to the application
1221 /// <param name="config">The new configuration</param>
1222 virtual int ConfigurationChanged(LibCECConfiguration
^ config
)
1228 /// Called by libCEC to send back an alert message to the application
1230 /// <param name="data">The alert message</param>
1231 virtual int ReceiveAlert(CecAlert alert
, CecParameter
^ data
)
1237 /// Called by libCEC to send back a menu stata change to the application
1239 /// <param name="newVal">The new menu state</param>
1240 virtual int ReceiveMenuStateChange(CecMenuState newVal
)
1246 /// Called by libCEC to notify the application that the source that is handled by libCEC was (de)activated
1248 /// <param name="logicalAddress">The logical address that was (de)activated</param>
1249 /// <param name="activated">True when activated, false when deactivated</param>
1250 virtual void SourceActivated(CecLogicalAddress logicalAddress
, bool activated
)
1255 // managed callback methods
1256 int CecLogMessageManaged(const CEC::cec_log_message
&message
)
1259 if (m_bHasCallbacks
)
1260 iReturn
= m_callbacks
->ReceiveLogMessage(gcnew
CecLogMessage(gcnew
System::String(message
.message
), (CecLogLevel
)message
.level
, message
.time
));
1264 int CecKeyPressManaged(const CEC::cec_keypress
&key
)
1267 if (m_bHasCallbacks
)
1268 iReturn
= m_callbacks
->ReceiveKeypress(gcnew
CecKeypress((CecUserControlCode
)key
.keycode
, key
.duration
));
1272 int CecCommandManaged(const CEC::cec_command
&command
)
1275 if (m_bHasCallbacks
)
1277 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
);
1278 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
1279 newCommand
->Parameters
->PushBack(command
.parameters
[iPtr
]);
1280 iReturn
= m_callbacks
->ReceiveCommand(newCommand
);
1285 int CecConfigManaged(const CEC::libcec_configuration
&config
)
1288 if (m_bHasCallbacks
)
1290 LibCECConfiguration
^netConfig
= gcnew
LibCECConfiguration();
1291 netConfig
->Update(config
);
1292 iReturn
= m_callbacks
->ConfigurationChanged(netConfig
);
1297 int CecAlertManaged(const CEC::libcec_alert alert
, const CEC::libcec_parameter
&data
)
1300 if (m_bHasCallbacks
)
1302 CecParameterType newType
= (CecParameterType
)data
.paramType
;
1303 if (newType
== CecParameterType::ParameterTypeString
)
1305 System::String
^ newData
= gcnew
System::String((const char *)data
.paramData
, 0, 128);
1306 CecParameter
^ newParam
= gcnew
CecParameter(newType
, newData
);
1307 iReturn
= m_callbacks
->ReceiveAlert((CecAlert
)alert
, newParam
);
1313 int CecMenuManaged(const CEC::cec_menu_state newVal
)
1316 if (m_bHasCallbacks
)
1318 iReturn
= m_callbacks
->ReceiveMenuStateChange((CecMenuState
)newVal
);
1323 void CecSourceActivatedManaged(const CEC::cec_logical_address logicalAddress
, const uint8_t bActivated
)
1325 if (m_bHasCallbacks
)
1326 m_callbacks
->SourceActivated((CecLogicalAddress
)logicalAddress
, bActivated
== 1);
1329 void DestroyDelegates()
1331 m_bHasCallbacks
= false;
1332 if (m_bDelegatesCreated
)
1334 m_bDelegatesCreated
= false;
1335 m_logMessageGCHandle
.Free();
1336 m_keypressGCHandle
.Free();
1337 m_commandGCHandle
.Free();
1338 m_alertGCHandle
.Free();
1339 m_menuGCHandle
.Free();
1340 m_sourceActivatedGCHandle
.Free();
1344 void CreateDelegates()
1348 if (!m_bDelegatesCreated
)
1350 msclr::interop::marshal_context
^ context
= gcnew
msclr::interop::marshal_context();
1352 // create the delegate method for the log message callback
1353 m_logMessageDelegate
= gcnew
CecLogMessageManagedDelegate(this, &CecCallbackMethods::CecLogMessageManaged
);
1354 m_logMessageGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_logMessageDelegate
);
1355 m_logMessageCallback
= static_cast<LOGCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_logMessageDelegate
).ToPointer());
1357 // create the delegate method for the keypress callback
1358 m_keypressDelegate
= gcnew
CecKeyPressManagedDelegate(this, &CecCallbackMethods::CecKeyPressManaged
);
1359 m_keypressGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_keypressDelegate
);
1360 m_keypressCallback
= static_cast<KEYCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_keypressDelegate
).ToPointer());
1362 // create the delegate method for the command callback
1363 m_commandDelegate
= gcnew
CecCommandManagedDelegate(this, &CecCallbackMethods::CecCommandManaged
);
1364 m_commandGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_commandDelegate
);
1365 m_commandCallback
= static_cast<COMMANDCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_commandDelegate
).ToPointer());
1367 // create the delegate method for the configuration change callback
1368 m_configDelegate
= gcnew
CecConfigManagedDelegate(this, &CecCallbackMethods::CecConfigManaged
);
1369 m_configGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_configDelegate
);
1370 m_configCallback
= static_cast<CONFIGCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_configDelegate
).ToPointer());
1372 // create the delegate method for the alert callback
1373 m_alertDelegate
= gcnew
CecAlertManagedDelegate(this, &CecCallbackMethods::CecAlertManaged
);
1374 m_alertGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_alertDelegate
);
1375 m_alertCallback
= static_cast<ALERTCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_alertDelegate
).ToPointer());
1377 // create the delegate method for the menu callback
1378 m_menuDelegate
= gcnew
CecMenuManagedDelegate(this, &CecCallbackMethods::CecMenuManaged
);
1379 m_menuGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_menuDelegate
);
1380 m_menuCallback
= static_cast<MENUCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_menuDelegate
).ToPointer());
1382 // create the delegate method for the source activated callback
1383 m_sourceActivatedDelegate
= gcnew
CecSourceActivatedManagedDelegate(this, &CecCallbackMethods::CecSourceActivatedManaged
);
1384 m_sourceActivatedGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_sourceActivatedDelegate
);
1385 m_sourceActivatedCallback
= static_cast<ACTICB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_sourceActivatedDelegate
).ToPointer());
1389 UnmanagedCecCallbacks unmanagedCallbacks
;
1390 unmanagedCallbacks
.logCB
= m_logMessageCallback
;
1391 unmanagedCallbacks
.keyCB
= m_keypressCallback
;
1392 unmanagedCallbacks
.commandCB
= m_commandCallback
;
1393 unmanagedCallbacks
.configCB
= m_configCallback
;
1394 unmanagedCallbacks
.alertCB
= m_alertCallback
;
1395 unmanagedCallbacks
.menuCB
= m_menuCallback
;
1396 unmanagedCallbacks
.sourceActivatedCB
= m_sourceActivatedCallback
;
1398 PLATFORM::CLockObject
lock(g_callbackMutex
);
1399 g_unmanagedCallbacks
.push_back(unmanagedCallbacks
);
1400 m_iCallbackPtr
= g_unmanagedCallbacks
.size() - 1;
1401 m_bDelegatesCreated
= true;
1405 CecLogMessageManagedDelegate
^ m_logMessageDelegate
;
1406 static System::Runtime::InteropServices::GCHandle m_logMessageGCHandle
;
1407 LOGCB m_logMessageCallback
;
1409 CecKeyPressManagedDelegate
^ m_keypressDelegate
;
1410 static System::Runtime::InteropServices::GCHandle m_keypressGCHandle
;
1411 KEYCB m_keypressCallback
;
1413 CecCommandManagedDelegate
^ m_commandDelegate
;
1414 static System::Runtime::InteropServices::GCHandle m_commandGCHandle
;
1415 COMMANDCB m_commandCallback
;
1417 CecConfigManagedDelegate
^ m_configDelegate
;
1418 static System::Runtime::InteropServices::GCHandle m_configGCHandle
;
1419 CONFIGCB m_configCallback
;
1421 CecAlertManagedDelegate
^ m_alertDelegate
;
1422 static System::Runtime::InteropServices::GCHandle m_alertGCHandle
;
1423 ALERTCB m_alertCallback
;
1425 CecMenuManagedDelegate
^ m_menuDelegate
;
1426 static System::Runtime::InteropServices::GCHandle m_menuGCHandle
;
1427 MENUCB m_menuCallback
;
1429 CecSourceActivatedManagedDelegate
^ m_sourceActivatedDelegate
;
1430 static System::Runtime::InteropServices::GCHandle m_sourceActivatedGCHandle
;
1431 ACTICB m_sourceActivatedCallback
;
1433 CecCallbackMethods
^ m_callbacks
;
1434 bool m_bHasCallbacks
;
1435 bool m_bDelegatesCreated
;
1436 size_t m_iCallbackPtr
;