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
);
139 // delete the callbacks, since these might already have been destroyed in .NET
140 m_libCec
->EnableCallbacks(NULL
, NULL
);
144 virtual bool EnableCallbacks(CecCallbackMethods
^ callbacks
) override
146 if (m_libCec
&& CecCallbackMethods::EnableCallbacks(callbacks
))
147 return m_libCec
->EnableCallbacks(NULL
, &g_cecCallbacks
);
152 bool PingAdapter(void)
154 return m_libCec
->PingAdapter();
157 bool StartBootloader(void)
159 return m_libCec
->StartBootloader();
162 int GetMinLibVersion(void)
164 return m_libCec
->GetMinLibVersion();
167 int GetLibVersionMajor(void)
169 return m_libCec
->GetLibVersionMajor();
172 int GetLibVersionMinor(void)
174 return m_libCec
->GetLibVersionMinor();
177 CecLogMessage
^ GetNextLogMessage(void)
180 if (m_libCec
->GetNextLogMessage(&msg
))
182 return gcnew
CecLogMessage(gcnew
String(msg
.message
), (CecLogLevel
)msg
.level
, msg
.time
);
185 return gcnew
CecLogMessage();
188 CecKeypress
^ GetNextKeypress(void)
191 if (m_libCec
->GetNextKeypress(&key
))
193 return gcnew
CecKeypress(key
.keycode
, key
.duration
);
196 return gcnew
CecKeypress();
199 CecCommand
^ GetNextCommand(void)
202 if (m_libCec
->GetNextCommand(&command
))
204 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
);
205 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
206 retVal
->Parameters
->PushBack(command
.parameters
[iPtr
]);
210 return gcnew
CecCommand();
213 bool Transmit(CecCommand
^ command
)
215 cec_command ccommand
;
216 cec_command::Format(ccommand
, (cec_logical_address
)command
->Initiator
, (cec_logical_address
)command
->Destination
, (cec_opcode
)command
->Opcode
);
217 ccommand
.transmit_timeout
= command
->TransmitTimeout
;
218 ccommand
.eom
= command
->Eom
;
219 ccommand
.ack
= command
->Ack
;
220 for (unsigned int iPtr
= 0; iPtr
< command
->Parameters
->Size
; iPtr
++)
221 ccommand
.parameters
.PushBack(command
->Parameters
->Data
[iPtr
]);
223 return m_libCec
->Transmit(ccommand
);
226 bool SetLogicalAddress(CecLogicalAddress logicalAddress
)
228 return m_libCec
->SetLogicalAddress((cec_logical_address
) logicalAddress
);
231 bool SetPhysicalAddress(uint16_t physicalAddress
)
233 return m_libCec
->SetPhysicalAddress(physicalAddress
);
236 bool PowerOnDevices(CecLogicalAddress logicalAddress
)
238 return m_libCec
->PowerOnDevices((cec_logical_address
) logicalAddress
);
241 bool StandbyDevices(CecLogicalAddress logicalAddress
)
243 return m_libCec
->StandbyDevices((cec_logical_address
) logicalAddress
);
246 bool PollDevice(CecLogicalAddress logicalAddress
)
248 return m_libCec
->PollDevice((cec_logical_address
) logicalAddress
);
251 bool SetActiveSource(CecDeviceType type
)
253 return m_libCec
->SetActiveSource((cec_device_type
) type
);
256 bool SetDeckControlMode(CecDeckControlMode mode
, bool sendUpdate
)
258 return m_libCec
->SetDeckControlMode((cec_deck_control_mode
) mode
, sendUpdate
);
261 bool SetDeckInfo(CecDeckInfo info
, bool sendUpdate
)
263 return m_libCec
->SetDeckInfo((cec_deck_info
) info
, sendUpdate
);
266 bool SetInactiveView(void)
268 return m_libCec
->SetInactiveView();
271 bool SetMenuState(CecMenuState state
, bool sendUpdate
)
273 return m_libCec
->SetMenuState((cec_menu_state
) state
, sendUpdate
);
276 bool SetOSDString(CecLogicalAddress logicalAddress
, CecDisplayControl duration
, String
^ message
)
278 marshal_context
^ context
= gcnew
marshal_context();
279 const char* strMessageC
= context
->marshal_as
<const char*>(message
);
281 bool bReturn
= m_libCec
->SetOSDString((cec_logical_address
) logicalAddress
, (cec_display_control
) duration
, strMessageC
);
287 bool SwitchMonitoring(bool enable
)
289 return m_libCec
->SwitchMonitoring(enable
);
292 CecVersion
GetDeviceCecVersion(CecLogicalAddress logicalAddress
)
294 return (CecVersion
) m_libCec
->GetDeviceCecVersion((cec_logical_address
) logicalAddress
);
297 String
^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress
)
299 cec_menu_language lang
;
300 if (m_libCec
->GetDeviceMenuLanguage((cec_logical_address
) logicalAddress
, &lang
))
302 return gcnew
String(lang
.language
);
305 return gcnew
String("");
308 CecVendorId
GetDeviceVendorId(CecLogicalAddress logicalAddress
)
310 return (CecVendorId
)m_libCec
->GetDeviceVendorId((cec_logical_address
) logicalAddress
);
313 CecPowerStatus
GetDevicePowerStatus(CecLogicalAddress logicalAddress
)
315 return (CecPowerStatus
) m_libCec
->GetDevicePowerStatus((cec_logical_address
) logicalAddress
);
318 CecLogicalAddresses
^ GetActiveDevices(void)
320 CecLogicalAddresses
^ retVal
= gcnew
CecLogicalAddresses();
321 unsigned int iDevices
= 0;
323 cec_logical_addresses activeDevices
= m_libCec
->GetActiveDevices();
325 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
326 if (activeDevices
[iPtr
])
327 retVal
->Addresses
[iDevices
++] = (CecLogicalAddress
)iPtr
;
332 bool IsActiveDevice(CecLogicalAddress logicalAddress
)
334 return m_libCec
->IsActiveDevice((cec_logical_address
)logicalAddress
);
337 bool IsActiveDeviceType(CecDeviceType type
)
339 return m_libCec
->IsActiveDeviceType((cec_device_type
)type
);
342 bool SetHDMIPort(CecLogicalAddress address
, uint8_t port
)
344 return m_libCec
->SetHDMIPort((cec_logical_address
)address
, port
);
347 uint8_t VolumeUp(bool wait
)
349 return m_libCec
->VolumeUp(wait
);
352 uint8_t VolumeDown(bool wait
)
354 return m_libCec
->VolumeDown(wait
);
357 uint8_t MuteAudio(bool wait
)
359 return m_libCec
->MuteAudio(wait
);
362 bool SendKeypress(CecLogicalAddress destination
, CecUserControlCode key
, bool wait
)
364 return m_libCec
->SendKeypress((cec_logical_address
)destination
, (cec_user_control_code
)key
, wait
);
367 bool SendKeyRelease(CecLogicalAddress destination
, bool wait
)
369 return m_libCec
->SendKeyRelease((cec_logical_address
)destination
, wait
);
372 String
^ GetDeviceOSDName(CecLogicalAddress logicalAddress
)
374 cec_osd_name osd
= m_libCec
->GetDeviceOSDName((cec_logical_address
) logicalAddress
);
375 return gcnew
String(osd
.name
);
378 CecLogicalAddress
GetActiveSource()
380 return (CecLogicalAddress
)m_libCec
->GetActiveSource();
383 bool IsActiveSource(CecLogicalAddress logicalAddress
)
385 return m_libCec
->IsActiveSource((cec_logical_address
)logicalAddress
);
388 uint16_t GetDevicePhysicalAddress(CecLogicalAddress iAddress
)
390 return m_libCec
->GetDevicePhysicalAddress((cec_logical_address
)iAddress
);
393 bool SetStreamPath(CecLogicalAddress iAddress
)
395 return m_libCec
->SetStreamPath((cec_logical_address
)iAddress
);
398 bool SetStreamPath(uint16_t iPhysicalAddress
)
400 return m_libCec
->SetStreamPath(iPhysicalAddress
);
403 CecLogicalAddresses
^GetLogicalAddresses(void)
405 CecLogicalAddresses
^addr
= gcnew
CecLogicalAddresses();
406 cec_logical_addresses libAddr
= m_libCec
->GetLogicalAddresses();
407 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
408 addr
->Addresses
[iPtr
] = (CecLogicalAddress
)libAddr
.addresses
[iPtr
];
409 addr
->Primary
= (CecLogicalAddress
)libAddr
.primary
;
413 bool GetCurrentConfiguration(LibCECConfiguration
^configuration
)
415 libcec_configuration config
;
418 if (m_libCec
->GetCurrentConfiguration(&config
))
420 configuration
->BaseDevice
= (CecLogicalAddress
)config
.baseDevice
;
421 configuration
->DeviceName
= gcnew
String(config
.strDeviceName
);
422 configuration
->HDMIPort
= config
.iHDMIPort
;
423 configuration
->PhysicalAddress
= config
.iPhysicalAddress
;
424 configuration
->PowerOffOnStandby
= config
.bPowerOffOnStandby
== 1;
425 configuration
->PowerOffScreensaver
= config
.bPowerOffScreensaver
== 1;
426 configuration
->PowerOffShutdown
= config
.bPowerOffShutdown
== 1;
427 configuration
->PowerOnStartup
= config
.bPowerOnStartup
== 1;
428 configuration
->UseTVMenuLanguage
= config
.bUseTVMenuLanguage
== 1;
429 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
430 m_configuration
->DeviceTypes
->Types
[iPtr
] = (CecDeviceType
)config
.deviceTypes
.types
[iPtr
];
436 bool CanPersistConfiguration(void)
438 return m_libCec
->CanPersistConfiguration();
441 bool PersistConfiguration(LibCECConfiguration
^configuration
)
443 marshal_context
^ context
= gcnew
marshal_context();
444 libcec_configuration config
;
445 GetConfiguration(context
, config
);
447 bool bReturn
= m_libCec
->PersistConfiguration(&config
);
453 String
^ ToString(CecLogicalAddress iAddress
)
455 const char *retVal
= m_libCec
->ToString((cec_logical_address
)iAddress
);
456 return gcnew
String(retVal
);
459 String
^ ToString(CecVendorId iVendorId
)
461 const char *retVal
= m_libCec
->ToString((cec_vendor_id
)iVendorId
);
462 return gcnew
String(retVal
);
465 String
^ ToString(CecVersion iVersion
)
467 const char *retVal
= m_libCec
->ToString((cec_version
)iVersion
);
468 return gcnew
String(retVal
);
471 String
^ ToString(CecPowerStatus iState
)
473 const char *retVal
= m_libCec
->ToString((cec_power_status
)iState
);
474 return gcnew
String(retVal
);
477 String
^ ToString(CecMenuState iState
)
479 const char *retVal
= m_libCec
->ToString((cec_menu_state
)iState
);
480 return gcnew
String(retVal
);
483 String
^ ToString(CecDeckControlMode iMode
)
485 const char *retVal
= m_libCec
->ToString((cec_deck_control_mode
)iMode
);
486 return gcnew
String(retVal
);
489 String
^ ToString(CecDeckInfo status
)
491 const char *retVal
= m_libCec
->ToString((cec_deck_info
)status
);
492 return gcnew
String(retVal
);
495 String
^ ToString(CecOpcode opcode
)
497 const char *retVal
= m_libCec
->ToString((cec_opcode
)opcode
);
498 return gcnew
String(retVal
);
501 String
^ ToString(CecSystemAudioStatus mode
)
503 const char *retVal
= m_libCec
->ToString((cec_system_audio_status
)mode
);
504 return gcnew
String(retVal
);
507 String
^ ToString(CecAudioStatus status
)
509 const char *retVal
= m_libCec
->ToString((cec_audio_status
)status
);
510 return gcnew
String(retVal
);
514 ICECAdapter
* m_libCec
;
515 LibCECConfiguration
^m_configuration
;