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 ref
class CecAdapter
150 CecAdapter(String
^ strPath
, String
^ strComPort
)
153 ComPort
= strComPort
;
156 property String
^ Path
;
157 property String
^ ComPort
;
160 public ref
class CecDeviceTypeList
163 CecDeviceTypeList(void)
165 Types
= gcnew array
<CecDeviceType
>(5);
166 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
167 Types
[iPtr
] = CecDeviceType::Reserved
;
170 property array
<CecDeviceType
> ^ Types
;
173 public ref
class CecDatapacket
178 Data
= gcnew array
<uint8_t>(100);
182 void PushBack(uint8_t data
)
191 property array
<uint8_t> ^ Data
;
192 property
uint8_t Size
;
195 public ref
class CecCommand
198 CecCommand(CecLogicalAddress iInitiator
, CecLogicalAddress iDestination
, bool bAck
, bool bEom
, int8_t iOpcode
, int32_t iTransmitTimeout
)
200 Initiator
= iInitiator
;
201 Destination
= iDestination
;
206 TransmitTimeout
= iTransmitTimeout
;
207 Parameters
= gcnew CecDatapacket
;
213 Initiator
= CecLogicalAddress::Unknown
;
214 Destination
= CecLogicalAddress::Unknown
;
220 Parameters
= gcnew CecDatapacket
;
224 void PushBack(uint8_t data
)
226 if (Initiator
== CecLogicalAddress::Unknown
&& Destination
== CecLogicalAddress::Unknown
)
228 Initiator
= (CecLogicalAddress
) (data
>> 4);
229 Destination
= (CecLogicalAddress
) (data
& 0xF);
238 Parameters
->PushBack(data
);
243 property CecLogicalAddress Initiator
;
244 property CecLogicalAddress Destination
;
247 property
int8_t Opcode
;
248 property CecDatapacket
^ Parameters
;
249 property
bool OpcodeSet
;
250 property
int32_t TransmitTimeout
;
253 public ref
class CecKeypress
256 CecKeypress(int iKeycode
, unsigned int iDuration
)
259 Duration
= iDuration
;
271 property
int Keycode
;
272 property
unsigned int Duration
;
275 public ref
class CecLogMessage
278 CecLogMessage(String
^ strMessage
, CecLogLevel iLevel
, int64_t iTime
)
280 Message
= strMessage
;
289 Level
= CecLogLevel::None
;
295 property String
^ Message
;
296 property CecLogLevel Level
;
297 property
int64_t Time
;
300 public ref
class LibCecSharp
303 LibCecSharp(String
^ strDeviceName
, CecDeviceTypeList
^ deviceTypes
)
305 marshal_context
^ context
= gcnew
marshal_context();
307 const char* strDeviceNameC
= context
->marshal_as
<const char*>(strDeviceName
);
309 cec_device_type_list types
;
310 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
311 types
.types
[iPtr
] = (cec_device_type
)deviceTypes
->Types
[iPtr
];
312 m_libCec
= (ICECAdapter
*) CECInit(strDeviceNameC
, types
);
318 CECDestroy(m_libCec
);
325 CECDestroy(m_libCec
);
330 array
<CecAdapter
^> ^ FindAdapters(String
^ path
)
332 cec_adapter
*devices
= new cec_adapter
[10];
334 marshal_context
^ context
= gcnew
marshal_context();
335 const char* strPathC
= path
->Length
> 0 ? context
->marshal_as
<const char*>(path
) : NULL
;
337 uint8_t iDevicesFound
= m_libCec
->FindAdapters(devices
, 10, NULL
);
339 array
<CecAdapter
^> ^ adapters
= gcnew array
<CecAdapter
^>(iDevicesFound
);
340 for (unsigned int iPtr
= 0; iPtr
< iDevicesFound
; iPtr
++)
341 adapters
[iPtr
] = gcnew
CecAdapter(gcnew
String(devices
[iPtr
].path
), gcnew
String(devices
[iPtr
].comm
));
348 bool Open(String
^ strPort
, int iTimeoutMs
)
350 marshal_context
^ context
= gcnew
marshal_context();
351 const char* strPortC
= context
->marshal_as
<const char*>(strPort
);
352 bool bReturn
= m_libCec
->Open(strPortC
, iTimeoutMs
);
362 bool PingAdapter(void)
364 return m_libCec
->PingAdapter();
367 bool StartBootloader(void)
369 return m_libCec
->StartBootloader();
372 int GetMinLibVersion(void)
374 return m_libCec
->GetMinLibVersion();
377 int GetLibVersionMajor(void)
379 return m_libCec
->GetLibVersionMajor();
382 int GetLibVersionMinor(void)
384 return m_libCec
->GetLibVersionMinor();
387 CecLogMessage
^ GetNextLogMessage(void)
390 if (m_libCec
->GetNextLogMessage(&msg
))
392 return gcnew
CecLogMessage(gcnew
String(msg
.message
), (CecLogLevel
)msg
.level
, msg
.time
);
395 return gcnew
CecLogMessage();
398 CecKeypress
^ GetNextKeypress(void)
401 if (m_libCec
->GetNextKeypress(&key
))
403 return gcnew
CecKeypress(key
.keycode
, key
.duration
);
406 return gcnew
CecKeypress();
409 CecCommand
^ GetNextCommand(void)
412 if (m_libCec
->GetNextCommand(&command
))
415 return gcnew
CecCommand((CecLogicalAddress
)command
.initiator
, (CecLogicalAddress
)command
.destination
, command
.ack
== 1 ? true : false, command
.eom
== 1 ? true : false, command
.opcode
, command
.transmit_timeout
);
418 return gcnew
CecCommand();
421 bool Transmit(CecCommand
^ command
)
423 cec_command ccommand
;
424 cec_command::Format(ccommand
, (cec_logical_address
)command
->Initiator
, (cec_logical_address
)command
->Destination
, (cec_opcode
)command
->Opcode
);
425 ccommand
.transmit_timeout
= command
->TransmitTimeout
;
426 ccommand
.eom
= command
->Eom
;
427 ccommand
.ack
= command
->Ack
;
428 for (unsigned int iPtr
= 0; iPtr
< command
->Parameters
->Size
; iPtr
++)
429 ccommand
.parameters
.PushBack(command
->Parameters
->Data
[iPtr
]);
431 return m_libCec
->Transmit(ccommand
);
434 bool SetLogicalAddress(CecLogicalAddress logicalAddress
)
436 return m_libCec
->SetLogicalAddress((cec_logical_address
) logicalAddress
);
439 bool SetPhysicalAddress(int16_t physicalAddress
)
441 return m_libCec
->SetPhysicalAddress(physicalAddress
);
444 bool PowerOnDevices(CecLogicalAddress logicalAddress
)
446 return m_libCec
->PowerOnDevices((cec_logical_address
) logicalAddress
);
449 bool StandbyDevices(CecLogicalAddress logicalAddress
)
451 return m_libCec
->StandbyDevices((cec_logical_address
) logicalAddress
);
454 bool PollDevice(CecLogicalAddress logicalAddress
)
456 return m_libCec
->PollDevice((cec_logical_address
) logicalAddress
);
459 bool SetActiveSource(CecDeviceType type
)
461 return m_libCec
->SetActiveSource((cec_device_type
) type
);
464 bool SetDeckControlMode(CecDeckControlMode mode
, bool sendUpdate
)
466 return m_libCec
->SetDeckControlMode((cec_deck_control_mode
) mode
, sendUpdate
);
469 bool SetDeckInfo(CecDeckInfo info
, bool sendUpdate
)
471 return m_libCec
->SetDeckInfo((cec_deck_info
) info
, sendUpdate
);
474 bool SetInactiveView(void)
476 return m_libCec
->SetInactiveView();
479 bool SetMenuState(CecMenuState state
, bool sendUpdate
)
481 return m_libCec
->SetMenuState((cec_menu_state
) state
, sendUpdate
);
484 bool SetOSDString(CecLogicalAddress logicalAddress
, CecDisplayControl duration
, String
^ message
)
486 marshal_context
^ context
= gcnew
marshal_context();
487 const char* strMessageC
= context
->marshal_as
<const char*>(message
);
489 bool bReturn
= m_libCec
->SetOSDString((cec_logical_address
) logicalAddress
, (cec_display_control
) duration
, strMessageC
);
495 bool SwitchMonitoring(bool enable
)
497 return m_libCec
->SwitchMonitoring(enable
);
500 CecVersion
GetDeviceCecVersion(CecLogicalAddress logicalAddress
)
502 return (CecVersion
) m_libCec
->GetDeviceCecVersion((cec_logical_address
) logicalAddress
);
505 String
^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress
)
507 cec_menu_language lang
;
508 if (m_libCec
->GetDeviceMenuLanguage((cec_logical_address
) logicalAddress
, &lang
))
510 return gcnew
String(lang
.language
);
513 return gcnew
String("");
516 uint64_t GetDeviceVendorId(CecLogicalAddress logicalAddress
)
518 return m_libCec
->GetDeviceVendorId((cec_logical_address
) logicalAddress
);
521 CecPowerStatus
GetDevicePowerStatus(CecLogicalAddress logicalAddress
)
523 return (CecPowerStatus
) m_libCec
->GetDevicePowerStatus((cec_logical_address
) logicalAddress
);
527 ICECAdapter
* m_libCec
;