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"
45 /// The device type. For client applications, libCEC only supports RecordingDevice, PlaybackDevice or Tuner.
46 /// libCEC uses RecordingDevice by default.
48 public enum class CecDeviceType
59 /// Reserved / do not use
71 /// Audio system / AVR
77 /// Log level that can be used by the logging callback method to filter messages from libCEC.
79 public enum class CecLogLevel
86 /// libCEC encountered a serious problem, and couldn't complete an action.
90 /// libCEC warns that it encountered a problem, but recovered.
94 /// libCEC informs the client about a CEC state change.
98 /// Raw CEC data traffic
102 /// Debugging messages
106 /// Display all messages
112 /// A logical address on the CEC bus
114 public enum class CecLogicalAddress
117 /// Not a valid logical address
125 /// Recording device 1
127 RecordingDevice1
= 1,
129 /// Recording device 2
131 RecordingDevice2
= 2,
137 /// Playback device 1
141 /// Audio system / AVR
153 /// Playback device 2
157 /// Recording device 3
159 RecordingDevice3
= 9,
165 /// Playback device 3
167 PlaybackDevice3
= 11,
169 /// Reserved address 1
173 /// Reserved address 2
181 /// Unregistered / new device
185 /// Broadcast address
191 /// The type of alert when libCEC calls the CecAlert callback
193 public enum class CecAlert
196 /// The device needs servicing. This is set when the firmware can be upgraded, or when a problem with the firmware is detected.
197 /// The latest firmware flash tool can be downloaded from http://packages.pulse-eight.net/
203 /// The type of parameter that is sent with the CecAlert callback
205 public enum class CecParameterType
208 /// The parameter is a string
210 ParameterTypeString
= 1
214 /// A parameter for the CecAlert callback
216 public ref
class CecParameter
220 /// Create a new parameter
222 /// <param name="type">The type of this parameter.</param>
223 /// <param name="data">The value of this parameter.</param>
224 CecParameter(CecParameterType type
, System::String
^ data
)
231 /// The type of this parameter
233 property CecParameterType Type
;
235 /// The value of this parameter
237 property
System::String
^ Data
;
241 /// The power status of a CEC device
243 public enum class CecPowerStatus
254 /// In transition from standby to on
256 InTransitionStandbyToOn
= 0x02,
258 /// In transition from on to standby
260 InTransitionOnToStandby
= 0x03,
268 /// The CEC version of a CEC device
270 public enum class CecVersion
299 /// Parameter for OSD string display, that controls how to display the string
301 public enum class CecDisplayControl
304 /// Display for the default time
306 DisplayForDefaultTime
= 0x00,
308 /// Display until it is cleared by ClearPreviousMessage
310 DisplayUntilCleared
= 0x40,
312 /// Clear message displayed by DisplayUntilCleared
314 ClearPreviousMessage
= 0x80,
316 /// Reserved / do not use
318 ReservedForFutureUse
= 0xC0
322 /// The menu state of a CEC device
324 public enum class CecMenuState
337 /// Deck control mode for playback and recording devices
339 public enum class CecDeckControlMode
342 /// Skip forward / wind
346 /// Skip reverse / rewind
348 SkipReverseRewind
= 2,
360 /// Deck status for playback and recording devices
362 public enum class CecDeckInfo
377 /// Showing still frame
385 /// Playing slow reverse
397 /// No media detected
401 /// Stop / not playing
405 /// Skip forward / wind
407 SkipForwardWind
= 0x1B,
409 /// Skip reverse / rewind
411 SkipReverseRewind
= 0x1C,
413 /// Index search forward
415 IndexSearchForward
= 0x1D,
417 /// Index search reverse
419 IndexSearchReverse
= 0x1E,
421 /// Other / unknown status
427 /// User control code, the key code when the user presses or releases a button on the remote.
428 /// Used by SendKeypress() and the CecKeyPress() callback.
430 public enum class CecUserControlCode
453 /// Direction right + up
457 /// Direction right + down
461 /// Direction left + up
465 /// Direction left + down
555 PreviousChannel
= 0x32,
557 /// Select sound track
565 /// Display information
567 DisplayInformation
= 0x35,
581 /// Toggle powered on / standby
645 /// Toggle sub picture
649 /// Toggle video on demand
651 VideoOnDemand
= 0x52,
653 /// Toggle electronic program guide (EPG)
655 ElectronicProgramGuide
= 0x53,
657 /// Toggle timer programming
659 TimerProgramming
= 0x54,
661 /// Set initial configuration
663 InitialConfiguration
= 0x55,
665 /// Start playback function
669 /// Pause playback function
671 PausePlayFunction
= 0x61,
673 /// Toggle recording function
675 RecordFunction
= 0x62,
677 /// Pause recording function
679 PauseRecordFunction
= 0x63,
681 /// Stop playback function
685 /// Mute audio function
689 /// Restore volume function
691 RestoreVolumeFunction
= 0x66,
697 /// Select media function
699 SelectMediaFunction
= 0x68,
701 /// Select AV input function
703 SelectAVInputFunction
= 0x69,
705 /// Select audio input function
707 SelectAudioInputFunction
= 0x6A,
709 /// Toggle powered on / standby function
711 PowerToggleFunction
= 0x6B,
713 /// Power off function
715 PowerOffFunction
= 0x6C,
717 /// Power on function
719 PowerOnFunction
= 0x6D,
729 /// F3 / green button
733 /// F4 / yellow button
745 /// Max. valid key code for standard buttons
749 /// Extra return button on Samsung remotes
751 SamsungReturn
= 0x91,
753 /// Unknown / invalid key code
759 /// Vendor IDs for CEC devices
761 public enum class CecVendorId
765 Panasonic
= 0x008045,
783 /// Audio status of audio system / AVR devices
785 public enum class CecAudioStatus
790 MuteStatusMask
= 0x80,
792 /// Not muted, volume status mask
794 VolumeStatusMask
= 0x7F,
806 VolumeStatusUnknown
= 0x7F
810 /// CEC opcodes, as described in the HDMI CEC specification
812 public enum class CecOpcode
819 /// Image view on: power on display for image display
823 /// Text view on: power on display for text display
827 /// Device no longer is the active source
829 InactiveSource
= 0x9D,
831 /// Request which device has the active source status
833 RequestActiveSource
= 0x85,
835 /// Routing change for HDMI switches
837 RoutingChange
= 0x80,
839 /// Routing information for HDMI switches
841 RoutingInformation
= 0x81,
843 /// Change the stream path to the given physical address
845 SetStreamPath
= 0x86,
847 /// Inform that a device went into standby mode
859 /// Recording status information
863 /// Record current display
865 RecordTvScreen
= 0x0F,
867 /// Clear analogue timer
869 ClearAnalogueTimer
= 0x33,
871 /// Clear digital timer
873 ClearDigitalTimer
= 0x99,
875 /// Clear external timer
877 ClearExternalTimer
= 0xA1,
879 /// Set analogue timer
881 SetAnalogueTimer
= 0x34,
883 /// Set digital timer
885 SetDigitalTimer
= 0x97,
887 /// Set external timer
889 SetExternalTimer
= 0xA2,
891 /// Set program title of a timer
893 SetTimerProgramTitle
= 0x67,
895 /// Timer status cleared
897 TimerClearedStatus
= 0x43,
899 /// Timer status information
903 /// CEC version used by a device
907 /// Request CEC version of a device
909 GetCecVersion
= 0x9F,
911 /// Request physical address of a device
913 GivePhysicalAddress
= 0x83,
915 /// Request language code of the menu language of a device
917 GetMenuLanguage
= 0x91,
919 /// Report the physical address
921 ReportPhysicalAddress
= 0x84,
923 /// Report the language code of the menu language
925 SetMenuLanguage
= 0x32,
927 /// Deck control for playback and recording devices
931 /// Deck status for playback and recording devices
935 /// Request deck status from playback and recording devices
937 GiveDeckStatus
= 0x1A,
939 /// Start playback on playback and recording devices
943 /// Request tuner status
945 GiveTunerDeviceStatus
= 0x08,
947 /// Select analogue service on a tuner
949 SelectAnalogueService
= 0x92,
951 /// Select digital service on a tuner
953 SelectDigtalService
= 0x93,
955 /// Report tuner device status
957 TunerDeviceStatus
= 0x07,
959 /// Tuner step decrement
961 TunerStepDecrement
= 0x06,
963 /// Tuner step increment
965 TunerStepIncrement
= 0x05,
967 /// Report device vendor ID
969 DeviceVendorId
= 0x87,
971 /// Request device vendor ID
973 GiveDeviceVendorId
= 0x8C,
975 /// Vendor specific command
977 VendorCommand
= 0x89,
979 /// Vendor specific command with vendor ID
981 VendorCommandWithId
= 0xA0,
983 /// Vendor specific remote button pressed
985 VendorRemoteButtonDown
= 0x8A,
987 /// Vendor specific remote button released
989 VendorRemoteButtonUp
= 0x8B,
991 /// Display / clear OSD string
995 /// Request device OSD name
999 /// Report device OSD name
1003 /// Request device menu status
1007 /// Report device menu status
1011 /// Remote button pressed
1013 UserControlPressed
= 0x44,
1015 /// Remote button released
1017 UserControlRelease
= 0x45,
1019 /// Request device power status
1021 GiveDevicePowerStatus
= 0x8F,
1023 /// Report device power status
1025 ReportPowerStatus
= 0x90,
1027 /// Feature abort / unsupported command
1029 FeatureAbort
= 0x00,
1035 /// Give audio status
1037 GiveAudioStatus
= 0x71,
1039 /// Give audiosystem mode
1041 GiveSystemAudioMode
= 0x7D,
1043 /// Report device audio status
1045 ReportAudioStatus
= 0x7A,
1047 /// Set audiosystem mode
1049 SetSystemAudioMode
= 0x72,
1051 /// Request audiosystem mode
1053 SystemAudioModeRequest
= 0x70,
1055 /// Report audiosystem mode
1057 SystemAudioModeStatus
= 0x7E,
1059 /// Set audio bitrate
1061 SetAudioRate
= 0x9A,
1063 /// When this opcode is set, no opcode will be sent to the device / poll message
1064 /// This is one of the reserved numbers
1070 /// Audiosystem status
1072 public enum class CecSystemAudioStatus
1085 /// libCEC client application version
1087 public enum class CecClientVersion
1096 Version1_5_0
= 0x1500,
1100 Version1_5_1
= 0x1501,
1104 Version1_5_2
= 0x1502,
1108 Version1_5_3
= 0x1503,
1112 Version1_6_0
= 0x1600,
1116 Version1_6_1
= 0x1601,
1120 Version1_6_2
= 0x1602,
1124 Version1_6_3
= 0x1603,
1128 Version1_7_0
= 0x1700,
1132 Version1_7_1
= 0x1701,
1136 Version1_7_2
= 0x1702,
1140 Version1_8_0
= 0x1800,
1144 Version1_8_1
= 0x1801,
1148 Version1_8_2
= 0x1802,
1152 Version1_9_0
= 0x1900
1158 public enum class CecServerVersion
1167 Version1_5_0
= 0x1500,
1171 Version1_5_1
= 0x1501,
1175 Version1_5_2
= 0x1502,
1179 Version1_5_3
= 0x1503,
1183 Version1_6_0
= 0x1600,
1187 Version1_6_1
= 0x1601,
1191 Version1_6_2
= 0x1602,
1195 Version1_6_3
= 0x1603,
1199 Version1_7_0
= 0x1700,
1203 Version1_7_1
= 0x1701,
1207 Version1_7_2
= 0x1702,
1211 Version1_8_0
= 0x1800,
1215 Version1_8_1
= 0x1801,
1219 Version1_8_2
= 0x1802,
1223 Version1_9_0
= 0x1900
1227 /// Type of adapter to which libCEC is connected
1229 public enum class CecAdapterType
1232 /// Unknown adapter type
1236 /// Pulse-Eight USB-CEC adapter
1238 PulseEightExternal
= 0x1,
1240 /// Pulse-Eight CEC daughterboard
1242 PulseEightDaughterboard
= 0x2,
1250 /// Descriptor of a CEC adapter, returned when scanning for adapters that are connected to the system
1252 public ref
class CecAdapter
1256 /// Create a new CEC adapter descriptor
1258 /// <param name="path"> The path descriptor for this CEC adapter</param>
1259 /// <param name="comPort">The COM port of this CEC adapter</param>
1260 CecAdapter(System::String
^ path
, System::String
^ comPort
)
1267 /// The path descriptor for this CEC adapter
1269 property
System::String
^ Path
;
1272 /// The COM port of this CEC adapter
1274 property
System::String
^ ComPort
;
1278 /// A list of CEC device types
1280 public ref
class CecDeviceTypeList
1284 /// Create a new empty list of CEC device types
1286 CecDeviceTypeList(void)
1288 Types
= gcnew array
<CecDeviceType
>(5);
1289 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
1290 Types
[iPtr
] = CecDeviceType::Reserved
;
1294 /// The array with CecDeviceType instances in this list.
1296 property array
<CecDeviceType
> ^ Types
;
1300 /// A list of logical addresses
1302 public ref
class CecLogicalAddresses
1306 /// Create a new empty list of logical addresses
1308 CecLogicalAddresses(void)
1310 Addresses
= gcnew array
<CecLogicalAddress
>(16);
1315 /// Clears this list
1319 Primary
= CecLogicalAddress::Unknown
;
1320 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
1321 Addresses
[iPtr
] = CecLogicalAddress::Unknown
;
1325 /// Checks whether a logical address is set in this list.
1327 /// <param name="address">The address to check.</param>
1328 /// <returns>True when set, false otherwise</returns>
1329 bool IsSet(CecLogicalAddress address
)
1331 return Addresses
[(unsigned int)address
] != CecLogicalAddress::Unknown
;
1335 /// Add a logical address to this list (if it's not set already)
1337 /// <param name="address">The address to add.</param>
1338 void Set(CecLogicalAddress address
)
1340 Addresses
[(unsigned int)address
] = address
;
1341 if (Primary
== CecLogicalAddress::Unknown
)
1346 /// The primary (first) address in this list
1348 property CecLogicalAddress Primary
;
1351 /// The list of addresses
1353 property array
<CecLogicalAddress
> ^ Addresses
;
1358 /// Byte array used for CEC command parameters
1360 public ref
class CecDatapacket
1364 /// Create a new byte array with maximum size 100
1368 Data
= gcnew array
<uint8_t>(100);
1373 /// Adds a byte to this byte array
1375 /// <param name="data">The byte to add.</param>
1376 void PushBack(uint8_t data
)
1388 property array
<uint8_t> ^ Data
;
1391 /// Current data size
1393 property
uint8_t Size
;
1397 /// A CEC command that is received or transmitted over the CEC bus
1399 public ref
class CecCommand
1403 /// Create a new CEC command instance
1405 /// <param name="initiator">The initiator of the command</param>
1406 /// <param name="destination">The receiver of the command</param>
1407 /// <param name="ack">True when the ack bit is set, false otherwise</param>
1408 /// <param name="eom">True when the eom bit is set, false otherwise</param>
1409 /// <param name="opcode">The CEC opcode of this command</param>
1410 /// <param name="transmitTimeout">The timeout to use when transmitting a command</param>
1411 CecCommand(CecLogicalAddress initiator
, CecLogicalAddress destination
, bool ack
, bool eom
, CecOpcode opcode
, int32_t transmitTimeout
)
1413 Initiator
= initiator
;
1414 Destination
= destination
;
1419 TransmitTimeout
= transmitTimeout
;
1420 Parameters
= gcnew CecDatapacket
;
1425 /// Create a new empty CEC command instance
1429 Initiator
= CecLogicalAddress::Unknown
;
1430 Destination
= CecLogicalAddress::Unknown
;
1433 Opcode
= CecOpcode::None
;
1435 TransmitTimeout
= 0;
1436 Parameters
= gcnew CecDatapacket
;
1441 /// Pushes a byte of data to this CEC command
1443 /// <param name="data">The byte to add</param>
1444 void PushBack(uint8_t data
)
1446 if (Initiator
== CecLogicalAddress::Unknown
&& Destination
== CecLogicalAddress::Unknown
)
1448 Initiator
= (CecLogicalAddress
) (data
>> 4);
1449 Destination
= (CecLogicalAddress
) (data
& 0xF);
1451 else if (!OpcodeSet
)
1454 Opcode
= (CecOpcode
)data
;
1458 Parameters
->PushBack(data
);
1463 /// True when this command is empty, false otherwise.
1465 property
bool Empty
;
1467 /// The initiator of the command
1469 property CecLogicalAddress Initiator
;
1471 /// The destination of the command
1473 property CecLogicalAddress Destination
;
1475 /// True when the ack bit is set, false otherwise
1479 /// True when the eom bit is set, false otherwise
1483 /// The CEC opcode of the command
1485 property CecOpcode Opcode
;
1487 /// The parameters of this command
1489 property CecDatapacket
^ Parameters
;
1491 /// True when an opcode is set, false otherwise (poll message)
1493 property
bool OpcodeSet
;
1495 /// The timeout to use when transmitting a command
1497 property
int32_t TransmitTimeout
;
1501 /// A key press that was received
1503 public ref
class CecKeypress
1507 /// Create a new key press instance
1509 /// <param name="keycode">The key code of this key press</param>
1510 /// <param name="duration">The duration of this key press in milliseconds</param>
1511 CecKeypress(CecUserControlCode keycode
, unsigned int duration
)
1514 Duration
= duration
;
1519 /// Create a new empty key press instance
1523 Keycode
= CecUserControlCode::Unknown
;
1529 /// True when empty, false otherwise
1531 property
bool Empty
;
1533 /// The key code of this key press
1535 property CecUserControlCode Keycode
;
1537 /// The duration of this key press in milliseconds
1539 property
unsigned int Duration
;
1543 /// A log message that libCEC generated
1545 public ref
class CecLogMessage
1549 /// Create a new log message
1551 /// <param name="message">The actual message</param>
1552 /// <param name="level">The log level, so the application can choose what type information to display</param>
1553 /// <param name="time">The timestamp of this message, in milliseconds after connecting</param>
1554 CecLogMessage(System::String
^ message
, CecLogLevel level
, int64_t time
)
1563 /// Create a new empty log message
1568 Level
= CecLogLevel::None
;
1574 /// True when empty, false otherwise.
1576 property
bool Empty
;
1578 /// The actual message
1580 property
System::String
^Message
;
1582 /// The log level, so the application can choose what type information to display
1584 property CecLogLevel Level
;
1586 /// The timestamp of this message, in milliseconds after connecting
1588 property
int64_t Time
;
1591 ref
class CecCallbackMethods
; //forward declaration
1594 /// The configuration that libCEC uses.
1596 public ref
class LibCECConfiguration
1600 /// Create a new configuration instance with default settings.
1602 LibCECConfiguration(void)
1605 DeviceTypes
= gcnew
CecDeviceTypeList();
1606 AutodetectAddress
= true;
1607 PhysicalAddress
= CEC_DEFAULT_PHYSICAL_ADDRESS
;
1608 BaseDevice
= (CecLogicalAddress
)CEC_DEFAULT_BASE_DEVICE
;
1609 HDMIPort
= CEC_DEFAULT_HDMI_PORT
;
1610 ClientVersion
= CecClientVersion::VersionPre1_5
;
1611 ServerVersion
= CecServerVersion::VersionPre1_5
;
1612 TvVendor
= CecVendorId::Unknown
;
1614 GetSettingsFromROM
= false;
1615 UseTVMenuLanguage
= CEC_DEFAULT_SETTING_USE_TV_MENU_LANGUAGE
== 1;
1616 ActivateSource
= CEC_DEFAULT_SETTING_ACTIVATE_SOURCE
== 1;
1618 WakeDevices
= gcnew
CecLogicalAddresses();
1619 if (CEC_DEFAULT_SETTING_ACTIVATE_SOURCE
== 1)
1620 WakeDevices
->Set(CecLogicalAddress::Tv
);
1622 PowerOffDevices
= gcnew
CecLogicalAddresses();
1623 if (CEC_DEFAULT_SETTING_POWER_OFF_SHUTDOWN
== 1)
1624 PowerOffDevices
->Set(CecLogicalAddress::Broadcast
);
1626 PowerOffScreensaver
= CEC_DEFAULT_SETTING_POWER_OFF_SCREENSAVER
== 1;
1627 PowerOffOnStandby
= CEC_DEFAULT_SETTING_POWER_OFF_ON_STANDBY
== 1;
1629 SendInactiveSource
= CEC_DEFAULT_SETTING_SEND_INACTIVE_SOURCE
== 1;
1630 LogicalAddresses
= gcnew
CecLogicalAddresses();
1631 FirmwareVersion
= 1;
1632 PowerOffDevicesOnStandby
= CEC_DEFAULT_SETTING_POWER_OFF_DEVICES_STANDBY
== 1;
1633 ShutdownOnStandby
= CEC_DEFAULT_SETTING_SHUTDOWN_ON_STANDBY
== 1;
1634 DeviceLanguage
= "";
1635 FirmwareBuildDate
= gcnew
System::DateTime(1970,1,1,0,0,0,0);
1636 CECVersion
= (CecVersion
)CEC_DEFAULT_SETTING_CEC_VERSION
;
1637 AdapterType
= CecAdapterType::Unknown
;
1641 /// Change the callback method pointers in this configuration instance.
1643 /// <param name="callbacks">The new callbacks</param>
1644 void SetCallbacks(CecCallbackMethods
^callbacks
)
1646 Callbacks
= callbacks
;
1650 /// Update this configuration with data received from libCEC
1652 /// <param name="config">The configuration that was received from libCEC</param>
1653 void Update(const CEC::libcec_configuration
&config
)
1655 DeviceName
= gcnew
System::String(config
.strDeviceName
);
1657 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
1658 DeviceTypes
->Types
[iPtr
] = (CecDeviceType
)config
.deviceTypes
.types
[iPtr
];
1660 AutodetectAddress
= config
.bAutodetectAddress
== 1;
1661 PhysicalAddress
= config
.iPhysicalAddress
;
1662 BaseDevice
= (CecLogicalAddress
)config
.baseDevice
;
1663 HDMIPort
= config
.iHDMIPort
;
1664 ClientVersion
= (CecClientVersion
)config
.clientVersion
;
1665 ServerVersion
= (CecServerVersion
)config
.serverVersion
;
1666 TvVendor
= (CecVendorId
)config
.tvVendor
;
1668 // player specific settings
1669 GetSettingsFromROM
= config
.bGetSettingsFromROM
== 1;
1670 UseTVMenuLanguage
= config
.bUseTVMenuLanguage
== 1;
1671 ActivateSource
= config
.bActivateSource
== 1;
1673 WakeDevices
->Clear();
1674 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
1675 if (config
.wakeDevices
[iPtr
])
1676 WakeDevices
->Set((CecLogicalAddress
)iPtr
);
1678 PowerOffDevices
->Clear();
1679 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
1680 if (config
.powerOffDevices
[iPtr
])
1681 PowerOffDevices
->Set((CecLogicalAddress
)iPtr
);
1683 PowerOffScreensaver
= config
.bPowerOffScreensaver
== 1;
1684 PowerOffOnStandby
= config
.bPowerOffOnStandby
== 1;
1686 if (ServerVersion
>= CecServerVersion::Version1_5_1
)
1687 SendInactiveSource
= config
.bSendInactiveSource
== 1;
1689 if (ServerVersion
>= CecServerVersion::Version1_5_3
)
1691 LogicalAddresses
->Clear();
1692 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
1693 if (config
.logicalAddresses
[iPtr
])
1694 LogicalAddresses
->Set((CecLogicalAddress
)iPtr
);
1697 if (ServerVersion
>= CecServerVersion::Version1_6_0
)
1699 FirmwareVersion
= config
.iFirmwareVersion
;
1700 PowerOffDevicesOnStandby
= config
.bPowerOffDevicesOnStandby
== 1;
1701 ShutdownOnStandby
= config
.bShutdownOnStandby
== 1;
1704 if (ServerVersion
>= CecServerVersion::Version1_6_2
)
1706 DeviceLanguage
= gcnew
System::String(config
.strDeviceLanguage
);
1707 FirmwareBuildDate
= gcnew
System::DateTime(1970,1,1,0,0,0,0);
1708 FirmwareBuildDate
= FirmwareBuildDate
->AddSeconds(config
.iFirmwareBuildDate
);
1711 if (ServerVersion
>= CecServerVersion::Version1_6_3
)
1712 MonitorOnlyClient
= config
.bMonitorOnly
== 1;
1714 if (ServerVersion
>= CecServerVersion::Version1_8_0
)
1715 CECVersion
= (CecVersion
)config
.cecVersion
;
1717 if (ServerVersion
>= CecServerVersion::Version1_8_2
)
1718 AdapterType
= (CecAdapterType
)config
.adapterType
;
1722 /// The device name to use on the CEC bus
1724 property
System::String
^ DeviceName
;
1727 /// The device type(s) to use on the CEC bus for libCEC
1729 property CecDeviceTypeList
^ DeviceTypes
;
1732 /// True to try to autodetect the physical address, false otherwise
1734 property
bool AutodetectAddress
;
1737 /// The physical address that libCEC uses on the CEC bus
1739 property
uint16_t PhysicalAddress
;
1742 /// The logical address of the device to which the CEC adapter is connected, only used when PhysicalAddress isn't set
1744 property CecLogicalAddress BaseDevice
;
1747 /// The hdmi port number on the device to which the CEC adapter is connected, only used when PhysicalAddress isn't set
1749 property
uint8_t HDMIPort
;
1752 /// The client API version to use
1754 property CecClientVersion ClientVersion
;
1757 /// The version of libCEC
1759 property CecServerVersion ServerVersion
;
1762 /// Override the vendor ID of the TV when set (for quirks mode)
1764 property CecVendorId TvVendor
;
1767 /// True to read the settings from the EEPROM, which possibly override the settings passed here
1769 property
bool GetSettingsFromROM
;
1772 /// Use the language setting of the TV in the client application. Must be implemented by the client application.
1774 property
bool UseTVMenuLanguage
;
1777 /// Make libCEC the active source when starting the client application
1779 property
bool ActivateSource
;
1782 /// List of devices to send a power on command to when starting the client application
1784 property CecLogicalAddresses
^WakeDevices
;
1787 /// List of devices to send a standby command to when exiting the client application
1789 property CecLogicalAddresses
^PowerOffDevices
;
1792 /// Send standby commands when the client application activates the screensaver. Must be implemented by the client application.
1794 property
bool PowerOffScreensaver
;
1797 /// Power off the PC when the TV powers off. Must be implemented by the client application.
1799 property
bool PowerOffOnStandby
;
1802 /// Send an inactive source message when exiting the client application.
1804 property
bool SendInactiveSource
;
1807 /// The list of logical addresses that libCEC is using
1809 property CecLogicalAddresses
^LogicalAddresses
;
1812 /// The firmware version of the adapter to which libCEC is connected
1814 property
uint16_t FirmwareVersion
;
1817 /// Send standby commands when the client application activates standby mode (S3). Must be implemented by the client application.
1819 property
bool PowerOffDevicesOnStandby
;
1820 property
bool ShutdownOnStandby
;
1823 /// True to start a monitor-only client, false to start a standard client.
1825 property
bool MonitorOnlyClient
;
1828 /// The language code of the menu language that libCEC reports to other devices.
1830 property
System::String
^ DeviceLanguage
;
1833 /// The callback methods to use.
1835 property CecCallbackMethods
^ Callbacks
;
1838 /// The build date of the firmware.
1840 property
System::DateTime
^ FirmwareBuildDate
;
1843 /// The CEC version that libCEC uses.
1845 property CecVersion CECVersion
;
1848 /// The type of adapter that libCEC is connected to.
1850 property CecAdapterType AdapterType
;
1853 // the callback methods are called by unmanaged code, so we need some delegates for this
1855 // unmanaged callback methods
1856 typedef int (__stdcall
*LOGCB
) (const CEC::cec_log_message
&message
);
1857 typedef int (__stdcall
*KEYCB
) (const CEC::cec_keypress
&key
);
1858 typedef int (__stdcall
*COMMANDCB
)(const CEC::cec_command
&command
);
1859 typedef int (__stdcall
*CONFIGCB
) (const CEC::libcec_configuration
&config
);
1860 typedef int (__stdcall
*ALERTCB
) (const CEC::libcec_alert
, const CEC::libcec_parameter
&data
);
1861 typedef int (__stdcall
*MENUCB
) (const CEC::cec_menu_state newVal
);
1862 typedef void (__stdcall
*ACTICB
) (const CEC::cec_logical_address logicalAddress
, const uint8_t bActivated
);
1865 /// libCEC callback methods. Unmanaged code.
1870 /// Log message callback
1874 /// Key press/release callback
1878 /// Raw CEC data callback
1880 COMMANDCB commandCB
;
1882 /// Updated configuration callback
1886 /// Alert message callback
1890 /// Menu status change callback
1894 /// Source (de)activated callback
1896 ACTICB sourceActivatedCB
;
1897 } UnmanagedCecCallbacks
;
1899 static PLATFORM::CMutex g_callbackMutex
;
1900 static std::vector
<UnmanagedCecCallbacks
> g_unmanagedCallbacks
;
1901 static CEC::ICECCallbacks g_cecCallbacks
;
1904 /// Called by libCEC to send back a log message to the application
1906 /// <param name="cbParam">Pointer to the callback struct</param>
1907 /// <param name="message">The log message</param>
1908 /// <return>1 when handled, 0 otherwise</return>
1909 int CecLogMessageCB(void *cbParam
, const CEC::cec_log_message
&message
)
1913 size_t iPtr
= (size_t)cbParam
;
1914 PLATFORM::CLockObject
lock(g_callbackMutex
);
1915 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1916 return g_unmanagedCallbacks
[iPtr
].logCB(message
);
1922 /// Called by libCEC to send back a key press or release to the application
1924 /// <param name="cbParam">Pointer to the callback struct</param>
1925 /// <param name="key">The key press command that libCEC received</param>
1926 /// <return>1 when handled, 0 otherwise</return>
1927 int CecKeyPressCB(void *cbParam
, const CEC::cec_keypress
&key
)
1931 size_t iPtr
= (size_t)cbParam
;
1932 PLATFORM::CLockObject
lock(g_callbackMutex
);
1933 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1934 return g_unmanagedCallbacks
[iPtr
].keyCB(key
);
1940 /// Called by libCEC to send back raw CEC data to the application
1942 /// <param name="cbParam">Pointer to the callback struct</param>
1943 /// <param name="command">The raw CEC data</param>
1944 /// <return>1 when handled, 0 otherwise</return>
1945 int CecCommandCB(void *cbParam
, const CEC::cec_command
&command
)
1949 size_t iPtr
= (size_t)cbParam
;
1950 PLATFORM::CLockObject
lock(g_callbackMutex
);
1951 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1952 return g_unmanagedCallbacks
[iPtr
].commandCB(command
);
1958 /// Called by libCEC to send back an updated configuration to the application
1960 /// <param name="cbParam">Pointer to the callback struct</param>
1961 /// <param name="config">The new configuration</param>
1962 /// <return>1 when handled, 0 otherwise</return>
1963 int CecConfigCB(void *cbParam
, const CEC::libcec_configuration
&config
)
1967 size_t iPtr
= (size_t)cbParam
;
1968 PLATFORM::CLockObject
lock(g_callbackMutex
);
1969 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1970 return g_unmanagedCallbacks
[iPtr
].configCB(config
);
1976 /// Called by libCEC to send back an alert message to the application
1978 /// <param name="cbParam">Pointer to the callback struct</param>
1979 /// <param name="data">The alert message</param>
1980 /// <return>1 when handled, 0 otherwise</return>
1981 int CecAlertCB(void *cbParam
, const CEC::libcec_alert alert
, const CEC::libcec_parameter
&data
)
1985 size_t iPtr
= (size_t)cbParam
;
1986 PLATFORM::CLockObject
lock(g_callbackMutex
);
1987 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
1988 return g_unmanagedCallbacks
[iPtr
].alertCB(alert
, data
);
1994 /// Called by libCEC to send back a menu state change to the application
1996 /// <param name="cbParam">Pointer to the callback struct</param>
1997 /// <param name="newVal">The new menu state</param>
1998 /// <return>1 when handled, 0 otherwise</return>
1999 int CecMenuCB(void *cbParam
, const CEC::cec_menu_state newVal
)
2003 size_t iPtr
= (size_t)cbParam
;
2004 PLATFORM::CLockObject
lock(g_callbackMutex
);
2005 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
2006 return g_unmanagedCallbacks
[iPtr
].menuCB(newVal
);
2012 /// Called by libCEC to notify the application that the source that is handled by libCEC was (de)activated
2014 /// <param name="cbParam">Pointer to the callback struct</param>
2015 /// <param name="logicalAddress">The logical address that was (de)activated</param>
2016 /// <param name="activated">True when activated, false when deactivated</param>
2017 void CecSourceActivatedCB(void *cbParam
, const CEC::cec_logical_address logicalAddress
, const uint8_t activated
)
2021 size_t iPtr
= (size_t)cbParam
;
2022 PLATFORM::CLockObject
lock(g_callbackMutex
);
2023 if (iPtr
>= 0 && iPtr
< g_unmanagedCallbacks
.size())
2024 g_unmanagedCallbacks
[iPtr
].sourceActivatedCB(logicalAddress
, activated
);
2029 // delegates for the unmanaged callback methods
2030 public delegate
int CecLogMessageManagedDelegate(const CEC::cec_log_message
&);
2031 public delegate
int CecKeyPressManagedDelegate(const CEC::cec_keypress
&);
2032 public delegate
int CecCommandManagedDelegate(const CEC::cec_command
&);
2033 public delegate
int CecConfigManagedDelegate(const CEC::libcec_configuration
&);
2034 public delegate
int CecAlertManagedDelegate(const CEC::libcec_alert
, const CEC::libcec_parameter
&);
2035 public delegate
int CecMenuManagedDelegate(const CEC::cec_menu_state newVal
);
2036 public delegate
void CecSourceActivatedManagedDelegate(const CEC::cec_logical_address logicalAddress
, const uint8_t bActivated
);
2039 /// Assign the callback methods in the g_cecCallbacks struct
2041 void AssignCallbacks()
2043 g_cecCallbacks
.CBCecLogMessage
= CecLogMessageCB
;
2044 g_cecCallbacks
.CBCecKeyPress
= CecKeyPressCB
;
2045 g_cecCallbacks
.CBCecCommand
= CecCommandCB
;
2046 g_cecCallbacks
.CBCecConfigurationChanged
= CecConfigCB
;
2047 g_cecCallbacks
.CBCecAlert
= CecAlertCB
;
2048 g_cecCallbacks
.CBCecMenuStateChanged
= CecMenuCB
;
2049 g_cecCallbacks
.CBCecSourceActivated
= CecSourceActivatedCB
;
2053 /// The callback methods that libCEC uses
2055 public ref
class CecCallbackMethods
2058 CecCallbackMethods(void)
2060 m_iCallbackPtr
= -1;
2062 m_bHasCallbacks
= false;
2063 m_bDelegatesCreated
= false;
2066 ~CecCallbackMethods(void)
2072 /// Pointer to the callbacks struct entry
2074 size_t GetCallbackPtr(void)
2076 PLATFORM::CLockObject
lock(g_callbackMutex
);
2077 return m_iCallbackPtr
;
2081 !CecCallbackMethods(void)
2088 /// Disable callback methods
2090 virtual void DisableCallbacks(void)
2096 /// Enable callback methods
2098 /// <param name="callbacks">Callback methods to activate</param>
2099 /// <return>true when handled, false otherwise</return>
2100 virtual bool EnableCallbacks(CecCallbackMethods
^ callbacks
)
2103 if (!m_bHasCallbacks
)
2105 m_bHasCallbacks
= true;
2106 m_callbacks
= callbacks
;
2114 /// Called by libCEC to send back a log message to the application.
2115 /// Override in the application to handle this callback.
2117 /// <param name="message">The log message</param>
2118 /// <return>1 when handled, 0 otherwise</return>
2119 virtual int ReceiveLogMessage(CecLogMessage
^ message
)
2125 /// Called by libCEC to send back a key press or release to the application.
2126 /// Override in the application to handle this callback.
2128 /// <param name="key">The key press command that libCEC received</param>
2129 /// <return>1 when handled, 0 otherwise</return>
2130 virtual int ReceiveKeypress(CecKeypress
^ key
)
2136 /// Called by libCEC to send back raw CEC data to the application.
2137 /// Override in the application to handle this callback.
2139 /// <param name="command">The raw CEC data</param>
2140 /// <return>1 when handled, 0 otherwise</return>
2141 virtual int ReceiveCommand(CecCommand
^ command
)
2147 /// Called by libCEC to send back an updated configuration to the application.
2148 /// Override in the application to handle this callback.
2150 /// <param name="config">The new configuration</param>
2151 /// <return>1 when handled, 0 otherwise</return>
2152 virtual int ConfigurationChanged(LibCECConfiguration
^ config
)
2158 /// Called by libCEC to send back an alert message to the application.
2159 /// Override in the application to handle this callback.
2161 /// <param name="data">The alert message</param>
2162 /// <return>1 when handled, 0 otherwise</return>
2163 virtual int ReceiveAlert(CecAlert alert
, CecParameter
^ data
)
2169 /// Called by libCEC to send back a menu state change to the application.
2170 /// Override in the application to handle this callback.
2172 /// <param name="newVal">The new menu state</param>
2173 /// <return>1 when handled, 0 otherwise</return>
2174 virtual int ReceiveMenuStateChange(CecMenuState newVal
)
2180 /// Called by libCEC to notify the application that the source that is handled by libCEC was (de)activated.
2181 /// Override in the application to handle this callback.
2183 /// <param name="logicalAddress">The logical address that was (de)activated</param>
2184 /// <param name="activated">True when activated, false when deactivated</param>
2185 virtual void SourceActivated(CecLogicalAddress logicalAddress
, bool activated
)
2190 // managed callback methods
2191 int CecLogMessageManaged(const CEC::cec_log_message
&message
)
2194 if (m_bHasCallbacks
)
2195 iReturn
= m_callbacks
->ReceiveLogMessage(gcnew
CecLogMessage(gcnew
System::String(message
.message
), (CecLogLevel
)message
.level
, message
.time
));
2199 int CecKeyPressManaged(const CEC::cec_keypress
&key
)
2202 if (m_bHasCallbacks
)
2203 iReturn
= m_callbacks
->ReceiveKeypress(gcnew
CecKeypress((CecUserControlCode
)key
.keycode
, key
.duration
));
2207 int CecCommandManaged(const CEC::cec_command
&command
)
2210 if (m_bHasCallbacks
)
2212 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
);
2213 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
2214 newCommand
->Parameters
->PushBack(command
.parameters
[iPtr
]);
2215 iReturn
= m_callbacks
->ReceiveCommand(newCommand
);
2220 int CecConfigManaged(const CEC::libcec_configuration
&config
)
2223 if (m_bHasCallbacks
)
2225 LibCECConfiguration
^netConfig
= gcnew
LibCECConfiguration();
2226 netConfig
->Update(config
);
2227 iReturn
= m_callbacks
->ConfigurationChanged(netConfig
);
2232 int CecAlertManaged(const CEC::libcec_alert alert
, const CEC::libcec_parameter
&data
)
2235 if (m_bHasCallbacks
)
2237 CecParameterType newType
= (CecParameterType
)data
.paramType
;
2238 if (newType
== CecParameterType::ParameterTypeString
)
2240 System::String
^ newData
= gcnew
System::String((const char *)data
.paramData
, 0, 128);
2241 CecParameter
^ newParam
= gcnew
CecParameter(newType
, newData
);
2242 iReturn
= m_callbacks
->ReceiveAlert((CecAlert
)alert
, newParam
);
2248 int CecMenuManaged(const CEC::cec_menu_state newVal
)
2251 if (m_bHasCallbacks
)
2253 iReturn
= m_callbacks
->ReceiveMenuStateChange((CecMenuState
)newVal
);
2258 void CecSourceActivatedManaged(const CEC::cec_logical_address logicalAddress
, const uint8_t bActivated
)
2260 if (m_bHasCallbacks
)
2261 m_callbacks
->SourceActivated((CecLogicalAddress
)logicalAddress
, bActivated
== 1);
2264 void DestroyDelegates()
2266 m_bHasCallbacks
= false;
2267 if (m_bDelegatesCreated
)
2269 m_bDelegatesCreated
= false;
2270 m_logMessageGCHandle
.Free();
2271 m_keypressGCHandle
.Free();
2272 m_commandGCHandle
.Free();
2273 m_alertGCHandle
.Free();
2274 m_menuGCHandle
.Free();
2275 m_sourceActivatedGCHandle
.Free();
2279 void CreateDelegates()
2283 if (!m_bDelegatesCreated
)
2285 msclr::interop::marshal_context
^ context
= gcnew
msclr::interop::marshal_context();
2287 // create the delegate method for the log message callback
2288 m_logMessageDelegate
= gcnew
CecLogMessageManagedDelegate(this, &CecCallbackMethods::CecLogMessageManaged
);
2289 m_logMessageGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_logMessageDelegate
);
2290 m_logMessageCallback
= static_cast<LOGCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_logMessageDelegate
).ToPointer());
2292 // create the delegate method for the keypress callback
2293 m_keypressDelegate
= gcnew
CecKeyPressManagedDelegate(this, &CecCallbackMethods::CecKeyPressManaged
);
2294 m_keypressGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_keypressDelegate
);
2295 m_keypressCallback
= static_cast<KEYCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_keypressDelegate
).ToPointer());
2297 // create the delegate method for the command callback
2298 m_commandDelegate
= gcnew
CecCommandManagedDelegate(this, &CecCallbackMethods::CecCommandManaged
);
2299 m_commandGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_commandDelegate
);
2300 m_commandCallback
= static_cast<COMMANDCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_commandDelegate
).ToPointer());
2302 // create the delegate method for the configuration change callback
2303 m_configDelegate
= gcnew
CecConfigManagedDelegate(this, &CecCallbackMethods::CecConfigManaged
);
2304 m_configGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_configDelegate
);
2305 m_configCallback
= static_cast<CONFIGCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_configDelegate
).ToPointer());
2307 // create the delegate method for the alert callback
2308 m_alertDelegate
= gcnew
CecAlertManagedDelegate(this, &CecCallbackMethods::CecAlertManaged
);
2309 m_alertGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_alertDelegate
);
2310 m_alertCallback
= static_cast<ALERTCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_alertDelegate
).ToPointer());
2312 // create the delegate method for the menu callback
2313 m_menuDelegate
= gcnew
CecMenuManagedDelegate(this, &CecCallbackMethods::CecMenuManaged
);
2314 m_menuGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_menuDelegate
);
2315 m_menuCallback
= static_cast<MENUCB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_menuDelegate
).ToPointer());
2317 // create the delegate method for the source activated callback
2318 m_sourceActivatedDelegate
= gcnew
CecSourceActivatedManagedDelegate(this, &CecCallbackMethods::CecSourceActivatedManaged
);
2319 m_sourceActivatedGCHandle
= System::Runtime::InteropServices::GCHandle::Alloc(m_sourceActivatedDelegate
);
2320 m_sourceActivatedCallback
= static_cast<ACTICB
>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_sourceActivatedDelegate
).ToPointer());
2324 UnmanagedCecCallbacks unmanagedCallbacks
;
2325 unmanagedCallbacks
.logCB
= m_logMessageCallback
;
2326 unmanagedCallbacks
.keyCB
= m_keypressCallback
;
2327 unmanagedCallbacks
.commandCB
= m_commandCallback
;
2328 unmanagedCallbacks
.configCB
= m_configCallback
;
2329 unmanagedCallbacks
.alertCB
= m_alertCallback
;
2330 unmanagedCallbacks
.menuCB
= m_menuCallback
;
2331 unmanagedCallbacks
.sourceActivatedCB
= m_sourceActivatedCallback
;
2333 PLATFORM::CLockObject
lock(g_callbackMutex
);
2334 g_unmanagedCallbacks
.push_back(unmanagedCallbacks
);
2335 m_iCallbackPtr
= g_unmanagedCallbacks
.size() - 1;
2336 m_bDelegatesCreated
= true;
2340 CecLogMessageManagedDelegate
^ m_logMessageDelegate
;
2341 static System::Runtime::InteropServices::GCHandle m_logMessageGCHandle
;
2342 LOGCB m_logMessageCallback
;
2344 CecKeyPressManagedDelegate
^ m_keypressDelegate
;
2345 static System::Runtime::InteropServices::GCHandle m_keypressGCHandle
;
2346 KEYCB m_keypressCallback
;
2348 CecCommandManagedDelegate
^ m_commandDelegate
;
2349 static System::Runtime::InteropServices::GCHandle m_commandGCHandle
;
2350 COMMANDCB m_commandCallback
;
2352 CecConfigManagedDelegate
^ m_configDelegate
;
2353 static System::Runtime::InteropServices::GCHandle m_configGCHandle
;
2354 CONFIGCB m_configCallback
;
2356 CecAlertManagedDelegate
^ m_alertDelegate
;
2357 static System::Runtime::InteropServices::GCHandle m_alertGCHandle
;
2358 ALERTCB m_alertCallback
;
2360 CecMenuManagedDelegate
^ m_menuDelegate
;
2361 static System::Runtime::InteropServices::GCHandle m_menuGCHandle
;
2362 MENUCB m_menuCallback
;
2364 CecSourceActivatedManagedDelegate
^ m_sourceActivatedDelegate
;
2365 static System::Runtime::InteropServices::GCHandle m_sourceActivatedGCHandle
;
2366 ACTICB m_sourceActivatedCallback
;
2368 CecCallbackMethods
^ m_callbacks
;
2369 bool m_bHasCallbacks
;
2370 bool m_bDelegatesCreated
;
2371 size_t m_iCallbackPtr
;