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 CecCallbackMethods::EnableCallbacks(config
->Callbacks
);
49 if (!InitialiseLibCec(config
))
50 throw gcnew
Exception("Could not initialise LibCecSharp");
53 LibCecSharp(String
^ strDeviceName
, CecDeviceTypeList
^ deviceTypes
)
55 LibCECConfiguration
^config
= gcnew
LibCECConfiguration();
56 config
->SetCallbacks(this);
57 config
->DeviceName
= strDeviceName
;
58 config
->DeviceTypes
= deviceTypes
;
59 if (!InitialiseLibCec(config
))
60 throw gcnew
Exception("Could not initialise LibCecSharp");
76 bool InitialiseLibCec(LibCECConfiguration
^config
)
78 marshal_context
^ context
= gcnew
marshal_context();
79 libcec_configuration libCecConfig
;
80 ConvertConfiguration(context
, config
, libCecConfig
);
82 m_libCec
= (ICECAdapter
*) CECInitialise(&libCecConfig
);
85 return m_libCec
!= NULL
;
88 void ConvertConfiguration(marshal_context
^context
, LibCECConfiguration
^netConfig
, CEC::libcec_configuration
&config
)
92 _snprintf_s(config
.strDeviceName
, 13, context
->marshal_as
<const char*>(netConfig
->DeviceName
));
93 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
94 config
.deviceTypes
.types
[iPtr
] = (cec_device_type
)netConfig
->DeviceTypes
->Types
[iPtr
];
96 config
.iPhysicalAddress
= netConfig
->PhysicalAddress
;
97 config
.baseDevice
= (cec_logical_address
)netConfig
->BaseDevice
;
98 config
.iHDMIPort
= netConfig
->HDMIPort
;
99 config
.clientVersion
= (cec_client_version
)netConfig
->ClientVersion
;
100 config
.bGetSettingsFromROM
= netConfig
->GetSettingsFromROM
;
101 config
.bActivateSource
= netConfig
->ActivateSource
;
102 config
.tvVendor
= (cec_vendor_id
)netConfig
->TvVendor
;
103 config
.wakeDevices
.Clear();
104 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
106 if (netConfig
->WakeDevices
->IsSet((CecLogicalAddress
)iPtr
))
107 config
.wakeDevices
.Set((cec_logical_address
)iPtr
);
109 config
.powerOffDevices
.Clear();
110 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
112 if (netConfig
->PowerOffDevices
->IsSet((CecLogicalAddress
)iPtr
))
113 config
.powerOffDevices
.Set((cec_logical_address
)iPtr
);
115 config
.bPowerOffScreensaver
= netConfig
->PowerOffScreensaver
;
116 config
.bPowerOffOnStandby
= netConfig
->PowerOffOnStandby
;
117 config
.callbacks
= &g_cecCallbacks
;
121 array
<CecAdapter
^> ^ FindAdapters(String
^ path
)
123 cec_adapter
*devices
= new cec_adapter
[10];
125 marshal_context
^ context
= gcnew
marshal_context();
126 const char* strPathC
= path
->Length
> 0 ? context
->marshal_as
<const char*>(path
) : NULL
;
128 uint8_t iDevicesFound
= m_libCec
->FindAdapters(devices
, 10, NULL
);
130 array
<CecAdapter
^> ^ adapters
= gcnew array
<CecAdapter
^>(iDevicesFound
);
131 for (unsigned int iPtr
= 0; iPtr
< iDevicesFound
; iPtr
++)
132 adapters
[iPtr
] = gcnew
CecAdapter(gcnew
String(devices
[iPtr
].path
), gcnew
String(devices
[iPtr
].comm
));
139 bool Open(String
^ strPort
, int iTimeoutMs
)
141 marshal_context
^ context
= gcnew
marshal_context();
142 const char* strPortC
= context
->marshal_as
<const char*>(strPort
);
143 bool bReturn
= m_libCec
->Open(strPortC
, iTimeoutMs
);
150 // delete the callbacks, since these might already have been destroyed in .NET
151 m_libCec
->EnableCallbacks(NULL
, NULL
);
152 m_libCec
->StandbyDevices();
156 virtual bool EnableCallbacks(CecCallbackMethods
^ callbacks
) override
158 if (m_libCec
&& CecCallbackMethods::EnableCallbacks(callbacks
))
159 return m_libCec
->EnableCallbacks(NULL
, &g_cecCallbacks
);
164 bool PingAdapter(void)
166 return m_libCec
->PingAdapter();
169 bool StartBootloader(void)
171 return m_libCec
->StartBootloader();
174 int GetMinLibVersion(void)
176 return m_libCec
->GetMinLibVersion();
179 int GetLibVersionMajor(void)
181 return m_libCec
->GetLibVersionMajor();
184 int GetLibVersionMinor(void)
186 return m_libCec
->GetLibVersionMinor();
189 CecLogMessage
^ GetNextLogMessage(void)
192 if (m_libCec
->GetNextLogMessage(&msg
))
194 return gcnew
CecLogMessage(gcnew
String(msg
.message
), (CecLogLevel
)msg
.level
, msg
.time
);
197 return gcnew
CecLogMessage();
200 CecKeypress
^ GetNextKeypress(void)
203 if (m_libCec
->GetNextKeypress(&key
))
205 return gcnew
CecKeypress(key
.keycode
, key
.duration
);
208 return gcnew
CecKeypress();
211 CecCommand
^ GetNextCommand(void)
214 if (m_libCec
->GetNextCommand(&command
))
216 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
);
217 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
218 retVal
->Parameters
->PushBack(command
.parameters
[iPtr
]);
222 return gcnew
CecCommand();
225 bool Transmit(CecCommand
^ command
)
227 cec_command ccommand
;
228 cec_command::Format(ccommand
, (cec_logical_address
)command
->Initiator
, (cec_logical_address
)command
->Destination
, (cec_opcode
)command
->Opcode
);
229 ccommand
.transmit_timeout
= command
->TransmitTimeout
;
230 ccommand
.eom
= command
->Eom
;
231 ccommand
.ack
= command
->Ack
;
232 for (unsigned int iPtr
= 0; iPtr
< command
->Parameters
->Size
; iPtr
++)
233 ccommand
.parameters
.PushBack(command
->Parameters
->Data
[iPtr
]);
235 return m_libCec
->Transmit(ccommand
);
238 bool SetLogicalAddress(CecLogicalAddress logicalAddress
)
240 return m_libCec
->SetLogicalAddress((cec_logical_address
) logicalAddress
);
243 bool SetPhysicalAddress(uint16_t physicalAddress
)
245 return m_libCec
->SetPhysicalAddress(physicalAddress
);
248 bool PowerOnDevices(CecLogicalAddress logicalAddress
)
250 return m_libCec
->PowerOnDevices((cec_logical_address
) logicalAddress
);
253 bool StandbyDevices(CecLogicalAddress logicalAddress
)
255 return m_libCec
->StandbyDevices((cec_logical_address
) logicalAddress
);
258 bool PollDevice(CecLogicalAddress logicalAddress
)
260 return m_libCec
->PollDevice((cec_logical_address
) logicalAddress
);
263 bool SetActiveSource(CecDeviceType type
)
265 return m_libCec
->SetActiveSource((cec_device_type
) type
);
268 bool SetDeckControlMode(CecDeckControlMode mode
, bool sendUpdate
)
270 return m_libCec
->SetDeckControlMode((cec_deck_control_mode
) mode
, sendUpdate
);
273 bool SetDeckInfo(CecDeckInfo info
, bool sendUpdate
)
275 return m_libCec
->SetDeckInfo((cec_deck_info
) info
, sendUpdate
);
278 bool SetInactiveView(void)
280 return m_libCec
->SetInactiveView();
283 bool SetMenuState(CecMenuState state
, bool sendUpdate
)
285 return m_libCec
->SetMenuState((cec_menu_state
) state
, sendUpdate
);
288 bool SetOSDString(CecLogicalAddress logicalAddress
, CecDisplayControl duration
, String
^ message
)
290 marshal_context
^ context
= gcnew
marshal_context();
291 const char* strMessageC
= context
->marshal_as
<const char*>(message
);
293 bool bReturn
= m_libCec
->SetOSDString((cec_logical_address
) logicalAddress
, (cec_display_control
) duration
, strMessageC
);
299 bool SwitchMonitoring(bool enable
)
301 return m_libCec
->SwitchMonitoring(enable
);
304 CecVersion
GetDeviceCecVersion(CecLogicalAddress logicalAddress
)
306 return (CecVersion
) m_libCec
->GetDeviceCecVersion((cec_logical_address
) logicalAddress
);
309 String
^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress
)
311 cec_menu_language lang
;
312 if (m_libCec
->GetDeviceMenuLanguage((cec_logical_address
) logicalAddress
, &lang
))
314 return gcnew
String(lang
.language
);
317 return gcnew
String("");
320 CecVendorId
GetDeviceVendorId(CecLogicalAddress logicalAddress
)
322 return (CecVendorId
)m_libCec
->GetDeviceVendorId((cec_logical_address
) logicalAddress
);
325 CecPowerStatus
GetDevicePowerStatus(CecLogicalAddress logicalAddress
)
327 return (CecPowerStatus
) m_libCec
->GetDevicePowerStatus((cec_logical_address
) logicalAddress
);
330 CecLogicalAddresses
^ GetActiveDevices(void)
332 CecLogicalAddresses
^ retVal
= gcnew
CecLogicalAddresses();
333 unsigned int iDevices
= 0;
335 cec_logical_addresses activeDevices
= m_libCec
->GetActiveDevices();
337 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
338 if (activeDevices
[iPtr
])
339 retVal
->Addresses
[iDevices
++] = (CecLogicalAddress
)iPtr
;
344 bool IsActiveDevice(CecLogicalAddress logicalAddress
)
346 return m_libCec
->IsActiveDevice((cec_logical_address
)logicalAddress
);
349 bool IsActiveDeviceType(CecDeviceType type
)
351 return m_libCec
->IsActiveDeviceType((cec_device_type
)type
);
354 bool SetHDMIPort(CecLogicalAddress address
, uint8_t port
)
356 return m_libCec
->SetHDMIPort((cec_logical_address
)address
, port
);
359 uint8_t VolumeUp(bool wait
)
361 return m_libCec
->VolumeUp(wait
);
364 uint8_t VolumeDown(bool wait
)
366 return m_libCec
->VolumeDown(wait
);
369 uint8_t MuteAudio(bool wait
)
371 return m_libCec
->MuteAudio(wait
);
374 bool SendKeypress(CecLogicalAddress destination
, CecUserControlCode key
, bool wait
)
376 return m_libCec
->SendKeypress((cec_logical_address
)destination
, (cec_user_control_code
)key
, wait
);
379 bool SendKeyRelease(CecLogicalAddress destination
, bool wait
)
381 return m_libCec
->SendKeyRelease((cec_logical_address
)destination
, wait
);
384 String
^ GetDeviceOSDName(CecLogicalAddress logicalAddress
)
386 cec_osd_name osd
= m_libCec
->GetDeviceOSDName((cec_logical_address
) logicalAddress
);
387 return gcnew
String(osd
.name
);
390 CecLogicalAddress
GetActiveSource()
392 return (CecLogicalAddress
)m_libCec
->GetActiveSource();
395 bool IsActiveSource(CecLogicalAddress logicalAddress
)
397 return m_libCec
->IsActiveSource((cec_logical_address
)logicalAddress
);
400 uint16_t GetDevicePhysicalAddress(CecLogicalAddress iAddress
)
402 return m_libCec
->GetDevicePhysicalAddress((cec_logical_address
)iAddress
);
405 bool SetStreamPath(CecLogicalAddress iAddress
)
407 return m_libCec
->SetStreamPath((cec_logical_address
)iAddress
);
410 bool SetStreamPath(uint16_t iPhysicalAddress
)
412 return m_libCec
->SetStreamPath(iPhysicalAddress
);
415 CecLogicalAddresses
^GetLogicalAddresses(void)
417 CecLogicalAddresses
^addr
= gcnew
CecLogicalAddresses();
418 cec_logical_addresses libAddr
= m_libCec
->GetLogicalAddresses();
419 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
420 addr
->Addresses
[iPtr
] = (CecLogicalAddress
)libAddr
.addresses
[iPtr
];
421 addr
->Primary
= (CecLogicalAddress
)libAddr
.primary
;
425 bool GetCurrentConfiguration(LibCECConfiguration
^configuration
)
427 libcec_configuration config
;
430 if (m_libCec
->GetCurrentConfiguration(&config
))
432 configuration
->BaseDevice
= (CecLogicalAddress
)config
.baseDevice
;
433 configuration
->DeviceName
= gcnew
String(config
.strDeviceName
);
434 configuration
->HDMIPort
= config
.iHDMIPort
;
435 configuration
->PhysicalAddress
= config
.iPhysicalAddress
;
436 configuration
->PowerOffOnStandby
= config
.bPowerOffOnStandby
== 1;
437 configuration
->PowerOffScreensaver
= config
.bPowerOffScreensaver
== 1;
438 configuration
->ActivateSource
= config
.bActivateSource
== 1;
439 configuration
->TvVendor
= (CecVendorId
)config
.tvVendor
;
441 configuration
->WakeDevices
->Clear();
443 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
444 if (config
.wakeDevices
[iPtr
])
445 configuration
->WakeDevices
->Addresses
[iDevices
++] = (CecLogicalAddress
)iPtr
;
447 configuration
->PowerOffDevices
->Clear();
449 for (uint8_t iPtr
= 0; iPtr
<= 16; iPtr
++)
450 if (config
.powerOffDevices
[iPtr
])
451 configuration
->PowerOffDevices
->Addresses
[iDevices
++] = (CecLogicalAddress
)iPtr
;
453 configuration
->UseTVMenuLanguage
= config
.bUseTVMenuLanguage
== 1;
454 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
455 configuration
->DeviceTypes
->Types
[iPtr
] = (CecDeviceType
)config
.deviceTypes
.types
[iPtr
];
461 bool CanPersistConfiguration(void)
463 return m_libCec
->CanPersistConfiguration();
466 bool PersistConfiguration(LibCECConfiguration
^configuration
)
468 marshal_context
^ context
= gcnew
marshal_context();
469 libcec_configuration config
;
470 ConvertConfiguration(context
, configuration
, config
);
472 bool bReturn
= m_libCec
->PersistConfiguration(&config
);
478 bool SetConfiguration(LibCECConfiguration
^configuration
)
480 marshal_context
^ context
= gcnew
marshal_context();
481 libcec_configuration config
;
482 ConvertConfiguration(context
, configuration
, config
);
484 bool bReturn
= m_libCec
->SetConfiguration(&config
);
490 String
^ ToString(CecLogicalAddress iAddress
)
492 const char *retVal
= m_libCec
->ToString((cec_logical_address
)iAddress
);
493 return gcnew
String(retVal
);
496 String
^ ToString(CecVendorId iVendorId
)
498 const char *retVal
= m_libCec
->ToString((cec_vendor_id
)iVendorId
);
499 return gcnew
String(retVal
);
502 String
^ ToString(CecVersion iVersion
)
504 const char *retVal
= m_libCec
->ToString((cec_version
)iVersion
);
505 return gcnew
String(retVal
);
508 String
^ ToString(CecPowerStatus iState
)
510 const char *retVal
= m_libCec
->ToString((cec_power_status
)iState
);
511 return gcnew
String(retVal
);
514 String
^ ToString(CecMenuState iState
)
516 const char *retVal
= m_libCec
->ToString((cec_menu_state
)iState
);
517 return gcnew
String(retVal
);
520 String
^ ToString(CecDeckControlMode iMode
)
522 const char *retVal
= m_libCec
->ToString((cec_deck_control_mode
)iMode
);
523 return gcnew
String(retVal
);
526 String
^ ToString(CecDeckInfo status
)
528 const char *retVal
= m_libCec
->ToString((cec_deck_info
)status
);
529 return gcnew
String(retVal
);
532 String
^ ToString(CecOpcode opcode
)
534 const char *retVal
= m_libCec
->ToString((cec_opcode
)opcode
);
535 return gcnew
String(retVal
);
538 String
^ ToString(CecSystemAudioStatus mode
)
540 const char *retVal
= m_libCec
->ToString((cec_system_audio_status
)mode
);
541 return gcnew
String(retVal
);
544 String
^ ToString(CecAudioStatus status
)
546 const char *retVal
= m_libCec
->ToString((cec_audio_status
)status
);
547 return gcnew
String(retVal
);
551 ICECAdapter
* m_libCec
;