2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
36 #include <msclr/marshal.h>
40 using namespace System
;
42 using namespace msclr::interop
;
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 CecPowerStatus
91 InTransitionStandbyToOn
= 0x02,
92 InTransitionOnToStandby
= 0x03,
96 public enum class CecVersion
106 public enum class CecDisplayControl
108 DisplayForDefaultTime
= 0x00,
109 DisplayUntilCleared
= 0x40,
110 ClearPreviousMessage
= 0x80,
111 ReservedForFutureUse
= 0xC0
114 public enum class CecMenuState
120 public enum class CecDeckControlMode
123 SkipReverseRewind
= 2,
128 public enum class CecDeckInfo
140 SkipForwardWind
= 0x1B,
141 SkipReverseRewind
= 0x1C,
142 IndexSearchForward
= 0x1D,
143 IndexSearchReverse
= 0x1E,
147 public enum class CecUserControlCode
179 PreviousChannel
= 0x32,
182 DisplayInformation
= 0x35,
203 VideoOnDemand
= 0x52,
204 ElectronicProgramGuide
= 0x53,
205 TimerProgramming
= 0x54,
206 InitialConfiguration
= 0x55,
208 PausePlayFunction
= 0x61,
209 RecordFunction
= 0x62,
210 PauseRecordFunction
= 0x63,
213 RestoreVolumeFunction
= 0x66,
215 SelectMediaFunction
= 0x68,
216 SelectAVInputFunction
= 0x69,
217 SelectAudioInputFunction
= 0x6A,
218 PowerToggleFunction
= 0x6B,
219 PowerOffFunction
= 0x6C,
220 PowerOnFunction
= 0x6D,
231 public ref
class CecAdapter
234 CecAdapter(String
^ strPath
, String
^ strComPort
)
237 ComPort
= strComPort
;
240 property String
^ Path
;
241 property String
^ ComPort
;
244 public ref
class CecDeviceTypeList
247 CecDeviceTypeList(void)
249 Types
= gcnew array
<CecDeviceType
>(5);
250 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
251 Types
[iPtr
] = CecDeviceType::Reserved
;
254 property array
<CecDeviceType
> ^ Types
;
257 public ref
class CecLogicalAddresses
260 CecLogicalAddresses(void)
262 Addresses
= gcnew array
<CecLogicalAddress
>(16);
263 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
264 Addresses
[iPtr
] = CecLogicalAddress::Unregistered
;
267 property array
<CecLogicalAddress
> ^ Addresses
;
270 public ref
class CecDatapacket
275 Data
= gcnew array
<uint8_t>(100);
279 void PushBack(uint8_t data
)
288 property array
<uint8_t> ^ Data
;
289 property
uint8_t Size
;
292 public ref
class CecCommand
295 CecCommand(CecLogicalAddress iInitiator
, CecLogicalAddress iDestination
, bool bAck
, bool bEom
, int8_t iOpcode
, int32_t iTransmitTimeout
)
297 Initiator
= iInitiator
;
298 Destination
= iDestination
;
303 TransmitTimeout
= iTransmitTimeout
;
304 Parameters
= gcnew CecDatapacket
;
310 Initiator
= CecLogicalAddress::Unknown
;
311 Destination
= CecLogicalAddress::Unknown
;
317 Parameters
= gcnew CecDatapacket
;
321 void PushBack(uint8_t data
)
323 if (Initiator
== CecLogicalAddress::Unknown
&& Destination
== CecLogicalAddress::Unknown
)
325 Initiator
= (CecLogicalAddress
) (data
>> 4);
326 Destination
= (CecLogicalAddress
) (data
& 0xF);
335 Parameters
->PushBack(data
);
340 property CecLogicalAddress Initiator
;
341 property CecLogicalAddress Destination
;
344 property
int8_t Opcode
;
345 property CecDatapacket
^ Parameters
;
346 property
bool OpcodeSet
;
347 property
int32_t TransmitTimeout
;
350 public ref
class CecKeypress
353 CecKeypress(int iKeycode
, unsigned int iDuration
)
356 Duration
= iDuration
;
368 property
int Keycode
;
369 property
unsigned int Duration
;
372 public ref
class CecLogMessage
375 CecLogMessage(String
^ strMessage
, CecLogLevel iLevel
, int64_t iTime
)
377 Message
= strMessage
;
386 Level
= CecLogLevel::None
;
392 property String
^ Message
;
393 property CecLogLevel Level
;
394 property
int64_t Time
;
397 public ref
class LibCecSharp
400 LibCecSharp(String
^ strDeviceName
, CecDeviceTypeList
^ deviceTypes
)
402 marshal_context
^ context
= gcnew
marshal_context();
404 const char* strDeviceNameC
= context
->marshal_as
<const char*>(strDeviceName
);
406 cec_device_type_list types
;
407 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
408 types
.types
[iPtr
] = (cec_device_type
)deviceTypes
->Types
[iPtr
];
409 m_libCec
= (ICECAdapter
*) CECInit(strDeviceNameC
, types
);
415 CECDestroy(m_libCec
);
422 CECDestroy(m_libCec
);
427 array
<CecAdapter
^> ^ FindAdapters(String
^ path
)
429 cec_adapter
*devices
= new cec_adapter
[10];
431 marshal_context
^ context
= gcnew
marshal_context();
432 const char* strPathC
= path
->Length
> 0 ? context
->marshal_as
<const char*>(path
) : NULL
;
434 uint8_t iDevicesFound
= m_libCec
->FindAdapters(devices
, 10, NULL
);
436 array
<CecAdapter
^> ^ adapters
= gcnew array
<CecAdapter
^>(iDevicesFound
);
437 for (unsigned int iPtr
= 0; iPtr
< iDevicesFound
; iPtr
++)
438 adapters
[iPtr
] = gcnew
CecAdapter(gcnew
String(devices
[iPtr
].path
), gcnew
String(devices
[iPtr
].comm
));
445 bool Open(String
^ strPort
, int iTimeoutMs
)
447 marshal_context
^ context
= gcnew
marshal_context();
448 const char* strPortC
= context
->marshal_as
<const char*>(strPort
);
449 bool bReturn
= m_libCec
->Open(strPortC
, iTimeoutMs
);
459 bool PingAdapter(void)
461 return m_libCec
->PingAdapter();
464 bool StartBootloader(void)
466 return m_libCec
->StartBootloader();
469 int GetMinLibVersion(void)
471 return m_libCec
->GetMinLibVersion();
474 int GetLibVersionMajor(void)
476 return m_libCec
->GetLibVersionMajor();
479 int GetLibVersionMinor(void)
481 return m_libCec
->GetLibVersionMinor();
484 CecLogMessage
^ GetNextLogMessage(void)
487 if (m_libCec
->GetNextLogMessage(&msg
))
489 return gcnew
CecLogMessage(gcnew
String(msg
.message
), (CecLogLevel
)msg
.level
, msg
.time
);
492 return gcnew
CecLogMessage();
495 CecKeypress
^ GetNextKeypress(void)
498 if (m_libCec
->GetNextKeypress(&key
))
500 return gcnew
CecKeypress(key
.keycode
, key
.duration
);
503 return gcnew
CecKeypress();
506 CecCommand
^ GetNextCommand(void)
509 if (m_libCec
->GetNextCommand(&command
))
511 CecCommand
^ retVal
= gcnew
CecCommand((CecLogicalAddress
)command
.initiator
, (CecLogicalAddress
)command
.destination
, command
.ack
== 1 ? true : false, command
.eom
== 1 ? true : false, command
.opcode
, command
.transmit_timeout
);
512 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
513 retVal
->Parameters
->PushBack(command
.parameters
[iPtr
]);
517 return gcnew
CecCommand();
520 bool Transmit(CecCommand
^ command
)
522 cec_command ccommand
;
523 cec_command::Format(ccommand
, (cec_logical_address
)command
->Initiator
, (cec_logical_address
)command
->Destination
, (cec_opcode
)command
->Opcode
);
524 ccommand
.transmit_timeout
= command
->TransmitTimeout
;
525 ccommand
.eom
= command
->Eom
;
526 ccommand
.ack
= command
->Ack
;
527 for (unsigned int iPtr
= 0; iPtr
< command
->Parameters
->Size
; iPtr
++)
528 ccommand
.parameters
.PushBack(command
->Parameters
->Data
[iPtr
]);
530 return m_libCec
->Transmit(ccommand
);
533 bool SetLogicalAddress(CecLogicalAddress logicalAddress
)
535 return m_libCec
->SetLogicalAddress((cec_logical_address
) logicalAddress
);
538 bool SetPhysicalAddress(int16_t physicalAddress
)
540 return m_libCec
->SetPhysicalAddress(physicalAddress
);
543 bool PowerOnDevices(CecLogicalAddress logicalAddress
)
545 return m_libCec
->PowerOnDevices((cec_logical_address
) logicalAddress
);
548 bool StandbyDevices(CecLogicalAddress logicalAddress
)
550 return m_libCec
->StandbyDevices((cec_logical_address
) logicalAddress
);
553 bool PollDevice(CecLogicalAddress logicalAddress
)
555 return m_libCec
->PollDevice((cec_logical_address
) logicalAddress
);
558 bool SetActiveSource(CecDeviceType type
)
560 return m_libCec
->SetActiveSource((cec_device_type
) type
);
563 bool SetDeckControlMode(CecDeckControlMode mode
, bool sendUpdate
)
565 return m_libCec
->SetDeckControlMode((cec_deck_control_mode
) mode
, sendUpdate
);
568 bool SetDeckInfo(CecDeckInfo info
, bool sendUpdate
)
570 return m_libCec
->SetDeckInfo((cec_deck_info
) info
, sendUpdate
);
573 bool SetInactiveView(void)
575 return m_libCec
->SetInactiveView();
578 bool SetMenuState(CecMenuState state
, bool sendUpdate
)
580 return m_libCec
->SetMenuState((cec_menu_state
) state
, sendUpdate
);
583 bool SetOSDString(CecLogicalAddress logicalAddress
, CecDisplayControl duration
, String
^ message
)
585 marshal_context
^ context
= gcnew
marshal_context();
586 const char* strMessageC
= context
->marshal_as
<const char*>(message
);
588 bool bReturn
= m_libCec
->SetOSDString((cec_logical_address
) logicalAddress
, (cec_display_control
) duration
, strMessageC
);
594 bool SwitchMonitoring(bool enable
)
596 return m_libCec
->SwitchMonitoring(enable
);
599 CecVersion
GetDeviceCecVersion(CecLogicalAddress logicalAddress
)
601 return (CecVersion
) m_libCec
->GetDeviceCecVersion((cec_logical_address
) logicalAddress
);
604 String
^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress
)
606 cec_menu_language lang
;
607 if (m_libCec
->GetDeviceMenuLanguage((cec_logical_address
) logicalAddress
, &lang
))
609 return gcnew
String(lang
.language
);
612 return gcnew
String("");
615 uint64_t GetDeviceVendorId(CecLogicalAddress logicalAddress
)
617 return m_libCec
->GetDeviceVendorId((cec_logical_address
) logicalAddress
);
620 CecPowerStatus
GetDevicePowerStatus(CecLogicalAddress logicalAddress
)
622 return (CecPowerStatus
) m_libCec
->GetDevicePowerStatus((cec_logical_address
) logicalAddress
);
625 CecLogicalAddresses
^ GetActiveDevices(void)
627 CecLogicalAddresses
^ retVal
= gcnew
CecLogicalAddresses();
628 unsigned int iDevices
= 0;
630 cec_logical_addresses activeDevices
= m_libCec
->GetActiveDevices();
632 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
633 if (activeDevices
[iPtr
])
634 retVal
->Addresses
[iDevices
++] = (CecLogicalAddress
)iPtr
;
639 bool IsActiveDevice(CecLogicalAddress logicalAddress
)
641 return m_libCec
->IsActiveDevice((cec_logical_address
)logicalAddress
);
644 bool IsActiveDeviceType(CecDeviceType type
)
646 return m_libCec
->IsActiveDeviceType((cec_device_type
)type
);
649 bool SetHDMIPort(CecLogicalAddress address
, uint8_t port
)
651 return m_libCec
->SetHDMIPort((cec_logical_address
)address
, port
);
654 uint8_t VolumeUp(bool wait
)
656 return m_libCec
->VolumeUp(wait
);
659 uint8_t VolumeDown(bool wait
)
661 return m_libCec
->VolumeDown(wait
);
664 uint8_t MuteAudio(bool wait
)
666 return m_libCec
->MuteAudio(wait
);
669 bool SendKeypress(CecLogicalAddress destination
, CecUserControlCode key
, bool wait
)
671 return m_libCec
->SendKeypress((cec_logical_address
)destination
, (cec_user_control_code
)key
, wait
);
674 bool SendKeyRelease(CecLogicalAddress destination
, bool wait
)
676 return m_libCec
->SendKeyRelease((cec_logical_address
)destination
, wait
);
679 String
^ GetOSDName(CecLogicalAddress logicalAddress
)
681 cec_osd_name osd
= m_libCec
->GetOSDName((cec_logical_address
) logicalAddress
);
682 return gcnew
String(osd
.name
);
686 ICECAdapter
*m_libCec
;