cec: fix 104125dc8316fb58253c34f417ded1e85b22c9f8
[deb_libcec.git] / src / LibCecSharp / LibCecSharp.cpp
CommitLineData
61f3c2ad
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
5d6aa900 4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
61f3c2ad
LOK
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
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.
13 *
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.
18 *
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.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
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/
31 */
32
4ef3b314 33#include "CecSharpTypes.h"
61f3c2ad
LOK
34#using <System.dll>
35
36using namespace System;
0cac9547 37using namespace System::Runtime::InteropServices;
61f3c2ad
LOK
38using namespace CEC;
39using namespace msclr::interop;
40
4ef3b314 41namespace CecSharp
61f3c2ad 42{
4ef3b314
LOK
43 public ref class LibCecSharp : public CecCallbackMethods
44 {
45 public:
41297a45
LOK
46 LibCecSharp(LibCECConfiguration ^config)
47 {
48 m_configuration = config;
46fb055c 49 CecCallbackMethods::EnableCallbacks(m_configuration->Callbacks);
41297a45
LOK
50 if (!InitialiseLibCec())
51 throw gcnew Exception("Could not initialise LibCecSharp");
52 }
53
4ef3b314 54 LibCecSharp(String ^ strDeviceName, CecDeviceTypeList ^ deviceTypes)
41297a45
LOK
55 {
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");
62 }
63
64 ~LibCecSharp(void)
65 {
66 Close();
67 m_libCec = NULL;
68 }
69
70 private:
71 !LibCecSharp(void)
72 {
73 Close();
74 m_libCec = NULL;
75 }
76
77 bool InitialiseLibCec(void)
4ef3b314
LOK
78 {
79 marshal_context ^ context = gcnew marshal_context();
41297a45
LOK
80 libcec_configuration config;
81 GetConfiguration(context, config);
4ef3b314 82
41297a45 83 m_libCec = (ICECAdapter *) CECInitialise(&config);
4ef3b314
LOK
84
85 delete context;
41297a45
LOK
86 return m_libCec != NULL;
87 }
88
89 void GetConfiguration(marshal_context ^context, libcec_configuration &config)
90 {
91 config.Clear();
92
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];
96
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;
4ef3b314 107 }
4ef3b314
LOK
108
109 public:
110 array<CecAdapter ^> ^ FindAdapters(String ^ path)
111 {
112 cec_adapter *devices = new cec_adapter[10];
113
114 marshal_context ^ context = gcnew marshal_context();
115 const char* strPathC = path->Length > 0 ? context->marshal_as<const char*>(path) : NULL;
116
117 uint8_t iDevicesFound = m_libCec->FindAdapters(devices, 10, NULL);
118
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));
122
123 delete devices;
124 delete context;
125 return adapters;
126 }
127
128 bool Open(String ^ strPort, int iTimeoutMs)
129 {
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);
133 delete context;
134 return bReturn;
135 }
136
137 void Close(void)
138 {
139 m_libCec->Close();
140 }
141
142 virtual bool EnableCallbacks(CecCallbackMethods ^ callbacks) override
143 {
144 if (m_libCec && CecCallbackMethods::EnableCallbacks(callbacks))
145 return m_libCec->EnableCallbacks(NULL, &g_cecCallbacks);
146
147 return false;
148 }
149
150 bool PingAdapter(void)
151 {
152 return m_libCec->PingAdapter();
153 }
154
155 bool StartBootloader(void)
156 {
157 return m_libCec->StartBootloader();
158 }
159
160 int GetMinLibVersion(void)
161 {
162 return m_libCec->GetMinLibVersion();
163 }
164
165 int GetLibVersionMajor(void)
166 {
167 return m_libCec->GetLibVersionMajor();
168 }
169
170 int GetLibVersionMinor(void)
171 {
172 return m_libCec->GetLibVersionMinor();
173 }
174
175 CecLogMessage ^ GetNextLogMessage(void)
176 {
177 cec_log_message msg;
178 if (m_libCec->GetNextLogMessage(&msg))
179 {
180 return gcnew CecLogMessage(gcnew String(msg.message), (CecLogLevel)msg.level, msg.time);
181 }
182
183 return gcnew CecLogMessage();
184 }
185
186 CecKeypress ^ GetNextKeypress(void)
187 {
188 cec_keypress key;
189 if (m_libCec->GetNextKeypress(&key))
190 {
191 return gcnew CecKeypress(key.keycode, key.duration);
192 }
193
194 return gcnew CecKeypress();
195 }
196
197 CecCommand ^ GetNextCommand(void)
198 {
199 cec_command command;
200 if (m_libCec->GetNextCommand(&command))
201 {
202 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);
203 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
204 retVal->Parameters->PushBack(command.parameters[iPtr]);
205 return retVal;
206 }
207
208 return gcnew CecCommand();
209 }
210
211 bool Transmit(CecCommand ^ command)
212 {
213 cec_command ccommand;
214 cec_command::Format(ccommand, (cec_logical_address)command->Initiator, (cec_logical_address)command->Destination, (cec_opcode)command->Opcode);
215 ccommand.transmit_timeout = command->TransmitTimeout;
216 ccommand.eom = command->Eom;
217 ccommand.ack = command->Ack;
218 for (unsigned int iPtr = 0; iPtr < command->Parameters->Size; iPtr++)
219 ccommand.parameters.PushBack(command->Parameters->Data[iPtr]);
220
221 return m_libCec->Transmit(ccommand);
222 }
223
224 bool SetLogicalAddress(CecLogicalAddress logicalAddress)
225 {
226 return m_libCec->SetLogicalAddress((cec_logical_address) logicalAddress);
227 }
228
006b76b9 229 bool SetPhysicalAddress(uint16_t physicalAddress)
4ef3b314
LOK
230 {
231 return m_libCec->SetPhysicalAddress(physicalAddress);
232 }
233
234 bool PowerOnDevices(CecLogicalAddress logicalAddress)
235 {
236 return m_libCec->PowerOnDevices((cec_logical_address) logicalAddress);
237 }
238
239 bool StandbyDevices(CecLogicalAddress logicalAddress)
240 {
241 return m_libCec->StandbyDevices((cec_logical_address) logicalAddress);
242 }
243
244 bool PollDevice(CecLogicalAddress logicalAddress)
245 {
246 return m_libCec->PollDevice((cec_logical_address) logicalAddress);
247 }
248
249 bool SetActiveSource(CecDeviceType type)
250 {
251 return m_libCec->SetActiveSource((cec_device_type) type);
252 }
253
254 bool SetDeckControlMode(CecDeckControlMode mode, bool sendUpdate)
255 {
256 return m_libCec->SetDeckControlMode((cec_deck_control_mode) mode, sendUpdate);
257 }
258
259 bool SetDeckInfo(CecDeckInfo info, bool sendUpdate)
260 {
261 return m_libCec->SetDeckInfo((cec_deck_info) info, sendUpdate);
262 }
263
264 bool SetInactiveView(void)
265 {
266 return m_libCec->SetInactiveView();
267 }
268
269 bool SetMenuState(CecMenuState state, bool sendUpdate)
270 {
271 return m_libCec->SetMenuState((cec_menu_state) state, sendUpdate);
272 }
273
274 bool SetOSDString(CecLogicalAddress logicalAddress, CecDisplayControl duration, String ^ message)
275 {
276 marshal_context ^ context = gcnew marshal_context();
277 const char* strMessageC = context->marshal_as<const char*>(message);
278
279 bool bReturn = m_libCec->SetOSDString((cec_logical_address) logicalAddress, (cec_display_control) duration, strMessageC);
280
281 delete context;
282 return bReturn;
283 }
284
285 bool SwitchMonitoring(bool enable)
286 {
287 return m_libCec->SwitchMonitoring(enable);
288 }
289
290 CecVersion GetDeviceCecVersion(CecLogicalAddress logicalAddress)
291 {
292 return (CecVersion) m_libCec->GetDeviceCecVersion((cec_logical_address) logicalAddress);
293 }
294
295 String ^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress)
296 {
297 cec_menu_language lang;
298 if (m_libCec->GetDeviceMenuLanguage((cec_logical_address) logicalAddress, &lang))
299 {
300 return gcnew String(lang.language);
301 }
302
303 return gcnew String("");
304 }
305
306 CecVendorId GetDeviceVendorId(CecLogicalAddress logicalAddress)
307 {
308 return (CecVendorId)m_libCec->GetDeviceVendorId((cec_logical_address) logicalAddress);
309 }
310
311 CecPowerStatus GetDevicePowerStatus(CecLogicalAddress logicalAddress)
312 {
313 return (CecPowerStatus) m_libCec->GetDevicePowerStatus((cec_logical_address) logicalAddress);
314 }
315
316 CecLogicalAddresses ^ GetActiveDevices(void)
317 {
318 CecLogicalAddresses ^ retVal = gcnew CecLogicalAddresses();
319 unsigned int iDevices = 0;
320
321 cec_logical_addresses activeDevices = m_libCec->GetActiveDevices();
322
323 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
324 if (activeDevices[iPtr])
325 retVal->Addresses[iDevices++] = (CecLogicalAddress)iPtr;
326
327 return retVal;
328 }
329
330 bool IsActiveDevice(CecLogicalAddress logicalAddress)
331 {
332 return m_libCec->IsActiveDevice((cec_logical_address)logicalAddress);
333 }
334
335 bool IsActiveDeviceType(CecDeviceType type)
336 {
337 return m_libCec->IsActiveDeviceType((cec_device_type)type);
338 }
339
340 bool SetHDMIPort(CecLogicalAddress address, uint8_t port)
341 {
342 return m_libCec->SetHDMIPort((cec_logical_address)address, port);
343 }
344
345 uint8_t VolumeUp(bool wait)
346 {
347 return m_libCec->VolumeUp(wait);
348 }
349
350 uint8_t VolumeDown(bool wait)
351 {
352 return m_libCec->VolumeDown(wait);
353 }
354
355 uint8_t MuteAudio(bool wait)
356 {
357 return m_libCec->MuteAudio(wait);
358 }
359
360 bool SendKeypress(CecLogicalAddress destination, CecUserControlCode key, bool wait)
361 {
362 return m_libCec->SendKeypress((cec_logical_address)destination, (cec_user_control_code)key, wait);
363 }
364
365 bool SendKeyRelease(CecLogicalAddress destination, bool wait)
366 {
367 return m_libCec->SendKeyRelease((cec_logical_address)destination, wait);
368 }
369
370 String ^ GetDeviceOSDName(CecLogicalAddress logicalAddress)
371 {
372 cec_osd_name osd = m_libCec->GetDeviceOSDName((cec_logical_address) logicalAddress);
373 return gcnew String(osd.name);
374 }
375
376 CecLogicalAddress GetActiveSource()
377 {
378 return (CecLogicalAddress)m_libCec->GetActiveSource();
379 }
380
381 bool IsActiveSource(CecLogicalAddress logicalAddress)
382 {
383 return m_libCec->IsActiveSource((cec_logical_address)logicalAddress);
384 }
385
386 uint16_t GetDevicePhysicalAddress(CecLogicalAddress iAddress)
387 {
388 return m_libCec->GetDevicePhysicalAddress((cec_logical_address)iAddress);
389 }
390
a9fb46b4
LOK
391 bool SetStreamPath(CecLogicalAddress iAddress)
392 {
393 return m_libCec->SetStreamPath((cec_logical_address)iAddress);
394 }
395
396 bool SetStreamPath(uint16_t iPhysicalAddress)
397 {
398 return m_libCec->SetStreamPath(iPhysicalAddress);
399 }
400
401 CecLogicalAddresses ^GetLogicalAddresses(void)
402 {
403 CecLogicalAddresses ^addr = gcnew CecLogicalAddresses();
404 cec_logical_addresses libAddr = m_libCec->GetLogicalAddresses();
405 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
406 addr->Addresses[iPtr] = (CecLogicalAddress)libAddr.addresses[iPtr];
407 addr->Primary = (CecLogicalAddress)libAddr.primary;
408 return addr;
409 }
410
5085a852
LOK
411 bool GetCurrentConfiguration(LibCECConfiguration ^configuration)
412 {
413 libcec_configuration config;
006b76b9
LOK
414 config.Clear();
415
5085a852
LOK
416 if (m_libCec->GetCurrentConfiguration(&config))
417 {
418 configuration->BaseDevice = (CecLogicalAddress)config.baseDevice;
419 configuration->DeviceName = gcnew String(config.strDeviceName);
420 configuration->HDMIPort = config.iHDMIPort;
421 configuration->PhysicalAddress = config.iPhysicalAddress;
422 configuration->PowerOffOnStandby = config.bPowerOffOnStandby == 1;
423 configuration->PowerOffScreensaver = config.bPowerOffScreensaver == 1;
424 configuration->PowerOffShutdown = config.bPowerOffShutdown == 1;
425 configuration->PowerOnStartup = config.bPowerOnStartup == 1;
426 configuration->UseTVMenuLanguage = config.bUseTVMenuLanguage == 1;
427 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
428 m_configuration->DeviceTypes->Types[iPtr] = (CecDeviceType)config.deviceTypes.types[iPtr];
429 return true;
430 }
431 return false;
432 }
433
434 bool CanPersistConfiguration(void)
435 {
436 return m_libCec->CanPersistConfiguration();
437 }
438
439 bool PersistConfiguration(LibCECConfiguration ^configuration)
440 {
441 marshal_context ^ context = gcnew marshal_context();
442 libcec_configuration config;
443 GetConfiguration(context, config);
444
445 bool bReturn = m_libCec->PersistConfiguration(&config);
446
447 delete context;
448 return bReturn;
449 }
450
4ef3b314
LOK
451 String ^ ToString(CecLogicalAddress iAddress)
452 {
453 const char *retVal = m_libCec->ToString((cec_logical_address)iAddress);
454 return gcnew String(retVal);
455 }
456
457 String ^ ToString(CecVendorId iVendorId)
458 {
459 const char *retVal = m_libCec->ToString((cec_vendor_id)iVendorId);
460 return gcnew String(retVal);
461 }
462
463 String ^ ToString(CecVersion iVersion)
464 {
465 const char *retVal = m_libCec->ToString((cec_version)iVersion);
466 return gcnew String(retVal);
467 }
468
469 String ^ ToString(CecPowerStatus iState)
470 {
471 const char *retVal = m_libCec->ToString((cec_power_status)iState);
472 return gcnew String(retVal);
473 }
474
475 String ^ ToString(CecMenuState iState)
476 {
477 const char *retVal = m_libCec->ToString((cec_menu_state)iState);
478 return gcnew String(retVal);
479 }
480
481 String ^ ToString(CecDeckControlMode iMode)
482 {
483 const char *retVal = m_libCec->ToString((cec_deck_control_mode)iMode);
484 return gcnew String(retVal);
485 }
486
487 String ^ ToString(CecDeckInfo status)
488 {
489 const char *retVal = m_libCec->ToString((cec_deck_info)status);
490 return gcnew String(retVal);
491 }
492
493 String ^ ToString(CecOpcode opcode)
494 {
495 const char *retVal = m_libCec->ToString((cec_opcode)opcode);
496 return gcnew String(retVal);
497 }
498
499 String ^ ToString(CecSystemAudioStatus mode)
500 {
501 const char *retVal = m_libCec->ToString((cec_system_audio_status)mode);
502 return gcnew String(retVal);
503 }
504
505 String ^ ToString(CecAudioStatus status)
506 {
507 const char *retVal = m_libCec->ToString((cec_audio_status)status);
508 return gcnew String(retVal);
509 }
510
511 private:
512 ICECAdapter * m_libCec;
41297a45 513 LibCECConfiguration ^m_configuration;
4ef3b314 514 };
0cac9547 515}