2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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/
33 #include "CecSharpTypes.h"
36 using namespace System
;
37 using namespace System::Runtime::InteropServices
;
39 using namespace msclr::interop
;
43 public ref
class LibCecSharp
: public CecCallbackMethods
46 LibCecSharp(LibCECConfiguration
^config
)
48 m_configuration
= config
;
49 CecCallbackMethods::EnableCallbacks(m_configuration
->Callbacks
);
50 if (!InitialiseLibCec())
51 throw gcnew
Exception("Could not initialise LibCecSharp");
54 LibCecSharp(String
^ strDeviceName
, CecDeviceTypeList
^ deviceTypes
)
56 m_configuration
= gcnew
LibCECConfiguration();
57 m_configuration
->SetCallbacks(this);
58 m_configuration
->DeviceName
= strDeviceName
;
59 m_configuration
->DeviceTypes
= deviceTypes
;
60 if (!InitialiseLibCec())
61 throw gcnew
Exception("Could not initialise LibCecSharp");
77 bool InitialiseLibCec(void)
79 marshal_context
^ context
= gcnew
marshal_context();
80 libcec_configuration config
;
81 GetConfiguration(context
, config
);
83 m_libCec
= (ICECAdapter
*) CECInitialise(&config
);
86 return m_libCec
!= NULL
;
89 void GetConfiguration(marshal_context
^context
, libcec_configuration
&config
)
93 _snprintf_s(config
.strDeviceName
, 13, context
->marshal_as
<const char*>(m_configuration
->DeviceName
));
94 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
95 config
.deviceTypes
.types
[iPtr
] = (cec_device_type
)m_configuration
->DeviceTypes
->Types
[iPtr
];
97 config
.iPhysicalAddress
= m_configuration
->PhysicalAddress
;
98 config
.baseDevice
= (cec_logical_address
)m_configuration
->BaseDevice
;
99 config
.iHDMIPort
= m_configuration
->HDMIPort
;
100 config
.clientVersion
= (cec_client_version
)m_configuration
->ClientVersion
;
101 config
.bGetSettingsFromROM
= m_configuration
->GetSettingsFromROM
;
102 config
.bPowerOnStartup
= m_configuration
->PowerOnStartup
;
103 config
.bPowerOffShutdown
= m_configuration
->PowerOffShutdown
;
104 config
.bPowerOffScreensaver
= m_configuration
->PowerOffScreensaver
;
105 config
.bPowerOffOnStandby
= m_configuration
->PowerOffOnStandby
;
106 config
.callbacks
= &g_cecCallbacks
;
110 array
<CecAdapter
^> ^ FindAdapters(String
^ path
)
112 cec_adapter
*devices
= new cec_adapter
[10];
114 marshal_context
^ context
= gcnew
marshal_context();
115 const char* strPathC
= path
->Length
> 0 ? context
->marshal_as
<const char*>(path
) : NULL
;
117 uint8_t iDevicesFound
= m_libCec
->FindAdapters(devices
, 10, NULL
);
119 array
<CecAdapter
^> ^ adapters
= gcnew array
<CecAdapter
^>(iDevicesFound
);
120 for (unsigned int iPtr
= 0; iPtr
< iDevicesFound
; iPtr
++)
121 adapters
[iPtr
] = gcnew
CecAdapter(gcnew
String(devices
[iPtr
].path
), gcnew
String(devices
[iPtr
].comm
));
128 bool Open(String
^ strPort
, int iTimeoutMs
)
130 marshal_context
^ context
= gcnew
marshal_context();
131 const char* strPortC
= context
->marshal_as
<const char*>(strPort
);
132 bool bReturn
= m_libCec
->Open(strPortC
, iTimeoutMs
);
142 virtual bool EnableCallbacks(CecCallbackMethods
^ callbacks
) override
144 if (m_libCec
&& CecCallbackMethods::EnableCallbacks(callbacks
))
145 return m_libCec
->EnableCallbacks(NULL
, &g_cecCallbacks
);
150 bool PingAdapter(void)
152 return m_libCec
->PingAdapter();
155 bool StartBootloader(void)
157 return m_libCec
->StartBootloader();
160 int GetMinLibVersion(void)
162 return m_libCec
->GetMinLibVersion();
165 int GetLibVersionMajor(void)
167 return m_libCec
->GetLibVersionMajor();
170 int GetLibVersionMinor(void)
172 return m_libCec
->GetLibVersionMinor();
175 CecLogMessage
^ GetNextLogMessage(void)
178 if (m_libCec
->GetNextLogMessage(&msg
))
180 return gcnew
CecLogMessage(gcnew
String(msg
.message
), (CecLogLevel
)msg
.level
, msg
.time
);
183 return gcnew
CecLogMessage();
186 CecKeypress
^ GetNextKeypress(void)
189 if (m_libCec
->GetNextKeypress(&key
))
191 return gcnew
CecKeypress(key
.keycode
, key
.duration
);
194 return gcnew
CecKeypress();
197 CecCommand
^ GetNextCommand(void)
200 if (m_libCec
->GetNextCommand(&command
))
202 CecCommand
^ retVal
= gcnew
CecCommand((CecLogicalAddress
)command
.initiator
, (CecLogicalAddress
)command
.destination
, command
.ack
== 1 ? true : false, command
.eom
== 1 ? true : false, (CecOpcode
)command
.opcode
, command
.transmit_timeout
);
203 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
204 retVal
->Parameters
->PushBack(command
.parameters
[iPtr
]);
208 return gcnew
CecCommand();
211 bool Transmit(CecCommand
^ command
)
213 cec_command ccommand
;
214 cec_command::Format(ccommand
, (cec_logical_address
)command
->Initiator
, (cec_logical_address
)command
->Destination
, (cec_opcode
)command
->Opcode
);
215 ccommand
.transmit_timeout
= command
->TransmitTimeout
;
216 ccommand
.eom
= command
->Eom
;
217 ccommand
.ack
= command
->Ack
;
218 for (unsigned int iPtr
= 0; iPtr
< command
->Parameters
->Size
; iPtr
++)
219 ccommand
.parameters
.PushBack(command
->Parameters
->Data
[iPtr
]);
221 return m_libCec
->Transmit(ccommand
);
224 bool SetLogicalAddress(CecLogicalAddress logicalAddress
)
226 return m_libCec
->SetLogicalAddress((cec_logical_address
) logicalAddress
);
229 bool SetPhysicalAddress(uint16_t physicalAddress
)
231 return m_libCec
->SetPhysicalAddress(physicalAddress
);
234 bool PowerOnDevices(CecLogicalAddress logicalAddress
)
236 return m_libCec
->PowerOnDevices((cec_logical_address
) logicalAddress
);
239 bool StandbyDevices(CecLogicalAddress logicalAddress
)
241 return m_libCec
->StandbyDevices((cec_logical_address
) logicalAddress
);
244 bool PollDevice(CecLogicalAddress logicalAddress
)
246 return m_libCec
->PollDevice((cec_logical_address
) logicalAddress
);
249 bool SetActiveSource(CecDeviceType type
)
251 return m_libCec
->SetActiveSource((cec_device_type
) type
);
254 bool SetDeckControlMode(CecDeckControlMode mode
, bool sendUpdate
)
256 return m_libCec
->SetDeckControlMode((cec_deck_control_mode
) mode
, sendUpdate
);
259 bool SetDeckInfo(CecDeckInfo info
, bool sendUpdate
)
261 return m_libCec
->SetDeckInfo((cec_deck_info
) info
, sendUpdate
);
264 bool SetInactiveView(void)
266 return m_libCec
->SetInactiveView();
269 bool SetMenuState(CecMenuState state
, bool sendUpdate
)
271 return m_libCec
->SetMenuState((cec_menu_state
) state
, sendUpdate
);
274 bool SetOSDString(CecLogicalAddress logicalAddress
, CecDisplayControl duration
, String
^ message
)
276 marshal_context
^ context
= gcnew
marshal_context();
277 const char* strMessageC
= context
->marshal_as
<const char*>(message
);
279 bool bReturn
= m_libCec
->SetOSDString((cec_logical_address
) logicalAddress
, (cec_display_control
) duration
, strMessageC
);
285 bool SwitchMonitoring(bool enable
)
287 return m_libCec
->SwitchMonitoring(enable
);
290 CecVersion
GetDeviceCecVersion(CecLogicalAddress logicalAddress
)
292 return (CecVersion
) m_libCec
->GetDeviceCecVersion((cec_logical_address
) logicalAddress
);
295 String
^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress
)
297 cec_menu_language lang
;
298 if (m_libCec
->GetDeviceMenuLanguage((cec_logical_address
) logicalAddress
, &lang
))
300 return gcnew
String(lang
.language
);
303 return gcnew
String("");
306 CecVendorId
GetDeviceVendorId(CecLogicalAddress logicalAddress
)
308 return (CecVendorId
)m_libCec
->GetDeviceVendorId((cec_logical_address
) logicalAddress
);
311 CecPowerStatus
GetDevicePowerStatus(CecLogicalAddress logicalAddress
)
313 return (CecPowerStatus
) m_libCec
->GetDevicePowerStatus((cec_logical_address
) logicalAddress
);
316 CecLogicalAddresses
^ GetActiveDevices(void)
318 CecLogicalAddresses
^ retVal
= gcnew
CecLogicalAddresses();
319 unsigned int iDevices
= 0;
321 cec_logical_addresses activeDevices
= m_libCec
->GetActiveDevices();
323 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
324 if (activeDevices
[iPtr
])
325 retVal
->Addresses
[iDevices
++] = (CecLogicalAddress
)iPtr
;
330 bool IsActiveDevice(CecLogicalAddress logicalAddress
)
332 return m_libCec
->IsActiveDevice((cec_logical_address
)logicalAddress
);
335 bool IsActiveDeviceType(CecDeviceType type
)
337 return m_libCec
->IsActiveDeviceType((cec_device_type
)type
);
340 bool SetHDMIPort(CecLogicalAddress address
, uint8_t port
)
342 return m_libCec
->SetHDMIPort((cec_logical_address
)address
, port
);
345 uint8_t VolumeUp(bool wait
)
347 return m_libCec
->VolumeUp(wait
);
350 uint8_t VolumeDown(bool wait
)
352 return m_libCec
->VolumeDown(wait
);
355 uint8_t MuteAudio(bool wait
)
357 return m_libCec
->MuteAudio(wait
);
360 bool SendKeypress(CecLogicalAddress destination
, CecUserControlCode key
, bool wait
)
362 return m_libCec
->SendKeypress((cec_logical_address
)destination
, (cec_user_control_code
)key
, wait
);
365 bool SendKeyRelease(CecLogicalAddress destination
, bool wait
)
367 return m_libCec
->SendKeyRelease((cec_logical_address
)destination
, wait
);
370 String
^ GetDeviceOSDName(CecLogicalAddress logicalAddress
)
372 cec_osd_name osd
= m_libCec
->GetDeviceOSDName((cec_logical_address
) logicalAddress
);
373 return gcnew
String(osd
.name
);
376 CecLogicalAddress
GetActiveSource()
378 return (CecLogicalAddress
)m_libCec
->GetActiveSource();
381 bool IsActiveSource(CecLogicalAddress logicalAddress
)
383 return m_libCec
->IsActiveSource((cec_logical_address
)logicalAddress
);
386 uint16_t GetDevicePhysicalAddress(CecLogicalAddress iAddress
)
388 return m_libCec
->GetDevicePhysicalAddress((cec_logical_address
)iAddress
);
391 bool SetStreamPath(CecLogicalAddress iAddress
)
393 return m_libCec
->SetStreamPath((cec_logical_address
)iAddress
);
396 bool SetStreamPath(uint16_t iPhysicalAddress
)
398 return m_libCec
->SetStreamPath(iPhysicalAddress
);
401 CecLogicalAddresses
^GetLogicalAddresses(void)
403 CecLogicalAddresses
^addr
= gcnew
CecLogicalAddresses();
404 cec_logical_addresses libAddr
= m_libCec
->GetLogicalAddresses();
405 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
406 addr
->Addresses
[iPtr
] = (CecLogicalAddress
)libAddr
.addresses
[iPtr
];
407 addr
->Primary
= (CecLogicalAddress
)libAddr
.primary
;
411 bool GetCurrentConfiguration(LibCECConfiguration
^configuration
)
413 libcec_configuration config
;
416 if (m_libCec
->GetCurrentConfiguration(&config
))
418 configuration
->BaseDevice
= (CecLogicalAddress
)config
.baseDevice
;
419 configuration
->DeviceName
= gcnew
String(config
.strDeviceName
);
420 configuration
->HDMIPort
= config
.iHDMIPort
;
421 configuration
->PhysicalAddress
= config
.iPhysicalAddress
;
422 configuration
->PowerOffOnStandby
= config
.bPowerOffOnStandby
== 1;
423 configuration
->PowerOffScreensaver
= config
.bPowerOffScreensaver
== 1;
424 configuration
->PowerOffShutdown
= config
.bPowerOffShutdown
== 1;
425 configuration
->PowerOnStartup
= config
.bPowerOnStartup
== 1;
426 configuration
->UseTVMenuLanguage
= config
.bUseTVMenuLanguage
== 1;
427 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
428 m_configuration
->DeviceTypes
->Types
[iPtr
] = (CecDeviceType
)config
.deviceTypes
.types
[iPtr
];
434 bool CanPersistConfiguration(void)
436 return m_libCec
->CanPersistConfiguration();
439 bool PersistConfiguration(LibCECConfiguration
^configuration
)
441 marshal_context
^ context
= gcnew
marshal_context();
442 libcec_configuration config
;
443 GetConfiguration(context
, config
);
445 bool bReturn
= m_libCec
->PersistConfiguration(&config
);
451 String
^ ToString(CecLogicalAddress iAddress
)
453 const char *retVal
= m_libCec
->ToString((cec_logical_address
)iAddress
);
454 return gcnew
String(retVal
);
457 String
^ ToString(CecVendorId iVendorId
)
459 const char *retVal
= m_libCec
->ToString((cec_vendor_id
)iVendorId
);
460 return gcnew
String(retVal
);
463 String
^ ToString(CecVersion iVersion
)
465 const char *retVal
= m_libCec
->ToString((cec_version
)iVersion
);
466 return gcnew
String(retVal
);
469 String
^ ToString(CecPowerStatus iState
)
471 const char *retVal
= m_libCec
->ToString((cec_power_status
)iState
);
472 return gcnew
String(retVal
);
475 String
^ ToString(CecMenuState iState
)
477 const char *retVal
= m_libCec
->ToString((cec_menu_state
)iState
);
478 return gcnew
String(retVal
);
481 String
^ ToString(CecDeckControlMode iMode
)
483 const char *retVal
= m_libCec
->ToString((cec_deck_control_mode
)iMode
);
484 return gcnew
String(retVal
);
487 String
^ ToString(CecDeckInfo status
)
489 const char *retVal
= m_libCec
->ToString((cec_deck_info
)status
);
490 return gcnew
String(retVal
);
493 String
^ ToString(CecOpcode opcode
)
495 const char *retVal
= m_libCec
->ToString((cec_opcode
)opcode
);
496 return gcnew
String(retVal
);
499 String
^ ToString(CecSystemAudioStatus mode
)
501 const char *retVal
= m_libCec
->ToString((cec_system_audio_status
)mode
);
502 return gcnew
String(retVal
);
505 String
^ ToString(CecAudioStatus status
)
507 const char *retVal
= m_libCec
->ToString((cec_audio_status
)status
);
508 return gcnew
String(retVal
);
512 ICECAdapter
* m_libCec
;
513 LibCECConfiguration
^m_configuration
;