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
.bAutodetectAddress
= netConfig
->AutodetectAddress
? 1 : 0;
97 config
.iPhysicalAddress
= netConfig
->PhysicalAddress
;
98 config
.baseDevice
= (cec_logical_address
)netConfig
->BaseDevice
;
99 config
.iHDMIPort
= netConfig
->HDMIPort
;
100 config
.clientVersion
= (cec_client_version
)netConfig
->ClientVersion
;
101 config
.bGetSettingsFromROM
= netConfig
->GetSettingsFromROM
? 1 : 0;
102 config
.bActivateSource
= netConfig
->ActivateSource
? 1 : 0;
103 config
.tvVendor
= (cec_vendor_id
)netConfig
->TvVendor
;
104 config
.wakeDevices
.Clear();
105 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
107 if (netConfig
->WakeDevices
->IsSet((CecLogicalAddress
)iPtr
))
108 config
.wakeDevices
.Set((cec_logical_address
)iPtr
);
110 config
.powerOffDevices
.Clear();
111 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
113 if (netConfig
->PowerOffDevices
->IsSet((CecLogicalAddress
)iPtr
))
114 config
.powerOffDevices
.Set((cec_logical_address
)iPtr
);
116 config
.bPowerOffScreensaver
= netConfig
->PowerOffScreensaver
? 1 : 0;
117 config
.bPowerOffOnStandby
= netConfig
->PowerOffOnStandby
? 1 : 0;
118 config
.callbacks
= &g_cecCallbacks
;
122 array
<CecAdapter
^> ^ FindAdapters(String
^ path
)
124 cec_adapter
*devices
= new cec_adapter
[10];
126 marshal_context
^ context
= gcnew
marshal_context();
127 const char* strPathC
= path
->Length
> 0 ? context
->marshal_as
<const char*>(path
) : NULL
;
129 uint8_t iDevicesFound
= m_libCec
->FindAdapters(devices
, 10, NULL
);
131 array
<CecAdapter
^> ^ adapters
= gcnew array
<CecAdapter
^>(iDevicesFound
);
132 for (unsigned int iPtr
= 0; iPtr
< iDevicesFound
; iPtr
++)
133 adapters
[iPtr
] = gcnew
CecAdapter(gcnew
String(devices
[iPtr
].path
), gcnew
String(devices
[iPtr
].comm
));
140 bool Open(String
^ strPort
, int iTimeoutMs
)
142 marshal_context
^ context
= gcnew
marshal_context();
143 const char* strPortC
= context
->marshal_as
<const char*>(strPort
);
144 bool bReturn
= m_libCec
->Open(strPortC
, iTimeoutMs
);
155 virtual void DisableCallbacks(void) override
157 // delete the callbacks, since these might already have been destroyed in .NET
158 CecCallbackMethods::DisableCallbacks();
160 m_libCec
->EnableCallbacks(NULL
, NULL
);
163 virtual bool EnableCallbacks(CecCallbackMethods
^ callbacks
) override
165 if (m_libCec
&& CecCallbackMethods::EnableCallbacks(callbacks
))
166 return m_libCec
->EnableCallbacks(NULL
, &g_cecCallbacks
);
171 bool PingAdapter(void)
173 return m_libCec
->PingAdapter();
176 bool StartBootloader(void)
178 return m_libCec
->StartBootloader();
181 int GetMinLibVersion(void)
183 return m_libCec
->GetMinLibVersion();
186 int GetLibVersionMajor(void)
188 return m_libCec
->GetLibVersionMajor();
191 int GetLibVersionMinor(void)
193 return m_libCec
->GetLibVersionMinor();
196 CecLogMessage
^ GetNextLogMessage(void)
199 if (m_libCec
->GetNextLogMessage(&msg
))
201 return gcnew
CecLogMessage(gcnew
String(msg
.message
), (CecLogLevel
)msg
.level
, msg
.time
);
204 return gcnew
CecLogMessage();
207 CecKeypress
^ GetNextKeypress(void)
210 if (m_libCec
->GetNextKeypress(&key
))
212 return gcnew
CecKeypress(key
.keycode
, key
.duration
);
215 return gcnew
CecKeypress();
218 CecCommand
^ GetNextCommand(void)
221 if (m_libCec
->GetNextCommand(&command
))
223 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
);
224 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
225 retVal
->Parameters
->PushBack(command
.parameters
[iPtr
]);
229 return gcnew
CecCommand();
232 bool Transmit(CecCommand
^ command
)
234 cec_command ccommand
;
235 cec_command::Format(ccommand
, (cec_logical_address
)command
->Initiator
, (cec_logical_address
)command
->Destination
, (cec_opcode
)command
->Opcode
);
236 ccommand
.transmit_timeout
= command
->TransmitTimeout
;
237 ccommand
.eom
= command
->Eom
;
238 ccommand
.ack
= command
->Ack
;
239 for (unsigned int iPtr
= 0; iPtr
< command
->Parameters
->Size
; iPtr
++)
240 ccommand
.parameters
.PushBack(command
->Parameters
->Data
[iPtr
]);
242 return m_libCec
->Transmit(ccommand
);
245 bool SetLogicalAddress(CecLogicalAddress logicalAddress
)
247 return m_libCec
->SetLogicalAddress((cec_logical_address
) logicalAddress
);
250 bool SetPhysicalAddress(uint16_t physicalAddress
)
252 return m_libCec
->SetPhysicalAddress(physicalAddress
);
255 bool PowerOnDevices(CecLogicalAddress logicalAddress
)
257 return m_libCec
->PowerOnDevices((cec_logical_address
) logicalAddress
);
260 bool StandbyDevices(CecLogicalAddress logicalAddress
)
262 return m_libCec
->StandbyDevices((cec_logical_address
) logicalAddress
);
265 bool PollDevice(CecLogicalAddress logicalAddress
)
267 return m_libCec
->PollDevice((cec_logical_address
) logicalAddress
);
270 bool SetActiveSource(CecDeviceType type
)
272 return m_libCec
->SetActiveSource((cec_device_type
) type
);
275 bool SetDeckControlMode(CecDeckControlMode mode
, bool sendUpdate
)
277 return m_libCec
->SetDeckControlMode((cec_deck_control_mode
) mode
, sendUpdate
);
280 bool SetDeckInfo(CecDeckInfo info
, bool sendUpdate
)
282 return m_libCec
->SetDeckInfo((cec_deck_info
) info
, sendUpdate
);
285 bool SetInactiveView(void)
287 return m_libCec
->SetInactiveView();
290 bool SetMenuState(CecMenuState state
, bool sendUpdate
)
292 return m_libCec
->SetMenuState((cec_menu_state
) state
, sendUpdate
);
295 bool SetOSDString(CecLogicalAddress logicalAddress
, CecDisplayControl duration
, String
^ message
)
297 marshal_context
^ context
= gcnew
marshal_context();
298 const char* strMessageC
= context
->marshal_as
<const char*>(message
);
300 bool bReturn
= m_libCec
->SetOSDString((cec_logical_address
) logicalAddress
, (cec_display_control
) duration
, strMessageC
);
306 bool SwitchMonitoring(bool enable
)
308 return m_libCec
->SwitchMonitoring(enable
);
311 CecVersion
GetDeviceCecVersion(CecLogicalAddress logicalAddress
)
313 return (CecVersion
) m_libCec
->GetDeviceCecVersion((cec_logical_address
) logicalAddress
);
316 String
^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress
)
318 cec_menu_language lang
;
319 if (m_libCec
->GetDeviceMenuLanguage((cec_logical_address
) logicalAddress
, &lang
))
321 return gcnew
String(lang
.language
);
324 return gcnew
String("");
327 CecVendorId
GetDeviceVendorId(CecLogicalAddress logicalAddress
)
329 return (CecVendorId
)m_libCec
->GetDeviceVendorId((cec_logical_address
) logicalAddress
);
332 CecPowerStatus
GetDevicePowerStatus(CecLogicalAddress logicalAddress
)
334 return (CecPowerStatus
) m_libCec
->GetDevicePowerStatus((cec_logical_address
) logicalAddress
);
337 void RescanActiveDevices(void)
339 m_libCec
->RescanActiveDevices();
342 CecLogicalAddresses
^ GetActiveDevices(void)
344 CecLogicalAddresses
^ retVal
= gcnew
CecLogicalAddresses();
345 unsigned int iDevices
= 0;
347 cec_logical_addresses activeDevices
= m_libCec
->GetActiveDevices();
349 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
350 if (activeDevices
[iPtr
])
351 retVal
->Addresses
[iDevices
++] = (CecLogicalAddress
)iPtr
;
356 bool IsActiveDevice(CecLogicalAddress logicalAddress
)
358 return m_libCec
->IsActiveDevice((cec_logical_address
)logicalAddress
);
361 bool IsActiveDeviceType(CecDeviceType type
)
363 return m_libCec
->IsActiveDeviceType((cec_device_type
)type
);
366 bool SetHDMIPort(CecLogicalAddress address
, uint8_t port
)
368 return m_libCec
->SetHDMIPort((cec_logical_address
)address
, port
);
371 uint8_t VolumeUp(bool wait
)
373 return m_libCec
->VolumeUp(wait
);
376 uint8_t VolumeDown(bool wait
)
378 return m_libCec
->VolumeDown(wait
);
381 uint8_t MuteAudio(bool wait
)
383 return m_libCec
->MuteAudio(wait
);
386 bool SendKeypress(CecLogicalAddress destination
, CecUserControlCode key
, bool wait
)
388 return m_libCec
->SendKeypress((cec_logical_address
)destination
, (cec_user_control_code
)key
, wait
);
391 bool SendKeyRelease(CecLogicalAddress destination
, bool wait
)
393 return m_libCec
->SendKeyRelease((cec_logical_address
)destination
, wait
);
396 String
^ GetDeviceOSDName(CecLogicalAddress logicalAddress
)
398 cec_osd_name osd
= m_libCec
->GetDeviceOSDName((cec_logical_address
) logicalAddress
);
399 return gcnew
String(osd
.name
);
402 CecLogicalAddress
GetActiveSource()
404 return (CecLogicalAddress
)m_libCec
->GetActiveSource();
407 bool IsActiveSource(CecLogicalAddress logicalAddress
)
409 return m_libCec
->IsActiveSource((cec_logical_address
)logicalAddress
);
412 uint16_t GetDevicePhysicalAddress(CecLogicalAddress iAddress
)
414 return m_libCec
->GetDevicePhysicalAddress((cec_logical_address
)iAddress
);
417 bool SetStreamPath(CecLogicalAddress iAddress
)
419 return m_libCec
->SetStreamPath((cec_logical_address
)iAddress
);
422 bool SetStreamPath(uint16_t iPhysicalAddress
)
424 return m_libCec
->SetStreamPath(iPhysicalAddress
);
427 CecLogicalAddresses
^GetLogicalAddresses(void)
429 CecLogicalAddresses
^addr
= gcnew
CecLogicalAddresses();
430 cec_logical_addresses libAddr
= m_libCec
->GetLogicalAddresses();
431 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
432 addr
->Addresses
[iPtr
] = (CecLogicalAddress
)libAddr
.addresses
[iPtr
];
433 addr
->Primary
= (CecLogicalAddress
)libAddr
.primary
;
437 bool GetCurrentConfiguration(LibCECConfiguration
^configuration
)
439 libcec_configuration config
;
442 if (m_libCec
->GetCurrentConfiguration(&config
))
444 configuration
->Update(config
);
450 bool CanPersistConfiguration(void)
452 return m_libCec
->CanPersistConfiguration();
455 bool PersistConfiguration(LibCECConfiguration
^configuration
)
457 marshal_context
^ context
= gcnew
marshal_context();
458 libcec_configuration config
;
459 ConvertConfiguration(context
, configuration
, config
);
461 bool bReturn
= m_libCec
->PersistConfiguration(&config
);
467 bool SetConfiguration(LibCECConfiguration
^configuration
)
469 marshal_context
^ context
= gcnew
marshal_context();
470 libcec_configuration config
;
471 ConvertConfiguration(context
, configuration
, config
);
473 bool bReturn
= m_libCec
->SetConfiguration(&config
);
479 String
^ ToString(CecLogicalAddress iAddress
)
481 const char *retVal
= m_libCec
->ToString((cec_logical_address
)iAddress
);
482 return gcnew
String(retVal
);
485 String
^ ToString(CecVendorId iVendorId
)
487 const char *retVal
= m_libCec
->ToString((cec_vendor_id
)iVendorId
);
488 return gcnew
String(retVal
);
491 String
^ ToString(CecVersion iVersion
)
493 const char *retVal
= m_libCec
->ToString((cec_version
)iVersion
);
494 return gcnew
String(retVal
);
497 String
^ ToString(CecPowerStatus iState
)
499 const char *retVal
= m_libCec
->ToString((cec_power_status
)iState
);
500 return gcnew
String(retVal
);
503 String
^ ToString(CecMenuState iState
)
505 const char *retVal
= m_libCec
->ToString((cec_menu_state
)iState
);
506 return gcnew
String(retVal
);
509 String
^ ToString(CecDeckControlMode iMode
)
511 const char *retVal
= m_libCec
->ToString((cec_deck_control_mode
)iMode
);
512 return gcnew
String(retVal
);
515 String
^ ToString(CecDeckInfo status
)
517 const char *retVal
= m_libCec
->ToString((cec_deck_info
)status
);
518 return gcnew
String(retVal
);
521 String
^ ToString(CecOpcode opcode
)
523 const char *retVal
= m_libCec
->ToString((cec_opcode
)opcode
);
524 return gcnew
String(retVal
);
527 String
^ ToString(CecSystemAudioStatus mode
)
529 const char *retVal
= m_libCec
->ToString((cec_system_audio_status
)mode
);
530 return gcnew
String(retVal
);
533 String
^ ToString(CecAudioStatus status
)
535 const char *retVal
= m_libCec
->ToString((cec_audio_status
)status
);
536 return gcnew
String(retVal
);
539 String
^ ToString(CecClientVersion version
)
541 const char *retVal
= m_libCec
->ToString((cec_client_version
)version
);
542 return gcnew
String(retVal
);
545 String
^ ToString(CecServerVersion version
)
547 const char *retVal
= m_libCec
->ToString((cec_server_version
)version
);
548 return gcnew
String(retVal
);
552 ICECAdapter
* m_libCec
;