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
400 /// Descriptor of a CEC adapter,
402 public ref
class CecAdapter
406 /// Create a new CEC adapter descriptor
408 /// <param name="path"> The path descriptor for this CEC adapter</param>
409 /// <param name="comPort">The COM port of this CEC adapter</param>
410 CecAdapter(System::String
^ path
, System::String
^ comPort
)
417 /// The path descriptor for this CEC adapter
419 property
System::String
^ Path
;
422 /// The COM port of this CEC adapter
424 property
System::String
^ ComPort
;
428 /// A list of CEC device types
430 public ref
class CecDeviceTypeList
434 /// Create a new empty list of CEC device types
436 CecDeviceTypeList(void)
438 Types
= gcnew array
<CecDeviceType
>(5);
439 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
440 Types
[iPtr
] = CecDeviceType::Reserved
;
444 /// The array with CecDeviceType instances in this list.
446 property array
<CecDeviceType
> ^ Types
;
450 /// A list of logical addresses
452 public ref
class CecLogicalAddresses
456 /// Create a new empty list of logical addresses
458 CecLogicalAddresses(void)
460 Addresses
= gcnew array
<CecLogicalAddress
>(16);
469 Primary
= CecLogicalAddress::Unknown
;
470 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
471 Addresses
[iPtr
] = CecLogicalAddress::Unknown
;
475 /// Checks whether a logical address is set in this list.
477 /// <param name="address">The address to check.</param>
478 /// <returns>True when set, false otherwise</returns>
479 bool IsSet(CecLogicalAddress address
)
481 return Addresses
[(unsigned int)address
] != CecLogicalAddress::Unknown
;
485 /// Add a logical address to this list (if it's not set already)
487 /// <param name="address">The address to add.</param>
488 void Set(CecLogicalAddress address
)
490 Addresses
[(unsigned int)address
] = address
;
491 if (Primary
== CecLogicalAddress::Unknown
)
496 /// The primary (first) address in this list
498 property CecLogicalAddress Primary
;
501 /// The list of addresses
503 property array
<CecLogicalAddress
> ^ Addresses
;
508 /// Byte array used for CEC command parameters
510 public ref
class CecDatapacket
514 /// Create a new byte array with maximum size 100
518 Data
= gcnew array
<uint8_t>(100);
523 /// Adds a byte to this byte array
525 /// <param name="data">The byte to add.</param>
526 void PushBack(uint8_t data
)
538 property array
<uint8_t> ^ Data
;
541 /// Current data size
543 property
uint8_t Size
;
547 /// A CEC command that is received or transmitted over the CEC bus
549 public ref
class CecCommand
553 /// Create a new CEC command instance
555 /// <param name="initiator">The initiator of the command</param>
556 /// <param name="destination">The receiver of the command</param>
557 /// <param name="ack">True when the ack bit is set, false otherwise</param>
558 /// <param name="eom">True when the eom bit is set, false otherwise</param>
559 /// <param name="opcode">The CEC opcode of this command</param>
560 /// <param name="transmitTimeout">The timeout to use when transmitting a command</param>
561 CecCommand(CecLogicalAddress initiator
, CecLogicalAddress destination
, bool ack
, bool eom
, CecOpcode opcode
, int32_t transmitTimeout
)
563 Initiator
= initiator
;
564 Destination
= destination
;
569 TransmitTimeout
= transmitTimeout
;
570 Parameters
= gcnew CecDatapacket
;
575 /// Create a new empty CEC command instance
579 Initiator
= CecLogicalAddress::Unknown
;
580 Destination
= CecLogicalAddress::Unknown
;
583 Opcode
= CecOpcode::None
;
586 Parameters
= gcnew CecDatapacket
;
591 /// Pushes a byte of data to this CEC command
593 /// <param name="data">The byte to add</param>
594 void PushBack(uint8_t data
)
596 if (Initiator
== CecLogicalAddress::Unknown
&& Destination
== CecLogicalAddress::Unknown
)
598 Initiator
= (CecLogicalAddress
) (data
>> 4);
599 Destination
= (CecLogicalAddress
) (data
& 0xF);
604 Opcode
= (CecOpcode
)data
;
608 Parameters
->PushBack(data
);
613 /// True when this command is empty, false otherwise.
617 /// The initiator of the command
619 property CecLogicalAddress Initiator
;
621 /// The destination of the command
623 property CecLogicalAddress Destination
;
625 /// True when the ack bit is set, false otherwise
629 /// True when the eom bit is set, false otherwise
633 /// The CEC opcode of the command
635 property CecOpcode Opcode
;
637 /// The parameters of this command
639 property CecDatapacket
^ Parameters
;
641 /// True when an opcode is set, false otherwise (poll message)
643 property
bool OpcodeSet
;
645 /// The timeout to use when transmitting a command
647 property
int32_t TransmitTimeout
;
651 /// A key press that was received
653 public ref
class CecKeypress
657 /// Create a new key press instance
659 /// <param name="keycode">The key code of this key press</param>
660 /// <param name="duration">The duration of this key press in milliseconds</param>
661 CecKeypress(CecUserControlCode keycode
, unsigned int duration
)
669 /// Create a new empty key press instance
673 Keycode
= CecUserControlCode::Unknown
;
679 /// True when empty, false otherwise
683 /// The key code of this key press
685 property CecUserControlCode Keycode
;
687 /// The duration of this key press in milliseconds
689 property
unsigned int Duration
;
693 /// A log message that libCEC generated
695 public ref
class CecLogMessage
699 /// Create a new log message
701 /// <param name="message">The actual message</param>
702 /// <param name="level">The log level, so the application can choose what type information to display</param>
703 /// <param name="time">The timestamp of this message, in milliseconds after connecting</param>
704 CecLogMessage(System::String
^ message
, CecLogLevel level
, int64_t time
)
713 /// Create a new empty log message
718 Level
= CecLogLevel::None
;
724 /// True when empty, false otherwise.
728 /// The actual message
730 property
System::String
^Message
;
732 /// The log level, so the application can choose what type information to display
734 property CecLogLevel Level
;
736 /// The timestamp of this message, in milliseconds after connecting
738 property
int64_t Time
;
741 ref
class CecCallbackMethods
; //forward declaration
744 /// The configuration that libCEC uses.
746 public ref
class LibCECConfiguration
750 /// Create a new configuration instance with default settings.
752 LibCECConfiguration(void)
755 DeviceTypes
= gcnew
CecDeviceTypeList();
756 AutodetectAddress
= true;
757 PhysicalAddress
= CEC_DEFAULT_PHYSICAL_ADDRESS
;
758 BaseDevice
= (CecLogicalAddress
)CEC_DEFAULT_BASE_DEVICE
;
759 HDMIPort
= CEC_DEFAULT_HDMI_PORT
;
760 ClientVersion
= CecClientVersion::VersionPre1_5
;
761 ServerVersion
= CecServerVersion::VersionPre1_5
;
762 TvVendor
= CecVendorId::Unknown
;
764 GetSettingsFromROM
= false;
765 UseTVMenuLanguage
= CEC_DEFAULT_SETTING_USE_TV_MENU_LANGUAGE
== 1;
766 ActivateSource
= CEC_DEFAULT_SETTING_ACTIVATE_SOURCE
== 1;
768 WakeDevices
= gcnew
CecLogicalAddresses();
769 if (CEC_DEFAULT_SETTING_ACTIVATE_SOURCE
== 1)
770 WakeDevices
->Set(CecLogicalAddress::Tv
);
772 PowerOffDevices
= gcnew
CecLogicalAddresses();
773 if (CEC_DEFAULT_SETTING_POWER_OFF_SHUTDOWN
== 1)
774 PowerOffDevices
->Set(CecLogicalAddress::Broadcast
);
776 PowerOffScreensaver
= CEC_DEFAULT_SETTING_POWER_OFF_SCREENSAVER
== 1;
777 PowerOffOnStandby
= CEC_DEFAULT_SETTING_POWER_OFF_ON_STANDBY
== 1;
779 SendInactiveSource
= CEC_DEFAULT_SETTING_SEND_INACTIVE_SOURCE
== 1;
780 LogicalAddresses
= gcnew
CecLogicalAddresses();
782 PowerOffDevicesOnStandby
= CEC_DEFAULT_SETTING_POWER_OFF_DEVICES_STANDBY
== 1;
783 ShutdownOnStandby
= CEC_DEFAULT_SETTING_SHUTDOWN_ON_STANDBY
== 1;
788 /// Change the callback method pointers in this configuration instance.
790 /// <param name="callbacks">The new callbacks</param>
791 void SetCallbacks(CecCallbackMethods
^callbacks
)
793 Callbacks
= callbacks
;
797 /// Update this configuration with data received from libCEC
799 /// <param name="config">The configuration that was received from libCEC</param>
800 void Update(const CEC::libcec_configuration
&config
)
802 DeviceName
= gcnew
System::String(config
.strDeviceName
);
804 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
805 DeviceTypes
->Types
[iPtr
] = (CecDeviceType
)config
.deviceTypes
.types
[iPtr
];
807 AutodetectAddress
= config
.bAutodetectAddress
== 1;
808 PhysicalAddress
= config
.iPhysicalAddress
;
809 BaseDevice
= (CecLogicalAddress
)config
.baseDevice
;
810 HDMIPort
= config
.iHDMIPort
;
811 ClientVersion
= (CecClientVersion
)config
.clientVersion
;
812 ServerVersion
= (CecServerVersion
)config
.serverVersion
;
813 TvVendor
= (CecVendorId
)config
.tvVendor
;
815 // player specific settings
816 GetSettingsFromROM
= config
.bGetSettingsFromROM
== 1;
817 UseTVMenuLanguage
= config
.bUseTVMenuLanguage
== 1;
818 ActivateSource
= config
.bActivateSource
== 1;
820 WakeDevices
->Clear();
821 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
822 if (config
.wakeDevices
[iPtr
])
823 WakeDevices
->Set((CecLogicalAddress
)iPtr
);
825 PowerOffDevices
->Clear();
826 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
827 if (config
.powerOffDevices
[iPtr
])
828 PowerOffDevices
->Set((CecLogicalAddress
)iPtr
);
830 PowerOffScreensaver
= config
.bPowerOffScreensaver
== 1;
831 PowerOffOnStandby
= config
.bPowerOffOnStandby
== 1;
833 if (ServerVersion
>= CecServerVersion::Version1_5_1
)
834 SendInactiveSource
= config
.bSendInactiveSource
== 1;
836 if (ServerVersion
>= CecServerVersion::Version1_5_3
)
838 LogicalAddresses
->Clear();
839 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
840 if (config
.logicalAddresses
[iPtr
])
841 LogicalAddresses
->Set((CecLogicalAddress
)iPtr
);
844 if (ServerVersion
>= CecServerVersion::Version1_6_0
)
846 FirmwareVersion
= config
.iFirmwareVersion
;
847 PowerOffDevicesOnStandby
= config
.bPowerOffDevicesOnStandby
== 1;
848 ShutdownOnStandby
= config
.bShutdownOnStandby
== 1;
851 if (ServerVersion
>= CecServerVersion::Version1_6_2
)
852 DeviceLanguage
= gcnew
System::String(config
.strDeviceLanguage
);
854 if (ServerVersion
>= CecServerVersion::Version1_6_3
)
855 MonitorOnlyClient
= config
.bMonitorOnly
== 1;
859 /// The device name to use on the CEC bus
861 property
System::String
^ DeviceName
;
864 /// The device type(s) to use on the CEC bus for libCEC
866 property CecDeviceTypeList
^ DeviceTypes
;
869 /// True to try to autodetect the physical address, false otherwise
871 property
bool AutodetectAddress
;
874 /// The physical address that libCEC uses on the CEC bus
876 property
uint16_t PhysicalAddress
;
879 /// The logical address of the device to which the CEC adapter is connected, only used when PhysicalAddress isn't set
881 property CecLogicalAddress BaseDevice
;
884 /// The hdmi port number on the device to which the CEC adapter is connected, only used when PhysicalAddress isn't set
886 property
uint8_t HDMIPort
;
889 /// The client API version to use
891 property CecClientVersion ClientVersion
;
894 /// The version of libCEC
896 property CecServerVersion ServerVersion
;
899 /// Override the vendor ID of the TV when set (for quirks mode)
901 property CecVendorId TvVendor
;
904 /// True to read the settings from the EEPROM, which possibly override the settings passed here
906 property
bool GetSettingsFromROM
;
909 /// Use the language setting of the TV in the client application. Must be implemented by the client application.
911 property
bool UseTVMenuLanguage
;
914 /// Make libCEC the active source when starting the client application
916 property
bool ActivateSource
;
919 /// List of devices to send a power on command to when starting the client application
921 property CecLogicalAddresses
^WakeDevices
;
924 /// List of devices to send a standby command to when exiting the client application
926 property CecLogicalAddresses
^PowerOffDevices
;
929 /// Send standby commands when the client application activates the screensaver. Must be implemented by the client application.
931 property
bool PowerOffScreensaver
;
934 /// Power off the PC when the TV powers off. Must be implemented by the client application.
936 property
bool PowerOffOnStandby
;
939 /// Send an inactive source message when exiting the client application.
941 property
bool SendInactiveSource
;
944 /// The list of logical addresses that libCEC is using
946 property CecLogicalAddresses
^LogicalAddresses
;
949 /// The firmware version of the adapter to which libCEC is connected
951 property
uint16_t FirmwareVersion
;
954 /// Send standby commands when the client application activates standby mode (S3). Must be implemented by the client application.
956 property
bool PowerOffDevicesOnStandby
;
957 property
bool ShutdownOnStandby
;
960 /// True to start a monitor-only client, false to start a standard client.
962 property
bool MonitorOnlyClient
;
965 /// The language code of the menu language that libCEC reports to other devices.
967 property
System::String
^ DeviceLanguage
;
970 /// The callback methods to use.
972 property CecCallbackMethods
^ Callbacks
;
975 // the callback methods are called by unmanaged code, so we need some delegates for this
977 // unmanaged callback methods
978 typedef int (__stdcall
*LOGCB
) (const CEC::cec_log_message
&message
);
979 typedef int (__stdcall
*KEYCB
) (const CEC::cec_keypress
&key
);
980 typedef int (__stdcall
*COMMANDCB
)(const CEC::cec_command
&command
);
981 typedef int (__stdcall
*CONFIGCB
) (const CEC::libcec_configuration
&config
);
982 typedef int (__stdcall
*ALERTCB
) (const CEC::libcec_alert
, const CEC::libcec_parameter
&data
);
983 typedef int (__stdcall
*MENUCB
) (const CEC::cec_menu_state newVal
);
984 typedef void (__stdcall
*ACTICB
) (const CEC::cec_logical_address logicalAddress
, const uint8_t bActivated
);
994 ACTICB sourceActivatedCB
;
995 } UnmanagedCecCallbacks
;
997 static PLATFORM::CMutex g_callbackMutex
;
998 static std::vector
<UnmanagedCecCallbacks
> g_unmanagedCallbacks
;
999 static CEC::ICECCallbacks g_cecCallbacks
;
1001 int CecLogMessageCB(void *cbParam
, const CEC::cec_log_message
&message
)
1005 size_t iPtr
= (size_t)cbParam
;
1006 PLATFORM::CLockObject
lock(g_callbackMutex
);
1007 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1008 return g_unmanagedCallbacks
[iPtr
].logCB(message
);
1013 int CecKeyPressCB(void *cbParam
, const CEC::cec_keypress
&key
)
1017 size_t iPtr
= (size_t)cbParam
;
1018 PLATFORM::CLockObject
lock(g_callbackMutex
);
1019 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1020 return g_unmanagedCallbacks
[iPtr
].keyCB(key
);
1025 int CecCommandCB(void *cbParam
, const CEC::cec_command
&command
)
1029 size_t iPtr
= (size_t)cbParam
;
1030 PLATFORM::CLockObject
lock(g_callbackMutex
);
1031 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1032 return g_unmanagedCallbacks
[iPtr
].commandCB(command
);
1037 int CecConfigCB(void *cbParam
, const CEC::libcec_configuration
&config
)
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
].configCB(config
);
1049 int CecAlertCB(void *cbParam
, const CEC::libcec_alert alert
, const CEC::libcec_parameter
&data
)
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
].alertCB(alert
, data
);
1061 int CecMenuCB(void *cbParam
, const CEC::cec_menu_state newVal
)
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
].menuCB(newVal
);
1073 void CecSourceActivatedCB(void *cbParam
, const CEC::cec_logical_address logicalAddress
, const uint8_t bActivated
)
1077 size_t iPtr
= (size_t)cbParam
;
1078 PLATFORM::CLockObject
lock(g_callbackMutex
);
1079 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1080 g_unmanagedCallbacks
[iPtr
].sourceActivatedCB(logicalAddress
, bActivated
);
1085 // delegates for the unmanaged callback methods
1086 public delegate
int CecLogMessageManagedDelegate(const CEC::cec_log_message
&);
1087 public delegate
int CecKeyPressManagedDelegate(const CEC::cec_keypress
&);
1088 public delegate
int CecCommandManagedDelegate(const CEC::cec_command
&);
1089 public delegate
int CecConfigManagedDelegate(const CEC::libcec_configuration
&);
1090 public delegate
int CecAlertManagedDelegate(const CEC::libcec_alert
, const CEC::libcec_parameter
&);
1091 public delegate
int CecMenuManagedDelegate(const CEC::cec_menu_state newVal
);
1092 public delegate
void CecSourceActivatedManagedDelegate(const CEC::cec_logical_address logicalAddress
, const uint8_t bActivated
);
1094 void AssignCallbacks()
1096 g_cecCallbacks
.CBCecLogMessage
= CecLogMessageCB
;
1097 g_cecCallbacks
.CBCecKeyPress
= CecKeyPressCB
;
1098 g_cecCallbacks
.CBCecCommand
= CecCommandCB
;
1099 g_cecCallbacks
.CBCecConfigurationChanged
= CecConfigCB
;
1100 g_cecCallbacks
.CBCecAlert
= CecAlertCB
;
1101 g_cecCallbacks
.CBCecMenuStateChanged
= CecMenuCB
;
1102 g_cecCallbacks
.CBCecSourceActivated
= CecSourceActivatedCB
;
1106 /// The callback methods that libCEC uses
1108 public ref
class CecCallbackMethods
1111 CecCallbackMethods(void)
1113 m_iCallbackPtr
= -1;
1115 m_bHasCallbacks
= false;
1116 m_bDelegatesCreated
= false;
1119 ~CecCallbackMethods(void)
1124 size_t GetCallbackPtr(void)
1126 PLATFORM::CLockObject
lock(g_callbackMutex
);
1127 return m_iCallbackPtr
;
1131 !CecCallbackMethods(void)
1137 virtual void DisableCallbacks(void)
1142 virtual bool EnableCallbacks(CecCallbackMethods
^ callbacks
)
1145 if (!m_bHasCallbacks
)
1147 m_bHasCallbacks
= true;
1148 m_callbacks
= callbacks
;
1156 /// Called by libCEC to send back a log message to the application
1158 /// <param name="message">The log message</param>
1159 virtual int ReceiveLogMessage(CecLogMessage
^ message
)
1165 /// Called by libCEC to send back a key press to the application
1167 /// <param name="key">The key press command that libCEC received</param>
1168 virtual int ReceiveKeypress(CecKeypress
^ key
)
1174 /// Called by libCEC to send back raw CEC data to the application
1176 /// <param name="command">The raw CEC data</param>
1177 virtual int ReceiveCommand(CecCommand
^ command
)
1183 /// Called by libCEC to send back an updated configuration to the application
1185 /// <param name="config">The new configuration</param>
1186 virtual int ConfigurationChanged(LibCECConfiguration
^ config
)
1192 /// Called by libCEC to send back an alert message to the application
1194 /// <param name="data">The alert message</param>
1195 virtual int ReceiveAlert(CecAlert alert
, CecParameter
^ data
)
1201 /// Called by libCEC to send back a menu stata change to the application
1203 /// <param name="newVal">The new menu state</param>
1204 virtual int ReceiveMenuStateChange(CecMenuState newVal
)
1210 /// Called by libCEC to notify the application that the source that is handled by libCEC was (de)activated
1212 /// <param name="logicalAddress">The logical address that was (de)activated</param>
1213 /// <param name="activated">True when activated, false when deactivated</param>
1214 virtual void SourceActivated(CecLogicalAddress logicalAddress
, bool activated
)
1219 // managed callback methods
1220 int CecLogMessageManaged(const CEC::cec_log_message
&message
)
1223 if (m_bHasCallbacks
)
1224 iReturn
= m_callbacks
->ReceiveLogMessage(gcnew
CecLogMessage(gcnew
System::String(message
.message
), (CecLogLevel
)message
.level
, message
.time
));
1228 int CecKeyPressManaged(const CEC::cec_keypress
&key
)
1231 if (m_bHasCallbacks
)
1232 iReturn
= m_callbacks
->ReceiveKeypress(gcnew
CecKeypress((CecUserControlCode
)key
.keycode
, key
.duration
));
1236 int CecCommandManaged(const CEC::cec_command
&command
)
1239 if (m_bHasCallbacks
)
1241 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
);
1242 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
1243 newCommand
->Parameters
->PushBack(command
.parameters
[iPtr
]);
1244 iReturn
= m_callbacks
->ReceiveCommand(newCommand
);
1249 int CecConfigManaged(const CEC::libcec_configuration
&config
)
1252 if (m_bHasCallbacks
)
1254 LibCECConfiguration
^netConfig
= gcnew
LibCECConfiguration();
1255 netConfig
->Update(config
);
1256 iReturn
= m_callbacks
->ConfigurationChanged(netConfig
);
1261 int CecAlertManaged(const CEC::libcec_alert alert
, const CEC::libcec_parameter
&data
)
1264 if (m_bHasCallbacks
)
1266 CecParameterType newType
= (CecParameterType
)data
.paramType
;
1267 if (newType
== CecParameterType::ParameterTypeString
)
1269 System::String
^ newData
= gcnew
System::String((const char *)data
.paramData
, 0, 128);
1270 CecParameter
^ newParam
= gcnew
CecParameter(newType
, newData
);
1271 iReturn
= m_callbacks
->ReceiveAlert((CecAlert
)alert
, newParam
);
1277 int CecMenuManaged(const CEC::cec_menu_state newVal
)
1280 if (m_bHasCallbacks
)
1282 iReturn
= m_callbacks
->ReceiveMenuStateChange((CecMenuState
)newVal
);
1287 void CecSourceActivatedManaged(const CEC::cec_logical_address logicalAddress
, const uint8_t bActivated
)
1289 if (m_bHasCallbacks
)
1290 m_callbacks
->SourceActivated((CecLogicalAddress
)logicalAddress
, bActivated
== 1);
1293 void DestroyDelegates()
1295 m_bHasCallbacks
= false;
1296 if (m_bDelegatesCreated
)
1298 m_bDelegatesCreated
= false;
1299 m_logMessageGCHandle
.Free();
1300 m_keypressGCHandle
.Free();
1301 m_commandGCHandle
.Free();
1302 m_alertGCHandle
.Free();
1303 m_menuGCHandle
.Free();
1304 m_sourceActivatedGCHandle
.Free();
1308 void CreateDelegates()
1312 if (!m_bDelegatesCreated
)
1314 msclr::interop::marshal_context
^ context
= gcnew
msclr::interop::marshal_context();
1316 // create the delegate method for the log message callback
1317 m_logMessageDelegate
= gcnew
CecLogMessageManagedDelegate(this, &CecCallbackMethods::CecLogMessageManaged
);
1318 m_logMessageGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_logMessageDelegate
);
1319 m_logMessageCallback
= static_cast<LOGCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_logMessageDelegate
).ToPointer());
1321 // create the delegate method for the keypress callback
1322 m_keypressDelegate
= gcnew
CecKeyPressManagedDelegate(this, &CecCallbackMethods::CecKeyPressManaged
);
1323 m_keypressGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_keypressDelegate
);
1324 m_keypressCallback
= static_cast<KEYCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_keypressDelegate
).ToPointer());
1326 // create the delegate method for the command callback
1327 m_commandDelegate
= gcnew
CecCommandManagedDelegate(this, &CecCallbackMethods::CecCommandManaged
);
1328 m_commandGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_commandDelegate
);
1329 m_commandCallback
= static_cast<COMMANDCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_commandDelegate
).ToPointer());
1331 // create the delegate method for the configuration change callback
1332 m_configDelegate
= gcnew
CecConfigManagedDelegate(this, &CecCallbackMethods::CecConfigManaged
);
1333 m_configGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_configDelegate
);
1334 m_configCallback
= static_cast<CONFIGCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_configDelegate
).ToPointer());
1336 // create the delegate method for the alert callback
1337 m_alertDelegate
= gcnew
CecAlertManagedDelegate(this, &CecCallbackMethods::CecAlertManaged
);
1338 m_alertGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_alertDelegate
);
1339 m_alertCallback
= static_cast<ALERTCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_alertDelegate
).ToPointer());
1341 // create the delegate method for the menu callback
1342 m_menuDelegate
= gcnew
CecMenuManagedDelegate(this, &CecCallbackMethods::CecMenuManaged
);
1343 m_menuGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_menuDelegate
);
1344 m_menuCallback
= static_cast<MENUCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_menuDelegate
).ToPointer());
1346 // create the delegate method for the source activated callback
1347 m_sourceActivatedDelegate
= gcnew
CecSourceActivatedManagedDelegate(this, &CecCallbackMethods::CecSourceActivatedManaged
);
1348 m_sourceActivatedGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_sourceActivatedDelegate
);
1349 m_sourceActivatedCallback
= static_cast<ACTICB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_sourceActivatedDelegate
).ToPointer());
1353 UnmanagedCecCallbacks unmanagedCallbacks
;
1354 unmanagedCallbacks
.logCB
= m_logMessageCallback
;
1355 unmanagedCallbacks
.keyCB
= m_keypressCallback
;
1356 unmanagedCallbacks
.commandCB
= m_commandCallback
;
1357 unmanagedCallbacks
.configCB
= m_configCallback
;
1358 unmanagedCallbacks
.alertCB
= m_alertCallback
;
1359 unmanagedCallbacks
.menuCB
= m_menuCallback
;
1360 unmanagedCallbacks
.sourceActivatedCB
= m_sourceActivatedCallback
;
1362 PLATFORM::CLockObject
lock(g_callbackMutex
);
1363 g_unmanagedCallbacks
.push_back(unmanagedCallbacks
);
1364 m_iCallbackPtr
= g_unmanagedCallbacks
.size() - 1;
1365 m_bDelegatesCreated
= true;
1369 CecLogMessageManagedDelegate
^ m_logMessageDelegate
;
1370 static System::Runtime::InteropServices::GCHandle m_logMessageGCHandle
;
1371 LOGCB m_logMessageCallback
;
1373 CecKeyPressManagedDelegate
^ m_keypressDelegate
;
1374 static System::Runtime::InteropServices::GCHandle m_keypressGCHandle
;
1375 KEYCB m_keypressCallback
;
1377 CecCommandManagedDelegate
^ m_commandDelegate
;
1378 static System::Runtime::InteropServices::GCHandle m_commandGCHandle
;
1379 COMMANDCB m_commandCallback
;
1381 CecConfigManagedDelegate
^ m_configDelegate
;
1382 static System::Runtime::InteropServices::GCHandle m_configGCHandle
;
1383 CONFIGCB m_configCallback
;
1385 CecAlertManagedDelegate
^ m_alertDelegate
;
1386 static System::Runtime::InteropServices::GCHandle m_alertGCHandle
;
1387 ALERTCB m_alertCallback
;
1389 CecMenuManagedDelegate
^ m_menuDelegate
;
1390 static System::Runtime::InteropServices::GCHandle m_menuGCHandle
;
1391 MENUCB m_menuCallback
;
1393 CecSourceActivatedManagedDelegate
^ m_sourceActivatedDelegate
;
1394 static System::Runtime::InteropServices::GCHandle m_sourceActivatedGCHandle
;
1395 ACTICB m_sourceActivatedCallback
;
1397 CecCallbackMethods
^ m_callbacks
;
1398 bool m_bHasCallbacks
;
1399 bool m_bDelegatesCreated
;
1400 size_t m_iCallbackPtr
;