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 _snprintf_s(config
.strDeviceName
, 13, context
->marshal_as
<const char*>(netConfig
->DeviceName
));
95 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
96 config
.deviceTypes
.types
[iPtr
] = (cec_device_type
)netConfig
->DeviceTypes
->Types
[iPtr
];
98 config
.bAutodetectAddress
= netConfig
->AutodetectAddress
? 1 : 0;
99 config
.iPhysicalAddress
= netConfig
->PhysicalAddress
;
100 config
.baseDevice
= (cec_logical_address
)netConfig
->BaseDevice
;
101 config
.iHDMIPort
= netConfig
->HDMIPort
;
102 config
.clientVersion
= (cec_client_version
)netConfig
->ClientVersion
;
103 config
.bGetSettingsFromROM
= netConfig
->GetSettingsFromROM
? 1 : 0;
104 config
.bActivateSource
= netConfig
->ActivateSource
? 1 : 0;
105 config
.tvVendor
= (cec_vendor_id
)netConfig
->TvVendor
;
106 config
.wakeDevices
.Clear();
107 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
109 if (netConfig
->WakeDevices
->IsSet((CecLogicalAddress
)iPtr
))
110 config
.wakeDevices
.Set((cec_logical_address
)iPtr
);
112 config
.powerOffDevices
.Clear();
113 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
115 if (netConfig
->PowerOffDevices
->IsSet((CecLogicalAddress
)iPtr
))
116 config
.powerOffDevices
.Set((cec_logical_address
)iPtr
);
118 config
.bPowerOffScreensaver
= netConfig
->PowerOffScreensaver
? 1 : 0;
119 config
.bPowerOffOnStandby
= netConfig
->PowerOffOnStandby
? 1 : 0;
120 config
.callbacks
= &g_cecCallbacks
;
124 array
<CecAdapter
^> ^ FindAdapters(String
^ path
)
126 cec_adapter
*devices
= new cec_adapter
[10];
128 marshal_context
^ context
= gcnew
marshal_context();
129 const char* strPathC
= path
->Length
> 0 ? context
->marshal_as
<const char*>(path
) : NULL
;
131 uint8_t iDevicesFound
= m_libCec
->FindAdapters(devices
, 10, NULL
);
133 array
<CecAdapter
^> ^ adapters
= gcnew array
<CecAdapter
^>(iDevicesFound
);
134 for (unsigned int iPtr
= 0; iPtr
< iDevicesFound
; iPtr
++)
135 adapters
[iPtr
] = gcnew
CecAdapter(gcnew
String(devices
[iPtr
].path
), gcnew
String(devices
[iPtr
].comm
));
142 bool Open(String
^ strPort
, int iTimeoutMs
)
144 CecCallbackMethods::EnableCallbacks(m_callbacks
);
145 EnableCallbacks(m_callbacks
);
146 marshal_context
^ context
= gcnew
marshal_context();
147 const char* strPortC
= context
->marshal_as
<const char*>(strPort
);
148 bool bReturn
= m_libCec
->Open(strPortC
, iTimeoutMs
);
159 virtual void DisableCallbacks(void) override
161 // delete the callbacks, since these might already have been destroyed in .NET
162 CecCallbackMethods::DisableCallbacks();
164 m_libCec
->EnableCallbacks(NULL
, NULL
);
167 virtual bool EnableCallbacks(CecCallbackMethods
^ callbacks
) override
169 if (m_libCec
&& CecCallbackMethods::EnableCallbacks(callbacks
))
170 return m_libCec
->EnableCallbacks(NULL
, &g_cecCallbacks
);
175 bool PingAdapter(void)
177 return m_libCec
->PingAdapter();
180 bool StartBootloader(void)
182 return m_libCec
->StartBootloader();
185 int GetMinLibVersion(void)
187 return m_libCec
->GetMinLibVersion();
190 int GetLibVersionMajor(void)
192 return m_libCec
->GetLibVersionMajor();
195 int GetLibVersionMinor(void)
197 return m_libCec
->GetLibVersionMinor();
200 CecLogMessage
^ GetNextLogMessage(void)
203 if (m_libCec
->GetNextLogMessage(&msg
))
205 return gcnew
CecLogMessage(gcnew
String(msg
.message
), (CecLogLevel
)msg
.level
, msg
.time
);
208 return gcnew
CecLogMessage();
211 CecKeypress
^ GetNextKeypress(void)
214 if (m_libCec
->GetNextKeypress(&key
))
216 return gcnew
CecKeypress(key
.keycode
, key
.duration
);
219 return gcnew
CecKeypress();
222 CecCommand
^ GetNextCommand(void)
225 if (m_libCec
->GetNextCommand(&command
))
227 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
);
228 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
229 retVal
->Parameters
->PushBack(command
.parameters
[iPtr
]);
233 return gcnew
CecCommand();
236 bool Transmit(CecCommand
^ command
)
238 cec_command ccommand
;
239 cec_command::Format(ccommand
, (cec_logical_address
)command
->Initiator
, (cec_logical_address
)command
->Destination
, (cec_opcode
)command
->Opcode
);
240 ccommand
.transmit_timeout
= command
->TransmitTimeout
;
241 ccommand
.eom
= command
->Eom
;
242 ccommand
.ack
= command
->Ack
;
243 for (unsigned int iPtr
= 0; iPtr
< command
->Parameters
->Size
; iPtr
++)
244 ccommand
.parameters
.PushBack(command
->Parameters
->Data
[iPtr
]);
246 return m_libCec
->Transmit(ccommand
);
249 bool SetLogicalAddress(CecLogicalAddress logicalAddress
)
251 return m_libCec
->SetLogicalAddress((cec_logical_address
) logicalAddress
);
254 bool SetPhysicalAddress(uint16_t physicalAddress
)
256 return m_libCec
->SetPhysicalAddress(physicalAddress
);
259 bool PowerOnDevices(CecLogicalAddress logicalAddress
)
261 return m_libCec
->PowerOnDevices((cec_logical_address
) logicalAddress
);
264 bool StandbyDevices(CecLogicalAddress logicalAddress
)
266 return m_libCec
->StandbyDevices((cec_logical_address
) logicalAddress
);
269 bool PollDevice(CecLogicalAddress logicalAddress
)
271 return m_libCec
->PollDevice((cec_logical_address
) logicalAddress
);
274 bool SetActiveSource(CecDeviceType type
)
276 return m_libCec
->SetActiveSource((cec_device_type
) type
);
279 bool SetDeckControlMode(CecDeckControlMode mode
, bool sendUpdate
)
281 return m_libCec
->SetDeckControlMode((cec_deck_control_mode
) mode
, sendUpdate
);
284 bool SetDeckInfo(CecDeckInfo info
, bool sendUpdate
)
286 return m_libCec
->SetDeckInfo((cec_deck_info
) info
, sendUpdate
);
289 bool SetInactiveView(void)
291 return m_libCec
->SetInactiveView();
294 bool SetMenuState(CecMenuState state
, bool sendUpdate
)
296 return m_libCec
->SetMenuState((cec_menu_state
) state
, sendUpdate
);
299 bool SetOSDString(CecLogicalAddress logicalAddress
, CecDisplayControl duration
, String
^ message
)
301 marshal_context
^ context
= gcnew
marshal_context();
302 const char* strMessageC
= context
->marshal_as
<const char*>(message
);
304 bool bReturn
= m_libCec
->SetOSDString((cec_logical_address
) logicalAddress
, (cec_display_control
) duration
, strMessageC
);
310 bool SwitchMonitoring(bool enable
)
312 return m_libCec
->SwitchMonitoring(enable
);
315 CecVersion
GetDeviceCecVersion(CecLogicalAddress logicalAddress
)
317 return (CecVersion
) m_libCec
->GetDeviceCecVersion((cec_logical_address
) logicalAddress
);
320 String
^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress
)
322 cec_menu_language lang
;
323 if (m_libCec
->GetDeviceMenuLanguage((cec_logical_address
) logicalAddress
, &lang
))
325 return gcnew
String(lang
.language
);
328 return gcnew
String("");
331 CecVendorId
GetDeviceVendorId(CecLogicalAddress logicalAddress
)
333 return (CecVendorId
)m_libCec
->GetDeviceVendorId((cec_logical_address
) logicalAddress
);
336 CecPowerStatus
GetDevicePowerStatus(CecLogicalAddress logicalAddress
)
338 return (CecPowerStatus
) m_libCec
->GetDevicePowerStatus((cec_logical_address
) logicalAddress
);
341 void RescanActiveDevices(void)
343 m_libCec
->RescanActiveDevices();
346 CecLogicalAddresses
^ GetActiveDevices(void)
348 CecLogicalAddresses
^ retVal
= gcnew
CecLogicalAddresses();
349 unsigned int iDevices
= 0;
351 cec_logical_addresses activeDevices
= m_libCec
->GetActiveDevices();
353 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
354 if (activeDevices
[iPtr
])
355 retVal
->Addresses
[iDevices
++] = (CecLogicalAddress
)iPtr
;
360 bool IsActiveDevice(CecLogicalAddress logicalAddress
)
362 return m_libCec
->IsActiveDevice((cec_logical_address
)logicalAddress
);
365 bool IsActiveDeviceType(CecDeviceType type
)
367 return m_libCec
->IsActiveDeviceType((cec_device_type
)type
);
370 bool SetHDMIPort(CecLogicalAddress address
, uint8_t port
)
372 return m_libCec
->SetHDMIPort((cec_logical_address
)address
, port
);
375 uint8_t VolumeUp(bool wait
)
377 return m_libCec
->VolumeUp(wait
);
380 uint8_t VolumeDown(bool wait
)
382 return m_libCec
->VolumeDown(wait
);
385 uint8_t MuteAudio(bool wait
)
387 return m_libCec
->MuteAudio(wait
);
390 bool SendKeypress(CecLogicalAddress destination
, CecUserControlCode key
, bool wait
)
392 return m_libCec
->SendKeypress((cec_logical_address
)destination
, (cec_user_control_code
)key
, wait
);
395 bool SendKeyRelease(CecLogicalAddress destination
, bool wait
)
397 return m_libCec
->SendKeyRelease((cec_logical_address
)destination
, wait
);
400 String
^ GetDeviceOSDName(CecLogicalAddress logicalAddress
)
402 cec_osd_name osd
= m_libCec
->GetDeviceOSDName((cec_logical_address
) logicalAddress
);
403 return gcnew
String(osd
.name
);
406 CecLogicalAddress
GetActiveSource()
408 return (CecLogicalAddress
)m_libCec
->GetActiveSource();
411 bool IsActiveSource(CecLogicalAddress logicalAddress
)
413 return m_libCec
->IsActiveSource((cec_logical_address
)logicalAddress
);
416 uint16_t GetDevicePhysicalAddress(CecLogicalAddress iAddress
)
418 return m_libCec
->GetDevicePhysicalAddress((cec_logical_address
)iAddress
);
421 bool SetStreamPath(CecLogicalAddress iAddress
)
423 return m_libCec
->SetStreamPath((cec_logical_address
)iAddress
);
426 bool SetStreamPath(uint16_t iPhysicalAddress
)
428 return m_libCec
->SetStreamPath(iPhysicalAddress
);
431 CecLogicalAddresses
^GetLogicalAddresses(void)
433 CecLogicalAddresses
^addr
= gcnew
CecLogicalAddresses();
434 cec_logical_addresses libAddr
= m_libCec
->GetLogicalAddresses();
435 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
436 addr
->Addresses
[iPtr
] = (CecLogicalAddress
)libAddr
.addresses
[iPtr
];
437 addr
->Primary
= (CecLogicalAddress
)libAddr
.primary
;
441 bool GetCurrentConfiguration(LibCECConfiguration
^configuration
)
443 libcec_configuration config
;
446 if (m_libCec
->GetCurrentConfiguration(&config
))
448 configuration
->Update(config
);
454 bool CanPersistConfiguration(void)
456 return m_libCec
->CanPersistConfiguration();
459 bool PersistConfiguration(LibCECConfiguration
^configuration
)
461 marshal_context
^ context
= gcnew
marshal_context();
462 libcec_configuration config
;
463 ConvertConfiguration(context
, configuration
, config
);
465 bool bReturn
= m_libCec
->PersistConfiguration(&config
);
471 bool SetConfiguration(LibCECConfiguration
^configuration
)
473 marshal_context
^ context
= gcnew
marshal_context();
474 libcec_configuration config
;
475 ConvertConfiguration(context
, configuration
, config
);
477 bool bReturn
= m_libCec
->SetConfiguration(&config
);
483 String
^ ToString(CecLogicalAddress iAddress
)
485 const char *retVal
= m_libCec
->ToString((cec_logical_address
)iAddress
);
486 return gcnew
String(retVal
);
489 String
^ ToString(CecVendorId iVendorId
)
491 const char *retVal
= m_libCec
->ToString((cec_vendor_id
)iVendorId
);
492 return gcnew
String(retVal
);
495 String
^ ToString(CecVersion iVersion
)
497 const char *retVal
= m_libCec
->ToString((cec_version
)iVersion
);
498 return gcnew
String(retVal
);
501 String
^ ToString(CecPowerStatus iState
)
503 const char *retVal
= m_libCec
->ToString((cec_power_status
)iState
);
504 return gcnew
String(retVal
);
507 String
^ ToString(CecMenuState iState
)
509 const char *retVal
= m_libCec
->ToString((cec_menu_state
)iState
);
510 return gcnew
String(retVal
);
513 String
^ ToString(CecDeckControlMode iMode
)
515 const char *retVal
= m_libCec
->ToString((cec_deck_control_mode
)iMode
);
516 return gcnew
String(retVal
);
519 String
^ ToString(CecDeckInfo status
)
521 const char *retVal
= m_libCec
->ToString((cec_deck_info
)status
);
522 return gcnew
String(retVal
);
525 String
^ ToString(CecOpcode opcode
)
527 const char *retVal
= m_libCec
->ToString((cec_opcode
)opcode
);
528 return gcnew
String(retVal
);
531 String
^ ToString(CecSystemAudioStatus mode
)
533 const char *retVal
= m_libCec
->ToString((cec_system_audio_status
)mode
);
534 return gcnew
String(retVal
);
537 String
^ ToString(CecAudioStatus status
)
539 const char *retVal
= m_libCec
->ToString((cec_audio_status
)status
);
540 return gcnew
String(retVal
);
543 String
^ ToString(CecClientVersion version
)
545 const char *retVal
= m_libCec
->ToString((cec_client_version
)version
);
546 return gcnew
String(retVal
);
549 String
^ ToString(CecServerVersion version
)
551 const char *retVal
= m_libCec
->ToString((cec_server_version
)version
);
552 return gcnew
String(retVal
);
556 ICECAdapter
* m_libCec
;
557 CecCallbackMethods
^ m_callbacks
;