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_callbacks
= config
->Callbacks
;
49 CecCallbackMethods::EnableCallbacks(m_callbacks
);
50 if (!InitialiseLibCec(config
))
51 throw gcnew
Exception("Could not initialise LibCecSharp");
54 LibCecSharp(String
^ strDeviceName
, CecDeviceTypeList
^ deviceTypes
)
56 m_callbacks
= gcnew
CecCallbackMethods();
57 LibCECConfiguration
^config
= gcnew
LibCECConfiguration();
58 config
->SetCallbacks(this);
59 config
->DeviceName
= strDeviceName
;
60 config
->DeviceTypes
= deviceTypes
;
61 if (!InitialiseLibCec(config
))
62 throw gcnew
Exception("Could not initialise LibCecSharp");
78 bool InitialiseLibCec(LibCECConfiguration
^config
)
80 marshal_context
^ context
= gcnew
marshal_context();
81 libcec_configuration libCecConfig
;
82 ConvertConfiguration(context
, config
, libCecConfig
);
84 m_libCec
= (ICECAdapter
*) CECInitialise(&libCecConfig
);
87 return m_libCec
!= NULL
;
90 void ConvertConfiguration(marshal_context
^context
, LibCECConfiguration
^netConfig
, CEC::libcec_configuration
&config
)
94 const char *strDeviceName
= context
->marshal_as
<const char*>(netConfig
->DeviceName
);
95 memcpy_s(config
.strDeviceName
, 13, strDeviceName
, 13);
96 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
97 config
.deviceTypes
.types
[iPtr
] = (cec_device_type
)netConfig
->DeviceTypes
->Types
[iPtr
];
99 config
.bAutodetectAddress
= netConfig
->AutodetectAddress
? 1 : 0;
100 config
.iPhysicalAddress
= netConfig
->PhysicalAddress
;
101 config
.baseDevice
= (cec_logical_address
)netConfig
->BaseDevice
;
102 config
.iHDMIPort
= netConfig
->HDMIPort
;
103 config
.clientVersion
= (cec_client_version
)netConfig
->ClientVersion
;
104 config
.bGetSettingsFromROM
= netConfig
->GetSettingsFromROM
? 1 : 0;
105 config
.bActivateSource
= netConfig
->ActivateSource
? 1 : 0;
106 config
.tvVendor
= (cec_vendor_id
)netConfig
->TvVendor
;
107 config
.wakeDevices
.Clear();
108 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
110 if (netConfig
->WakeDevices
->IsSet((CecLogicalAddress
)iPtr
))
111 config
.wakeDevices
.Set((cec_logical_address
)iPtr
);
113 config
.powerOffDevices
.Clear();
114 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
116 if (netConfig
->PowerOffDevices
->IsSet((CecLogicalAddress
)iPtr
))
117 config
.powerOffDevices
.Set((cec_logical_address
)iPtr
);
119 config
.bPowerOffScreensaver
= netConfig
->PowerOffScreensaver
? 1 : 0;
120 config
.bPowerOffOnStandby
= netConfig
->PowerOffOnStandby
? 1 : 0;
122 if (netConfig
->ServerVersion
>= CecServerVersion::Version1_5_1
)
123 config
.bSendInactiveSource
= netConfig
->SendInactiveSource
? 1 : 0;
125 if (netConfig
->ServerVersion
>= CecServerVersion::Version1_6_0
)
127 config
.bPowerOffDevicesOnStandby
= netConfig
->PowerOffDevicesOnStandby
? 1 : 0;
128 config
.bShutdownOnStandby
= netConfig
->ShutdownOnStandby
? 1 : 0;
131 if (netConfig
->ServerVersion
>= CecServerVersion::Version1_6_2
)
133 const char *strDeviceLanguage
= context
->marshal_as
<const char*>(netConfig
->DeviceLanguage
);
134 memcpy_s(config
.strDeviceLanguage
, 3, strDeviceLanguage
, 3);
137 if (netConfig
->ServerVersion
>= CecServerVersion::Version1_6_3
)
139 config
.bMonitorOnly
= netConfig
->MonitorOnlyClient
? 1 : 0;
142 config
.callbacks
= &g_cecCallbacks
;
146 array
<CecAdapter
^> ^ FindAdapters(String
^ path
)
148 cec_adapter
*devices
= new cec_adapter
[10];
150 marshal_context
^ context
= gcnew
marshal_context();
151 const char* strPathC
= path
->Length
> 0 ? context
->marshal_as
<const char*>(path
) : NULL
;
153 uint8_t iDevicesFound
= m_libCec
->FindAdapters(devices
, 10, NULL
);
155 array
<CecAdapter
^> ^ adapters
= gcnew array
<CecAdapter
^>(iDevicesFound
);
156 for (unsigned int iPtr
= 0; iPtr
< iDevicesFound
; iPtr
++)
157 adapters
[iPtr
] = gcnew
CecAdapter(gcnew
String(devices
[iPtr
].path
), gcnew
String(devices
[iPtr
].comm
));
164 bool Open(String
^ strPort
, int iTimeoutMs
)
166 CecCallbackMethods::EnableCallbacks(m_callbacks
);
167 EnableCallbacks(m_callbacks
);
168 marshal_context
^ context
= gcnew
marshal_context();
169 const char* strPortC
= context
->marshal_as
<const char*>(strPort
);
170 bool bReturn
= m_libCec
->Open(strPortC
, iTimeoutMs
);
181 virtual void DisableCallbacks(void) override
183 // delete the callbacks, since these might already have been destroyed in .NET
184 CecCallbackMethods::DisableCallbacks();
186 m_libCec
->EnableCallbacks(NULL
, NULL
);
189 virtual bool EnableCallbacks(CecCallbackMethods
^ callbacks
) override
191 if (m_libCec
&& CecCallbackMethods::EnableCallbacks(callbacks
))
192 return m_libCec
->EnableCallbacks(NULL
, &g_cecCallbacks
);
197 bool PingAdapter(void)
199 return m_libCec
->PingAdapter();
202 bool StartBootloader(void)
204 return m_libCec
->StartBootloader();
207 int GetMinLibVersion(void)
209 return m_libCec
->GetMinLibVersion();
212 int GetLibVersionMajor(void)
214 return m_libCec
->GetLibVersionMajor();
217 int GetLibVersionMinor(void)
219 return m_libCec
->GetLibVersionMinor();
222 CecLogMessage
^ GetNextLogMessage(void)
225 if (m_libCec
->GetNextLogMessage(&msg
))
227 return gcnew
CecLogMessage(gcnew
String(msg
.message
), (CecLogLevel
)msg
.level
, msg
.time
);
230 return gcnew
CecLogMessage();
233 CecKeypress
^ GetNextKeypress(void)
236 if (m_libCec
->GetNextKeypress(&key
))
238 return gcnew
CecKeypress((CecUserControlCode
)key
.keycode
, key
.duration
);
241 return gcnew
CecKeypress();
244 CecCommand
^ GetNextCommand(void)
247 if (m_libCec
->GetNextCommand(&command
))
249 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
);
250 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
251 retVal
->Parameters
->PushBack(command
.parameters
[iPtr
]);
255 return gcnew
CecCommand();
258 bool Transmit(CecCommand
^ command
)
260 cec_command ccommand
;
261 cec_command::Format(ccommand
, (cec_logical_address
)command
->Initiator
, (cec_logical_address
)command
->Destination
, (cec_opcode
)command
->Opcode
);
262 ccommand
.transmit_timeout
= command
->TransmitTimeout
;
263 ccommand
.eom
= command
->Eom
;
264 ccommand
.ack
= command
->Ack
;
265 for (unsigned int iPtr
= 0; iPtr
< command
->Parameters
->Size
; iPtr
++)
266 ccommand
.parameters
.PushBack(command
->Parameters
->Data
[iPtr
]);
268 return m_libCec
->Transmit(ccommand
);
271 bool SetLogicalAddress(CecLogicalAddress logicalAddress
)
273 return m_libCec
->SetLogicalAddress((cec_logical_address
) logicalAddress
);
276 bool SetPhysicalAddress(uint16_t physicalAddress
)
278 return m_libCec
->SetPhysicalAddress(physicalAddress
);
281 bool PowerOnDevices(CecLogicalAddress logicalAddress
)
283 return m_libCec
->PowerOnDevices((cec_logical_address
) logicalAddress
);
286 bool StandbyDevices(CecLogicalAddress logicalAddress
)
288 return m_libCec
->StandbyDevices((cec_logical_address
) logicalAddress
);
291 bool PollDevice(CecLogicalAddress logicalAddress
)
293 return m_libCec
->PollDevice((cec_logical_address
) logicalAddress
);
296 bool SetActiveSource(CecDeviceType type
)
298 return m_libCec
->SetActiveSource((cec_device_type
) type
);
301 bool SetDeckControlMode(CecDeckControlMode mode
, bool sendUpdate
)
303 return m_libCec
->SetDeckControlMode((cec_deck_control_mode
) mode
, sendUpdate
);
306 bool SetDeckInfo(CecDeckInfo info
, bool sendUpdate
)
308 return m_libCec
->SetDeckInfo((cec_deck_info
) info
, sendUpdate
);
311 bool SetInactiveView(void)
313 return m_libCec
->SetInactiveView();
316 bool SetMenuState(CecMenuState state
, bool sendUpdate
)
318 return m_libCec
->SetMenuState((cec_menu_state
) state
, sendUpdate
);
321 bool SetOSDString(CecLogicalAddress logicalAddress
, CecDisplayControl duration
, String
^ message
)
323 marshal_context
^ context
= gcnew
marshal_context();
324 const char* strMessageC
= context
->marshal_as
<const char*>(message
);
326 bool bReturn
= m_libCec
->SetOSDString((cec_logical_address
) logicalAddress
, (cec_display_control
) duration
, strMessageC
);
332 bool SwitchMonitoring(bool enable
)
334 return m_libCec
->SwitchMonitoring(enable
);
337 CecVersion
GetDeviceCecVersion(CecLogicalAddress logicalAddress
)
339 return (CecVersion
) m_libCec
->GetDeviceCecVersion((cec_logical_address
) logicalAddress
);
342 String
^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress
)
344 cec_menu_language lang
;
345 if (m_libCec
->GetDeviceMenuLanguage((cec_logical_address
) logicalAddress
, &lang
))
347 return gcnew
String(lang
.language
);
350 return gcnew
String("");
353 CecVendorId
GetDeviceVendorId(CecLogicalAddress logicalAddress
)
355 return (CecVendorId
)m_libCec
->GetDeviceVendorId((cec_logical_address
) logicalAddress
);
358 CecPowerStatus
GetDevicePowerStatus(CecLogicalAddress logicalAddress
)
360 return (CecPowerStatus
) m_libCec
->GetDevicePowerStatus((cec_logical_address
) logicalAddress
);
363 void RescanActiveDevices(void)
365 m_libCec
->RescanActiveDevices();
368 CecLogicalAddresses
^ GetActiveDevices(void)
370 CecLogicalAddresses
^ retVal
= gcnew
CecLogicalAddresses();
371 unsigned int iDevices
= 0;
373 cec_logical_addresses activeDevices
= m_libCec
->GetActiveDevices();
375 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
376 if (activeDevices
[iPtr
])
377 retVal
->Addresses
[iDevices
++] = (CecLogicalAddress
)iPtr
;
382 bool IsActiveDevice(CecLogicalAddress logicalAddress
)
384 return m_libCec
->IsActiveDevice((cec_logical_address
)logicalAddress
);
387 bool IsActiveDeviceType(CecDeviceType type
)
389 return m_libCec
->IsActiveDeviceType((cec_device_type
)type
);
392 bool SetHDMIPort(CecLogicalAddress address
, uint8_t port
)
394 return m_libCec
->SetHDMIPort((cec_logical_address
)address
, port
);
397 uint8_t VolumeUp(bool wait
)
399 return m_libCec
->VolumeUp(wait
);
402 uint8_t VolumeDown(bool wait
)
404 return m_libCec
->VolumeDown(wait
);
407 uint8_t MuteAudio(bool wait
)
409 return m_libCec
->MuteAudio(wait
);
412 bool SendKeypress(CecLogicalAddress destination
, CecUserControlCode key
, bool wait
)
414 return m_libCec
->SendKeypress((cec_logical_address
)destination
, (cec_user_control_code
)key
, wait
);
417 bool SendKeyRelease(CecLogicalAddress destination
, bool wait
)
419 return m_libCec
->SendKeyRelease((cec_logical_address
)destination
, wait
);
422 String
^ GetDeviceOSDName(CecLogicalAddress logicalAddress
)
424 cec_osd_name osd
= m_libCec
->GetDeviceOSDName((cec_logical_address
) logicalAddress
);
425 return gcnew
String(osd
.name
);
428 CecLogicalAddress
GetActiveSource()
430 return (CecLogicalAddress
)m_libCec
->GetActiveSource();
433 bool IsActiveSource(CecLogicalAddress logicalAddress
)
435 return m_libCec
->IsActiveSource((cec_logical_address
)logicalAddress
);
438 uint16_t GetDevicePhysicalAddress(CecLogicalAddress iAddress
)
440 return m_libCec
->GetDevicePhysicalAddress((cec_logical_address
)iAddress
);
443 bool SetStreamPath(CecLogicalAddress iAddress
)
445 return m_libCec
->SetStreamPath((cec_logical_address
)iAddress
);
448 bool SetStreamPath(uint16_t iPhysicalAddress
)
450 return m_libCec
->SetStreamPath(iPhysicalAddress
);
453 CecLogicalAddresses
^GetLogicalAddresses(void)
455 CecLogicalAddresses
^addr
= gcnew
CecLogicalAddresses();
456 cec_logical_addresses libAddr
= m_libCec
->GetLogicalAddresses();
457 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
458 addr
->Addresses
[iPtr
] = (CecLogicalAddress
)libAddr
.addresses
[iPtr
];
459 addr
->Primary
= (CecLogicalAddress
)libAddr
.primary
;
463 bool GetCurrentConfiguration(LibCECConfiguration
^configuration
)
465 libcec_configuration config
;
468 if (m_libCec
->GetCurrentConfiguration(&config
))
470 configuration
->Update(config
);
476 bool CanPersistConfiguration(void)
478 return m_libCec
->CanPersistConfiguration();
481 bool PersistConfiguration(LibCECConfiguration
^configuration
)
483 marshal_context
^ context
= gcnew
marshal_context();
484 libcec_configuration config
;
485 ConvertConfiguration(context
, configuration
, config
);
487 bool bReturn
= m_libCec
->PersistConfiguration(&config
);
493 bool SetConfiguration(LibCECConfiguration
^configuration
)
495 marshal_context
^ context
= gcnew
marshal_context();
496 libcec_configuration config
;
497 ConvertConfiguration(context
, configuration
, config
);
499 bool bReturn
= m_libCec
->SetConfiguration(&config
);
505 bool IsLibCECActiveSource()
507 return m_libCec
->IsLibCECActiveSource();
510 bool GetDeviceInformation(String
^ port
, LibCECConfiguration
^configuration
, uint32_t timeoutMs
)
513 marshal_context
^ context
= gcnew
marshal_context();
515 libcec_configuration config
;
518 const char* strPortC
= port
->Length
> 0 ? context
->marshal_as
<const char*>(port
) : NULL
;
520 if (m_libCec
->GetDeviceInformation(strPortC
, &config
, timeoutMs
))
522 configuration
->Update(config
);
530 String
^ ToString(CecLogicalAddress iAddress
)
532 const char *retVal
= m_libCec
->ToString((cec_logical_address
)iAddress
);
533 return gcnew
String(retVal
);
536 String
^ ToString(CecVendorId iVendorId
)
538 const char *retVal
= m_libCec
->ToString((cec_vendor_id
)iVendorId
);
539 return gcnew
String(retVal
);
542 String
^ ToString(CecVersion iVersion
)
544 const char *retVal
= m_libCec
->ToString((cec_version
)iVersion
);
545 return gcnew
String(retVal
);
548 String
^ ToString(CecPowerStatus iState
)
550 const char *retVal
= m_libCec
->ToString((cec_power_status
)iState
);
551 return gcnew
String(retVal
);
554 String
^ ToString(CecMenuState iState
)
556 const char *retVal
= m_libCec
->ToString((cec_menu_state
)iState
);
557 return gcnew
String(retVal
);
560 String
^ ToString(CecDeckControlMode iMode
)
562 const char *retVal
= m_libCec
->ToString((cec_deck_control_mode
)iMode
);
563 return gcnew
String(retVal
);
566 String
^ ToString(CecDeckInfo status
)
568 const char *retVal
= m_libCec
->ToString((cec_deck_info
)status
);
569 return gcnew
String(retVal
);
572 String
^ ToString(CecOpcode opcode
)
574 const char *retVal
= m_libCec
->ToString((cec_opcode
)opcode
);
575 return gcnew
String(retVal
);
578 String
^ ToString(CecSystemAudioStatus mode
)
580 const char *retVal
= m_libCec
->ToString((cec_system_audio_status
)mode
);
581 return gcnew
String(retVal
);
584 String
^ ToString(CecAudioStatus status
)
586 const char *retVal
= m_libCec
->ToString((cec_audio_status
)status
);
587 return gcnew
String(retVal
);
590 String
^ ToString(CecClientVersion version
)
592 const char *retVal
= m_libCec
->ToString((cec_client_version
)version
);
593 return gcnew
String(retVal
);
596 String
^ ToString(CecServerVersion version
)
598 const char *retVal
= m_libCec
->ToString((cec_server_version
)version
);
599 return gcnew
String(retVal
);
603 ICECAdapter
* m_libCec
;
604 CecCallbackMethods
^ m_callbacks
;